Skip to content

Commit 57a94da

Browse files
committed
fix: should match case-insensitve on header values #27
1 parent 2f3cb8c commit 57a94da

File tree

5 files changed

+137
-13
lines changed

5 files changed

+137
-13
lines changed

src/lib/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pub struct ProgramConfig {
1212
pub presets: Vec<PresetConfig>,
1313
}
1414

15+
impl Default for ProgramConfig {
16+
fn default() -> ProgramConfig {
17+
ProgramConfig { presets: vec![] }
18+
}
19+
}
20+
1521
impl FromFile for ProgramConfig {}
1622

1723
impl ProgramConfig {

src/lib/proxy_transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn get_host_port(incoming_request: &HttpRequest<AppState>, bind_port: u16) -
143143

144144
match (split.get(0), split.get(1)) {
145145
(Some(h), Some(p)) => (h.to_string(), p.parse().expect("parsed port")),
146-
(Some(h), None) => (h.to_string(), 80 as u16),
146+
(Some(h), None) => (h.to_string(), bind_port),
147147
_ => ("127.0.0.1".to_string(), bind_port),
148148
}
149149
}

src/lib/without_body.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,12 @@ fn should_rewrite_body(uri: &Uri, resp: &ClientResponse) -> bool {
140140
resp.headers()
141141
.get(header::CONTENT_TYPE)
142142
.map_or(false, |header_value| {
143-
match header_value.to_str().unwrap_or("") {
144-
"text/html" | "text/html; charset=UTF-8" => true,
145-
_ => false,
143+
match header_value.to_str().and_then(|s| Ok(s.to_lowercase())) {
144+
Ok(s) => match s.as_str() {
145+
"text/html" | "text/html; charset=utf-8" => true,
146+
_ => false,
147+
},
148+
Err(..) => false,
146149
}
147150
})
148151
}

tests/api.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ const DEFAULT_ARGS: &'static [&'static str] = &[
3232
fn test_config_json() {
3333
api(DEFAULT_ARGS.to_vec(), "/__bs/config.json", |result| {
3434
let (_sys, _url, mut res) = result.expect("api returned");
35-
let _c: RequireJsClientConfig = serde_json::from_str(
36-
&res.text().expect("unwrap text response"),
37-
).expect("serde deserialize");
35+
let _c: RequireJsClientConfig =
36+
serde_json::from_str(&res.text().expect("unwrap text response"))
37+
.expect("serde deserialize");
3838
});
3939
}
4040

@@ -75,9 +75,9 @@ fn test_seed_seeded_json() {
7575
fn test_build_json() {
7676
api(DEFAULT_ARGS.to_vec(), "/__bs/build.json", |result| {
7777
let (_sys, _url, mut res) = result.expect("api returned");
78-
let _c: RequireJsBuildConfig = serde_json::from_str(
79-
&res.text().expect("unwrap text response"),
80-
).expect("serde deserialize");
78+
let _c: RequireJsBuildConfig =
79+
serde_json::from_str(&res.text().expect("unwrap text response"))
80+
.expect("serde deserialize");
8181
});
8282
}
8383

@@ -91,9 +91,9 @@ fn test_build_json_from_json_config() {
9191
];
9292
api(args, "/__bs/build.json", |result| {
9393
let (_sys, _url, mut res) = result.expect("api returned");
94-
let _c: RequireJsBuildConfig = serde_json::from_str(
95-
&res.text().expect("unwrap text response"),
96-
).expect("serde deserialize");
94+
let _c: RequireJsBuildConfig =
95+
serde_json::from_str(&res.text().expect("unwrap text response"))
96+
.expect("serde deserialize");
9797
});
9898
}
9999

tests/proxy_transform.rs

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
extern crate actix_web;
2+
extern crate bs;
3+
extern crate mime;
4+
5+
#[macro_use]
6+
extern crate log;
7+
extern crate env_logger;
8+
9+
use actix_web::http::header;
10+
use actix_web::http::Cookie;
11+
use actix_web::test::TestApp;
12+
use actix_web::HttpMessage;
13+
use actix_web::HttpRequest;
14+
use actix_web::{test, HttpResponse};
15+
use bs::config::ProgramConfig;
16+
use bs::options::ProgramOptions;
17+
use bs::preset::AppState;
18+
use bs::presets::m2::requirejs_config::RequireJsClientConfig;
19+
use bs::proxy_transform::proxy_transform;
20+
use mime::{TEXT_HTML, TEXT_HTML_UTF_8};
21+
use std::cell::RefCell;
22+
use std::str;
23+
use std::sync::Arc;
24+
use std::sync::Mutex;
25+
26+
fn test_str(adr: impl Into<String>) -> String {
27+
format!(
28+
r#"
29+
<!doctype html>
30+
<html lang="en">
31+
<head>
32+
<meta charset="UTF-8">
33+
</head>
34+
<body>
35+
<a href="http://{}"></a>
36+
</body>
37+
</html>
38+
"#,
39+
adr.into()
40+
)
41+
}
42+
43+
#[test]
44+
fn test_forwards_headers() {
45+
let server = test::TestServer::new(|app| {
46+
app.handler(|req: &HttpRequest| {
47+
println!("headers received at proxy addr: {:#?}", req.headers());
48+
assert_eq!(req.headers().get(header::ACCEPT).unwrap(), "text/html");
49+
assert_eq!(
50+
req.headers().get(header::COOKIE).unwrap(),
51+
"hello there; hello there 2"
52+
);
53+
54+
let srv_address = req
55+
.headers()
56+
.get("srv_address")
57+
.expect("missing srv_address header")
58+
.to_str()
59+
.expect("headervalue -> str");
60+
61+
HttpResponse::Ok()
62+
.header("shane", "kittens")
63+
.header(header::CONTENT_TYPE, TEXT_HTML_UTF_8)
64+
.body(test_str(srv_address))
65+
})
66+
});
67+
68+
let srv_address = server.addr().to_string();
69+
let srv_address2 = srv_address.clone();
70+
71+
let mut proxy = test::TestServer::build_with_state(move || {
72+
let addr = srv_address.clone();
73+
let opts = ProgramOptions::new(addr.clone(), "http");
74+
AppState {
75+
opts,
76+
program_config: ProgramConfig::default(),
77+
rewrites: vec![],
78+
req_log: Mutex::new(vec![]),
79+
rjs_client_config: Arc::new(Mutex::new(RequireJsClientConfig::default())),
80+
}
81+
}).start(move |app| {
82+
app.handler(proxy_transform);
83+
});
84+
85+
println!("PROXY={}", proxy.addr().to_string());
86+
println!("TARGET={}", srv_address2.clone());
87+
88+
let request = proxy
89+
.get()
90+
.header(header::ACCEPT, "text/html")
91+
.set_header(header::HOST, proxy.addr().to_string())
92+
.header("cookie", "hello there")
93+
.header("cookie", "hello there 2")
94+
.header("srv_address", srv_address2)
95+
.set_header(
96+
header::ORIGIN,
97+
format!("https://{}", proxy.addr().to_string()),
98+
).uri(proxy.url("/"))
99+
.finish()
100+
.unwrap();
101+
102+
let response = proxy.execute(request.send()).unwrap();
103+
let _bytes = proxy.execute(response.body()).unwrap();
104+
let response_body = str::from_utf8(&_bytes[..])
105+
.expect("bytes->String")
106+
.to_string();
107+
let expected_body = test_str(proxy.addr().to_string());
108+
109+
debug!("main resp headers: {:#?}", response.headers());
110+
111+
let has_header = response.headers().get("shane").is_some();
112+
113+
assert_eq!(has_header, true);
114+
// assert_eq!(response_body, expected_body);
115+
}

0 commit comments

Comments
 (0)