http2: normalize host when there is user info

Ticket: 6479
pull/9789/head
Philippe Antoine 2 years ago committed by Victor Julien
parent b6cd66f41d
commit 6249722589

@ -613,13 +613,28 @@ fn http2_lower(value: &[u8]) -> Option<Vec<u8>> {
} }
// returns a tuple with the value and its size // returns a tuple with the value and its size
fn http2_normalize_host(value: &[u8]) -> (Option<Vec<u8>>, usize) { fn http2_normalize_host(value: &[u8]) -> &[u8] {
match value.iter().position(|&x| x == b'@') {
Some(i) => {
let value = &value[i+1..];
match value.iter().position(|&x| x == b':') {
Some(i) => {
return &value[..i];
}
None => {
return value;
}
}
}
None => {
match value.iter().position(|&x| x == b':') { match value.iter().position(|&x| x == b':') {
Some(i) => { Some(i) => {
return (http2_lower(&value[..i]), i); return &value[..i];
} }
None => { None => {
return (http2_lower(value), value.len()); return value;
}
}
} }
} }
} }
@ -632,7 +647,7 @@ pub unsafe extern "C" fn rs_http2_tx_get_host_norm(
let r = http2_normalize_host(value); let r = http2_normalize_host(value);
// r is a tuple with the value and its size // r is a tuple with the value and its size
// this is useful when we only take a substring (before the port) // this is useful when we only take a substring (before the port)
match r.0 { match http2_lower(r) {
Some(normval) => { Some(normval) => {
// In case we needed some normalization, // In case we needed some normalization,
// the transaction needs to take ownership of this normalized host // the transaction needs to take ownership of this normalized host
@ -640,12 +655,12 @@ pub unsafe extern "C" fn rs_http2_tx_get_host_norm(
let idx = tx.escaped.len() - 1; let idx = tx.escaped.len() - 1;
let resvalue = &tx.escaped[idx]; let resvalue = &tx.escaped[idx];
*buffer = resvalue.as_ptr(); //unsafe *buffer = resvalue.as_ptr(); //unsafe
*buffer_len = r.1 as u32; *buffer_len = resvalue.len() as u32;
return 1; return 1;
} }
None => { None => {
*buffer = value.as_ptr(); //unsafe *buffer = r.as_ptr(); //unsafe
*buffer_len = r.1 as u32; *buffer_len = r.len() as u32;
return 1; return 1;
} }
} }
@ -1008,32 +1023,19 @@ mod tests {
fn test_http2_normalize_host() { fn test_http2_normalize_host() {
let buf0 = "aBC.com:1234".as_bytes(); let buf0 = "aBC.com:1234".as_bytes();
let r0 = http2_normalize_host(buf0); let r0 = http2_normalize_host(buf0);
match r0.0 { assert_eq!(r0, "aBC.com".as_bytes().to_vec());
Some(r) => {
assert_eq!(r, "abc.com".as_bytes().to_vec());
}
None => {
panic!("Result should not have been None");
}
}
let buf1 = "oisf.net".as_bytes(); let buf1 = "oisf.net".as_bytes();
let r1 = http2_normalize_host(buf1); let r1 = http2_normalize_host(buf1);
match r1.0 { assert_eq!(r1, "oisf.net".as_bytes().to_vec());
Some(r) => {
panic!("Result should not have been None, not {:?}", r);
}
None => {}
}
assert_eq!(r1.1, "oisf.net".len());
let buf2 = "localhost:3000".as_bytes(); let buf2 = "localhost:3000".as_bytes();
let r2 = http2_normalize_host(buf2); let r2 = http2_normalize_host(buf2);
match r2.0 { assert_eq!(r2, "localhost".as_bytes().to_vec());
Some(r) => { let buf3 = "user:pass@localhost".as_bytes();
panic!("Result should not have been None, not {:?}", r); let r3 = http2_normalize_host(buf3);
} assert_eq!(r3, "localhost".as_bytes().to_vec());
None => {} let buf4 = "user:pass@localhost:123".as_bytes();
} let r4 = http2_normalize_host(buf4);
assert_eq!(r2.1, "localhost".len()); assert_eq!(r4, "localhost".as_bytes().to_vec());
} }
#[test] #[test]

Loading…
Cancel
Save