Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HeaderCaseMap public #2

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::error::Error as StdError;
use std::fmt;
use std::mem;
Expand Down Expand Up @@ -1143,17 +1142,6 @@ impl Builder {
self
}

/// Allows passing a HashMap of headers that will be sent with
/// specified casing
///
/// Note that this setting does not affect HTTP/2.
///
/// Default is None.
pub fn http1_special_headers(&mut self, val: Option<&'static HashMap<&'static str, &'static [u8]>>) -> &mut Self {
self.conn_builder.http1_special_headers(val);
self
}

/// Set whether HTTP/0.9 responses should be tolerated.
///
/// Default is false.
Expand Down
17 changes: 1 addition & 16 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//! # }
//! ```

use std::{error::Error as StdError, collections::HashMap};
use std::error::Error as StdError;
use std::fmt;
#[cfg(not(all(feature = "http1", feature = "http2")))]
use std::marker::PhantomData;
Expand Down Expand Up @@ -156,7 +156,6 @@ pub struct Builder {
h1_writev: Option<bool>,
h1_title_case_headers: bool,
h1_preserve_header_case: bool,
h1_special_headers: Option<&'static HashMap<&'static str, &'static [u8]>>,
#[cfg(feature = "ffi")]
h1_preserve_header_order: bool,
h1_read_buf_exact_size: Option<usize>,
Expand Down Expand Up @@ -572,7 +571,6 @@ impl Builder {
version: Proto::Http1,
#[cfg(not(feature = "http1"))]
version: Proto::Http2,
h1_special_headers: None,
}
}

Expand Down Expand Up @@ -728,17 +726,6 @@ impl Builder {
self
}

/// Allows passing a HashMap of headers that will be sent with
/// specified casing
///
/// Note that this setting does not affect HTTP/2.
///
/// Default is None.
pub fn http1_special_headers(&mut self, val: Option<&'static HashMap<&'static str, &'static [u8]>>) -> &mut Self {
self.h1_special_headers = val;
self
}

/// Set whether to support preserving original header order.
///
/// Currently, this will record the order in which headers are received, and store this
Expand Down Expand Up @@ -1002,8 +989,6 @@ impl Builder {
conn.set_preserve_header_case();
}

conn.set_special_headers(opts.h1_special_headers);

#[cfg(feature = "ffi")]
if opts.h1_preserve_header_order {
conn.set_preserve_header_order();
Expand Down
8 changes: 7 additions & 1 deletion src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl fmt::Debug for Protocol {
///
/// [`http1_preserve_header_case`]: /client/struct.Client.html#method.http1_preserve_header_case
#[derive(Clone, Debug)]
pub(crate) struct HeaderCaseMap(HeaderMap<Bytes>);
pub struct HeaderCaseMap(HeaderMap<Bytes>);

#[cfg(feature = "http1")]
impl HeaderCaseMap {
Expand Down Expand Up @@ -131,6 +131,12 @@ impl HeaderCaseMap {
}
}

impl From<HeaderMap<Bytes>> for HeaderCaseMap {
fn from(hdr_map: HeaderMap<Bytes>) -> Self {
Self(hdr_map)
}
}

#[cfg(feature = "ffi")]
#[derive(Clone, Debug)]
/// Hashmap<Headername, numheaders with that name>
Expand Down
8 changes: 0 additions & 8 deletions src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::fmt;
use std::io;
use std::marker::PhantomData;
Expand Down Expand Up @@ -62,7 +61,6 @@ where
#[cfg(feature = "ffi")]
preserve_header_order: false,
title_case_headers: false,
special_headers: None,
h09_responses: false,
#[cfg(feature = "ffi")]
on_informational: None,
Expand Down Expand Up @@ -111,10 +109,6 @@ where
self.state.title_case_headers = true;
}

pub(crate) fn set_special_headers(&mut self, val: Option<&'static HashMap<&'static str, &'static [u8]>>) {
self.state.special_headers = val;
}

pub(crate) fn set_preserve_header_case(&mut self) {
self.state.preserve_header_case = true;
}
Expand Down Expand Up @@ -568,7 +562,6 @@ where
keep_alive: self.state.wants_keep_alive(),
req_method: &mut self.state.method,
title_case_headers: self.state.title_case_headers,
special_headers: self.state.special_headers,
},
buf,
) {
Expand Down Expand Up @@ -834,7 +827,6 @@ struct State {
#[cfg(feature = "ffi")]
preserve_header_order: bool,
title_case_headers: bool,
special_headers: Option<&'static HashMap<&'static str, &'static [u8]>>,
h09_responses: bool,
/// If set, called with each 1xx informational response received for
/// the current request. MUST be unset after a non-1xx response is
Expand Down
2 changes: 0 additions & 2 deletions src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
#[cfg(all(feature = "server", feature = "runtime"))]
use std::{pin::Pin, time::Duration};

Expand Down Expand Up @@ -101,7 +100,6 @@ pub(crate) struct Encode<'a, T> {
keep_alive: bool,
req_method: &'a mut Option<Method>,
title_case_headers: bool,
special_headers: Option<&'static HashMap<&'static str, &'static [u8]>>,
}

/// Extra flags that a request "wants", like expect-continue or upgrades.
Expand Down
41 changes: 3 additions & 38 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::fmt::{self, Write};
use std::mem::MaybeUninit;

Expand Down Expand Up @@ -1147,7 +1146,7 @@ impl Http1Transaction for Client {
} else if msg.title_case_headers {
write_headers_title_case(&msg.head.headers, dst);
} else {
write_headers(&msg.head.headers, dst, msg.special_headers);
write_headers(&msg.head.headers, dst);
}

extend(dst, b"\r\n");
Expand Down Expand Up @@ -1442,17 +1441,9 @@ fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec<u8>) {
}
}

fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>, special_headers: Option<&HashMap<&'static str, &'static [u8]>>) {
fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>) {
for (name, value) in headers {
let name = if let Some(special_headers) = special_headers.as_ref() {
match special_headers.get(&name.as_str()) {
Some(x) => x,
None => name.as_str().as_bytes(),
}
} else {
name.as_str().as_bytes()
};
extend(dst, name);
extend(dst, name.as_str().as_bytes());
extend(dst, b": ");
extend(dst, value.as_bytes());
extend(dst, b"\r\n");
Expand Down Expand Up @@ -2413,7 +2404,6 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2445,7 +2435,6 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: false,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2480,7 +2469,6 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
special_headers: None,
},
&mut vec,
)
Expand All @@ -2505,7 +2493,6 @@ mod tests {
keep_alive: true,
req_method: &mut Some(Method::CONNECT),
title_case_headers: false,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2535,7 +2522,6 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2570,7 +2556,6 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: false,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2605,7 +2590,6 @@ mod tests {
keep_alive: true,
req_method: &mut None,
title_case_headers: true,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2682,23 +2666,6 @@ mod tests {
assert_eq!(dst, b"X-Empty: a\r\nX-EMPTY: b\r\n");
}

#[test]
fn test_write_special_headers() {
let special_headers = HashMap::from([
("a-special-header", "A-spEciAl-hEaDER".as_bytes()),
]);

let mut headers = HeaderMap::new();
headers.insert(http::header::HeaderName::from_static("a-special-header"), "a".parse().unwrap());
headers.insert(http::header::HeaderName::from_static("not-a-special-header"), "a".parse().unwrap());

let mut dst = Vec::new();
super::write_headers(&headers, &mut dst, Some(&special_headers));

assert_eq!(dst, b"A-spEciAl-hEaDER: a\r\nnot-a-special-header: a\r\n");
}


#[cfg(feature = "nightly")]
use test::Bencher;

Expand Down Expand Up @@ -2839,7 +2806,6 @@ mod tests {
keep_alive: true,
req_method: &mut Some(Method::GET),
title_case_headers: false,
special_headers: None,
},
&mut vec,
)
Expand Down Expand Up @@ -2868,7 +2834,6 @@ mod tests {
keep_alive: true,
req_method: &mut Some(Method::GET),
title_case_headers: false,
special_headers: None,
},
&mut vec,
)
Expand Down