1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-19 16:11:07 +00:00

do not override content-length header

This commit is contained in:
Nikolay Kim 2018-10-08 15:30:59 -07:00
parent 07f6ca4b71
commit 4e7fac08b9
3 changed files with 19 additions and 14 deletions

View file

@ -176,13 +176,11 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
buffer.extend_from_slice(reason); buffer.extend_from_slice(reason);
// content length // content length
let mut len_is_set = true;
match info.length { match info.length {
ResponseLength::Chunked => { ResponseLength::Chunked => {
buffer.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n") buffer.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n")
} }
ResponseLength::Zero => {
buffer.extend_from_slice(b"\r\ncontent-length: 0\r\n")
}
ResponseLength::Length(len) => { ResponseLength::Length(len) => {
helpers::write_content_length(len, &mut buffer) helpers::write_content_length(len, &mut buffer)
} }
@ -191,6 +189,10 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
write!(buffer.writer(), "{}", len)?; write!(buffer.writer(), "{}", len)?;
buffer.extend_from_slice(b"\r\n"); buffer.extend_from_slice(b"\r\n");
} }
ResponseLength::Zero => {
len_is_set = false;
buffer.extend_from_slice(b"\r\n");
}
ResponseLength::None => buffer.extend_from_slice(b"\r\n"), ResponseLength::None => buffer.extend_from_slice(b"\r\n"),
} }
if let Some(ce) = info.content_encoding { if let Some(ce) = info.content_encoding {
@ -212,6 +214,9 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
}, },
CONTENT_LENGTH => match info.length { CONTENT_LENGTH => match info.length {
ResponseLength::None => (), ResponseLength::None => (),
ResponseLength::Zero => {
len_is_set = true;
}
_ => continue, _ => continue,
}, },
DATE => { DATE => {
@ -248,6 +253,9 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
unsafe { unsafe {
buffer.advance_mut(pos); buffer.advance_mut(pos);
} }
if !len_is_set {
buffer.extend_from_slice(b"content-length: 0\r\n")
}
// optimized date header, set_date writes \r\n // optimized date header, set_date writes \r\n
if !has_date { if !has_date {

View file

@ -191,15 +191,13 @@ impl Output {
let transfer = match resp.body() { let transfer = match resp.body() {
Body::Empty => { Body::Empty => {
if !info.head { info.length = match resp.status() {
info.length = match resp.status() { StatusCode::NO_CONTENT
StatusCode::NO_CONTENT | StatusCode::CONTINUE
| StatusCode::CONTINUE | StatusCode::SWITCHING_PROTOCOLS
| StatusCode::SWITCHING_PROTOCOLS | StatusCode::PROCESSING => ResponseLength::None,
| StatusCode::PROCESSING => ResponseLength::None, _ => ResponseLength::Zero,
_ => ResponseLength::Zero, };
};
}
*self = Output::Empty(buf); *self = Output::Empty(buf);
return; return;
} }

View file

@ -1358,8 +1358,8 @@ fn test_ssl_handshake_timeout() {
#[test] #[test]
fn test_content_length() { fn test_content_length() {
use http::StatusCode;
use actix_web::http::header::{HeaderName, HeaderValue}; use actix_web::http::header::{HeaderName, HeaderValue};
use http::StatusCode;
let mut srv = test::TestServer::new(move |app| { let mut srv = test::TestServer::new(move |app| {
app.resource("/{status}", |r| { app.resource("/{status}", |r| {
@ -1398,4 +1398,3 @@ fn test_content_length() {
assert_eq!(response.headers().get(&header), Some(&value)); assert_eq!(response.headers().get(&header), Some(&value));
} }
} }