nghttpx: Ignore Content-Length and Transfer-Encoding in 1xx or 200 to CONNECT

A well known server sends content-length: 0 in 101 response.  RFC 7230
says Content-Length or Transfer-Encoding in 200 response to CONNECT
request: https://tools.ietf.org/html/rfc7230#section-3.3.3
This commit is contained in:
Tatsuhiro Tsujikawa 2019-05-17 22:04:46 +09:00
parent 6975c336fc
commit 4fca2502d8
3 changed files with 20 additions and 7 deletions

View File

@ -573,6 +573,18 @@ void FieldStore::append_last_trailer_value(const char *data, size_t len) {
trailers_, data, len);
}
void FieldStore::erase_content_length_and_transfer_encoding() {
for (auto &kv : headers_) {
switch (kv.token) {
case http2::HD_CONTENT_LENGTH:
case http2::HD_TRANSFER_ENCODING:
kv.name = StringRef{};
kv.token = -1;
break;
}
}
}
void Downstream::set_request_start_time(
std::chrono::high_resolution_clock::time_point time) {
request_start_time_ = std::move(time);

View File

@ -119,6 +119,10 @@ public:
bool trailer_key_prev() const { return trailer_key_prev_; }
// erase_content_length_and_transfer_encoding erases content-length
// and transfer-encoding header fields.
void erase_content_length_and_transfer_encoding();
// content-length, -1 if it is unknown.
int64_t content_length;

View File

@ -935,18 +935,15 @@ int htp_hdrs_completecb(llhttp_t *htp) {
return -1;
}
if (resp.fs.content_length == 0) {
auto cl = resp.fs.header(http2::HD_CONTENT_LENGTH);
assert(cl);
http2::erase_header(cl);
resp.fs.erase_content_length_and_transfer_encoding();
} else if (resp.fs.content_length != -1) {
return -1;
}
} else if (resp.http_status / 100 == 1 ||
(resp.http_status / 100 == 2 && req.method == HTTP_CONNECT)) {
if (resp.fs.header(http2::HD_CONTENT_LENGTH) ||
resp.fs.header(http2::HD_TRANSFER_ENCODING)) {
return -1;
}
// Server MUST NOT send Content-Length and Transfer-Encoding in
// these responses.
resp.fs.erase_content_length_and_transfer_encoding();
} else if (resp.fs.parse_content_length() != 0) {
downstream->set_response_state(DownstreamState::MSG_BAD_HEADER);
return -1;