Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmnghttp2/lib/nghttp2_frame.c
3153 views
1
/*
2
* nghttp2 - HTTP/2 C Library
3
*
4
* Copyright (c) 2013 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_frame.h"
26
27
#include <string.h>
28
#include <assert.h>
29
#include <stdio.h>
30
#include <errno.h>
31
32
#include "nghttp2_helper.h"
33
#include "nghttp2_net.h"
34
#include "nghttp2_priority_spec.h"
35
#include "nghttp2_debug.h"
36
37
void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
38
nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
39
buf[3] = hd->type;
40
buf[4] = hd->flags;
41
nghttp2_put_uint32be(&buf[5], (uint32_t)hd->stream_id);
42
/* ignore hd->reserved for now */
43
}
44
45
void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
46
hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
47
hd->type = buf[3];
48
hd->flags = buf[4];
49
hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
50
hd->reserved = 0;
51
}
52
53
void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
54
uint8_t flags, int32_t stream_id) {
55
hd->length = length;
56
hd->type = type;
57
hd->flags = flags;
58
hd->stream_id = stream_id;
59
hd->reserved = 0;
60
}
61
62
void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
63
int32_t stream_id, nghttp2_headers_category cat,
64
const nghttp2_priority_spec *pri_spec,
65
nghttp2_nv *nva, size_t nvlen) {
66
nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
67
frame->padlen = 0;
68
frame->nva = nva;
69
frame->nvlen = nvlen;
70
frame->cat = cat;
71
72
if (pri_spec) {
73
frame->pri_spec = *pri_spec;
74
} else {
75
nghttp2_priority_spec_default_init(&frame->pri_spec);
76
}
77
}
78
79
void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
80
nghttp2_nv_array_del(frame->nva, mem);
81
}
82
83
void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
84
const nghttp2_priority_spec *pri_spec) {
85
nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
86
NGHTTP2_FLAG_NONE, stream_id);
87
frame->pri_spec = *pri_spec;
88
}
89
90
void nghttp2_frame_priority_free(nghttp2_priority *frame) { (void)frame; }
91
92
void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
93
uint32_t error_code) {
94
nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
95
stream_id);
96
frame->error_code = error_code;
97
}
98
99
void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame) { (void)frame; }
100
101
void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
102
nghttp2_settings_entry *iv, size_t niv) {
103
nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
104
NGHTTP2_SETTINGS, flags, 0);
105
frame->niv = niv;
106
frame->iv = iv;
107
}
108
109
void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
110
nghttp2_mem_free(mem, frame->iv);
111
}
112
113
void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
114
int32_t stream_id,
115
int32_t promised_stream_id,
116
nghttp2_nv *nva, size_t nvlen) {
117
nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
118
frame->padlen = 0;
119
frame->nva = nva;
120
frame->nvlen = nvlen;
121
frame->promised_stream_id = promised_stream_id;
122
frame->reserved = 0;
123
}
124
125
void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
126
nghttp2_mem *mem) {
127
nghttp2_nv_array_del(frame->nva, mem);
128
}
129
130
void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
131
const uint8_t *opaque_data) {
132
nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
133
if (opaque_data) {
134
memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
135
} else {
136
memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
137
}
138
}
139
140
void nghttp2_frame_ping_free(nghttp2_ping *frame) { (void)frame; }
141
142
void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
143
uint32_t error_code, uint8_t *opaque_data,
144
size_t opaque_data_len) {
145
nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
146
NGHTTP2_FLAG_NONE, 0);
147
frame->last_stream_id = last_stream_id;
148
frame->error_code = error_code;
149
frame->opaque_data = opaque_data;
150
frame->opaque_data_len = opaque_data_len;
151
frame->reserved = 0;
152
}
153
154
void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
155
nghttp2_mem_free(mem, frame->opaque_data);
156
}
157
158
void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
159
uint8_t flags, int32_t stream_id,
160
int32_t window_size_increment) {
161
nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
162
frame->window_size_increment = window_size_increment;
163
frame->reserved = 0;
164
}
165
166
void nghttp2_frame_window_update_free(nghttp2_window_update *frame) {
167
(void)frame;
168
}
169
170
size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
171
/* We have iframe->padlen == 0, but iframe->frame.hd.flags may have
172
NGHTTP2_FLAG_PADDED set. This happens when receiving
173
CONTINUATION frame, since we don't reset flags after HEADERS was
174
received. */
175
if (padlen == 0) {
176
return 0;
177
}
178
return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
179
}
180
181
void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
182
int32_t stream_id) {
183
/* At this moment, the length of DATA frame is unknown */
184
nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
185
frame->padlen = 0;
186
}
187
188
void nghttp2_frame_data_free(nghttp2_data *frame) { (void)frame; }
189
190
void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
191
uint8_t flags, int32_t stream_id,
192
void *payload) {
193
nghttp2_frame_hd_init(&frame->hd, 0, type, flags, stream_id);
194
frame->payload = payload;
195
}
196
197
void nghttp2_frame_extension_free(nghttp2_extension *frame) { (void)frame; }
198
199
void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
200
uint8_t *origin, size_t origin_len,
201
uint8_t *field_value, size_t field_value_len) {
202
nghttp2_ext_altsvc *altsvc;
203
204
nghttp2_frame_hd_init(&frame->hd, 2 + origin_len + field_value_len,
205
NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, stream_id);
206
207
altsvc = frame->payload;
208
altsvc->origin = origin;
209
altsvc->origin_len = origin_len;
210
altsvc->field_value = field_value;
211
altsvc->field_value_len = field_value_len;
212
}
213
214
void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem) {
215
nghttp2_ext_altsvc *altsvc;
216
217
altsvc = frame->payload;
218
if (altsvc == NULL) {
219
return;
220
}
221
/* We use the same buffer for altsvc->origin and
222
altsvc->field_value. */
223
nghttp2_mem_free(mem, altsvc->origin);
224
}
225
226
void nghttp2_frame_origin_init(nghttp2_extension *frame,
227
nghttp2_origin_entry *ov, size_t nov) {
228
nghttp2_ext_origin *origin;
229
size_t payloadlen = 0;
230
size_t i;
231
232
for (i = 0; i < nov; ++i) {
233
payloadlen += 2 + ov[i].origin_len;
234
}
235
236
nghttp2_frame_hd_init(&frame->hd, payloadlen, NGHTTP2_ORIGIN,
237
NGHTTP2_FLAG_NONE, 0);
238
239
origin = frame->payload;
240
origin->ov = ov;
241
origin->nov = nov;
242
}
243
244
void nghttp2_frame_origin_free(nghttp2_extension *frame, nghttp2_mem *mem) {
245
nghttp2_ext_origin *origin;
246
247
origin = frame->payload;
248
if (origin == NULL) {
249
return;
250
}
251
/* We use the same buffer for all resources pointed by the field of
252
origin directly or indirectly. */
253
nghttp2_mem_free(mem, origin->ov);
254
}
255
256
void nghttp2_frame_priority_update_init(nghttp2_extension *frame,
257
int32_t stream_id, uint8_t *field_value,
258
size_t field_value_len) {
259
nghttp2_ext_priority_update *priority_update;
260
261
nghttp2_frame_hd_init(&frame->hd, 4 + field_value_len,
262
NGHTTP2_PRIORITY_UPDATE, NGHTTP2_FLAG_NONE, 0);
263
264
priority_update = frame->payload;
265
priority_update->stream_id = stream_id;
266
priority_update->field_value = field_value;
267
priority_update->field_value_len = field_value_len;
268
}
269
270
void nghttp2_frame_priority_update_free(nghttp2_extension *frame,
271
nghttp2_mem *mem) {
272
nghttp2_ext_priority_update *priority_update;
273
274
priority_update = frame->payload;
275
if (priority_update == NULL) {
276
return;
277
}
278
nghttp2_mem_free(mem, priority_update->field_value);
279
}
280
281
size_t nghttp2_frame_priority_len(uint8_t flags) {
282
if (flags & NGHTTP2_FLAG_PRIORITY) {
283
return NGHTTP2_PRIORITY_SPECLEN;
284
}
285
286
return 0;
287
}
288
289
size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
290
return nghttp2_frame_priority_len(frame->hd.flags);
291
}
292
293
/*
294
* Call this function after payload was serialized, but not before
295
* changing buf->pos and serializing frame header.
296
*
297
* This function assumes bufs->cur points to the last buf chain of the
298
* frame(s).
299
*
300
* This function serializes frame header for HEADERS/PUSH_PROMISE and
301
* handles their successive CONTINUATION frames.
302
*
303
* We don't process any padding here.
304
*/
305
static int frame_pack_headers_shared(nghttp2_bufs *bufs,
306
nghttp2_frame_hd *frame_hd) {
307
nghttp2_buf *buf;
308
nghttp2_buf_chain *ci, *ce;
309
nghttp2_frame_hd hd;
310
311
buf = &bufs->head->buf;
312
313
hd = *frame_hd;
314
hd.length = nghttp2_buf_len(buf);
315
316
DEBUGF("send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length);
317
318
/* We have multiple frame buffers, which means one or more
319
CONTINUATION frame is involved. Remove END_HEADERS flag from the
320
first frame. */
321
if (bufs->head != bufs->cur) {
322
hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
323
}
324
325
buf->pos -= NGHTTP2_FRAME_HDLEN;
326
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
327
328
if (bufs->head != bufs->cur) {
329
/* 2nd and later frames are CONTINUATION frames. */
330
hd.type = NGHTTP2_CONTINUATION;
331
/* We don't have no flags except for last CONTINUATION */
332
hd.flags = NGHTTP2_FLAG_NONE;
333
334
ce = bufs->cur;
335
336
for (ci = bufs->head->next; ci != ce; ci = ci->next) {
337
buf = &ci->buf;
338
339
hd.length = nghttp2_buf_len(buf);
340
341
DEBUGF("send: int CONTINUATION, payloadlen=%zu\n", hd.length);
342
343
buf->pos -= NGHTTP2_FRAME_HDLEN;
344
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
345
}
346
347
buf = &ci->buf;
348
hd.length = nghttp2_buf_len(buf);
349
/* Set END_HEADERS flag for last CONTINUATION */
350
hd.flags = NGHTTP2_FLAG_END_HEADERS;
351
352
DEBUGF("send: last CONTINUATION, payloadlen=%zu\n", hd.length);
353
354
buf->pos -= NGHTTP2_FRAME_HDLEN;
355
nghttp2_frame_pack_frame_hd(buf->pos, &hd);
356
}
357
358
return 0;
359
}
360
361
int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
362
nghttp2_hd_deflater *deflater) {
363
size_t nv_offset;
364
int rv;
365
nghttp2_buf *buf;
366
367
assert(bufs->head == bufs->cur);
368
369
nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
370
371
buf = &bufs->cur->buf;
372
373
buf->pos += nv_offset;
374
buf->last = buf->pos;
375
376
/* This call will adjust buf->last to the correct position */
377
rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
378
379
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
380
rv = NGHTTP2_ERR_HEADER_COMP;
381
}
382
383
buf->pos -= nv_offset;
384
385
if (rv != 0) {
386
return rv;
387
}
388
389
if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
390
nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
391
}
392
393
frame->padlen = 0;
394
frame->hd.length = nghttp2_bufs_len(bufs);
395
396
return frame_pack_headers_shared(bufs, &frame->hd);
397
}
398
399
void nghttp2_frame_pack_priority_spec(uint8_t *buf,
400
const nghttp2_priority_spec *pri_spec) {
401
nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
402
if (pri_spec->exclusive) {
403
buf[0] |= 0x80;
404
}
405
buf[4] = (uint8_t)(pri_spec->weight - 1);
406
}
407
408
void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
409
const uint8_t *payload) {
410
int32_t dep_stream_id;
411
uint8_t exclusive;
412
int32_t weight;
413
414
dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
415
exclusive = (payload[0] & 0x80) > 0;
416
weight = payload[4] + 1;
417
418
nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
419
}
420
421
int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
422
const uint8_t *payload) {
423
if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
424
nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
425
} else {
426
nghttp2_priority_spec_default_init(&frame->pri_spec);
427
}
428
429
frame->nva = NULL;
430
frame->nvlen = 0;
431
432
return 0;
433
}
434
435
int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
436
nghttp2_buf *buf;
437
438
assert(bufs->head == bufs->cur);
439
440
buf = &bufs->head->buf;
441
442
assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
443
444
buf->pos -= NGHTTP2_FRAME_HDLEN;
445
446
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
447
448
nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
449
450
buf->last += NGHTTP2_PRIORITY_SPECLEN;
451
452
return 0;
453
}
454
455
void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
456
const uint8_t *payload) {
457
nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
458
}
459
460
int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
461
nghttp2_rst_stream *frame) {
462
nghttp2_buf *buf;
463
464
assert(bufs->head == bufs->cur);
465
466
buf = &bufs->head->buf;
467
468
assert(nghttp2_buf_avail(buf) >= 4);
469
470
buf->pos -= NGHTTP2_FRAME_HDLEN;
471
472
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
473
474
nghttp2_put_uint32be(buf->last, frame->error_code);
475
buf->last += 4;
476
477
return 0;
478
}
479
480
void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
481
const uint8_t *payload) {
482
frame->error_code = nghttp2_get_uint32(payload);
483
}
484
485
int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
486
nghttp2_buf *buf;
487
488
assert(bufs->head == bufs->cur);
489
490
buf = &bufs->head->buf;
491
492
if (nghttp2_buf_avail(buf) < frame->hd.length) {
493
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
494
}
495
496
buf->pos -= NGHTTP2_FRAME_HDLEN;
497
498
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
499
500
buf->last +=
501
nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
502
503
return 0;
504
}
505
506
size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
507
const nghttp2_settings_entry *iv,
508
size_t niv) {
509
size_t i;
510
for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
511
nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
512
nghttp2_put_uint32be(buf + 2, iv[i].value);
513
}
514
return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
515
}
516
517
void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
518
nghttp2_settings_entry *iv,
519
size_t niv) {
520
frame->iv = iv;
521
frame->niv = niv;
522
}
523
524
void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
525
const uint8_t *payload) {
526
iv->settings_id = nghttp2_get_uint16(&payload[0]);
527
iv->value = nghttp2_get_uint32(&payload[2]);
528
}
529
530
int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
531
size_t *niv_ptr,
532
const uint8_t *payload,
533
size_t payloadlen,
534
nghttp2_mem *mem) {
535
size_t i;
536
537
*niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
538
539
if (*niv_ptr == 0) {
540
*iv_ptr = NULL;
541
542
return 0;
543
}
544
545
*iv_ptr =
546
nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
547
548
if (*iv_ptr == NULL) {
549
return NGHTTP2_ERR_NOMEM;
550
}
551
552
for (i = 0; i < *niv_ptr; ++i) {
553
size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
554
nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
555
}
556
557
return 0;
558
}
559
560
int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
561
nghttp2_push_promise *frame,
562
nghttp2_hd_deflater *deflater) {
563
size_t nv_offset = 4;
564
int rv;
565
nghttp2_buf *buf;
566
567
assert(bufs->head == bufs->cur);
568
569
buf = &bufs->cur->buf;
570
571
buf->pos += nv_offset;
572
buf->last = buf->pos;
573
574
/* This call will adjust buf->last to the correct position */
575
rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
576
577
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
578
rv = NGHTTP2_ERR_HEADER_COMP;
579
}
580
581
buf->pos -= nv_offset;
582
583
if (rv != 0) {
584
return rv;
585
}
586
587
nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
588
589
frame->padlen = 0;
590
frame->hd.length = nghttp2_bufs_len(bufs);
591
592
return frame_pack_headers_shared(bufs, &frame->hd);
593
}
594
595
int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
596
const uint8_t *payload) {
597
frame->promised_stream_id =
598
nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
599
frame->nva = NULL;
600
frame->nvlen = 0;
601
return 0;
602
}
603
604
int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
605
nghttp2_buf *buf;
606
607
assert(bufs->head == bufs->cur);
608
609
buf = &bufs->head->buf;
610
611
assert(nghttp2_buf_avail(buf) >= 8);
612
613
buf->pos -= NGHTTP2_FRAME_HDLEN;
614
615
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
616
617
buf->last =
618
nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
619
620
return 0;
621
}
622
623
void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
624
const uint8_t *payload) {
625
memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
626
}
627
628
int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
629
int rv;
630
nghttp2_buf *buf;
631
632
assert(bufs->head == bufs->cur);
633
634
buf = &bufs->head->buf;
635
636
buf->pos -= NGHTTP2_FRAME_HDLEN;
637
638
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
639
640
nghttp2_put_uint32be(buf->last, (uint32_t)frame->last_stream_id);
641
buf->last += 4;
642
643
nghttp2_put_uint32be(buf->last, frame->error_code);
644
buf->last += 4;
645
646
rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
647
648
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
649
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
650
}
651
652
if (rv != 0) {
653
return rv;
654
}
655
656
return 0;
657
}
658
659
void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
660
const uint8_t *payload,
661
uint8_t *var_gift_payload,
662
size_t var_gift_payloadlen) {
663
frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
664
frame->error_code = nghttp2_get_uint32(payload + 4);
665
666
frame->opaque_data = var_gift_payload;
667
frame->opaque_data_len = var_gift_payloadlen;
668
}
669
670
int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
671
const uint8_t *payload,
672
size_t payloadlen, nghttp2_mem *mem) {
673
uint8_t *var_gift_payload;
674
size_t var_gift_payloadlen;
675
676
if (payloadlen > 8) {
677
var_gift_payloadlen = payloadlen - 8;
678
} else {
679
var_gift_payloadlen = 0;
680
}
681
682
if (!var_gift_payloadlen) {
683
var_gift_payload = NULL;
684
} else {
685
var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
686
687
if (var_gift_payload == NULL) {
688
return NGHTTP2_ERR_NOMEM;
689
}
690
691
memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
692
}
693
694
nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
695
var_gift_payloadlen);
696
697
return 0;
698
}
699
700
int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
701
nghttp2_window_update *frame) {
702
nghttp2_buf *buf;
703
704
assert(bufs->head == bufs->cur);
705
706
buf = &bufs->head->buf;
707
708
assert(nghttp2_buf_avail(buf) >= 4);
709
710
buf->pos -= NGHTTP2_FRAME_HDLEN;
711
712
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
713
714
nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
715
buf->last += 4;
716
717
return 0;
718
}
719
720
void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
721
const uint8_t *payload) {
722
frame->window_size_increment =
723
nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
724
}
725
726
int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
727
int rv;
728
nghttp2_buf *buf;
729
nghttp2_ext_altsvc *altsvc;
730
731
/* This is required with --disable-assert. */
732
(void)rv;
733
734
altsvc = frame->payload;
735
736
buf = &bufs->head->buf;
737
738
assert(nghttp2_buf_avail(buf) >=
739
2 + altsvc->origin_len + altsvc->field_value_len);
740
741
buf->pos -= NGHTTP2_FRAME_HDLEN;
742
743
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
744
745
nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
746
buf->last += 2;
747
748
rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
749
750
assert(rv == 0);
751
752
rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
753
754
assert(rv == 0);
755
756
return 0;
757
}
758
759
void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
760
size_t origin_len, uint8_t *payload,
761
size_t payloadlen) {
762
nghttp2_ext_altsvc *altsvc;
763
uint8_t *p;
764
765
altsvc = frame->payload;
766
p = payload;
767
768
altsvc->origin = p;
769
770
p += origin_len;
771
772
altsvc->origin_len = origin_len;
773
774
altsvc->field_value = p;
775
altsvc->field_value_len = (size_t)(payload + payloadlen - p);
776
}
777
778
int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
779
const uint8_t *payload,
780
size_t payloadlen, nghttp2_mem *mem) {
781
uint8_t *buf;
782
size_t origin_len;
783
784
if (payloadlen < 2) {
785
return NGHTTP2_FRAME_SIZE_ERROR;
786
}
787
788
origin_len = nghttp2_get_uint16(payload);
789
790
buf = nghttp2_mem_malloc(mem, payloadlen - 2);
791
if (!buf) {
792
return NGHTTP2_ERR_NOMEM;
793
}
794
795
nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
796
797
nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
798
799
return 0;
800
}
801
802
int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *frame) {
803
nghttp2_buf *buf;
804
nghttp2_ext_origin *origin;
805
nghttp2_origin_entry *orig;
806
size_t i;
807
808
origin = frame->payload;
809
810
buf = &bufs->head->buf;
811
812
if (nghttp2_buf_avail(buf) < frame->hd.length) {
813
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
814
}
815
816
buf->pos -= NGHTTP2_FRAME_HDLEN;
817
818
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
819
820
for (i = 0; i < origin->nov; ++i) {
821
orig = &origin->ov[i];
822
nghttp2_put_uint16be(buf->last, (uint16_t)orig->origin_len);
823
buf->last += 2;
824
buf->last = nghttp2_cpymem(buf->last, orig->origin, orig->origin_len);
825
}
826
827
assert(nghttp2_buf_len(buf) == NGHTTP2_FRAME_HDLEN + frame->hd.length);
828
829
return 0;
830
}
831
832
int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
833
const uint8_t *payload,
834
size_t payloadlen, nghttp2_mem *mem) {
835
nghttp2_ext_origin *origin;
836
const uint8_t *p, *end;
837
uint8_t *dst;
838
size_t originlen;
839
nghttp2_origin_entry *ov;
840
size_t nov = 0;
841
size_t len = 0;
842
843
origin = frame->payload;
844
p = end = payload;
845
if (payloadlen) {
846
end += payloadlen;
847
}
848
849
for (; p != end;) {
850
if (end - p < 2) {
851
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
852
}
853
originlen = nghttp2_get_uint16(p);
854
p += 2;
855
if (originlen == 0) {
856
continue;
857
}
858
if (originlen > (size_t)(end - p)) {
859
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
860
}
861
p += originlen;
862
/* 1 for terminal NULL */
863
len += originlen + 1;
864
++nov;
865
}
866
867
if (nov == 0) {
868
origin->ov = NULL;
869
origin->nov = 0;
870
871
return 0;
872
}
873
874
len += nov * sizeof(nghttp2_origin_entry);
875
876
ov = nghttp2_mem_malloc(mem, len);
877
if (ov == NULL) {
878
return NGHTTP2_ERR_NOMEM;
879
}
880
881
origin->ov = ov;
882
origin->nov = nov;
883
884
dst = (uint8_t *)ov + nov * sizeof(nghttp2_origin_entry);
885
p = payload;
886
887
for (; p != end;) {
888
originlen = nghttp2_get_uint16(p);
889
p += 2;
890
if (originlen == 0) {
891
continue;
892
}
893
ov->origin = dst;
894
ov->origin_len = originlen;
895
dst = nghttp2_cpymem(dst, p, originlen);
896
*dst++ = '\0';
897
p += originlen;
898
++ov;
899
}
900
901
return 0;
902
}
903
904
int nghttp2_frame_pack_priority_update(nghttp2_bufs *bufs,
905
nghttp2_extension *frame) {
906
int rv;
907
nghttp2_buf *buf;
908
nghttp2_ext_priority_update *priority_update;
909
910
/* This is required with --disable-assert. */
911
(void)rv;
912
913
priority_update = frame->payload;
914
915
buf = &bufs->head->buf;
916
917
assert(nghttp2_buf_avail(buf) >= 4 + priority_update->field_value_len);
918
919
buf->pos -= NGHTTP2_FRAME_HDLEN;
920
921
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
922
923
nghttp2_put_uint32be(buf->last, (uint32_t)priority_update->stream_id);
924
buf->last += 4;
925
926
rv = nghttp2_bufs_add(bufs, priority_update->field_value,
927
priority_update->field_value_len);
928
929
assert(rv == 0);
930
931
return 0;
932
}
933
934
void nghttp2_frame_unpack_priority_update_payload(nghttp2_extension *frame,
935
uint8_t *payload,
936
size_t payloadlen) {
937
nghttp2_ext_priority_update *priority_update;
938
939
assert(payloadlen >= 4);
940
941
priority_update = frame->payload;
942
943
priority_update->stream_id =
944
nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
945
946
if (payloadlen > 4) {
947
priority_update->field_value = payload + 4;
948
priority_update->field_value_len = payloadlen - 4;
949
} else {
950
priority_update->field_value = NULL;
951
priority_update->field_value_len = 0;
952
}
953
}
954
955
nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
956
size_t niv, nghttp2_mem *mem) {
957
nghttp2_settings_entry *iv_copy;
958
size_t len = niv * sizeof(nghttp2_settings_entry);
959
960
if (len == 0) {
961
return NULL;
962
}
963
964
iv_copy = nghttp2_mem_malloc(mem, len);
965
966
if (iv_copy == NULL) {
967
return NULL;
968
}
969
970
memcpy(iv_copy, iv, len);
971
972
return iv_copy;
973
}
974
975
int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
976
if (a->namelen != b->namelen || a->valuelen != b->valuelen) {
977
return 0;
978
}
979
980
if (a->name == NULL || b->name == NULL) {
981
assert(a->namelen == 0);
982
assert(b->namelen == 0);
983
} else if (memcmp(a->name, b->name, a->namelen) != 0) {
984
return 0;
985
}
986
987
if (a->value == NULL || b->value == NULL) {
988
assert(a->valuelen == 0);
989
assert(b->valuelen == 0);
990
} else if (memcmp(a->value, b->value, a->valuelen) != 0) {
991
return 0;
992
}
993
994
return 1;
995
}
996
997
void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
998
nghttp2_mem_free(mem, nva);
999
}
1000
1001
static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
1002
size_t blen) {
1003
int rv;
1004
1005
if (alen == blen) {
1006
return memcmp(a, b, alen);
1007
}
1008
1009
if (alen < blen) {
1010
rv = memcmp(a, b, alen);
1011
1012
if (rv == 0) {
1013
return -1;
1014
}
1015
1016
return rv;
1017
}
1018
1019
rv = memcmp(a, b, blen);
1020
1021
if (rv == 0) {
1022
return 1;
1023
}
1024
1025
return rv;
1026
}
1027
1028
int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
1029
return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
1030
}
1031
1032
static int nv_compar(const void *lhs, const void *rhs) {
1033
const nghttp2_nv *a = (const nghttp2_nv *)lhs;
1034
const nghttp2_nv *b = (const nghttp2_nv *)rhs;
1035
int rv;
1036
1037
rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
1038
1039
if (rv == 0) {
1040
return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
1041
}
1042
1043
return rv;
1044
}
1045
1046
void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
1047
qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
1048
}
1049
1050
int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
1051
size_t nvlen, nghttp2_mem *mem) {
1052
size_t i;
1053
uint8_t *data = NULL;
1054
size_t buflen = 0;
1055
nghttp2_nv *p;
1056
1057
if (nvlen == 0) {
1058
*nva_ptr = NULL;
1059
1060
return 0;
1061
}
1062
1063
for (i = 0; i < nvlen; ++i) {
1064
/* + 1 for null-termination */
1065
if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
1066
buflen += nva[i].namelen + 1;
1067
}
1068
if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
1069
buflen += nva[i].valuelen + 1;
1070
}
1071
}
1072
1073
buflen += sizeof(nghttp2_nv) * nvlen;
1074
1075
*nva_ptr = nghttp2_mem_malloc(mem, buflen);
1076
1077
if (*nva_ptr == NULL) {
1078
return NGHTTP2_ERR_NOMEM;
1079
}
1080
1081
p = *nva_ptr;
1082
data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
1083
1084
for (i = 0; i < nvlen; ++i) {
1085
p->flags = nva[i].flags;
1086
1087
if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
1088
p->name = nva[i].name;
1089
p->namelen = nva[i].namelen;
1090
} else {
1091
if (nva[i].namelen) {
1092
memcpy(data, nva[i].name, nva[i].namelen);
1093
}
1094
p->name = data;
1095
p->namelen = nva[i].namelen;
1096
data[p->namelen] = '\0';
1097
nghttp2_downcase(p->name, p->namelen);
1098
data += nva[i].namelen + 1;
1099
}
1100
1101
if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
1102
p->value = nva[i].value;
1103
p->valuelen = nva[i].valuelen;
1104
} else {
1105
if (nva[i].valuelen) {
1106
memcpy(data, nva[i].value, nva[i].valuelen);
1107
}
1108
p->value = data;
1109
p->valuelen = nva[i].valuelen;
1110
data[p->valuelen] = '\0';
1111
data += nva[i].valuelen + 1;
1112
}
1113
1114
++p;
1115
}
1116
return 0;
1117
}
1118
1119
int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
1120
size_t i;
1121
for (i = 0; i < niv; ++i) {
1122
switch (iv[i].settings_id) {
1123
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
1124
break;
1125
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
1126
break;
1127
case NGHTTP2_SETTINGS_ENABLE_PUSH:
1128
if (iv[i].value != 0 && iv[i].value != 1) {
1129
return 0;
1130
}
1131
break;
1132
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
1133
if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
1134
return 0;
1135
}
1136
break;
1137
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
1138
if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
1139
iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
1140
return 0;
1141
}
1142
break;
1143
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
1144
break;
1145
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
1146
if (iv[i].value != 0 && iv[i].value != 1) {
1147
return 0;
1148
}
1149
break;
1150
case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
1151
if (iv[i].value != 0 && iv[i].value != 1) {
1152
return 0;
1153
}
1154
break;
1155
}
1156
}
1157
return 1;
1158
}
1159
1160
static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
1161
size_t trail_padlen;
1162
size_t newlen;
1163
1164
DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
1165
1166
memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
1167
1168
--buf->pos;
1169
1170
buf->pos[4] |= NGHTTP2_FLAG_PADDED;
1171
1172
newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
1173
nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
1174
1175
if (framehd_only) {
1176
return;
1177
}
1178
1179
trail_padlen = padlen - 1;
1180
buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
1181
1182
/* zero out padding */
1183
memset(buf->last, 0, trail_padlen);
1184
/* extend buffers trail_padlen bytes, since we ate previous padlen -
1185
trail_padlen byte(s) */
1186
buf->last += trail_padlen;
1187
}
1188
1189
int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
1190
size_t padlen, int framehd_only) {
1191
nghttp2_buf *buf;
1192
1193
if (padlen == 0) {
1194
DEBUGF("send: padlen = 0, nothing to do\n");
1195
1196
return 0;
1197
}
1198
1199
/*
1200
* We have arranged bufs like this:
1201
*
1202
* 0 1 2 3
1203
* 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
1204
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1205
* | |Frame header | Frame payload... :
1206
* +-+-----------------+-------------------------------------------+
1207
* | |Frame header | Frame payload... :
1208
* +-+-----------------+-------------------------------------------+
1209
* | |Frame header | Frame payload... :
1210
* +-+-----------------+-------------------------------------------+
1211
*
1212
* We arranged padding so that it is included in the first frame
1213
* completely. For padded frame, we are going to adjust buf->pos of
1214
* frame which includes padding and serialize (memmove) frame header
1215
* in the correct position. Also extends buf->last to include
1216
* padding.
1217
*/
1218
1219
buf = &bufs->head->buf;
1220
1221
assert(nghttp2_buf_avail(buf) >= padlen - 1);
1222
1223
frame_set_pad(buf, padlen, framehd_only);
1224
1225
hd->length += padlen;
1226
hd->flags |= NGHTTP2_FLAG_PADDED;
1227
1228
DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);
1229
1230
return 0;
1231
}
1232
1233