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