Skip to content

Commit 5305453

Browse files
authored
Merge pull request #31 from uqbar-dao/bp/http-pathfix
http: request impls
2 parents 9790c0f + 62a5ec9 commit 5305453

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/http.rs

Lines changed: 39 additions & 9 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

@@ -128,6 +128,7 @@ pub enum WsMessageType {
128128
Binary,
129129
Ping,
130130
Pong,
131+
Close,
131132
}
132133

133134
/// Part of the Response type issued by http_server
@@ -189,23 +190,52 @@ impl HttpServerRequest {
189190

190191
impl IncomingHttpRequest {
191192
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))
193+
url::Url::parse(&self.url).map_err(|e| anyhow::anyhow!("couldn't parse url: {:?}", e))
193194
}
194195

195196
pub fn method(&self) -> anyhow::Result<http::Method> {
196197
http::Method::from_bytes(self.method.as_bytes())
197198
.map_err(|e| anyhow::anyhow!("couldn't parse method: {:?}", e))
198199
}
199200

201+
pub fn source_socket_addr(&self) -> anyhow::Result<std::net::SocketAddr> {
202+
match &self.source_socket_addr {
203+
Some(addr) => addr
204+
.parse()
205+
.map_err(|_| anyhow::anyhow!("Invalid format for socket address: {}", addr)),
206+
None => Err(anyhow::anyhow!("No source socket address provided")),
207+
}
208+
}
209+
200210
pub fn path(&self) -> anyhow::Result<String> {
201-
let url = url::Url::parse(&self.raw_path)?;
211+
let url = url::Url::parse(&self.url)?;
202212
// skip the first path segment, which is the process ID.
203-
Ok(url
213+
let path = url
204214
.path_segments()
205215
.ok_or(anyhow::anyhow!("url path missing process ID!"))?
206216
.skip(1)
207217
.collect::<Vec<&str>>()
208-
.join("/"))
218+
.join("/");
219+
Ok(format!("/{}", path))
220+
}
221+
222+
pub fn headers(&self) -> HeaderMap {
223+
let mut header_map = HeaderMap::new();
224+
for (key, value) in self.headers.iter() {
225+
let key_bytes = key.as_bytes();
226+
let Ok(key_name) = HeaderName::from_bytes(key_bytes) else {
227+
continue;
228+
};
229+
let Ok(value_header) = HeaderValue::from_str(&value) else {
230+
continue;
231+
};
232+
header_map.insert(key_name, value_header);
233+
}
234+
header_map
235+
}
236+
237+
pub fn query_params(&self) -> &HashMap<String, String> {
238+
&self.query_params
209239
}
210240
}
211241

0 commit comments

Comments
 (0)