nghttp2/lib/nghttp2_frame.c

992 lines
28 KiB
C
Raw Normal View History

/*
2014-03-30 12:09:21 +02:00
* nghttp2 - HTTP/2 C Library
*
2013-07-15 14:45:59 +02:00
* Copyright (c) 2013 Tatsuhiro Tsujikawa
*
* 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"
#include <string.h>
#include <assert.h>
#include <stdio.h>
2012-04-05 18:45:39 +02:00
#include <errno.h>
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"
2014-11-27 15:39:04 +01:00
void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
2014-07-09 15:40:31 +02:00
nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
2014-11-27 15:39:04 +01:00
buf[3] = hd->type;
2014-07-09 15:40:31 +02:00
buf[4] = hd->flags;
2015-09-23 07:41:53 +02:00
nghttp2_put_uint32be(&buf[5], (uint32_t)hd->stream_id);
/* ignore hd->reserved for now */
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
2014-07-09 15:40:31 +02:00
hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
hd->type = buf[3];
hd->flags = buf[4];
hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
hd->reserved = 0;
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
uint8_t flags, int32_t stream_id) {
2013-07-15 14:45:59 +02:00
hd->length = length;
hd->type = type;
hd->flags = flags;
hd->stream_id = stream_id;
hd->reserved = 0;
2012-04-05 18:45:39 +02:00
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
int32_t stream_id, nghttp2_headers_category cat,
2014-03-25 18:04:24 +01:00
const nghttp2_priority_spec *pri_spec,
2014-11-27 15:39:04 +01:00
nghttp2_nv *nva, size_t nvlen) {
nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
frame->padlen = 0;
2013-07-19 17:08:14 +02:00
frame->nva = nva;
frame->nvlen = nvlen;
frame->cat = cat;
2014-11-27 15:39:04 +01:00
if (pri_spec) {
frame->pri_spec = *pri_spec;
} else {
nghttp2_priority_spec_default_init(&frame->pri_spec);
}
}
void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
nghttp2_nv_array_del(frame->nva, mem);
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
2014-11-27 15:39:04 +01:00
const nghttp2_priority_spec *pri_spec) {
nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
NGHTTP2_FLAG_NONE, stream_id);
2014-03-25 18:04:24 +01:00
frame->pri_spec = *pri_spec;
2012-01-28 11:22:38 +01:00
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_priority_free(nghttp2_priority *frame _U_) {}
2012-01-28 11:22:38 +01:00
2014-11-27 15:39:04 +01:00
void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
uint32_t error_code) {
nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
stream_id);
2013-07-15 14:45:59 +02:00
frame->error_code = error_code;
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame _U_) {}
void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
2014-11-27 15:39:04 +01:00
nghttp2_settings_entry *iv, size_t niv) {
nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
NGHTTP2_SETTINGS, flags, 0);
frame->niv = niv;
frame->iv = iv;
}
void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
nghttp2_mem_free(mem, frame->iv);
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
int32_t stream_id,
2013-07-24 18:49:05 +02:00
int32_t promised_stream_id,
2014-11-27 15:39:04 +01:00
nghttp2_nv *nva, size_t nvlen) {
nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
frame->padlen = 0;
2013-07-24 18:49:05 +02:00
frame->nva = nva;
frame->nvlen = nvlen;
frame->promised_stream_id = promised_stream_id;
frame->reserved = 0;
2013-07-24 18:49:05 +02:00
}
void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
nghttp2_mem *mem) {
nghttp2_nv_array_del(frame->nva, mem);
2013-07-24 18:49:05 +02:00
}
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,
2014-11-27 15:39:04 +01:00
const uint8_t *opaque_data) {
nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
2014-11-27 15:39:04 +01:00
if (opaque_data) {
2013-07-15 14:45:59 +02:00
memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
} else {
memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
2012-04-05 18:45:39 +02:00
}
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_ping_free(nghttp2_ping *frame _U_) {}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
2014-11-27 15:39:04 +01:00
uint32_t error_code, uint8_t *opaque_data,
size_t opaque_data_len) {
nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
NGHTTP2_FLAG_NONE, 0);
2013-07-15 14:45:59 +02:00
frame->last_stream_id = last_stream_id;
frame->error_code = error_code;
frame->opaque_data = opaque_data;
frame->opaque_data_len = opaque_data_len;
frame->reserved = 0;
}
void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
nghttp2_mem_free(mem, frame->opaque_data);
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
2014-11-27 15:39:04 +01:00
uint8_t flags, int32_t stream_id,
int32_t window_size_increment) {
nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
2013-07-15 14:45:59 +02:00
frame->window_size_increment = window_size_increment;
frame->reserved = 0;
2012-01-27 11:35:05 +01:00
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_window_update_free(nghttp2_window_update *frame _U_) {}
2012-01-27 11:35:05 +01:00
2014-11-27 15:39:04 +01:00
size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
/* We have iframe->padlen == 0, but iframe->frame.hd.flags may have
NGHTTP2_FLAG_PADDED set. This happens when receiving
CONTINUATION frame, since we don't reset flags after HEADERS was
received. */
if (padlen == 0) {
return 0;
}
return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
2014-02-07 15:22:17 +01:00
}
void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
2014-11-27 15:39:04 +01:00
int32_t stream_id) {
2013-07-15 14:45:59 +02:00
/* At this moment, the length of DATA frame is unknown */
nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
frame->padlen = 0;
2012-01-28 11:22:38 +01:00
}
2014-11-27 15:39:04 +01:00
void nghttp2_frame_data_free(nghttp2_data *frame _U_) {}
2012-01-28 11:22:38 +01:00
void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
uint8_t flags, int32_t stream_id,
void *payload) {
nghttp2_frame_hd_init(&frame->hd, 0, type, flags, stream_id);
frame->payload = payload;
}
void nghttp2_frame_extension_free(nghttp2_extension *frame _U_) {}
2016-03-29 16:29:26 +02:00
void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
uint8_t *origin, size_t origin_len,
uint8_t *field_value, size_t field_value_len) {
nghttp2_ext_altsvc *altsvc;
nghttp2_frame_hd_init(&frame->hd, 2 + origin_len + field_value_len,
NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, stream_id);
altsvc = frame->payload;
altsvc->origin = origin;
altsvc->origin_len = origin_len;
altsvc->field_value = field_value;
altsvc->field_value_len = field_value_len;
}
void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem) {
nghttp2_ext_altsvc *altsvc;
altsvc = frame->payload;
/* We use the same buffer for altsvc->origin and
altsvc->field_value. */
nghttp2_mem_free(mem, altsvc->origin);
}
2014-11-27 15:39:04 +01:00
size_t nghttp2_frame_priority_len(uint8_t flags) {
if (flags & NGHTTP2_FLAG_PRIORITY) {
return NGHTTP2_PRIORITY_SPECLEN;
2014-03-25 18:04:24 +01:00
}
return 0;
}
2014-11-27 15:39:04 +01:00
size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
2014-03-25 18:04:24 +01:00
return nghttp2_frame_priority_len(frame->hd.flags);
}
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,
2014-11-27 15:39:04 +01:00
nghttp2_frame_hd *frame_hd) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
nghttp2_buf_chain *ci, *ce;
nghttp2_frame_hd hd;
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
2014-03-13 14:11:02 +01:00
hd = *frame_hd;
hd.length = nghttp2_buf_len(buf);
2014-11-27 15:39:04 +01:00
DEBUGF(fprintf(stderr, "send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n",
hd.length));
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. */
2014-11-27 15:39:04 +01:00
if (bufs->head != bufs->cur) {
2015-09-23 11:07:38 +02:00
hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
2013-07-19 17:08:14 +02:00
}
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
2014-11-27 15:39:04 +01:00
if (bufs->head != bufs->cur) {
2014-03-13 14:11:02 +01:00
/* 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-03-13 14:11:02 +01:00
ce = bufs->cur;
2014-11-27 15:39:04 +01:00
for (ci = bufs->head->next; ci != ce; ci = ci->next) {
2014-03-13 14:11:02 +01:00
buf = &ci->buf;
2014-03-13 14:11:02 +01:00
hd.length = nghttp2_buf_len(buf);
2014-11-27 15:39:04 +01:00
DEBUGF(fprintf(stderr, "send: int CONTINUATION, payloadlen=%zu\n",
hd.length));
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
}
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-11-27 15:39:04 +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;
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
2014-03-13 14:11:02 +01:00
}
return 0;
}
2014-11-27 15:39:04 +01:00
int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
nghttp2_hd_deflater *deflater) {
2014-03-13 14:11:02 +01:00
size_t nv_offset;
int rv;
2014-03-13 14:11:02 +01:00
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 */
2014-05-13 16:42:55 +02:00
rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
2014-03-13 14:11:02 +01:00
2014-11-27 15:39:04 +01:00
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
rv = NGHTTP2_ERR_HEADER_COMP;
}
2014-03-13 14:11:02 +01:00
buf->pos -= nv_offset;
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2014-03-13 14:11:02 +01:00
return rv;
}
2014-11-27 15:39:04 +01:00
if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
}
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);
}
2014-03-25 18:04:24 +01:00
void nghttp2_frame_pack_priority_spec(uint8_t *buf,
2014-11-27 15:39:04 +01:00
const nghttp2_priority_spec *pri_spec) {
2015-09-23 07:41:53 +02:00
nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
2014-11-27 15:39:04 +01:00
if (pri_spec->exclusive) {
buf[0] |= 0x80;
2014-03-25 18:04:24 +01:00
}
2015-09-23 07:41:53 +02:00
buf[4] = (uint8_t)(pri_spec->weight - 1);
2014-03-25 18:04:24 +01:00
}
void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
uint8_t flags _U_,
2014-03-25 18:04:24 +01:00
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen _U_) {
int32_t dep_stream_id;
uint8_t exclusive;
int32_t weight;
2014-03-25 18:04:24 +01:00
dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
exclusive = (payload[0] & 0x80) > 0;
weight = payload[4] + 1;
2014-03-25 18:04:24 +01:00
nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
2014-03-25 18:04:24 +01:00
}
int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen) {
if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags,
payload, payloadlen);
} else {
nghttp2_priority_spec_default_init(&frame->pri_spec);
}
frame->nva = NULL;
frame->nvlen = 0;
2014-03-25 18:04:24 +01:00
return 0;
}
2014-11-27 15:39:04 +01:00
int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-03-13 14:11:02 +01:00
assert(bufs->head == bufs->cur);
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
2014-03-13 14:11:02 +01:00
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
2014-03-25 18:04:24 +01:00
nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
buf->last += NGHTTP2_PRIORITY_SPECLEN;
2014-03-13 14:11:02 +01:00
return 0;
}
void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen) {
nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags, payload,
payloadlen);
}
2014-03-13 14:11:02 +01:00
int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
2014-11-27 15:39:04 +01:00
nghttp2_rst_stream *frame) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-03-13 14:11:02 +01:00
assert(bufs->head == bufs->cur);
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
assert(nghttp2_buf_avail(buf) >= 4);
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
2014-03-13 14:11:02 +01:00
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
nghttp2_put_uint32be(buf->last, frame->error_code);
buf->last += 4;
2014-03-13 14:11:02 +01:00
return 0;
}
void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen _U_) {
frame->error_code = nghttp2_get_uint32(payload);
}
2014-11-27 15:39:04 +01:00
int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-03-13 14:11:02 +01:00
assert(bufs->head == bufs->cur);
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
2015-09-23 07:41:53 +02:00
if (nghttp2_buf_avail(buf) < frame->hd.length) {
2014-03-13 14:11:02 +01:00
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
}
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
2014-03-13 14:11:02 +01:00
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
2014-11-27 15:39:04 +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;
}
2013-08-03 11:05:14 +02:00
size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
const nghttp2_settings_entry *iv,
2014-11-27 15:39:04 +01:00
size_t niv) {
2013-08-03 11:05:14 +02:00
size_t i;
2014-11-27 15:39:04 +01:00
for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
2015-09-23 07:41:53 +02:00
nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
2014-06-07 09:37:29 +02:00
nghttp2_put_uint32be(buf + 2, iv[i].value);
2013-08-03 11:05:14 +02:00
}
return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
2013-08-03 11:05:14 +02:00
}
int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
nghttp2_settings_entry *iv,
size_t niv, nghttp2_mem *mem) {
size_t payloadlen = niv * sizeof(nghttp2_settings_entry);
2014-11-27 15:39:04 +01:00
if (niv == 0) {
2014-04-30 15:40:43 +02:00
frame->iv = NULL;
} else {
frame->iv = nghttp2_mem_malloc(mem, payloadlen);
2014-04-30 15:40:43 +02:00
2014-11-27 15:39:04 +01:00
if (frame->iv == NULL) {
2014-04-30 15:40:43 +02:00
return NGHTTP2_ERR_NOMEM;
}
memcpy(frame->iv, iv, payloadlen);
}
2014-04-30 15:40:43 +02:00
frame->niv = niv;
return 0;
}
void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
2014-11-27 15:39:04 +01:00
const uint8_t *payload) {
2014-06-07 09:37:29 +02:00
iv->settings_id = nghttp2_get_uint16(&payload[0]);
iv->value = nghttp2_get_uint32(&payload[2]);
}
int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
size_t *niv_ptr,
const uint8_t *payload,
size_t payloadlen,
nghttp2_mem *mem) {
2013-08-03 11:05:14 +02:00
size_t i;
2014-04-30 15:40:43 +02:00
*niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
2014-04-30 15:40:43 +02:00
2014-11-27 15:39:04 +01:00
if (*niv_ptr == 0) {
2014-04-30 15:40:43 +02:00
*iv_ptr = NULL;
return 0;
}
*iv_ptr =
nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
2014-04-30 15:40:43 +02:00
2014-11-27 15:39:04 +01:00
if (*iv_ptr == NULL) {
2013-07-12 17:19:03 +02:00
return NGHTTP2_ERR_NOMEM;
}
2014-04-30 15:40:43 +02:00
2014-11-27 15:39:04 +01:00
for (i = 0; i < *niv_ptr; ++i) {
size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
}
2014-04-30 15:40:43 +02:00
return 0;
}
2014-03-13 14:11:02 +01:00
int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
nghttp2_push_promise *frame,
2014-11-27 15:39:04 +01:00
nghttp2_hd_deflater *deflater) {
2014-03-13 14:11:02 +01:00
size_t nv_offset = 4;
int 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-13 14:11:02 +01:00
buf = &bufs->cur->buf;
2014-02-15 08:30:43 +01:00
buf->pos += nv_offset;
buf->last = buf->pos;
/* This call will adjust buf->last to the correct position */
2014-05-13 16:42:55 +02:00
rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
2014-03-13 14:11:02 +01:00
2014-11-27 15:39:04 +01:00
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
rv = NGHTTP2_ERR_HEADER_COMP;
}
buf->pos -= nv_offset;
2014-02-15 08:30:43 +01:00
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2013-07-24 18:49:05 +02:00
return rv;
}
2015-09-23 07:41:53 +02:00
nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
2014-03-13 14:11:02 +01:00
frame->padlen = 0;
frame->hd.length = nghttp2_bufs_len(bufs);
2014-03-13 14:11:02 +01:00
return frame_pack_headers_shared(bufs, &frame->hd);
2013-07-24 18:49:05 +02:00
}
int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen _U_) {
frame->promised_stream_id =
nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
frame->nva = NULL;
frame->nvlen = 0;
return 0;
}
2014-11-27 15:39:04 +01:00
int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-03-13 14:11:02 +01:00
assert(bufs->head == bufs->cur);
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
assert(nghttp2_buf_avail(buf) >= 8);
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
2014-03-13 14:11:02 +01:00
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
2014-11-27 15:39:04 +01:00
buf->last =
nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
2014-03-13 14:11:02 +01:00
return 0;
2012-04-05 18:45:39 +02:00
}
void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen _U_) {
memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
}
2013-07-15 14:45:59 +02:00
2014-11-27 15:39:04 +01:00
int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
2014-02-18 16:45:20 +01:00
int rv;
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-03-13 14:11:02 +01:00
assert(bufs->head == bufs->cur);
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
2014-03-13 14:11:02 +01:00
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
2015-09-23 07:41:53 +02:00
nghttp2_put_uint32be(buf->last, (uint32_t)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);
2014-11-27 15:39:04 +01:00
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
}
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2014-03-13 14:11:02 +01:00
return rv;
}
2014-03-13 14:11:02 +01:00
return 0;
2013-07-15 14:45:59 +02:00
}
void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
const uint8_t *payload,
size_t payloadlen _U_,
2014-03-22 10:59:59 +01:00
uint8_t *var_gift_payload,
2014-11-27 15:39:04 +01:00
size_t var_gift_payloadlen) {
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, nghttp2_mem *mem) {
2014-03-22 10:59:59 +01:00
uint8_t *var_gift_payload;
size_t var_gift_payloadlen;
2014-11-27 15:39:04 +01:00
if (payloadlen > 8) {
2014-03-22 10:59:59 +01:00
var_gift_payloadlen = payloadlen - 8;
} else {
var_gift_payloadlen = 0;
}
payloadlen -= var_gift_payloadlen;
2014-11-27 15:39:04 +01:00
if (!var_gift_payloadlen) {
2014-04-30 15:09:02 +02:00
var_gift_payload = NULL;
} else {
var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
2014-03-22 10:59:59 +01:00
2014-11-27 15:39:04 +01:00
if (var_gift_payload == NULL) {
2014-04-30 15:09:02 +02:00
return NGHTTP2_ERR_NOMEM;
}
2014-03-22 10:59:59 +01:00
2014-04-30 15:09:02 +02:00
memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
}
2014-03-22 10:59:59 +01:00
nghttp2_frame_unpack_goaway_payload(frame, payload, payloadlen,
var_gift_payload, var_gift_payloadlen);
return 0;
}
2014-03-13 14:11:02 +01:00
int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
2014-11-27 15:39:04 +01:00
nghttp2_window_update *frame) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-03-13 14:11:02 +01:00
assert(bufs->head == bufs->cur);
2014-03-13 14:11:02 +01:00
buf = &bufs->head->buf;
assert(nghttp2_buf_avail(buf) >= 4);
2014-03-13 14:11:02 +01:00
buf->pos -= NGHTTP2_FRAME_HDLEN;
2014-03-13 14:11:02 +01:00
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
2015-09-23 07:41:53 +02:00
nghttp2_put_uint32be(buf->last, (uint32_t)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
}
void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
const uint8_t *payload,
2014-11-27 15:39:04 +01:00
size_t payloadlen _U_) {
frame->window_size_increment =
nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
}
2016-03-29 16:29:26 +02:00
int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
int rv;
nghttp2_buf *buf;
nghttp2_ext_altsvc *altsvc;
altsvc = frame->payload;
buf = &bufs->head->buf;
assert(nghttp2_buf_avail(buf) >=
2 + altsvc->origin_len + altsvc->field_value_len);
buf->pos -= NGHTTP2_FRAME_HDLEN;
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
buf->last += 2;
rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
assert(rv == 0);
rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
assert(rv == 0);
return 0;
}
2016-04-03 15:58:25 +02:00
void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
size_t origin_len, uint8_t *payload,
size_t payloadlen) {
nghttp2_ext_altsvc *altsvc;
uint8_t *p;
altsvc = frame->payload;
p = payload;
altsvc->origin = p;
p += origin_len;
altsvc->origin_len = origin_len;
altsvc->field_value = p;
altsvc->field_value_len = (size_t)(payload + payloadlen - p);
}
2014-11-27 15:39:04 +01:00
nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
size_t niv, nghttp2_mem *mem) {
2013-07-12 17:19:03 +02:00
nghttp2_settings_entry *iv_copy;
2014-11-27 15:39:04 +01:00
size_t len = niv * sizeof(nghttp2_settings_entry);
2014-04-30 15:40:43 +02:00
2014-11-27 15:39:04 +01:00
if (len == 0) {
2014-04-30 15:40:43 +02:00
return NULL;
}
iv_copy = nghttp2_mem_malloc(mem, len);
2014-04-30 15:40:43 +02:00
2014-11-27 15:39:04 +01:00
if (iv_copy == NULL) {
return NULL;
}
2014-04-30 15:40:43 +02:00
memcpy(iv_copy, iv, len);
2014-04-30 15:40:43 +02:00
return iv_copy;
}
2012-03-10 10:41:01 +01:00
2014-11-27 15:39:04 +01:00
int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
2013-07-19 09:50:31 +02:00
return a->namelen == b->namelen && a->valuelen == b->valuelen &&
2014-11-27 15:39:04 +01:00
memcmp(a->name, b->name, a->namelen) == 0 &&
memcmp(a->value, b->value, a->valuelen) == 0;
2013-07-19 09:50:31 +02:00
}
void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
nghttp2_mem_free(mem, nva);
}
2013-07-19 17:08:14 +02:00
2014-11-27 15:39:04 +01:00
static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
size_t blen) {
2014-05-06 11:39:04 +02:00
int rv;
2014-11-27 15:39:04 +01:00
if (alen == blen) {
return memcmp(a, b, alen);
2014-05-06 11:39:04 +02:00
}
2014-11-27 15:39:04 +01:00
if (alen < blen) {
2014-05-06 11:39:04 +02:00
rv = memcmp(a, b, alen);
2014-11-27 15:39:04 +01:00
if (rv == 0) {
2013-07-19 17:08:14 +02:00
return -1;
}
2014-05-06 11:39:04 +02:00
return rv;
}
rv = memcmp(a, b, blen);
2014-11-27 15:39:04 +01:00
if (rv == 0) {
2014-05-06 11:39:04 +02:00
return 1;
2013-07-19 17:08:14 +02:00
}
2014-05-06 11:39:04 +02:00
return rv;
2013-07-19 17:08:14 +02:00
}
2014-11-27 15:39:04 +01:00
int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
}
2014-11-27 15:39:04 +01:00
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;
2014-05-06 11:39:04 +02:00
rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
2014-05-06 11:39:04 +02:00
2014-11-27 15:39:04 +01:00
if (rv == 0) {
return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
}
2014-05-06 11:39:04 +02:00
return rv;
2013-08-27 17:09:46 +02:00
}
2014-11-27 15:39:04 +01:00
void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
2013-07-19 17:08:14 +02:00
}
2014-11-27 15:39:04 +01:00
int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
size_t nvlen, nghttp2_mem *mem) {
size_t i;
uint8_t *data = NULL;
size_t buflen = 0;
nghttp2_nv *p;
2014-05-06 11:39:04 +02:00
2014-11-27 15:39:04 +01:00
if (nvlen == 0) {
*nva_ptr = NULL;
2014-05-06 11:39:04 +02:00
return 0;
}
2014-05-06 11:39:04 +02:00
for (i = 0; i < nvlen; ++i) {
/* + 1 for null-termination */
if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
buflen += nva[i].namelen + 1;
}
if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
buflen += nva[i].valuelen + 1;
}
}
2014-11-27 15:39:04 +01:00
buflen += sizeof(nghttp2_nv) * nvlen;
2014-05-06 11:39:04 +02:00
*nva_ptr = nghttp2_mem_malloc(mem, buflen);
2014-05-06 11:39:04 +02:00
2014-11-27 15:39:04 +01:00
if (*nva_ptr == NULL) {
return NGHTTP2_ERR_NOMEM;
}
2014-05-06 11:39:04 +02:00
p = *nva_ptr;
2014-11-27 15:39:04 +01:00
data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
2014-11-27 15:39:04 +01:00
for (i = 0; i < nvlen; ++i) {
p->flags = nva[i].flags;
if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
p->name = nva[i].name;
p->namelen = nva[i].namelen;
} else {
memcpy(data, nva[i].name, nva[i].namelen);
p->name = data;
p->namelen = nva[i].namelen;
data[p->namelen] = '\0';
nghttp2_downcase(p->name, p->namelen);
data += nva[i].namelen + 1;
}
if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
p->value = nva[i].value;
p->valuelen = nva[i].valuelen;
} else {
memcpy(data, nva[i].value, nva[i].valuelen);
p->value = data;
p->valuelen = nva[i].valuelen;
data[p->valuelen] = '\0';
data += nva[i].valuelen + 1;
}
++p;
}
return 0;
}
2014-11-27 15:39:04 +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;
2014-11-27 15:39:04 +01:00
for (i = 0; i < niv; ++i) {
switch (iv[i].settings_id) {
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
break;
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
break;
case NGHTTP2_SETTINGS_ENABLE_PUSH:
2014-11-27 15:39:04 +01:00
if (iv[i].value != 0 && iv[i].value != 1) {
return 0;
}
break;
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
2014-11-27 15:39:04 +01:00
if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
return 0;
}
break;
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
2014-11-27 15:39:04 +01:00
if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
return 0;
}
break;
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
break;
2013-08-03 11:05:14 +02:00
}
}
return 1;
}
static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
2014-03-13 14:11:02 +01:00
size_t trail_padlen;
2014-07-09 15:40:31 +02:00
size_t newlen;
DEBUGF(fprintf(stderr, "send: padlen=%zu, shift left 1 bytes\n", padlen));
memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
--buf->pos;
2014-07-09 15:40:31 +02:00
buf->pos[4] |= NGHTTP2_FLAG_PADDED;
2014-07-09 15:40:31 +02:00
newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
2014-03-13 14:11:02 +01:00
if (framehd_only) {
return;
}
trail_padlen = padlen - 1;
2015-09-23 07:41:53 +02:00
buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
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;
}
int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
size_t padlen, int framehd_only) {
2014-03-13 14:11:02 +01:00
nghttp2_buf *buf;
2014-11-27 15:39:04 +01:00
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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2014-07-09 15:40:31 +02:00
* | |Frame header | Frame payload... :
* +-+-----------------+-------------------------------------------+
* | |Frame header | Frame payload... :
* +-+-----------------+-------------------------------------------+
* | |Frame header | Frame payload... :
* +-+-----------------+-------------------------------------------+
2014-03-13 14:11:02 +01:00
*
* We arranged padding so that it is included in the first frame
* completely. For padded frame, we are going to adjust buf->pos of
* frame which includes padding and serialize (memmove) frame header
* in the correct position. Also extends buf->last to include
* padding.
2014-03-13 14:11:02 +01:00
*/
buf = &bufs->head->buf;
2014-03-13 14:11:02 +01:00
2015-09-23 07:41:53 +02:00
assert(nghttp2_buf_avail(buf) >= padlen - 1);
2014-03-13 14:11:02 +01:00
frame_set_pad(buf, padlen, framehd_only);
2014-03-13 14:11:02 +01:00
hd->length += padlen;
hd->flags |= NGHTTP2_FLAG_PADDED;
2014-03-13 14:11:02 +01:00
2014-11-27 15:39:04 +01:00
DEBUGF(fprintf(stderr, "send: final payloadlen=%zu, padlen=%zu\n", hd->length,
padlen));
2014-03-13 14:11:02 +01:00
return 0;
}