Skip to content

Commit

Permalink
refactor(h1): add spans for parse_headers and encode_headers (#2262)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Aug 5, 2020
1 parent 6e7e4e2 commit 3de81c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ where
self.enforce_version(&mut head);

let buf = self.io.headers_buf();
match T::encode(
match super::role::encode_headers::<T>(
Encode {
head: &mut head,
body,
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ where
S: Http1Transaction,
{
loop {
match S::parse(
match super::role::parse_headers::<S>(
&mut self.read_buf,
ParseContext {
cached_headers: parse_ctx.cached_headers,
Expand Down
44 changes: 38 additions & 6 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ macro_rules! header_value {
}};
}

pub(super) fn parse_headers<T>(
bytes: &mut BytesMut,
ctx: ParseContext<'_>,
) -> ParseResult<T::Incoming>
where
T: Http1Transaction,
{
// If the buffer is empty, don't bother entering the span, it's just noise.
if bytes.is_empty() {
return Ok(None);
}

let span = trace_span!("parse_headers");
let _s = span.enter();
T::parse(bytes, ctx)
}

pub(super) fn encode_headers<T>(
enc: Encode<'_, T::Outgoing>,
dst: &mut Vec<u8>,
) -> crate::Result<Encoder>
where
T: Http1Transaction,
{
let span = trace_span!("encode_headers");
let _s = span.enter();
T::encode(enc, dst)
}

// There are 2 main roles, Client and Server.

pub(crate) enum Client {}
Expand All @@ -70,9 +99,7 @@ impl Http1Transaction for Server {
const LOG: &'static str = "{role=server}";

fn parse(buf: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<RequestLine> {
if buf.is_empty() {
return Ok(None);
}
debug_assert!(!buf.is_empty(), "parse called with empty buf");

let mut keep_alive;
let is_http_11;
Expand Down Expand Up @@ -614,11 +641,10 @@ impl Http1Transaction for Client {
const LOG: &'static str = "{role=client}";

fn parse(buf: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<StatusCode> {
debug_assert!(!buf.is_empty(), "parse called with empty buf");

// Loop to skip information status code headers (100 Continue, etc).
loop {
if buf.is_empty() {
return Ok(None);
}
// Unsafe: see comment in Server Http1Transaction, above.
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::uninitialized() };
let (len, status, version, headers_len) = {
Expand Down Expand Up @@ -688,6 +714,12 @@ impl Http1Transaction for Client {
wants_upgrade: is_upgrade,
}));
}

// Parsing a 1xx response could have consumed the buffer, check if
// it is empty now...
if buf.is_empty() {
return Ok(None);
}
}
}

Expand Down

0 comments on commit 3de81c8

Please sign in to comment.