Skip to content

Commit 3de81c8

Browse files
authored
refactor(h1): add spans for parse_headers and encode_headers (#2262)
1 parent 6e7e4e2 commit 3de81c8

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/proto/h1/conn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ where
474474
self.enforce_version(&mut head);
475475

476476
let buf = self.io.headers_buf();
477-
match T::encode(
477+
match super::role::encode_headers::<T>(
478478
Encode {
479479
head: &mut head,
480480
body,

src/proto/h1/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ where
149149
S: Http1Transaction,
150150
{
151151
loop {
152-
match S::parse(
152+
match super::role::parse_headers::<S>(
153153
&mut self.read_buf,
154154
ParseContext {
155155
cached_headers: parse_ctx.cached_headers,

src/proto/h1/role.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ macro_rules! header_value {
5858
}};
5959
}
6060

61+
pub(super) fn parse_headers<T>(
62+
bytes: &mut BytesMut,
63+
ctx: ParseContext<'_>,
64+
) -> ParseResult<T::Incoming>
65+
where
66+
T: Http1Transaction,
67+
{
68+
// If the buffer is empty, don't bother entering the span, it's just noise.
69+
if bytes.is_empty() {
70+
return Ok(None);
71+
}
72+
73+
let span = trace_span!("parse_headers");
74+
let _s = span.enter();
75+
T::parse(bytes, ctx)
76+
}
77+
78+
pub(super) fn encode_headers<T>(
79+
enc: Encode<'_, T::Outgoing>,
80+
dst: &mut Vec<u8>,
81+
) -> crate::Result<Encoder>
82+
where
83+
T: Http1Transaction,
84+
{
85+
let span = trace_span!("encode_headers");
86+
let _s = span.enter();
87+
T::encode(enc, dst)
88+
}
89+
6190
// There are 2 main roles, Client and Server.
6291

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

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

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

616643
fn parse(buf: &mut BytesMut, ctx: ParseContext<'_>) -> ParseResult<StatusCode> {
644+
debug_assert!(!buf.is_empty(), "parse called with empty buf");
645+
617646
// Loop to skip information status code headers (100 Continue, etc).
618647
loop {
619-
if buf.is_empty() {
620-
return Ok(None);
621-
}
622648
// Unsafe: see comment in Server Http1Transaction, above.
623649
let mut headers_indices: [HeaderIndices; MAX_HEADERS] = unsafe { mem::uninitialized() };
624650
let (len, status, version, headers_len) = {
@@ -688,6 +714,12 @@ impl Http1Transaction for Client {
688714
wants_upgrade: is_upgrade,
689715
}));
690716
}
717+
718+
// Parsing a 1xx response could have consumed the buffer, check if
719+
// it is empty now...
720+
if buf.is_empty() {
721+
return Ok(None);
722+
}
691723
}
692724
}
693725

0 commit comments

Comments
 (0)