From 0872ce27f456d1cd541ebbf3be53ebf728bfbc35 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 18 Jan 2014 16:50:52 +0900 Subject: [PATCH] nghttpx: Use nghttp2_session_mem_recv instead of nghttp2_session_recv --- src/shrpx_http2_session.cc | 57 ++++++++++++++--------------------- src/shrpx_http2_upstream.cc | 59 +++++++++++++++---------------------- 2 files changed, 45 insertions(+), 71 deletions(-) diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 3c4dd1dd..c7925a24 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -711,24 +711,6 @@ ssize_t send_callback(nghttp2_session *session, } } // namespace -namespace { -ssize_t recv_callback(nghttp2_session *session, - uint8_t *data, size_t len, int flags, void *user_data) -{ - auto http2session = reinterpret_cast(user_data); - auto bev = http2session->get_bev(); - auto input = bufferevent_get_input(bev); - int nread = evbuffer_remove(input, data, len); - if(nread == -1) { - return NGHTTP2_ERR_CALLBACK_FAILURE; - } else if(nread == 0) { - return NGHTTP2_ERR_WOULDBLOCK; - } else { - return nread; - } -} -} // namespace - namespace { int on_stream_close_callback (nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code, @@ -1215,7 +1197,6 @@ int Http2Session::on_connect() nghttp2_session_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.send_callback = send_callback; - callbacks.recv_callback = recv_callback; callbacks.on_stream_close_callback = on_stream_close_callback; callbacks.on_frame_recv_callback = on_frame_recv_callback; callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback; @@ -1290,27 +1271,33 @@ int Http2Session::on_connect() int Http2Session::on_read() { - int rv = 0; - if((rv = nghttp2_session_recv(session_)) < 0) { - if(rv != NGHTTP2_ERR_EOF) { - SSLOG(ERROR, this) << "nghttp2_session_recv() returned error: " - << nghttp2_strerror(rv); - } - } else if((rv = nghttp2_session_send(session_)) < 0) { + ssize_t rv = 0; + auto input = bufferevent_get_input(bev_); + auto inputlen = evbuffer_get_length(input); + auto mem = evbuffer_pullup(input, -1); + + rv = nghttp2_session_mem_recv(session_, mem, inputlen); + if(rv < 0) { + SSLOG(ERROR, this) << "nghttp2_session_recv() returned error: " + << nghttp2_strerror(rv); + return -1; + } + evbuffer_drain(input, rv); + rv = nghttp2_session_send(session_); + if(rv < 0) { SSLOG(ERROR, this) << "nghttp2_session_send() returned error: " << nghttp2_strerror(rv); + return -1; } - if(rv == 0) { - if(nghttp2_session_want_read(session_) == 0 && - nghttp2_session_want_write(session_) == 0 && - evbuffer_get_length(bufferevent_get_output(bev_)) == 0) { - if(LOG_ENABLED(INFO)) { - SSLOG(INFO, this) << "No more read/write for this session"; - } - rv = -1; + if(nghttp2_session_want_read(session_) == 0 && + nghttp2_session_want_write(session_) == 0 && + evbuffer_get_length(bufferevent_get_output(bev_)) == 0) { + if(LOG_ENABLED(INFO)) { + SSLOG(INFO, this) << "No more read/write for this session"; } + return -1; } - return rv; + return 0; } int Http2Session::on_write() diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 0096a0f9..3a9c5e86 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -73,25 +73,6 @@ ssize_t send_callback(nghttp2_session *session, } } // namespace -namespace { -ssize_t recv_callback(nghttp2_session *session, - uint8_t *data, size_t len, int flags, void *user_data) -{ - auto upstream = reinterpret_cast(user_data); - auto handler = upstream->get_client_handler(); - auto bev = handler->get_bev(); - auto input = bufferevent_get_input(bev); - int nread = evbuffer_remove(input, data, len); - if(nread == -1) { - return NGHTTP2_ERR_CALLBACK_FAILURE; - } else if(nread == 0) { - return NGHTTP2_ERR_WOULDBLOCK; - } else { - return nread; - } -} -} // namespace - namespace { int on_stream_close_callback (nghttp2_session *session, int32_t stream_id, nghttp2_error_code error_code, @@ -533,7 +514,6 @@ Http2Upstream::Http2Upstream(ClientHandler *handler) nghttp2_session_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.send_callback = send_callback; - callbacks.recv_callback = recv_callback; callbacks.on_stream_close_callback = on_stream_close_callback; callbacks.on_frame_recv_callback = on_frame_recv_callback; callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback; @@ -593,27 +573,34 @@ Http2Upstream::~Http2Upstream() int Http2Upstream::on_read() { - int rv = 0; - if((rv = nghttp2_session_recv(session_)) < 0) { - if(rv != NGHTTP2_ERR_EOF) { - ULOG(ERROR, this) << "nghttp2_session_recv() returned error: " - << nghttp2_strerror(rv); - } - } else if((rv = nghttp2_session_send(session_)) < 0) { + ssize_t rv = 0; + auto bev = handler_->get_bev(); + auto input = bufferevent_get_input(bev); + auto inputlen = evbuffer_get_length(input); + auto mem = evbuffer_pullup(input, -1); + + rv = nghttp2_session_mem_recv(session_, mem, inputlen); + if(rv < 0) { + ULOG(ERROR, this) << "nghttp2_session_recv() returned error: " + << nghttp2_strerror(rv); + return -1; + } + evbuffer_drain(input, rv); + rv = nghttp2_session_send(session_); + if(rv < 0) { ULOG(ERROR, this) << "nghttp2_session_send() returned error: " << nghttp2_strerror(rv); + return -1; } - if(rv == 0) { - if(nghttp2_session_want_read(session_) == 0 && - nghttp2_session_want_write(session_) == 0 && - evbuffer_get_length(bufferevent_get_output(handler_->get_bev())) == 0) { - if(LOG_ENABLED(INFO)) { - ULOG(INFO, this) << "No more read/write for this HTTP2 session"; - } - rv = -1; + if(nghttp2_session_want_read(session_) == 0 && + nghttp2_session_want_write(session_) == 0 && + evbuffer_get_length(bufferevent_get_output(bev)) == 0) { + if(LOG_ENABLED(INFO)) { + ULOG(INFO, this) << "No more read/write for this HTTP2 session"; } + rv = -1; } - return rv; + return 0; } int Http2Upstream::on_write()