Code cleanup

This commit is contained in:
Tatsuhiro Tsujikawa 2014-01-27 23:00:08 +09:00
parent a3082b7c1e
commit a3193bee69
1 changed files with 136 additions and 145 deletions

View File

@ -1414,9 +1414,9 @@ static int session_call_on_frame_send(nghttp2_session *session,
*/ */
static int nghttp2_session_after_frame_sent(nghttp2_session *session) static int nghttp2_session_after_frame_sent(nghttp2_session *session)
{ {
int rv;
nghttp2_outbound_item *item = session->aob.item; nghttp2_outbound_item *item = session->aob.item;
if(item->frame_cat == NGHTTP2_CAT_CTRL) { if(item->frame_cat == NGHTTP2_CAT_CTRL) {
int r;
nghttp2_frame *frame; nghttp2_frame *frame;
frame = nghttp2_outbound_item_get_ctrl_frame(session->aob.item); frame = nghttp2_outbound_item_get_ctrl_frame(session->aob.item);
if(frame->hd.type == NGHTTP2_HEADERS || if(frame->hd.type == NGHTTP2_HEADERS ||
@ -1445,39 +1445,39 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
CONTINUATION frame. */ CONTINUATION frame. */
frame->hd.length = session->aob.framebuflen - NGHTTP2_FRAME_HEAD_LENGTH; frame->hd.length = session->aob.framebuflen - NGHTTP2_FRAME_HEAD_LENGTH;
} }
r = session_call_on_frame_send(session, frame); rv = session_call_on_frame_send(session, frame);
if(nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
switch(frame->hd.type) { switch(frame->hd.type) {
case NGHTTP2_HEADERS: { case NGHTTP2_HEADERS: {
nghttp2_headers_aux_data *aux_data;
nghttp2_stream *stream = nghttp2_stream *stream =
nghttp2_session_get_stream(session, frame->hd.stream_id); nghttp2_session_get_stream(session, frame->hd.stream_id);
nghttp2_headers_aux_data *aux_data; if(!stream) {
if(stream) { break;
}
switch(frame->headers.cat) { switch(frame->headers.cat) {
case NGHTTP2_HCAT_REQUEST: { case NGHTTP2_HCAT_REQUEST: {
stream->state = NGHTTP2_STREAM_OPENING; stream->state = NGHTTP2_STREAM_OPENING;
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR); nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
} }
r = nghttp2_session_close_stream_if_shut_rdwr(session, stream); rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
if(r != 0 && nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
r = 0;
/* We assume aux_data is a pointer to nghttp2_headers_aux_data */ /* We assume aux_data is a pointer to nghttp2_headers_aux_data */
aux_data = (nghttp2_headers_aux_data*)item->aux_data; aux_data = (nghttp2_headers_aux_data*)item->aux_data;
if(aux_data && aux_data->data_prd) { if(aux_data && aux_data->data_prd) {
/* nghttp2_submit_data() makes a copy of aux_data->data_prd */ /* nghttp2_submit_data() makes a copy of aux_data->data_prd */
r = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM, rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM,
frame->hd.stream_id, aux_data->data_prd); frame->hd.stream_id, aux_data->data_prd);
if(r != 0 &&nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
/* If r is not fatal, the only possible error is closed /* If r 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. */
r = 0;
} }
break; break;
} }
@ -1487,36 +1487,32 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR); nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
} }
r = nghttp2_session_close_stream_if_shut_rdwr(session, stream); rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
if(r != 0 && nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
r = 0;
/* We assume aux_data is a pointer to nghttp2_headers_aux_data */ /* We assume aux_data is a pointer to nghttp2_headers_aux_data */
aux_data = (nghttp2_headers_aux_data*)item->aux_data; aux_data = (nghttp2_headers_aux_data*)item->aux_data;
if(aux_data && aux_data->data_prd) { if(aux_data && aux_data->data_prd) {
r = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM, rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM,
frame->hd.stream_id, aux_data->data_prd); frame->hd.stream_id, aux_data->data_prd);
if(r != 0 && nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
/* If r is not fatal, the only possible error is closed /* If r 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. */
r = 0;
} }
break; break;
case NGHTTP2_HCAT_HEADERS: case NGHTTP2_HCAT_HEADERS:
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR); nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
} }
r = nghttp2_session_close_stream_if_shut_rdwr(session, stream); rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
if(r != 0 && nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
r = 0;
break; break;
} }
}
break; break;
} }
case NGHTTP2_PRIORITY: { case NGHTTP2_PRIORITY: {
@ -1538,12 +1534,11 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
break; break;
} }
case NGHTTP2_RST_STREAM: case NGHTTP2_RST_STREAM:
r = nghttp2_session_close_stream(session, frame->hd.stream_id, rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
frame->rst_stream.error_code); frame->rst_stream.error_code);
if(r != 0 && nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
r = 0;
break; break;
case NGHTTP2_SETTINGS: { case NGHTTP2_SETTINGS: {
size_t i; size_t i;
@ -1582,9 +1577,10 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
break; break;
} }
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
return 0;
} else if(item->frame_cat == NGHTTP2_CAT_DATA) { } else if(item->frame_cat == NGHTTP2_CAT_DATA) {
int r;
nghttp2_private_data *data_frame; nghttp2_private_data *data_frame;
nghttp2_outbound_item* next_item;
data_frame = nghttp2_outbound_item_get_data_frame(session->aob.item); data_frame = nghttp2_outbound_item_get_data_frame(session->aob.item);
if(session->callbacks.on_frame_send_callback) { if(session->callbacks.on_frame_send_callback) {
@ -1594,9 +1590,9 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
if(!data_frame->eof) { if(!data_frame->eof) {
public_data_frame.hd.flags &= ~NGHTTP2_FLAG_END_STREAM; public_data_frame.hd.flags &= ~NGHTTP2_FLAG_END_STREAM;
} }
r = session_call_on_frame_send(session, &public_data_frame); rv = session_call_on_frame_send(session, &public_data_frame);
if(nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
} }
if(data_frame->eof && (data_frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) { if(data_frame->eof && (data_frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
@ -1604,11 +1600,10 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
nghttp2_session_get_stream(session, data_frame->hd.stream_id); nghttp2_session_get_stream(session, data_frame->hd.stream_id);
if(stream) { if(stream) {
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR); nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
r = nghttp2_session_close_stream_if_shut_rdwr(session, stream); rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
if(r != 0 && nghttp2_is_fatal(r)) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
r = 0;
} }
} }
/* If session is closed or RST_STREAM was queued, we won't send /* If session is closed or RST_STREAM was queued, we won't send
@ -1617,8 +1612,8 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
nghttp2_session_predicate_data_send(session, nghttp2_session_predicate_data_send(session,
data_frame->hd.stream_id) != 0) { data_frame->hd.stream_id) != 0) {
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
} else { return 0;
nghttp2_outbound_item* next_item; }
next_item = nghttp2_session_get_next_ob_item(session); next_item = nghttp2_session_get_next_ob_item(session);
/* If priority of this stream is higher or equal to other stream /* If priority of this stream is higher or equal to other stream
waiting at the top of the queue, we continue to send this waiting at the top of the queue, we continue to send this
@ -1637,56 +1632,52 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
return 0; return 0;
} }
r = nghttp2_session_pack_data(session, rv = nghttp2_session_pack_data(session,
&session->aob.framebuf, &session->aob.framebuf,
&session->aob.framebufmax, &session->aob.framebufmax,
next_readmax, next_readmax,
data_frame); data_frame);
if(r == NGHTTP2_ERR_DEFERRED) { if(nghttp2_is_fatal(rv)) {
return rv;
}
if(rv == NGHTTP2_ERR_DEFERRED) {
nghttp2_stream_defer_data(stream, session->aob.item, nghttp2_stream_defer_data(stream, session->aob.item,
NGHTTP2_DEFERRED_NONE); NGHTTP2_DEFERRED_NONE);
session->aob.item = NULL; session->aob.item = NULL;
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
} else if(r == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) { return 0;
}
if(rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
/* Stop DATA frame chain and issue RST_STREAM to close the /* Stop DATA frame chain and issue RST_STREAM to close the
stream. We don't return stream. We don't return
NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE intentionally. */ NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE intentionally. */
r = nghttp2_session_add_rst_stream(session, data_frame->hd.stream_id, rv = nghttp2_session_add_rst_stream(session,
data_frame->hd.stream_id,
NGHTTP2_INTERNAL_ERROR); NGHTTP2_INTERNAL_ERROR);
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
if(r != 0) { if(nghttp2_is_fatal(rv)) {
return r; return rv;
} }
} else if(r < 0) { return 0;
/* In this context, r is either NGHTTP2_ERR_NOMEM or }
NGHTTP2_ERR_CALLBACK_FAILURE */ assert(rv >= 0);
nghttp2_active_outbound_item_reset(&session->aob); session->aob.framebuflen = session->aob.framebufmark = rv;
return r;
} else {
session->aob.framebuflen = session->aob.framebufmark = r;
session->aob.framebufoff = 0; session->aob.framebufoff = 0;
return 0;
} }
} else {
/* Update seq to interleave other streams with the same /* Update seq to interleave other streams with the same
priority. */ priority. */
session->aob.item->seq = session->next_seq++; session->aob.item->seq = session->next_seq++;
r = nghttp2_pq_push(&session->ob_pq, session->aob.item); rv = nghttp2_pq_push(&session->ob_pq, session->aob.item);
if(r == 0) { if(nghttp2_is_fatal(rv)) {
return rv;
}
session->aob.item = NULL; session->aob.item = NULL;
nghttp2_active_outbound_item_reset(&session->aob); nghttp2_active_outbound_item_reset(&session->aob);
} else { return 0;
/* FATAL error */
assert(r < NGHTTP2_ERR_FATAL);
nghttp2_active_outbound_item_reset(&session->aob);
return r;
} }
}
}
} else {
/* Unreachable */ /* Unreachable */
assert(0); assert(0);
}
return 0;
} }
int nghttp2_session_send(nghttp2_session *session) int nghttp2_session_send(nghttp2_session *session)