1
1
use std:: {
2
2
fmt:: Debug ,
3
- net:: { AddrParseError , SocketAddr } ,
3
+ net:: SocketAddr ,
4
4
} ;
5
5
6
6
use bytes:: { Bytes , BytesMut } ;
7
- use url:: { ParseError , Url } ;
7
+ use url:: Url ;
8
8
9
- use crate :: Headers ;
9
+ use super :: Headers ;
10
10
11
11
/// Represents an HTTP request. Includes the method, URL, headers, and body.
12
12
///
@@ -17,7 +17,7 @@ use crate::Headers;
17
17
///
18
18
/// let request = Request::builder()
19
19
/// .method("POST")
20
- /// .url("http://example.com/test.php").expect("invalid url")
20
+ /// .url("http://example.com/test.php")
21
21
/// .header("Accept", "text/html")
22
22
/// .header("Accept", "application/json")
23
23
/// .header("Host", "example.com")
@@ -94,7 +94,7 @@ impl Request {
94
94
///
95
95
/// let request = Request::builder()
96
96
/// .method("POST")
97
- /// .url("http://example.com/test.php").expect("invalid url")
97
+ /// .url("http://example.com/test.php")
98
98
/// .header("Content-Type", "text/html")
99
99
/// .header("Content-Length", 13.to_string())
100
100
/// .body("Hello, World!")
@@ -120,7 +120,7 @@ impl Request {
120
120
///
121
121
/// let request = Request::builder()
122
122
/// .method("GET")
123
- /// .url("http://example.com/test.php").expect("invalid url")
123
+ /// .url("http://example.com/test.php")
124
124
/// .header("Content-Type", "text/plain")
125
125
/// .build()
126
126
/// .expect("should build request");
@@ -282,13 +282,19 @@ impl Request {
282
282
#[ derive( Debug , PartialEq ) ]
283
283
pub enum RequestBuilderException {
284
284
/// Url is required
285
- MissingUrl ,
285
+ UrlMissing ,
286
+ /// Url could not be parsed
287
+ UrlParseFailed ( String ) ,
288
+ /// SocketAddr could not be parsed
289
+ SocketParseFailed ( String )
286
290
}
287
291
288
292
impl std:: fmt:: Display for RequestBuilderException {
289
293
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
290
294
match self {
291
- RequestBuilderException :: MissingUrl => write ! ( f, "Expected url to be set" ) ,
295
+ RequestBuilderException :: UrlMissing => write ! ( f, "Expected url to be set" ) ,
296
+ RequestBuilderException :: UrlParseFailed ( u) => write ! ( f, "Failed to parse url: \" {}\" " , u) ,
297
+ RequestBuilderException :: SocketParseFailed ( s) => write ! ( f, "Failed to parse socket info: \" {}\" " , s)
292
298
}
293
299
}
294
300
}
@@ -302,7 +308,7 @@ impl std::fmt::Display for RequestBuilderException {
302
308
///
303
309
/// let request = Request::builder()
304
310
/// .method("POST")
305
- /// .url("http://example.com/test.php").expect("invalid url")
311
+ /// .url("http://example.com/test.php")
306
312
/// .header("Content-Type", "text/html")
307
313
/// .header("Content-Length", 13.to_string())
308
314
/// .body("Hello, World!")
@@ -318,11 +324,11 @@ impl std::fmt::Display for RequestBuilderException {
318
324
#[ derive( Clone ) ]
319
325
pub struct RequestBuilder {
320
326
method : Option < String > ,
321
- url : Option < Url > ,
327
+ url : Option < String > ,
322
328
headers : Headers ,
323
329
body : BytesMut ,
324
- local_socket : Option < SocketAddr > ,
325
- remote_socket : Option < SocketAddr > ,
330
+ local_socket : Option < String > ,
331
+ remote_socket : Option < String > ,
326
332
}
327
333
328
334
impl RequestBuilder {
@@ -377,11 +383,11 @@ impl RequestBuilder {
377
383
pub fn extend ( request : & Request ) -> Self {
378
384
Self {
379
385
method : Some ( request. method ( ) . into ( ) ) ,
380
- url : Some ( request. url ( ) . clone ( ) ) ,
386
+ url : Some ( request. url ( ) . to_string ( ) ) ,
381
387
headers : request. headers ( ) . clone ( ) ,
382
388
body : BytesMut :: from ( request. body ( ) ) ,
383
- local_socket : request. local_socket ,
384
- remote_socket : request. remote_socket ,
389
+ local_socket : request. local_socket . map ( |s| s . to_string ( ) ) ,
390
+ remote_socket : request. remote_socket . map ( |s| s . to_string ( ) ) ,
385
391
}
386
392
}
387
393
@@ -394,7 +400,7 @@ impl RequestBuilder {
394
400
///
395
401
/// let request = RequestBuilder::new()
396
402
/// .method("POST")
397
- /// .url("http://example.com/test.php").expect("invalid url")
403
+ /// .url("http://example.com/test.php")
398
404
/// .build()
399
405
/// .expect("should build request");
400
406
///
@@ -413,23 +419,18 @@ impl RequestBuilder {
413
419
/// use lang_handler::RequestBuilder;
414
420
///
415
421
/// let request = RequestBuilder::new()
416
- /// .url("http://example.com/test.php").expect("invalid url")
422
+ /// .url("http://example.com/test.php")
417
423
/// .build()
418
424
/// .expect("should build request");
419
425
///
420
426
/// assert_eq!(request.url().as_str(), "http://example.com/test.php");
421
427
/// ```
422
- pub fn url < T > ( mut self , url : T ) -> Result < Self , ParseError >
428
+ pub fn url < T > ( mut self , url : T ) -> Self
423
429
where
424
430
T : Into < String > ,
425
431
{
426
- match url. into ( ) . parse ( ) {
427
- Ok ( url) => {
428
- self . url = Some ( url) ;
429
- Ok ( self )
430
- }
431
- Err ( e) => Err ( e) ,
432
- }
432
+ self . url = Some ( url. into ( ) ) ;
433
+ self
433
434
}
434
435
435
436
/// Sets a header of the request.
@@ -440,7 +441,7 @@ impl RequestBuilder {
440
441
/// use lang_handler::RequestBuilder;
441
442
///
442
443
/// let request = RequestBuilder::new()
443
- /// .url("http://example.com/test.php").expect("invalid url")
444
+ /// .url("http://example.com/test.php")
444
445
/// .header("Accept", "text/html")
445
446
/// .build()
446
447
/// .expect("should build request");
@@ -464,7 +465,7 @@ impl RequestBuilder {
464
465
/// use lang_handler::RequestBuilder;
465
466
///
466
467
/// let request = RequestBuilder::new()
467
- /// .url("http://example.com/test.php").expect("invalid url")
468
+ /// .url("http://example.com/test.php")
468
469
/// .body("Hello, World!")
469
470
/// .build()
470
471
/// .expect("should build request");
@@ -485,8 +486,8 @@ impl RequestBuilder {
485
486
/// use lang_handler::RequestBuilder;
486
487
///
487
488
/// let request = RequestBuilder::new()
488
- /// .url("http://example.com/test.php").expect("invalid url")
489
- /// .local_socket("127.0.0.1:8080").expect("invalid local socket")
489
+ /// .url("http://example.com/test.php")
490
+ /// .local_socket("127.0.0.1:8080")
490
491
/// .build()
491
492
/// .expect("should build request");
492
493
///
@@ -495,17 +496,12 @@ impl RequestBuilder {
495
496
/// .expect("should parse");
496
497
/// assert_eq!(request.local_socket(), Some(expected));
497
498
/// ```
498
- pub fn local_socket < T > ( mut self , local_socket : T ) -> Result < Self , AddrParseError >
499
+ pub fn local_socket < T > ( mut self , local_socket : T ) -> Self
499
500
where
500
501
T : Into < String > ,
501
502
{
502
- match local_socket. into ( ) . parse ( ) {
503
- Err ( e) => Err ( e) ,
504
- Ok ( local_socket) => {
505
- self . local_socket = Some ( local_socket) ;
506
- Ok ( self )
507
- }
508
- }
503
+ self . local_socket = Some ( local_socket. into ( ) ) ;
504
+ self
509
505
}
510
506
511
507
/// Sets the remote socket of the request.
@@ -517,8 +513,8 @@ impl RequestBuilder {
517
513
/// use lang_handler::RequestBuilder;
518
514
///
519
515
/// let request = RequestBuilder::new()
520
- /// .url("http://example.com/test.php").expect("invalid url")
521
- /// .remote_socket("127.0.0.1:8080").expect("invalid remote socket")
516
+ /// .url("http://example.com/test.php")
517
+ /// .remote_socket("127.0.0.1:8080")
522
518
/// .build()
523
519
/// .expect("should build request");
524
520
///
@@ -527,17 +523,12 @@ impl RequestBuilder {
527
523
/// .expect("should parse");
528
524
/// assert_eq!(request.remote_socket(), Some(expected));
529
525
/// ```
530
- pub fn remote_socket < T > ( mut self , remote_socket : T ) -> Result < Self , AddrParseError >
526
+ pub fn remote_socket < T > ( mut self , remote_socket : T ) -> Self
531
527
where
532
528
T : Into < String > ,
533
529
{
534
- match remote_socket. into ( ) . parse ( ) {
535
- Err ( e) => Err ( e) ,
536
- Ok ( remote_socket) => {
537
- self . remote_socket = Some ( remote_socket) ;
538
- Ok ( self )
539
- }
540
- }
530
+ self . remote_socket = Some ( remote_socket. into ( ) ) ;
531
+ self
541
532
}
542
533
543
534
/// Builds the request.
@@ -548,7 +539,7 @@ impl RequestBuilder {
548
539
/// use lang_handler::RequestBuilder;
549
540
///
550
541
/// let request = RequestBuilder::new()
551
- /// .url("http://example.com/test.php").expect("invalid url")
542
+ /// .url("http://example.com/test.php")
552
543
/// .build()
553
544
/// .expect("should build request");
554
545
///
@@ -559,11 +550,11 @@ impl RequestBuilder {
559
550
pub fn build ( self ) -> Result < Request , RequestBuilderException > {
560
551
Ok ( Request {
561
552
method : self . method . unwrap_or_else ( || "GET" . to_string ( ) ) ,
562
- url : self . url . ok_or ( RequestBuilderException :: MissingUrl ) ?,
553
+ url : parse_url ( self . url ) ?,
563
554
headers : self . headers ,
564
555
body : self . body . freeze ( ) ,
565
- local_socket : self . local_socket ,
566
- remote_socket : self . remote_socket ,
556
+ local_socket : parse_socket ( self . local_socket ) ? ,
557
+ remote_socket : parse_socket ( self . remote_socket ) ?
567
558
} )
568
559
}
569
560
}
@@ -573,3 +564,21 @@ impl Default for RequestBuilder {
573
564
Self :: new ( )
574
565
}
575
566
}
567
+
568
+ fn parse_url ( url : Option < String > ) -> Result < Url , RequestBuilderException > {
569
+ url
570
+ . ok_or ( RequestBuilderException :: UrlMissing )
571
+ . and_then ( |u| {
572
+ u. parse ( )
573
+ . map_err ( |_| RequestBuilderException :: UrlParseFailed ( u) )
574
+ } )
575
+ }
576
+
577
+ fn parse_socket ( socket : Option < String > ) -> Result < Option < SocketAddr > , RequestBuilderException > {
578
+ socket. map_or_else ( || Ok ( None ) , |s| {
579
+ Ok ( Some (
580
+ s. parse :: < SocketAddr > ( )
581
+ . map_err ( |_| RequestBuilderException :: SocketParseFailed ( s) ) ?
582
+ ) )
583
+ } )
584
+ }
0 commit comments