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