From 292f219900f5cfba402ae7918c99ef302b82c6e5 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 24 Dec 2014 00:31:21 +0900 Subject: [PATCH] nghttpd: Avoid sending last 0-sized DATA --- src/HttpServer.cc | 9 ++++++--- src/HttpServer.h | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 129e90d5..b505a7c8 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -110,8 +110,8 @@ Config::Config() Config::~Config() { nghttp2_option_del(session_option); } Stream::Stream(Http2Handler *handler, int32_t stream_id) - : handler(handler), rtimer(nullptr), wtimer(nullptr), stream_id(stream_id), - file(-1) {} + : handler(handler), rtimer(nullptr), wtimer(nullptr), body_left(0), + stream_id(stream_id), file(-1) {} Stream::~Stream() { if (file != -1) { @@ -692,7 +692,8 @@ ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id, return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; } - if (nread == 0) { + stream->body_left -= nread; + if (nread == 0 || stream->body_left <= 0) { *data_flags |= NGHTTP2_DATA_FLAG_EOF; if (nghttp2_session_get_stream_remote_close(session, stream_id) == 0) { @@ -747,6 +748,7 @@ void prepare_status_response(Stream *stream, Http2Handler *hd, close(pipefd[1]); stream->file = pipefd[0]; + stream->body_left = body.size(); nghttp2_data_provider data_prd; data_prd.source.fd = pipefd[0]; data_prd.read_callback = file_read_callback; @@ -856,6 +858,7 @@ void prepare_response(Stream *stream, Http2Handler *hd, } stream->file = file; + stream->body_left = buf.st_size; nghttp2_data_provider data_prd; diff --git a/src/HttpServer.h b/src/HttpServer.h index 9596bbd4..38d8f5e1 100644 --- a/src/HttpServer.h +++ b/src/HttpServer.h @@ -90,6 +90,7 @@ struct Stream { Http2Handler *handler; event *rtimer; event *wtimer; + int64_t body_left; int32_t stream_id; int file; Stream(Http2Handler *handler, int32_t stream_id);