2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #ifndef NGHTTP2_FRAME_H
26 #define NGHTTP2_FRAME_H
30 #endif /* HAVE_CONFIG_H */
32 #include <nghttp2/nghttp2.h>
33 #include "nghttp2_hd.h"
34 #include "nghttp2_buf.h"
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)
42 /* The number of bytes of frame header. */
43 #define NGHTTP2_FRAME_HDLEN 9
45 #define NGHTTP2_MAX_FRAME_SIZE_MAX ((1 << 24) - 1)
46 #define NGHTTP2_MAX_FRAME_SIZE_MIN (1 << 14)
48 #define NGHTTP2_MAX_PAYLOADLEN 16384
49 /* The one frame buffer length for tranmission. 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)
55 /* The default length of DATA frame payload. */
56 #define NGHTTP2_DATA_PAYLOADLEN NGHTTP2_MAX_FRAME_SIZE_MIN
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_size(). */
61 #define NGHTTP2_MAX_HEADERSLEN 65536
63 /* The number of bytes for each SETTINGS entry */
64 #define NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH 6
66 /* Length of priority related fields in HEADERS/PRIORITY frames */
67 #define NGHTTP2_PRIORITY_SPECLEN 5
69 /* Maximum length of padding in bytes. */
70 #define NGHTTP2_MAX_PADLEN 256
72 /* Union of extension frame payload */
74 nghttp2_ext_altsvc altsvc;
75 nghttp2_ext_origin origin;
76 } nghttp2_ext_frame_payload;
78 void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd);
80 void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf);
83 * Initializes frame header |hd| with given parameters. Reserved bit
86 void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
87 uint8_t flags, int32_t stream_id);
90 * Returns the number of priority field depending on the |flags|. If
91 * |flags| has neither NGHTTP2_FLAG_PRIORITY_GROUP nor
92 * NGHTTP2_FLAG_PRIORITY_DEPENDENCY set, return 0.
94 size_t nghttp2_frame_priority_len(uint8_t flags);
97 * Packs the |pri_spec| in |buf|. This function assumes |buf| has
98 * enough space for serialization.
100 void nghttp2_frame_pack_priority_spec(uint8_t *buf,
101 const nghttp2_priority_spec *pri_spec);
104 * Unpacks the priority specification from payload |payload| of length
105 * |payloadlen| to |pri_spec|. The |flags| is used to determine what
106 * kind of priority specification is in |payload|. This function
107 * assumes the |payload| contains whole priority specification.
109 void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
110 const uint8_t *payload);
113 * Returns the offset from the HEADERS frame payload where the
114 * compressed header block starts. The frame payload does not include
117 size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame);
120 * Packs HEADERS frame |frame| in wire format and store it in |bufs|.
121 * This function expands |bufs| as necessary to store frame.
123 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
124 * before calling this function.
126 * frame->hd.length is assigned after length is determined during
127 * packing process. CONTINUATION frames are also serialized in this
128 * function. This function does not handle padding.
130 * This function returns 0 if it succeeds, or returns one of the
131 * following negative error codes:
133 * NGHTTP2_ERR_HEADER_COMP
134 * The deflate operation failed.
138 int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
139 nghttp2_hd_deflater *deflater);
142 * Unpacks HEADERS frame byte sequence into |frame|. This function
143 * only unapcks bytes that come before name/value header block and
144 * after possible Pad Length field.
146 * This function always succeeds and returns 0.
148 int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
149 const uint8_t *payload);
152 * Packs PRIORITY frame |frame| in wire format and store it in
155 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
156 * before calling this function.
158 * This function always succeeds and returns 0.
160 int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame);
163 * Unpacks PRIORITY wire format into |frame|.
165 void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
166 const uint8_t *payload);
169 * Packs RST_STREAM frame |frame| in wire frame format and store it in
172 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
173 * before calling this function.
175 * This function always succeeds and returns 0.
177 int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
178 nghttp2_rst_stream *frame);
181 * Unpacks RST_STREAM frame byte sequence into |frame|.
183 void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
184 const uint8_t *payload);
187 * Packs SETTINGS frame |frame| in wire format and store it in
190 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
191 * before calling this function.
193 * This function returns 0 if it succeeds, or returns one of the
194 * following negative error codes:
196 * NGHTTP2_ERR_FRAME_SIZE_ERROR
197 * The length of the frame is too large.
199 int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame);
202 * Packs the |iv|, which includes |niv| entries, in the |buf|,
203 * assuming the |buf| has at least 8 * |niv| bytes.
205 * Returns the number of bytes written into the |buf|.
207 size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
208 const nghttp2_settings_entry *iv,
211 void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
212 const uint8_t *payload);
215 * Initializes payload of frame->settings. The |frame| takes
218 void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
219 nghttp2_settings_entry *iv,
223 * Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are
224 * assigned to the |*niv_ptr|. This function allocates enough memory
225 * to store the result in |*iv_ptr|. The caller is responsible to free
226 * |*iv_ptr| after its use.
228 * This function returns 0 if it succeeds or one of the following
229 * negative error codes:
234 int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
236 const uint8_t *payload,
237 size_t payloadlen, nghttp2_mem *mem);
240 * Packs PUSH_PROMISE frame |frame| in wire format and store it in
241 * |bufs|. This function expands |bufs| as necessary to store
244 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
245 * before calling this function.
247 * frame->hd.length is assigned after length is determined during
248 * packing process. CONTINUATION frames are also serialized in this
249 * function. This function does not handle padding.
251 * This function returns 0 if it succeeds, or returns one of the
252 * following negative error codes:
254 * NGHTTP2_ERR_HEADER_COMP
255 * The deflate operation failed.
259 int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
260 nghttp2_push_promise *frame,
261 nghttp2_hd_deflater *deflater);
264 * Unpacks PUSH_PROMISE frame byte sequence into |frame|. This
265 * function only unapcks bytes that come before name/value header
266 * block and after possible Pad Length field.
268 * This function returns 0 if it succeeds or one of the following
269 * negative error codes:
272 * TODO END_HEADERS flag is not set
274 int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
275 const uint8_t *payload);
278 * Packs PING frame |frame| in wire format and store it in
281 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
282 * before calling this function.
284 * This function always succeeds and returns 0.
286 int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame);
289 * Unpacks PING wire format into |frame|.
291 void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
292 const uint8_t *payload);
295 * Packs GOAWAY frame |frame| in wire format and store it in |bufs|.
296 * This function expands |bufs| as necessary to store frame.
298 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
299 * before calling this function.
301 * This function returns 0 if it succeeds or one of the following
302 * negative error codes:
306 * NGHTTP2_ERR_FRAME_SIZE_ERROR
307 * The length of the frame is too large.
309 int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame);
312 * Unpacks GOAWAY wire format into |frame|. The |payload| of length
313 * |payloadlen| contains first 8 bytes of payload. The
314 * |var_gift_payload| of length |var_gift_payloadlen| contains
315 * remaining payload and its buffer is gifted to the function and then
316 * |frame|. The |var_gift_payloadlen| must be freed by
317 * nghttp2_frame_goaway_free().
319 void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
320 const uint8_t *payload,
321 uint8_t *var_gift_payload,
322 size_t var_gift_payloadlen);
325 * Unpacks GOAWAY wire format into |frame|. This function only exists
326 * for unit test. After allocating buffer for debug data, this
327 * function internally calls nghttp2_frame_unpack_goaway_payload().
329 * This function returns 0 if it succeeds, or one of the following
330 * negative error codes:
335 int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
336 const uint8_t *payload,
337 size_t payloadlen, nghttp2_mem *mem);
340 * Packs WINDOW_UPDATE frame |frame| in wire frame format and store it
343 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
344 * before calling this function.
346 * This function always succeeds and returns 0.
348 int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
349 nghttp2_window_update *frame);
352 * Unpacks WINDOW_UPDATE frame byte sequence into |frame|.
354 void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
355 const uint8_t *payload);
358 * Packs ALTSVC frame |frame| in wire frame format and store it in
361 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
362 * before calling this function.
364 * This function always succeeds and returns 0.
366 int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *ext);
369 * Unpacks ALTSVC wire format into |frame|. The |payload| of
370 * |payloadlen| bytes contains frame payload. This function assumes
371 * that frame->payload points to the nghttp2_ext_altsvc object.
373 * This function always succeeds and returns 0.
375 void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
376 size_t origin_len, uint8_t *payload,
380 * Unpacks ALTSVC wire format into |frame|. This function only exists
381 * for unit test. After allocating buffer for fields, this function
382 * internally calls nghttp2_frame_unpack_altsvc_payload().
384 * This function returns 0 if it succeeds, or one of the following
385 * negative error codes:
389 * NGHTTP2_ERR_FRAME_SIZE_ERROR
390 * The payload is too small.
392 int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
393 const uint8_t *payload,
394 size_t payloadlen, nghttp2_mem *mem);
397 * Packs ORIGIN frame |frame| in wire frame format and store it in
400 * The caller must make sure that nghttp2_bufs_reset(bufs) is called
401 * before calling this function.
403 * This function returns 0 if it succeeds, or one of the following
404 * negative error codes:
406 * NGHTTP2_ERR_FRAME_SIZE_ERROR
407 * The length of the frame is too large.
409 int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *ext);
412 * Unpacks ORIGIN wire format into |frame|. The |payload| of length
413 * |payloadlen| contains the frame payload.
415 * This function returns 0 if it succeeds, or one of the following
416 * negative error codes:
420 * NGHTTP2_ERR_FRAME_SIZE_ERROR
421 * The payload is too small.
423 int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
424 const uint8_t *payload,
425 size_t payloadlen, nghttp2_mem *mem);
427 * Initializes HEADERS frame |frame| with given values. |frame| takes
428 * ownership of |nva|, so caller must not free it. If |stream_id| is
429 * not assigned yet, it must be -1.
431 void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
432 int32_t stream_id, nghttp2_headers_category cat,
433 const nghttp2_priority_spec *pri_spec,
434 nghttp2_nv *nva, size_t nvlen);
436 void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem);
438 void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
439 const nghttp2_priority_spec *pri_spec);
441 void nghttp2_frame_priority_free(nghttp2_priority *frame);
443 void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
444 uint32_t error_code);
446 void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame);
449 * Initializes PUSH_PROMISE frame |frame| with given values. |frame|
450 * takes ownership of |nva|, so caller must not free it.
452 void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
454 int32_t promised_stream_id,
455 nghttp2_nv *nva, size_t nvlen);
457 void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
461 * Initializes SETTINGS frame |frame| with given values. |frame| takes
462 * ownership of |iv|, so caller must not free it. The |flags| are
463 * bitwise-OR of one or more of nghttp2_settings_flag.
465 void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
466 nghttp2_settings_entry *iv, size_t niv);
468 void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem);
471 * Initializes PING frame |frame| with given values. If the
472 * |opqeue_data| is not NULL, it must point to 8 bytes memory region
473 * of data. The data pointed by |opaque_data| is copied. It can be
474 * NULL. In this case, 8 bytes NULL is used.
476 void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
477 const uint8_t *opque_data);
479 void nghttp2_frame_ping_free(nghttp2_ping *frame);
482 * Initializes GOAWAY frame |frame| with given values. On success,
483 * this function takes ownership of |opaque_data|, so caller must not
484 * free it. If the |opaque_data_len| is 0, opaque_data could be NULL.
486 void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
487 uint32_t error_code, uint8_t *opaque_data,
488 size_t opaque_data_len);
490 void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem);
492 void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
493 uint8_t flags, int32_t stream_id,
494 int32_t window_size_increment);
496 void nghttp2_frame_window_update_free(nghttp2_window_update *frame);
498 void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
499 uint8_t flags, int32_t stream_id,
502 void nghttp2_frame_extension_free(nghttp2_extension *frame);
505 * Initializes ALTSVC frame |frame| with given values. This function
506 * assumes that frame->payload points to nghttp2_ext_altsvc object.
507 * Also |origin| and |field_value| are allocated in single buffer,
508 * starting |origin|. On success, this function takes ownership of
509 * |origin|, so caller must not free it.
511 void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
512 uint8_t *origin, size_t origin_len,
513 uint8_t *field_value, size_t field_value_len);
516 * Frees up resources under |frame|. This function does not free
517 * nghttp2_ext_altsvc object pointed by frame->payload. This function
518 * only frees origin pointed by nghttp2_ext_altsvc.origin. Therefore,
519 * other fields must be allocated in the same buffer with origin.
521 void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem);
524 * Initializes ORIGIN frame |frame| with given values. This function
525 * assumes that frame->payload points to nghttp2_ext_origin object.
526 * Also |ov| and the memory pointed by the field of its elements are
527 * allocated in single buffer, starting with |ov|. On success, this
528 * function takes ownership of |ov|, so caller must not free it.
530 void nghttp2_frame_origin_init(nghttp2_extension *frame,
531 nghttp2_origin_entry *ov, size_t nov);
534 * Frees up resources under |frame|. This function does not free
535 * nghttp2_ext_origin object pointed by frame->payload. This function
536 * only frees nghttp2_ext_origin.ov. Therefore, other fields must be
537 * allocated in the same buffer with ov.
539 void nghttp2_frame_origin_free(nghttp2_extension *frame, nghttp2_mem *mem);
542 * Returns the number of padding bytes after payload. The total
543 * padding length is given in the |padlen|. The returned value does
544 * not include the Pad Length field. If |padlen| is 0, this function
545 * returns 0, regardless of frame->hd.flags.
547 size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen);
549 void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
552 void nghttp2_frame_data_free(nghttp2_data *frame);
555 * Makes copy of |iv| and return the copy. The |niv| is the number of
556 * entries in |iv|. This function returns the pointer to the copy if
557 * it succeeds, or NULL.
559 nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
560 size_t niv, nghttp2_mem *mem);
563 * Sorts the |nva| in ascending order of name and value. If names are
564 * equivalent, sort them by value.
566 void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen);
569 * Copies name/value pairs from |nva|, which contains |nvlen| pairs,
570 * to |*nva_ptr|, which is dynamically allocated so that all items can
571 * be stored. The resultant name and value in nghttp2_nv are
572 * guaranteed to be NULL-terminated even if the input is not
575 * The |*nva_ptr| must be freed using nghttp2_nv_array_del().
577 * This function returns 0 if it succeeds or one of the following
578 * negative error codes:
583 int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
584 size_t nvlen, nghttp2_mem *mem);
587 * Returns nonzero if the name/value pair |a| equals to |b|. The name
588 * is compared in case-sensitive, because we ensure that this function
589 * is called after the name is lower-cased.
591 int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b);
596 void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem);
599 * Checks that the |iv|, which includes |niv| entries, does not have
602 * This function returns nonzero if it succeeds, or 0.
604 int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv);
607 * Sets Pad Length field and flags and adjusts frame header position
608 * of each buffers in |bufs|. The number of padding is given in the
609 * |padlen| including Pad Length field. The |hd| is the frame header
610 * for the serialized data. This function fills zeros padding region
611 * unless framehd_only is nonzero.
613 * This function returns 0 if it succeeds, or one of the following
614 * negative error codes:
618 * NGHTTP2_ERR_FRAME_SIZE_ERROR
619 * The length of the resulting frame is too large.
621 int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
622 size_t padlen, int framehd_only);
624 #endif /* NGHTTP2_FRAME_H */