2012-01-24 14:02:24 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-01-24 14:02:24 +01:00
|
|
|
*
|
2013-07-15 14:45:59 +02:00
|
|
|
* Copyright (c) 2013 Tatsuhiro Tsujikawa
|
2012-01-24 14:02:24 +01: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.
|
|
|
|
*/
|
2013-07-12 17:19:03 +02:00
|
|
|
#include "nghttp2_frame.h"
|
2012-01-24 14:02:24 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
2012-04-05 18:45:39 +02:00
|
|
|
#include <errno.h>
|
2012-01-24 14:02:24 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
#include "nghttp2_helper.h"
|
|
|
|
#include "nghttp2_net.h"
|
2014-03-25 18:04:24 +01:00
|
|
|
#include "nghttp2_priority_spec.h"
|
2012-01-24 14:02:24 +01:00
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_frame_is_data_frame(uint8_t *head)
|
|
|
|
{
|
|
|
|
return head[2] == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_frame_pack_frame_hd(uint8_t* buf, const nghttp2_frame_hd *hd)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_put_uint16be(&buf[0], hd->length);
|
|
|
|
buf[2]= hd->type;
|
|
|
|
buf[3] = hd->flags;
|
|
|
|
nghttp2_put_uint32be(&buf[4], hd->stream_id);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t* buf)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2013-10-27 13:17:09 +01:00
|
|
|
hd->length = nghttp2_get_uint16(&buf[0]) & NGHTTP2_FRAME_LENGTH_MASK;
|
2013-07-15 14:45:59 +02:00
|
|
|
hd->type = buf[2];
|
|
|
|
hd->flags = buf[3];
|
|
|
|
hd->stream_id = nghttp2_get_uint32(&buf[4]) & NGHTTP2_STREAM_ID_MASK;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
static void nghttp2_frame_set_hd(nghttp2_frame_hd *hd, uint16_t length,
|
|
|
|
uint8_t type, uint8_t flags,
|
|
|
|
int32_t stream_id)
|
2012-04-05 18:45:39 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
hd->length = length;
|
|
|
|
hd->type = type;
|
|
|
|
hd->flags = flags;
|
|
|
|
hd->stream_id = stream_id;
|
2012-04-05 18:45:39 +02:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_headers_init(nghttp2_headers *frame,
|
2014-03-25 18:04:24 +01:00
|
|
|
uint8_t flags, int32_t stream_id,
|
|
|
|
const nghttp2_priority_spec *pri_spec,
|
2013-07-19 17:08:14 +02:00
|
|
|
nghttp2_nv *nva, size_t nvlen)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
|
2014-03-13 15:21:04 +01:00
|
|
|
frame->padlen = 0;
|
2013-07-19 17:08:14 +02:00
|
|
|
frame->nva = nva;
|
|
|
|
frame->nvlen = nvlen;
|
2013-07-25 13:53:30 +02:00
|
|
|
frame->cat = NGHTTP2_HCAT_REQUEST;
|
2014-03-25 18:04:24 +01:00
|
|
|
frame->pri_spec = *pri_spec;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_headers_free(nghttp2_headers *frame)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2013-07-19 17:08:14 +02:00
|
|
|
nghttp2_nv_array_del(frame->nva);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
|
2014-03-25 18:04:24 +01:00
|
|
|
const nghttp2_priority_spec *pri_spec)
|
2012-01-27 11:35:05 +01:00
|
|
|
{
|
2014-03-25 18:04:24 +01:00
|
|
|
uint8_t flags;
|
|
|
|
|
|
|
|
switch(pri_spec->pri_type) {
|
|
|
|
case NGHTTP2_PRIORITY_TYPE_GROUP:
|
|
|
|
flags = NGHTTP2_FLAG_PRIORITY_GROUP;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NGHTTP2_PRIORITY_TYPE_DEP:
|
|
|
|
flags = NGHTTP2_FLAG_PRIORITY_DEPENDENCY;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_PRIORITY, flags, stream_id);
|
|
|
|
frame->pri_spec = *pri_spec;
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_priority_free(nghttp2_priority *frame)
|
2012-01-28 11:22:38 +01:00
|
|
|
{}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame,
|
2013-07-15 14:45:59 +02:00
|
|
|
int32_t stream_id,
|
|
|
|
nghttp2_error_code error_code)
|
2012-01-25 13:31:28 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
|
|
|
|
stream_id);
|
|
|
|
frame->error_code = error_code;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame)
|
2012-01-25 13:31:28 +01:00
|
|
|
{}
|
|
|
|
|
2012-02-24 17:47:37 +01:00
|
|
|
|
2013-10-27 11:22:51 +01:00
|
|
|
void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_settings_entry *iv, size_t niv)
|
2012-01-31 16:26:26 +01:00
|
|
|
{
|
2014-02-06 14:06:42 +01:00
|
|
|
nghttp2_frame_set_hd(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
|
|
|
|
NGHTTP2_SETTINGS, flags, 0);
|
2012-01-31 16:26:26 +01:00
|
|
|
frame->niv = niv;
|
|
|
|
frame->iv = iv;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_settings_free(nghttp2_settings *frame)
|
2012-01-31 16:26:26 +01:00
|
|
|
{
|
|
|
|
free(frame->iv);
|
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame,
|
|
|
|
uint8_t flags, int32_t stream_id,
|
|
|
|
int32_t promised_stream_id,
|
|
|
|
nghttp2_nv *nva, size_t nvlen)
|
|
|
|
{
|
|
|
|
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
|
2014-03-13 15:21:04 +01:00
|
|
|
frame->padlen = 0;
|
2013-07-24 18:49:05 +02:00
|
|
|
frame->nva = nva;
|
|
|
|
frame->nvlen = nvlen;
|
2014-03-13 15:21:04 +01:00
|
|
|
frame->promised_stream_id = promised_stream_id;
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame)
|
|
|
|
{
|
|
|
|
nghttp2_nv_array_del(frame->nva);
|
|
|
|
}
|
2012-04-05 18:45:39 +02:00
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
|
|
|
|
const uint8_t *opaque_data)
|
2012-04-05 18:45:39 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_set_hd(&frame->hd, 8, NGHTTP2_PING, flags, 0);
|
|
|
|
if(opaque_data) {
|
|
|
|
memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
|
2014-03-13 15:21:04 +01:00
|
|
|
} else {
|
|
|
|
memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
|
2012-04-05 18:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_ping_free(nghttp2_ping *frame)
|
2012-01-26 17:17:40 +01:00
|
|
|
{}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
|
|
|
|
nghttp2_error_code error_code,
|
|
|
|
uint8_t *opaque_data, size_t opaque_data_len)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_set_hd(&frame->hd, 8+opaque_data_len, NGHTTP2_GOAWAY,
|
|
|
|
NGHTTP2_FLAG_NONE, 0);
|
|
|
|
frame->last_stream_id = last_stream_id;
|
|
|
|
frame->error_code = error_code;
|
|
|
|
frame->opaque_data = opaque_data;
|
|
|
|
frame->opaque_data_len = opaque_data_len;
|
2012-05-25 06:49:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_goaway_free(nghttp2_goaway *frame)
|
2012-05-25 06:49:18 +02:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
free(frame->opaque_data);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
|
|
|
|
uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
int32_t window_size_increment)
|
2012-01-27 11:35:05 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
|
|
|
|
frame->window_size_increment = window_size_increment;
|
2012-01-27 11:35:05 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
void nghttp2_frame_window_update_free(nghttp2_window_update *frame)
|
|
|
|
{}
|
2012-01-27 11:35:05 +01:00
|
|
|
|
2014-04-01 14:47:51 +02:00
|
|
|
void nghttp2_frame_altsvc_init(nghttp2_altsvc *frame, int32_t stream_id,
|
|
|
|
uint32_t max_age,
|
|
|
|
uint16_t port,
|
|
|
|
uint8_t *protocol_id,
|
|
|
|
size_t protocol_id_len,
|
|
|
|
uint8_t *host, size_t host_len,
|
|
|
|
uint8_t *origin, size_t origin_len)
|
|
|
|
{
|
|
|
|
size_t payloadlen;
|
|
|
|
|
|
|
|
/* 8 for Max-Age, Port, Reserved and PID_LEN. 1 for HOST_LEN. */
|
|
|
|
payloadlen = 8 + protocol_id_len + 1 + host_len + origin_len;
|
|
|
|
|
|
|
|
nghttp2_frame_set_hd(&frame->hd, payloadlen, NGHTTP2_ALTSVC,
|
|
|
|
NGHTTP2_FLAG_NONE, stream_id);
|
|
|
|
|
|
|
|
frame->max_age = max_age;
|
|
|
|
frame->port = port;
|
|
|
|
frame->protocol_id = protocol_id;
|
|
|
|
frame->protocol_id_len = protocol_id_len;
|
|
|
|
frame->host = host;
|
|
|
|
frame->host_len = host_len;
|
|
|
|
frame->origin = origin;
|
|
|
|
frame->origin_len = origin_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_frame_altsvc_free(nghttp2_altsvc *frame)
|
|
|
|
{
|
|
|
|
free(frame->protocol_id);
|
|
|
|
}
|
|
|
|
|
2014-01-27 15:28:45 +01:00
|
|
|
void nghttp2_frame_data_init(nghttp2_data *frame, nghttp2_private_data *pdata)
|
|
|
|
{
|
|
|
|
frame->hd = pdata->hd;
|
2014-02-07 15:22:17 +01:00
|
|
|
frame->padlen = pdata->padlen;
|
2014-03-05 16:19:02 +01:00
|
|
|
/* flags may have NGHTTP2_FLAG_END_STREAM or
|
|
|
|
NGHTTP2_FLAG_END_SEGMENT even if the sent chunk is not the end of
|
|
|
|
the stream */
|
2014-01-27 15:28:45 +01:00
|
|
|
if(!pdata->eof) {
|
2014-03-05 16:19:02 +01:00
|
|
|
frame->hd.flags &= ~(NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_SEGMENT);
|
2014-01-27 15:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen)
|
2014-02-07 15:22:17 +01:00
|
|
|
{
|
2014-02-08 16:35:21 +01:00
|
|
|
return padlen
|
2014-02-07 15:22:17 +01:00
|
|
|
- ((frame->hd.flags & NGHTTP2_FLAG_PAD_HIGH) > 0)
|
|
|
|
- ((frame->hd.flags & NGHTTP2_FLAG_PAD_LOW) > 0);
|
|
|
|
}
|
|
|
|
|
2014-01-27 14:13:41 +01:00
|
|
|
void nghttp2_frame_private_data_init(nghttp2_private_data *frame,
|
|
|
|
uint8_t flags,
|
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_data_provider *data_prd)
|
2012-01-28 11:22:38 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
/* At this moment, the length of DATA frame is unknown */
|
|
|
|
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
|
|
|
|
frame->data_prd = *data_prd;
|
2014-03-13 15:21:04 +01:00
|
|
|
frame->padlen = 0;
|
|
|
|
frame->eof = 0;
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 14:13:41 +01:00
|
|
|
void nghttp2_frame_private_data_free(nghttp2_private_data *frame)
|
2013-07-15 14:45:59 +02:00
|
|
|
{}
|
2012-01-28 11:22:38 +01:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
size_t nghttp2_frame_priority_len(uint8_t flags)
|
2014-01-16 15:41:13 +01:00
|
|
|
{
|
2014-03-25 18:04:24 +01:00
|
|
|
if(flags & NGHTTP2_FLAG_PRIORITY_GROUP) {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flags & NGHTTP2_FLAG_PRIORITY_DEPENDENCY) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 4;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame)
|
|
|
|
{
|
|
|
|
return nghttp2_frame_priority_len(frame->hd.flags);
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
/*
|
|
|
|
* Call this function after payload was serialized, but not before
|
|
|
|
* changing buf->pos and serializing frame header.
|
|
|
|
*
|
|
|
|
* This function assumes bufs->cur points to the last buf chain of the
|
|
|
|
* frame(s).
|
|
|
|
*
|
|
|
|
* This function serializes frame header for HEADERS/PUSH_PROMISE and
|
|
|
|
* handles their successive CONTINUATION frames.
|
|
|
|
*
|
|
|
|
* We don't process any padding here.
|
|
|
|
*/
|
|
|
|
static int frame_pack_headers_shared(nghttp2_bufs *bufs,
|
|
|
|
nghttp2_frame_hd *frame_hd)
|
2012-01-27 10:21:14 +01:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
|
|
|
nghttp2_buf_chain *ci, *ce;
|
|
|
|
nghttp2_frame_hd hd;
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
hd = *frame_hd;
|
|
|
|
hd.length = nghttp2_buf_len(buf);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length));
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
/* We have multiple frame buffers, which means one or more
|
|
|
|
CONTINUATION frame is involved. Remove END_HEADERS flag from the
|
|
|
|
first frame. */
|
|
|
|
if(bufs->head != bufs->cur) {
|
|
|
|
hd.flags &= ~NGHTTP2_FLAG_END_HEADERS;
|
2013-07-19 17:08:14 +02:00
|
|
|
}
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
if(bufs->head != bufs->cur) {
|
|
|
|
/* 2nd and later frames are CONTINUATION frames. */
|
|
|
|
hd.type = NGHTTP2_CONTINUATION;
|
|
|
|
/* We don't have no flags except for last CONTINUATION */
|
|
|
|
hd.flags = NGHTTP2_FLAG_NONE;
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
ce = bufs->cur;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
for(ci = bufs->head->next; ci != ce; ci = ci->next) {
|
|
|
|
buf = &ci->buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
hd.length = nghttp2_buf_len(buf);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"send: int CONTINUATION, payloadlen=%zu\n", hd.length));
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &ci->buf;
|
|
|
|
hd.length = nghttp2_buf_len(buf);
|
|
|
|
/* Set END_HEADERS flag for last CONTINUATION */
|
|
|
|
hd.flags = NGHTTP2_FLAG_END_HEADERS;
|
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"send: last CONTINUATION, payloadlen=%zu\n", hd.length));
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_frame_pack_headers(nghttp2_bufs *bufs,
|
|
|
|
nghttp2_headers *frame,
|
|
|
|
nghttp2_hd_deflater *deflater)
|
|
|
|
{
|
|
|
|
size_t nv_offset;
|
|
|
|
ssize_t rv;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
|
|
|
|
assert(bufs->head == bufs->cur);
|
|
|
|
|
|
|
|
nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
|
|
|
|
|
|
|
|
buf = &bufs->cur->buf;
|
|
|
|
|
|
|
|
buf->pos += nv_offset;
|
|
|
|
buf->last = buf->pos;
|
|
|
|
|
|
|
|
/* This call will adjust buf->last to the correct position */
|
|
|
|
rv = nghttp2_hd_deflate_hd(deflater, bufs, frame->nva, frame->nvlen);
|
|
|
|
|
|
|
|
buf->pos -= nv_offset;
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
frame->padlen = 0;
|
|
|
|
frame->hd.length = nghttp2_bufs_len(bufs);
|
|
|
|
|
|
|
|
return frame_pack_headers_shared(bufs, &frame->hd);
|
2012-01-27 10:21:14 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
void nghttp2_frame_pack_priority_spec(uint8_t *buf,
|
|
|
|
const nghttp2_priority_spec *pri_spec)
|
|
|
|
{
|
|
|
|
switch(pri_spec->pri_type) {
|
|
|
|
case NGHTTP2_PRIORITY_TYPE_GROUP:
|
|
|
|
|
2014-04-05 11:40:44 +02:00
|
|
|
nghttp2_put_uint32be(buf, pri_spec->spec.group.pri_group_id);
|
|
|
|
buf[4] = pri_spec->spec.group.weight - 1;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
case NGHTTP2_PRIORITY_TYPE_DEP:
|
|
|
|
|
2014-04-05 11:40:44 +02:00
|
|
|
nghttp2_put_uint32be(buf, pri_spec->spec.dep.stream_id);
|
|
|
|
if(pri_spec->spec.dep.exclusive) {
|
2014-03-25 18:04:24 +01:00
|
|
|
buf[0] |= 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
|
|
|
|
uint8_t flags,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
|
|
|
if(flags & NGHTTP2_FLAG_PRIORITY_GROUP) {
|
|
|
|
int32_t pri_group_id;
|
2014-04-01 15:54:20 +02:00
|
|
|
int32_t weight;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
pri_group_id = nghttp2_get_uint32(payload) & NGHTTP2_PRI_GROUP_ID_MASK;
|
2014-04-01 15:54:20 +02:00
|
|
|
weight = payload[4] + 1;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
nghttp2_priority_spec_group_init(pri_spec, pri_group_id, weight);
|
|
|
|
} else if(flags & NGHTTP2_FLAG_PRIORITY_DEPENDENCY) {
|
|
|
|
int32_t dep_stream_id;
|
|
|
|
uint8_t exclusive;
|
|
|
|
|
|
|
|
dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
|
|
|
|
exclusive = (payload[0] & 0x80) > 0;
|
|
|
|
|
|
|
|
nghttp2_priority_spec_dep_init(pri_spec, dep_stream_id, exclusive);
|
|
|
|
} else {
|
|
|
|
pri_spec->pri_type = NGHTTP2_PRIORITY_TYPE_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags,
|
|
|
|
payload, payloadlen);
|
2014-01-26 07:44:43 +01:00
|
|
|
frame->nva = NULL;
|
|
|
|
frame->nvlen = 0;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame)
|
2012-01-25 13:31:28 +01:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
|
|
|
|
|
|
|
|
buf->last += nghttp2_frame_priority_len(frame->hd.flags);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags,
|
|
|
|
payload, payloadlen);
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2012-01-31 16:26:26 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
|
|
|
|
nghttp2_rst_stream *frame)
|
2012-02-24 17:47:37 +01:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
|
|
|
nghttp2_put_uint32be(buf->last, frame->error_code);
|
|
|
|
buf->last += 4;
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2012-02-24 17:47:37 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
|
|
|
frame->error_code = nghttp2_get_uint32(payload);
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame)
|
2012-01-31 16:26:26 +01:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
|
|
|
|
|
|
|
if(nghttp2_buf_avail(buf) < (ssize_t)frame->hd.length) {
|
|
|
|
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
|
|
|
buf->last += nghttp2_frame_pack_settings_payload(buf->last,
|
|
|
|
frame->iv, frame->niv);
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:05:14 +02:00
|
|
|
size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
|
2013-09-09 14:30:39 +02:00
|
|
|
const nghttp2_settings_entry *iv,
|
2013-08-03 11:05:14 +02:00
|
|
|
size_t niv)
|
|
|
|
{
|
|
|
|
size_t i;
|
2014-02-06 14:06:42 +01:00
|
|
|
for(i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
|
|
|
|
buf[0] = iv[i].settings_id;
|
|
|
|
nghttp2_put_uint32be(buf + 1, iv[i].value);
|
2013-08-03 11:05:14 +02:00
|
|
|
}
|
2014-02-06 14:06:42 +01:00
|
|
|
return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
|
2013-08-03 11:05:14 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 08:46:18 +01:00
|
|
|
int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
|
|
|
|
nghttp2_settings_entry *iv,
|
|
|
|
size_t niv)
|
2014-01-26 07:44:43 +01:00
|
|
|
{
|
|
|
|
size_t payloadlen = niv * sizeof(nghttp2_settings_entry);
|
|
|
|
|
|
|
|
frame->iv = malloc(payloadlen);
|
|
|
|
if(frame->iv == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
memcpy(frame->iv, iv, payloadlen);
|
|
|
|
frame->niv = niv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
|
|
|
|
const uint8_t *payload)
|
|
|
|
{
|
2014-02-06 14:06:42 +01:00
|
|
|
iv->settings_id = payload[0];
|
|
|
|
iv->value = nghttp2_get_uint32(&payload[1]);
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 08:46:18 +01:00
|
|
|
int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
|
|
|
|
size_t *niv_ptr,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
2013-08-03 11:05:14 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
2014-02-06 14:06:42 +01:00
|
|
|
*niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
|
2013-08-03 11:05:14 +02:00
|
|
|
*iv_ptr = malloc((*niv_ptr)*sizeof(nghttp2_settings_entry));
|
|
|
|
if(*iv_ptr == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
2013-08-03 11:05:14 +02:00
|
|
|
for(i = 0; i < *niv_ptr; ++i) {
|
2014-02-06 14:06:42 +01:00
|
|
|
size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
|
|
|
|
nghttp2_push_promise *frame,
|
|
|
|
nghttp2_hd_deflater *deflater)
|
2013-07-24 18:49:05 +02:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
size_t nv_offset = 4;
|
2013-07-24 18:49:05 +02:00
|
|
|
ssize_t rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-02-15 08:30:43 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->cur->buf;
|
2014-02-15 08:30:43 +01:00
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
buf->pos += nv_offset;
|
|
|
|
buf->last = buf->pos;
|
|
|
|
|
|
|
|
/* This call will adjust buf->last to the correct position */
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_hd_deflate_hd(deflater, bufs, frame->nva, frame->nvlen);
|
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
buf->pos -= nv_offset;
|
2014-02-15 08:30:43 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
if(rv != 0) {
|
2013-07-24 18:49:05 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_put_uint32be(buf->pos, frame->promised_stream_id);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
frame->padlen = 0;
|
|
|
|
frame->hd.length = nghttp2_bufs_len(bufs);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return frame_pack_headers_shared(bufs, &frame->hd);
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
|
|
|
frame->promised_stream_id = nghttp2_get_uint32(payload) &
|
|
|
|
NGHTTP2_STREAM_ID_MASK;
|
|
|
|
frame->nva = NULL;
|
|
|
|
frame->nvlen = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame)
|
2012-04-05 18:45:39 +02:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->last = nghttp2_cpymem(buf->last, frame->opaque_data,
|
|
|
|
sizeof(frame->opaque_data));
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2012-04-05 18:45:39 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
|
|
|
memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
|
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame)
|
2012-04-05 18:45:39 +02:00
|
|
|
{
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
|
|
|
nghttp2_put_uint32be(buf->last, frame->last_stream_id);
|
|
|
|
buf->last += 4;
|
|
|
|
|
|
|
|
nghttp2_put_uint32be(buf->last, frame->error_code);
|
|
|
|
buf->last += 4;
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2013-07-15 14:45:59 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
|
|
|
|
const uint8_t *payload,
|
2014-03-22 10:59:59 +01:00
|
|
|
size_t payloadlen,
|
|
|
|
uint8_t *var_gift_payload,
|
|
|
|
size_t var_gift_payloadlen)
|
2014-01-26 07:44:43 +01:00
|
|
|
{
|
|
|
|
frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
|
|
|
|
frame->error_code = nghttp2_get_uint32(payload+4);
|
2014-03-22 10:59:59 +01:00
|
|
|
|
|
|
|
frame->opaque_data = var_gift_payload;
|
|
|
|
frame->opaque_data_len = var_gift_payloadlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
|
|
|
uint8_t *var_gift_payload;
|
|
|
|
size_t var_gift_payloadlen;
|
|
|
|
|
|
|
|
if(payloadlen > 8) {
|
|
|
|
var_gift_payloadlen = payloadlen - 8;
|
|
|
|
} else {
|
|
|
|
var_gift_payloadlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
payloadlen -= var_gift_payloadlen;
|
|
|
|
|
|
|
|
var_gift_payload = malloc(var_gift_payloadlen);
|
|
|
|
|
|
|
|
if(var_gift_payload == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
|
|
|
|
|
|
|
|
nghttp2_frame_unpack_goaway_payload(frame, payload, payloadlen,
|
|
|
|
var_gift_payload, var_gift_payloadlen);
|
|
|
|
|
|
|
|
return 0;
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
|
|
|
|
nghttp2_window_update *frame)
|
2012-04-05 18:45:39 +02:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->head->buf;
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
|
|
|
nghttp2_put_uint32be(buf->last, frame->window_size_increment);
|
|
|
|
buf->last += 4;
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2013-07-15 14:45:59 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen)
|
|
|
|
{
|
|
|
|
frame->window_size_increment = nghttp2_get_uint32(payload) &
|
|
|
|
NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
|
|
|
|
}
|
|
|
|
|
2014-04-01 14:47:51 +02:00
|
|
|
int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_altsvc *frame)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
|
|
|
|
assert(bufs->head == bufs->cur);
|
|
|
|
|
|
|
|
buf = &bufs->head->buf;
|
|
|
|
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
|
|
|
|
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
|
|
|
|
|
|
|
nghttp2_put_uint32be(buf->last, frame->max_age);
|
|
|
|
buf->last += 4;
|
|
|
|
|
|
|
|
nghttp2_put_uint16be(buf->last, frame->port);
|
|
|
|
buf->last += 2;
|
|
|
|
|
|
|
|
/* Reserved */
|
|
|
|
buf->last[0] = 0;
|
|
|
|
++buf->last;
|
|
|
|
|
|
|
|
buf->last[0] = frame->protocol_id_len;
|
|
|
|
++buf->last;
|
|
|
|
|
|
|
|
rv = nghttp2_bufs_add(bufs, frame->protocol_id, frame->protocol_id_len);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_bufs_addb(bufs, frame->host_len);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_bufs_add(bufs, frame->host, frame->host_len);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_bufs_add(bufs, frame->origin, frame->origin_len);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_frame_unpack_altsvc_payload(nghttp2_altsvc *frame,
|
|
|
|
const uint8_t *payload,
|
|
|
|
size_t payloadlen,
|
|
|
|
uint8_t *var_gift_payload,
|
|
|
|
size_t var_gift_payloadlen)
|
|
|
|
{
|
|
|
|
nghttp2_buf buf;
|
|
|
|
|
|
|
|
frame->max_age = nghttp2_get_uint32(payload);
|
|
|
|
payload += 4;
|
|
|
|
|
|
|
|
frame->port = nghttp2_get_uint16(payload);
|
|
|
|
payload += 2;
|
|
|
|
|
|
|
|
/* Skip Reserved */
|
|
|
|
++payload;
|
|
|
|
|
|
|
|
frame->protocol_id_len = *payload;
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&buf, var_gift_payload, var_gift_payloadlen);
|
|
|
|
buf.last += var_gift_payloadlen;
|
|
|
|
|
|
|
|
/* 1 for HOST_LEN */
|
|
|
|
if(nghttp2_buf_len(&buf) < 1 + (ssize_t)frame->protocol_id_len) {
|
|
|
|
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->protocol_id = buf.pos;
|
|
|
|
buf.pos += frame->protocol_id_len;
|
|
|
|
|
|
|
|
frame->host_len = *buf.pos;
|
|
|
|
++buf.pos;
|
|
|
|
|
|
|
|
if(nghttp2_buf_len(&buf) < (ssize_t)frame->host_len) {
|
|
|
|
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->host = buf.pos;
|
|
|
|
buf.pos += frame->host_len;
|
|
|
|
|
|
|
|
frame->origin = buf.pos;
|
|
|
|
frame->origin_len = nghttp2_buf_len(&buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_settings_entry* nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
|
2012-01-31 16:26:26 +01:00
|
|
|
size_t niv)
|
|
|
|
{
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_settings_entry *iv_copy;
|
|
|
|
size_t len = niv*sizeof(nghttp2_settings_entry);
|
2012-01-31 16:26:26 +01:00
|
|
|
iv_copy = malloc(len);
|
|
|
|
if(iv_copy == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(iv_copy, iv, len);
|
|
|
|
return iv_copy;
|
|
|
|
}
|
2012-03-10 10:41:01 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b)
|
|
|
|
{
|
|
|
|
return a->namelen == b->namelen && a->valuelen == b->valuelen &&
|
|
|
|
memcmp(a->name, b->name, a->namelen) == 0 &&
|
|
|
|
memcmp(a->value, b->value, a->valuelen) == 0;
|
|
|
|
}
|
|
|
|
|
2013-07-19 17:08:14 +02:00
|
|
|
void nghttp2_nv_array_del(nghttp2_nv *nva)
|
2013-07-19 09:50:31 +02:00
|
|
|
{
|
|
|
|
free(nva);
|
|
|
|
}
|
2013-07-19 17:08:14 +02:00
|
|
|
|
2013-11-13 15:56:02 +01:00
|
|
|
static int bytes_compar(const uint8_t *a, size_t alen,
|
|
|
|
const uint8_t *b, size_t blen)
|
2013-07-19 17:08:14 +02:00
|
|
|
{
|
2013-11-13 15:56:02 +01:00
|
|
|
if(alen == blen) {
|
|
|
|
return memcmp(a, b, alen);
|
|
|
|
} else if(alen < blen) {
|
|
|
|
int rv = memcmp(a, b, alen);
|
2013-07-19 17:08:14 +02:00
|
|
|
if(rv == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-13 15:56:02 +01:00
|
|
|
int rv = memcmp(a, b, blen);
|
2013-07-19 17:08:14 +02:00
|
|
|
if(rv == 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:09:46 +02:00
|
|
|
int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs)
|
|
|
|
{
|
2013-11-13 15:56:02 +01:00
|
|
|
return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nv_compar(const void *lhs, const void *rhs)
|
|
|
|
{
|
|
|
|
const nghttp2_nv *a = (const nghttp2_nv*)lhs;
|
|
|
|
const nghttp2_nv *b = (const nghttp2_nv*)rhs;
|
|
|
|
int rv;
|
|
|
|
rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
|
|
|
|
if(rv == 0) {
|
|
|
|
return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
|
|
|
|
}
|
|
|
|
return rv;
|
2013-08-27 17:09:46 +02:00
|
|
|
}
|
|
|
|
|
2013-07-19 17:08:14 +02:00
|
|
|
void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen)
|
|
|
|
{
|
2013-11-13 15:56:02 +01:00
|
|
|
qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
|
2013-07-19 17:08:14 +02:00
|
|
|
}
|
|
|
|
|
2013-09-10 17:55:35 +02:00
|
|
|
ssize_t nghttp2_nv_array_copy(nghttp2_nv **nva_ptr,
|
|
|
|
const nghttp2_nv *nva, size_t nvlen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
uint8_t *data;
|
|
|
|
size_t buflen = 0;
|
|
|
|
nghttp2_nv *p;
|
|
|
|
for(i = 0; i < nvlen; ++i) {
|
|
|
|
buflen += nva[i].namelen + nva[i].valuelen;
|
|
|
|
}
|
|
|
|
/* If all name/value pair is 0-length, remove them */
|
|
|
|
if(nvlen == 0 || buflen == 0) {
|
|
|
|
*nva_ptr = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buflen += sizeof(nghttp2_nv)*nvlen;
|
|
|
|
*nva_ptr = malloc(buflen);
|
|
|
|
if(*nva_ptr == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
p = *nva_ptr;
|
|
|
|
data = (uint8_t*)(*nva_ptr) + sizeof(nghttp2_nv)*nvlen;
|
|
|
|
|
|
|
|
for(i = 0; i < nvlen; ++i) {
|
2014-04-01 16:17:50 +02:00
|
|
|
p->flags = nva[i].flags;
|
|
|
|
|
2013-09-10 17:55:35 +02:00
|
|
|
memcpy(data, nva[i].name, nva[i].namelen);
|
|
|
|
p->name = data;
|
|
|
|
p->namelen = nva[i].namelen;
|
|
|
|
nghttp2_downcase(p->name, p->namelen);
|
|
|
|
data += nva[i].namelen;
|
|
|
|
memcpy(data, nva[i].value, nva[i].valuelen);
|
|
|
|
p->value = data;
|
|
|
|
p->valuelen = nva[i].valuelen;
|
|
|
|
data += nva[i].valuelen;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return nvlen;
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:23:20 +01:00
|
|
|
int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv)
|
2013-08-03 11:05:14 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < niv; ++i) {
|
2013-08-17 16:41:04 +02:00
|
|
|
switch(iv[i].settings_id) {
|
2014-02-06 13:38:16 +01:00
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
|
|
|
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
|
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_ENABLE_PUSH:
|
|
|
|
if(iv[i].value != 0 && iv[i].value != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-17 16:41:04 +02:00
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
|
|
|
if(iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2014-02-11 08:53:08 +01:00
|
|
|
default:
|
|
|
|
return 0;
|
2013-08-03 11:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2014-02-08 09:46:21 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
static void frame_set_pad(nghttp2_buf *buf, size_t padlen)
|
2014-02-08 09:46:21 +01:00
|
|
|
{
|
2014-03-13 14:11:02 +01:00
|
|
|
size_t trail_padlen;
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
if(padlen > 256) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: padlen=%zu, shift left 2 bytes\n", padlen));
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
memmove(buf->pos - 2, buf->pos, NGHTTP2_FRAME_HDLEN);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos -= 2;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos[3] |= NGHTTP2_FLAG_PAD_HIGH | NGHTTP2_FLAG_PAD_LOW;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_put_uint16be(buf->pos, nghttp2_get_uint16(buf->pos) + padlen);
|
|
|
|
|
|
|
|
trail_padlen = padlen - 2;
|
|
|
|
buf->pos[NGHTTP2_FRAME_HDLEN] = trail_padlen >> 8;
|
|
|
|
buf->pos[NGHTTP2_FRAME_HDLEN + 1] = trail_padlen & 0xff;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-02-08 09:46:21 +01:00
|
|
|
} else if(padlen > 0) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: padlen=%zu, shift left 1 bytes\n", padlen));
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
|
|
|
|
|
|
|
|
--buf->pos;
|
|
|
|
|
|
|
|
buf->pos[3] |= NGHTTP2_FLAG_PAD_LOW;
|
|
|
|
|
|
|
|
nghttp2_put_uint16be(buf->pos, nghttp2_get_uint16(buf->pos) + padlen);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-02-08 09:46:21 +01:00
|
|
|
trail_padlen = padlen - 1;
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos[NGHTTP2_FRAME_HDLEN] = trail_padlen;
|
|
|
|
|
|
|
|
} else {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: padlen=0, no shift left was made\n"));
|
2014-02-08 09:46:21 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return;
|
2014-02-08 09:46:21 +01:00
|
|
|
}
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
/* zero out padding */
|
|
|
|
memset(buf->last, 0, trail_padlen);
|
|
|
|
/* extend buffers trail_padlen bytes, since we ate previous padlen -
|
|
|
|
trail_padlen byte(s) */
|
|
|
|
buf->last += trail_padlen;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
|
|
|
|
size_t padlen, nghttp2_frame_type type)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
size_t trail_padlen;
|
|
|
|
size_t last_avail;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
nghttp2_frame_hd last_hd;
|
|
|
|
|
|
|
|
if(padlen == 0) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: padlen = 0, nothing to do\n"));
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have arranged bufs like this:
|
|
|
|
*
|
|
|
|
* 0 1 2 3
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | | |Frame header | Frame payload... |
|
|
|
|
* +-+-+---------------+-------------------------------------------+
|
|
|
|
* | | |Frame header | Frame payload... |
|
|
|
|
* +-+-+---------------+-------------------------------------------+
|
|
|
|
* | | |Frame header | Frame payload... |
|
|
|
|
* +-+-+---------------+-------------------------------------------+
|
|
|
|
*
|
|
|
|
* Since we limit the length of the padding bytes, they are
|
|
|
|
* completely included in one frame payload or across 2 frames. We
|
|
|
|
* are going to adjust buf->pos of frame which includes part of
|
|
|
|
* padding. And serialize (memmove) frame header in the correct
|
|
|
|
* position. Also extends buf->last to include padding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nghttp2_bufs_seek_last_present(bufs);
|
|
|
|
|
|
|
|
buf = &bufs->cur->buf;
|
|
|
|
|
|
|
|
last_avail = nghttp2_buf_avail(buf);
|
|
|
|
|
|
|
|
if(last_avail >= padlen) {
|
|
|
|
/* Last frame can include all paddings bytes */
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: last frame includes all paddings\n"));
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
frame_set_pad(buf, padlen);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* padding across 2 frames */
|
|
|
|
|
|
|
|
/* type = DATA must not be here */
|
|
|
|
assert(type == NGHTTP2_CONTINUATION);
|
|
|
|
|
|
|
|
/* This will seek to the last chain */
|
|
|
|
rv = nghttp2_bufs_advance(bufs);
|
|
|
|
if(rv == NGHTTP2_ERR_BUFFER_ERROR) {
|
|
|
|
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(type == NGHTTP2_CONTINUATION) {
|
|
|
|
/* former last frame has END_HEADERS flag set. Clear it. */
|
|
|
|
buf->pos[3] &= ~NGHTTP2_FLAG_END_HEADERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
trail_padlen = nghttp2_buf_avail(buf);
|
|
|
|
|
|
|
|
/* former last frame may have zero buffer available */
|
|
|
|
if(trail_padlen == 0) {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr,
|
|
|
|
"send: last frame has no space to include padding\n"));
|
2014-03-13 14:11:02 +01:00
|
|
|
} else {
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: padding across 2 frames\n"));
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
frame_set_pad(buf, trail_padlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This buffer does not have frame header serialized */
|
|
|
|
buf = &bufs->cur->buf;
|
|
|
|
|
|
|
|
trail_padlen = padlen - trail_padlen;
|
|
|
|
|
|
|
|
last_hd.length = 0;
|
|
|
|
last_hd.type = type;
|
|
|
|
last_hd.stream_id = hd->stream_id;
|
|
|
|
|
|
|
|
if(type == NGHTTP2_CONTINUATION) {
|
|
|
|
last_hd.flags = NGHTTP2_FLAG_END_HEADERS;
|
|
|
|
} else {
|
|
|
|
last_hd.flags = NGHTTP2_FLAG_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &last_hd);
|
|
|
|
|
|
|
|
frame_set_pad(buf, trail_padlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
hd->length += padlen;
|
|
|
|
|
2014-03-19 16:27:39 +01:00
|
|
|
DEBUGF(fprintf(stderr, "send: final payloadlen=%zu, padlen=%zu\n",
|
2014-03-13 14:11:02 +01:00
|
|
|
hd->length, padlen));
|
|
|
|
|
|
|
|
return 0;
|
2014-02-08 09:46:21 +01:00
|
|
|
}
|