Code cleanup

This commit is contained in:
Tatsuhiro Tsujikawa 2014-02-19 00:45:20 +09:00
parent 2966ad2d15
commit 4ced1c1622
3 changed files with 186 additions and 184 deletions

View File

@ -287,10 +287,10 @@ ssize_t nghttp2_frame_pack_priority(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_priority *frame) nghttp2_priority *frame)
{ {
ssize_t framelen= NGHTTP2_FRAME_HEAD_LENGTH + 4; ssize_t framelen= NGHTTP2_FRAME_HEAD_LENGTH + 4;
int r; int rv;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen); rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
memset(*buf_ptr, 0, framelen); memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd); nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
@ -309,10 +309,10 @@ ssize_t nghttp2_frame_pack_rst_stream(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_rst_stream *frame) nghttp2_rst_stream *frame)
{ {
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4; ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4;
int r; int rv;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen); rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
memset(*buf_ptr, 0, framelen); memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd); nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
@ -331,10 +331,10 @@ ssize_t nghttp2_frame_pack_settings(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_settings *frame) nghttp2_settings *frame)
{ {
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length; ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length;
int r; int rv;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen); rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
memset(*buf_ptr, 0, framelen); memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd); nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
@ -452,10 +452,10 @@ ssize_t nghttp2_frame_pack_ping(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_ping *frame) nghttp2_ping *frame)
{ {
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 8; ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 8;
int r; int rv;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen); rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
memset(*buf_ptr, 0, framelen); memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd); nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
@ -474,10 +474,10 @@ ssize_t nghttp2_frame_pack_goaway(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_goaway *frame) nghttp2_goaway *frame)
{ {
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length; ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length;
int r; int rv;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen); rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
memset(*buf_ptr, 0, framelen); memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd); nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
@ -502,10 +502,10 @@ ssize_t nghttp2_frame_pack_window_update(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_window_update *frame) nghttp2_window_update *frame)
{ {
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4; ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4;
int r; int rv;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen); rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
memset(*buf_ptr, 0, framelen); memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd); nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);

View File

@ -110,12 +110,15 @@ int nghttp2_session_terminate_session(nghttp2_session *session,
int nghttp2_session_is_my_stream_id(nghttp2_session *session, int nghttp2_session_is_my_stream_id(nghttp2_session *session,
int32_t stream_id) int32_t stream_id)
{ {
int r; int rem;
if(stream_id == 0) { if(stream_id == 0) {
return 0; return 0;
} }
r = stream_id % 2; rem = stream_id & 0x1;
return (session->server && r == 0) || (!session->server && r == 1); if(session->server) {
return rem == 0;
}
return rem == 1;
} }
nghttp2_stream* nghttp2_session_get_stream(nghttp2_session *session, nghttp2_stream* nghttp2_session_get_stream(nghttp2_session *session,
@ -212,11 +215,11 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
uint32_t opt_set_mask, uint32_t opt_set_mask,
const nghttp2_opt_set *opt_set) const nghttp2_opt_set *opt_set)
{ {
int r; int rv;
*session_ptr = malloc(sizeof(nghttp2_session)); *session_ptr = malloc(sizeof(nghttp2_session));
if(*session_ptr == NULL) { if(*session_ptr == NULL) {
r = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail_session; goto fail_session;
} }
memset(*session_ptr, 0, sizeof(nghttp2_session)); memset(*session_ptr, 0, sizeof(nghttp2_session));
@ -249,31 +252,31 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
if(server) { if(server) {
(*session_ptr)->server = 1; (*session_ptr)->server = 1;
} }
r = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater); rv = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater);
if(r != 0) { if(rv != 0) {
goto fail_hd_deflater; goto fail_hd_deflater;
} }
r = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater); rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater);
if(r != 0) { if(rv != 0) {
goto fail_hd_inflater; goto fail_hd_inflater;
} }
r = nghttp2_map_init(&(*session_ptr)->streams); rv = nghttp2_map_init(&(*session_ptr)->streams);
if(r != 0) { if(rv != 0) {
goto fail_map; goto fail_map;
} }
r = nghttp2_pq_init(&(*session_ptr)->ob_pq, nghttp2_outbound_item_compar); rv = nghttp2_pq_init(&(*session_ptr)->ob_pq, nghttp2_outbound_item_compar);
if(r != 0) { if(rv != 0) {
goto fail_ob_pq; goto fail_ob_pq;
} }
r = nghttp2_pq_init(&(*session_ptr)->ob_ss_pq, nghttp2_outbound_item_compar); rv = nghttp2_pq_init(&(*session_ptr)->ob_ss_pq, nghttp2_outbound_item_compar);
if(r != 0) { if(rv != 0) {
goto fail_ob_ss_pq; goto fail_ob_ss_pq;
} }
(*session_ptr)->aob.framebuf = malloc (*session_ptr)->aob.framebuf = malloc
(NGHTTP2_INITIAL_OUTBOUND_FRAMEBUF_LENGTH); (NGHTTP2_INITIAL_OUTBOUND_FRAMEBUF_LENGTH);
if((*session_ptr)->aob.framebuf == NULL) { if((*session_ptr)->aob.framebuf == NULL) {
r = NGHTTP2_ERR_NOMEM; rv = NGHTTP2_ERR_NOMEM;
goto fail_aob_framebuf; goto fail_aob_framebuf;
} }
(*session_ptr)->aob.framebufmax = NGHTTP2_INITIAL_OUTBOUND_FRAMEBUF_LENGTH; (*session_ptr)->aob.framebufmax = NGHTTP2_INITIAL_OUTBOUND_FRAMEBUF_LENGTH;
@ -312,7 +315,7 @@ static int nghttp2_session_new(nghttp2_session **session_ptr,
fail_hd_deflater: fail_hd_deflater:
free(*session_ptr); free(*session_ptr);
fail_session: fail_session:
return r; return rv;
} }
int nghttp2_session_client_new(nghttp2_session **session_ptr, int nghttp2_session_client_new(nghttp2_session **session_ptr,
@ -329,15 +332,16 @@ int nghttp2_session_client_new2(nghttp2_session **session_ptr,
uint32_t opt_set_mask, uint32_t opt_set_mask,
const nghttp2_opt_set *opt_set) const nghttp2_opt_set *opt_set)
{ {
int r; int rv;
/* For client side session, header compression is disabled. */ /* For client side session, header compression is disabled. */
r = nghttp2_session_new(session_ptr, callbacks, user_data, 0, rv = nghttp2_session_new(session_ptr, callbacks, user_data, 0,
opt_set_mask, opt_set); opt_set_mask, opt_set);
if(r == 0) { if(rv != 0) {
return rv;
}
/* IDs for use in client */ /* IDs for use in client */
(*session_ptr)->next_stream_id = 1; (*session_ptr)->next_stream_id = 1;
} return 0;
return r;
} }
int nghttp2_session_server_new(nghttp2_session **session_ptr, int nghttp2_session_server_new(nghttp2_session **session_ptr,
@ -354,15 +358,16 @@ int nghttp2_session_server_new2(nghttp2_session **session_ptr,
uint32_t opt_set_mask, uint32_t opt_set_mask,
const nghttp2_opt_set *opt_set) const nghttp2_opt_set *opt_set)
{ {
int r; int rv;
/* Enable header compression on server side. */ /* Enable header compression on server side. */
r = nghttp2_session_new(session_ptr, callbacks, user_data, 1, rv = nghttp2_session_new(session_ptr, callbacks, user_data, 1,
opt_set_mask, opt_set); opt_set_mask, opt_set);
if(r == 0) { if(rv != 0) {
return rv;
}
/* IDs for use in client */ /* IDs for use in client */
(*session_ptr)->next_stream_id = 2; (*session_ptr)->next_stream_id = 2;
} return 0;
return r;
} }
static int nghttp2_free_streams(nghttp2_map_entry *entry, void *ptr) static int nghttp2_free_streams(nghttp2_map_entry *entry, void *ptr)
@ -457,7 +462,7 @@ int nghttp2_session_add_frame(nghttp2_session *session,
{ {
/* TODO Return error if stream is not found for the frame requiring /* TODO Return error if stream is not found for the frame requiring
stream presence. */ stream presence. */
int r = 0; int rv = 0;
nghttp2_outbound_item *item; nghttp2_outbound_item *item;
item = malloc(sizeof(nghttp2_outbound_item)); item = malloc(sizeof(nghttp2_outbound_item));
if(item == NULL) { if(item == NULL) {
@ -526,9 +531,9 @@ int nghttp2_session_add_frame(nghttp2_session *session,
/* TODO If 2 HEADERS are submitted for reserved stream, then /* TODO If 2 HEADERS are submitted for reserved stream, then
both of them are queued into ob_ss_pq, which is not both of them are queued into ob_ss_pq, which is not
desirable. */ desirable. */
r = nghttp2_pq_push(&session->ob_ss_pq, item); rv = nghttp2_pq_push(&session->ob_ss_pq, item);
} else { } else {
r = nghttp2_pq_push(&session->ob_pq, item); rv = nghttp2_pq_push(&session->ob_pq, item);
} }
} else if(frame_cat == NGHTTP2_CAT_DATA) { } else if(frame_cat == NGHTTP2_CAT_DATA) {
nghttp2_private_data *data_frame = (nghttp2_private_data*)abs_frame; nghttp2_private_data *data_frame = (nghttp2_private_data*)abs_frame;
@ -537,14 +542,14 @@ int nghttp2_session_add_frame(nghttp2_session *session,
if(stream) { if(stream) {
item->pri = stream->pri; item->pri = stream->pri;
} }
r = nghttp2_pq_push(&session->ob_pq, item); rv = nghttp2_pq_push(&session->ob_pq, item);
} else { } else {
/* Unreachable */ /* Unreachable */
assert(0); assert(0);
} }
if(r != 0) { if(rv != 0) {
free(item); free(item);
return r; return rv;
} }
return 0; return 0;
} }
@ -553,18 +558,18 @@ int nghttp2_session_add_rst_stream(nghttp2_session *session,
int32_t stream_id, int32_t stream_id,
nghttp2_error_code error_code) nghttp2_error_code error_code)
{ {
int r; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
frame = malloc(sizeof(nghttp2_frame)); frame = malloc(sizeof(nghttp2_frame));
if(frame == NULL) { if(frame == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
nghttp2_frame_rst_stream_init(&frame->rst_stream, stream_id, error_code); nghttp2_frame_rst_stream_init(&frame->rst_stream, stream_id, error_code);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
if(r != 0) { if(rv != 0) {
nghttp2_frame_rst_stream_free(&frame->rst_stream); nghttp2_frame_rst_stream_free(&frame->rst_stream);
free(frame); free(frame);
return r; return rv;
} }
return 0; return 0;
} }
@ -575,7 +580,7 @@ nghttp2_stream* nghttp2_session_open_stream(nghttp2_session *session,
nghttp2_stream_state initial_state, nghttp2_stream_state initial_state,
void *stream_user_data) void *stream_user_data)
{ {
int r; int rv;
nghttp2_stream *stream = malloc(sizeof(nghttp2_stream)); nghttp2_stream *stream = malloc(sizeof(nghttp2_stream));
if(stream == NULL) { if(stream == NULL) {
return NULL; return NULL;
@ -586,8 +591,8 @@ nghttp2_stream* nghttp2_session_open_stream(nghttp2_session *session,
session->local_settings session->local_settings
[NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE], [NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE],
stream_user_data); stream_user_data);
r = nghttp2_map_insert(&session->streams, &stream->map_entry); rv = nghttp2_map_insert(&session->streams, &stream->map_entry);
if(r != 0) { if(rv != 0) {
free(stream); free(stream);
return NULL; return NULL;
} }
@ -755,10 +760,10 @@ static int nghttp2_session_predicate_response_headers_send
(nghttp2_session *session, int32_t stream_id) (nghttp2_session *session, int32_t stream_id)
{ {
nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id); nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id);
int r; int rv;
r = nghttp2_predicate_stream_for_send(stream); rv = nghttp2_predicate_stream_for_send(stream);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
if(nghttp2_session_is_my_stream_id(session, stream_id)) { if(nghttp2_session_is_my_stream_id(session, stream_id)) {
return NGHTTP2_ERR_INVALID_STREAM_ID; return NGHTTP2_ERR_INVALID_STREAM_ID;
@ -793,11 +798,11 @@ static int nghttp2_session_predicate_push_response_headers_send
(nghttp2_session *session, int32_t stream_id) (nghttp2_session *session, int32_t stream_id)
{ {
nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id); nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id);
int r; int rv;
/* TODO Should disallow HEADERS if GOAWAY has already been issued? */ /* TODO Should disallow HEADERS if GOAWAY has already been issued? */
r = nghttp2_predicate_stream_for_send(stream); rv = nghttp2_predicate_stream_for_send(stream);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
if(stream->state != NGHTTP2_STREAM_RESERVED) { if(stream->state != NGHTTP2_STREAM_RESERVED) {
return NGHTTP2_ERR_PROTO; return NGHTTP2_ERR_PROTO;
@ -829,10 +834,10 @@ static int nghttp2_session_predicate_stream_frame_send
(nghttp2_session* session, int32_t stream_id) (nghttp2_session* session, int32_t stream_id)
{ {
nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id); nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id);
int r; int rv;
r = nghttp2_predicate_stream_for_send(stream); rv = nghttp2_predicate_stream_for_send(stream);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
if(nghttp2_session_is_my_stream_id(session, stream_id)) { if(nghttp2_session_is_my_stream_id(session, stream_id)) {
if(stream->state == NGHTTP2_STREAM_CLOSING) { if(stream->state == NGHTTP2_STREAM_CLOSING) {
@ -1046,10 +1051,10 @@ static int nghttp2_session_predicate_data_send(nghttp2_session *session,
int32_t stream_id) int32_t stream_id)
{ {
nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id); nghttp2_stream *stream = nghttp2_session_get_stream(session, stream_id);
int r; int rv;
r = nghttp2_predicate_stream_for_send(stream); rv = nghttp2_predicate_stream_for_send(stream);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
if(stream->deferred_data != NULL) { if(stream->deferred_data != NULL) {
/* stream->deferred_data != NULL means previously queued DATA /* stream->deferred_data != NULL means previously queued DATA
@ -1172,21 +1177,21 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
nghttp2_outbound_item *item) nghttp2_outbound_item *item)
{ {
ssize_t framebuflen = 0; ssize_t framebuflen = 0;
int rv;
if(item->frame_cat == NGHTTP2_CAT_CTRL) { if(item->frame_cat == NGHTTP2_CAT_CTRL) {
nghttp2_frame *frame; nghttp2_frame *frame;
frame = nghttp2_outbound_item_get_ctrl_frame(item); frame = nghttp2_outbound_item_get_ctrl_frame(item);
switch(frame->hd.type) { switch(frame->hd.type) {
case NGHTTP2_HEADERS: { case NGHTTP2_HEADERS: {
int r;
nghttp2_headers_aux_data *aux_data; nghttp2_headers_aux_data *aux_data;
aux_data = (nghttp2_headers_aux_data*)item->aux_data; aux_data = (nghttp2_headers_aux_data*)item->aux_data;
if(frame->hd.stream_id == -1) { if(frame->hd.stream_id == -1) {
/* initial HEADERS, which opens stream */ /* initial HEADERS, which opens stream */
frame->headers.cat = NGHTTP2_HCAT_REQUEST; frame->headers.cat = NGHTTP2_HCAT_REQUEST;
r = nghttp2_session_predicate_request_headers_send(session, rv = nghttp2_session_predicate_request_headers_send(session,
&frame->headers); &frame->headers);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
frame->hd.stream_id = session->next_stream_id; frame->hd.stream_id = session->next_stream_id;
session->next_stream_id += 2; session->next_stream_id += 2;
@ -1198,10 +1203,10 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
frame->headers.cat = NGHTTP2_HCAT_RESPONSE; frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
} else { } else {
frame->headers.cat = NGHTTP2_HCAT_HEADERS; frame->headers.cat = NGHTTP2_HCAT_HEADERS;
r = nghttp2_session_predicate_headers_send(session, rv = nghttp2_session_predicate_headers_send(session,
frame->hd.stream_id); frame->hd.stream_id);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
} }
framebuflen = nghttp2_frame_pack_headers(&session->aob.framebuf, framebuflen = nghttp2_frame_pack_headers(&session->aob.framebuf,
@ -1243,11 +1248,10 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
break; break;
} }
case NGHTTP2_PRIORITY: { case NGHTTP2_PRIORITY: {
int r; rv = nghttp2_session_predicate_priority_send
r = nghttp2_session_predicate_priority_send
(session, frame->hd.stream_id); (session, frame->hd.stream_id);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
framebuflen = nghttp2_frame_pack_priority(&session->aob.framebuf, framebuflen = nghttp2_frame_pack_priority(&session->aob.framebuf,
&session->aob.framebufmax, &session->aob.framebufmax,
@ -1266,10 +1270,9 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
} }
break; break;
case NGHTTP2_SETTINGS: { case NGHTTP2_SETTINGS: {
int r; rv = nghttp2_session_predicate_settings_send(session, frame);
r = nghttp2_session_predicate_settings_send(session, frame); if(rv != 0) {
if(r != 0) { return rv;
return r;
} }
framebuflen = nghttp2_frame_pack_settings(&session->aob.framebuf, framebuflen = nghttp2_frame_pack_settings(&session->aob.framebuf,
&session->aob.framebufmax, &session->aob.framebufmax,
@ -1280,12 +1283,11 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
break; break;
} }
case NGHTTP2_PUSH_PROMISE: { case NGHTTP2_PUSH_PROMISE: {
int r;
nghttp2_stream *stream; nghttp2_stream *stream;
r = nghttp2_session_predicate_push_promise_send(session, rv = nghttp2_session_predicate_push_promise_send(session,
frame->hd.stream_id); frame->hd.stream_id);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
frame->push_promise.promised_stream_id = session->next_stream_id; frame->push_promise.promised_stream_id = session->next_stream_id;
session->next_stream_id += 2; session->next_stream_id += 2;
@ -1323,11 +1325,10 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
} }
break; break;
case NGHTTP2_WINDOW_UPDATE: { case NGHTTP2_WINDOW_UPDATE: {
int r; rv = nghttp2_session_predicate_window_update_send
r = nghttp2_session_predicate_window_update_send
(session, frame->hd.stream_id); (session, frame->hd.stream_id);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
framebuflen = nghttp2_frame_pack_window_update(&session->aob.framebuf, framebuflen = nghttp2_frame_pack_window_update(&session->aob.framebuf,
&session->aob.framebufmax, &session->aob.framebufmax,
@ -1355,15 +1356,15 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
default: default:
framebuflen = NGHTTP2_ERR_INVALID_ARGUMENT; framebuflen = NGHTTP2_ERR_INVALID_ARGUMENT;
} }
return framebuflen;
} else if(item->frame_cat == NGHTTP2_CAT_DATA) { } else if(item->frame_cat == NGHTTP2_CAT_DATA) {
size_t next_readmax; size_t next_readmax;
nghttp2_stream *stream; nghttp2_stream *stream;
nghttp2_private_data *data_frame; nghttp2_private_data *data_frame;
int r;
data_frame = nghttp2_outbound_item_get_data_frame(item); data_frame = nghttp2_outbound_item_get_data_frame(item);
r = nghttp2_session_predicate_data_send(session, data_frame->hd.stream_id); rv = nghttp2_session_predicate_data_send(session, data_frame->hd.stream_id);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
stream = nghttp2_session_get_stream(session, data_frame->hd.stream_id); stream = nghttp2_session_get_stream(session, data_frame->hd.stream_id);
/* Assuming stream is not NULL */ /* Assuming stream is not NULL */
@ -1386,22 +1387,21 @@ static ssize_t nghttp2_session_prep_frame(nghttp2_session *session,
session->aob.item = NULL; session->aob.item = NULL;
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
return NGHTTP2_ERR_DEFERRED; return NGHTTP2_ERR_DEFERRED;
} else if(framebuflen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { }
r = nghttp2_session_add_rst_stream(session, data_frame->hd.stream_id, if(framebuflen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
rv = nghttp2_session_add_rst_stream(session, data_frame->hd.stream_id,
NGHTTP2_INTERNAL_ERROR); NGHTTP2_INTERNAL_ERROR);
if(r == 0) { if(rv != 0) {
return framebuflen; return rv;
} else {
return r;
} }
} else if(framebuflen < 0) {
return framebuflen; return framebuflen;
} }
/* This handles framebuflen < 0 case */
return framebuflen;
} else { } else {
/* Unreachable */ /* Unreachable */
assert(0); assert(0);
} }
return framebuflen;
} }
nghttp2_outbound_item* nghttp2_session_get_ob_pq_top nghttp2_outbound_item* nghttp2_session_get_ob_pq_top
@ -1621,7 +1621,7 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
if(nghttp2_is_fatal(rv)) { if(nghttp2_is_fatal(rv)) {
return rv; return rv;
} }
/* If r is not fatal, the only possible error is closed /* If rv is not fatal, the only possible error is closed
stream, so we have nothing to do here. */ stream, so we have nothing to do here. */
} }
break; break;
@ -1644,7 +1644,7 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
if(nghttp2_is_fatal(rv)) { if(nghttp2_is_fatal(rv)) {
return rv; return rv;
} }
/* If r is not fatal, the only possible error is closed /* If rv is not fatal, the only possible error is closed
stream, so we have nothing to do here. */ stream, so we have nothing to do here. */
} }
break; break;
@ -1960,19 +1960,17 @@ int nghttp2_session_send(nghttp2_session *session)
static ssize_t nghttp2_recv(nghttp2_session *session, uint8_t *buf, size_t len) static ssize_t nghttp2_recv(nghttp2_session *session, uint8_t *buf, size_t len)
{ {
ssize_t r; ssize_t rv;
r = session->callbacks.recv_callback rv = session->callbacks.recv_callback(session, buf, len, 0,
(session, buf, len, 0, session->user_data); session->user_data);
if(r > 0) { if(rv > 0) {
if((size_t)r > len) { if((size_t)rv > len) {
return NGHTTP2_ERR_CALLBACK_FAILURE; return NGHTTP2_ERR_CALLBACK_FAILURE;
} }
} else if(r < 0) { } else if(rv < 0 && rv != NGHTTP2_ERR_WOULDBLOCK && rv != NGHTTP2_ERR_EOF) {
if(r != NGHTTP2_ERR_WOULDBLOCK && r != NGHTTP2_ERR_EOF) { return NGHTTP2_ERR_CALLBACK_FAILURE;
r = NGHTTP2_ERR_CALLBACK_FAILURE;
} }
} return rv;
return r;
} }
static int nghttp2_session_call_on_frame_received static int nghttp2_session_call_on_frame_received
@ -2178,10 +2176,10 @@ static int nghttp2_session_handle_invalid_stream
nghttp2_frame *frame, nghttp2_frame *frame,
nghttp2_error_code error_code) nghttp2_error_code error_code)
{ {
int r; int rv;
r = nghttp2_session_add_rst_stream(session, frame->hd.stream_id, error_code); rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id, error_code);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
if(session->callbacks.on_invalid_frame_recv_callback) { if(session->callbacks.on_invalid_frame_recv_callback) {
if(session->callbacks.on_invalid_frame_recv_callback if(session->callbacks.on_invalid_frame_recv_callback
@ -2463,7 +2461,7 @@ int nghttp2_session_on_headers_received(nghttp2_session *session,
nghttp2_frame *frame, nghttp2_frame *frame,
nghttp2_stream *stream) nghttp2_stream *stream)
{ {
int r = 0; int rv = 0;
if(frame->hd.stream_id == 0) { if(frame->hd.stream_id == 0) {
return nghttp2_session_inflate_handle_invalid_connection return nghttp2_session_inflate_handle_invalid_connection
(session, frame, NGHTTP2_PROTOCOL_ERROR); (session, frame, NGHTTP2_PROTOCOL_ERROR);
@ -2488,9 +2486,9 @@ int nghttp2_session_on_headers_received(nghttp2_session *session,
} }
if(nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) { if(nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
if(stream->state == NGHTTP2_STREAM_OPENED) { if(stream->state == NGHTTP2_STREAM_OPENED) {
r = session_call_on_begin_headers(session, frame); rv = session_call_on_begin_headers(session, frame);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
return 0; return 0;
} else if(stream->state == NGHTTP2_STREAM_CLOSING) { } else if(stream->state == NGHTTP2_STREAM_CLOSING) {
@ -2508,9 +2506,9 @@ int nghttp2_session_on_headers_received(nghttp2_session *session,
NGHTTP2_STREAM_CLOSING, we discard the frame. This is a race NGHTTP2_STREAM_CLOSING, we discard the frame. This is a race
condition. */ condition. */
if(stream->state != NGHTTP2_STREAM_CLOSING) { if(stream->state != NGHTTP2_STREAM_CLOSING) {
r = session_call_on_begin_headers(session, frame); rv = session_call_on_begin_headers(session, frame);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
return 0; return 0;
} }
@ -3036,17 +3034,17 @@ static int session_process_push_promise_frame(nghttp2_session *session)
int nghttp2_session_on_ping_received(nghttp2_session *session, int nghttp2_session_on_ping_received(nghttp2_session *session,
nghttp2_frame *frame) nghttp2_frame *frame)
{ {
int r = 0; int rv = 0;
if(frame->hd.stream_id != 0) { if(frame->hd.stream_id != 0) {
return nghttp2_session_handle_invalid_connection(session, frame, return nghttp2_session_handle_invalid_connection(session, frame,
NGHTTP2_PROTOCOL_ERROR); NGHTTP2_PROTOCOL_ERROR);
} }
if((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) { if((frame->hd.flags & NGHTTP2_FLAG_ACK) == 0) {
/* Peer sent ping, so ping it back */ /* Peer sent ping, so ping it back */
r = nghttp2_session_add_ping(session, NGHTTP2_FLAG_ACK, rv = nghttp2_session_add_ping(session, NGHTTP2_FLAG_ACK,
frame->ping.opaque_data); frame->ping.opaque_data);
if(r != 0) { if(rv != 0) {
return r; return rv;
} }
} }
return nghttp2_session_call_on_frame_received(session, frame); return nghttp2_session_call_on_frame_received(session, frame);
@ -3246,14 +3244,13 @@ int nghttp2_session_on_data_received(nghttp2_session *session,
/* For errors, this function only returns FATAL error. */ /* For errors, this function only returns FATAL error. */
static int nghttp2_session_process_data_frame(nghttp2_session *session) static int nghttp2_session_process_data_frame(nghttp2_session *session)
{ {
int r; int rv;
nghttp2_frame *public_data_frame = &session->iframe.frame; nghttp2_frame *public_data_frame = &session->iframe.frame;
r = nghttp2_session_on_data_received(session, public_data_frame); rv = nghttp2_session_on_data_received(session, public_data_frame);
if(nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} else {
return 0;
} }
return 0;
} }
/* /*
@ -4363,19 +4360,20 @@ int nghttp2_session_want_write(nghttp2_session *session)
int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags, int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
uint8_t *opaque_data) uint8_t *opaque_data)
{ {
int r; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
frame = malloc(sizeof(nghttp2_frame)); frame = malloc(sizeof(nghttp2_frame));
if(frame == NULL) { if(frame == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
nghttp2_frame_ping_init(&frame->ping, flags, opaque_data); nghttp2_frame_ping_init(&frame->ping, flags, opaque_data);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
if(r != 0) { if(rv != 0) {
nghttp2_frame_ping_free(&frame->ping); nghttp2_frame_ping_free(&frame->ping);
free(frame); free(frame);
return rv;
} }
return r; return 0;
} }
int nghttp2_session_add_goaway(nghttp2_session *session, int nghttp2_session_add_goaway(nghttp2_session *session,
@ -4383,7 +4381,7 @@ int nghttp2_session_add_goaway(nghttp2_session *session,
nghttp2_error_code error_code, nghttp2_error_code error_code,
uint8_t *opaque_data, size_t opaque_data_len) uint8_t *opaque_data, size_t opaque_data_len)
{ {
int r; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
uint8_t *opaque_data_copy = NULL; uint8_t *opaque_data_copy = NULL;
if(opaque_data_len) { if(opaque_data_len) {
@ -4403,19 +4401,20 @@ int nghttp2_session_add_goaway(nghttp2_session *session,
} }
nghttp2_frame_goaway_init(&frame->goaway, last_stream_id, error_code, nghttp2_frame_goaway_init(&frame->goaway, last_stream_id, error_code,
opaque_data_copy, opaque_data_len); opaque_data_copy, opaque_data_len);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
if(r != 0) { if(rv != 0) {
nghttp2_frame_goaway_free(&frame->goaway); nghttp2_frame_goaway_free(&frame->goaway);
free(frame); free(frame);
return rv;
} }
return r; return 0;
} }
int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags, int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
int32_t stream_id, int32_t stream_id,
int32_t window_size_increment) int32_t window_size_increment)
{ {
int r; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
frame = malloc(sizeof(nghttp2_frame)); frame = malloc(sizeof(nghttp2_frame));
if(frame == NULL) { if(frame == NULL) {
@ -4423,12 +4422,13 @@ int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
} }
nghttp2_frame_window_update_init(&frame->window_update, flags, nghttp2_frame_window_update_init(&frame->window_update, flags,
stream_id, window_size_increment); stream_id, window_size_increment);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
if(r != 0) { if(rv != 0) {
nghttp2_frame_window_update_free(&frame->window_update); nghttp2_frame_window_update_free(&frame->window_update);
free(frame); free(frame);
return rv;
} }
return r; return 0;
} }
int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags, int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
@ -4436,7 +4436,7 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
{ {
nghttp2_frame *frame; nghttp2_frame *frame;
nghttp2_settings_entry *iv_copy; nghttp2_settings_entry *iv_copy;
int r; int rv;
if(!nghttp2_iv_check(iv, niv)) { if(!nghttp2_iv_check(iv, niv)) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
@ -4450,14 +4450,15 @@ int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
nghttp2_frame_settings_init(&frame->settings, flags, iv_copy, niv); nghttp2_frame_settings_init(&frame->settings, flags, iv_copy, niv);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
if(r != 0) { if(rv != 0) {
/* The only expected error is fatal one */ /* The only expected error is fatal one */
assert(r < NGHTTP2_ERR_FATAL); assert(nghttp2_is_fatal(rv));
nghttp2_frame_settings_free(&frame->settings); nghttp2_frame_settings_free(&frame->settings);
free(frame); free(frame);
return rv;
} }
return r; return 0;
} }
ssize_t nghttp2_session_pack_data(nghttp2_session *session, ssize_t nghttp2_session_pack_data(nghttp2_session *session,
@ -4574,18 +4575,18 @@ int nghttp2_session_set_stream_user_data(nghttp2_session *session,
int nghttp2_session_resume_data(nghttp2_session *session, int32_t stream_id) int nghttp2_session_resume_data(nghttp2_session *session, int32_t stream_id)
{ {
int r; int rv;
nghttp2_stream *stream; nghttp2_stream *stream;
stream = nghttp2_session_get_stream(session, stream_id); stream = nghttp2_session_get_stream(session, stream_id);
if(stream == NULL || stream->deferred_data == NULL || if(stream == NULL || stream->deferred_data == NULL ||
(stream->deferred_flags & NGHTTP2_DEFERRED_FLOW_CONTROL)) { (stream->deferred_flags & NGHTTP2_DEFERRED_FLOW_CONTROL)) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
r = nghttp2_pq_push(&session->ob_pq, stream->deferred_data); rv = nghttp2_pq_push(&session->ob_pq, stream->deferred_data);
if(r == 0) { if(rv == 0) {
nghttp2_stream_detach_deferred_data(stream); nghttp2_stream_detach_deferred_data(stream);
} }
return r; return rv;
} }
size_t nghttp2_session_get_outbound_queue_size(nghttp2_session *session) size_t nghttp2_session_get_outbound_queue_size(nghttp2_session *session)

View File

@ -139,7 +139,7 @@ int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags, int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
int32_t stream_id, int32_t pri) int32_t stream_id, int32_t pri)
{ {
int r; int rv;
nghttp2_frame *frame; nghttp2_frame *frame;
if(pri < 0) { if(pri < 0) {
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
@ -149,11 +149,11 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
nghttp2_frame_priority_init(&frame->priority, stream_id, pri); nghttp2_frame_priority_init(&frame->priority, stream_id, pri);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
if(r != 0) { if(rv != 0) {
nghttp2_frame_priority_free(&frame->priority); nghttp2_frame_priority_free(&frame->priority);
free(frame); free(frame);
return r; return rv;
} }
return 0; return 0;
} }
@ -294,7 +294,7 @@ int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
int32_t stream_id, int32_t stream_id,
const nghttp2_data_provider *data_prd) const nghttp2_data_provider *data_prd)
{ {
int r; int rv;
nghttp2_private_data *data_frame; nghttp2_private_data *data_frame;
uint8_t nflags = 0; uint8_t nflags = 0;
@ -306,12 +306,13 @@ int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
nflags |= NGHTTP2_FLAG_END_STREAM; nflags |= NGHTTP2_FLAG_END_STREAM;
} }
nghttp2_frame_private_data_init(data_frame, nflags, stream_id, data_prd); nghttp2_frame_private_data_init(data_frame, nflags, stream_id, data_prd);
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_DATA, data_frame, NULL); rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_DATA, data_frame, NULL);
if(r != 0) { if(rv != 0) {
nghttp2_frame_private_data_free(data_frame); nghttp2_frame_private_data_free(data_frame);
free(data_frame); free(data_frame);
return rv;
} }
return r; return 0;
} }
ssize_t nghttp2_pack_settings_payload(uint8_t *buf, ssize_t nghttp2_pack_settings_payload(uint8_t *buf,