From e7e52b11ce2b654fd37adbf76628bd3d8b676db3 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 14 Jan 2016 23:20:44 +0900 Subject: [PATCH] nghttpx: Refactor Downstream::response_bodylen_ --- src/shrpx_downstream.cc | 20 +++++++------------- src/shrpx_downstream.h | 13 +++++-------- src/shrpx_http2_session.cc | 4 +++- src/shrpx_http2_upstream.cc | 2 +- src/shrpx_http_downstream_connection.cc | 3 ++- src/shrpx_https_upstream.cc | 2 +- src/shrpx_spdy_upstream.cc | 2 +- 7 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/shrpx_downstream.cc b/src/shrpx_downstream.cc index c14c5eaf..11ef248e 100644 --- a/src/shrpx_downstream.cc +++ b/src/shrpx_downstream.cc @@ -115,10 +115,10 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool, int32_t stream_id, int32_t priority) : dlnext(nullptr), dlprev(nullptr), request_start_time_(std::chrono::high_resolution_clock::now()), - request_buf_(mcpool), response_buf_(mcpool), response_bodylen_(0), - response_sent_bodylen_(0), upstream_(upstream), blocked_link_(nullptr), - request_datalen_(0), response_datalen_(0), num_retry_(0), - stream_id_(stream_id), priority_(priority), downstream_stream_id_(-1), + request_buf_(mcpool), response_buf_(mcpool), response_sent_bodylen_(0), + upstream_(upstream), blocked_link_(nullptr), request_datalen_(0), + response_datalen_(0), num_retry_(0), stream_id_(stream_id), + priority_(priority), downstream_stream_id_(-1), response_rst_stream_error_code_(NGHTTP2_NO_ERROR), request_state_(INITIAL), response_state_(INITIAL), dispatch_state_(DISPATCH_NONE), upgraded_(false), chunked_request_(false), @@ -628,12 +628,6 @@ bool Downstream::response_buf_full() { } } -void Downstream::add_response_bodylen(size_t amount) { - response_bodylen_ += amount; -} - -int64_t Downstream::get_response_bodylen() const { return response_bodylen_; } - void Downstream::add_response_sent_bodylen(size_t amount) { response_sent_bodylen_ += amount; } @@ -659,16 +653,16 @@ bool Downstream::validate_request_recv_body_length() const { return true; } -bool Downstream::validate_response_bodylen() const { +bool Downstream::validate_response_recv_body_length() const { if (!expect_response_body() || resp_.fs.content_length == -1) { return true; } - if (resp_.fs.content_length != response_bodylen_) { + if (resp_.fs.content_length != resp_.recv_body_length) { if (LOG_ENABLED(INFO)) { DLOG(INFO, this) << "response invalid bodylen: content-length=" << resp_.fs.content_length - << ", received=" << response_bodylen_; + << ", received=" << resp_.recv_body_length; } return false; } diff --git a/src/shrpx_downstream.h b/src/shrpx_downstream.h index 8c80d1a2..2a25b1f2 100644 --- a/src/shrpx_downstream.h +++ b/src/shrpx_downstream.h @@ -161,10 +161,12 @@ struct Request { struct Response { Response() - : fs(32), http_status(0), http_major(1), http_minor(1), - connection_close(false) {} + : fs(32), recv_body_length(0), http_status(0), http_major(1), + http_minor(1), connection_close(false) {} FieldStore fs; + // the length of response body received so far + int64_t recv_body_length; // HTTP status code unsigned int http_status; int http_major, http_minor; @@ -282,13 +284,11 @@ public: int get_response_state() const; DefaultMemchunks *get_response_buf(); bool response_buf_full(); - void add_response_bodylen(size_t amount); - int64_t get_response_bodylen() const; void add_response_sent_bodylen(size_t amount); int64_t get_response_sent_bodylen() const; // Validates that received response body length and content-length // matches. - bool validate_response_bodylen() const; + bool validate_response_recv_body_length() const; uint32_t get_response_rst_stream_error_code() const; void set_response_rst_stream_error_code(uint32_t error_code); // Inspects HTTP/1 response. This checks tranfer-encoding etc. @@ -392,9 +392,6 @@ private: ev_timer downstream_rtimer_; ev_timer downstream_wtimer_; - // the length of response body received so far - int64_t response_bodylen_; - // the length of response body sent to upstream client int64_t response_sent_bodylen_; diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 477aeda2..61e53282 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -1177,7 +1177,9 @@ int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags, downstream->reset_downstream_rtimer(); - downstream->add_response_bodylen(len); + auto &resp = downstream->response(); + + resp.recv_body_length += len; auto upstream = downstream->get_upstream(); rv = upstream->on_downstream_body(downstream, data, len, false); diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index dc227613..c7c81f4a 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -1624,7 +1624,7 @@ int Http2Upstream::on_downstream_body_complete(Downstream *downstream) { auto &resp = downstream->response(); - if (!downstream->validate_response_bodylen()) { + if (!downstream->validate_response_recv_body_length()) { rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR); resp.connection_close = true; return 0; diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 7d985fb6..eb1e9766 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -597,8 +597,9 @@ int htp_hdr_valcb(http_parser *htp, const char *data, size_t len) { namespace { int htp_bodycb(http_parser *htp, const char *data, size_t len) { auto downstream = static_cast(htp->data); + auto &resp = downstream->response(); - downstream->add_response_bodylen(len); + resp.recv_body_length += len; return downstream->get_upstream()->on_downstream_body( downstream, reinterpret_cast(data), len, true); diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index 2ca2fe8f..ce394549 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -1076,7 +1076,7 @@ int HttpsUpstream::on_downstream_body_complete(Downstream *downstream) { DLOG(INFO, downstream) << "HTTP response completed"; } - if (!downstream->validate_response_bodylen()) { + if (!downstream->validate_response_recv_body_length()) { resp.connection_close = true; } diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index e518b568..def2703c 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -1093,7 +1093,7 @@ int SpdyUpstream::on_downstream_body_complete(Downstream *downstream) { auto &resp = downstream->response(); - if (!downstream->validate_response_bodylen()) { + if (!downstream->validate_response_recv_body_length()) { rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR); resp.connection_close = true; return 0;