2013-07-12 17:19:03 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2.0 C Library
|
|
|
|
*
|
2013-09-08 23:21:06 +02:00
|
|
|
* Copyright (c) 2012, 2013 Tatsuhiro Tsujikawa
|
2013-07-12 17:19:03 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "nghttp2_submit.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2013-08-08 17:58:52 +02:00
|
|
|
#include <assert.h>
|
2013-07-12 17:19:03 +02:00
|
|
|
|
|
|
|
#include "nghttp2_session.h"
|
|
|
|
#include "nghttp2_frame.h"
|
|
|
|
#include "nghttp2_helper.h"
|
|
|
|
|
2013-09-10 17:55:35 +02:00
|
|
|
/* This function takes ownership of |nva_copy|. Regardless of the
|
|
|
|
return value, the caller must not free |nva_copy| after this
|
|
|
|
function returns. */
|
2013-07-15 14:45:59 +02:00
|
|
|
static int nghttp2_submit_headers_shared
|
2013-07-12 17:19:03 +02:00
|
|
|
(nghttp2_session *session,
|
|
|
|
uint8_t flags,
|
2013-07-15 14:45:59 +02:00
|
|
|
int32_t stream_id,
|
|
|
|
int32_t pri,
|
2013-09-10 17:55:35 +02:00
|
|
|
nghttp2_nv *nva_copy,
|
|
|
|
size_t nvlen,
|
2013-07-12 17:19:03 +02:00
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2013-09-10 17:55:35 +02:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
uint8_t flags_copy;
|
2013-09-10 17:55:35 +02:00
|
|
|
nghttp2_frame *frame = NULL;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_data_provider *data_prd_copy = NULL;
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_headers_aux_data *aux_data = NULL;
|
|
|
|
if(pri < 0) {
|
2013-09-10 17:55:35 +02:00
|
|
|
rv = NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
goto fail;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
if(data_prd != NULL && data_prd->read_callback != NULL) {
|
|
|
|
data_prd_copy = malloc(sizeof(nghttp2_data_provider));
|
|
|
|
if(data_prd_copy == NULL) {
|
2013-09-10 17:55:35 +02:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
*data_prd_copy = *data_prd;
|
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
if(data_prd || stream_user_data) {
|
|
|
|
aux_data = malloc(sizeof(nghttp2_headers_aux_data));
|
|
|
|
if(aux_data == NULL) {
|
2013-09-10 17:55:35 +02:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
2013-07-15 14:45:59 +02:00
|
|
|
}
|
|
|
|
aux_data->data_prd = data_prd_copy;
|
|
|
|
aux_data->stream_user_data = stream_user_data;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
|
|
|
if(frame == NULL) {
|
2013-09-10 17:55:35 +02:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
/* TODO Implement header continuation */
|
|
|
|
flags_copy = (flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
|
|
|
|
NGHTTP2_FLAG_END_HEADERS;
|
|
|
|
|
|
|
|
nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, pri,
|
2013-07-19 17:08:14 +02:00
|
|
|
nva_copy, nvlen);
|
2013-09-10 17:55:35 +02:00
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame,
|
|
|
|
aux_data);
|
|
|
|
if(rv != 0) {
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_headers_free(&frame->headers);
|
2013-09-10 17:55:35 +02:00
|
|
|
goto fail2;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-09-10 17:55:35 +02:00
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
/* nghttp2_frame_headers_init() takes ownership of nva_copy. */
|
|
|
|
nghttp2_nv_array_del(nva_copy);
|
|
|
|
fail2:
|
|
|
|
free(frame);
|
|
|
|
free(aux_data);
|
|
|
|
free(data_prd_copy);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nghttp2_submit_headers_shared_nva
|
|
|
|
(nghttp2_session *session,
|
|
|
|
uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
int32_t pri,
|
|
|
|
const nghttp2_nv *nva,
|
|
|
|
size_t nvlen,
|
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
|
|
|
ssize_t rv;
|
|
|
|
nghttp2_nv *nva_copy;
|
|
|
|
rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen);
|
|
|
|
if(rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return nghttp2_submit_headers_shared(session, flags, stream_id,
|
|
|
|
pri, nva_copy, rv, data_prd,
|
|
|
|
stream_user_data);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
|
2013-07-15 14:45:59 +02:00
|
|
|
int32_t stream_id, int32_t pri,
|
2013-12-08 13:19:33 +01:00
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
|
|
|
void *stream_user_data)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-12-08 13:19:33 +01:00
|
|
|
return nghttp2_submit_headers_shared_nva(session, flags, stream_id, pri,
|
|
|
|
nva, nvlen, NULL, stream_user_data);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
|
|
|
|
uint8_t *opaque_data)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
return nghttp2_session_add_ping(session, NGHTTP2_FLAG_NONE, opaque_data);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id, int32_t pri)
|
2013-07-22 17:11:39 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
nghttp2_frame *frame;
|
2013-08-09 17:02:24 +02:00
|
|
|
nghttp2_stream *stream;
|
2013-07-22 17:11:39 +02:00
|
|
|
if(pri < 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2013-08-09 17:02:24 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
if(stream == NULL) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
|
|
|
}
|
2013-07-22 17:11:39 +02:00
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
nghttp2_frame_priority_init(&frame->priority, stream_id, pri);
|
|
|
|
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
|
|
|
|
if(r != 0) {
|
|
|
|
nghttp2_frame_priority_free(&frame->priority);
|
|
|
|
free(frame);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_error_code error_code)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
return nghttp2_session_add_rst_stream(session, stream_id, error_code);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_error_code error_code,
|
|
|
|
uint8_t *opaque_data, size_t opaque_data_len)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-11-28 15:26:34 +01:00
|
|
|
return nghttp2_session_add_goaway(session, session->last_stream_id,
|
2013-07-15 14:45:59 +02:00
|
|
|
error_code, opaque_data, opaque_data_len);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 15:50:24 +02:00
|
|
|
int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
|
2013-07-12 17:19:03 +02:00
|
|
|
const nghttp2_settings_entry *iv, size_t niv)
|
|
|
|
{
|
2013-10-27 11:22:51 +01:00
|
|
|
return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
int nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
|
2013-12-08 13:19:33 +01:00
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen)
|
2013-07-24 18:49:05 +02:00
|
|
|
{
|
|
|
|
nghttp2_frame *frame;
|
2013-12-08 13:19:33 +01:00
|
|
|
nghttp2_nv *nva_copy;
|
2013-07-24 18:49:05 +02:00
|
|
|
uint8_t flags_copy;
|
2013-12-08 13:19:33 +01:00
|
|
|
int rv;
|
2013-07-24 18:49:05 +02:00
|
|
|
|
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2013-12-08 13:19:33 +01:00
|
|
|
rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen);
|
|
|
|
if(rv < 0) {
|
2013-07-24 18:49:05 +02:00
|
|
|
free(frame);
|
2013-12-08 13:19:33 +01:00
|
|
|
return rv;
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
|
|
|
/* TODO Implement header continuation */
|
|
|
|
flags_copy = NGHTTP2_FLAG_END_PUSH_PROMISE;
|
|
|
|
nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy,
|
2013-12-08 13:19:33 +01:00
|
|
|
stream_id, -1, nva_copy, nvlen);
|
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
|
|
|
|
if(rv != 0) {
|
2013-07-24 18:49:05 +02:00
|
|
|
nghttp2_frame_push_promise_free(&frame->push_promise);
|
|
|
|
free(frame);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
int32_t window_size_increment)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-08-08 17:58:52 +02:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream *stream;
|
2013-08-18 15:04:09 +02:00
|
|
|
if(window_size_increment == 0) {
|
2013-10-12 10:02:37 +02:00
|
|
|
return 0;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-08-18 15:04:09 +02:00
|
|
|
flags = 0;
|
2013-07-15 14:45:59 +02:00
|
|
|
if(stream_id == 0) {
|
2013-08-08 17:58:52 +02:00
|
|
|
if(!session->local_flow_control) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
rv = nghttp2_adjust_local_window_size(&session->local_window_size,
|
|
|
|
&session->recv_window_size,
|
2013-10-12 10:02:37 +02:00
|
|
|
&session->recv_reduction,
|
|
|
|
&window_size_increment);
|
2013-08-08 17:58:52 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
} else {
|
2013-07-15 14:45:59 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
2013-08-08 17:58:52 +02:00
|
|
|
if(!stream->local_flow_control) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
rv = nghttp2_adjust_local_window_size(&stream->local_window_size,
|
|
|
|
&stream->recv_window_size,
|
2013-10-12 10:02:37 +02:00
|
|
|
&stream->recv_reduction,
|
|
|
|
&window_size_increment);
|
2013-08-08 17:58:52 +02:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
} else {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
|
|
|
}
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-08-09 16:46:12 +02:00
|
|
|
if(window_size_increment > 0) {
|
2013-08-08 17:58:52 +02:00
|
|
|
return nghttp2_session_add_window_update(session, flags, stream_id,
|
|
|
|
window_size_increment);
|
|
|
|
}
|
|
|
|
return 0;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-10 17:55:35 +02:00
|
|
|
static uint8_t set_request_flags(int32_t pri,
|
|
|
|
const nghttp2_data_provider *data_prd)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
uint8_t flags = NGHTTP2_FLAG_NONE;
|
2013-07-12 17:19:03 +02:00
|
|
|
if(data_prd == NULL || data_prd->read_callback == NULL) {
|
2013-07-15 14:45:59 +02:00
|
|
|
flags |= NGHTTP2_FLAG_END_STREAM;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
if(pri != NGHTTP2_PRI_DEFAULT) {
|
|
|
|
flags |= NGHTTP2_FLAG_PRIORITY;
|
|
|
|
}
|
2013-09-10 17:55:35 +02:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_submit_request(nghttp2_session *session, int32_t pri,
|
2013-12-08 13:19:33 +01:00
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
2013-09-10 17:55:35 +02:00
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
|
|
|
uint8_t flags = set_request_flags(pri, data_prd);
|
|
|
|
return nghttp2_submit_headers_shared_nva(session, flags, -1, pri, nva, nvlen,
|
|
|
|
data_prd, stream_user_data);
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 13:35:48 +01:00
|
|
|
static uint8_t set_response_flags(const nghttp2_data_provider *data_prd)
|
2013-07-12 17:19:03 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
uint8_t flags = NGHTTP2_FLAG_NONE;
|
|
|
|
if(data_prd == NULL || data_prd->read_callback == NULL) {
|
|
|
|
flags |= NGHTTP2_FLAG_END_STREAM;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-11-28 13:35:48 +01:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_submit_response(nghttp2_session *session,
|
2013-12-08 13:19:33 +01:00
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
2013-11-28 13:35:48 +01:00
|
|
|
const nghttp2_data_provider *data_prd)
|
|
|
|
{
|
|
|
|
uint8_t flags = set_response_flags(data_prd);
|
|
|
|
return nghttp2_submit_headers_shared_nva(session, flags, stream_id,
|
|
|
|
NGHTTP2_PRI_DEFAULT, nva, nvlen,
|
|
|
|
data_prd, NULL);
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
2013-07-12 17:19:03 +02:00
|
|
|
const nghttp2_data_provider *data_prd)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
nghttp2_data *data_frame;
|
|
|
|
uint8_t nflags = 0;
|
2013-08-09 17:02:24 +02:00
|
|
|
|
|
|
|
if(nghttp2_session_get_stream(session, stream_id) == NULL) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
|
|
|
}
|
2013-09-07 09:38:21 +02:00
|
|
|
data_frame = malloc(sizeof(nghttp2_data));
|
2013-07-12 17:19:03 +02:00
|
|
|
if(data_frame == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
if(flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
nflags |= NGHTTP2_FLAG_END_STREAM;
|
2013-07-12 17:19:03 +02:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_data_init(data_frame, nflags, stream_id, data_prd);
|
|
|
|
r = nghttp2_session_add_frame(session, NGHTTP2_CAT_DATA, data_frame, NULL);
|
2013-07-12 17:19:03 +02:00
|
|
|
if(r != 0) {
|
|
|
|
nghttp2_frame_data_free(data_frame);
|
|
|
|
free(data_frame);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2013-08-03 11:05:14 +02:00
|
|
|
|
|
|
|
ssize_t nghttp2_pack_settings_payload(uint8_t *buf,
|
2013-09-08 23:21:06 +02:00
|
|
|
size_t buflen,
|
2013-09-09 14:30:39 +02:00
|
|
|
const nghttp2_settings_entry *iv,
|
|
|
|
size_t niv)
|
2013-08-03 11:05:14 +02:00
|
|
|
{
|
2013-08-17 16:41:04 +02:00
|
|
|
/* Assume that current flow_control_option is 0 (which means that
|
|
|
|
flow control is enabled) */
|
|
|
|
if(!nghttp2_iv_check(iv, niv, 0)) {
|
2013-08-03 11:05:14 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2013-09-08 23:21:06 +02:00
|
|
|
|
|
|
|
if(buflen < (niv * 8))
|
|
|
|
return NGHTTP2_ERR_INSUFF_BUFSIZE;
|
|
|
|
|
2013-08-03 11:05:14 +02:00
|
|
|
return nghttp2_frame_pack_settings_payload(buf, iv, niv);
|
|
|
|
}
|