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.
nghttp2-go/nghttp2.c

206 lines
7.0 KiB
C

7 years ago
#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)
6 years ago
{
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;
6 years ago
}
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)
6 years ago
{
switch (frame->hd.type)
{
case NGHTTP2_HEADERS:
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
6 years ago
{
onHeadersDoneCallback(user_data, frame->hd.stream_id);
6 years ago
}
case NGHTTP2_DATA:
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)
{
onStreamEndCallback(user_data, frame->hd.stream_id);
6 years ago
}
break;
}
return 0;
}
static int on_stream_close_callback(nghttp2_session *session,
int32_t stream_id,
uint32_t error_code,
void *user_data)
6 years ago
{
onStreamClose(user_data, stream_id);
6 years ago
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)
6 years ago
{
switch (frame->hd.type)
{
case NGHTTP2_HEADERS:
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
6 years ago
{
onHeaderCallback(user_data, frame->hd.stream_id,
(void *)name, namelen, (void *)value, valuelen);
6 years ago
}
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)
6 years ago
{
return onDataChunkRecv(user_data, stream_id, (void *)data, len);
6 years ago
}
static int on_begin_headers_callback(nghttp2_session *session,
const nghttp2_frame *frame,
void *user_data)
6 years ago
{
switch (frame->hd.type)
{
case NGHTTP2_HEADERS:
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
6 years ago
{
onBeginHeaderCallback(user_data, frame->hd.stream_id);
6 years ago
}
break;
}
return 0;
}
nghttp2_session *init_nghttp2_server_session(size_t data)
6 years ago
{
nghttp2_session_callbacks *callbacks;
nghttp2_session *session;
nghttp2_session_callbacks_new(&callbacks);
init_nghttp2_callbacks(callbacks);
6 years ago
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)
7 years ago
{
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)));
7 years ago
nghttp2_session_callbacks_del(callbacks);
return session;
7 years ago
}
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);
7 years ago
nghttp2_session_callbacks_set_on_stream_close_callback(
callbacks, on_stream_close_callback);
7 years ago
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(callbacks,
on_invalid_frame_recv_callback);
7 years ago
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
callbacks, on_data_chunk_recv_callback);
7 years ago
nghttp2_session_callbacks_set_on_header_callback(callbacks,
on_header_callback);
7 years ago
nghttp2_session_callbacks_set_error_callback(callbacks, on_error_callback);
7 years ago
nghttp2_session_callbacks_set_on_begin_headers_callback(
callbacks, on_begin_headers_callback);
}
7 years ago
int send_connection_header(nghttp2_session *session)
{
nghttp2_settings_entry iv[1] = {
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100}};
7 years ago
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);
7 years ago
}