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