2012-01-24 14:02:24 +01:00
|
|
|
/*
|
2013-07-12 17:19:03 +02:00
|
|
|
* nghttp2 - HTTP/2.0 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"
|
2012-01-24 14:02:24 +01:00
|
|
|
|
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)
|
2012-02-24 17:17:03 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
return 2;
|
2012-02-24 17:17:03 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
static uint8_t* nghttp2_pack_str(uint8_t *buf, const char *str, size_t len)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_put_nv_len(buf, len);
|
|
|
|
buf += nghttp2_frame_get_len_size();
|
2012-01-24 14:02:24 +01:00
|
|
|
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)
|
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-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;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
ssize_t nghttp2_frame_pack_nv(uint8_t *buf, char **nv, size_t len_size)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
|
|
|
int i;
|
2012-02-24 15:05:49 +01:00
|
|
|
uint8_t *bufp = buf+len_size;
|
|
|
|
uint32_t num_nv = 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
const char *prev = "";
|
2013-03-23 11:31:22 +01:00
|
|
|
uint8_t *cur_vallen_buf = NULL;
|
|
|
|
uint32_t cur_vallen = 0;
|
|
|
|
size_t prevkeylen = 0;
|
|
|
|
size_t prevvallen = 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
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);
|
2013-03-23 11:31:22 +01:00
|
|
|
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);
|
2013-03-23 11:31:22 +01:00
|
|
|
*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);
|
2013-03-23 11:31:22 +01:00
|
|
|
memcpy(bufp, val, vallen);
|
|
|
|
bufp += vallen;
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 14:02:24 +01:00
|
|
|
} else {
|
|
|
|
++num_nv;
|
2013-07-15 14:45:59 +02:00
|
|
|
bufp = nghttp2_pack_str(bufp, key, keylen);
|
2012-01-24 14:02:24 +01:00
|
|
|
prev = key;
|
2013-03-23 11:31:22 +01:00
|
|
|
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);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_put_nv_len(buf, num_nv);
|
2012-01-24 14:02:24 +01:00
|
|
|
return bufp-buf;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_nv_del(char **nv)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
2012-02-04 15:09:03 +01:00
|
|
|
free(nv);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
char** nghttp2_frame_nv_copy(const char **nv)
|
2012-01-26 17:17:40 +01:00
|
|
|
{
|
2012-02-04 15:09:03 +01:00
|
|
|
int i;
|
|
|
|
char *buf;
|
2012-05-20 10:40:29 +02:00
|
|
|
char **idx, *data;
|
2012-02-04 15:09:03 +01:00
|
|
|
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) {
|
2012-01-26 17:17:40 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-05-20 10:40:29 +02:00
|
|
|
idx = (char**)buf;
|
2012-02-04 15:09:03 +01:00
|
|
|
data = buf+(i+1)*sizeof(char*);
|
|
|
|
|
|
|
|
for(i = 0; nv[i]; ++i) {
|
|
|
|
size_t len = strlen(nv[i])+1;
|
|
|
|
memcpy(data, nv[i], len);
|
2012-05-20 10:40:29 +02:00
|
|
|
*idx++ = data;
|
2012-02-04 15:09:03 +01:00
|
|
|
data += len;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
2012-05-20 10:40:29 +02:00
|
|
|
*idx = NULL;
|
2012-02-04 15:09:03 +01:00
|
|
|
return (char**)buf;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
static int nghttp2_string_compar(const void *lhs, const void *rhs)
|
2012-01-27 16:09:01 +01:00
|
|
|
{
|
|
|
|
return strcmp(*(char **)lhs, *(char **)rhs);
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_nv_sort(char **nv)
|
2012-01-27 16:09:01 +01:00
|
|
|
{
|
|
|
|
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);
|
2012-01-27 16:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_nv_downcase(char **nv)
|
2012-02-15 15:54:42 +01:00
|
|
|
{
|
|
|
|
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)
|
2012-02-28 15:38:40 +01:00
|
|
|
{
|
|
|
|
char **nv_copy;
|
2013-07-12 17:19:03 +02:00
|
|
|
nv_copy = nghttp2_frame_nv_copy(nv);
|
2012-02-28 15:38:40 +01:00
|
|
|
if(nv_copy != NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame_nv_downcase(nv_copy);
|
|
|
|
nghttp2_frame_nv_sort(nv_copy);
|
2012-02-28 15:38:40 +01:00
|
|
|
}
|
|
|
|
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)
|
2012-01-24 14:02:24 +01:00
|
|
|
{
|
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);
|
2012-01-24 14:02:24 +01:00
|
|
|
frame->pri = pri;
|
2013-07-19 17:08:14 +02:00
|
|
|
frame->nva = nva;
|
|
|
|
frame->nvlen = nvlen;
|
2013-07-15 14:45:59 +02:00
|
|
|
frame->cat = NGHTTP2_HCAT_START_STREAM;
|
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,
|
|
|
|
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)
|
2012-01-25 13:31:28 +01:00
|
|
|
{
|
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;
|
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-07-12 17:19:03 +02:00
|
|
|
void nghttp2_frame_settings_init(nghttp2_settings *frame,
|
|
|
|
nghttp2_settings_entry *iv, size_t niv)
|
2012-01-31 16:26:26 +01:00
|
|
|
{
|
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);
|
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);
|
|
|
|
}
|
|
|
|
|
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)
|
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
|
|
|
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;
|
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
|
|
|
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)
|
2012-01-27 10:21:14 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2012-02-24 17:17:03 +01:00
|
|
|
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);
|
|
|
|
}
|
2012-01-27 10:21:14 +01:00
|
|
|
return framelen;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_frame_unpack_headers(nghttp2_headers *frame,
|
2012-01-27 10:21:14 +01:00
|
|
|
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)
|
2012-01-27 10:21:14 +01:00
|
|
|
{
|
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,
|
2012-05-25 06:49:18 +02:00
|
|
|
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;
|
2012-05-25 06:49:18 +02:00
|
|
|
}
|
2013-07-19 17:08:14 +02:00
|
|
|
frame->nvlen = r;
|
|
|
|
return 0;
|
2012-05-25 06:49:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_frame_unpack_headers_without_nv(nghttp2_headers *frame,
|
2012-05-25 06:49:18 +02:00
|
|
|
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;
|
2012-05-25 03:46:40 +02:00
|
|
|
}
|
2013-07-19 17:08:14 +02:00
|
|
|
frame->nva = NULL;
|
|
|
|
frame->nvlen = 0;
|
2012-05-25 06:49:18 +02:00
|
|
|
return 0;
|
2012-01-27 10:21:14 +01:00
|
|
|
}
|
|
|
|
|
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)
|
2012-01-25 13:31:28 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
ssize_t framelen= NGHTTP2_FRAME_HEAD_LENGTH + 4;
|
2012-02-16 12:54:30 +01:00
|
|
|
int r;
|
2013-07-12 17:19:03 +02:00
|
|
|
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
|
2012-02-16 12:54:30 +01:00
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2012-02-16 12:54:30 +01:00
|
|
|
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);
|
2012-01-25 13:31:28 +01:00
|
|
|
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)
|
2012-01-25 13:31:28 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
if(payloadlen != 4) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_FRAME;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
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;
|
2012-01-25 13:31:28 +01:00
|
|
|
return 0;
|
2013-07-15 14:45:59 +02:00
|
|
|
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2012-01-31 16:26:26 +01: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)
|
2012-02-24 17:47:37 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + 4;
|
2012-02-24 17:47:37 +01:00
|
|
|
int r;
|
2013-07-12 17:19:03 +02:00
|
|
|
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
|
2012-02-24 17:47:37 +01: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);
|
|
|
|
nghttp2_put_uint32be(&(*buf_ptr)[8], frame->error_code);
|
2012-02-24 17:47:37 +01:00
|
|
|
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)
|
2012-02-24 17:47:37 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
if(payloadlen != 4) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_FRAME;
|
2012-02-24 17:47:37 +01:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
|
|
|
|
frame->error_code = nghttp2_get_uint32(payload);
|
2012-02-24 17:47:37 +01:00
|
|
|
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)
|
2012-01-31 16:26:26 +01:00
|
|
|
{
|
2013-07-15 14:45:59 +02:00
|
|
|
ssize_t framelen = NGHTTP2_FRAME_HEAD_LENGTH + frame->hd.length;
|
2012-03-22 18:17:48 +01:00
|
|
|
size_t i;
|
|
|
|
int r;
|
2013-07-12 17:19:03 +02:00
|
|
|
r = nghttp2_reserve_buffer(buf_ptr, buflen_ptr, framelen);
|
2012-02-16 12:54:30 +01:00
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
2012-02-16 12:54:30 +01:00
|
|
|
memset(*buf_ptr, 0, framelen);
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_pack_frame_hd(*buf_ptr, &frame->hd);
|
|
|
|
for(i = 0; i < frame->niv; ++i) {
|
|
|
|
int off = i*8;
|
|
|
|
nghttp2_put_uint32be(&(*buf_ptr)[8+off], frame->iv[i].settings_id);
|
|
|
|
nghttp2_put_uint32be(&(*buf_ptr)[12+off], frame->iv[i].value);
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
|
|
|
return framelen;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_frame_unpack_settings(nghttp2_settings *frame,
|
2012-01-31 16:26:26 +01:00
|
|
|
const uint8_t *head, size_t headlen,
|
|
|
|
const uint8_t *payload, size_t payloadlen)
|
|
|
|
{
|
2012-03-22 18:17:48 +01:00
|
|
|
size_t i;
|
2013-07-15 14:45:59 +02:00
|
|
|
if(payloadlen % 8) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_FRAME;
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_unpack_frame_hd(&frame->hd, head);
|
|
|
|
frame->niv = payloadlen / 8;
|
2013-07-12 17:19:03 +02:00
|
|
|
frame->iv = malloc(frame->niv*sizeof(nghttp2_settings_entry));
|
2012-01-31 16:26:26 +01:00
|
|
|
if(frame->iv == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
for(i = 0; i < frame->niv; ++i) {
|
|
|
|
size_t off = i*8;
|
|
|
|
frame->iv[i].settings_id = nghttp2_get_uint32(&payload[off]) &
|
|
|
|
NGHTTP2_SETTINGS_ID_MASK;
|
|
|
|
frame->iv[i].value = nghttp2_get_uint32(&payload[4+off]);
|
2012-01-31 16:26:26 +01:00
|
|
|
}
|
|
|
|
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,
|
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-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
|
|
|
}
|
2012-05-25 03:46:40 +02:00
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
ssize_t nghttp2_frame_nv_offset(const uint8_t *head)
|
2012-05-25 03:46:40 +02:00
|
|
|
{
|
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;
|
2012-05-25 03:46:40 +02:00
|
|
|
}
|
2013-07-15 14:45:59 +02:00
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
|
|
|
return NGHTTP2_FRAME_HEAD_LENGTH + 4;
|
2012-05-25 03:46:40 +02:00
|
|
|
default:
|
2013-07-15 14:45:59 +02:00
|
|
|
return -1;
|
2012-05-25 03:46:40 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-21 16:19:15 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_frame_nv_check_null(const char **nv)
|
2012-08-21 16:19:15 +02:00
|
|
|
{
|
2013-03-23 13:23:13 +01:00
|
|
|
size_t i, j;
|
2012-08-21 16:19:15 +02:00
|
|
|
for(i = 0; nv[i]; i += 2) {
|
|
|
|
if(nv[i][0] == '\0' || nv[i+1] == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-23 13:23:13 +01:00
|
|
|
for(j = 0; nv[i][j]; ++j) {
|
|
|
|
unsigned char c = nv[i][j];
|
|
|
|
if(c < 0x20 || c > 0x7e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-08-21 16:19:15 +02:00
|
|
|
}
|
|
|
|
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(nvlen == 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; 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;
|
|
|
|
}
|