2013-07-12 15:19:03 +00:00
|
|
|
/*
|
2014-03-30 10:09:21 +00:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-12 15:19:03 +00:00
|
|
|
*
|
2013-09-08 21:21:06 +00:00
|
|
|
* Copyright (c) 2012, 2013 Tatsuhiro Tsujikawa
|
2013-07-12 15:19:03 +00: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 15:58:52 +00:00
|
|
|
#include <assert.h>
|
2013-07-12 15:19:03 +00:00
|
|
|
|
|
|
|
#include "nghttp2_session.h"
|
|
|
|
#include "nghttp2_frame.h"
|
|
|
|
#include "nghttp2_helper.h"
|
2014-03-25 17:04:24 +00:00
|
|
|
#include "nghttp2_priority_spec.h"
|
2013-07-12 15:19:03 +00:00
|
|
|
|
2013-09-10 15:55:35 +00:00
|
|
|
/* This function takes ownership of |nva_copy|. Regardless of the
|
|
|
|
return value, the caller must not free |nva_copy| after this
|
|
|
|
function returns. */
|
2014-05-08 14:54:07 +00:00
|
|
|
static int32_t submit_headers_shared
|
2013-07-12 15:19:03 +00:00
|
|
|
(nghttp2_session *session,
|
|
|
|
uint8_t flags,
|
2013-07-15 12:45:59 +00:00
|
|
|
int32_t stream_id,
|
2014-03-25 17:04:24 +00:00
|
|
|
const nghttp2_priority_spec *pri_spec,
|
2013-09-10 15:55:35 +00:00
|
|
|
nghttp2_nv *nva_copy,
|
|
|
|
size_t nvlen,
|
2013-07-12 15:19:03 +00:00
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2013-09-10 15:55:35 +00:00
|
|
|
int rv;
|
2013-07-12 15:19:03 +00:00
|
|
|
uint8_t flags_copy;
|
2013-09-10 15:55:35 +00:00
|
|
|
nghttp2_frame *frame = NULL;
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_data_provider *data_prd_copy = NULL;
|
2013-07-15 12:45:59 +00:00
|
|
|
nghttp2_headers_aux_data *aux_data = NULL;
|
2014-05-07 14:24:07 +00:00
|
|
|
nghttp2_headers_category hcat;
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-06-07 09:48:37 +00:00
|
|
|
if(stream_id == 0) {
|
|
|
|
rv = NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:19:03 +00: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 15:55:35 +00:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
*data_prd_copy = *data_prd;
|
|
|
|
}
|
2013-07-15 12:45:59 +00:00
|
|
|
if(data_prd || stream_user_data) {
|
|
|
|
aux_data = malloc(sizeof(nghttp2_headers_aux_data));
|
|
|
|
if(aux_data == NULL) {
|
2013-09-10 15:55:35 +00:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
2013-07-15 12:45:59 +00:00
|
|
|
}
|
|
|
|
aux_data->data_prd = data_prd_copy;
|
|
|
|
aux_data->stream_user_data = stream_user_data;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
|
|
|
if(frame == NULL) {
|
2013-09-10 15:55:35 +00:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
goto fail;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-02-15 09:55:52 +00:00
|
|
|
flags_copy =
|
2014-03-25 17:04:24 +00:00
|
|
|
(flags & (NGHTTP2_FLAG_END_STREAM |
|
|
|
|
NGHTTP2_FLAG_END_SEGMENT |
|
2014-04-14 14:53:54 +00:00
|
|
|
NGHTTP2_FLAG_PRIORITY)) |
|
2013-07-15 12:45:59 +00:00
|
|
|
NGHTTP2_FLAG_END_HEADERS;
|
|
|
|
|
2014-05-07 14:24:07 +00:00
|
|
|
if(stream_id == -1) {
|
|
|
|
if(session->next_stream_id > INT32_MAX) {
|
|
|
|
rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_id = session->next_stream_id;
|
|
|
|
session->next_stream_id += 2;
|
|
|
|
|
|
|
|
hcat = NGHTTP2_HCAT_REQUEST;
|
|
|
|
} else {
|
|
|
|
/* More specific categorization will be done later. */
|
|
|
|
hcat = NGHTTP2_HCAT_HEADERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id,
|
|
|
|
hcat, pri_spec, nva_copy, nvlen);
|
|
|
|
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2013-09-10 15:55:35 +00:00
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame,
|
|
|
|
aux_data);
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2013-09-10 15:55:35 +00:00
|
|
|
if(rv != 0) {
|
2013-07-15 12:45:59 +00:00
|
|
|
nghttp2_frame_headers_free(&frame->headers);
|
2013-09-10 15:55:35 +00:00
|
|
|
goto fail2;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2014-05-07 14:24:07 +00:00
|
|
|
|
|
|
|
if(hcat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return stream_id;
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:55:35 +00:00
|
|
|
return 0;
|
2014-05-07 14:24:07 +00:00
|
|
|
|
2013-09-10 15:55:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:53:54 +00:00
|
|
|
static void adjust_priority_spec_weight(nghttp2_priority_spec *pri_spec)
|
2014-04-01 13:54:20 +00:00
|
|
|
{
|
2014-04-14 14:53:54 +00:00
|
|
|
if(pri_spec->weight < NGHTTP2_MIN_WEIGHT) {
|
|
|
|
pri_spec->weight = NGHTTP2_MIN_WEIGHT;
|
|
|
|
} else if(pri_spec->weight > NGHTTP2_MAX_WEIGHT) {
|
|
|
|
pri_spec->weight = NGHTTP2_MAX_WEIGHT;
|
2014-04-01 13:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
static int32_t submit_headers_shared_nva
|
2013-09-10 15:55:35 +00:00
|
|
|
(nghttp2_session *session,
|
|
|
|
uint8_t flags,
|
|
|
|
int32_t stream_id,
|
2014-03-25 17:04:24 +00:00
|
|
|
const nghttp2_priority_spec *pri_spec,
|
2013-09-10 15:55:35 +00:00
|
|
|
const nghttp2_nv *nva,
|
|
|
|
size_t nvlen,
|
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2014-06-11 14:37:16 +00:00
|
|
|
int rv;
|
2013-09-10 15:55:35 +00:00
|
|
|
nghttp2_nv *nva_copy;
|
2014-04-14 14:53:54 +00:00
|
|
|
nghttp2_priority_spec copy_pri_spec;
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-04-01 13:54:20 +00:00
|
|
|
if(pri_spec) {
|
|
|
|
copy_pri_spec = *pri_spec;
|
2014-04-14 14:53:54 +00:00
|
|
|
adjust_priority_spec_weight(©_pri_spec);
|
|
|
|
} else {
|
|
|
|
nghttp2_priority_spec_default_init(©_pri_spec);
|
2014-03-25 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 15:13:11 +00:00
|
|
|
rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen);
|
|
|
|
if(rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
return submit_headers_shared(session, flags, stream_id,
|
2014-06-11 14:37:16 +00:00
|
|
|
©_pri_spec, nva_copy, nvlen, data_prd,
|
2014-05-08 14:54:07 +00:00
|
|
|
stream_user_data);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 14:24:07 +00:00
|
|
|
int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_priority_spec *pri_spec,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
|
|
|
void *stream_user_data)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2014-03-25 17:04:24 +00:00
|
|
|
flags &= NGHTTP2_FLAG_END_STREAM;
|
|
|
|
|
2014-04-14 14:53:54 +00:00
|
|
|
if(pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
|
|
|
|
flags |= NGHTTP2_FLAG_PRIORITY;
|
|
|
|
} else {
|
|
|
|
pri_spec = NULL;
|
2014-03-25 17:04:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
return submit_headers_shared_nva(session, flags, stream_id, pri_spec,
|
|
|
|
nva, nvlen, NULL, stream_user_data);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 12:45:59 +00:00
|
|
|
|
2013-10-25 13:50:24 +00:00
|
|
|
int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
|
|
|
|
uint8_t *opaque_data)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2013-07-15 12:45:59 +00:00
|
|
|
return nghttp2_session_add_ping(session, NGHTTP2_FLAG_NONE, opaque_data);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:50:24 +00:00
|
|
|
int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
|
2014-03-25 17:04:24 +00:00
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_priority_spec *pri_spec)
|
2013-07-22 15:11:39 +00:00
|
|
|
{
|
2014-02-18 15:45:20 +00:00
|
|
|
int rv;
|
2013-07-22 15:11:39 +00:00
|
|
|
nghttp2_frame *frame;
|
2014-04-01 13:54:20 +00:00
|
|
|
nghttp2_priority_spec copy_pri_spec;
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-06-07 09:48:37 +00:00
|
|
|
if(stream_id == 0 || pri_spec == NULL) {
|
2013-07-22 15:11:39 +00:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-04-14 14:53:54 +00:00
|
|
|
if(stream_id == pri_spec->stream_id) {
|
2014-03-25 17:04:24 +00:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:53:54 +00:00
|
|
|
copy_pri_spec = *pri_spec;
|
|
|
|
|
|
|
|
adjust_priority_spec_weight(©_pri_spec);
|
|
|
|
|
2013-07-22 15:11:39 +00:00
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2013-07-22 15:11:39 +00:00
|
|
|
if(frame == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-04-01 13:54:20 +00:00
|
|
|
nghttp2_frame_priority_init(&frame->priority, stream_id, ©_pri_spec);
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-02-18 15:45:20 +00:00
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-02-18 15:45:20 +00:00
|
|
|
if(rv != 0) {
|
2013-07-22 15:11:39 +00:00
|
|
|
nghttp2_frame_priority_free(&frame->priority);
|
|
|
|
free(frame);
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2014-02-18 15:45:20 +00:00
|
|
|
return rv;
|
2013-07-22 15:11:39 +00:00
|
|
|
}
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2013-07-22 15:11:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-25 13:50:24 +00:00
|
|
|
int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
2013-07-15 12:45:59 +00:00
|
|
|
nghttp2_error_code error_code)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2014-06-07 09:48:37 +00:00
|
|
|
if(stream_id == 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2013-07-15 12:45:59 +00:00
|
|
|
return nghttp2_session_add_rst_stream(session, stream_id, error_code);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:50:24 +00:00
|
|
|
int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
|
2013-07-15 12:45:59 +00:00
|
|
|
nghttp2_error_code error_code,
|
2014-03-22 10:05:58 +00:00
|
|
|
const uint8_t *opaque_data, size_t opaque_data_len)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2014-06-18 01:56:32 +00:00
|
|
|
return nghttp2_session_add_goaway(session, session->last_proc_stream_id,
|
2013-07-15 12:45:59 +00:00
|
|
|
error_code, opaque_data, opaque_data_len);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 13:50:24 +00:00
|
|
|
int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
|
2013-07-12 15:19:03 +00:00
|
|
|
const nghttp2_settings_entry *iv, size_t niv)
|
|
|
|
{
|
2013-10-27 10:22:51 +00:00
|
|
|
return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 14:24:07 +00:00
|
|
|
int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
|
|
|
void *promised_stream_user_data)
|
2013-07-24 16:49:05 +00:00
|
|
|
{
|
|
|
|
nghttp2_frame *frame;
|
2013-12-08 12:19:33 +00:00
|
|
|
nghttp2_nv *nva_copy;
|
2013-07-24 16:49:05 +00:00
|
|
|
uint8_t flags_copy;
|
2014-02-24 15:26:12 +00:00
|
|
|
nghttp2_headers_aux_data *aux_data = NULL;
|
2014-05-07 14:24:07 +00:00
|
|
|
int32_t promised_stream_id;
|
2013-12-08 12:19:33 +00:00
|
|
|
int rv;
|
2013-07-24 16:49:05 +00:00
|
|
|
|
2014-06-07 09:48:37 +00:00
|
|
|
if(stream_id == 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:13:11 +00:00
|
|
|
if(!session->server) {
|
|
|
|
return NGHTTP2_ERR_PROTO;
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:49:05 +00:00
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-02-24 15:26:12 +00:00
|
|
|
if(promised_stream_user_data) {
|
|
|
|
aux_data = malloc(sizeof(nghttp2_headers_aux_data));
|
|
|
|
if(aux_data == NULL) {
|
|
|
|
free(frame);
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
aux_data->data_prd = NULL;
|
|
|
|
aux_data->stream_user_data = promised_stream_user_data;
|
|
|
|
}
|
2013-12-08 12:19:33 +00:00
|
|
|
rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen);
|
|
|
|
if(rv < 0) {
|
2014-02-24 15:26:12 +00:00
|
|
|
free(aux_data);
|
2013-07-24 16:49:05 +00:00
|
|
|
free(frame);
|
2013-12-08 12:19:33 +00:00
|
|
|
return rv;
|
2013-07-24 16:49:05 +00:00
|
|
|
}
|
2014-05-07 14:24:07 +00:00
|
|
|
|
2014-02-21 12:23:51 +00:00
|
|
|
flags_copy = NGHTTP2_FLAG_END_HEADERS;
|
2014-05-07 14:24:07 +00:00
|
|
|
|
|
|
|
/* All 32bit signed stream IDs are spent. */
|
|
|
|
if(session->next_stream_id > INT32_MAX) {
|
|
|
|
free(aux_data);
|
|
|
|
free(frame);
|
|
|
|
|
|
|
|
return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
promised_stream_id = session->next_stream_id;
|
|
|
|
session->next_stream_id += 2;
|
|
|
|
|
2013-07-24 16:49:05 +00:00
|
|
|
nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy,
|
2014-05-07 14:24:07 +00:00
|
|
|
stream_id, promised_stream_id,
|
|
|
|
nva_copy, nvlen);
|
|
|
|
|
2014-02-24 15:26:12 +00:00
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, aux_data);
|
2014-05-07 14:24:07 +00:00
|
|
|
|
2013-12-08 12:19:33 +00:00
|
|
|
if(rv != 0) {
|
2013-07-24 16:49:05 +00:00
|
|
|
nghttp2_frame_push_promise_free(&frame->push_promise);
|
2014-02-24 15:26:12 +00:00
|
|
|
free(aux_data);
|
2013-07-24 16:49:05 +00:00
|
|
|
free(frame);
|
2014-05-07 14:24:07 +00:00
|
|
|
|
|
|
|
return rv;
|
2013-07-24 16:49:05 +00:00
|
|
|
}
|
2014-05-07 14:24:07 +00:00
|
|
|
|
|
|
|
return promised_stream_id;
|
2013-07-24 16:49:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 12:45:59 +00:00
|
|
|
int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
int32_t window_size_increment)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2013-08-08 15:58:52 +00:00
|
|
|
int rv;
|
2013-07-12 15:19:03 +00:00
|
|
|
nghttp2_stream *stream;
|
2013-08-18 13:04:09 +00:00
|
|
|
if(window_size_increment == 0) {
|
2013-10-12 08:02:37 +00:00
|
|
|
return 0;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2013-08-18 13:04:09 +00:00
|
|
|
flags = 0;
|
2013-07-15 12:45:59 +00:00
|
|
|
if(stream_id == 0) {
|
2013-08-08 15:58:52 +00:00
|
|
|
rv = nghttp2_adjust_local_window_size(&session->local_window_size,
|
|
|
|
&session->recv_window_size,
|
2013-10-12 08:02:37 +00:00
|
|
|
&session->recv_reduction,
|
|
|
|
&window_size_increment);
|
2013-08-08 15:58:52 +00:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-07-12 15:19:03 +00:00
|
|
|
} else {
|
2013-07-15 12:45:59 +00:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
2013-08-08 15:58:52 +00:00
|
|
|
rv = nghttp2_adjust_local_window_size(&stream->local_window_size,
|
|
|
|
&stream->recv_window_size,
|
2013-10-12 08:02:37 +00:00
|
|
|
&stream->recv_reduction,
|
|
|
|
&window_size_increment);
|
2013-08-08 15:58:52 +00:00
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-07-15 12:45:59 +00:00
|
|
|
} else {
|
2014-01-18 07:19:28 +00:00
|
|
|
return 0;
|
2013-07-15 12:45:59 +00:00
|
|
|
}
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2013-08-09 14:46:12 +00:00
|
|
|
if(window_size_increment > 0) {
|
2013-08-08 15:58:52 +00:00
|
|
|
return nghttp2_session_add_window_update(session, flags, stream_id,
|
|
|
|
window_size_increment);
|
|
|
|
}
|
|
|
|
return 0;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 12:47:51 +00:00
|
|
|
int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
uint32_t max_age, uint16_t port,
|
|
|
|
const uint8_t *protocol_id, size_t protocol_id_len,
|
|
|
|
const uint8_t *host, size_t host_len,
|
|
|
|
const uint8_t *origin, size_t origin_len)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
size_t varlen;
|
|
|
|
uint8_t *var, *varp;
|
|
|
|
nghttp2_frame *frame;
|
2014-06-09 14:16:54 +00:00
|
|
|
nghttp2_ext_altsvc *altsvc;
|
2014-04-01 12:47:51 +00:00
|
|
|
uint8_t *copy_protocol_id, *copy_host, *copy_origin;
|
|
|
|
|
|
|
|
if(!session->server) {
|
2014-04-08 15:13:11 +00:00
|
|
|
return NGHTTP2_ERR_PROTO;
|
2014-04-01 12:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
varlen = protocol_id_len + host_len + origin_len;
|
|
|
|
|
2014-04-01 12:55:29 +00:00
|
|
|
/* 9 = fixed part 8 bytes + HOST_LEN 1 byte */
|
|
|
|
if(varlen + 9 > NGHTTP2_MAX_PAYLOADLEN) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-06-09 14:16:54 +00:00
|
|
|
altsvc = malloc(sizeof(nghttp2_ext_altsvc));
|
|
|
|
|
|
|
|
if(altsvc == NULL) {
|
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:47:51 +00:00
|
|
|
if(varlen == 0) {
|
|
|
|
var = NULL;
|
|
|
|
copy_protocol_id = NULL;
|
|
|
|
copy_host = NULL;
|
|
|
|
copy_origin = NULL;
|
|
|
|
} else {
|
|
|
|
var = malloc(varlen);
|
|
|
|
|
|
|
|
if(var == NULL) {
|
2014-06-09 14:16:54 +00:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
|
|
|
|
|
|
|
goto var_alloc_fail;
|
2014-04-01 12:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
varp = var;
|
|
|
|
|
|
|
|
memcpy(varp, protocol_id, protocol_id_len);
|
|
|
|
|
|
|
|
copy_protocol_id = varp;
|
|
|
|
varp += protocol_id_len;
|
|
|
|
|
|
|
|
memcpy(varp, host, host_len);
|
|
|
|
|
|
|
|
copy_host = varp;
|
|
|
|
varp += host_len;
|
|
|
|
|
|
|
|
memcpy(varp, origin, origin_len);
|
|
|
|
|
|
|
|
copy_origin = varp;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = malloc(sizeof(nghttp2_frame));
|
|
|
|
|
|
|
|
if(frame == NULL) {
|
2014-06-09 14:16:54 +00:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
2014-04-01 12:47:51 +00:00
|
|
|
|
2014-06-09 14:16:54 +00:00
|
|
|
goto frame_alloc_fail;
|
2014-04-01 12:47:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 14:16:54 +00:00
|
|
|
frame->ext.payload = altsvc;
|
|
|
|
|
|
|
|
nghttp2_frame_altsvc_init(&frame->ext, stream_id, max_age, port,
|
2014-04-01 12:47:51 +00:00
|
|
|
copy_protocol_id, protocol_id_len,
|
|
|
|
copy_host, host_len, copy_origin, origin_len);
|
|
|
|
|
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_CTRL, frame, NULL);
|
|
|
|
|
|
|
|
if(rv != 0) {
|
2014-06-09 14:16:54 +00:00
|
|
|
nghttp2_frame_altsvc_free(&frame->ext);
|
2014-04-01 12:47:51 +00:00
|
|
|
free(frame);
|
2014-06-09 14:16:54 +00:00
|
|
|
free(altsvc);
|
2014-04-01 12:47:51 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-06-09 14:16:54 +00:00
|
|
|
|
|
|
|
frame_alloc_fail:
|
|
|
|
free(var);
|
|
|
|
|
|
|
|
var_alloc_fail:
|
|
|
|
free(altsvc);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return rv;
|
2014-04-01 12:47:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 17:04:24 +00:00
|
|
|
static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
|
2013-09-10 15:55:35 +00:00
|
|
|
const nghttp2_data_provider *data_prd)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2013-07-15 12:45:59 +00:00
|
|
|
uint8_t flags = NGHTTP2_FLAG_NONE;
|
2013-07-12 15:19:03 +00:00
|
|
|
if(data_prd == NULL || data_prd->read_callback == NULL) {
|
2013-07-15 12:45:59 +00:00
|
|
|
flags |= NGHTTP2_FLAG_END_STREAM;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2014-03-25 17:04:24 +00:00
|
|
|
|
|
|
|
if(pri_spec) {
|
2014-04-14 14:53:54 +00:00
|
|
|
flags |= NGHTTP2_FLAG_PRIORITY;
|
2013-07-15 12:45:59 +00:00
|
|
|
}
|
2014-03-25 17:04:24 +00:00
|
|
|
|
2013-09-10 15:55:35 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2014-05-07 14:24:07 +00:00
|
|
|
int32_t nghttp2_submit_request(nghttp2_session *session,
|
|
|
|
const nghttp2_priority_spec *pri_spec,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
|
|
|
const nghttp2_data_provider *data_prd,
|
|
|
|
void *stream_user_data)
|
2013-09-10 15:55:35 +00:00
|
|
|
{
|
2014-03-25 17:04:24 +00:00
|
|
|
uint8_t flags;
|
|
|
|
|
2014-04-14 14:53:54 +00:00
|
|
|
if(pri_spec && nghttp2_priority_spec_check_default(pri_spec)) {
|
|
|
|
pri_spec = NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-25 17:04:24 +00:00
|
|
|
flags = set_request_flags(pri_spec, data_prd);
|
|
|
|
|
2014-05-08 14:54:07 +00:00
|
|
|
return submit_headers_shared_nva(session, flags, -1, pri_spec,
|
|
|
|
nva, nvlen,
|
|
|
|
data_prd, stream_user_data);
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 12:35:48 +00:00
|
|
|
static uint8_t set_response_flags(const nghttp2_data_provider *data_prd)
|
2013-07-12 15:19:03 +00:00
|
|
|
{
|
2013-07-15 12:45:59 +00:00
|
|
|
uint8_t flags = NGHTTP2_FLAG_NONE;
|
|
|
|
if(data_prd == NULL || data_prd->read_callback == NULL) {
|
|
|
|
flags |= NGHTTP2_FLAG_END_STREAM;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2013-11-28 12:35:48 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_submit_response(nghttp2_session *session,
|
2013-12-08 12:19:33 +00:00
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen,
|
2013-11-28 12:35:48 +00:00
|
|
|
const nghttp2_data_provider *data_prd)
|
|
|
|
{
|
|
|
|
uint8_t flags = set_response_flags(data_prd);
|
2014-05-08 14:54:07 +00:00
|
|
|
return submit_headers_shared_nva(session, flags, stream_id,
|
|
|
|
NULL, nva, nvlen,
|
|
|
|
data_prd, NULL);
|
2013-11-28 12:35:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 12:45:59 +00:00
|
|
|
int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id,
|
2013-07-12 15:19:03 +00:00
|
|
|
const nghttp2_data_provider *data_prd)
|
|
|
|
{
|
2014-02-18 15:45:20 +00:00
|
|
|
int rv;
|
2014-01-27 12:22:33 +00:00
|
|
|
nghttp2_private_data *data_frame;
|
2014-03-05 15:19:02 +00:00
|
|
|
uint8_t nflags = flags & (NGHTTP2_FLAG_END_STREAM |
|
|
|
|
NGHTTP2_FLAG_END_SEGMENT);
|
2013-08-09 15:02:24 +00:00
|
|
|
|
2014-06-07 09:48:37 +00:00
|
|
|
if(stream_id == 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-01-27 12:22:33 +00:00
|
|
|
data_frame = malloc(sizeof(nghttp2_private_data));
|
2013-07-12 15:19:03 +00:00
|
|
|
if(data_frame == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-01-27 13:13:41 +00:00
|
|
|
nghttp2_frame_private_data_init(data_frame, nflags, stream_id, data_prd);
|
2014-02-18 15:45:20 +00:00
|
|
|
rv = nghttp2_session_add_frame(session, NGHTTP2_CAT_DATA, data_frame, NULL);
|
|
|
|
if(rv != 0) {
|
2014-01-27 13:13:41 +00:00
|
|
|
nghttp2_frame_private_data_free(data_frame);
|
2013-07-12 15:19:03 +00:00
|
|
|
free(data_frame);
|
2014-02-18 15:45:20 +00:00
|
|
|
return rv;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2014-02-18 15:45:20 +00:00
|
|
|
return 0;
|
2013-07-12 15:19:03 +00:00
|
|
|
}
|
2013-08-03 09:05:14 +00:00
|
|
|
|
|
|
|
ssize_t nghttp2_pack_settings_payload(uint8_t *buf,
|
2013-09-08 21:21:06 +00:00
|
|
|
size_t buflen,
|
2013-09-09 12:30:39 +00:00
|
|
|
const nghttp2_settings_entry *iv,
|
|
|
|
size_t niv)
|
2013-08-03 09:05:14 +00:00
|
|
|
{
|
2014-02-05 15:23:20 +00:00
|
|
|
if(!nghttp2_iv_check(iv, niv)) {
|
2013-08-03 09:05:14 +00:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2013-09-08 21:21:06 +00:00
|
|
|
|
2014-02-06 13:06:42 +00:00
|
|
|
if(buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
|
2013-09-08 21:21:06 +00:00
|
|
|
return NGHTTP2_ERR_INSUFF_BUFSIZE;
|
2014-02-06 13:06:42 +00:00
|
|
|
}
|
2013-09-08 21:21:06 +00:00
|
|
|
|
2013-08-03 09:05:14 +00:00
|
|
|
return nghttp2_frame_pack_settings_payload(buf, iv, niv);
|
|
|
|
}
|