1
1
use crate :: io:: { empty, Empty } ;
2
2
3
- use super :: { Body , IntoBody , Method } ;
3
+ use super :: { header_map_to_wasi , Body , Error , HeaderMap , IntoBody , Method , Result } ;
4
4
use http:: uri:: Uri ;
5
5
use wasi:: http:: outgoing_handler:: OutgoingRequest ;
6
- use wasi:: http:: types:: { Headers as WasiHeaders , Scheme } ;
6
+ use wasi:: http:: types:: Scheme ;
7
7
8
8
/// An HTTP request
9
9
#[ derive( Debug ) ]
10
10
pub struct Request < B : Body > {
11
11
method : Method ,
12
12
uri : Uri ,
13
- headers : WasiHeaders ,
13
+ headers : HeaderMap ,
14
14
body : B ,
15
15
}
16
16
@@ -21,12 +21,22 @@ impl Request<Empty> {
21
21
body : empty ( ) ,
22
22
method,
23
23
uri,
24
- headers : WasiHeaders :: new ( ) ,
24
+ headers : HeaderMap :: new ( ) ,
25
25
}
26
26
}
27
27
}
28
28
29
29
impl < B : Body > Request < B > {
30
+ /// Get the HTTP headers from the impl
31
+ pub fn headers ( & self ) -> & HeaderMap {
32
+ & self . headers
33
+ }
34
+
35
+ /// Mutably get the HTTP headers from the impl
36
+ pub fn headers_mut ( & mut self ) -> & mut HeaderMap {
37
+ & mut self . headers
38
+ }
39
+
30
40
/// Set an HTTP body.
31
41
pub fn set_body < C : IntoBody > ( self , body : C ) -> Request < C :: IntoBody > {
32
42
let Self {
@@ -43,13 +53,14 @@ impl<B: Body> Request<B> {
43
53
}
44
54
}
45
55
46
- pub ( crate ) fn into_outgoing ( self ) -> ( OutgoingRequest , B ) {
47
- let wasi_req = OutgoingRequest :: new ( self . headers ) ;
56
+ pub ( crate ) fn into_outgoing ( self ) -> Result < ( OutgoingRequest , B ) > {
57
+ let wasi_req = OutgoingRequest :: new ( header_map_to_wasi ( & self . headers ) ? ) ;
48
58
49
59
// Set the HTTP method
60
+ let method = self . method . into ( ) ;
50
61
wasi_req
51
- . set_method ( & self . method . into ( ) )
52
- . expect ( "method accepted by wasi-http implementation" ) ;
62
+ . set_method ( & method)
63
+ . map_err ( | ( ) | Error :: other ( format ! ( "method rejected by wasi-http: {method:?}" , ) ) ) ? ;
53
64
54
65
// Set the url scheme
55
66
let scheme = match self . uri . scheme ( ) . map ( |s| s. as_str ( ) ) {
@@ -59,21 +70,24 @@ impl<B: Body> Request<B> {
59
70
} ;
60
71
wasi_req
61
72
. set_scheme ( Some ( & scheme) )
62
- . expect ( "scheme accepted by wasi-http implementation" ) ;
73
+ . map_err ( | ( ) | Error :: other ( format ! ( "scheme rejected by wasi-http: {scheme:?}" ) ) ) ? ;
63
74
64
75
// Set authority
76
+ let authority = self . uri . authority ( ) . map ( |a| a. as_str ( ) ) ;
65
77
wasi_req
66
- . set_authority ( self . uri . authority ( ) . map ( |a| a . as_str ( ) ) )
67
- . expect ( "authority accepted by wasi-http implementation" ) ;
78
+ . set_authority ( authority)
79
+ . map_err ( | ( ) | Error :: other ( format ! ( "authority rejected by wasi-http {authority:?}" ) ) ) ? ;
68
80
69
81
// Set the url path + query string
70
82
if let Some ( p_and_q) = self . uri . path_and_query ( ) {
71
83
wasi_req
72
84
. set_path_with_query ( Some ( & p_and_q. to_string ( ) ) )
73
- . expect ( "path with query accepted by wasi-http implementation" )
85
+ . map_err ( |( ) | {
86
+ Error :: other ( format ! ( "path and query rejected by wasi-http {p_and_q:?}" ) )
87
+ } ) ?;
74
88
}
75
89
76
90
// All done; request is ready for send-off
77
- ( wasi_req, self . body )
91
+ Ok ( ( wasi_req, self . body ) )
78
92
}
79
93
}
0 commit comments