nghttpx: Refactor Downstream::response_bodylen_
This commit is contained in:
parent
bdef0e0b1a
commit
e7e52b11ce
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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_;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<Downstream *>(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<const uint8_t *>(data), len, true);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue