nghttpx: Refactor Downstream::response_bodylen_

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-14 23:20:44 +09:00
parent bdef0e0b1a
commit e7e52b11ce
7 changed files with 20 additions and 26 deletions

View File

@ -115,10 +115,10 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool,
int32_t stream_id, int32_t priority) int32_t stream_id, int32_t priority)
: dlnext(nullptr), dlprev(nullptr), : dlnext(nullptr), dlprev(nullptr),
request_start_time_(std::chrono::high_resolution_clock::now()), request_start_time_(std::chrono::high_resolution_clock::now()),
request_buf_(mcpool), response_buf_(mcpool), response_bodylen_(0), request_buf_(mcpool), response_buf_(mcpool), response_sent_bodylen_(0),
response_sent_bodylen_(0), upstream_(upstream), blocked_link_(nullptr), upstream_(upstream), blocked_link_(nullptr), request_datalen_(0),
request_datalen_(0), response_datalen_(0), num_retry_(0), response_datalen_(0), num_retry_(0), stream_id_(stream_id),
stream_id_(stream_id), priority_(priority), downstream_stream_id_(-1), priority_(priority), downstream_stream_id_(-1),
response_rst_stream_error_code_(NGHTTP2_NO_ERROR), response_rst_stream_error_code_(NGHTTP2_NO_ERROR),
request_state_(INITIAL), response_state_(INITIAL), request_state_(INITIAL), response_state_(INITIAL),
dispatch_state_(DISPATCH_NONE), upgraded_(false), chunked_request_(false), 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) { void Downstream::add_response_sent_bodylen(size_t amount) {
response_sent_bodylen_ += amount; response_sent_bodylen_ += amount;
} }
@ -659,16 +653,16 @@ bool Downstream::validate_request_recv_body_length() const {
return true; 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) { if (!expect_response_body() || resp_.fs.content_length == -1) {
return true; return true;
} }
if (resp_.fs.content_length != response_bodylen_) { if (resp_.fs.content_length != resp_.recv_body_length) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
DLOG(INFO, this) << "response invalid bodylen: content-length=" DLOG(INFO, this) << "response invalid bodylen: content-length="
<< resp_.fs.content_length << resp_.fs.content_length
<< ", received=" << response_bodylen_; << ", received=" << resp_.recv_body_length;
} }
return false; return false;
} }

View File

@ -161,10 +161,12 @@ struct Request {
struct Response { struct Response {
Response() Response()
: fs(32), http_status(0), http_major(1), http_minor(1), : fs(32), recv_body_length(0), http_status(0), http_major(1),
connection_close(false) {} http_minor(1), connection_close(false) {}
FieldStore fs; FieldStore fs;
// the length of response body received so far
int64_t recv_body_length;
// HTTP status code // HTTP status code
unsigned int http_status; unsigned int http_status;
int http_major, http_minor; int http_major, http_minor;
@ -282,13 +284,11 @@ public:
int get_response_state() const; int get_response_state() const;
DefaultMemchunks *get_response_buf(); DefaultMemchunks *get_response_buf();
bool response_buf_full(); 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); void add_response_sent_bodylen(size_t amount);
int64_t get_response_sent_bodylen() const; int64_t get_response_sent_bodylen() const;
// Validates that received response body length and content-length // Validates that received response body length and content-length
// matches. // matches.
bool validate_response_bodylen() const; bool validate_response_recv_body_length() const;
uint32_t get_response_rst_stream_error_code() const; uint32_t get_response_rst_stream_error_code() const;
void set_response_rst_stream_error_code(uint32_t error_code); void set_response_rst_stream_error_code(uint32_t error_code);
// Inspects HTTP/1 response. This checks tranfer-encoding etc. // Inspects HTTP/1 response. This checks tranfer-encoding etc.
@ -392,9 +392,6 @@ private:
ev_timer downstream_rtimer_; ev_timer downstream_rtimer_;
ev_timer downstream_wtimer_; 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 // the length of response body sent to upstream client
int64_t response_sent_bodylen_; int64_t response_sent_bodylen_;

View File

@ -1177,7 +1177,9 @@ int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
downstream->reset_downstream_rtimer(); downstream->reset_downstream_rtimer();
downstream->add_response_bodylen(len); auto &resp = downstream->response();
resp.recv_body_length += len;
auto upstream = downstream->get_upstream(); auto upstream = downstream->get_upstream();
rv = upstream->on_downstream_body(downstream, data, len, false); rv = upstream->on_downstream_body(downstream, data, len, false);

View File

@ -1624,7 +1624,7 @@ int Http2Upstream::on_downstream_body_complete(Downstream *downstream) {
auto &resp = downstream->response(); auto &resp = downstream->response();
if (!downstream->validate_response_bodylen()) { if (!downstream->validate_response_recv_body_length()) {
rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR); rst_stream(downstream, NGHTTP2_PROTOCOL_ERROR);
resp.connection_close = true; resp.connection_close = true;
return 0; return 0;

View File

@ -597,8 +597,9 @@ int htp_hdr_valcb(http_parser *htp, const char *data, size_t len) {
namespace { namespace {
int htp_bodycb(http_parser *htp, const char *data, size_t len) { int htp_bodycb(http_parser *htp, const char *data, size_t len) {
auto downstream = static_cast<Downstream *>(htp->data); auto downstream = static_cast<Downstream *>(htp->data);
auto &resp = downstream->response();
downstream->add_response_bodylen(len); resp.recv_body_length += len;
return downstream->get_upstream()->on_downstream_body( return downstream->get_upstream()->on_downstream_body(
downstream, reinterpret_cast<const uint8_t *>(data), len, true); downstream, reinterpret_cast<const uint8_t *>(data), len, true);

View File

@ -1076,7 +1076,7 @@ int HttpsUpstream::on_downstream_body_complete(Downstream *downstream) {
DLOG(INFO, downstream) << "HTTP response completed"; DLOG(INFO, downstream) << "HTTP response completed";
} }
if (!downstream->validate_response_bodylen()) { if (!downstream->validate_response_recv_body_length()) {
resp.connection_close = true; resp.connection_close = true;
} }

View File

@ -1093,7 +1093,7 @@ int SpdyUpstream::on_downstream_body_complete(Downstream *downstream) {
auto &resp = downstream->response(); auto &resp = downstream->response();
if (!downstream->validate_response_bodylen()) { if (!downstream->validate_response_recv_body_length()) {
rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR); rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
resp.connection_close = true; resp.connection_close = true;
return 0; return 0;