Skip to content

Commit d7495a7

Browse files
committed
Merge branch '0.13.x' of github.com:hyperium/hyper into 0.13.x
2 parents 17f5426 + a00cc20 commit d7495a7

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

src/proto/h1/decode.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ enum ChunkedState {
5555
Body,
5656
BodyCr,
5757
BodyLf,
58+
Trailer,
59+
TrailerLf,
5860
EndCr,
5961
EndLf,
6062
End,
@@ -196,6 +198,8 @@ impl ChunkedState {
196198
Body => ChunkedState::read_body(cx, body, size, buf),
197199
BodyCr => ChunkedState::read_body_cr(cx, body),
198200
BodyLf => ChunkedState::read_body_lf(cx, body),
201+
Trailer => ChunkedState::read_trailer(cx, body),
202+
TrailerLf => ChunkedState::read_trailer_lf(cx, body),
199203
EndCr => ChunkedState::read_end_cr(cx, body),
200204
EndLf => ChunkedState::read_end_lf(cx, body),
201205
End => Poll::Ready(Ok(ChunkedState::End)),
@@ -340,18 +344,38 @@ impl ChunkedState {
340344
}
341345
}
342346

343-
fn read_end_cr<R: MemRead>(
347+
fn read_trailer<R: MemRead>(
344348
cx: &mut task::Context<'_>,
345349
rdr: &mut R,
346350
) -> Poll<Result<ChunkedState, io::Error>> {
351+
trace!("read_trailer");
347352
match byte!(rdr, cx) {
348-
b'\r' => Poll::Ready(Ok(ChunkedState::EndLf)),
353+
b'\r' => Poll::Ready(Ok(ChunkedState::TrailerLf)),
354+
_ => Poll::Ready(Ok(ChunkedState::Trailer)),
355+
}
356+
}
357+
fn read_trailer_lf<R: MemRead>(
358+
cx: &mut task::Context<'_>,
359+
rdr: &mut R,
360+
) -> Poll<Result<ChunkedState, io::Error>> {
361+
match byte!(rdr, cx) {
362+
b'\n' => Poll::Ready(Ok(ChunkedState::EndCr)),
349363
_ => Poll::Ready(Err(io::Error::new(
350364
io::ErrorKind::InvalidInput,
351-
"Invalid chunk end CR",
365+
"Invalid trailer end LF",
352366
))),
353367
}
354368
}
369+
370+
fn read_end_cr<R: MemRead>(
371+
cx: &mut task::Context<'_>,
372+
rdr: &mut R,
373+
) -> Poll<Result<ChunkedState, io::Error>> {
374+
match byte!(rdr, cx) {
375+
b'\r' => Poll::Ready(Ok(ChunkedState::EndLf)),
376+
_ => Poll::Ready(Ok(ChunkedState::Trailer)),
377+
}
378+
}
355379
fn read_end_lf<R: MemRead>(
356380
cx: &mut task::Context<'_>,
357381
rdr: &mut R,
@@ -537,6 +561,15 @@ mod tests {
537561
assert_eq!("1234567890abcdef", &result);
538562
}
539563

564+
#[tokio::test]
565+
async fn test_read_chunked_trailer_with_missing_lf() {
566+
let mut mock_buf = &b"10\r\n1234567890abcdef\r\n0\r\nbad\r\r\n"[..];
567+
let mut decoder = Decoder::chunked();
568+
decoder.decode_fut(&mut mock_buf).await.expect("decode");
569+
let e = decoder.decode_fut(&mut mock_buf).await.unwrap_err();
570+
assert_eq!(e.kind(), io::ErrorKind::InvalidInput);
571+
}
572+
540573
#[tokio::test]
541574
async fn test_read_chunked_after_eof() {
542575
let mut mock_buf = &b"10\r\n1234567890abcdef\r\n0\r\n\r\n"[..];

tests/client.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,69 @@ test! {
430430
body: None,
431431
}
432432

433+
test! {
434+
name: client_get_req_body_chunked_with_trailer,
435+
436+
server:
437+
expected: "\
438+
GET / HTTP/1.1\r\n\
439+
host: {addr}\r\n\
440+
\r\n\
441+
",
442+
reply: "\
443+
HTTP/1.1 200 OK\r\n\
444+
Transfer-Encoding: chunked\r\n\
445+
\r\n\
446+
5\r\n\
447+
hello\r\n\
448+
0\r\n\
449+
Trailer: value\r\n\
450+
\r\n\
451+
",
452+
453+
client:
454+
request: {
455+
method: GET,
456+
url: "http://{addr}/",
457+
},
458+
response:
459+
status: OK,
460+
headers: {},
461+
body: &b"hello"[..],
462+
}
463+
464+
test! {
465+
name: client_get_req_body_chunked_with_multiple_trailers,
466+
467+
server:
468+
expected: "\
469+
GET / HTTP/1.1\r\n\
470+
host: {addr}\r\n\
471+
\r\n\
472+
",
473+
reply: "\
474+
HTTP/1.1 200 OK\r\n\
475+
Transfer-Encoding: chunked\r\n\
476+
\r\n\
477+
5\r\n\
478+
hello\r\n\
479+
0\r\n\
480+
Trailer: value\r\n\
481+
another-trainer: another-value\r\n\
482+
\r\n\
483+
",
484+
485+
client:
486+
request: {
487+
method: GET,
488+
url: "http://{addr}/",
489+
},
490+
response:
491+
status: OK,
492+
headers: {},
493+
body: &b"hello"[..],
494+
}
495+
433496
test! {
434497
name: client_get_req_body_sized,
435498

0 commit comments

Comments
 (0)