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