1/*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 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#ifndef NGHTTP2_FRAME_H
26#define NGHTTP2_FRAME_H
27
28#ifdef HAVE_CONFIG_H
29#  include <config.h>
30#endif /* HAVE_CONFIG_H */
31
32#include <nghttp2/nghttp2.h>
33#include "nghttp2_hd.h"
34#include "nghttp2_buf.h"
35
36#define NGHTTP2_STREAM_ID_MASK ((1u << 31) - 1)
37#define NGHTTP2_PRI_GROUP_ID_MASK ((1u << 31) - 1)
38#define NGHTTP2_PRIORITY_MASK ((1u << 31) - 1)
39#define NGHTTP2_WINDOW_SIZE_INCREMENT_MASK ((1u << 31) - 1)
40#define NGHTTP2_SETTINGS_ID_MASK ((1 << 24) - 1)
41
42/* The number of bytes of frame header. */
43#define NGHTTP2_FRAME_HDLEN 9
44
45#define NGHTTP2_MAX_FRAME_SIZE_MAX ((1 << 24) - 1)
46#define NGHTTP2_MAX_FRAME_SIZE_MIN (1 << 14)
47
48#define NGHTTP2_MAX_PAYLOADLEN 16384
49/* The one frame buffer length for transmission.  We may use several of
50   them to support CONTINUATION.  To account for Pad Length field, we
51   allocate extra 1 byte, which saves extra large memcopying. */
52#define NGHTTP2_FRAMEBUF_CHUNKLEN                                              \
53  (NGHTTP2_FRAME_HDLEN + 1 + NGHTTP2_MAX_PAYLOADLEN)
54
55/* The default length of DATA frame payload. */
56#define NGHTTP2_DATA_PAYLOADLEN NGHTTP2_MAX_FRAME_SIZE_MIN
57
58/* Maximum headers block size to send, calculated using
59   nghttp2_hd_deflate_bound().  This is the default value, and can be
60   overridden by nghttp2_option_set_max_send_header_block_length(). */
61#define NGHTTP2_MAX_HEADERSLEN 65536
62
63/* The number of bytes for each SETTINGS entry */
64#define NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH 6
65
66/* Length of priority related fields in HEADERS/PRIORITY frames */
67#define NGHTTP2_PRIORITY_SPECLEN 5
68
69/* Maximum length of padding in bytes. */
70#define NGHTTP2_MAX_PADLEN 256
71
72/* Union of extension frame payload */
73typedef union {
74  nghttp2_ext_altsvc altsvc;
75  nghttp2_ext_origin origin;
76  nghttp2_ext_priority_update priority_update;
77} nghttp2_ext_frame_payload;
78
79void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd);
80
81void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf);
82
83/**
84 * Initializes frame header |hd| with given parameters.  Reserved bit
85 * is set to 0.
86 */
87void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
88                           uint8_t flags, int32_t stream_id);
89
90/**
91 * Returns the number of priority field depending on the |flags|.  If
92 * |flags| has neither NGHTTP2_FLAG_PRIORITY_GROUP nor
93 * NGHTTP2_FLAG_PRIORITY_DEPENDENCY set, return 0.
94 */
95size_t nghttp2_frame_priority_len(uint8_t flags);
96
97/**
98 * Packs the |pri_spec| in |buf|.  This function assumes |buf| has
99 * enough space for serialization.
100 */
101void nghttp2_frame_pack_priority_spec(uint8_t *buf,
102                                      const nghttp2_priority_spec *pri_spec);
103
104/**
105 * Unpacks the priority specification from payload |payload| of length
106 * |payloadlen| to |pri_spec|.  The |flags| is used to determine what
107 * kind of priority specification is in |payload|.  This function
108 * assumes the |payload| contains whole priority specification.
109 */
110void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
111                                        const uint8_t *payload);
112
113/*
114 * Returns the offset from the HEADERS frame payload where the
115 * compressed header block starts. The frame payload does not include
116 * frame header.
117 */
118size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame);
119
120/*
121 * Packs HEADERS frame |frame| in wire format and store it in |bufs|.
122 * This function expands |bufs| as necessary to store frame.
123 *
124 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
125 * before calling this function.
126 *
127 * frame->hd.length is assigned after length is determined during
128 * packing process.  CONTINUATION frames are also serialized in this
129 * function. This function does not handle padding.
130 *
131 * This function returns 0 if it succeeds, or returns one of the
132 * following negative error codes:
133 *
134 * NGHTTP2_ERR_HEADER_COMP
135 *     The deflate operation failed.
136 * NGHTTP2_ERR_NOMEM
137 *     Out of memory.
138 */
139int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
140                               nghttp2_hd_deflater *deflater);
141
142/*
143 * Unpacks HEADERS frame byte sequence into |frame|.  This function
144 * only unapcks bytes that come before name/value header block and
145 * after possible Pad Length field.
146 */
147void nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
148                                          const uint8_t *payload);
149
150/*
151 * Packs PRIORITY frame |frame| in wire format and store it in
152 * |bufs|.
153 *
154 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
155 * before calling this function.
156 */
157void nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame);
158
159/*
160 * Unpacks PRIORITY wire format into |frame|.
161 */
162void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
163                                           const uint8_t *payload);
164
165/*
166 * Packs RST_STREAM frame |frame| in wire frame format and store it in
167 * |bufs|.
168 *
169 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
170 * before calling this function.
171 */
172void nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
173                                   nghttp2_rst_stream *frame);
174
175/*
176 * Unpacks RST_STREAM frame byte sequence into |frame|.
177 */
178void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
179                                             const uint8_t *payload);
180
181/*
182 * Packs SETTINGS frame |frame| in wire format and store it in
183 * |bufs|.
184 *
185 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
186 * before calling this function.
187 *
188 * This function returns 0 if it succeeds, or returns one of the
189 * following negative error codes:
190 *
191 * NGHTTP2_ERR_FRAME_SIZE_ERROR
192 *     The length of the frame is too large.
193 */
194int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame);
195
196/*
197 * Packs the |iv|, which includes |niv| entries, in the |buf|,
198 * assuming the |buf| has at least 8 * |niv| bytes.
199 *
200 * Returns the number of bytes written into the |buf|.
201 */
202size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
203                                           const nghttp2_settings_entry *iv,
204                                           size_t niv);
205
206void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
207                                         const uint8_t *payload);
208
209/*
210 * Initializes payload of frame->settings.  The |frame| takes
211 * ownership of |iv|.
212 */
213void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
214                                           nghttp2_settings_entry *iv,
215                                           size_t niv);
216
217/*
218 * Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are
219 * assigned to the |*niv_ptr|. This function allocates enough memory
220 * to store the result in |*iv_ptr|. The caller is responsible to free
221 * |*iv_ptr| after its use.
222 *
223 * This function returns 0 if it succeeds or one of the following
224 * negative error codes:
225 *
226 * NGHTTP2_ERR_NOMEM
227 *     Out of memory.
228 */
229int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
230                                           size_t *niv_ptr,
231                                           const uint8_t *payload,
232                                           size_t payloadlen, nghttp2_mem *mem);
233
234/*
235 * Packs PUSH_PROMISE frame |frame| in wire format and store it in
236 * |bufs|.  This function expands |bufs| as necessary to store
237 * frame.
238 *
239 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
240 * before calling this function.
241 *
242 * frame->hd.length is assigned after length is determined during
243 * packing process.  CONTINUATION frames are also serialized in this
244 * function. This function does not handle padding.
245 *
246 * This function returns 0 if it succeeds, or returns one of the
247 * following negative error codes:
248 *
249 * NGHTTP2_ERR_HEADER_COMP
250 *     The deflate operation failed.
251 * NGHTTP2_ERR_NOMEM
252 *     Out of memory.
253 */
254int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
255                                    nghttp2_push_promise *frame,
256                                    nghttp2_hd_deflater *deflater);
257
258/*
259 * Unpacks PUSH_PROMISE frame byte sequence into |frame|.  This
260 * function only unapcks bytes that come before name/value header
261 * block and after possible Pad Length field.
262 */
263void nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
264                                               const uint8_t *payload);
265
266/*
267 * Packs PING frame |frame| in wire format and store it in
268 * |bufs|.
269 *
270 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
271 * before calling this function.
272 */
273void nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame);
274
275/*
276 * Unpacks PING wire format into |frame|.
277 */
278void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
279                                       const uint8_t *payload);
280
281/*
282 * Packs GOAWAY frame |frame| in wire format and store it in |bufs|.
283 * This function expands |bufs| as necessary to store frame.
284 *
285 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
286 * before calling this function.
287 *
288 * This function returns 0 if it succeeds or one of the following
289 * negative error codes:
290 *
291 * NGHTTP2_ERR_NOMEM
292 *     Out of memory.
293 * NGHTTP2_ERR_FRAME_SIZE_ERROR
294 *     The length of the frame is too large.
295 */
296int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame);
297
298/*
299 * Unpacks GOAWAY wire format into |frame|.  The |payload| of length
300 * |payloadlen| contains first 8 bytes of payload.  The
301 * |var_gift_payload| of length |var_gift_payloadlen| contains
302 * remaining payload and its buffer is gifted to the function and then
303 * |frame|.  The |var_gift_payloadlen| must be freed by
304 * nghttp2_frame_goaway_free().
305 */
306void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
307                                         const uint8_t *payload,
308                                         uint8_t *var_gift_payload,
309                                         size_t var_gift_payloadlen);
310
311/*
312 * Unpacks GOAWAY wire format into |frame|.  This function only exists
313 * for unit test.  After allocating buffer for debug data, this
314 * function internally calls nghttp2_frame_unpack_goaway_payload().
315 *
316 * This function returns 0 if it succeeds, or one of the following
317 * negative error codes:
318 *
319 * NGHTTP2_ERR_NOMEM
320 *     Out of memory.
321 */
322int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
323                                         const uint8_t *payload,
324                                         size_t payloadlen, nghttp2_mem *mem);
325
326/*
327 * Packs WINDOW_UPDATE frame |frame| in wire frame format and store it
328 * in |bufs|.
329 *
330 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
331 * before calling this function.
332 */
333void nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
334                                      nghttp2_window_update *frame);
335
336/*
337 * Unpacks WINDOW_UPDATE frame byte sequence into |frame|.
338 */
339void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
340                                                const uint8_t *payload);
341
342/*
343 * Packs ALTSVC frame |frame| in wire frame format and store it in
344 * |bufs|.
345 *
346 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
347 * before calling this function.
348 */
349void nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *ext);
350
351/*
352 * Unpacks ALTSVC wire format into |frame|.  The |payload| of
353 * |payloadlen| bytes contains frame payload.  This function assumes
354 * that frame->payload points to the nghttp2_ext_altsvc object.
355 */
356void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
357                                         size_t origin_len, uint8_t *payload,
358                                         size_t payloadlen);
359
360/*
361 * Unpacks ALTSVC wire format into |frame|.  This function only exists
362 * for unit test.  After allocating buffer for fields, this function
363 * internally calls nghttp2_frame_unpack_altsvc_payload().
364 *
365 * This function returns 0 if it succeeds, or one of the following
366 * negative error codes:
367 *
368 * NGHTTP2_ERR_NOMEM
369 *     Out of memory.
370 * NGHTTP2_ERR_FRAME_SIZE_ERROR
371 *     The payload is too small.
372 */
373int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
374                                         const uint8_t *payload,
375                                         size_t payloadlen, nghttp2_mem *mem);
376
377/*
378 * Packs ORIGIN frame |frame| in wire frame format and store it in
379 * |bufs|.
380 *
381 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
382 * before calling this function.
383 *
384 * This function returns 0 if it succeeds, or one of the following
385 * negative error codes:
386 *
387 * NGHTTP2_ERR_FRAME_SIZE_ERROR
388 *     The length of the frame is too large.
389 */
390int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *ext);
391
392/*
393 * Unpacks ORIGIN wire format into |frame|.  The |payload| of length
394 * |payloadlen| contains the frame payload.
395 *
396 * This function returns 0 if it succeeds, or one of the following
397 * negative error codes:
398 *
399 * NGHTTP2_ERR_NOMEM
400 *     Out of memory.
401 * NGHTTP2_ERR_FRAME_SIZE_ERROR
402 *     The payload is too small.
403 */
404int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
405                                        const uint8_t *payload,
406                                        size_t payloadlen, nghttp2_mem *mem);
407
408/*
409 * Packs PRIORITY_UPDATE frame |frame| in wire frame format and store
410 * it in |bufs|.
411 *
412 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
413 * before calling this function.
414 */
415void nghttp2_frame_pack_priority_update(nghttp2_bufs *bufs,
416                                        nghttp2_extension *ext);
417
418/*
419 * Unpacks PRIORITY_UPDATE wire format into |frame|.  The |payload| of
420 * |payloadlen| bytes contains frame payload.  This function assumes
421 * that frame->payload points to the nghttp2_ext_priority_update
422 * object.
423 */
424void nghttp2_frame_unpack_priority_update_payload(nghttp2_extension *frame,
425                                                  uint8_t *payload,
426                                                  size_t payloadlen);
427
428/*
429 * Initializes HEADERS frame |frame| with given values.  |frame| takes
430 * ownership of |nva|, so caller must not free it. If |stream_id| is
431 * not assigned yet, it must be -1.
432 */
433void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
434                                int32_t stream_id, nghttp2_headers_category cat,
435                                const nghttp2_priority_spec *pri_spec,
436                                nghttp2_nv *nva, size_t nvlen);
437
438void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem);
439
440void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
441                                 const nghttp2_priority_spec *pri_spec);
442
443void nghttp2_frame_priority_free(nghttp2_priority *frame);
444
445void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
446                                   uint32_t error_code);
447
448void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame);
449
450/*
451 * Initializes PUSH_PROMISE frame |frame| with given values.  |frame|
452 * takes ownership of |nva|, so caller must not free it.
453 */
454void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
455                                     int32_t stream_id,
456                                     int32_t promised_stream_id,
457                                     nghttp2_nv *nva, size_t nvlen);
458
459void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
460                                     nghttp2_mem *mem);
461
462/*
463 * Initializes SETTINGS frame |frame| with given values. |frame| takes
464 * ownership of |iv|, so caller must not free it. The |flags| are
465 * bitwise-OR of one or more of nghttp2_settings_flag.
466 */
467void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
468                                 nghttp2_settings_entry *iv, size_t niv);
469
470void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem);
471
472/*
473 * Initializes PING frame |frame| with given values. If the
474 * |opqeue_data| is not NULL, it must point to 8 bytes memory region
475 * of data. The data pointed by |opaque_data| is copied. It can be
476 * NULL. In this case, 8 bytes NULL is used.
477 */
478void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
479                             const uint8_t *opque_data);
480
481void nghttp2_frame_ping_free(nghttp2_ping *frame);
482
483/*
484 * Initializes GOAWAY frame |frame| with given values. On success,
485 * this function takes ownership of |opaque_data|, so caller must not
486 * free it. If the |opaque_data_len| is 0, opaque_data could be NULL.
487 */
488void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
489                               uint32_t error_code, uint8_t *opaque_data,
490                               size_t opaque_data_len);
491
492void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem);
493
494void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
495                                      uint8_t flags, int32_t stream_id,
496                                      int32_t window_size_increment);
497
498void nghttp2_frame_window_update_free(nghttp2_window_update *frame);
499
500void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
501                                  uint8_t flags, int32_t stream_id,
502                                  void *payload);
503
504void nghttp2_frame_extension_free(nghttp2_extension *frame);
505
506/*
507 * Initializes ALTSVC frame |frame| with given values.  This function
508 * assumes that frame->payload points to nghttp2_ext_altsvc object.
509 * Also |origin| and |field_value| are allocated in single buffer,
510 * starting |origin|.  On success, this function takes ownership of
511 * |origin|, so caller must not free it.
512 */
513void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
514                               uint8_t *origin, size_t origin_len,
515                               uint8_t *field_value, size_t field_value_len);
516
517/*
518 * Frees up resources under |frame|.  This function does not free
519 * nghttp2_ext_altsvc object pointed by frame->payload.  This function
520 * only frees origin pointed by nghttp2_ext_altsvc.origin.  Therefore,
521 * other fields must be allocated in the same buffer with origin.
522 */
523void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem);
524
525/*
526 * Initializes ORIGIN frame |frame| with given values.  This function
527 * assumes that frame->payload points to nghttp2_ext_origin object.
528 * Also |ov| and the memory pointed by the field of its elements are
529 * allocated in single buffer, starting with |ov|.  On success, this
530 * function takes ownership of |ov|, so caller must not free it.
531 */
532void nghttp2_frame_origin_init(nghttp2_extension *frame,
533                               nghttp2_origin_entry *ov, size_t nov);
534
535/*
536 * Frees up resources under |frame|.  This function does not free
537 * nghttp2_ext_origin object pointed by frame->payload.  This function
538 * only frees nghttp2_ext_origin.ov.  Therefore, other fields must be
539 * allocated in the same buffer with ov.
540 */
541void nghttp2_frame_origin_free(nghttp2_extension *frame, nghttp2_mem *mem);
542
543/*
544 * Initializes PRIORITY_UPDATE frame |frame| with given values.  This
545 * function assumes that frame->payload points to
546 * nghttp2_ext_priority_update object.  On success, this function
547 * takes ownership of |field_value|, so caller must not free it.
548 */
549void nghttp2_frame_priority_update_init(nghttp2_extension *frame,
550                                        int32_t stream_id, uint8_t *field_value,
551                                        size_t field_value_len);
552
553/*
554 * Frees up resources under |frame|.  This function does not free
555 * nghttp2_ext_priority_update object pointed by frame->payload.  This
556 * function only frees field_value pointed by
557 * nghttp2_ext_priority_update.field_value.
558 */
559void nghttp2_frame_priority_update_free(nghttp2_extension *frame,
560                                        nghttp2_mem *mem);
561
562/*
563 * Returns the number of padding bytes after payload.  The total
564 * padding length is given in the |padlen|.  The returned value does
565 * not include the Pad Length field.  If |padlen| is 0, this function
566 * returns 0, regardless of frame->hd.flags.
567 */
568size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen);
569
570void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
571                             int32_t stream_id);
572
573void nghttp2_frame_data_free(nghttp2_data *frame);
574
575/*
576 * Makes copy of |iv| and return the copy. The |niv| is the number of
577 * entries in |iv|. This function returns the pointer to the copy if
578 * it succeeds, or NULL.
579 */
580nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
581                                              size_t niv, nghttp2_mem *mem);
582
583/*
584 * Sorts the |nva| in ascending order of name and value. If names are
585 * equivalent, sort them by value.
586 */
587void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen);
588
589/*
590 * Copies name/value pairs from |nva|, which contains |nvlen| pairs,
591 * to |*nva_ptr|, which is dynamically allocated so that all items can
592 * be stored.  The resultant name and value in nghttp2_nv are
593 * guaranteed to be NULL-terminated even if the input is not
594 * null-terminated.
595 *
596 * The |*nva_ptr| must be freed using nghttp2_nv_array_del().
597 *
598 * This function returns 0 if it succeeds or one of the following
599 * negative error codes:
600 *
601 * NGHTTP2_ERR_NOMEM
602 *     Out of memory.
603 */
604int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
605                          size_t nvlen, nghttp2_mem *mem);
606
607/*
608 * Returns nonzero if the name/value pair |a| equals to |b|. The name
609 * is compared in case-sensitive, because we ensure that this function
610 * is called after the name is lower-cased.
611 */
612int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b);
613
614/*
615 * Frees |nva|.
616 */
617void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem);
618
619/*
620 * Checks that the |iv|, which includes |niv| entries, does not have
621 * invalid values.
622 *
623 * This function returns nonzero if it succeeds, or 0.
624 */
625int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv);
626
627/*
628 * Sets Pad Length field and flags and adjusts frame header position
629 * of each buffers in |bufs|.  The number of padding is given in the
630 * |padlen| including Pad Length field.  The |hd| is the frame header
631 * for the serialized data.  This function fills zeros padding region
632 * unless framehd_only is nonzero.
633 */
634void nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
635                           size_t padlen, int framehd_only);
636
637#endif /* NGHTTP2_FRAME_H */
638