1
1
use crate :: io:: { empty, Empty } ;
2
2
3
3
use super :: { Body , IntoBody , Method } ;
4
- use url :: Url ;
4
+ use http :: uri :: Uri ;
5
5
use wasi:: http:: outgoing_handler:: OutgoingRequest ;
6
6
use wasi:: http:: types:: { Headers as WasiHeaders , Scheme } ;
7
7
8
8
/// An HTTP request
9
9
#[ derive( Debug ) ]
10
10
pub struct Request < B : Body > {
11
11
method : Method ,
12
- url : Url ,
12
+ uri : Uri ,
13
13
headers : WasiHeaders ,
14
14
body : B ,
15
15
}
16
16
17
17
impl Request < Empty > {
18
18
/// Create a new HTTP request to send off to the client.
19
- pub fn new ( method : Method , url : Url ) -> Self {
19
+ pub fn new ( method : Method , uri : Uri ) -> Self {
20
20
Self {
21
21
body : empty ( ) ,
22
22
method,
23
- url ,
23
+ uri ,
24
24
headers : WasiHeaders :: new ( ) ,
25
25
}
26
26
}
@@ -31,42 +31,47 @@ impl<B: Body> Request<B> {
31
31
pub fn set_body < C : IntoBody > ( self , body : C ) -> Request < C :: IntoBody > {
32
32
let Self {
33
33
method,
34
- url ,
34
+ uri ,
35
35
headers,
36
36
..
37
37
} = self ;
38
38
Request {
39
39
method,
40
- url ,
40
+ uri ,
41
41
headers,
42
42
body : body. into_body ( ) ,
43
43
}
44
44
}
45
45
46
- pub fn into_outgoing ( self ) -> ( OutgoingRequest , B ) {
46
+ pub ( crate ) fn into_outgoing ( self ) -> ( OutgoingRequest , B ) {
47
47
let wasi_req = OutgoingRequest :: new ( self . headers ) ;
48
48
49
49
// Set the HTTP method
50
- wasi_req. set_method ( & self . method . into ( ) ) . unwrap ( ) ;
50
+ wasi_req
51
+ . set_method ( & self . method . into ( ) )
52
+ . expect ( "method accepted by wasi-http implementation" ) ;
51
53
52
54
// Set the url scheme
53
- let scheme = match self . url . scheme ( ) {
54
- "http" => Scheme :: Http ,
55
- "https" => Scheme :: Https ,
56
- other => Scheme :: Other ( other. to_owned ( ) ) ,
55
+ let scheme = match self . uri . scheme ( ) . map ( |s| s . as_str ( ) ) {
56
+ Some ( "http" ) => Scheme :: Http ,
57
+ Some ( "https" ) | None => Scheme :: Https ,
58
+ Some ( other) => Scheme :: Other ( other. to_owned ( ) ) ,
57
59
} ;
58
- wasi_req. set_scheme ( Some ( & scheme) ) . unwrap ( ) ;
60
+ wasi_req
61
+ . set_scheme ( Some ( & scheme) )
62
+ . expect ( "scheme accepted by wasi-http implementation" ) ;
59
63
60
- // Set the url path + query string
61
- let path = match self . url . query ( ) {
62
- Some ( query) => format ! ( "{}?{query}" , self . url. path( ) ) ,
63
- None => self . url . path ( ) . to_owned ( ) ,
64
- } ;
65
- wasi_req. set_path_with_query ( Some ( & path) ) . unwrap ( ) ;
64
+ // Set authority
65
+ wasi_req
66
+ . set_authority ( self . uri . authority ( ) . map ( |a| a. as_str ( ) ) )
67
+ . expect ( "authority accepted by wasi-http implementation" ) ;
66
68
67
- // Not sure why we also have to set the authority, but sure we can do
68
- // that too!
69
- wasi_req. set_authority ( Some ( self . url . authority ( ) ) ) . unwrap ( ) ;
69
+ // Set the url path + query string
70
+ if let Some ( p_and_q) = self . uri . path_and_query ( ) {
71
+ wasi_req
72
+ . set_path_with_query ( Some ( & p_and_q. to_string ( ) ) )
73
+ . expect ( "path with query accepted by wasi-http implementation" )
74
+ }
70
75
71
76
// All done; request is ready for send-off
72
77
( wasi_req, self . body )
0 commit comments