You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.0 KiB
C

#include "_nghttp2.h"
int on_error_callback(nghttp2_session *session, const char *msg,
size_t len, void *user_data);
int on_invalid_frame_recv_callback(nghttp2_session *session,
const nghttp2_frame *frame,
int lib_error_code, void *user_data);
int init_nghttp2_callbacks(nghttp2_session_callbacks *callbacks);
int on_error_callback(nghttp2_session *session, const char *msg,
size_t len, void *user_data)
{
printf("on error callback: %s\n", msg);
return 0;
}
int on_invalid_frame_recv_callback(nghttp2_session *session,
const nghttp2_frame *frame,
int lib_error_code, void *user_data)
{
printf("invalid frame recv: %s\n", nghttp2_strerror(lib_error_code));
return 0;
}
static ssize_t on_data_source_read_callback(nghttp2_session *session, int32_t stream_id,
uint8_t *buf, size_t length, uint32_t *data_flags,
nghttp2_data_source *source, void *user_data)
{
int ret = onDataSourceReadCallback(user_data, stream_id, buf, length);
if (ret == 0)
{
*data_flags = NGHTTP2_DATA_FLAG_EOF;
}
return ret;
}
static ssize_t on_send_callback(nghttp2_session *session,
const uint8_t *data, size_t length,
int flags, void *user_data)
{
return onDataSendCallback(user_data, (void *)data, length);
}
static int on_frame_recv_callback(nghttp2_session *session,
const nghttp2_frame *frame,
void *user_data)
{
switch (frame->hd.type)
{
case NGHTTP2_HEADERS:
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
{
onHeadersDoneCallback(user_data, frame->hd.stream_id);
}
case NGHTTP2_DATA:
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)
{
onStreamEndCallback(user_data, frame->hd.stream_id);
}
break;
}
return 0;
}
static int on_stream_close_callback(nghttp2_session *session,
int32_t stream_id,
uint32_t error_code,
void *user_data)
{
onStreamClose(user_data, stream_id);
return 0;
}
static int on_header_callback(nghttp2_session *session,
const nghttp2_frame *frame,
const uint8_t *name, size_t namelen,
const uint8_t *value,
size_t valuelen, uint8_t flags,
void *user_data)
{
switch (frame->hd.type)
{
case NGHTTP2_HEADERS:
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
{
onHeaderCallback(user_data, frame->hd.stream_id,
(void *)name, namelen, (void *)value, valuelen);
}
break;
}
return 0;
}
static int 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)
{
return onDataChunkRecv(user_data, stream_id, (void *)data, len);
}
static int on_begin_headers_callback(nghttp2_session *session,
const nghttp2_frame *frame,
void *user_data)
{
switch (frame->hd.type)
{
case NGHTTP2_HEADERS:
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
{
onBeginHeaderCallback(user_data, frame->hd.stream_id);
}
break;
}
return 0;
}
nghttp2_session *init_nghttp2_server_session(size_t data)
{
nghttp2_session_callbacks *callbacks;
nghttp2_session *session;
nghttp2_session_callbacks_new(&callbacks);
init_nghttp2_callbacks(callbacks);
nghttp2_session_server_new(&session, callbacks, (void *)((int *)(data)));
nghttp2_session_callbacks_del(callbacks);
return session;
}
nghttp2_session *init_nghttp2_client_session(size_t data)
{
nghttp2_session_callbacks *callbacks;
nghttp2_session *session;
nghttp2_session_callbacks_new(&callbacks);
init_nghttp2_callbacks(callbacks);
nghttp2_session_client_new(&session, callbacks, (void *)((int *)(data)));
nghttp2_session_callbacks_del(callbacks);
return session;
}
int init_nghttp2_callbacks(nghttp2_session_callbacks *callbacks)
{
nghttp2_session_callbacks_set_send_callback(callbacks, on_send_callback);
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
on_frame_recv_callback);
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(callbacks,
on_invalid_frame_recv_callback);
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
callbacks, on_data_chunk_recv_callback);
nghttp2_session_callbacks_set_on_header_callback(callbacks,
on_header_callback);
nghttp2_session_callbacks_set_error_callback(callbacks, on_error_callback);
nghttp2_session_callbacks_set_on_begin_headers_callback(
callbacks, on_begin_headers_callback);
}
int send_connection_header(nghttp2_session *session)
{
nghttp2_settings_entry iv[1] = {
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}};
int rv;
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv,
ARRLEN(iv));
return rv;
}
int data_provider_set_callback(size_t dp, size_t data, int t)
{
nghttp2_data_provider *cdp = (nghttp2_data_provider *)dp;
cdp->source.ptr = (void *)data;
cdp->read_callback = on_data_source_read_callback;
}
int _nghttp2_submit_response(nghttp2_session *sess, int streamid,
size_t nv, size_t nvlen, nghttp2_data_provider *dp)
{
return nghttp2_submit_response(sess, streamid, (nghttp2_nv *)nv, nvlen, dp);
}
int _nghttp2_submit_request(nghttp2_session *session, const nghttp2_priority_spec *pri_spec,
size_t nva, size_t nvlen,
const nghttp2_data_provider *data_prd, void *stream_user_data)
{
return nghttp2_submit_request(session, pri_spec,
(nghttp2_nv *)nva, nvlen,
data_prd, stream_user_data);
}