about summary refs log tree commit diff
path: root/users/Profpatsch/read-http
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-01-29T14·45+0100
committerProfpatsch <mail@profpatsch.de>2021-01-31T11·10+0000
commitf05bece2cb240090a4eb40071daa69b857c0663c (patch)
tree99619860899db0db3333710f83d8e479dedb410c /users/Profpatsch/read-http
parentef743ffbb5f03b0503edc0ed6c569dd17f391289 (diff)
refactor(users/Profpatsch/read-http): parse headers as utf8 r/2169
Headers should always be ASCII, so let’s crash if they are not. The
thing gets a lot easier to use, and clients who fail this restriction
can just fuck off.

Also actually print the results to stdout instead of stderr …

Change-Id: I782c96c537ae11b541175e96453c4114e0a71b05
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2451
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/read-http')
-rw-r--r--users/Profpatsch/read-http/read-http.rs20
1 files changed, 11 insertions, 9 deletions
diff --git a/users/Profpatsch/read-http/read-http.rs b/users/Profpatsch/read-http/read-http.rs
index 6d419542c707..a43bb7d3b20b 100644
--- a/users/Profpatsch/read-http/read-http.rs
+++ b/users/Profpatsch/read-http/read-http.rs
@@ -60,12 +60,14 @@ fn main() -> std::io::Result<()> {
     }
 
 
-    fn lowercase_headers<'a>(headers: &'a [httparse::Header]) -> Vec<(String, &'a [u8])> {
+    fn normalize_headers<'a>(headers: &'a [httparse::Header]) -> Vec<(String, &'a str)> {
         let mut res = vec![];
         for httparse::Header { name, value } in headers {
+            let val = std::str::from_utf8(*value)
+                .expect(&format!("read-http: we require header values to be UTF-8 (they should be ASCII), but the header {} was {:?}", name, value));
             // lowercase the headers, since the standard doesn’t care
             // and we want unique strings to match agains
-            res.push((name.to_lowercase(), *value))
+            res.push((name.to_lowercase(), val))
         }
         res
     }
@@ -105,7 +107,7 @@ fn main() -> std::io::Result<()> {
             }
             let method = req.method.expect("method must be filled on complete parse");
             let path = req.path.expect("path must be filled on complete parse");
-            write_dict_req(method, path, &lowercase_headers(req.headers))
+            write_dict_req(method, path, &normalize_headers(req.headers))
         },
         Response => {
             let mut resp = httparse::Response::new(&mut headers);
@@ -120,12 +122,12 @@ fn main() -> std::io::Result<()> {
             }
             let code = resp.code.expect("code must be filled on complete parse");
             let reason = resp.reason.expect("reason must be filled on complete parse");
-            write_dict_resp(code, reason, &lowercase_headers(resp.headers))
+            write_dict_resp(code, reason, &normalize_headers(resp.headers))
         }
     }
 }
 
-fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String, &[u8])]) -> std::io::Result<()> {
+fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> {
     let mut http = vec![
         ("method", U::Text(method.as_bytes())),
         ("path", U::Text(path.as_bytes())),
@@ -133,7 +135,7 @@ fn write_dict_req<'buf>(method: &'buf str, path: &'buf str, headers: &[(String,
     write_dict(http, headers)
 }
 
-fn write_dict_resp<'buf>(code: u16, reason: &'buf str, headers: &[(String, &[u8])]) -> std::io::Result<()> {
+fn write_dict_resp<'buf>(code: u16, reason: &'buf str, headers: &[(String, &str)]) -> std::io::Result<()> {
     let mut http = vec![
         ("status", U::N6(code as u64)),
         ("status-text", U::Text(reason.as_bytes())),
@@ -142,16 +144,16 @@ fn write_dict_resp<'buf>(code: u16, reason: &'buf str, headers: &[(String, &[u8]
 }
 
 
-fn write_dict<'buf, 'a>(mut http: Vec<(&str, U<'a>)>, headers: &'a[(String, &[u8])]) -> std::io::Result<()> {
+fn write_dict<'buf, 'a>(mut http: Vec<(&str, U<'a>)>, headers: &'a[(String, &str)]) -> std::io::Result<()> {
     http.push(("headers", U::Record(
         headers.iter().map(
             |(name, value)|
-            (name.as_str(), U::Binary(value))
+            (name.as_str(), U::Text(value.as_bytes()))
         ).collect::<Vec<_>>()
     )));
 
     netencode::encode(
-        &mut std::io::stderr(),
+        &mut std::io::stdout(),
         U::Record(http)
     )?;
     Ok(())