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