nghttp2/lib/nghttp2_frame.c

850 lines
25 KiB
C
Raw Normal View History

/*
2013-07-12 17:19:03 +02:00
* nghttp2 - HTTP/2.0 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"
2013-07-15 14:45:59 +02:00
/* This is SPDY stuff, and will be removed after header compression is
implemented */
static size_t nghttp2_frame_get_len_size(void)
{
2013-07-15 14:45:59 +02:00
return 2;
}
2013-07-15 14:45:59 +02:00
static uint8_t* nghttp2_pack_str(uint8_t *buf, const char *str, size_t len)
{
2013-07-15 14:45:59 +02:00
nghttp2_frame_put_nv_len(buf, len);
buf += nghttp2_frame_get_len_size();
memcpy(buf, str, len);
return buf+len;
}
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)
{
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);
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t* buf)
{
2013-07-15 14:45:59 +02:00
hd->length = nghttp2_get_uint16(&buf[0]);
hd->type = buf[2];
hd->flags = buf[3];
hd->stream_id = nghttp2_get_uint32(&buf[4]) & NGHTTP2_STREAM_ID_MASK;
}
2013-07-12 17:19:03 +02:00
ssize_t nghttp2_frame_pack_nv(uint8_t *buf, char **nv, size_t len_size)
{
int i;
uint8_t *bufp = buf+len_size;
uint32_t num_nv = 0;
const char *prev = "";
uint8_t *cur_vallen_buf = NULL;
uint32_t cur_vallen = 0;
size_t prevkeylen = 0;
size_t prevvallen = 0;
for(i = 0; nv[i]; i += 2) {
const char *key = nv[i];
const char *val = nv[i+1];
size_t keylen = strlen(key);
size_t vallen = strlen(val);
if(prevkeylen == keylen && memcmp(prev, key, keylen) == 0) {
if(vallen) {
if(prevvallen) {
/* Join previous value, with NULL character */
cur_vallen += vallen+1;
2013-07-15 14:45:59 +02:00
nghttp2_frame_put_nv_len(cur_vallen_buf, cur_vallen);
*bufp = '\0';
++bufp;
memcpy(bufp, val, vallen);
bufp += vallen;
} else {
/* Previous value is empty. In this case, drop the
previous. */
cur_vallen += vallen;
2013-07-15 14:45:59 +02:00
nghttp2_frame_put_nv_len(cur_vallen_buf, cur_vallen);
memcpy(bufp, val, vallen);
bufp += vallen;
}
}
} else {
++num_nv;
2013-07-15 14:45:59 +02:00
bufp = nghttp2_pack_str(bufp, key, keylen);
prev = key;
cur_vallen_buf = bufp;
cur_vallen = vallen;
prevkeylen = keylen;
prevvallen = vallen;
2013-07-15 14:45:59 +02:00
bufp = nghttp2_pack_str(bufp, val, vallen);
}
}
2013-07-15 14:45:59 +02:00
nghttp2_frame_put_nv_len(buf, num_nv);
return bufp-buf;
}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_nv_del(char **nv)
{
free(nv);
}
2013-07-12 17:19:03 +02:00
char** nghttp2_frame_nv_copy(const char **nv)
{
int i;
char *buf;
char **idx, *data;
size_t buflen = 0;
for(i = 0; nv[i]; ++i) {
buflen += strlen(nv[i])+1;
}
buflen += (i+1)*sizeof(char*);
buf = malloc(buflen);
if(buf == NULL) {
return NULL;
}
idx = (char**)buf;
data = buf+(i+1)*sizeof(char*);
for(i = 0; nv[i]; ++i) {
size_t len = strlen(nv[i])+1;
memcpy(data, nv[i], len);
*idx++ = data;
data += len;
}
*idx = NULL;
return (char**)buf;
}
2013-07-12 17:19:03 +02:00
static int nghttp2_string_compar(const void *lhs, const void *rhs)
{
return strcmp(*(char **)lhs, *(char **)rhs);
}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_nv_sort(char **nv)
{
int n;
for(n = 0; nv[n]; ++n);
2013-07-12 17:19:03 +02:00
qsort(nv, n/2, 2*sizeof(char*), nghttp2_string_compar);
}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_nv_downcase(char **nv)
{
int i, j;
for(i = 0; nv[i]; i += 2) {
for(j = 0; nv[i][j] != '\0'; ++j) {
if('A' <= nv[i][j] && nv[i][j] <= 'Z') {
nv[i][j] += 'a'-'A';
}
}
}
}
2013-07-12 17:19:03 +02:00
char** nghttp2_frame_nv_norm_copy(const char **nv)
{
char **nv_copy;
2013-07-12 17:19:03 +02:00
nv_copy = nghttp2_frame_nv_copy(nv);
if(nv_copy != NULL) {
2013-07-12 17:19:03 +02:00
nghttp2_frame_nv_downcase(nv_copy);
nghttp2_frame_nv_sort(nv_copy);
}
return nv_copy;
}
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,
uint8_t flags, int32_t stream_id, int32_t pri,
2013-07-19 17:08:14 +02:00
nghttp2_nv *nva, size_t nvlen)
{
2013-07-15 14:45:59 +02:00
memset(frame, 0, sizeof(nghttp2_headers));
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
frame->pri = pri;
2013-07-19 17:08:14 +02:00
frame->nva = nva;
frame->nvlen = nvlen;
frame->cat = NGHTTP2_HCAT_REQUEST;
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_headers_free(nghttp2_headers *frame)
{
2013-07-19 17:08:14 +02:00
nghttp2_nv_array_del(frame->nva);
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
int32_t pri)
2012-01-27 11:35:05 +01:00
{
2013-07-15 14:45:59 +02:00
memset(frame, 0, sizeof(nghttp2_priority));
nghttp2_frame_set_hd(&frame->hd, 4, NGHTTP2_PRIORITY, NGHTTP2_FLAG_NONE,
stream_id);
frame->pri = pri;
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)
{
2013-07-12 17:19:03 +02:00
memset(frame, 0, sizeof(nghttp2_rst_stream));
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;
}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame)
{}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_settings_init(nghttp2_settings *frame,
nghttp2_settings_entry *iv, size_t niv)
{
2013-07-12 17:19:03 +02:00
memset(frame, 0, sizeof(nghttp2_settings));
2013-07-15 14:45:59 +02:00
nghttp2_frame_set_hd(&frame->hd, niv*8, NGHTTP2_SETTINGS, NGHTTP2_FLAG_NONE,
0);
frame->niv = niv;
frame->iv = iv;
}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_settings_free(nghttp2_settings *frame)
{
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)
{
memset(frame, 0, sizeof(nghttp2_push_promise));
nghttp2_frame_set_hd(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
frame->promised_stream_id = promised_stream_id;
frame->nva = nva;
frame->nvlen = nvlen;
}
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
memset(frame, 0, sizeof(nghttp2_ping));
nghttp2_frame_set_hd(&frame->hd, 8, NGHTTP2_PING, flags, 0);
if(opaque_data) {
memcpy(frame->opaque_data, opaque_data, 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)
{}
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)
{
2013-07-15 14:45:59 +02:00
memset(frame, 0, sizeof(nghttp2_goaway));
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;
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_goaway_free(nghttp2_goaway *frame)
{
2013-07-15 14:45:59 +02:00
free(frame->opaque_data);
}
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
memset(frame, 0, sizeof(nghttp2_window_update));
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
2013-07-15 14:45:59 +02:00
void nghttp2_frame_data_init(nghttp2_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
memset(frame, 0, sizeof(nghttp2_data));
/* 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;
2012-01-28 11:22:38 +01:00
}
2013-07-15 14:45:59 +02:00
void nghttp2_frame_data_free(nghttp2_data *frame)
{}
2012-01-28 11:22:38 +01:00
2013-07-19 17:08:14 +02:00
static size_t headers_nv_offset(nghttp2_headers *frame)
{
if(frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
return NGHTTP2_FRAME_HEAD_LENGTH + 4;
} else {
return NGHTTP2_FRAME_HEAD_LENGTH;
}
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_pack_headers(uint8_t **buf_ptr,
size_t *buflen_ptr,
2013-07-12 17:19:03 +02:00
nghttp2_headers *frame,
2013-07-19 17:08:14 +02:00
nghttp2_hd_context *deflater)
{
ssize_t framelen;
2013-07-19 17:08:14 +02:00
size_t nv_offset = headers_nv_offset(frame);
ssize_t rv;
rv = nghttp2_hd_deflate_hd(deflater, buf_ptr, buflen_ptr, nv_offset,
frame->nva, frame->nvlen);
if(rv < 0) {
return rv;
}
framelen = rv + nv_offset;
2013-07-15 14:45:59 +02:00
frame->hd.length = framelen - NGHTTP2_FRAME_HEAD_LENGTH;
2013-07-19 17:08:14 +02:00
/* If frame->nvlen == 0, *buflen_ptr may be smaller than
nv_offset */
rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, nv_offset);
if(rv < 0) {
return rv;
}
memset(*buf_ptr, 0, nv_offset);
2013-07-15 14:45:59 +02:00
/* pack ctrl header after length is determined */
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
if(frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->pri);
}
return framelen;
}
2013-07-12 17:19:03 +02:00
int nghttp2_frame_unpack_headers(nghttp2_headers *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload, size_t payloadlen,
2013-07-19 17:08:14 +02:00
nghttp2_hd_context *inflater)
{
2013-07-19 17:08:14 +02:00
ssize_t r;
size_t pnv_offset;
2013-07-12 17:19:03 +02:00
r = nghttp2_frame_unpack_headers_without_nv(frame, head, headlen,
payload, payloadlen);
2013-07-19 17:08:14 +02:00
if(r < 0) {
return r;
}
pnv_offset = headers_nv_offset(frame) - NGHTTP2_FRAME_HEAD_LENGTH;
r = nghttp2_hd_inflate_hd(inflater, &frame->nva,
(uint8_t*)payload + pnv_offset,
payloadlen - pnv_offset);
if(r < 0) {
return r;
}
2013-07-19 17:08:14 +02:00
frame->nvlen = r;
return 0;
}
2013-07-12 17:19:03 +02:00
int nghttp2_frame_unpack_headers_without_nv(nghttp2_headers *frame,
const uint8_t *head,
size_t headlen,
const uint8_t *payload,
size_t payloadlen)
{
2013-07-15 14:45:59 +02:00
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
if(head[3] & NGHTTP2_FLAG_PRIORITY) {
2013-07-19 17:08:14 +02:00
if(payloadlen < 4) {
2013-07-15 14:45:59 +02:00
return NGHTTP2_ERR_INVALID_FRAME;
}
frame->pri = nghttp2_get_uint32(payload) & NGHTTP2_PRIORITY_MASK;
} else {
frame->pri = NGHTTP2_PRI_DEFAULT;
}
2013-07-19 17:08:14 +02:00
frame->nva = NULL;
frame->nvlen = 0;
return 0;
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_pack_priority(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_priority *frame)
{
2013-07-15 14:45:59 +02:00
ssize_t framelen= NGHTTP2_FRAME_HEAD_LENGTH + 4;
int r;
2013-07-12 17:19:03 +02:00
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) {
return r;
}
memset(*buf_ptr, 0, framelen);
2013-07-15 14:45:59 +02:00
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->pri);
return framelen;
}
2013-07-15 14:45:59 +02:00
int nghttp2_frame_unpack_priority(nghttp2_priority *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload, size_t payloadlen)
{
2013-07-15 14:45:59 +02:00
if(payloadlen != 4) {
2013-07-12 17:19:03 +02:00
return NGHTTP2_ERR_INVALID_FRAME;
}
2013-07-15 14:45:59 +02:00
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
frame->pri = nghttp2_get_uint32(payload) & NGHTTP2_PRIORITY_MASK;
return 0;
2013-07-15 14:45:59 +02:00
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_pack_rst_stream(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_rst_stream *frame)
{
2013-07-15 14:45:59 +02:00
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4;
int r;
2013-07-12 17:19:03 +02:00
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) {
return r;
}
memset(*buf_ptr, 0, framelen);
2013-07-15 14:45:59 +02:00
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->error_code);
return framelen;
}
2013-07-15 14:45:59 +02:00
int nghttp2_frame_unpack_rst_stream(nghttp2_rst_stream *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload, size_t payloadlen)
{
2013-07-15 14:45:59 +02:00
if(payloadlen != 4) {
2013-07-12 17:19:03 +02:00
return NGHTTP2_ERR_INVALID_FRAME;
}
2013-07-15 14:45:59 +02:00
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
frame->error_code = nghttp2_get_uint32(payload);
return 0;
}
2013-07-12 17:19:03 +02:00
ssize_t nghttp2_frame_pack_settings(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_settings *frame)
{
2013-07-15 14:45:59 +02:00
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length;
int r;
2013-07-12 17:19:03 +02:00
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) {
return r;
}
memset(*buf_ptr, 0, framelen);
2013-07-15 14:45:59 +02:00
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
2013-08-03 11:05:14 +02:00
nghttp2_frame_pack_settings_payload(*buf_ptr + 8, frame->iv, frame->niv);
return framelen;
}
2013-08-03 11:05:14 +02:00
size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
nghttp2_settings_entry *iv,
size_t niv)
{
size_t i;
for(i = 0; i < niv; ++i, buf += 8) {
nghttp2_put_uint32be(buf, iv[i].settings_id);
nghttp2_put_uint32be(buf + 4, iv[i].value);
}
return 8 * niv;
}
2013-07-12 17:19:03 +02:00
int nghttp2_frame_unpack_settings(nghttp2_settings *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload, size_t payloadlen)
{
2013-08-03 11:05:14 +02:00
int rv;
2013-07-15 14:45:59 +02:00
if(payloadlen % 8) {
2013-07-12 17:19:03 +02:00
return NGHTTP2_ERR_INVALID_FRAME;
}
2013-07-15 14:45:59 +02:00
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
2013-08-03 11:05:14 +02:00
rv = nghttp2_frame_unpack_settings_payload(&frame->iv, &frame->niv,
payload, payloadlen);
if(rv != 0) {
return rv;
}
return 0;
}
int nghttp2_frame_unpack_settings_payload(nghttp2_settings_entry **iv_ptr,
size_t *niv_ptr,
const uint8_t *payload,
size_t payloadlen)
{
size_t i;
*niv_ptr = payloadlen / 8;
*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;
}
2013-08-03 11:05:14 +02:00
for(i = 0; i < *niv_ptr; ++i) {
2013-07-15 14:45:59 +02:00
size_t off = i*8;
2013-08-03 11:05:14 +02:00
(*iv_ptr)[i].settings_id = nghttp2_get_uint32(&payload[off]) &
2013-07-15 14:45:59 +02:00
NGHTTP2_SETTINGS_ID_MASK;
2013-08-03 11:05:14 +02:00
(*iv_ptr)[i].value = nghttp2_get_uint32(&payload[4+off]);
}
return 0;
}
2013-07-24 18:49:05 +02:00
ssize_t nghttp2_frame_pack_push_promise(uint8_t **buf_ptr,
size_t *buflen_ptr,
nghttp2_push_promise *frame,
nghttp2_hd_context *deflater)
{
ssize_t framelen;
size_t nv_offset = NGHTTP2_FRAME_HEAD_LENGTH + 4;
ssize_t rv;
rv = nghttp2_hd_deflate_hd(deflater, buf_ptr, buflen_ptr, nv_offset,
frame->nva, frame->nvlen);
if(rv < 0) {
return rv;
}
framelen = rv + nv_offset;
frame->hd.length = framelen - NGHTTP2_FRAME_HEAD_LENGTH;
/* If frame->nvlen == 0, *buflen_ptr may be smaller than
nv_offset */
rv = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, nv_offset);
if(rv < 0) {
return rv;
}
memset(*buf_ptr, 0, nv_offset);
/* pack ctrl header after length is determined */
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->promised_stream_id);
return framelen;
}
int nghttp2_frame_unpack_push_promise(nghttp2_push_promise *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload,
size_t payloadlen,
nghttp2_hd_context *inflater)
{
ssize_t r;
r = nghttp2_frame_unpack_push_promise_without_nv(frame, head, headlen,
payload, payloadlen);
if(r < 0) {
return r;
}
r = nghttp2_hd_inflate_hd(inflater, &frame->nva,
(uint8_t*)payload + 4, payloadlen - 4);
if(r < 0) {
return r;
}
frame->nvlen = r;
return 0;
}
int nghttp2_frame_unpack_push_promise_without_nv(nghttp2_push_promise *frame,
const uint8_t *head,
size_t headlen,
const uint8_t *payload,
size_t payloadlen)
{
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
if(payloadlen < 4) {
return NGHTTP2_ERR_INVALID_FRAME;
}
frame->promised_stream_id = nghttp2_get_uint32(payload) &
NGHTTP2_STREAM_ID_MASK;
frame->nva = NULL;
frame->nvlen = 0;
return 0;
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_pack_ping(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_ping *frame)
2012-04-05 18:45:39 +02:00
{
2013-07-15 14:45:59 +02:00
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 8;
2012-04-05 18:45:39 +02:00
int r;
2013-07-12 17:19:03 +02:00
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
2012-04-05 18:45:39 +02:00
if(r != 0) {
return r;
}
memset(*buf_ptr, 0, framelen);
2013-07-15 14:45:59 +02:00
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
memcpy(&(*buf_ptr)[8], frame->opaque_data, sizeof(frame->opaque_data));
2012-04-05 18:45:39 +02:00
return framelen;
}
2013-07-15 14:45:59 +02:00
int nghttp2_frame_unpack_ping(nghttp2_ping *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload, size_t payloadlen)
2012-04-05 18:45:39 +02:00
{
2013-07-15 14:45:59 +02:00
if(payloadlen != 8) {
return NGHTTP2_ERR_INVALID_FRAME;
2012-04-05 18:45:39 +02:00
}
2013-07-15 14:45:59 +02:00
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
return 0;
2012-04-05 18:45:39 +02:00
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_pack_goaway(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_goaway *frame)
2012-04-05 18:45:39 +02:00
{
2013-07-15 14:45:59 +02:00
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length;
int r;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) {
return r;
2012-04-05 18:45:39 +02:00
}
2013-07-15 14:45:59 +02:00
memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->last_stream_id);
nghttp2_put_uint32be(&(*buf_ptr)[12], frame->error_code);
memcpy(&(*buf_ptr)[16], frame->opaque_data, frame->opaque_data_len);
return framelen;
}
int nghttp2_frame_unpack_goaway(nghttp2_goaway *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload, size_t payloadlen)
{
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
if(payloadlen < 8) {
return NGHTTP2_ERR_INVALID_FRAME;
}
frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
frame->error_code = nghttp2_get_uint32(payload+4);
frame->opaque_data_len = payloadlen - 8;
if(frame->opaque_data_len == 0) {
frame->opaque_data = NULL;
} else {
frame->opaque_data = malloc(frame->opaque_data_len);
if(frame->opaque_data == NULL) {
return NGHTTP2_ERR_NOMEM;
2012-04-05 18:45:39 +02:00
}
2013-07-15 14:45:59 +02:00
memcpy(frame->opaque_data, &payload[8], frame->opaque_data_len);
2012-04-05 18:45:39 +02:00
}
return 0;
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_pack_window_update(uint8_t **buf_ptr, size_t *buflen_ptr,
nghttp2_window_update *frame)
2012-04-05 18:45:39 +02:00
{
2013-07-15 14:45:59 +02:00
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4;
int r;
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
if(r != 0) {
return r;
2012-04-05 18:45:39 +02:00
}
2013-07-15 14:45:59 +02:00
memset(*buf_ptr, 0, framelen);
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->window_size_increment);
return framelen;
}
int nghttp2_frame_unpack_window_update(nghttp2_window_update *frame,
const uint8_t *head, size_t headlen,
const uint8_t *payload,
size_t payloadlen)
{
if(payloadlen != 4) {
2013-07-12 17:19:03 +02:00
return NGHTTP2_ERR_INVALID_FRAME;
2012-04-05 18:45:39 +02:00
}
2013-07-15 14:45:59 +02:00
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
frame->window_size_increment = nghttp2_get_uint32(payload) &
NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
2012-04-05 18:45:39 +02:00
return 0;
}
2013-07-12 17:19:03 +02:00
nghttp2_settings_entry* nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
size_t niv)
{
2013-07-12 17:19:03 +02:00
nghttp2_settings_entry *iv_copy;
size_t len = niv*sizeof(nghttp2_settings_entry);
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-12 17:19:03 +02:00
static int nghttp2_settings_entry_compar(const void *lhs, const void *rhs)
2012-03-10 10:41:01 +01:00
{
2013-07-12 17:19:03 +02:00
return ((nghttp2_settings_entry *)lhs)->settings_id
-((nghttp2_settings_entry *)rhs)->settings_id;
2012-03-10 10:41:01 +01:00
}
2013-07-12 17:19:03 +02:00
void nghttp2_frame_iv_sort(nghttp2_settings_entry *iv, size_t niv)
2012-03-10 10:41:01 +01:00
{
2013-07-12 17:19:03 +02:00
qsort(iv, niv, sizeof(nghttp2_settings_entry), nghttp2_settings_entry_compar);
2012-03-10 10:41:01 +01:00
}
2013-07-15 14:45:59 +02:00
ssize_t nghttp2_frame_nv_offset(const uint8_t *head)
{
2013-07-15 14:45:59 +02:00
switch(head[2]) {
case NGHTTP2_HEADERS:
if(head[3] & NGHTTP2_FLAG_PRIORITY) {
return NGHTTP2_FRAME_HEAD_LENGTH + 4;
} else {
return NGHTTP2_FRAME_HEAD_LENGTH;
}
2013-07-15 14:45:59 +02:00
case NGHTTP2_PUSH_PROMISE:
return NGHTTP2_FRAME_HEAD_LENGTH + 4;
default:
2013-07-15 14:45:59 +02:00
return -1;
}
}
2013-07-12 17:19:03 +02:00
int nghttp2_frame_nv_check_null(const char **nv)
{
size_t i, j;
for(i = 0; nv[i]; i += 2) {
if(nv[i][0] == '\0' || nv[i+1] == NULL) {
return 0;
}
for(j = 0; nv[i][j]; ++j) {
unsigned char c = nv[i][j];
if(c < 0x20 || c > 0x7e) {
return 0;
}
}
}
return 1;
}
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
static int nghttp2_nv_name_compar(const void *lhs, const void *rhs)
{
nghttp2_nv *a = (nghttp2_nv*)lhs, *b = (nghttp2_nv*)rhs;
if(a->namelen == b->namelen) {
return memcmp(a->name, b->name, a->namelen);
} else if(a->namelen < b->namelen) {
int rv = memcmp(a->name, b->name, a->namelen);
if(rv == 0) {
return -1;
} else {
return rv;
}
} else {
int rv = memcmp(a->name, b->name, b->namelen);
if(rv == 0) {
return 1;
} else {
return rv;
}
}
}
void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen)
{
qsort(nva, nvlen, sizeof(nghttp2_nv), nghttp2_nv_name_compar);
}
ssize_t nghttp2_nv_array_from_cstr(nghttp2_nv **nva_ptr, const char **nv)
{
int i;
uint8_t *data;
size_t buflen = 0, nvlen = 0;
nghttp2_nv *p;
for(i = 0; nv[i]; ++i) {
size_t len = strlen(nv[i]);
if(len > (1 << 16) - 1) {
return NGHTTP2_ERR_INVALID_ARGUMENT;
}
buflen += len;
}
nvlen = i/2;
/* If all name/value pair is 0-length, remove them */
if(nvlen == 0 || buflen == 0) {
2013-07-19 17:08:14 +02:00
*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; nv[i]; i += 2) {
size_t len = strlen(nv[i]);
memcpy(data, nv[i], len);
p->name = data;
p->namelen = len;
nghttp2_downcase(p->name, p->namelen);
data += len;
len = strlen(nv[i+1]);
memcpy(data, nv[i+1], len);
p->value = data;
p->valuelen = len;
data += len;
++p;
}
nghttp2_nv_array_sort(*nva_ptr, nvlen);
return nvlen;
}
2013-08-03 11:05:14 +02:00
int nghttp2_settings_check_duplicate(const nghttp2_settings_entry *iv,
size_t niv)
{
int check[NGHTTP2_SETTINGS_MAX+1];
size_t i;
memset(check, 0, sizeof(check));
for(i = 0; i < niv; ++i) {
if(iv[i].settings_id > NGHTTP2_SETTINGS_MAX || iv[i].settings_id == 0 ||
check[iv[i].settings_id] == 1) {
return 0;
} else {
check[iv[i].settings_id] = 1;
}
}
return 1;
}