nghttpx: Refactor Downstream::request_bodylen_

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-14 23:14:58 +09:00
parent 543f2d58fc
commit bdef0e0b1a
3 changed files with 14 additions and 15 deletions

View File

@ -115,11 +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), request_bodylen_(0), request_buf_(mcpool), response_buf_(mcpool), response_bodylen_(0),
response_bodylen_(0), response_sent_bodylen_(0), upstream_(upstream), response_sent_bodylen_(0), upstream_(upstream), blocked_link_(nullptr),
blocked_link_(nullptr), request_datalen_(0), response_datalen_(0), request_datalen_(0), response_datalen_(0), num_retry_(0),
num_retry_(0), stream_id_(stream_id), priority_(priority), stream_id_(stream_id), priority_(priority), downstream_stream_id_(-1),
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),
@ -518,7 +517,7 @@ int Downstream::push_upload_data_chunk(const uint8_t *data, size_t datalen) {
DLOG(INFO, this) << "dconn_ is NULL"; DLOG(INFO, this) << "dconn_ is NULL";
return -1; return -1;
} }
request_bodylen_ += datalen; req_.recv_body_length += datalen;
if (dconn_->push_upload_data_chunk(data, datalen) != 0) { if (dconn_->push_upload_data_chunk(data, datalen) != 0) {
return -1; return -1;
} }
@ -643,16 +642,16 @@ int64_t Downstream::get_response_sent_bodylen() const {
return response_sent_bodylen_; return response_sent_bodylen_;
} }
bool Downstream::validate_request_bodylen() const { bool Downstream::validate_request_recv_body_length() const {
if (req_.fs.content_length == -1) { if (req_.fs.content_length == -1) {
return true; return true;
} }
if (req_.fs.content_length != request_bodylen_) { if (req_.fs.content_length != req_.recv_body_length) {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
DLOG(INFO, this) << "request invalid bodylen: content-length=" DLOG(INFO, this) << "request invalid bodylen: content-length="
<< req_.fs.content_length << req_.fs.content_length
<< ", received=" << request_bodylen_; << ", received=" << req_.recv_body_length;
} }
return false; return false;
} }

View File

@ -124,7 +124,7 @@ private:
struct Request { struct Request {
Request() Request()
: fs(16), method(-1), http_major(1), http_minor(1), : fs(16), recv_body_length(0), method(-1), http_major(1), http_minor(1),
upgrade_request(false), http2_upgrade_seen(false), upgrade_request(false), http2_upgrade_seen(false),
connection_close(false), http2_expect_body(false) {} connection_close(false), http2_expect_body(false) {}
@ -142,6 +142,8 @@ struct Request {
// request-target. For HTTP/2, this is :path header field value. // request-target. For HTTP/2, this is :path header field value.
// For CONNECT request, this is empty. // For CONNECT request, this is empty.
std::string path; std::string path;
// the length of request body received so far
int64_t recv_body_length;
int method; int method;
// HTTP major and minor version // HTTP major and minor version
int http_major, http_minor; int http_major, http_minor;
@ -239,7 +241,7 @@ public:
void reset_request_datalen(); void reset_request_datalen();
// Validates that received request body length and content-length // Validates that received request body length and content-length
// matches. // matches.
bool validate_request_bodylen() const; bool validate_request_recv_body_length() const;
void set_request_downstream_host(std::string host); void set_request_downstream_host(std::string host);
bool expect_response_body() const; bool expect_response_body() const;
enum { enum {
@ -390,8 +392,6 @@ private:
ev_timer downstream_rtimer_; ev_timer downstream_rtimer_;
ev_timer downstream_wtimer_; ev_timer downstream_wtimer_;
// the length of request body received so far
int64_t request_bodylen_;
// the length of response body received so far // the length of response body received so far
int64_t response_bodylen_; int64_t response_bodylen_;

View File

@ -263,7 +263,7 @@ void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
#endif // HAVE_MRUBY #endif // HAVE_MRUBY
if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) { if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
if (!downstream->validate_request_bodylen()) { if (!downstream->validate_request_recv_body_length()) {
upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR); upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
return; return;
} }
@ -386,7 +386,7 @@ void on_data_recv_callback(spdylay_session *session, uint8_t flags,
auto downstream = static_cast<Downstream *>( auto downstream = static_cast<Downstream *>(
spdylay_session_get_stream_user_data(session, stream_id)); spdylay_session_get_stream_user_data(session, stream_id));
if (downstream && (flags & SPDYLAY_DATA_FLAG_FIN)) { if (downstream && (flags & SPDYLAY_DATA_FLAG_FIN)) {
if (!downstream->validate_request_bodylen()) { if (!downstream->validate_request_recv_body_length()) {
upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR); upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
return; return;
} }