From ede3004cf563e4d46c0e8afa5fd1e18732de6c69 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 23 Jun 2020 03:39:52 +0200 Subject: From: Convert HTTP headers The HTTP headers from `fastcgi` come with keys in the following format: "HTTP_USER_AGENT", "HTTP_ACCEPT_LANGUAGE". Since most headers use an uppercase first letter of a word with hyphen word separators, make the conversion by: 1. Removing the `HTTP_` prefix 2. Changing `_` to `-` 3. Making the first letter of each word uppercase using the Inflector crate --- src/lib.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 88752fb..975308a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use std::io::Write; use http::{Request, Response}; use http::request; +use inflector::cases::traincase::to_train_case; pub fn run(handler: F) @@ -49,10 +50,33 @@ impl From for http::request::Builder { request.param("REQUEST_URI").unwrap_or("".to_owned()), ); - return http::request::Builder::new() + let mut http_request = http::request::Builder::new() .method(&*method) - .uri(&uri) + .uri(&uri); + + let headers = headers_from_params(request.params()); + for (k, v) in headers { + http_request = http_request.header(&k, &v); + } + + http_request // HTTP_* params become headers } } + +fn headers_from_params(params: fastcgi::Params) -> Vec<(String, String)> { + return params + .filter(|(key, _)| key.starts_with("HTTP_")) + .map(|(key, value)| { + let mut key = key.get(5..).unwrap_or("").to_owned(); + key = key.replace("_", "-"); + key = to_train_case(&key); + + // Change _ to - + // Uppercase each word + + (key, value) + }) + .collect() +} -- cgit v1.2.3