nghttp2_data_source_read_callback: Replace eof with uint32_t *data_flags

Replace int *eof with uint32_t *data_flags so that we can easily
extend functionality if we have to (but we don't do if possible).
This commit is contained in:
Tatsuhiro Tsujikawa 2014-04-05 17:59:24 +09:00
parent a0d93e7744
commit e7ad3633c7
8 changed files with 52 additions and 34 deletions

View File

@ -328,7 +328,7 @@ static char* percent_decode(const uint8_t *value, size_t valuelen)
static ssize_t file_read_callback static ssize_t file_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t length, int *eof, uint8_t *buf, size_t length, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
int fd = source->fd; int fd = source->fd;
@ -338,7 +338,7 @@ static ssize_t file_read_callback
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} }
if(r == 0) { if(r == 0) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
} }
return r; return r;
} }

View File

@ -621,6 +621,23 @@ typedef union {
void *ptr; void *ptr;
} nghttp2_data_source; } nghttp2_data_source;
/**
* @enum
*
* The flags used to set in |data_flags| output parameter in
* :type:`nghttp2_data_source_read_callback`.
*/
typedef enum {
/**
* No flag set.
*/
NGHTTP2_DATA_FLAG_NONE = 0,
/**
* Indicates EOF was sensed.
*/
NGHTTP2_DATA_FLAG_EOF = 0x01
} nghttp2_data_flag;
/** /**
* @functypedef * @functypedef
* *
@ -629,21 +646,22 @@ typedef union {
* implementation of this function must read at most |length| bytes of * implementation of this function must read at most |length| bytes of
* data from |source| (or possibly other places) and store them in * data from |source| (or possibly other places) and store them in
* |buf| and return number of data stored in |buf|. If EOF is reached, * |buf| and return number of data stored in |buf|. If EOF is reached,
* set |*eof| to 1. If the application wants to postpone DATA frames, * set :enum:`NGHTTP2_DATA_FLAG_EOF` flag in |*data_falgs|. If the
* (e.g., asynchronous I/O, or reading data blocks for long time), it * application wants to postpone DATA frames, (e.g., asynchronous I/O,
* is achieved by returning :enum:`NGHTTP2_ERR_DEFERRED` without * or reading data blocks for long time), it is achieved by returning
* reading any data in this invocation. The library removes DATA * :enum:`NGHTTP2_ERR_DEFERRED` without reading any data in this
* frame from the outgoing queue temporarily. To move back deferred * invocation. The library removes DATA frame from the outgoing queue
* DATA frame to outgoing queue, call `nghttp2_session_resume_data()`. * temporarily. To move back deferred DATA frame to outgoing queue,
* In case of error, there are 2 choices. Returning * call `nghttp2_session_resume_data()`. In case of error, there are
* :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close the stream * 2 choices. Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`
* by issuing RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`. * will close the stream by issuing RST_STREAM with
* Returning :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will signal the * :enum:`NGHTTP2_INTERNAL_ERROR`. Returning
* entire session failure. * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will signal the entire session
* failure.
*/ */
typedef ssize_t (*nghttp2_data_source_read_callback) typedef ssize_t (*nghttp2_data_source_read_callback)
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t length, int *eof, uint8_t *buf, size_t length, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data); nghttp2_data_source *source, void *user_data);
/** /**

View File

@ -5388,7 +5388,7 @@ int nghttp2_session_pack_data(nghttp2_session *session,
nghttp2_private_data *frame) nghttp2_private_data *frame)
{ {
ssize_t rv; ssize_t rv;
int eof_flags; uint32_t data_flags;
uint8_t flags; uint8_t flags;
ssize_t payloadlen; ssize_t payloadlen;
ssize_t padded_payloadlen; ssize_t padded_payloadlen;
@ -5404,10 +5404,10 @@ int nghttp2_session_pack_data(nghttp2_session *session,
/* Current max DATA length is less then buffer chunk size */ /* Current max DATA length is less then buffer chunk size */
assert(nghttp2_buf_avail(buf) >= (ssize_t)datamax); assert(nghttp2_buf_avail(buf) >= (ssize_t)datamax);
eof_flags = 0; data_flags = NGHTTP2_DATA_FLAG_NONE;
payloadlen = frame->data_prd.read_callback payloadlen = frame->data_prd.read_callback
(session, frame->hd.stream_id, buf->pos, datamax, (session, frame->hd.stream_id, buf->pos, datamax,
&eof_flags, &frame->data_prd.source, session->user_data); &data_flags, &frame->data_prd.source, session->user_data);
if(payloadlen == NGHTTP2_ERR_DEFERRED || if(payloadlen == NGHTTP2_ERR_DEFERRED ||
payloadlen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { payloadlen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
@ -5430,7 +5430,7 @@ int nghttp2_session_pack_data(nghttp2_session *session,
frame->hd.flags &= (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_SEGMENT); frame->hd.flags &= (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_SEGMENT);
flags = NGHTTP2_FLAG_NONE; flags = NGHTTP2_FLAG_NONE;
if(eof_flags) { if(data_flags & NGHTTP2_DATA_FLAG_EOF) {
frame->eof = 1; frame->eof = 1;
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
flags |= NGHTTP2_FLAG_END_STREAM; flags |= NGHTTP2_FLAG_END_STREAM;

View File

@ -922,7 +922,7 @@ void Http2Handler::terminate_session(nghttp2_error_code error_code)
ssize_t file_read_callback ssize_t file_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t length, int *eof, uint8_t *buf, size_t length, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
auto hd = static_cast<Http2Handler*>(user_data); auto hd = static_cast<Http2Handler*>(user_data);
@ -942,7 +942,7 @@ ssize_t file_read_callback
} }
if(r == 0) { if(r == 0) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
} }
return r; return r;

View File

@ -1669,7 +1669,7 @@ int communicate(const std::string& scheme, const std::string& host,
namespace { namespace {
ssize_t file_read_callback ssize_t file_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t length, int *eof, uint8_t *buf, size_t length, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
auto req = (Request*)nghttp2_session_get_stream_user_data auto req = (Request*)nghttp2_session_get_stream_user_data
@ -1683,7 +1683,7 @@ ssize_t file_read_callback
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
} else { } else {
if(r == 0) { if(r == 0) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
} else { } else {
req->data_offset += r; req->data_offset += r;
} }

View File

@ -154,7 +154,7 @@ namespace {
ssize_t http2_data_read_callback(nghttp2_session *session, ssize_t http2_data_read_callback(nghttp2_session *session,
int32_t stream_id, int32_t stream_id,
uint8_t *buf, size_t length, uint8_t *buf, size_t length,
int *eof, uint32_t *data_flags,
nghttp2_data_source *source, nghttp2_data_source *source,
void *user_data) void *user_data)
{ {
@ -183,7 +183,7 @@ ssize_t http2_data_read_callback(nghttp2_session *session,
if(!downstream->get_upgrade_request() || if(!downstream->get_upgrade_request() ||
(downstream->get_response_state() == Downstream::HEADER_COMPLETE && (downstream->get_response_state() == Downstream::HEADER_COMPLETE &&
!downstream->get_upgraded())) { !downstream->get_upgraded())) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
} else { } else {
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;
} }
@ -204,7 +204,7 @@ ssize_t http2_data_read_callback(nghttp2_session *session,
if(evbuffer_get_length(body) == 0) { if(evbuffer_get_length(body) == 0) {
// Check get_request_state() == MSG_COMPLETE just in case // Check get_request_state() == MSG_COMPLETE just in case
if(downstream->get_request_state() == Downstream::MSG_COMPLETE) { if(downstream->get_request_state() == Downstream::MSG_COMPLETE) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
break; break;
} }
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;

View File

@ -891,7 +891,7 @@ namespace {
ssize_t downstream_data_read_callback(nghttp2_session *session, ssize_t downstream_data_read_callback(nghttp2_session *session,
int32_t stream_id, int32_t stream_id,
uint8_t *buf, size_t length, uint8_t *buf, size_t length,
int *eof, uint32_t *data_flags,
nghttp2_data_source *source, nghttp2_data_source *source,
void *user_data) void *user_data)
{ {
@ -910,7 +910,7 @@ ssize_t downstream_data_read_callback(nghttp2_session *session,
if(nread == 0 && if(nread == 0 &&
downstream->get_response_state() == Downstream::MSG_COMPLETE) { downstream->get_response_state() == Downstream::MSG_COMPLETE) {
if(!downstream->get_upgraded()) { if(!downstream->get_upgraded()) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
} else { } else {
// For tunneling, issue RST_STREAM to finish the stream. // For tunneling, issue RST_STREAM to finish the stream.
if(LOG_ENABLED(INFO)) { if(LOG_ENABLED(INFO)) {
@ -923,14 +923,14 @@ ssize_t downstream_data_read_callback(nghttp2_session *session,
} }
// Send WINDOW_UPDATE before buffer is empty to avoid delay because // Send WINDOW_UPDATE before buffer is empty to avoid delay because
// of RTT. // of RTT.
if(*eof != 1 && if(((*data_flags) & NGHTTP2_DATA_FLAG_EOF) == 0 &&
handler->get_outbuf_length() + evbuffer_get_length(body) < handler->get_outbuf_length() + evbuffer_get_length(body) <
OUTBUF_MAX_THRES) { OUTBUF_MAX_THRES) {
if(downstream->resume_read(SHRPX_NO_BUFFER) != 0) { if(downstream->resume_read(SHRPX_NO_BUFFER) != 0) {
return NGHTTP2_ERR_CALLBACK_FAILURE; return NGHTTP2_ERR_CALLBACK_FAILURE;
} }
} }
if(nread == 0 && *eof != 1) { if(nread == 0 && ((*data_flags) & NGHTTP2_DATA_FLAG_EOF) == 0) {
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;
} }
return nread; return nread;

View File

@ -232,7 +232,7 @@ static ssize_t select_padding_callback(nghttp2_session *session,
static ssize_t fixed_length_data_source_read_callback static ssize_t fixed_length_data_source_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t len, int *eof, uint8_t *buf, size_t len, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
my_user_data *ud = (my_user_data*)user_data; my_user_data *ud = (my_user_data*)user_data;
@ -244,14 +244,14 @@ static ssize_t fixed_length_data_source_read_callback
} }
ud->data_source_length -= wlen; ud->data_source_length -= wlen;
if(ud->data_source_length == 0) { if(ud->data_source_length == 0) {
*eof = 1; *data_flags |= NGHTTP2_DATA_FLAG_EOF;
} }
return wlen; return wlen;
} }
static ssize_t temporal_failure_data_source_read_callback static ssize_t temporal_failure_data_source_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t len, int *eof, uint8_t *buf, size_t len, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
@ -259,7 +259,7 @@ static ssize_t temporal_failure_data_source_read_callback
static ssize_t fail_data_source_read_callback static ssize_t fail_data_source_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t len, int *eof, uint8_t *buf, size_t len, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
return NGHTTP2_ERR_CALLBACK_FAILURE; return NGHTTP2_ERR_CALLBACK_FAILURE;
@ -332,7 +332,7 @@ static int on_begin_headers_callback(nghttp2_session *session,
static ssize_t defer_data_source_read_callback static ssize_t defer_data_source_read_callback
(nghttp2_session *session, int32_t stream_id, (nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t len, int *eof, uint8_t *buf, size_t len, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data) nghttp2_data_source *source, void *user_data)
{ {
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;