Skip to content

Commit 9b2998b

Browse files
committed
fix(server): use EmptyWriter for status codes that have no body
Previously, hyper was defaulting to Chunked which adds a Transfer-Encoding header, whenever there was no Content-Length header. RFC 7230 section 3.3.1 reads: ... A server MUST NOT send a Transfer-Encoding header field in any response with a status code of 1xx (Informational) or 204 (No Content). A server MUST NOT send a Transfer-Encoding header field in any 2xx (Successful) response to a CONNECT request ... This commit fixes the cases of 1xx (Informational), 204 (No Content) by using the EmptyWriter. It also uses EmptyWriter for 304 (NotModified) which should not have a body. It does NOT address the case of responses to CONNECT requests, or to HEAD requests which do not send a body. These cases cannot be determined using the data available in the response, and are left for future work.
1 parent 1b869c4 commit 9b2998b

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/server/response.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use time::now_utc;
1212

1313
use header;
1414
use http::h1::{CR, LF, LINE_ENDING, HttpWriter};
15-
use http::h1::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter};
15+
use http::h1::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
1616
use status;
1717
use net::{Fresh, Streaming};
1818
use version;
@@ -88,11 +88,14 @@ impl<'a, W: Any> Response<'a, W> {
8888
self.headers.set(header::Date(header::HttpDate(now_utc())));
8989
}
9090

91-
92-
let mut body_type = Body::Chunked;
93-
94-
if let Some(cl) = self.headers.get::<header::ContentLength>() {
95-
body_type = Body::Sized(**cl);
91+
let body_type = match self.status {
92+
status::StatusCode::NoContent | status::StatusCode::NotModified => Body::Empty,
93+
c if c.class() == status::StatusClass::Informational => Body::Empty,
94+
_ => if let Some(cl) = self.headers.get::<header::ContentLength>() {
95+
Body::Sized(**cl)
96+
} else {
97+
Body::Chunked
98+
}
9699
};
97100

98101
// can't do in match above, thanks borrowck
@@ -177,7 +180,8 @@ impl<'a> Response<'a, Fresh> {
177180
let (version, body, status, headers) = self.deconstruct();
178181
let stream = match body_type {
179182
Body::Chunked => ChunkedWriter(body.into_inner()),
180-
Body::Sized(len) => SizedWriter(body.into_inner(), len)
183+
Body::Sized(len) => SizedWriter(body.into_inner(), len),
184+
Body::Empty => EmptyWriter(body.into_inner()),
181185
};
182186

183187
// "copy" to change the phantom type
@@ -227,6 +231,7 @@ impl<'a> Write for Response<'a, Streaming> {
227231
enum Body {
228232
Chunked,
229233
Sized(u64),
234+
Empty,
230235
}
231236

232237
impl<'a, T: Any> Drop for Response<'a, T> {
@@ -235,6 +240,7 @@ impl<'a, T: Any> Drop for Response<'a, T> {
235240
let mut body = match self.write_head() {
236241
Ok(Body::Chunked) => ChunkedWriter(self.body.get_mut()),
237242
Ok(Body::Sized(len)) => SizedWriter(self.body.get_mut(), len),
243+
Ok(Body::Empty) => EmptyWriter(self.body.get_mut()),
238244
Err(e) => {
239245
debug!("error dropping request: {:?}", e);
240246
return;
@@ -361,4 +367,23 @@ mod tests {
361367
"" // empty zero body
362368
}
363369
}
370+
371+
#[test]
372+
fn test_no_content() {
373+
use std::io::Write;
374+
use status::StatusCode;
375+
let mut headers = Headers::new();
376+
let mut stream = MockStream::new();
377+
{
378+
let mut res = Response::new(&mut stream, &mut headers);
379+
*res.status_mut() = StatusCode::NoContent;
380+
res.start().unwrap();
381+
}
382+
383+
lines! { stream =
384+
"HTTP/1.1 204 No Content",
385+
_date,
386+
""
387+
}
388+
}
364389
}

0 commit comments

Comments
 (0)