Skip to content

Commit bd8817f

Browse files
committed
http: move request fields into impls
1 parent ed446df commit bd8817f

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

src/http.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ pub enum HttpServerRequest {
4040

4141
#[derive(Debug, Serialize, Deserialize)]
4242
pub struct IncomingHttpRequest {
43-
pub source_socket_addr: Option<String>, // will parse to SocketAddr
44-
pub method: String, // will parse to http::Method
45-
pub raw_path: String,
46-
pub headers: HashMap<String, String>,
47-
pub query_params: HashMap<String, String>,
43+
source_socket_addr: Option<String>, // will parse to SocketAddr
44+
method: String, // will parse to http::Method
45+
url: String, // will parse to url::Url
46+
headers: HashMap<String, String>, // will parse to http::HeaderMap
47+
query_params: HashMap<String, String>,
4848
// BODY is stored in the lazy_load_blob, as bytes
4949
}
5050

@@ -189,13 +189,53 @@ impl HttpServerRequest {
189189

190190
impl IncomingHttpRequest {
191191
pub fn url(&self) -> anyhow::Result<url::Url> {
192-
url::Url::parse(&self.raw_path).map_err(|e| anyhow::anyhow!("couldn't parse url: {:?}", e))
192+
url::Url::parse(&self.url).map_err(|e| anyhow::anyhow!("couldn't parse url: {:?}", e))
193193
}
194194

195195
pub fn method(&self) -> anyhow::Result<http::Method> {
196196
http::Method::from_bytes(self.method.as_bytes())
197197
.map_err(|e| anyhow::anyhow!("couldn't parse method: {:?}", e))
198198
}
199+
200+
pub fn source_socket_addr(&self) -> anyhow::Result<std::net::SocketAddr> {
201+
match &self.source_socket_addr {
202+
Some(addr) => addr
203+
.parse()
204+
.map_err(|_| anyhow::anyhow!("Invalid format for socket address: {}", addr)),
205+
None => Err(anyhow::anyhow!("No source socket address provided")),
206+
}
207+
}
208+
209+
pub fn path(&self) -> anyhow::Result<String> {
210+
let url = url::Url::parse(&self.url)?;
211+
// skip the first path segment, which is the process ID.
212+
let path = url
213+
.path_segments()
214+
.ok_or(anyhow::anyhow!("url path missing process ID!"))?
215+
.skip(1)
216+
.collect::<Vec<&str>>()
217+
.join("/");
218+
Ok(format!("/{}", path))
219+
}
220+
221+
pub fn headers(&self) -> HeaderMap {
222+
let mut header_map = HeaderMap::new();
223+
for (key, value) in self.headers.iter() {
224+
let key_bytes = key.as_bytes();
225+
let Ok(key_name) = HeaderName::from_bytes(key_bytes) else {
226+
continue;
227+
};
228+
let Ok(value_header) = HeaderValue::from_str(&value) else {
229+
continue;
230+
};
231+
header_map.insert(key_name, value_header);
232+
}
233+
header_map
234+
}
235+
236+
pub fn query_params(&self) -> HashMap<String, String> {
237+
self.query_params.clone()
238+
}
199239
}
200240

201241
/// Request type that can be shared over WASM boundary to apps.

0 commit comments

Comments
 (0)