2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "nghttp2_test_helper.h"
29 #include <CUnit/CUnit.h>
31 #include "nghttp2_helper.h"
32 #include "nghttp2_priority_spec.h"
34 int unpack_framebuf(nghttp2_frame *frame, nghttp2_bufs *bufs) {
37 /* Assuming we have required data in first buffer. We don't decode
38 header block so, we don't mind its space */
39 buf = &bufs->head->buf;
40 return unpack_frame(frame, buf->pos, nghttp2_buf_len(buf));
43 int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) {
45 const uint8_t *payload = in + NGHTTP2_FRAME_HDLEN;
46 size_t payloadlen = len - NGHTTP2_FRAME_HDLEN;
50 mem = nghttp2_mem_default();
52 nghttp2_frame_unpack_frame_hd(&frame->hd, in);
53 switch (frame->hd.type) {
55 payloadoff = ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
56 rv = nghttp2_frame_unpack_headers_payload(
57 &frame->headers, payload + payloadoff, payloadlen - payloadoff);
59 case NGHTTP2_PRIORITY:
60 nghttp2_frame_unpack_priority_payload(&frame->priority, payload,
63 case NGHTTP2_RST_STREAM:
64 nghttp2_frame_unpack_rst_stream_payload(&frame->rst_stream, payload,
67 case NGHTTP2_SETTINGS:
68 rv = nghttp2_frame_unpack_settings_payload2(
69 &frame->settings.iv, &frame->settings.niv, payload, payloadlen, mem);
71 case NGHTTP2_PUSH_PROMISE:
72 rv = nghttp2_frame_unpack_push_promise_payload(&frame->push_promise,
76 nghttp2_frame_unpack_ping_payload(&frame->ping, payload, payloadlen);
79 nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen,
82 case NGHTTP2_WINDOW_UPDATE:
83 nghttp2_frame_unpack_window_update_payload(&frame->window_update, payload,
87 /* Must not be reachable */
93 int strmemeq(const char *a, const uint8_t *b, size_t bn) {
99 for (; *a && b != c && *a == *b; ++a, ++b)
101 return !*a && b == c;
104 int nvnameeq(const char *a, nghttp2_nv *nv) {
105 return strmemeq(a, nv->name, nv->namelen);
108 int nvvalueeq(const char *a, nghttp2_nv *nv) {
109 return strmemeq(a, nv->value, nv->valuelen);
112 void nva_out_init(nva_out *out) {
113 memset(out->nva, 0, sizeof(out->nva));
117 void nva_out_reset(nva_out *out) {
119 for (i = 0; i < out->nvlen; ++i) {
120 free(out->nva[i].name);
121 free(out->nva[i].value);
123 memset(out->nva, 0, sizeof(out->nva));
127 void add_out(nva_out *out, nghttp2_nv *nv) {
128 nghttp2_nv *onv = &out->nva[out->nvlen];
130 onv->name = malloc(nv->namelen);
131 memcpy(onv->name, nv->name, nv->namelen);
136 onv->value = malloc(nv->valuelen);
137 memcpy(onv->value, nv->value, nv->valuelen);
141 onv->namelen = nv->namelen;
142 onv->valuelen = nv->valuelen;
144 onv->flags = nv->flags;
149 ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
150 nghttp2_bufs *bufs, size_t offset) {
154 nghttp2_buf_chain *ci;
162 for (ci = bufs->head; ci; ci = ci->next) {
164 final = nghttp2_buf_len(buf) == 0 || ci->next == NULL;
170 n = nghttp2_min((ssize_t)offset, nghttp2_buf_len(&bp));
177 rv = nghttp2_hd_inflate_hd(inflater, &nv, &inflate_flags, bp.pos,
178 nghttp2_buf_len(&bp), final);
187 if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
192 if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
198 nghttp2_hd_inflate_end_headers(inflater);
203 int frame_pack_bufs_init(nghttp2_bufs *bufs) {
204 /* 1 for Pad Length */
205 return nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 1,
206 nghttp2_mem_default());
209 void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size) {
210 /* 1 for Pad Length */
211 nghttp2_bufs_init2(bufs, chunk_size, 16, NGHTTP2_FRAME_HDLEN + 1,
212 nghttp2_mem_default());
215 static nghttp2_stream *open_stream_with_all(nghttp2_session *session,
216 int32_t stream_id, int32_t weight,
218 nghttp2_stream *dep_stream) {
219 nghttp2_priority_spec pri_spec;
220 int32_t dep_stream_id;
223 dep_stream_id = dep_stream->stream_id;
228 nghttp2_priority_spec_init(&pri_spec, dep_stream_id, weight, exclusive);
230 return nghttp2_session_open_stream(session, stream_id,
231 NGHTTP2_STREAM_FLAG_NONE, &pri_spec,
232 NGHTTP2_STREAM_OPENED, NULL);
235 nghttp2_stream *open_stream(nghttp2_session *session, int32_t stream_id) {
236 return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 0,
240 nghttp2_stream *open_stream_with_dep(nghttp2_session *session,
242 nghttp2_stream *dep_stream) {
243 return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 0,
247 nghttp2_stream *open_stream_with_dep_weight(nghttp2_session *session,
248 int32_t stream_id, int32_t weight,
249 nghttp2_stream *dep_stream) {
250 return open_stream_with_all(session, stream_id, weight, 0, dep_stream);
253 nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session,
255 nghttp2_stream *dep_stream) {
256 return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 1,
260 nghttp2_outbound_item *create_data_ob_item(void) {
261 nghttp2_outbound_item *item;
263 item = malloc(sizeof(nghttp2_outbound_item));
264 memset(item, 0, sizeof(nghttp2_outbound_item));