nghttpx: Loose HTTP Upgrade condition

This commit is contained in:
Tatsuhiro Tsujikawa 2015-12-16 00:38:30 +09:00
parent 71012fe83a
commit ef7d6e8a0c
2 changed files with 33 additions and 11 deletions

View File

@ -718,6 +718,34 @@ void ClientHandler::direct_http2_upgrade() {
int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) { int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) {
auto upstream = make_unique<Http2Upstream>(this); auto upstream = make_unique<Http2Upstream>(this);
auto output = upstream->get_response_buf();
// We might have written non-final header in response_buf, in this
// case, response_state is still INITIAL. If this non-final header
// and upgrade header fit in output buffer, do upgrade. Otherwise,
// to avoid to send this non-final header as response body in HTTP/2
// upstream, fail upgrade.
auto downstream = http->get_downstream();
auto input = downstream->get_response_buf();
static constexpr char res[] =
"HTTP/1.1 101 Switching Protocols\r\n"
"Connection: Upgrade\r\n"
"Upgrade: " NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "\r\n"
"\r\n";
auto required_size = str_size(res) + input->rleft();
if (output->wleft() < required_size) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this)
<< "HTTP Upgrade failed because of insufficient buffer space: need "
<< required_size << ", available " << output->wleft();
}
return -1;
}
if (upstream->upgrade_upstream(http) != 0) { if (upstream->upgrade_upstream(http) != 0) {
return -1; return -1;
} }
@ -729,12 +757,11 @@ int ClientHandler::perform_http2_upgrade(HttpsUpstream *http) {
on_read_ = &ClientHandler::upstream_http2_connhd_read; on_read_ = &ClientHandler::upstream_http2_connhd_read;
write_ = &ClientHandler::write_clear; write_ = &ClientHandler::write_clear;
static constexpr char res[] = auto nread =
"HTTP/1.1 101 Switching Protocols\r\n" downstream->get_response_buf()->remove(output->last, output->wleft());
"Connection: Upgrade\r\n" output->write(nread);
"Upgrade: " NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "\r\n"
"\r\n"; output->write(res, str_size(res));
upstream->get_response_buf()->write(res, sizeof(res) - 1);
upstream_ = std::move(upstream); upstream_ = std::move(upstream);
signal_write(); signal_write();

View File

@ -409,11 +409,6 @@ int htp_msg_completecb(http_parser *htp) {
if (handler->get_http2_upgrade_allowed() && if (handler->get_http2_upgrade_allowed() &&
downstream->get_http2_upgrade_request() && downstream->get_http2_upgrade_request() &&
// we may write non-final header in response_buf, in this case,
// response_state is still INITIAL. So don't upgrade in this
// case, otherwise we end up send this non-final header as
// response body in HTTP/2 upstream.
downstream->get_response_buf()->rleft() == 0 &&
handler->perform_http2_upgrade(upstream) != 0) { handler->perform_http2_upgrade(upstream) != 0) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
ULOG(INFO, upstream) << "HTTP Upgrade to HTTP/2 failed"; ULOG(INFO, upstream) << "HTTP Upgrade to HTTP/2 failed";