Skip to content

Commit

Permalink
nghttpx: Ignore Content-Length and Transfer-Encoding in 1xx or 200 to…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
tatsuhiro-t committed May 17, 2019
1 parent 6975c33 commit 4fca250
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/shrpx_downstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/shrpx_downstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class FieldStore {

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;

Expand Down
11 changes: 4 additions & 7 deletions src/shrpx_http_downstream_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4fca250

Please sign in to comment.