mirror of
https://github.com/nghttp2/nghttp2.git
synced 2025-12-08 19:18:53 +08:00
Remove nghttp2_on_data_recv_callback and nghttp2_on_data_send_callback
nghttp2_data is added to nghttp2_frame union. When DATA is received, nghttp2_on_frame_recv_callback is called. When DATA is sent, nghttp2_on_frame_send_callback is called.
This commit is contained in:
@@ -579,6 +579,16 @@ typedef struct {
|
||||
nghttp2_data_source_read_callback read_callback;
|
||||
} nghttp2_data_provider;
|
||||
|
||||
/**
|
||||
* @struct
|
||||
*
|
||||
* The DATA frame. The received data is delivered via
|
||||
* :type:`nghttp2_on_data_chunk_recv_callback`.
|
||||
*/
|
||||
typedef struct {
|
||||
nghttp2_frame_hd hd;
|
||||
} nghttp2_data;
|
||||
|
||||
/**
|
||||
* @enum
|
||||
*
|
||||
@@ -795,6 +805,10 @@ typedef union {
|
||||
* The frame header, which is convenient to inspect frame header.
|
||||
*/
|
||||
nghttp2_frame_hd hd;
|
||||
/**
|
||||
* The DATA frame.
|
||||
*/
|
||||
nghttp2_data data;
|
||||
/**
|
||||
* The HEADERS frame.
|
||||
*/
|
||||
@@ -869,9 +883,9 @@ typedef ssize_t (*nghttp2_recv_callback)
|
||||
/**
|
||||
* @functypedef
|
||||
*
|
||||
* Callback function invoked by `nghttp2_session_recv()` when a
|
||||
* non-DATA frame is received. The |user_data| pointer is the third
|
||||
* argument passed in to the call to `nghttp2_session_client_new()` or
|
||||
* Callback function invoked by `nghttp2_session_recv()` when a aframe
|
||||
* is received. The |user_data| pointer is the third argument passed
|
||||
* in to the call to `nghttp2_session_client_new()` or
|
||||
* `nghttp2_session_server_new()`.
|
||||
*
|
||||
* If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
|
||||
@@ -923,7 +937,7 @@ typedef int (*nghttp2_on_invalid_frame_recv_callback)
|
||||
* to. The |flags| is the flags of DATA frame which this data chunk is
|
||||
* contained. ``(flags & NGHTTP2_FLAG_END_STREAM) != 0`` does not
|
||||
* necessarily mean this chunk of data is the last one in the
|
||||
* stream. You should use :type:`nghttp2_on_data_recv_callback` to
|
||||
* stream. You should use :type:`nghttp2_on_frame_recv_callback` to
|
||||
* know all data frames are received. The |user_data| pointer is the
|
||||
* third argument passed in to the call to
|
||||
* `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
|
||||
@@ -946,24 +960,6 @@ typedef int (*nghttp2_on_data_chunk_recv_callback)
|
||||
(nghttp2_session *session, uint8_t flags, int32_t stream_id,
|
||||
const uint8_t *data, size_t len, void *user_data);
|
||||
|
||||
/**
|
||||
* @functypedef
|
||||
*
|
||||
* Callback function invoked when DATA frame is received. The actual
|
||||
* data it contains are received by
|
||||
* :type:`nghttp2_on_data_chunk_recv_callback`. The |user_data|
|
||||
* pointer is the third argument passed in to the call to
|
||||
* `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
|
||||
*
|
||||
* The implementation of this function must return 0 if it
|
||||
* succeeds. If nonzero is returned, it is treated as fatal error and
|
||||
* `nghttp2_session_recv()` and `nghttp2_session_send()` functions
|
||||
* immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
|
||||
*/
|
||||
typedef int (*nghttp2_on_data_recv_callback)
|
||||
(nghttp2_session *session, uint16_t length, uint8_t flags, int32_t stream_id,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* @functypedef
|
||||
*
|
||||
@@ -986,10 +982,9 @@ typedef int (*nghttp2_before_frame_send_callback)
|
||||
/**
|
||||
* @functypedef
|
||||
*
|
||||
* Callback function invoked after the non-DATA frame |frame| is sent.
|
||||
* The |user_data| pointer is the third argument passed in to the call
|
||||
* to `nghttp2_session_client_new()` or
|
||||
* `nghttp2_session_server_new()`.
|
||||
* Callback function invoked after the frame |frame| is sent. The
|
||||
* |user_data| pointer is the third argument passed in to the call to
|
||||
* `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
|
||||
*
|
||||
* The implementation of this function must return 0 if it
|
||||
* succeeds. If nonzero is returned, it is treated as fatal error and
|
||||
@@ -1018,22 +1013,6 @@ typedef int (*nghttp2_on_frame_not_send_callback)
|
||||
(nghttp2_session *session, const nghttp2_frame *frame, int lib_error_code,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* @functypedef
|
||||
*
|
||||
* Callback function invoked after DATA frame is sent. The |user_data|
|
||||
* pointer is the third argument passed in to the call to
|
||||
* `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
|
||||
*
|
||||
* The implementation of this function must return 0 if it
|
||||
* succeeds. If nonzero is returned, it is treated as fatal error and
|
||||
* `nghttp2_session_recv()` and `nghttp2_session_send()` functions
|
||||
* immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
|
||||
*/
|
||||
typedef int (*nghttp2_on_data_send_callback)
|
||||
(nghttp2_session *session, uint16_t length, uint8_t flags, int32_t stream_id,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* @functypedef
|
||||
*
|
||||
@@ -1190,10 +1169,6 @@ typedef struct {
|
||||
* received.
|
||||
*/
|
||||
nghttp2_on_data_chunk_recv_callback on_data_chunk_recv_callback;
|
||||
/**
|
||||
* Callback function invoked when DATA frame is received.
|
||||
*/
|
||||
nghttp2_on_data_recv_callback on_data_recv_callback;
|
||||
/**
|
||||
* Callback function invoked before the non-DATA frame is sent.
|
||||
*/
|
||||
@@ -1207,10 +1182,6 @@ typedef struct {
|
||||
* because of an error.
|
||||
*/
|
||||
nghttp2_on_frame_not_send_callback on_frame_not_send_callback;
|
||||
/**
|
||||
* Callback function invoked after DATA frame is sent.
|
||||
*/
|
||||
nghttp2_on_data_send_callback on_data_send_callback;
|
||||
/**
|
||||
* Callback function invoked when the stream is closed.
|
||||
*/
|
||||
@@ -1432,13 +1403,9 @@ void nghttp2_session_del(nghttp2_session *session);
|
||||
* invoked.
|
||||
* 6. :member:`nghttp2_session_callbacks.send_callback` is invoked one
|
||||
* or more times to send the frame.
|
||||
* 7. If the frame is a control frame,
|
||||
* :member:`nghttp2_session_callbacks.on_frame_send_callback` is
|
||||
* 7. :member:`nghttp2_session_callbacks.on_frame_send_callback` is
|
||||
* invoked.
|
||||
* 8. If the frame is a DATA frame,
|
||||
* :member:`nghttp2_session_callbacks.on_data_send_callback` is
|
||||
* invoked.
|
||||
* 9. If the transmission of the frame triggers closure of the stream,
|
||||
* 8. If the transmission of the frame triggers closure of the stream,
|
||||
* the stream is closed and
|
||||
* :member:`nghttp2_session_callbacks.on_stream_close_callback` is
|
||||
* invoked.
|
||||
@@ -1474,7 +1441,7 @@ int nghttp2_session_send(nghttp2_session *session);
|
||||
* :member:`nghttp2_session_callbacks.on_data_chunk_recv_callback`
|
||||
* is invoked.
|
||||
* 2. If one DATA frame is completely received,
|
||||
* :member:`nghttp2_session_callbacks.on_data_recv_callback` is
|
||||
* :member:`nghttp2_session_callbacks.on_frame_recv_callback` is
|
||||
* invoked. If the frame is the final frame of the request,
|
||||
* :member:`nghttp2_session_callbacks.on_request_recv_callback`
|
||||
* is invoked. If the reception of the frame triggers the
|
||||
|
||||
@@ -181,9 +181,10 @@ void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
|
||||
void nghttp2_frame_window_update_free(nghttp2_window_update *frame)
|
||||
{}
|
||||
|
||||
void nghttp2_frame_data_init(nghttp2_private_data *frame, uint8_t flags,
|
||||
int32_t stream_id,
|
||||
const nghttp2_data_provider *data_prd)
|
||||
void nghttp2_frame_private_data_init(nghttp2_private_data *frame,
|
||||
uint8_t flags,
|
||||
int32_t stream_id,
|
||||
const nghttp2_data_provider *data_prd)
|
||||
{
|
||||
memset(frame, 0, sizeof(nghttp2_private_data));
|
||||
/* At this moment, the length of DATA frame is unknown */
|
||||
@@ -191,7 +192,7 @@ void nghttp2_frame_data_init(nghttp2_private_data *frame, uint8_t flags,
|
||||
frame->data_prd = *data_prd;
|
||||
}
|
||||
|
||||
void nghttp2_frame_data_free(nghttp2_private_data *frame)
|
||||
void nghttp2_frame_private_data_free(nghttp2_private_data *frame)
|
||||
{}
|
||||
|
||||
/*
|
||||
|
||||
@@ -416,11 +416,12 @@ void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
|
||||
|
||||
void nghttp2_frame_window_update_free(nghttp2_window_update *frame);
|
||||
|
||||
void nghttp2_frame_data_init(nghttp2_private_data *frame, uint8_t flags,
|
||||
int32_t stream_id,
|
||||
const nghttp2_data_provider *data_prd);
|
||||
void nghttp2_frame_private_data_init(nghttp2_private_data *frame,
|
||||
uint8_t flags,
|
||||
int32_t stream_id,
|
||||
const nghttp2_data_provider *data_prd);
|
||||
|
||||
void nghttp2_frame_data_free(nghttp2_private_data *frame);
|
||||
void nghttp2_frame_private_data_free(nghttp2_private_data *frame);
|
||||
|
||||
/*
|
||||
* Makes copy of |iv| and return the copy. The |niv| is the number of
|
||||
|
||||
@@ -66,7 +66,7 @@ void nghttp2_outbound_item_free(nghttp2_outbound_item *item)
|
||||
} else if(item->frame_cat == NGHTTP2_CAT_DATA) {
|
||||
nghttp2_private_data *data_frame;
|
||||
data_frame = nghttp2_outbound_item_get_data_frame(item);
|
||||
nghttp2_frame_data_free(data_frame);
|
||||
nghttp2_frame_private_data_free(data_frame);
|
||||
} else {
|
||||
/* Unreachable */
|
||||
assert(0);
|
||||
|
||||
@@ -1387,6 +1387,20 @@ nghttp2_outbound_item* nghttp2_session_pop_next_ob_item
|
||||
}
|
||||
}
|
||||
|
||||
static int session_call_on_frame_send(nghttp2_session *session,
|
||||
nghttp2_frame *frame)
|
||||
{
|
||||
int rv;
|
||||
if(session->callbacks.on_frame_send_callback) {
|
||||
rv = session->callbacks.on_frame_send_callback(session, frame,
|
||||
session->user_data);
|
||||
if(rv != 0) {
|
||||
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Called after a frame is sent.
|
||||
*
|
||||
@@ -1431,11 +1445,9 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
|
||||
CONTINUATION frame. */
|
||||
frame->hd.length = session->aob.framebuflen - NGHTTP2_FRAME_HEAD_LENGTH;
|
||||
}
|
||||
if(session->callbacks.on_frame_send_callback) {
|
||||
if(session->callbacks.on_frame_send_callback(session, frame,
|
||||
session->user_data) != 0) {
|
||||
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
}
|
||||
r = session_call_on_frame_send(session, frame);
|
||||
if(nghttp2_is_fatal(r)) {
|
||||
return r;
|
||||
}
|
||||
switch(frame->hd.type) {
|
||||
case NGHTTP2_HEADERS: {
|
||||
@@ -1573,16 +1585,18 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session)
|
||||
} else if(item->frame_cat == NGHTTP2_CAT_DATA) {
|
||||
int r;
|
||||
nghttp2_private_data *data_frame;
|
||||
|
||||
data_frame = nghttp2_outbound_item_get_data_frame(session->aob.item);
|
||||
if(session->callbacks.on_data_send_callback) {
|
||||
if(session->callbacks.on_data_send_callback
|
||||
(session,
|
||||
session->aob.framebuflen - NGHTTP2_FRAME_HEAD_LENGTH,
|
||||
data_frame->eof ? data_frame->hd.flags :
|
||||
(data_frame->hd.flags & (~NGHTTP2_FLAG_END_STREAM)),
|
||||
data_frame->hd.stream_id,
|
||||
session->user_data) != 0) {
|
||||
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
if(session->callbacks.on_frame_send_callback) {
|
||||
nghttp2_frame public_data_frame = { data_frame->hd };
|
||||
/* flags may have NGHTTP2_FLAG_END_STREAM even if the sent chunk
|
||||
is not the end of the stream */
|
||||
if(!data_frame->eof) {
|
||||
public_data_frame.hd.flags &= ~NGHTTP2_FLAG_END_STREAM;
|
||||
}
|
||||
r = session_call_on_frame_send(session, &public_data_frame);
|
||||
if(nghttp2_is_fatal(r)) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
if(data_frame->eof && (data_frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
|
||||
@@ -3137,34 +3151,31 @@ static int session_process_window_update_frame(nghttp2_session *session)
|
||||
/* } */
|
||||
|
||||
int nghttp2_session_on_data_received(nghttp2_session *session,
|
||||
uint16_t length, uint8_t flags,
|
||||
int32_t stream_id)
|
||||
nghttp2_frame *frame)
|
||||
{
|
||||
int rv = 0;
|
||||
nghttp2_stream *stream;
|
||||
|
||||
stream = nghttp2_session_get_stream(session, stream_id);
|
||||
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
||||
if(!stream) {
|
||||
/* This should be treated as stream error, but it results in lots
|
||||
of RST_STREAM. So just ignore frame against nonexistent stream
|
||||
for now. */
|
||||
return 0;
|
||||
}
|
||||
if(session->callbacks.on_data_recv_callback) {
|
||||
if(session->callbacks.on_data_recv_callback
|
||||
(session, length, flags, stream_id, session->user_data) != 0) {
|
||||
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
}
|
||||
rv = nghttp2_session_call_on_frame_received(session, frame);
|
||||
if(nghttp2_is_fatal(rv)) {
|
||||
return rv;
|
||||
}
|
||||
if(!nghttp2_session_is_my_stream_id(session, stream_id)) {
|
||||
if(flags & NGHTTP2_FLAG_END_STREAM) {
|
||||
rv = nghttp2_session_call_on_request_recv(session, stream_id);
|
||||
if(!nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
|
||||
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
||||
rv = nghttp2_session_call_on_request_recv(session, frame->hd.stream_id);
|
||||
if(rv != 0) {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(flags & NGHTTP2_FLAG_END_STREAM) {
|
||||
if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
||||
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
|
||||
rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
|
||||
if(nghttp2_is_fatal(rv)) {
|
||||
@@ -3178,10 +3189,8 @@ int nghttp2_session_on_data_received(nghttp2_session *session,
|
||||
static int nghttp2_session_process_data_frame(nghttp2_session *session)
|
||||
{
|
||||
int r;
|
||||
nghttp2_frame *frame = &session->iframe.frame;
|
||||
r = nghttp2_session_on_data_received(session,
|
||||
frame->hd.length, frame->hd.flags,
|
||||
frame->hd.stream_id);
|
||||
nghttp2_frame *public_data_frame = &session->iframe.frame;
|
||||
r = nghttp2_session_on_data_received(session, public_data_frame);
|
||||
if(nghttp2_is_fatal(r)) {
|
||||
return r;
|
||||
} else {
|
||||
|
||||
@@ -488,17 +488,19 @@ int nghttp2_session_on_window_update_received(nghttp2_session *session,
|
||||
nghttp2_frame *frame);
|
||||
|
||||
/*
|
||||
* Called when DATA is received.
|
||||
* Called when DATA is received, assuming |frame| is properly
|
||||
* initialized.
|
||||
*
|
||||
* This function returns 0 if it succeeds, or one of the following
|
||||
* negative error codes:
|
||||
*
|
||||
* NGHTTP2_ERR_NOMEM
|
||||
* Out of memory.
|
||||
* NGHTTP2_ERR_CALLBACK_FAILURE
|
||||
* The callback function failed.
|
||||
*/
|
||||
int nghttp2_session_on_data_received(nghttp2_session *session,
|
||||
uint16_t length, uint8_t flags,
|
||||
int32_t stream_id);
|
||||
nghttp2_frame *frame);
|
||||
|
||||
/*
|
||||
* Returns nghttp2_stream* object whose stream ID is |stream_id|. It
|
||||
|
||||
@@ -311,10 +311,10 @@ int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
|
||||
if(flags & NGHTTP2_FLAG_END_STREAM) {
|
||||
nflags |= NGHTTP2_FLAG_END_STREAM;
|
||||
}
|
||||
nghttp2_frame_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);
|
||||
if(r != 0) {
|
||||
nghttp2_frame_data_free(data_frame);
|
||||
nghttp2_frame_private_data_free(data_frame);
|
||||
free(data_frame);
|
||||
}
|
||||
return r;
|
||||
|
||||
Reference in New Issue
Block a user