From 8026bdd45a15ad019459b72adec7c63f495da6a4 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 14 May 2016 17:16:50 +0900 Subject: [PATCH] nghttpx: Don't keep backend connection if request buffer is not empty --- src/shrpx_downstream.cc | 5 ++++- src/shrpx_https_upstream.cc | 9 ++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index e517bdf7..b8d16066 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -905,9 +905,12 @@ BlockedLink *Downstream::detach_blocked_link() { } bool Downstream::can_detach_downstream_connection() const { + // We should check request and response buffer. If request buffer + // is not empty, then we might leave downstream connection in weird + // state, especially for HTTP/1.1 return dconn_ && response_state_ == Downstream::MSG_COMPLETE && request_state_ == Downstream::MSG_COMPLETE && !upgraded_ && - !resp_.connection_close; + !resp_.connection_close && request_buf_.rleft() == 0; } DefaultMemchunks Downstream::pop_response_buf() { diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 5bfeab50..ed22f473 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -628,14 +628,13 @@ int HttpsUpstream::on_write() { // We need to postpone detachment until all data are sent so that // we can notify nghttp2 library all data consumed. if (downstream->get_response_state() == Downstream::MSG_COMPLETE) { - if (resp.connection_close || - downstream->get_request_state() != Downstream::MSG_COMPLETE) { + if (downstream->can_detach_downstream_connection()) { + // Keep-alive + downstream->detach_downstream_connection(); + } else { // Connection close downstream->pop_downstream_connection(); // dconn was deleted - } else { - // Keep-alive - downstream->detach_downstream_connection(); } // We need this if response ends before request. if (downstream->get_request_state() == Downstream::MSG_COMPLETE) {