Skip to content

Allow recording the raw HTTP/1 headers sent and received #3846

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions src/client/conn/http1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub struct Builder {
h1_parser_config: ParserConfig,
h1_writev: Option<bool>,
h1_title_case_headers: bool,
h1_record_raw_request_headers: bool,
h1_record_raw_response_headers: bool,
h1_preserve_header_case: bool,
h1_max_headers: Option<usize>,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -312,6 +314,8 @@ impl Builder {
h1_read_buf_exact_size: None,
h1_parser_config: Default::default(),
h1_title_case_headers: false,
h1_record_raw_request_headers: false,
h1_record_raw_response_headers: false,
h1_preserve_header_case: false,
h1_max_headers: None,
#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -428,6 +432,22 @@ impl Builder {
self
}

/// Set whether to record the raw headers sent.
///
/// Default is false.
pub fn record_raw_request_headers(&mut self, enabled: bool) -> &mut Builder {
self.h1_record_raw_request_headers = enabled;
self
}

/// Set whether to record the raw headers received.
///
/// Default is false.
pub fn record_raw_response_headers(&mut self, enabled: bool) -> &mut Builder {
self.h1_record_raw_response_headers = enabled;
self
}

/// Set whether to support preserving original header cases.
///
/// Currently, this will record the original cases received, and store them
Expand Down Expand Up @@ -539,6 +559,12 @@ impl Builder {
if opts.h1_title_case_headers {
conn.set_title_case_headers();
}
if opts.h1_record_raw_request_headers {
conn.set_record_raw_request_headers();
}
if opts.h1_record_raw_response_headers {
conn.set_record_raw_response_headers();
}
if opts.h1_preserve_header_case {
conn.set_preserve_header_case();
}
Expand Down
33 changes: 33 additions & 0 deletions src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ impl fmt::Debug for Protocol {
}
}

/// Raw request headers as sent over the TLS or TCP connection.
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
#[derive(Clone, Debug)]
pub struct RawRequestHeaders(Bytes);

#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
impl RawRequestHeaders {
/// Returns the raw bytes sent for the header of the request.
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub(crate) fn from(bytes: Bytes) -> Self {
Self(bytes)
}
}

/// Raw response headers as sent over the TLS or TCP connection.
#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
#[derive(Clone, Debug)]
pub struct RawResponseHeaders(Bytes);

#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))]
impl RawResponseHeaders {
/// Returns the raw bytes received for the header of the response.
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
#[cfg(feature = "client")]
pub(crate) fn from(bytes: Bytes) -> Self {
Self(bytes)
}
}

/// A map from header names to their original casing as received in an HTTP message.
///
/// If an HTTP/1 response `res` is parsed on a connection whose option
Expand Down
26 changes: 26 additions & 0 deletions src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ where
date_header: true,
#[cfg(feature = "server")]
timer: Time::Empty,
raw_request_headers: None,
record_raw_request_headers: false,
record_raw_response_headers: false,
preserve_header_case: false,
#[cfg(feature = "ffi")]
preserve_header_order: false,
Expand Down Expand Up @@ -123,6 +126,16 @@ where
self.state.title_case_headers = true;
}

#[cfg(feature = "client")]
pub(crate) fn set_record_raw_request_headers(&mut self) {
self.state.record_raw_request_headers = true;
}

#[cfg(feature = "client")]
pub(crate) fn set_record_raw_response_headers(&mut self) {
self.state.record_raw_response_headers = true;
}

pub(crate) fn set_preserve_header_case(&mut self) {
self.state.preserve_header_case = true;
}
Expand Down Expand Up @@ -241,6 +254,8 @@ where
req_method: &mut self.state.method,
h1_parser_config: self.state.h1_parser_config.clone(),
h1_max_headers: self.state.h1_max_headers,
raw_request_headers: self.state.raw_request_headers.as_ref(),
record_raw_headers: self.state.record_raw_response_headers,
preserve_header_case: self.state.preserve_header_case,
#[cfg(feature = "ffi")]
preserve_header_order: self.state.preserve_header_order,
Expand Down Expand Up @@ -617,6 +632,7 @@ where
self.enforce_version(&mut head);

let buf = self.io.headers_buf();
let headers_start = buf.len();
match super::role::encode_headers::<T>(
Encode {
head: &mut head,
Expand All @@ -633,6 +649,13 @@ where
Ok(encoder) => {
debug_assert!(self.state.cached_headers.is_none());
debug_assert!(head.headers.is_empty());
if self.state.record_raw_request_headers {
self.state.raw_request_headers = Some(crate::ext::RawRequestHeaders::from(
Bytes::copy_from_slice(&buf[headers_start..]),
));
} else {
self.state.raw_request_headers = None;
}
self.state.cached_headers = Some(head.headers);

#[cfg(feature = "client")]
Expand Down Expand Up @@ -934,6 +957,9 @@ struct State {
date_header: bool,
#[cfg(feature = "server")]
timer: Time,
raw_request_headers: Option<crate::ext::RawRequestHeaders>,
record_raw_request_headers: bool,
record_raw_response_headers: bool,
preserve_header_case: bool,
#[cfg(feature = "ffi")]
preserve_header_order: bool,
Expand Down
4 changes: 4 additions & 0 deletions src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ where
req_method: parse_ctx.req_method,
h1_parser_config: parse_ctx.h1_parser_config.clone(),
h1_max_headers: parse_ctx.h1_max_headers,
raw_request_headers: parse_ctx.raw_request_headers,
record_raw_headers: parse_ctx.record_raw_headers,
preserve_header_case: parse_ctx.preserve_header_case,
#[cfg(feature = "ffi")]
preserve_header_order: parse_ctx.preserve_header_order,
Expand Down Expand Up @@ -706,6 +708,8 @@ mod tests {
req_method: &mut None,
h1_parser_config: Default::default(),
h1_max_headers: None,
raw_request_headers: None,
record_raw_headers: false,
preserve_header_case: false,
#[cfg(feature = "ffi")]
preserve_header_order: false,
Expand Down
2 changes: 2 additions & 0 deletions src/proto/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub(crate) struct ParseContext<'a> {
req_method: &'a mut Option<Method>,
h1_parser_config: ParserConfig,
h1_max_headers: Option<usize>,
record_raw_headers: bool,
raw_request_headers: Option<&'a crate::ext::RawRequestHeaders>,
preserve_header_case: bool,
#[cfg(feature = "ffi")]
preserve_header_order: bool,
Expand Down
Loading
Loading