commit b729d413c5c77919ba8cd901dabe8e5848895c50
parent cc80610c81849e4eafa6cb767e8434a2052fb7d7
Author: lash <dev@holbrook.no>
Date: Fri, 2 May 2025 10:26:39 +0100
Add content length header, HEAD request handler
Diffstat:
4 files changed, 83 insertions(+), 1 deletion(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -1453,7 +1453,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wala"
-version = "0.1.8-beta.1"
+version = "0.1.8-beta.2"
dependencies = [
"ascii",
"base64 0.13.1",
diff --git a/src/record.rs b/src/record.rs
@@ -50,6 +50,8 @@ pub enum RequestResultType {
pub struct RequestResult {
/// Result code of the request.
pub typ: RequestResultType,
+ /// Contains the length of the body of a GET or HEAD request.
+ pub l: Option<u64>,
/// Contains the result body (reference string) of a PUT request.
pub v: Option<String>,
/// Contains the result body (as a reader) of a GET request.
@@ -69,6 +71,7 @@ impl RequestResult {
RequestResult {
typ: typ,
v: None,
+ l: None,
f: None,
m: None,
n: None,
@@ -96,6 +99,11 @@ impl RequestResult {
self.s = Some(s);
self
}
+
+ pub fn with_length(mut self, l: u64) -> RequestResult {
+ self.l = Some(l);
+ self
+ }
}
impl fmt::Display for RequestResult {
diff --git a/src/request.rs b/src/request.rs
@@ -124,6 +124,42 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
},
};
},
+ Method::Head => {
+ if &url == "" {
+ let mut res = RequestResult::new(RequestResultType::RecordError);
+ res = res.with_content(String::new());
+ return res;
+ }
+ let digest = match hex::decode(&url) {
+ Err(e) => {
+ let err_str = format!("{}", e);
+ let mut res = RequestResult::new(RequestResultType::InputError);
+ res = res.with_content(String::from(err_str));
+ return res;
+ },
+ Ok(v) => {
+ v
+ },
+ };
+
+ let full_path_buf = path.join(&url);
+ debug!("url {} resolved to {:?}", &url, &full_path_buf);
+ let mut res = RequestResult::new(RequestResultType::Found);
+ match full_path_buf.metadata() {
+ Ok(m) => {
+ res = res.with_length(m.len());
+ #[cfg(feature = "meta")]
+ {
+ res.m = get_meta_type(path, &digest);
+ res.n = get_meta_filename(path, &digest);
+ }
+ },
+ Err(e) => {
+ res = RequestResult::new(RequestResultType::ReadError);
+ },
+ }
+ return res;
+ },
_ => {},
};
RequestResult::new(RequestResultType::InputError)
diff --git a/src/response.rs b/src/response.rs
@@ -119,6 +119,18 @@ pub fn exec_response(req: Request, r: RequestResult) {
for v in auth_origin_headers.iter() {
res.add_header(v.clone());
}
+ match r.l {
+ Some(l) => {
+ let h = Header{
+ field: HeaderField::from_str("Content-Length").unwrap(),
+ value: AsciiString::from_ascii(l.to_string()).unwrap(),
+ };
+ res.add_header(h);
+ },
+ _ => {
+ },
+ };
+
req.respond(res);
return;
},
@@ -170,6 +182,18 @@ pub fn exec_response(req: Request, r: RequestResult) {
},
};
+ match r.l {
+ Some(l) => {
+ let h = Header{
+ field: HeaderField::from_str("Content-Length").unwrap(),
+ value: AsciiString::from_ascii(l.to_string()).unwrap(),
+ };
+ res.add_header(h);
+ },
+ _ => {
+ },
+ };
+
res = res.with_status_code(res_status);
for v in auth_origin_headers.iter() {
res.add_header(v.clone());
@@ -182,12 +206,26 @@ pub fn exec_response(req: Request, r: RequestResult) {
for v in auth_origin_headers.iter() {
res.add_header(v.clone());
}
+
+ match r.l {
+ Some(l) => {
+ let h = Header{
+ field: HeaderField::from_str("Content-Length").unwrap(),
+ value: AsciiString::from_ascii(l.to_string()).unwrap(),
+ };
+ res.add_header(h);
+ },
+ _ => {
+ },
+ };
+
req.respond(res);
return;
},
}
}
}
+
}
#[cfg(test)]