1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5 *
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:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
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.
24 */
25 #include "nghttp2_test_helper.h"
26
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include <CUnit/CUnit.h>
31
32 #include "nghttp2_helper.h"
33 #include "nghttp2_priority_spec.h"
34
unpack_framebuf(nghttp2_frame * frame,nghttp2_bufs * bufs)35 int unpack_framebuf(nghttp2_frame *frame, nghttp2_bufs *bufs) {
36 nghttp2_buf *buf;
37
38 /* Assuming we have required data in first buffer. We don't decode
39 header block so, we don't mind its space */
40 buf = &bufs->head->buf;
41 return unpack_frame(frame, buf->pos, nghttp2_buf_len(buf));
42 }
43
unpack_frame(nghttp2_frame * frame,const uint8_t * in,size_t len)44 int unpack_frame(nghttp2_frame *frame, const uint8_t *in, size_t len) {
45 int rv = 0;
46 const uint8_t *payload = in + NGHTTP2_FRAME_HDLEN;
47 size_t payloadlen = len - NGHTTP2_FRAME_HDLEN;
48 size_t payloadoff;
49 nghttp2_mem *mem;
50
51 mem = nghttp2_mem_default();
52
53 nghttp2_frame_unpack_frame_hd(&frame->hd, in);
54 switch (frame->hd.type) {
55 case NGHTTP2_HEADERS:
56 payloadoff = ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
57 rv = nghttp2_frame_unpack_headers_payload(&frame->headers,
58 payload + payloadoff);
59 break;
60 case NGHTTP2_PRIORITY:
61 nghttp2_frame_unpack_priority_payload(&frame->priority, payload);
62 break;
63 case NGHTTP2_RST_STREAM:
64 nghttp2_frame_unpack_rst_stream_payload(&frame->rst_stream, payload);
65 break;
66 case NGHTTP2_SETTINGS:
67 rv = nghttp2_frame_unpack_settings_payload2(
68 &frame->settings.iv, &frame->settings.niv, payload, payloadlen, mem);
69 break;
70 case NGHTTP2_PUSH_PROMISE:
71 rv = nghttp2_frame_unpack_push_promise_payload(&frame->push_promise,
72 payload);
73 break;
74 case NGHTTP2_PING:
75 nghttp2_frame_unpack_ping_payload(&frame->ping, payload);
76 break;
77 case NGHTTP2_GOAWAY:
78 nghttp2_frame_unpack_goaway_payload2(&frame->goaway, payload, payloadlen,
79 mem);
80 break;
81 case NGHTTP2_WINDOW_UPDATE:
82 nghttp2_frame_unpack_window_update_payload(&frame->window_update, payload);
83 break;
84 case NGHTTP2_ALTSVC:
85 assert(payloadlen > 2);
86 nghttp2_frame_unpack_altsvc_payload2(&frame->ext, payload, payloadlen, mem);
87 break;
88 case NGHTTP2_ORIGIN:
89 rv = nghttp2_frame_unpack_origin_payload(&frame->ext, payload, payloadlen,
90 mem);
91 break;
92 case NGHTTP2_PRIORITY_UPDATE:
93 assert(payloadlen >= 4);
94 nghttp2_frame_unpack_priority_update_payload(
95 &frame->ext, (uint8_t *)payload, payloadlen);
96 break;
97 default:
98 /* Must not be reachable */
99 assert(0);
100 }
101 return rv;
102 }
103
strmemeq(const char * a,const uint8_t * b,size_t bn)104 int strmemeq(const char *a, const uint8_t *b, size_t bn) {
105 const uint8_t *c;
106 if (!a || !b) {
107 return 0;
108 }
109 c = b + bn;
110 for (; *a && b != c && *a == *b; ++a, ++b)
111 ;
112 return !*a && b == c;
113 }
114
nvnameeq(const char * a,nghttp2_nv * nv)115 int nvnameeq(const char *a, nghttp2_nv *nv) {
116 return strmemeq(a, nv->name, nv->namelen);
117 }
118
nvvalueeq(const char * a,nghttp2_nv * nv)119 int nvvalueeq(const char *a, nghttp2_nv *nv) {
120 return strmemeq(a, nv->value, nv->valuelen);
121 }
122
nva_out_init(nva_out * out)123 void nva_out_init(nva_out *out) {
124 memset(out->nva, 0, sizeof(out->nva));
125 out->nvlen = 0;
126 }
127
nva_out_reset(nva_out * out,nghttp2_mem * mem)128 void nva_out_reset(nva_out *out, nghttp2_mem *mem) {
129 size_t i;
130 for (i = 0; i < out->nvlen; ++i) {
131 mem->free(out->nva[i].name, NULL);
132 mem->free(out->nva[i].value, NULL);
133 }
134 memset(out->nva, 0, sizeof(out->nva));
135 out->nvlen = 0;
136 }
137
add_out(nva_out * out,nghttp2_nv * nv,nghttp2_mem * mem)138 void add_out(nva_out *out, nghttp2_nv *nv, nghttp2_mem *mem) {
139 nghttp2_nv *onv = &out->nva[out->nvlen];
140 if (nv->namelen) {
141 onv->name = mem->malloc(nv->namelen, NULL);
142 memcpy(onv->name, nv->name, nv->namelen);
143 } else {
144 onv->name = NULL;
145 }
146 if (nv->valuelen) {
147 onv->value = mem->malloc(nv->valuelen, NULL);
148 memcpy(onv->value, nv->value, nv->valuelen);
149 } else {
150 onv->value = NULL;
151 }
152 onv->namelen = nv->namelen;
153 onv->valuelen = nv->valuelen;
154
155 onv->flags = nv->flags;
156
157 ++out->nvlen;
158 }
159
inflate_hd(nghttp2_hd_inflater * inflater,nva_out * out,nghttp2_bufs * bufs,size_t offset,nghttp2_mem * mem)160 ssize_t inflate_hd(nghttp2_hd_inflater *inflater, nva_out *out,
161 nghttp2_bufs *bufs, size_t offset, nghttp2_mem *mem) {
162 ssize_t rv;
163 nghttp2_nv nv;
164 int inflate_flags;
165 nghttp2_buf_chain *ci;
166 nghttp2_buf *buf;
167 nghttp2_buf bp;
168 int fin;
169 size_t processed;
170
171 processed = 0;
172
173 for (ci = bufs->head; ci; ci = ci->next) {
174 buf = &ci->buf;
175 fin = nghttp2_buf_len(buf) == 0 || ci->next == NULL;
176 bp = *buf;
177
178 if (offset) {
179 size_t n;
180
181 n = nghttp2_min(offset, nghttp2_buf_len(&bp));
182 bp.pos += n;
183 offset -= n;
184 }
185
186 for (;;) {
187 inflate_flags = 0;
188 rv = nghttp2_hd_inflate_hd2(inflater, &nv, &inflate_flags, bp.pos,
189 nghttp2_buf_len(&bp), fin);
190
191 if (rv < 0) {
192 return rv;
193 }
194
195 bp.pos += rv;
196 processed += (size_t)rv;
197
198 if (inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
199 if (out) {
200 add_out(out, &nv, mem);
201 }
202 }
203 if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
204 break;
205 }
206 if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
207 nghttp2_buf_len(&bp) == 0) {
208 break;
209 }
210 }
211 }
212
213 nghttp2_hd_inflate_end_headers(inflater);
214
215 return (ssize_t)processed;
216 }
217
pack_headers(nghttp2_bufs * bufs,nghttp2_hd_deflater * deflater,int32_t stream_id,uint8_t flags,const nghttp2_nv * nva,size_t nvlen,nghttp2_mem * mem)218 int pack_headers(nghttp2_bufs *bufs, nghttp2_hd_deflater *deflater,
219 int32_t stream_id, uint8_t flags, const nghttp2_nv *nva,
220 size_t nvlen, nghttp2_mem *mem) {
221 nghttp2_nv *dnva;
222 nghttp2_frame frame;
223 int rv;
224
225 nghttp2_nv_array_copy(&dnva, nva, nvlen, mem);
226
227 nghttp2_frame_headers_init(&frame.headers, flags, stream_id,
228 NGHTTP2_HCAT_HEADERS, NULL, dnva, nvlen);
229 rv = nghttp2_frame_pack_headers(bufs, &frame.headers, deflater);
230
231 nghttp2_frame_headers_free(&frame.headers, mem);
232
233 return rv;
234 }
235
pack_push_promise(nghttp2_bufs * bufs,nghttp2_hd_deflater * deflater,int32_t stream_id,uint8_t flags,int32_t promised_stream_id,const nghttp2_nv * nva,size_t nvlen,nghttp2_mem * mem)236 int pack_push_promise(nghttp2_bufs *bufs, nghttp2_hd_deflater *deflater,
237 int32_t stream_id, uint8_t flags,
238 int32_t promised_stream_id, const nghttp2_nv *nva,
239 size_t nvlen, nghttp2_mem *mem) {
240 nghttp2_nv *dnva;
241 nghttp2_frame frame;
242 int rv;
243
244 nghttp2_nv_array_copy(&dnva, nva, nvlen, mem);
245
246 nghttp2_frame_push_promise_init(&frame.push_promise, flags, stream_id,
247 promised_stream_id, dnva, nvlen);
248 rv = nghttp2_frame_pack_push_promise(bufs, &frame.push_promise, deflater);
249
250 nghttp2_frame_push_promise_free(&frame.push_promise, mem);
251
252 return rv;
253 }
254
frame_pack_bufs_init(nghttp2_bufs * bufs)255 int frame_pack_bufs_init(nghttp2_bufs *bufs) {
256 /* 1 for Pad Length */
257 return nghttp2_bufs_init2(bufs, 4096, 16, NGHTTP2_FRAME_HDLEN + 1,
258 nghttp2_mem_default());
259 }
260
bufs_large_init(nghttp2_bufs * bufs,size_t chunk_size)261 void bufs_large_init(nghttp2_bufs *bufs, size_t chunk_size) {
262 /* 1 for Pad Length */
263 nghttp2_bufs_init2(bufs, chunk_size, 16, NGHTTP2_FRAME_HDLEN + 1,
264 nghttp2_mem_default());
265 }
266
open_stream_with_all(nghttp2_session * session,int32_t stream_id,int32_t weight,uint8_t exclusive,nghttp2_stream * dep_stream)267 static nghttp2_stream *open_stream_with_all(nghttp2_session *session,
268 int32_t stream_id, int32_t weight,
269 uint8_t exclusive,
270 nghttp2_stream *dep_stream) {
271 nghttp2_priority_spec pri_spec;
272 int32_t dep_stream_id;
273
274 if (dep_stream) {
275 dep_stream_id = dep_stream->stream_id;
276 } else {
277 dep_stream_id = 0;
278 }
279
280 nghttp2_priority_spec_init(&pri_spec, dep_stream_id, weight, exclusive);
281
282 return nghttp2_session_open_stream(session, stream_id,
283 NGHTTP2_STREAM_FLAG_NONE, &pri_spec,
284 NGHTTP2_STREAM_OPENED, NULL);
285 }
286
open_stream(nghttp2_session * session,int32_t stream_id)287 nghttp2_stream *open_stream(nghttp2_session *session, int32_t stream_id) {
288 return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 0,
289 NULL);
290 }
291
open_stream_with_dep(nghttp2_session * session,int32_t stream_id,nghttp2_stream * dep_stream)292 nghttp2_stream *open_stream_with_dep(nghttp2_session *session,
293 int32_t stream_id,
294 nghttp2_stream *dep_stream) {
295 return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 0,
296 dep_stream);
297 }
298
open_stream_with_dep_weight(nghttp2_session * session,int32_t stream_id,int32_t weight,nghttp2_stream * dep_stream)299 nghttp2_stream *open_stream_with_dep_weight(nghttp2_session *session,
300 int32_t stream_id, int32_t weight,
301 nghttp2_stream *dep_stream) {
302 return open_stream_with_all(session, stream_id, weight, 0, dep_stream);
303 }
304
open_stream_with_dep_excl(nghttp2_session * session,int32_t stream_id,nghttp2_stream * dep_stream)305 nghttp2_stream *open_stream_with_dep_excl(nghttp2_session *session,
306 int32_t stream_id,
307 nghttp2_stream *dep_stream) {
308 return open_stream_with_all(session, stream_id, NGHTTP2_DEFAULT_WEIGHT, 1,
309 dep_stream);
310 }
311
create_data_ob_item(nghttp2_mem * mem)312 nghttp2_outbound_item *create_data_ob_item(nghttp2_mem *mem) {
313 nghttp2_outbound_item *item;
314
315 item = mem->malloc(sizeof(nghttp2_outbound_item), NULL);
316 memset(item, 0, sizeof(nghttp2_outbound_item));
317
318 return item;
319 }
320
open_sent_stream(nghttp2_session * session,int32_t stream_id)321 nghttp2_stream *open_sent_stream(nghttp2_session *session, int32_t stream_id) {
322 nghttp2_priority_spec pri_spec;
323
324 nghttp2_priority_spec_init(&pri_spec, 0, NGHTTP2_DEFAULT_WEIGHT, 0);
325 return open_sent_stream3(session, stream_id, NGHTTP2_FLAG_NONE, &pri_spec,
326 NGHTTP2_STREAM_OPENED, NULL);
327 }
328
open_sent_stream2(nghttp2_session * session,int32_t stream_id,nghttp2_stream_state initial_state)329 nghttp2_stream *open_sent_stream2(nghttp2_session *session, int32_t stream_id,
330 nghttp2_stream_state initial_state) {
331 nghttp2_priority_spec pri_spec;
332
333 nghttp2_priority_spec_init(&pri_spec, 0, NGHTTP2_DEFAULT_WEIGHT, 0);
334 return open_sent_stream3(session, stream_id, NGHTTP2_FLAG_NONE, &pri_spec,
335 initial_state, NULL);
336 }
337
open_sent_stream3(nghttp2_session * session,int32_t stream_id,uint8_t flags,nghttp2_priority_spec * pri_spec_in,nghttp2_stream_state initial_state,void * stream_user_data)338 nghttp2_stream *open_sent_stream3(nghttp2_session *session, int32_t stream_id,
339 uint8_t flags,
340 nghttp2_priority_spec *pri_spec_in,
341 nghttp2_stream_state initial_state,
342 void *stream_user_data) {
343 nghttp2_stream *stream;
344
345 assert(nghttp2_session_is_my_stream_id(session, stream_id));
346
347 stream = nghttp2_session_open_stream(session, stream_id, flags, pri_spec_in,
348 initial_state, stream_user_data);
349 session->last_sent_stream_id =
350 nghttp2_max(session->last_sent_stream_id, stream_id);
351 session->next_stream_id =
352 nghttp2_max(session->next_stream_id, (uint32_t)stream_id + 2);
353
354 return stream;
355 }
356
open_sent_stream_with_dep(nghttp2_session * session,int32_t stream_id,nghttp2_stream * dep_stream)357 nghttp2_stream *open_sent_stream_with_dep(nghttp2_session *session,
358 int32_t stream_id,
359 nghttp2_stream *dep_stream) {
360 return open_sent_stream_with_dep_weight(session, stream_id,
361 NGHTTP2_DEFAULT_WEIGHT, dep_stream);
362 }
363
open_sent_stream_with_dep_weight(nghttp2_session * session,int32_t stream_id,int32_t weight,nghttp2_stream * dep_stream)364 nghttp2_stream *open_sent_stream_with_dep_weight(nghttp2_session *session,
365 int32_t stream_id,
366 int32_t weight,
367 nghttp2_stream *dep_stream) {
368 nghttp2_stream *stream;
369
370 assert(nghttp2_session_is_my_stream_id(session, stream_id));
371
372 stream = open_stream_with_all(session, stream_id, weight, 0, dep_stream);
373
374 session->last_sent_stream_id =
375 nghttp2_max(session->last_sent_stream_id, stream_id);
376 session->next_stream_id =
377 nghttp2_max(session->next_stream_id, (uint32_t)stream_id + 2);
378
379 return stream;
380 }
381
open_recv_stream(nghttp2_session * session,int32_t stream_id)382 nghttp2_stream *open_recv_stream(nghttp2_session *session, int32_t stream_id) {
383 nghttp2_priority_spec pri_spec;
384
385 nghttp2_priority_spec_init(&pri_spec, 0, NGHTTP2_DEFAULT_WEIGHT, 0);
386 return open_recv_stream3(session, stream_id, NGHTTP2_FLAG_NONE, &pri_spec,
387 NGHTTP2_STREAM_OPENED, NULL);
388 }
389
open_recv_stream2(nghttp2_session * session,int32_t stream_id,nghttp2_stream_state initial_state)390 nghttp2_stream *open_recv_stream2(nghttp2_session *session, int32_t stream_id,
391 nghttp2_stream_state initial_state) {
392 nghttp2_priority_spec pri_spec;
393
394 nghttp2_priority_spec_init(&pri_spec, 0, NGHTTP2_DEFAULT_WEIGHT, 0);
395 return open_recv_stream3(session, stream_id, NGHTTP2_FLAG_NONE, &pri_spec,
396 initial_state, NULL);
397 }
398
open_recv_stream3(nghttp2_session * session,int32_t stream_id,uint8_t flags,nghttp2_priority_spec * pri_spec_in,nghttp2_stream_state initial_state,void * stream_user_data)399 nghttp2_stream *open_recv_stream3(nghttp2_session *session, int32_t stream_id,
400 uint8_t flags,
401 nghttp2_priority_spec *pri_spec_in,
402 nghttp2_stream_state initial_state,
403 void *stream_user_data) {
404 nghttp2_stream *stream;
405
406 assert(!nghttp2_session_is_my_stream_id(session, stream_id));
407
408 stream = nghttp2_session_open_stream(session, stream_id, flags, pri_spec_in,
409 initial_state, stream_user_data);
410 session->last_recv_stream_id =
411 nghttp2_max(session->last_recv_stream_id, stream_id);
412
413 return stream;
414 }
415
open_recv_stream_with_dep(nghttp2_session * session,int32_t stream_id,nghttp2_stream * dep_stream)416 nghttp2_stream *open_recv_stream_with_dep(nghttp2_session *session,
417 int32_t stream_id,
418 nghttp2_stream *dep_stream) {
419 return open_recv_stream_with_dep_weight(session, stream_id,
420 NGHTTP2_DEFAULT_WEIGHT, dep_stream);
421 }
422
open_recv_stream_with_dep_weight(nghttp2_session * session,int32_t stream_id,int32_t weight,nghttp2_stream * dep_stream)423 nghttp2_stream *open_recv_stream_with_dep_weight(nghttp2_session *session,
424 int32_t stream_id,
425 int32_t weight,
426 nghttp2_stream *dep_stream) {
427 nghttp2_stream *stream;
428
429 assert(!nghttp2_session_is_my_stream_id(session, stream_id));
430
431 stream = open_stream_with_all(session, stream_id, weight, 0, dep_stream);
432
433 session->last_recv_stream_id =
434 nghttp2_max(session->last_recv_stream_id, stream_id);
435
436 return stream;
437 }
438