1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-27 01:20:35 +00:00

Drop content length headers from 101 responses (#1767)

Co-authored-by: Sebastian Mayr <smayr@atlassian.com>
This commit is contained in:
Sebastian Mayr 2020-11-02 03:44:14 -05:00 committed by GitHub
parent b6385c2b4e
commit 5468c3c410
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 11 deletions

View file

@ -1,6 +1,10 @@
# Changes # Changes
## Unreleased - 2020-xx-xx ## Unreleased - 2020-xx-xx
### Fixed
* Started dropping `transfer-encoding: chunked` and `Content-Length` for 1XX and 204 responses. [#1767]
[#1767]: https://github.com/actix/actix-web/pull/1767
## 2.1.0 - 2020-10-30 ## 2.1.0 - 2020-10-30

View file

@ -64,14 +64,17 @@ pub(crate) trait MessageType: Sized {
// Content length // Content length
if let Some(status) = self.status() { if let Some(status) = self.status() {
match status { match status {
StatusCode::NO_CONTENT StatusCode::CONTINUE
| StatusCode::CONTINUE | StatusCode::SWITCHING_PROTOCOLS
| StatusCode::PROCESSING => length = BodySize::None, | StatusCode::PROCESSING
StatusCode::SWITCHING_PROTOCOLS => { | StatusCode::NO_CONTENT => {
// skip content-length and transfer-encoding headers
// See https://tools.ietf.org/html/rfc7230#section-3.3.1
// and https://tools.ietf.org/html/rfc7230#section-3.3.2
skip_len = true; skip_len = true;
length = BodySize::Stream; length = BodySize::None
} }
_ => (), _ => {}
} }
} }
match length { match length {
@ -676,4 +679,28 @@ mod tests {
assert!(data.contains("authorization: another authorization\r\n")); assert!(data.contains("authorization: another authorization\r\n"));
assert!(data.contains("date: date\r\n")); assert!(data.contains("date: date\r\n"));
} }
#[test]
fn test_no_content_length() {
let mut bytes = BytesMut::with_capacity(2048);
let mut res: Response<()> =
Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>();
res.headers_mut()
.insert(DATE, HeaderValue::from_static(&""));
res.headers_mut()
.insert(CONTENT_LENGTH, HeaderValue::from_static(&"0"));
let _ = res.encode_headers(
&mut bytes,
Version::HTTP_11,
BodySize::Stream,
ConnectionType::Upgrade,
&ServiceConfig::default(),
);
let data =
String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap();
assert!(!data.contains("content-length: 0\r\n"));
assert!(!data.contains("transfer-encoding: chunked\r\n"));
}
} }

View file

@ -164,7 +164,6 @@ pub fn handshake_with_protocols(
let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS) let mut response = HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
.upgrade("websocket") .upgrade("websocket")
.header(header::TRANSFER_ENCODING, "chunked")
.header(header::SEC_WEBSOCKET_ACCEPT, key.as_str()) .header(header::SEC_WEBSOCKET_ACCEPT, key.as_str())
.take(); .take();
@ -664,10 +663,10 @@ mod tests {
) )
.to_http_request(); .to_http_request();
assert_eq!( let resp = handshake(&req).unwrap().finish();
StatusCode::SWITCHING_PROTOCOLS, assert_eq!(StatusCode::SWITCHING_PROTOCOLS, resp.status());
handshake(&req).unwrap().finish().status() assert_eq!(None, resp.headers().get(&header::CONTENT_LENGTH));
); assert_eq!(None, resp.headers().get(&header::TRANSFER_ENCODING));
let req = TestRequest::default() let req = TestRequest::default()
.header( .header(