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
nghttp2_frame_pack_frame_hd(uint8_t * buf,const nghttp2_frame_hd * hd)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
nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd * hd,const uint8_t * buf)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
nghttp2_frame_hd_init(nghttp2_frame_hd * hd,size_t length,uint8_t type,uint8_t flags,int32_t stream_id)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
nghttp2_frame_headers_init(nghttp2_headers * frame,uint8_t flags,int32_t stream_id,nghttp2_headers_category cat,const nghttp2_priority_spec * pri_spec,nghttp2_nv * nva,size_t nvlen)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
nghttp2_frame_headers_free(nghttp2_headers * frame,nghttp2_mem * mem)79 void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
80 nghttp2_nv_array_del(frame->nva, mem);
81 }
82
nghttp2_frame_priority_init(nghttp2_priority * frame,int32_t stream_id,const nghttp2_priority_spec * pri_spec)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
nghttp2_frame_priority_free(nghttp2_priority * frame)90 void nghttp2_frame_priority_free(nghttp2_priority *frame) { (void)frame; }
91
nghttp2_frame_rst_stream_init(nghttp2_rst_stream * frame,int32_t stream_id,uint32_t error_code)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
nghttp2_frame_rst_stream_free(nghttp2_rst_stream * frame)99 void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame) { (void)frame; }
100
nghttp2_frame_settings_init(nghttp2_settings * frame,uint8_t flags,nghttp2_settings_entry * iv,size_t niv)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
nghttp2_frame_settings_free(nghttp2_settings * frame,nghttp2_mem * mem)109 void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
110 nghttp2_mem_free(mem, frame->iv);
111 }
112
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)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
nghttp2_frame_push_promise_free(nghttp2_push_promise * frame,nghttp2_mem * mem)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
nghttp2_frame_ping_init(nghttp2_ping * frame,uint8_t flags,const uint8_t * opaque_data)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
nghttp2_frame_ping_free(nghttp2_ping * frame)140 void nghttp2_frame_ping_free(nghttp2_ping *frame) { (void)frame; }
141
nghttp2_frame_goaway_init(nghttp2_goaway * frame,int32_t last_stream_id,uint32_t error_code,uint8_t * opaque_data,size_t opaque_data_len)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
nghttp2_frame_goaway_free(nghttp2_goaway * frame,nghttp2_mem * mem)154 void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
155 nghttp2_mem_free(mem, frame->opaque_data);
156 }
157
nghttp2_frame_window_update_init(nghttp2_window_update * frame,uint8_t flags,int32_t stream_id,int32_t window_size_increment)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
nghttp2_frame_window_update_free(nghttp2_window_update * frame)166 void nghttp2_frame_window_update_free(nghttp2_window_update *frame) {
167 (void)frame;
168 }
169
nghttp2_frame_trail_padlen(nghttp2_frame * frame,size_t padlen)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
nghttp2_frame_data_init(nghttp2_data * frame,uint8_t flags,int32_t stream_id)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
nghttp2_frame_data_free(nghttp2_data * frame)188 void nghttp2_frame_data_free(nghttp2_data *frame) { (void)frame; }
189
nghttp2_frame_extension_init(nghttp2_extension * frame,uint8_t type,uint8_t flags,int32_t stream_id,void * payload)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
nghttp2_frame_extension_free(nghttp2_extension * frame)197 void nghttp2_frame_extension_free(nghttp2_extension *frame) { (void)frame; }
198
nghttp2_frame_altsvc_init(nghttp2_extension * frame,int32_t stream_id,uint8_t * origin,size_t origin_len,uint8_t * field_value,size_t field_value_len)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
nghttp2_frame_altsvc_free(nghttp2_extension * frame,nghttp2_mem * mem)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
nghttp2_frame_origin_init(nghttp2_extension * frame,nghttp2_origin_entry * ov,size_t nov)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
nghttp2_frame_origin_free(nghttp2_extension * frame,nghttp2_mem * mem)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
nghttp2_frame_priority_len(uint8_t flags)256 size_t nghttp2_frame_priority_len(uint8_t flags) {
257 if (flags & NGHTTP2_FLAG_PRIORITY) {
258 return NGHTTP2_PRIORITY_SPECLEN;
259 }
260
261 return 0;
262 }
263
nghttp2_frame_headers_payload_nv_offset(nghttp2_headers * frame)264 size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
265 return nghttp2_frame_priority_len(frame->hd.flags);
266 }
267
268 /*
269 * Call this function after payload was serialized, but not before
270 * changing buf->pos and serializing frame header.
271 *
272 * This function assumes bufs->cur points to the last buf chain of the
273 * frame(s).
274 *
275 * This function serializes frame header for HEADERS/PUSH_PROMISE and
276 * handles their successive CONTINUATION frames.
277 *
278 * We don't process any padding here.
279 */
frame_pack_headers_shared(nghttp2_bufs * bufs,nghttp2_frame_hd * frame_hd)280 static int frame_pack_headers_shared(nghttp2_bufs *bufs,
281 nghttp2_frame_hd *frame_hd) {
282 nghttp2_buf *buf;
283 nghttp2_buf_chain *ci, *ce;
284 nghttp2_frame_hd hd;
285
286 buf = &bufs->head->buf;
287
288 hd = *frame_hd;
289 hd.length = nghttp2_buf_len(buf);
290
291 DEBUGF("send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length);
292
293 /* We have multiple frame buffers, which means one or more
294 CONTINUATION frame is involved. Remove END_HEADERS flag from the
295 first frame. */
296 if (bufs->head != bufs->cur) {
297 hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
298 }
299
300 buf->pos -= NGHTTP2_FRAME_HDLEN;
301 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
302
303 if (bufs->head != bufs->cur) {
304 /* 2nd and later frames are CONTINUATION frames. */
305 hd.type = NGHTTP2_CONTINUATION;
306 /* We don't have no flags except for last CONTINUATION */
307 hd.flags = NGHTTP2_FLAG_NONE;
308
309 ce = bufs->cur;
310
311 for (ci = bufs->head->next; ci != ce; ci = ci->next) {
312 buf = &ci->buf;
313
314 hd.length = nghttp2_buf_len(buf);
315
316 DEBUGF("send: int CONTINUATION, payloadlen=%zu\n", hd.length);
317
318 buf->pos -= NGHTTP2_FRAME_HDLEN;
319 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
320 }
321
322 buf = &ci->buf;
323 hd.length = nghttp2_buf_len(buf);
324 /* Set END_HEADERS flag for last CONTINUATION */
325 hd.flags = NGHTTP2_FLAG_END_HEADERS;
326
327 DEBUGF("send: last CONTINUATION, payloadlen=%zu\n", hd.length);
328
329 buf->pos -= NGHTTP2_FRAME_HDLEN;
330 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
331 }
332
333 return 0;
334 }
335
nghttp2_frame_pack_headers(nghttp2_bufs * bufs,nghttp2_headers * frame,nghttp2_hd_deflater * deflater)336 int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
337 nghttp2_hd_deflater *deflater) {
338 size_t nv_offset;
339 int rv;
340 nghttp2_buf *buf;
341
342 assert(bufs->head == bufs->cur);
343
344 nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
345
346 buf = &bufs->cur->buf;
347
348 buf->pos += nv_offset;
349 buf->last = buf->pos;
350
351 /* This call will adjust buf->last to the correct position */
352 rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
353
354 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
355 rv = NGHTTP2_ERR_HEADER_COMP;
356 }
357
358 buf->pos -= nv_offset;
359
360 if (rv != 0) {
361 return rv;
362 }
363
364 if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
365 nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
366 }
367
368 frame->padlen = 0;
369 frame->hd.length = nghttp2_bufs_len(bufs);
370
371 return frame_pack_headers_shared(bufs, &frame->hd);
372 }
373
nghttp2_frame_pack_priority_spec(uint8_t * buf,const nghttp2_priority_spec * pri_spec)374 void nghttp2_frame_pack_priority_spec(uint8_t *buf,
375 const nghttp2_priority_spec *pri_spec) {
376 nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
377 if (pri_spec->exclusive) {
378 buf[0] |= 0x80;
379 }
380 buf[4] = (uint8_t)(pri_spec->weight - 1);
381 }
382
nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec * pri_spec,const uint8_t * payload)383 void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
384 const uint8_t *payload) {
385 int32_t dep_stream_id;
386 uint8_t exclusive;
387 int32_t weight;
388
389 dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
390 exclusive = (payload[0] & 0x80) > 0;
391 weight = payload[4] + 1;
392
393 nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
394 }
395
nghttp2_frame_unpack_headers_payload(nghttp2_headers * frame,const uint8_t * payload)396 int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
397 const uint8_t *payload) {
398 if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
399 nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
400 } else {
401 nghttp2_priority_spec_default_init(&frame->pri_spec);
402 }
403
404 frame->nva = NULL;
405 frame->nvlen = 0;
406
407 return 0;
408 }
409
nghttp2_frame_pack_priority(nghttp2_bufs * bufs,nghttp2_priority * frame)410 int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
411 nghttp2_buf *buf;
412
413 assert(bufs->head == bufs->cur);
414
415 buf = &bufs->head->buf;
416
417 assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
418
419 buf->pos -= NGHTTP2_FRAME_HDLEN;
420
421 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
422
423 nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
424
425 buf->last += NGHTTP2_PRIORITY_SPECLEN;
426
427 return 0;
428 }
429
nghttp2_frame_unpack_priority_payload(nghttp2_priority * frame,const uint8_t * payload)430 void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
431 const uint8_t *payload) {
432 nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
433 }
434
nghttp2_frame_pack_rst_stream(nghttp2_bufs * bufs,nghttp2_rst_stream * frame)435 int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
436 nghttp2_rst_stream *frame) {
437 nghttp2_buf *buf;
438
439 assert(bufs->head == bufs->cur);
440
441 buf = &bufs->head->buf;
442
443 assert(nghttp2_buf_avail(buf) >= 4);
444
445 buf->pos -= NGHTTP2_FRAME_HDLEN;
446
447 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
448
449 nghttp2_put_uint32be(buf->last, frame->error_code);
450 buf->last += 4;
451
452 return 0;
453 }
454
nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream * frame,const uint8_t * payload)455 void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
456 const uint8_t *payload) {
457 frame->error_code = nghttp2_get_uint32(payload);
458 }
459
nghttp2_frame_pack_settings(nghttp2_bufs * bufs,nghttp2_settings * frame)460 int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
461 nghttp2_buf *buf;
462
463 assert(bufs->head == bufs->cur);
464
465 buf = &bufs->head->buf;
466
467 if (nghttp2_buf_avail(buf) < frame->hd.length) {
468 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
469 }
470
471 buf->pos -= NGHTTP2_FRAME_HDLEN;
472
473 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
474
475 buf->last +=
476 nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
477
478 return 0;
479 }
480
nghttp2_frame_pack_settings_payload(uint8_t * buf,const nghttp2_settings_entry * iv,size_t niv)481 size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
482 const nghttp2_settings_entry *iv,
483 size_t niv) {
484 size_t i;
485 for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
486 nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
487 nghttp2_put_uint32be(buf + 2, iv[i].value);
488 }
489 return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
490 }
491
nghttp2_frame_unpack_settings_payload(nghttp2_settings * frame,nghttp2_settings_entry * iv,size_t niv)492 void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
493 nghttp2_settings_entry *iv,
494 size_t niv) {
495 frame->iv = iv;
496 frame->niv = niv;
497 }
498
nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry * iv,const uint8_t * payload)499 void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
500 const uint8_t *payload) {
501 iv->settings_id = nghttp2_get_uint16(&payload[0]);
502 iv->value = nghttp2_get_uint32(&payload[2]);
503 }
504
nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry ** iv_ptr,size_t * niv_ptr,const uint8_t * payload,size_t payloadlen,nghttp2_mem * mem)505 int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
506 size_t *niv_ptr,
507 const uint8_t *payload,
508 size_t payloadlen,
509 nghttp2_mem *mem) {
510 size_t i;
511
512 *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
513
514 if (*niv_ptr == 0) {
515 *iv_ptr = NULL;
516
517 return 0;
518 }
519
520 *iv_ptr =
521 nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
522
523 if (*iv_ptr == NULL) {
524 return NGHTTP2_ERR_NOMEM;
525 }
526
527 for (i = 0; i < *niv_ptr; ++i) {
528 size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
529 nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
530 }
531
532 return 0;
533 }
534
nghttp2_frame_pack_push_promise(nghttp2_bufs * bufs,nghttp2_push_promise * frame,nghttp2_hd_deflater * deflater)535 int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
536 nghttp2_push_promise *frame,
537 nghttp2_hd_deflater *deflater) {
538 size_t nv_offset = 4;
539 int rv;
540 nghttp2_buf *buf;
541
542 assert(bufs->head == bufs->cur);
543
544 buf = &bufs->cur->buf;
545
546 buf->pos += nv_offset;
547 buf->last = buf->pos;
548
549 /* This call will adjust buf->last to the correct position */
550 rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
551
552 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
553 rv = NGHTTP2_ERR_HEADER_COMP;
554 }
555
556 buf->pos -= nv_offset;
557
558 if (rv != 0) {
559 return rv;
560 }
561
562 nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
563
564 frame->padlen = 0;
565 frame->hd.length = nghttp2_bufs_len(bufs);
566
567 return frame_pack_headers_shared(bufs, &frame->hd);
568 }
569
nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise * frame,const uint8_t * payload)570 int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
571 const uint8_t *payload) {
572 frame->promised_stream_id =
573 nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
574 frame->nva = NULL;
575 frame->nvlen = 0;
576 return 0;
577 }
578
nghttp2_frame_pack_ping(nghttp2_bufs * bufs,nghttp2_ping * frame)579 int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
580 nghttp2_buf *buf;
581
582 assert(bufs->head == bufs->cur);
583
584 buf = &bufs->head->buf;
585
586 assert(nghttp2_buf_avail(buf) >= 8);
587
588 buf->pos -= NGHTTP2_FRAME_HDLEN;
589
590 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
591
592 buf->last =
593 nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
594
595 return 0;
596 }
597
nghttp2_frame_unpack_ping_payload(nghttp2_ping * frame,const uint8_t * payload)598 void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
599 const uint8_t *payload) {
600 memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
601 }
602
nghttp2_frame_pack_goaway(nghttp2_bufs * bufs,nghttp2_goaway * frame)603 int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
604 int rv;
605 nghttp2_buf *buf;
606
607 assert(bufs->head == bufs->cur);
608
609 buf = &bufs->head->buf;
610
611 buf->pos -= NGHTTP2_FRAME_HDLEN;
612
613 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
614
615 nghttp2_put_uint32be(buf->last, (uint32_t)frame->last_stream_id);
616 buf->last += 4;
617
618 nghttp2_put_uint32be(buf->last, frame->error_code);
619 buf->last += 4;
620
621 rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
622
623 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
624 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
625 }
626
627 if (rv != 0) {
628 return rv;
629 }
630
631 return 0;
632 }
633
nghttp2_frame_unpack_goaway_payload(nghttp2_goaway * frame,const uint8_t * payload,uint8_t * var_gift_payload,size_t var_gift_payloadlen)634 void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
635 const uint8_t *payload,
636 uint8_t *var_gift_payload,
637 size_t var_gift_payloadlen) {
638 frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
639 frame->error_code = nghttp2_get_uint32(payload + 4);
640
641 frame->opaque_data = var_gift_payload;
642 frame->opaque_data_len = var_gift_payloadlen;
643 }
644
nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway * frame,const uint8_t * payload,size_t payloadlen,nghttp2_mem * mem)645 int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
646 const uint8_t *payload,
647 size_t payloadlen, nghttp2_mem *mem) {
648 uint8_t *var_gift_payload;
649 size_t var_gift_payloadlen;
650
651 if (payloadlen > 8) {
652 var_gift_payloadlen = payloadlen - 8;
653 } else {
654 var_gift_payloadlen = 0;
655 }
656
657 payloadlen -= var_gift_payloadlen;
658
659 if (!var_gift_payloadlen) {
660 var_gift_payload = NULL;
661 } else {
662 var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
663
664 if (var_gift_payload == NULL) {
665 return NGHTTP2_ERR_NOMEM;
666 }
667
668 memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
669 }
670
671 nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
672 var_gift_payloadlen);
673
674 return 0;
675 }
676
nghttp2_frame_pack_window_update(nghttp2_bufs * bufs,nghttp2_window_update * frame)677 int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
678 nghttp2_window_update *frame) {
679 nghttp2_buf *buf;
680
681 assert(bufs->head == bufs->cur);
682
683 buf = &bufs->head->buf;
684
685 assert(nghttp2_buf_avail(buf) >= 4);
686
687 buf->pos -= NGHTTP2_FRAME_HDLEN;
688
689 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
690
691 nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
692 buf->last += 4;
693
694 return 0;
695 }
696
nghttp2_frame_unpack_window_update_payload(nghttp2_window_update * frame,const uint8_t * payload)697 void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
698 const uint8_t *payload) {
699 frame->window_size_increment =
700 nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
701 }
702
nghttp2_frame_pack_altsvc(nghttp2_bufs * bufs,nghttp2_extension * frame)703 int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
704 int rv;
705 nghttp2_buf *buf;
706 nghttp2_ext_altsvc *altsvc;
707
708 /* This is required with --disable-assert. */
709 (void)rv;
710
711 altsvc = frame->payload;
712
713 buf = &bufs->head->buf;
714
715 assert(nghttp2_buf_avail(buf) >=
716 2 + altsvc->origin_len + altsvc->field_value_len);
717
718 buf->pos -= NGHTTP2_FRAME_HDLEN;
719
720 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
721
722 nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
723 buf->last += 2;
724
725 rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
726
727 assert(rv == 0);
728
729 rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
730
731 assert(rv == 0);
732
733 return 0;
734 }
735
nghttp2_frame_unpack_altsvc_payload(nghttp2_extension * frame,size_t origin_len,uint8_t * payload,size_t payloadlen)736 void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
737 size_t origin_len, uint8_t *payload,
738 size_t payloadlen) {
739 nghttp2_ext_altsvc *altsvc;
740 uint8_t *p;
741
742 altsvc = frame->payload;
743 p = payload;
744
745 altsvc->origin = p;
746
747 p += origin_len;
748
749 altsvc->origin_len = origin_len;
750
751 altsvc->field_value = p;
752 altsvc->field_value_len = (size_t)(payload + payloadlen - p);
753 }
754
nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension * frame,const uint8_t * payload,size_t payloadlen,nghttp2_mem * mem)755 int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
756 const uint8_t *payload,
757 size_t payloadlen, nghttp2_mem *mem) {
758 uint8_t *buf;
759 size_t origin_len;
760
761 if (payloadlen < 2) {
762 return NGHTTP2_FRAME_SIZE_ERROR;
763 }
764
765 origin_len = nghttp2_get_uint16(payload);
766
767 buf = nghttp2_mem_malloc(mem, payloadlen - 2);
768 if (!buf) {
769 return NGHTTP2_ERR_NOMEM;
770 }
771
772 nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
773
774 nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
775
776 return 0;
777 }
778
nghttp2_frame_pack_origin(nghttp2_bufs * bufs,nghttp2_extension * frame)779 int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *frame) {
780 nghttp2_buf *buf;
781 nghttp2_ext_origin *origin;
782 nghttp2_origin_entry *orig;
783 size_t i;
784
785 origin = frame->payload;
786
787 buf = &bufs->head->buf;
788
789 if (nghttp2_buf_avail(buf) < frame->hd.length) {
790 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
791 }
792
793 buf->pos -= NGHTTP2_FRAME_HDLEN;
794
795 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
796
797 for (i = 0; i < origin->nov; ++i) {
798 orig = &origin->ov[i];
799 nghttp2_put_uint16be(buf->last, (uint16_t)orig->origin_len);
800 buf->last += 2;
801 buf->last = nghttp2_cpymem(buf->last, orig->origin, orig->origin_len);
802 }
803
804 assert(nghttp2_buf_len(buf) == NGHTTP2_FRAME_HDLEN + frame->hd.length);
805
806 return 0;
807 }
808
nghttp2_frame_unpack_origin_payload(nghttp2_extension * frame,const uint8_t * payload,size_t payloadlen,nghttp2_mem * mem)809 int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
810 const uint8_t *payload,
811 size_t payloadlen, nghttp2_mem *mem) {
812 nghttp2_ext_origin *origin;
813 const uint8_t *p, *end;
814 uint8_t *dst;
815 size_t originlen;
816 nghttp2_origin_entry *ov;
817 size_t nov = 0;
818 size_t len = 0;
819
820 origin = frame->payload;
821 p = end = payload;
822 if (payloadlen) {
823 end += payloadlen;
824 }
825
826 for (; p != end;) {
827 if (end - p < 2) {
828 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
829 }
830 originlen = nghttp2_get_uint16(p);
831 p += 2;
832 if (originlen == 0) {
833 continue;
834 }
835 if (originlen > (size_t)(end - p)) {
836 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
837 }
838 p += originlen;
839 /* 1 for terminal NULL */
840 len += originlen + 1;
841 ++nov;
842 }
843
844 if (nov == 0) {
845 origin->ov = NULL;
846 origin->nov = 0;
847
848 return 0;
849 }
850
851 len += nov * sizeof(nghttp2_origin_entry);
852
853 ov = nghttp2_mem_malloc(mem, len);
854 if (ov == NULL) {
855 return NGHTTP2_ERR_NOMEM;
856 }
857
858 origin->ov = ov;
859 origin->nov = nov;
860
861 dst = (uint8_t *)ov + nov * sizeof(nghttp2_origin_entry);
862 p = payload;
863
864 for (; p != end;) {
865 originlen = nghttp2_get_uint16(p);
866 p += 2;
867 if (originlen == 0) {
868 continue;
869 }
870 ov->origin = dst;
871 ov->origin_len = originlen;
872 dst = nghttp2_cpymem(dst, p, originlen);
873 *dst++ = '\0';
874 p += originlen;
875 ++ov;
876 }
877
878 return 0;
879 }
880
nghttp2_frame_iv_copy(const nghttp2_settings_entry * iv,size_t niv,nghttp2_mem * mem)881 nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
882 size_t niv, nghttp2_mem *mem) {
883 nghttp2_settings_entry *iv_copy;
884 size_t len = niv * sizeof(nghttp2_settings_entry);
885
886 if (len == 0) {
887 return NULL;
888 }
889
890 iv_copy = nghttp2_mem_malloc(mem, len);
891
892 if (iv_copy == NULL) {
893 return NULL;
894 }
895
896 memcpy(iv_copy, iv, len);
897
898 return iv_copy;
899 }
900
nghttp2_nv_equal(const nghttp2_nv * a,const nghttp2_nv * b)901 int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
902 if (a->namelen != b->namelen || a->valuelen != b->valuelen) {
903 return 0;
904 }
905
906 if (a->name == NULL || b->name == NULL) {
907 assert(a->namelen == 0);
908 assert(b->namelen == 0);
909 } else if (memcmp(a->name, b->name, a->namelen) != 0) {
910 return 0;
911 }
912
913 if (a->value == NULL || b->value == NULL) {
914 assert(a->valuelen == 0);
915 assert(b->valuelen == 0);
916 } else if (memcmp(a->value, b->value, a->valuelen) != 0) {
917 return 0;
918 }
919
920 return 1;
921 }
922
nghttp2_nv_array_del(nghttp2_nv * nva,nghttp2_mem * mem)923 void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
924 nghttp2_mem_free(mem, nva);
925 }
926
bytes_compar(const uint8_t * a,size_t alen,const uint8_t * b,size_t blen)927 static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
928 size_t blen) {
929 int rv;
930
931 if (alen == blen) {
932 return memcmp(a, b, alen);
933 }
934
935 if (alen < blen) {
936 rv = memcmp(a, b, alen);
937
938 if (rv == 0) {
939 return -1;
940 }
941
942 return rv;
943 }
944
945 rv = memcmp(a, b, blen);
946
947 if (rv == 0) {
948 return 1;
949 }
950
951 return rv;
952 }
953
nghttp2_nv_compare_name(const nghttp2_nv * lhs,const nghttp2_nv * rhs)954 int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
955 return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
956 }
957
nv_compar(const void * lhs,const void * rhs)958 static int nv_compar(const void *lhs, const void *rhs) {
959 const nghttp2_nv *a = (const nghttp2_nv *)lhs;
960 const nghttp2_nv *b = (const nghttp2_nv *)rhs;
961 int rv;
962
963 rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
964
965 if (rv == 0) {
966 return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
967 }
968
969 return rv;
970 }
971
nghttp2_nv_array_sort(nghttp2_nv * nva,size_t nvlen)972 void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
973 qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
974 }
975
nghttp2_nv_array_copy(nghttp2_nv ** nva_ptr,const nghttp2_nv * nva,size_t nvlen,nghttp2_mem * mem)976 int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
977 size_t nvlen, nghttp2_mem *mem) {
978 size_t i;
979 uint8_t *data = NULL;
980 size_t buflen = 0;
981 nghttp2_nv *p;
982
983 if (nvlen == 0) {
984 *nva_ptr = NULL;
985
986 return 0;
987 }
988
989 for (i = 0; i < nvlen; ++i) {
990 /* + 1 for null-termination */
991 if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
992 buflen += nva[i].namelen + 1;
993 }
994 if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
995 buflen += nva[i].valuelen + 1;
996 }
997 }
998
999 buflen += sizeof(nghttp2_nv) * nvlen;
1000
1001 *nva_ptr = nghttp2_mem_malloc(mem, buflen);
1002
1003 if (*nva_ptr == NULL) {
1004 return NGHTTP2_ERR_NOMEM;
1005 }
1006
1007 p = *nva_ptr;
1008 data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
1009
1010 for (i = 0; i < nvlen; ++i) {
1011 p->flags = nva[i].flags;
1012
1013 if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
1014 p->name = nva[i].name;
1015 p->namelen = nva[i].namelen;
1016 } else {
1017 if (nva[i].namelen) {
1018 memcpy(data, nva[i].name, nva[i].namelen);
1019 }
1020 p->name = data;
1021 p->namelen = nva[i].namelen;
1022 data[p->namelen] = '\0';
1023 nghttp2_downcase(p->name, p->namelen);
1024 data += nva[i].namelen + 1;
1025 }
1026
1027 if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
1028 p->value = nva[i].value;
1029 p->valuelen = nva[i].valuelen;
1030 } else {
1031 if (nva[i].valuelen) {
1032 memcpy(data, nva[i].value, nva[i].valuelen);
1033 }
1034 p->value = data;
1035 p->valuelen = nva[i].valuelen;
1036 data[p->valuelen] = '\0';
1037 data += nva[i].valuelen + 1;
1038 }
1039
1040 ++p;
1041 }
1042 return 0;
1043 }
1044
nghttp2_iv_check(const nghttp2_settings_entry * iv,size_t niv)1045 int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
1046 size_t i;
1047 for (i = 0; i < niv; ++i) {
1048 switch (iv[i].settings_id) {
1049 case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
1050 break;
1051 case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
1052 break;
1053 case NGHTTP2_SETTINGS_ENABLE_PUSH:
1054 if (iv[i].value != 0 && iv[i].value != 1) {
1055 return 0;
1056 }
1057 break;
1058 case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
1059 if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
1060 return 0;
1061 }
1062 break;
1063 case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
1064 if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
1065 iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
1066 return 0;
1067 }
1068 break;
1069 case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
1070 break;
1071 case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
1072 if (iv[i].value != 0 && iv[i].value != 1) {
1073 return 0;
1074 }
1075 break;
1076 }
1077 }
1078 return 1;
1079 }
1080
frame_set_pad(nghttp2_buf * buf,size_t padlen,int framehd_only)1081 static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
1082 size_t trail_padlen;
1083 size_t newlen;
1084
1085 DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
1086
1087 memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
1088
1089 --buf->pos;
1090
1091 buf->pos[4] |= NGHTTP2_FLAG_PADDED;
1092
1093 newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
1094 nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
1095
1096 if (framehd_only) {
1097 return;
1098 }
1099
1100 trail_padlen = padlen - 1;
1101 buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
1102
1103 /* zero out padding */
1104 memset(buf->last, 0, trail_padlen);
1105 /* extend buffers trail_padlen bytes, since we ate previous padlen -
1106 trail_padlen byte(s) */
1107 buf->last += trail_padlen;
1108 }
1109
nghttp2_frame_add_pad(nghttp2_bufs * bufs,nghttp2_frame_hd * hd,size_t padlen,int framehd_only)1110 int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
1111 size_t padlen, int framehd_only) {
1112 nghttp2_buf *buf;
1113
1114 if (padlen == 0) {
1115 DEBUGF("send: padlen = 0, nothing to do\n");
1116
1117 return 0;
1118 }
1119
1120 /*
1121 * We have arranged bufs like this:
1122 *
1123 * 0 1 2 3
1124 * 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
1125 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1126 * | |Frame header | Frame payload... :
1127 * +-+-----------------+-------------------------------------------+
1128 * | |Frame header | Frame payload... :
1129 * +-+-----------------+-------------------------------------------+
1130 * | |Frame header | Frame payload... :
1131 * +-+-----------------+-------------------------------------------+
1132 *
1133 * We arranged padding so that it is included in the first frame
1134 * completely. For padded frame, we are going to adjust buf->pos of
1135 * frame which includes padding and serialize (memmove) frame header
1136 * in the correct position. Also extends buf->last to include
1137 * padding.
1138 */
1139
1140 buf = &bufs->head->buf;
1141
1142 assert(nghttp2_buf_avail(buf) >= padlen - 1);
1143
1144 frame_set_pad(buf, padlen, framehd_only);
1145
1146 hd->length += padlen;
1147 hd->flags |= NGHTTP2_FLAG_PADDED;
1148
1149 DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);
1150
1151 return 0;
1152 }
1153