1/*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2013, 2014 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_H
26#define NGHTTP2_H
27
28/* Define WIN32 when build target is Win32 API (borrowed from
29   libcurl) */
30#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
31#  define WIN32
32#endif
33
34/* Compatibility for non-Clang compilers */
35#ifndef __has_declspec_attribute
36#  define __has_declspec_attribute(x) 0
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#include <stdlib.h>
44#if defined(_MSC_VER) && (_MSC_VER < 1800)
45/* MSVC < 2013 does not have inttypes.h because it is not C99
46   compliant.  See compiler macros and version number in
47   https://sourceforge.net/p/predef/wiki/Compilers/ */
48#  include <stdint.h>
49#else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
50#  include <inttypes.h>
51#endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
52#include <sys/types.h>
53#include <stdarg.h>
54
55#include <nghttp2/nghttp2ver.h>
56
57#ifdef NGHTTP2_STATICLIB
58#  define NGHTTP2_EXTERN
59#elif defined(WIN32) || (__has_declspec_attribute(dllexport) &&                \
60                         __has_declspec_attribute(dllimport))
61#  ifdef BUILDING_NGHTTP2
62#    define NGHTTP2_EXTERN __declspec(dllexport)
63#  else /* !BUILDING_NGHTTP2 */
64#    define NGHTTP2_EXTERN __declspec(dllimport)
65#  endif /* !BUILDING_NGHTTP2 */
66#else    /* !defined(WIN32) */
67#  ifdef BUILDING_NGHTTP2
68#    define NGHTTP2_EXTERN __attribute__((visibility("default")))
69#  else /* !BUILDING_NGHTTP2 */
70#    define NGHTTP2_EXTERN
71#  endif /* !BUILDING_NGHTTP2 */
72#endif   /* !defined(WIN32) */
73
74/**
75 * @macro
76 *
77 * The protocol version identification string of this library
78 * supports.  This identifier is used if HTTP/2 is used over TLS.
79 */
80#define NGHTTP2_PROTO_VERSION_ID "h2"
81/**
82 * @macro
83 *
84 * The length of :macro:`NGHTTP2_PROTO_VERSION_ID`.
85 */
86#define NGHTTP2_PROTO_VERSION_ID_LEN 2
87
88/**
89 * @macro
90 *
91 * The serialized form of ALPN protocol identifier this library
92 * supports.  Notice that first byte is the length of following
93 * protocol identifier.  This is the same wire format of `TLS ALPN
94 * extension <https://tools.ietf.org/html/rfc7301>`_.  This is useful
95 * to process incoming ALPN tokens in wire format.
96 */
97#define NGHTTP2_PROTO_ALPN "\x2h2"
98
99/**
100 * @macro
101 *
102 * The length of :macro:`NGHTTP2_PROTO_ALPN`.
103 */
104#define NGHTTP2_PROTO_ALPN_LEN (sizeof(NGHTTP2_PROTO_ALPN) - 1)
105
106/**
107 * @macro
108 *
109 * The protocol version identification string of this library
110 * supports.  This identifier is used if HTTP/2 is used over cleartext
111 * TCP.
112 */
113#define NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "h2c"
114
115/**
116 * @macro
117 *
118 * The length of :macro:`NGHTTP2_CLEARTEXT_PROTO_VERSION_ID`.
119 */
120#define NGHTTP2_CLEARTEXT_PROTO_VERSION_ID_LEN 3
121
122struct nghttp2_session;
123/**
124 * @struct
125 *
126 * The primary structure to hold the resources needed for a HTTP/2
127 * session.  The details of this structure are intentionally hidden
128 * from the public API.
129 */
130typedef struct nghttp2_session nghttp2_session;
131
132/**
133 * @macro
134 *
135 * The age of :type:`nghttp2_info`
136 */
137#define NGHTTP2_VERSION_AGE 1
138
139/**
140 * @struct
141 *
142 * This struct is what `nghttp2_version()` returns.  It holds
143 * information about the particular nghttp2 version.
144 */
145typedef struct {
146  /**
147   * Age of this struct.  This instance of nghttp2 sets it to
148   * :macro:`NGHTTP2_VERSION_AGE` but a future version may bump it and
149   * add more struct fields at the bottom
150   */
151  int age;
152  /**
153   * the :macro:`NGHTTP2_VERSION_NUM` number (since age ==1)
154   */
155  int version_num;
156  /**
157   * points to the :macro:`NGHTTP2_VERSION` string (since age ==1)
158   */
159  const char *version_str;
160  /**
161   * points to the :macro:`NGHTTP2_PROTO_VERSION_ID` string this
162   * instance implements (since age ==1)
163   */
164  const char *proto_str;
165  /* -------- the above fields all exist when age == 1 */
166} nghttp2_info;
167
168/**
169 * @macro
170 *
171 * The default weight of stream dependency.
172 */
173#define NGHTTP2_DEFAULT_WEIGHT 16
174
175/**
176 * @macro
177 *
178 * The maximum weight of stream dependency.
179 */
180#define NGHTTP2_MAX_WEIGHT 256
181
182/**
183 * @macro
184 *
185 * The minimum weight of stream dependency.
186 */
187#define NGHTTP2_MIN_WEIGHT 1
188
189/**
190 * @macro
191 *
192 * The maximum window size
193 */
194#define NGHTTP2_MAX_WINDOW_SIZE ((int32_t)((1U << 31) - 1))
195
196/**
197 * @macro
198 *
199 * The initial window size for stream level flow control.
200 */
201#define NGHTTP2_INITIAL_WINDOW_SIZE ((1 << 16) - 1)
202/**
203 * @macro
204 *
205 * The initial window size for connection level flow control.
206 */
207#define NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE ((1 << 16) - 1)
208
209/**
210 * @macro
211 *
212 * The default header table size.
213 */
214#define NGHTTP2_DEFAULT_HEADER_TABLE_SIZE (1 << 12)
215
216/**
217 * @macro
218 *
219 * The client magic string, which is the first 24 bytes byte string of
220 * client connection preface.
221 */
222#define NGHTTP2_CLIENT_MAGIC "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
223
224/**
225 * @macro
226 *
227 * The length of :macro:`NGHTTP2_CLIENT_MAGIC`.
228 */
229#define NGHTTP2_CLIENT_MAGIC_LEN 24
230
231/**
232 * @macro
233 *
234 * The default max number of settings per SETTINGS frame
235 */
236#define NGHTTP2_DEFAULT_MAX_SETTINGS 32
237
238/**
239 * @enum
240 *
241 * Error codes used in this library.  The code range is [-999, -500],
242 * inclusive. The following values are defined:
243 */
244typedef enum {
245  /**
246   * Invalid argument passed.
247   */
248  NGHTTP2_ERR_INVALID_ARGUMENT = -501,
249  /**
250   * Out of buffer space.
251   */
252  NGHTTP2_ERR_BUFFER_ERROR = -502,
253  /**
254   * The specified protocol version is not supported.
255   */
256  NGHTTP2_ERR_UNSUPPORTED_VERSION = -503,
257  /**
258   * Used as a return value from :type:`nghttp2_send_callback`,
259   * :type:`nghttp2_recv_callback` and
260   * :type:`nghttp2_send_data_callback` to indicate that the operation
261   * would block.
262   */
263  NGHTTP2_ERR_WOULDBLOCK = -504,
264  /**
265   * General protocol error
266   */
267  NGHTTP2_ERR_PROTO = -505,
268  /**
269   * The frame is invalid.
270   */
271  NGHTTP2_ERR_INVALID_FRAME = -506,
272  /**
273   * The peer performed a shutdown on the connection.
274   */
275  NGHTTP2_ERR_EOF = -507,
276  /**
277   * Used as a return value from
278   * :func:`nghttp2_data_source_read_callback` to indicate that data
279   * transfer is postponed.  See
280   * :func:`nghttp2_data_source_read_callback` for details.
281   */
282  NGHTTP2_ERR_DEFERRED = -508,
283  /**
284   * Stream ID has reached the maximum value.  Therefore no stream ID
285   * is available.
286   */
287  NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE = -509,
288  /**
289   * The stream is already closed; or the stream ID is invalid.
290   */
291  NGHTTP2_ERR_STREAM_CLOSED = -510,
292  /**
293   * RST_STREAM has been added to the outbound queue.  The stream is
294   * in closing state.
295   */
296  NGHTTP2_ERR_STREAM_CLOSING = -511,
297  /**
298   * The transmission is not allowed for this stream (e.g., a frame
299   * with END_STREAM flag set has already sent).
300   */
301  NGHTTP2_ERR_STREAM_SHUT_WR = -512,
302  /**
303   * The stream ID is invalid.
304   */
305  NGHTTP2_ERR_INVALID_STREAM_ID = -513,
306  /**
307   * The state of the stream is not valid (e.g., DATA cannot be sent
308   * to the stream if response HEADERS has not been sent).
309   */
310  NGHTTP2_ERR_INVALID_STREAM_STATE = -514,
311  /**
312   * Another DATA frame has already been deferred.
313   */
314  NGHTTP2_ERR_DEFERRED_DATA_EXIST = -515,
315  /**
316   * Starting new stream is not allowed (e.g., GOAWAY has been sent
317   * and/or received).
318   */
319  NGHTTP2_ERR_START_STREAM_NOT_ALLOWED = -516,
320  /**
321   * GOAWAY has already been sent.
322   */
323  NGHTTP2_ERR_GOAWAY_ALREADY_SENT = -517,
324  /**
325   * The received frame contains the invalid header block (e.g., There
326   * are duplicate header names; or the header names are not encoded
327   * in US-ASCII character set and not lower cased; or the header name
328   * is zero-length string; or the header value contains multiple
329   * in-sequence NUL bytes).
330   */
331  NGHTTP2_ERR_INVALID_HEADER_BLOCK = -518,
332  /**
333   * Indicates that the context is not suitable to perform the
334   * requested operation.
335   */
336  NGHTTP2_ERR_INVALID_STATE = -519,
337  /**
338   * The user callback function failed due to the temporal error.
339   */
340  NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE = -521,
341  /**
342   * The length of the frame is invalid, either too large or too small.
343   */
344  NGHTTP2_ERR_FRAME_SIZE_ERROR = -522,
345  /**
346   * Header block inflate/deflate error.
347   */
348  NGHTTP2_ERR_HEADER_COMP = -523,
349  /**
350   * Flow control error
351   */
352  NGHTTP2_ERR_FLOW_CONTROL = -524,
353  /**
354   * Insufficient buffer size given to function.
355   */
356  NGHTTP2_ERR_INSUFF_BUFSIZE = -525,
357  /**
358   * Callback was paused by the application
359   */
360  NGHTTP2_ERR_PAUSE = -526,
361  /**
362   * There are too many in-flight SETTING frame and no more
363   * transmission of SETTINGS is allowed.
364   */
365  NGHTTP2_ERR_TOO_MANY_INFLIGHT_SETTINGS = -527,
366  /**
367   * The server push is disabled.
368   */
369  NGHTTP2_ERR_PUSH_DISABLED = -528,
370  /**
371   * DATA or HEADERS frame for a given stream has been already
372   * submitted and has not been fully processed yet.  Application
373   * should wait for the transmission of the previously submitted
374   * frame before submitting another.
375   */
376  NGHTTP2_ERR_DATA_EXIST = -529,
377  /**
378   * The current session is closing due to a connection error or
379   * `nghttp2_session_terminate_session()` is called.
380   */
381  NGHTTP2_ERR_SESSION_CLOSING = -530,
382  /**
383   * Invalid HTTP header field was received and stream is going to be
384   * closed.
385   */
386  NGHTTP2_ERR_HTTP_HEADER = -531,
387  /**
388   * Violation in HTTP messaging rule.
389   */
390  NGHTTP2_ERR_HTTP_MESSAGING = -532,
391  /**
392   * Stream was refused.
393   */
394  NGHTTP2_ERR_REFUSED_STREAM = -533,
395  /**
396   * Unexpected internal error, but recovered.
397   */
398  NGHTTP2_ERR_INTERNAL = -534,
399  /**
400   * Indicates that a processing was canceled.
401   */
402  NGHTTP2_ERR_CANCEL = -535,
403  /**
404   * When a local endpoint expects to receive SETTINGS frame, it
405   * receives an other type of frame.
406   */
407  NGHTTP2_ERR_SETTINGS_EXPECTED = -536,
408  /**
409   * When a local endpoint receives too many settings entries
410   * in a single SETTINGS frame.
411   */
412  NGHTTP2_ERR_TOO_MANY_SETTINGS = -537,
413  /**
414   * The errors < :enum:`nghttp2_error.NGHTTP2_ERR_FATAL` mean that
415   * the library is under unexpected condition and processing was
416   * terminated (e.g., out of memory).  If application receives this
417   * error code, it must stop using that :type:`nghttp2_session`
418   * object and only allowed operation for that object is deallocate
419   * it using `nghttp2_session_del()`.
420   */
421  NGHTTP2_ERR_FATAL = -900,
422  /**
423   * Out of memory.  This is a fatal error.
424   */
425  NGHTTP2_ERR_NOMEM = -901,
426  /**
427   * The user callback function failed.  This is a fatal error.
428   */
429  NGHTTP2_ERR_CALLBACK_FAILURE = -902,
430  /**
431   * Invalid client magic (see :macro:`NGHTTP2_CLIENT_MAGIC`) was
432   * received and further processing is not possible.
433   */
434  NGHTTP2_ERR_BAD_CLIENT_MAGIC = -903,
435  /**
436   * Possible flooding by peer was detected in this HTTP/2 session.
437   * Flooding is measured by how many PING and SETTINGS frames with
438   * ACK flag set are queued for transmission.  These frames are
439   * response for the peer initiated frames, and peer can cause memory
440   * exhaustion on server side to send these frames forever and does
441   * not read network.
442   */
443  NGHTTP2_ERR_FLOODED = -904
444} nghttp2_error;
445
446/**
447 * @struct
448 *
449 * The object representing single contiguous buffer.
450 */
451typedef struct {
452  /**
453   * The pointer to the buffer.
454   */
455  uint8_t *base;
456  /**
457   * The length of the buffer.
458   */
459  size_t len;
460} nghttp2_vec;
461
462struct nghttp2_rcbuf;
463
464/**
465 * @struct
466 *
467 * The object representing reference counted buffer.  The details of
468 * this structure are intentionally hidden from the public API.
469 */
470typedef struct nghttp2_rcbuf nghttp2_rcbuf;
471
472/**
473 * @function
474 *
475 * Increments the reference count of |rcbuf| by 1.
476 */
477NGHTTP2_EXTERN void nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf);
478
479/**
480 * @function
481 *
482 * Decrements the reference count of |rcbuf| by 1.  If the reference
483 * count becomes zero, the object pointed by |rcbuf| will be freed.
484 * In this case, application must not use |rcbuf| again.
485 */
486NGHTTP2_EXTERN void nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf);
487
488/**
489 * @function
490 *
491 * Returns the underlying buffer managed by |rcbuf|.
492 */
493NGHTTP2_EXTERN nghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf);
494
495/**
496 * @function
497 *
498 * Returns nonzero if the underlying buffer is statically allocated,
499 * and 0 otherwise. This can be useful for language bindings that wish
500 * to avoid creating duplicate strings for these buffers.
501 */
502NGHTTP2_EXTERN int nghttp2_rcbuf_is_static(const nghttp2_rcbuf *rcbuf);
503
504/**
505 * @enum
506 *
507 * The flags for header field name/value pair.
508 */
509typedef enum {
510  /**
511   * No flag set.
512   */
513  NGHTTP2_NV_FLAG_NONE = 0,
514  /**
515   * Indicates that this name/value pair must not be indexed ("Literal
516   * Header Field never Indexed" representation must be used in HPACK
517   * encoding).  Other implementation calls this bit as "sensitive".
518   */
519  NGHTTP2_NV_FLAG_NO_INDEX = 0x01,
520  /**
521   * This flag is set solely by application.  If this flag is set, the
522   * library does not make a copy of header field name.  This could
523   * improve performance.
524   */
525  NGHTTP2_NV_FLAG_NO_COPY_NAME = 0x02,
526  /**
527   * This flag is set solely by application.  If this flag is set, the
528   * library does not make a copy of header field value.  This could
529   * improve performance.
530   */
531  NGHTTP2_NV_FLAG_NO_COPY_VALUE = 0x04
532} nghttp2_nv_flag;
533
534/**
535 * @struct
536 *
537 * The name/value pair, which mainly used to represent header fields.
538 */
539typedef struct {
540  /**
541   * The |name| byte string.  If this struct is presented from library
542   * (e.g., :type:`nghttp2_on_frame_recv_callback`), |name| is
543   * guaranteed to be NULL-terminated.  For some callbacks
544   * (:type:`nghttp2_before_frame_send_callback`,
545   * :type:`nghttp2_on_frame_send_callback`, and
546   * :type:`nghttp2_on_frame_not_send_callback`), it may not be
547   * NULL-terminated if header field is passed from application with
548   * the flag :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME`).
549   * When application is constructing this struct, |name| is not
550   * required to be NULL-terminated.
551   */
552  uint8_t *name;
553  /**
554   * The |value| byte string.  If this struct is presented from
555   * library (e.g., :type:`nghttp2_on_frame_recv_callback`), |value|
556   * is guaranteed to be NULL-terminated.  For some callbacks
557   * (:type:`nghttp2_before_frame_send_callback`,
558   * :type:`nghttp2_on_frame_send_callback`, and
559   * :type:`nghttp2_on_frame_not_send_callback`), it may not be
560   * NULL-terminated if header field is passed from application with
561   * the flag :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_VALUE`).
562   * When application is constructing this struct, |value| is not
563   * required to be NULL-terminated.
564   */
565  uint8_t *value;
566  /**
567   * The length of the |name|, excluding terminating NULL.
568   */
569  size_t namelen;
570  /**
571   * The length of the |value|, excluding terminating NULL.
572   */
573  size_t valuelen;
574  /**
575   * Bitwise OR of one or more of :type:`nghttp2_nv_flag`.
576   */
577  uint8_t flags;
578} nghttp2_nv;
579
580/**
581 * @enum
582 *
583 * The frame types in HTTP/2 specification.
584 */
585typedef enum {
586  /**
587   * The DATA frame.
588   */
589  NGHTTP2_DATA = 0,
590  /**
591   * The HEADERS frame.
592   */
593  NGHTTP2_HEADERS = 0x01,
594  /**
595   * The PRIORITY frame.
596   */
597  NGHTTP2_PRIORITY = 0x02,
598  /**
599   * The RST_STREAM frame.
600   */
601  NGHTTP2_RST_STREAM = 0x03,
602  /**
603   * The SETTINGS frame.
604   */
605  NGHTTP2_SETTINGS = 0x04,
606  /**
607   * The PUSH_PROMISE frame.
608   */
609  NGHTTP2_PUSH_PROMISE = 0x05,
610  /**
611   * The PING frame.
612   */
613  NGHTTP2_PING = 0x06,
614  /**
615   * The GOAWAY frame.
616   */
617  NGHTTP2_GOAWAY = 0x07,
618  /**
619   * The WINDOW_UPDATE frame.
620   */
621  NGHTTP2_WINDOW_UPDATE = 0x08,
622  /**
623   * The CONTINUATION frame.  This frame type won't be passed to any
624   * callbacks because the library processes this frame type and its
625   * preceding HEADERS/PUSH_PROMISE as a single frame.
626   */
627  NGHTTP2_CONTINUATION = 0x09,
628  /**
629   * The ALTSVC frame, which is defined in `RFC 7383
630   * <https://tools.ietf.org/html/rfc7838#section-4>`_.
631   */
632  NGHTTP2_ALTSVC = 0x0a,
633  /**
634   * The ORIGIN frame, which is defined by `RFC 8336
635   * <https://tools.ietf.org/html/rfc8336>`_.
636   */
637  NGHTTP2_ORIGIN = 0x0c,
638  /**
639   * The PRIORITY_UPDATE frame, which is defined by :rfc:`9218`.
640   */
641  NGHTTP2_PRIORITY_UPDATE = 0x10
642} nghttp2_frame_type;
643
644/**
645 * @enum
646 *
647 * The flags for HTTP/2 frames.  This enum defines all flags for all
648 * frames.
649 */
650typedef enum {
651  /**
652   * No flag set.
653   */
654  NGHTTP2_FLAG_NONE = 0,
655  /**
656   * The END_STREAM flag.
657   */
658  NGHTTP2_FLAG_END_STREAM = 0x01,
659  /**
660   * The END_HEADERS flag.
661   */
662  NGHTTP2_FLAG_END_HEADERS = 0x04,
663  /**
664   * The ACK flag.
665   */
666  NGHTTP2_FLAG_ACK = 0x01,
667  /**
668   * The PADDED flag.
669   */
670  NGHTTP2_FLAG_PADDED = 0x08,
671  /**
672   * The PRIORITY flag.
673   */
674  NGHTTP2_FLAG_PRIORITY = 0x20
675} nghttp2_flag;
676
677/**
678 * @enum
679 * The SETTINGS ID.
680 */
681typedef enum {
682  /**
683   * SETTINGS_HEADER_TABLE_SIZE
684   */
685  NGHTTP2_SETTINGS_HEADER_TABLE_SIZE = 0x01,
686  /**
687   * SETTINGS_ENABLE_PUSH
688   */
689  NGHTTP2_SETTINGS_ENABLE_PUSH = 0x02,
690  /**
691   * SETTINGS_MAX_CONCURRENT_STREAMS
692   */
693  NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS = 0x03,
694  /**
695   * SETTINGS_INITIAL_WINDOW_SIZE
696   */
697  NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE = 0x04,
698  /**
699   * SETTINGS_MAX_FRAME_SIZE
700   */
701  NGHTTP2_SETTINGS_MAX_FRAME_SIZE = 0x05,
702  /**
703   * SETTINGS_MAX_HEADER_LIST_SIZE
704   */
705  NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE = 0x06,
706  /**
707   * SETTINGS_ENABLE_CONNECT_PROTOCOL
708   * (`RFC 8441 <https://tools.ietf.org/html/rfc8441>`_)
709   */
710  NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL = 0x08,
711  /**
712   * SETTINGS_NO_RFC7540_PRIORITIES (:rfc:`9218`)
713   */
714  NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES = 0x09
715} nghttp2_settings_id;
716/* Note: If we add SETTINGS, update the capacity of
717   NGHTTP2_INBOUND_NUM_IV as well */
718
719/**
720 * @macro
721 *
722 * .. warning::
723 *
724 *   Deprecated.  The initial max concurrent streams is 0xffffffffu.
725 *
726 * Default maximum number of incoming concurrent streams.  Use
727 * `nghttp2_submit_settings()` with
728 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS`
729 * to change the maximum number of incoming concurrent streams.
730 *
731 * .. note::
732 *
733 *   The maximum number of outgoing concurrent streams is 100 by
734 *   default.
735 */
736#define NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS ((1U << 31) - 1)
737
738/**
739 * @enum
740 * The status codes for the RST_STREAM and GOAWAY frames.
741 */
742typedef enum {
743  /**
744   * No errors.
745   */
746  NGHTTP2_NO_ERROR = 0x00,
747  /**
748   * PROTOCOL_ERROR
749   */
750  NGHTTP2_PROTOCOL_ERROR = 0x01,
751  /**
752   * INTERNAL_ERROR
753   */
754  NGHTTP2_INTERNAL_ERROR = 0x02,
755  /**
756   * FLOW_CONTROL_ERROR
757   */
758  NGHTTP2_FLOW_CONTROL_ERROR = 0x03,
759  /**
760   * SETTINGS_TIMEOUT
761   */
762  NGHTTP2_SETTINGS_TIMEOUT = 0x04,
763  /**
764   * STREAM_CLOSED
765   */
766  NGHTTP2_STREAM_CLOSED = 0x05,
767  /**
768   * FRAME_SIZE_ERROR
769   */
770  NGHTTP2_FRAME_SIZE_ERROR = 0x06,
771  /**
772   * REFUSED_STREAM
773   */
774  NGHTTP2_REFUSED_STREAM = 0x07,
775  /**
776   * CANCEL
777   */
778  NGHTTP2_CANCEL = 0x08,
779  /**
780   * COMPRESSION_ERROR
781   */
782  NGHTTP2_COMPRESSION_ERROR = 0x09,
783  /**
784   * CONNECT_ERROR
785   */
786  NGHTTP2_CONNECT_ERROR = 0x0a,
787  /**
788   * ENHANCE_YOUR_CALM
789   */
790  NGHTTP2_ENHANCE_YOUR_CALM = 0x0b,
791  /**
792   * INADEQUATE_SECURITY
793   */
794  NGHTTP2_INADEQUATE_SECURITY = 0x0c,
795  /**
796   * HTTP_1_1_REQUIRED
797   */
798  NGHTTP2_HTTP_1_1_REQUIRED = 0x0d
799} nghttp2_error_code;
800
801/**
802 * @struct
803 * The frame header.
804 */
805typedef struct {
806  /**
807   * The length field of this frame, excluding frame header.
808   */
809  size_t length;
810  /**
811   * The stream identifier (aka, stream ID)
812   */
813  int32_t stream_id;
814  /**
815   * The type of this frame.  See `nghttp2_frame_type`.
816   */
817  uint8_t type;
818  /**
819   * The flags.
820   */
821  uint8_t flags;
822  /**
823   * Reserved bit in frame header.  Currently, this is always set to 0
824   * and application should not expect something useful in here.
825   */
826  uint8_t reserved;
827} nghttp2_frame_hd;
828
829/**
830 * @union
831 *
832 * This union represents the some kind of data source passed to
833 * :type:`nghttp2_data_source_read_callback`.
834 */
835typedef union {
836  /**
837   * The integer field, suitable for a file descriptor.
838   */
839  int fd;
840  /**
841   * The pointer to an arbitrary object.
842   */
843  void *ptr;
844} nghttp2_data_source;
845
846/**
847 * @enum
848 *
849 * The flags used to set in |data_flags| output parameter in
850 * :type:`nghttp2_data_source_read_callback`.
851 */
852typedef enum {
853  /**
854   * No flag set.
855   */
856  NGHTTP2_DATA_FLAG_NONE = 0,
857  /**
858   * Indicates EOF was sensed.
859   */
860  NGHTTP2_DATA_FLAG_EOF = 0x01,
861  /**
862   * Indicates that END_STREAM flag must not be set even if
863   * NGHTTP2_DATA_FLAG_EOF is set.  Usually this flag is used to send
864   * trailer fields with `nghttp2_submit_request()` or
865   * `nghttp2_submit_response()`.
866   */
867  NGHTTP2_DATA_FLAG_NO_END_STREAM = 0x02,
868  /**
869   * Indicates that application will send complete DATA frame in
870   * :type:`nghttp2_send_data_callback`.
871   */
872  NGHTTP2_DATA_FLAG_NO_COPY = 0x04
873} nghttp2_data_flag;
874
875/**
876 * @functypedef
877 *
878 * Callback function invoked when the library wants to read data from
879 * the |source|.  The read data is sent in the stream |stream_id|.
880 * The implementation of this function must read at most |length|
881 * bytes of data from |source| (or possibly other places) and store
882 * them in |buf| and return number of data stored in |buf|.  If EOF is
883 * reached, set :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_EOF` flag
884 * in |*data_flags|.
885 *
886 * Sometime it is desirable to avoid copying data into |buf| and let
887 * application to send data directly.  To achieve this, set
888 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_NO_COPY` to
889 * |*data_flags| (and possibly other flags, just like when we do
890 * copy), and return the number of bytes to send without copying data
891 * into |buf|.  The library, seeing
892 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_NO_COPY`, will invoke
893 * :type:`nghttp2_send_data_callback`.  The application must send
894 * complete DATA frame in that callback.
895 *
896 * If this callback is set by `nghttp2_submit_request()`,
897 * `nghttp2_submit_response()` or `nghttp2_submit_headers()` and
898 * `nghttp2_submit_data()` with flag parameter
899 * :enum:`nghttp2_flag.NGHTTP2_FLAG_END_STREAM` set, and
900 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_EOF` flag is set to
901 * |*data_flags|, DATA frame will have END_STREAM flag set.  Usually,
902 * this is expected behaviour and all are fine.  One exception is send
903 * trailer fields.  You cannot send trailer fields after sending frame
904 * with END_STREAM set.  To avoid this problem, one can set
905 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_NO_END_STREAM` along
906 * with :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_EOF` to signal the
907 * library not to set END_STREAM in DATA frame.  Then application can
908 * use `nghttp2_submit_trailer()` to send trailer fields.
909 * `nghttp2_submit_trailer()` can be called inside this callback.
910 *
911 * If the application wants to postpone DATA frames (e.g.,
912 * asynchronous I/O, or reading data blocks for long time), it is
913 * achieved by returning :enum:`nghttp2_error.NGHTTP2_ERR_DEFERRED`
914 * without reading any data in this invocation.  The library removes
915 * DATA frame from the outgoing queue temporarily.  To move back
916 * deferred DATA frame to outgoing queue, call
917 * `nghttp2_session_resume_data()`.
918 *
919 * By default, |length| is limited to 16KiB at maximum.  If peer
920 * allows larger frames, application can enlarge transmission buffer
921 * size.  See :type:`nghttp2_data_source_read_length_callback` for
922 * more details.
923 *
924 * If the application just wants to return from
925 * `nghttp2_session_send()` or `nghttp2_session_mem_send()` without
926 * sending anything, return :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE`.
927 *
928 * In case of error, there are 2 choices. Returning
929 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will
930 * close the stream by issuing RST_STREAM with
931 * :enum:`nghttp2_error_code.NGHTTP2_INTERNAL_ERROR`.  If a different
932 * error code is desirable, use `nghttp2_submit_rst_stream()` with a
933 * desired error code and then return
934 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
935 * Returning :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` will
936 * signal the entire session failure.
937 */
938typedef ssize_t (*nghttp2_data_source_read_callback)(
939    nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t length,
940    uint32_t *data_flags, nghttp2_data_source *source, void *user_data);
941
942/**
943 * @struct
944 *
945 * This struct represents the data source and the way to read a chunk
946 * of data from it.
947 */
948typedef struct {
949  /**
950   * The data source.
951   */
952  nghttp2_data_source source;
953  /**
954   * The callback function to read a chunk of data from the |source|.
955   */
956  nghttp2_data_source_read_callback read_callback;
957} nghttp2_data_provider;
958
959/**
960 * @struct
961 *
962 * The DATA frame.  The received data is delivered via
963 * :type:`nghttp2_on_data_chunk_recv_callback`.
964 */
965typedef struct {
966  nghttp2_frame_hd hd;
967  /**
968   * The length of the padding in this frame.  This includes PAD_HIGH
969   * and PAD_LOW.
970   */
971  size_t padlen;
972} nghttp2_data;
973
974/**
975 * @enum
976 *
977 * The category of HEADERS, which indicates the role of the frame.  In
978 * HTTP/2 spec, request, response, push response and other arbitrary
979 * headers (e.g., trailer fields) are all called just HEADERS.  To
980 * give the application the role of incoming HEADERS frame, we define
981 * several categories.
982 */
983typedef enum {
984  /**
985   * The HEADERS frame is opening new stream, which is analogous to
986   * SYN_STREAM in SPDY.
987   */
988  NGHTTP2_HCAT_REQUEST = 0,
989  /**
990   * The HEADERS frame is the first response headers, which is
991   * analogous to SYN_REPLY in SPDY.
992   */
993  NGHTTP2_HCAT_RESPONSE = 1,
994  /**
995   * The HEADERS frame is the first headers sent against reserved
996   * stream.
997   */
998  NGHTTP2_HCAT_PUSH_RESPONSE = 2,
999  /**
1000   * The HEADERS frame which does not apply for the above categories,
1001   * which is analogous to HEADERS in SPDY.  If non-final response
1002   * (e.g., status 1xx) is used, final response HEADERS frame will be
1003   * categorized here.
1004   */
1005  NGHTTP2_HCAT_HEADERS = 3
1006} nghttp2_headers_category;
1007
1008/**
1009 * @struct
1010 *
1011 * The structure to specify stream dependency.
1012 */
1013typedef struct {
1014  /**
1015   * The stream ID of the stream to depend on.  Specifying 0 makes
1016   * stream not depend any other stream.
1017   */
1018  int32_t stream_id;
1019  /**
1020   * The weight of this dependency.
1021   */
1022  int32_t weight;
1023  /**
1024   * nonzero means exclusive dependency
1025   */
1026  uint8_t exclusive;
1027} nghttp2_priority_spec;
1028
1029/**
1030 * @struct
1031 *
1032 * The HEADERS frame.  It has the following members:
1033 */
1034typedef struct {
1035  /**
1036   * The frame header.
1037   */
1038  nghttp2_frame_hd hd;
1039  /**
1040   * The length of the padding in this frame.  This includes PAD_HIGH
1041   * and PAD_LOW.
1042   */
1043  size_t padlen;
1044  /**
1045   * The priority specification
1046   */
1047  nghttp2_priority_spec pri_spec;
1048  /**
1049   * The name/value pairs.
1050   */
1051  nghttp2_nv *nva;
1052  /**
1053   * The number of name/value pairs in |nva|.
1054   */
1055  size_t nvlen;
1056  /**
1057   * The category of this HEADERS frame.
1058   */
1059  nghttp2_headers_category cat;
1060} nghttp2_headers;
1061
1062/**
1063 * @struct
1064 *
1065 * The PRIORITY frame.  It has the following members:
1066 */
1067typedef struct {
1068  /**
1069   * The frame header.
1070   */
1071  nghttp2_frame_hd hd;
1072  /**
1073   * The priority specification.
1074   */
1075  nghttp2_priority_spec pri_spec;
1076} nghttp2_priority;
1077
1078/**
1079 * @struct
1080 *
1081 * The RST_STREAM frame.  It has the following members:
1082 */
1083typedef struct {
1084  /**
1085   * The frame header.
1086   */
1087  nghttp2_frame_hd hd;
1088  /**
1089   * The error code.  See :type:`nghttp2_error_code`.
1090   */
1091  uint32_t error_code;
1092} nghttp2_rst_stream;
1093
1094/**
1095 * @struct
1096 *
1097 * The SETTINGS ID/Value pair.  It has the following members:
1098 */
1099typedef struct {
1100  /**
1101   * The SETTINGS ID.  See :type:`nghttp2_settings_id`.
1102   */
1103  int32_t settings_id;
1104  /**
1105   * The value of this entry.
1106   */
1107  uint32_t value;
1108} nghttp2_settings_entry;
1109
1110/**
1111 * @struct
1112 *
1113 * The SETTINGS frame.  It has the following members:
1114 */
1115typedef struct {
1116  /**
1117   * The frame header.
1118   */
1119  nghttp2_frame_hd hd;
1120  /**
1121   * The number of SETTINGS ID/Value pairs in |iv|.
1122   */
1123  size_t niv;
1124  /**
1125   * The pointer to the array of SETTINGS ID/Value pair.
1126   */
1127  nghttp2_settings_entry *iv;
1128} nghttp2_settings;
1129
1130/**
1131 * @struct
1132 *
1133 * The PUSH_PROMISE frame.  It has the following members:
1134 */
1135typedef struct {
1136  /**
1137   * The frame header.
1138   */
1139  nghttp2_frame_hd hd;
1140  /**
1141   * The length of the padding in this frame.  This includes PAD_HIGH
1142   * and PAD_LOW.
1143   */
1144  size_t padlen;
1145  /**
1146   * The name/value pairs.
1147   */
1148  nghttp2_nv *nva;
1149  /**
1150   * The number of name/value pairs in |nva|.
1151   */
1152  size_t nvlen;
1153  /**
1154   * The promised stream ID
1155   */
1156  int32_t promised_stream_id;
1157  /**
1158   * Reserved bit.  Currently this is always set to 0 and application
1159   * should not expect something useful in here.
1160   */
1161  uint8_t reserved;
1162} nghttp2_push_promise;
1163
1164/**
1165 * @struct
1166 *
1167 * The PING frame.  It has the following members:
1168 */
1169typedef struct {
1170  /**
1171   * The frame header.
1172   */
1173  nghttp2_frame_hd hd;
1174  /**
1175   * The opaque data
1176   */
1177  uint8_t opaque_data[8];
1178} nghttp2_ping;
1179
1180/**
1181 * @struct
1182 *
1183 * The GOAWAY frame.  It has the following members:
1184 */
1185typedef struct {
1186  /**
1187   * The frame header.
1188   */
1189  nghttp2_frame_hd hd;
1190  /**
1191   * The last stream stream ID.
1192   */
1193  int32_t last_stream_id;
1194  /**
1195   * The error code.  See :type:`nghttp2_error_code`.
1196   */
1197  uint32_t error_code;
1198  /**
1199   * The additional debug data
1200   */
1201  uint8_t *opaque_data;
1202  /**
1203   * The length of |opaque_data| member.
1204   */
1205  size_t opaque_data_len;
1206  /**
1207   * Reserved bit.  Currently this is always set to 0 and application
1208   * should not expect something useful in here.
1209   */
1210  uint8_t reserved;
1211} nghttp2_goaway;
1212
1213/**
1214 * @struct
1215 *
1216 * The WINDOW_UPDATE frame.  It has the following members:
1217 */
1218typedef struct {
1219  /**
1220   * The frame header.
1221   */
1222  nghttp2_frame_hd hd;
1223  /**
1224   * The window size increment.
1225   */
1226  int32_t window_size_increment;
1227  /**
1228   * Reserved bit.  Currently this is always set to 0 and application
1229   * should not expect something useful in here.
1230   */
1231  uint8_t reserved;
1232} nghttp2_window_update;
1233
1234/**
1235 * @struct
1236 *
1237 * The extension frame.  It has following members:
1238 */
1239typedef struct {
1240  /**
1241   * The frame header.
1242   */
1243  nghttp2_frame_hd hd;
1244  /**
1245   * The pointer to extension payload.  The exact pointer type is
1246   * determined by hd.type.
1247   *
1248   * Currently, no extension is supported.  This is a place holder for
1249   * the future extensions.
1250   */
1251  void *payload;
1252} nghttp2_extension;
1253
1254/**
1255 * @union
1256 *
1257 * This union includes all frames to pass them to various function
1258 * calls as nghttp2_frame type.  The CONTINUATION frame is omitted
1259 * from here because the library deals with it internally.
1260 */
1261typedef union {
1262  /**
1263   * The frame header, which is convenient to inspect frame header.
1264   */
1265  nghttp2_frame_hd hd;
1266  /**
1267   * The DATA frame.
1268   */
1269  nghttp2_data data;
1270  /**
1271   * The HEADERS frame.
1272   */
1273  nghttp2_headers headers;
1274  /**
1275   * The PRIORITY frame.
1276   */
1277  nghttp2_priority priority;
1278  /**
1279   * The RST_STREAM frame.
1280   */
1281  nghttp2_rst_stream rst_stream;
1282  /**
1283   * The SETTINGS frame.
1284   */
1285  nghttp2_settings settings;
1286  /**
1287   * The PUSH_PROMISE frame.
1288   */
1289  nghttp2_push_promise push_promise;
1290  /**
1291   * The PING frame.
1292   */
1293  nghttp2_ping ping;
1294  /**
1295   * The GOAWAY frame.
1296   */
1297  nghttp2_goaway goaway;
1298  /**
1299   * The WINDOW_UPDATE frame.
1300   */
1301  nghttp2_window_update window_update;
1302  /**
1303   * The extension frame.
1304   */
1305  nghttp2_extension ext;
1306} nghttp2_frame;
1307
1308/**
1309 * @functypedef
1310 *
1311 * Callback function invoked when |session| wants to send data to the
1312 * remote peer.  The implementation of this function must send at most
1313 * |length| bytes of data stored in |data|.  The |flags| is currently
1314 * not used and always 0. It must return the number of bytes sent if
1315 * it succeeds.  If it cannot send any single byte without blocking,
1316 * it must return :enum:`nghttp2_error.NGHTTP2_ERR_WOULDBLOCK`.  For
1317 * other errors, it must return
1318 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  The
1319 * |user_data| pointer is the third argument passed in to the call to
1320 * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
1321 *
1322 * This callback is required if the application uses
1323 * `nghttp2_session_send()` to send data to the remote endpoint.  If
1324 * the application uses solely `nghttp2_session_mem_send()` instead,
1325 * this callback function is unnecessary.
1326 *
1327 * To set this callback to :type:`nghttp2_session_callbacks`, use
1328 * `nghttp2_session_callbacks_set_send_callback()`.
1329 *
1330 * .. note::
1331 *
1332 *   The |length| may be very small.  If that is the case, and
1333 *   application disables Nagle algorithm (``TCP_NODELAY``), then just
1334 *   writing |data| to the network stack leads to very small packet,
1335 *   and it is very inefficient.  An application should be responsible
1336 *   to buffer up small chunks of data as necessary to avoid this
1337 *   situation.
1338 */
1339typedef ssize_t (*nghttp2_send_callback)(nghttp2_session *session,
1340                                         const uint8_t *data, size_t length,
1341                                         int flags, void *user_data);
1342
1343/**
1344 * @functypedef
1345 *
1346 * Callback function invoked when
1347 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_NO_COPY` is used in
1348 * :type:`nghttp2_data_source_read_callback` to send complete DATA
1349 * frame.
1350 *
1351 * The |frame| is a DATA frame to send.  The |framehd| is the
1352 * serialized frame header (9 bytes). The |length| is the length of
1353 * application data to send (this does not include padding).  The
1354 * |source| is the same pointer passed to
1355 * :type:`nghttp2_data_source_read_callback`.
1356 *
1357 * The application first must send frame header |framehd| of length 9
1358 * bytes.  If ``frame->data.padlen > 0``, send 1 byte of value
1359 * ``frame->data.padlen - 1``.  Then send exactly |length| bytes of
1360 * application data.  Finally, if ``frame->data.padlen > 1``, send
1361 * ``frame->data.padlen - 1`` bytes of zero as padding.
1362 *
1363 * The application has to send complete DATA frame in this callback.
1364 * If all data were written successfully, return 0.
1365 *
1366 * If it cannot send any data at all, just return
1367 * :enum:`nghttp2_error.NGHTTP2_ERR_WOULDBLOCK`; the library will call
1368 * this callback with the same parameters later (It is recommended to
1369 * send complete DATA frame at once in this function to deal with
1370 * error; if partial frame data has already sent, it is impossible to
1371 * send another data in that state, and all we can do is tear down
1372 * connection).  When data is fully processed, but application wants
1373 * to make `nghttp2_session_mem_send()` or `nghttp2_session_send()`
1374 * return immediately without processing next frames, return
1375 * :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE`.  If application decided to
1376 * reset this stream, return
1377 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`, then
1378 * the library will send RST_STREAM with INTERNAL_ERROR as error code.
1379 * The application can also return
1380 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`, which will
1381 * result in connection closure.  Returning any other value is treated
1382 * as :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` is returned.
1383 */
1384typedef int (*nghttp2_send_data_callback)(nghttp2_session *session,
1385                                          nghttp2_frame *frame,
1386                                          const uint8_t *framehd, size_t length,
1387                                          nghttp2_data_source *source,
1388                                          void *user_data);
1389
1390/**
1391 * @functypedef
1392 *
1393 * Callback function invoked when |session| wants to receive data from
1394 * the remote peer.  The implementation of this function must read at
1395 * most |length| bytes of data and store it in |buf|.  The |flags| is
1396 * currently not used and always 0.  It must return the number of
1397 * bytes written in |buf| if it succeeds.  If it cannot read any
1398 * single byte without blocking, it must return
1399 * :enum:`nghttp2_error.NGHTTP2_ERR_WOULDBLOCK`.  If it gets EOF
1400 * before it reads any single byte, it must return
1401 * :enum:`nghttp2_error.NGHTTP2_ERR_EOF`.  For other errors, it must
1402 * return :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1403 * Returning 0 is treated as
1404 * :enum:`nghttp2_error.NGHTTP2_ERR_WOULDBLOCK`.  The |user_data|
1405 * pointer is the third argument passed in to the call to
1406 * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
1407 *
1408 * This callback is required if the application uses
1409 * `nghttp2_session_recv()` to receive data from the remote endpoint.
1410 * If the application uses solely `nghttp2_session_mem_recv()`
1411 * instead, this callback function is unnecessary.
1412 *
1413 * To set this callback to :type:`nghttp2_session_callbacks`, use
1414 * `nghttp2_session_callbacks_set_recv_callback()`.
1415 */
1416typedef ssize_t (*nghttp2_recv_callback)(nghttp2_session *session, uint8_t *buf,
1417                                         size_t length, int flags,
1418                                         void *user_data);
1419
1420/**
1421 * @functypedef
1422 *
1423 * Callback function invoked by `nghttp2_session_recv()` and
1424 * `nghttp2_session_mem_recv()` when a frame is received.  The
1425 * |user_data| pointer is the third argument passed in to the call to
1426 * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
1427 *
1428 * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
1429 * member of their data structure are always ``NULL`` and 0
1430 * respectively.  The header name/value pairs are emitted via
1431 * :type:`nghttp2_on_header_callback`.
1432 *
1433 * Only HEADERS and DATA frame can signal the end of incoming data.
1434 * If ``frame->hd.flags & NGHTTP2_FLAG_END_STREAM`` is nonzero, the
1435 * |frame| is the last frame from the remote peer in this stream.
1436 *
1437 * This callback won't be called for CONTINUATION frames.
1438 * HEADERS/PUSH_PROMISE + CONTINUATIONs are treated as single frame.
1439 *
1440 * The implementation of this function must return 0 if it succeeds.
1441 * If nonzero value is returned, it is treated as fatal error and
1442 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
1443 * immediately return
1444 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1445 *
1446 * To set this callback to :type:`nghttp2_session_callbacks`, use
1447 * `nghttp2_session_callbacks_set_on_frame_recv_callback()`.
1448 */
1449typedef int (*nghttp2_on_frame_recv_callback)(nghttp2_session *session,
1450                                              const nghttp2_frame *frame,
1451                                              void *user_data);
1452
1453/**
1454 * @functypedef
1455 *
1456 * Callback function invoked by `nghttp2_session_recv()` and
1457 * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is
1458 * received.  The error is indicated by the |lib_error_code|, which is
1459 * one of the values defined in :type:`nghttp2_error`.  When this
1460 * callback function is invoked, the library automatically submits
1461 * either RST_STREAM or GOAWAY frame.  The |user_data| pointer is the
1462 * third argument passed in to the call to
1463 * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
1464 *
1465 * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
1466 * member of their data structure are always ``NULL`` and 0
1467 * respectively.
1468 *
1469 * The implementation of this function must return 0 if it succeeds.
1470 * If nonzero is returned, it is treated as fatal error and
1471 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
1472 * immediately return
1473 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1474 *
1475 * To set this callback to :type:`nghttp2_session_callbacks`, use
1476 * `nghttp2_session_callbacks_set_on_invalid_frame_recv_callback()`.
1477 */
1478typedef int (*nghttp2_on_invalid_frame_recv_callback)(
1479    nghttp2_session *session, const nghttp2_frame *frame, int lib_error_code,
1480    void *user_data);
1481
1482/**
1483 * @functypedef
1484 *
1485 * Callback function invoked when a chunk of data in DATA frame is
1486 * received.  The |stream_id| is the stream ID this DATA frame belongs
1487 * to.  The |flags| is the flags of DATA frame which this data chunk
1488 * is contained.  ``(flags & NGHTTP2_FLAG_END_STREAM) != 0`` does not
1489 * necessarily mean this chunk of data is the last one in the stream.
1490 * You should use :type:`nghttp2_on_frame_recv_callback` to know all
1491 * data frames are received.  The |user_data| pointer is the third
1492 * argument passed in to the call to `nghttp2_session_client_new()` or
1493 * `nghttp2_session_server_new()`.
1494 *
1495 * If the application uses `nghttp2_session_mem_recv()`, it can return
1496 * :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE` to make
1497 * `nghttp2_session_mem_recv()` return without processing further
1498 * input bytes.  The memory by pointed by the |data| is retained until
1499 * `nghttp2_session_mem_recv()` or `nghttp2_session_recv()` is called.
1500 * The application must retain the input bytes which was used to
1501 * produce the |data| parameter, because it may refer to the memory
1502 * region included in the input bytes.
1503 *
1504 * The implementation of this function must return 0 if it succeeds.
1505 * If nonzero is returned, it is treated as fatal error, and
1506 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
1507 * immediately return
1508 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1509 *
1510 * To set this callback to :type:`nghttp2_session_callbacks`, use
1511 * `nghttp2_session_callbacks_set_on_data_chunk_recv_callback()`.
1512 */
1513typedef int (*nghttp2_on_data_chunk_recv_callback)(nghttp2_session *session,
1514                                                   uint8_t flags,
1515                                                   int32_t stream_id,
1516                                                   const uint8_t *data,
1517                                                   size_t len, void *user_data);
1518
1519/**
1520 * @functypedef
1521 *
1522 * Callback function invoked just before the non-DATA frame |frame| is
1523 * sent.  The |user_data| pointer is the third argument passed in to
1524 * the call to `nghttp2_session_client_new()` or
1525 * `nghttp2_session_server_new()`.
1526 *
1527 * The implementation of this function must return 0 if it succeeds.
1528 * It can also return :enum:`nghttp2_error.NGHTTP2_ERR_CANCEL` to
1529 * cancel the transmission of the given frame.
1530 *
1531 * If there is a fatal error while executing this callback, the
1532 * implementation should return
1533 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`, which makes
1534 * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
1535 * immediately return
1536 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1537 *
1538 * If the other value is returned, it is treated as if
1539 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` is returned.
1540 * But the implementation should not rely on this since the library
1541 * may define new return value to extend its capability.
1542 *
1543 * To set this callback to :type:`nghttp2_session_callbacks`, use
1544 * `nghttp2_session_callbacks_set_before_frame_send_callback()`.
1545 */
1546typedef int (*nghttp2_before_frame_send_callback)(nghttp2_session *session,
1547                                                  const nghttp2_frame *frame,
1548                                                  void *user_data);
1549
1550/**
1551 * @functypedef
1552 *
1553 * Callback function invoked after the frame |frame| is sent.  The
1554 * |user_data| pointer is the third argument passed in to the call to
1555 * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
1556 *
1557 * The implementation of this function must return 0 if it succeeds.
1558 * If nonzero is returned, it is treated as fatal error and
1559 * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
1560 * immediately return
1561 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1562 *
1563 * To set this callback to :type:`nghttp2_session_callbacks`, use
1564 * `nghttp2_session_callbacks_set_on_frame_send_callback()`.
1565 */
1566typedef int (*nghttp2_on_frame_send_callback)(nghttp2_session *session,
1567                                              const nghttp2_frame *frame,
1568                                              void *user_data);
1569
1570/**
1571 * @functypedef
1572 *
1573 * Callback function invoked after the non-DATA frame |frame| is not
1574 * sent because of the error.  The error is indicated by the
1575 * |lib_error_code|, which is one of the values defined in
1576 * :type:`nghttp2_error`.  The |user_data| pointer is the third
1577 * argument passed in to the call to `nghttp2_session_client_new()` or
1578 * `nghttp2_session_server_new()`.
1579 *
1580 * The implementation of this function must return 0 if it succeeds.
1581 * If nonzero is returned, it is treated as fatal error and
1582 * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
1583 * immediately return
1584 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1585 *
1586 * `nghttp2_session_get_stream_user_data()` can be used to get
1587 * associated data.
1588 *
1589 * To set this callback to :type:`nghttp2_session_callbacks`, use
1590 * `nghttp2_session_callbacks_set_on_frame_not_send_callback()`.
1591 */
1592typedef int (*nghttp2_on_frame_not_send_callback)(nghttp2_session *session,
1593                                                  const nghttp2_frame *frame,
1594                                                  int lib_error_code,
1595                                                  void *user_data);
1596
1597/**
1598 * @functypedef
1599 *
1600 * Callback function invoked when the stream |stream_id| is closed.
1601 * The reason of closure is indicated by the |error_code|.  The
1602 * |error_code| is usually one of :enum:`nghttp2_error_code`, but that
1603 * is not guaranteed.  The stream_user_data, which was specified in
1604 * `nghttp2_submit_request()` or `nghttp2_submit_headers()`, is still
1605 * available in this function.  The |user_data| pointer is the third
1606 * argument passed in to the call to `nghttp2_session_client_new()` or
1607 * `nghttp2_session_server_new()`.
1608 *
1609 * This function is also called for a stream in reserved state.
1610 *
1611 * The implementation of this function must return 0 if it succeeds.
1612 * If nonzero is returned, it is treated as fatal error and
1613 * `nghttp2_session_recv()`, `nghttp2_session_mem_recv()`,
1614 * `nghttp2_session_send()`, and `nghttp2_session_mem_send()`
1615 * functions immediately return
1616 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1617 *
1618 * To set this callback to :type:`nghttp2_session_callbacks`, use
1619 * `nghttp2_session_callbacks_set_on_stream_close_callback()`.
1620 */
1621typedef int (*nghttp2_on_stream_close_callback)(nghttp2_session *session,
1622                                                int32_t stream_id,
1623                                                uint32_t error_code,
1624                                                void *user_data);
1625
1626/**
1627 * @functypedef
1628 *
1629 * Callback function invoked when the reception of header block in
1630 * HEADERS or PUSH_PROMISE is started.  Each header name/value pair
1631 * will be emitted by :type:`nghttp2_on_header_callback`.
1632 *
1633 * The ``frame->hd.flags`` may not have
1634 * :enum:`nghttp2_flag.NGHTTP2_FLAG_END_HEADERS` flag set, which
1635 * indicates that one or more CONTINUATION frames are involved.  But
1636 * the application does not need to care about that because the header
1637 * name/value pairs are emitted transparently regardless of
1638 * CONTINUATION frames.
1639 *
1640 * The server applications probably create an object to store
1641 * information about new stream if ``frame->hd.type ==
1642 * NGHTTP2_HEADERS`` and ``frame->headers.cat ==
1643 * NGHTTP2_HCAT_REQUEST``.  If |session| is configured as server side,
1644 * ``frame->headers.cat`` is either ``NGHTTP2_HCAT_REQUEST``
1645 * containing request headers or ``NGHTTP2_HCAT_HEADERS`` containing
1646 * trailer fields and never get PUSH_PROMISE in this callback.
1647 *
1648 * For the client applications, ``frame->hd.type`` is either
1649 * ``NGHTTP2_HEADERS`` or ``NGHTTP2_PUSH_PROMISE``.  In case of
1650 * ``NGHTTP2_HEADERS``, ``frame->headers.cat ==
1651 * NGHTTP2_HCAT_RESPONSE`` means that it is the first response
1652 * headers, but it may be non-final response which is indicated by 1xx
1653 * status code.  In this case, there may be zero or more HEADERS frame
1654 * with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS`` which has
1655 * non-final response code and finally client gets exactly one HEADERS
1656 * frame with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS``
1657 * containing final response headers (non-1xx status code).  The
1658 * trailer fields also has ``frame->headers.cat ==
1659 * NGHTTP2_HCAT_HEADERS`` which does not contain any status code.
1660 *
1661 * Returning
1662 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will
1663 * close the stream (promised stream if frame is PUSH_PROMISE) by
1664 * issuing RST_STREAM with
1665 * :enum:`nghttp2_error_code.NGHTTP2_INTERNAL_ERROR`.  In this case,
1666 * :type:`nghttp2_on_header_callback` and
1667 * :type:`nghttp2_on_frame_recv_callback` will not be invoked.  If a
1668 * different error code is desirable, use
1669 * `nghttp2_submit_rst_stream()` with a desired error code and then
1670 * return :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
1671 * Again, use ``frame->push_promise.promised_stream_id`` as stream_id
1672 * parameter in `nghttp2_submit_rst_stream()` if frame is
1673 * PUSH_PROMISE.
1674 *
1675 * The implementation of this function must return 0 if it succeeds.
1676 * It can return
1677 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` to
1678 * reset the stream (promised stream if frame is PUSH_PROMISE).  For
1679 * critical errors, it must return
1680 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If the other
1681 * value is returned, it is treated as if
1682 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` is returned.  If
1683 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` is returned,
1684 * `nghttp2_session_mem_recv()` function will immediately return
1685 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1686 *
1687 * To set this callback to :type:`nghttp2_session_callbacks`, use
1688 * `nghttp2_session_callbacks_set_on_begin_headers_callback()`.
1689 */
1690typedef int (*nghttp2_on_begin_headers_callback)(nghttp2_session *session,
1691                                                 const nghttp2_frame *frame,
1692                                                 void *user_data);
1693
1694/**
1695 * @functypedef
1696 *
1697 * Callback function invoked when a header name/value pair is received
1698 * for the |frame|.  The |name| of length |namelen| is header name.
1699 * The |value| of length |valuelen| is header value.  The |flags| is
1700 * bitwise OR of one or more of :type:`nghttp2_nv_flag`.
1701 *
1702 * If :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_INDEX` is set in
1703 * |flags|, the receiver must not index this name/value pair when
1704 * forwarding it to the next hop.  More specifically, "Literal Header
1705 * Field never Indexed" representation must be used in HPACK encoding.
1706 *
1707 * When this callback is invoked, ``frame->hd.type`` is either
1708 * :enum:`nghttp2_frame_type.NGHTTP2_HEADERS` or
1709 * :enum:`nghttp2_frame_type.NGHTTP2_PUSH_PROMISE`.  After all header
1710 * name/value pairs are processed with this callback, and no error has
1711 * been detected, :type:`nghttp2_on_frame_recv_callback` will be
1712 * invoked.  If there is an error in decompression,
1713 * :type:`nghttp2_on_frame_recv_callback` for the |frame| will not be
1714 * invoked.
1715 *
1716 * Both |name| and |value| are guaranteed to be NULL-terminated.  The
1717 * |namelen| and |valuelen| do not include terminal NULL.  If
1718 * `nghttp2_option_set_no_http_messaging()` is used with nonzero
1719 * value, NULL character may be included in |name| or |value| before
1720 * terminating NULL.
1721 *
1722 * Please note that unless `nghttp2_option_set_no_http_messaging()` is
1723 * used, nghttp2 library does perform validation against the |name|
1724 * and the |value| using `nghttp2_check_header_name()` and
1725 * `nghttp2_check_header_value()`.  In addition to this, nghttp2
1726 * performs validation based on HTTP Messaging rule, which is briefly
1727 * explained in :ref:`http-messaging` section.
1728 *
1729 * If the application uses `nghttp2_session_mem_recv()`, it can return
1730 * :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE` to make
1731 * `nghttp2_session_mem_recv()` return without processing further
1732 * input bytes.  The memory pointed by |frame|, |name| and |value|
1733 * parameters are retained until `nghttp2_session_mem_recv()` or
1734 * `nghttp2_session_recv()` is called.  The application must retain
1735 * the input bytes which was used to produce these parameters, because
1736 * it may refer to the memory region included in the input bytes.
1737 *
1738 * Returning
1739 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will
1740 * close the stream (promised stream if frame is PUSH_PROMISE) by
1741 * issuing RST_STREAM with
1742 * :enum:`nghttp2_error_code.NGHTTP2_INTERNAL_ERROR`.  In this case,
1743 * :type:`nghttp2_on_header_callback` and
1744 * :type:`nghttp2_on_frame_recv_callback` will not be invoked.  If a
1745 * different error code is desirable, use
1746 * `nghttp2_submit_rst_stream()` with a desired error code and then
1747 * return :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
1748 * Again, use ``frame->push_promise.promised_stream_id`` as stream_id
1749 * parameter in `nghttp2_submit_rst_stream()` if frame is
1750 * PUSH_PROMISE.
1751 *
1752 * The implementation of this function must return 0 if it succeeds.
1753 * It may return :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE` or
1754 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  For
1755 * other critical failures, it must return
1756 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If the other
1757 * nonzero value is returned, it is treated as
1758 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If
1759 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` is returned,
1760 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
1761 * immediately return
1762 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1763 *
1764 * To set this callback to :type:`nghttp2_session_callbacks`, use
1765 * `nghttp2_session_callbacks_set_on_header_callback()`.
1766 *
1767 * .. warning::
1768 *
1769 *   Application should properly limit the total buffer size to store
1770 *   incoming header fields.  Without it, peer may send large number
1771 *   of header fields or large header fields to cause out of memory in
1772 *   local endpoint.  Due to how HPACK works, peer can do this
1773 *   effectively without using much memory on their own.
1774 */
1775typedef int (*nghttp2_on_header_callback)(nghttp2_session *session,
1776                                          const nghttp2_frame *frame,
1777                                          const uint8_t *name, size_t namelen,
1778                                          const uint8_t *value, size_t valuelen,
1779                                          uint8_t flags, void *user_data);
1780
1781/**
1782 * @functypedef
1783 *
1784 * Callback function invoked when a header name/value pair is received
1785 * for the |frame|.  The |name| is header name.  The |value| is header
1786 * value.  The |flags| is bitwise OR of one or more of
1787 * :type:`nghttp2_nv_flag`.
1788 *
1789 * This callback behaves like :type:`nghttp2_on_header_callback`,
1790 * except that |name| and |value| are stored in reference counted
1791 * buffer.  If application wishes to keep these references without
1792 * copying them, use `nghttp2_rcbuf_incref()` to increment their
1793 * reference count.  It is the application's responsibility to call
1794 * `nghttp2_rcbuf_decref()` if they called `nghttp2_rcbuf_incref()` so
1795 * as not to leak memory.  If the |session| is created by
1796 * `nghttp2_session_server_new3()` or `nghttp2_session_client_new3()`,
1797 * the function to free memory is the one belongs to the mem
1798 * parameter.  As long as this free function alives, |name| and
1799 * |value| can live after |session| was destroyed.
1800 */
1801typedef int (*nghttp2_on_header_callback2)(nghttp2_session *session,
1802                                           const nghttp2_frame *frame,
1803                                           nghttp2_rcbuf *name,
1804                                           nghttp2_rcbuf *value, uint8_t flags,
1805                                           void *user_data);
1806
1807/**
1808 * @functypedef
1809 *
1810 * Callback function invoked when a invalid header name/value pair is
1811 * received for the |frame|.
1812 *
1813 * The parameter and behaviour are similar to
1814 * :type:`nghttp2_on_header_callback`.  The difference is that this
1815 * callback is only invoked when a invalid header name/value pair is
1816 * received which is treated as stream error if this callback is not
1817 * set.  Only invalid regular header field are passed to this
1818 * callback.  In other words, invalid pseudo header field is not
1819 * passed to this callback.  Also header fields which includes upper
1820 * cased latter are also treated as error without passing them to this
1821 * callback.
1822 *
1823 * This callback is only considered if HTTP messaging validation is
1824 * turned on (which is on by default, see
1825 * `nghttp2_option_set_no_http_messaging()`).
1826 *
1827 * With this callback, application inspects the incoming invalid
1828 * field, and it also can reset stream from this callback by returning
1829 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  By
1830 * default, the error code is
1831 * :enum:`nghttp2_error_code.NGHTTP2_PROTOCOL_ERROR`.  To change the
1832 * error code, call `nghttp2_submit_rst_stream()` with the error code
1833 * of choice in addition to returning
1834 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
1835 *
1836 * If 0 is returned, the header field is ignored, and the stream is
1837 * not reset.
1838 */
1839typedef int (*nghttp2_on_invalid_header_callback)(
1840    nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name,
1841    size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags,
1842    void *user_data);
1843
1844/**
1845 * @functypedef
1846 *
1847 * Callback function invoked when a invalid header name/value pair is
1848 * received for the |frame|.
1849 *
1850 * The parameter and behaviour are similar to
1851 * :type:`nghttp2_on_header_callback2`.  The difference is that this
1852 * callback is only invoked when a invalid header name/value pair is
1853 * received which is silently ignored if this callback is not set.
1854 * Only invalid regular header field are passed to this callback.  In
1855 * other words, invalid pseudo header field is not passed to this
1856 * callback.  Also header fields which includes upper cased latter are
1857 * also treated as error without passing them to this callback.
1858 *
1859 * This callback is only considered if HTTP messaging validation is
1860 * turned on (which is on by default, see
1861 * `nghttp2_option_set_no_http_messaging()`).
1862 *
1863 * With this callback, application inspects the incoming invalid
1864 * field, and it also can reset stream from this callback by returning
1865 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  By
1866 * default, the error code is
1867 * :enum:`nghttp2_error_code.NGHTTP2_INTERNAL_ERROR`.  To change the
1868 * error code, call `nghttp2_submit_rst_stream()` with the error code
1869 * of choice in addition to returning
1870 * :enum:`nghttp2_error.NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
1871 */
1872typedef int (*nghttp2_on_invalid_header_callback2)(
1873    nghttp2_session *session, const nghttp2_frame *frame, nghttp2_rcbuf *name,
1874    nghttp2_rcbuf *value, uint8_t flags, void *user_data);
1875
1876/**
1877 * @functypedef
1878 *
1879 * Callback function invoked when the library asks application how
1880 * many padding bytes are required for the transmission of the
1881 * |frame|.  The application must choose the total length of payload
1882 * including padded bytes in range [frame->hd.length, max_payloadlen],
1883 * inclusive.  Choosing number not in this range will be treated as
1884 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  Returning
1885 * ``frame->hd.length`` means no padding is added.  Returning
1886 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` will make
1887 * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
1888 * immediately return
1889 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1890 *
1891 * To set this callback to :type:`nghttp2_session_callbacks`, use
1892 * `nghttp2_session_callbacks_set_select_padding_callback()`.
1893 */
1894typedef ssize_t (*nghttp2_select_padding_callback)(nghttp2_session *session,
1895                                                   const nghttp2_frame *frame,
1896                                                   size_t max_payloadlen,
1897                                                   void *user_data);
1898
1899/**
1900 * @functypedef
1901 *
1902 * Callback function invoked when library wants to get max length of
1903 * data to send data to the remote peer.  The implementation of this
1904 * function should return a value in the following range.  [1,
1905 * min(|session_remote_window_size|, |stream_remote_window_size|,
1906 * |remote_max_frame_size|)].  If a value greater than this range is
1907 * returned than the max allow value will be used.  Returning a value
1908 * smaller than this range is treated as
1909 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  The
1910 * |frame_type| is provided for future extensibility and identifies
1911 * the type of frame (see :type:`nghttp2_frame_type`) for which to get
1912 * the length for.  Currently supported frame types are:
1913 * :enum:`nghttp2_frame_type.NGHTTP2_DATA`.
1914 *
1915 * This callback can be used to control the length in bytes for which
1916 * :type:`nghttp2_data_source_read_callback` is allowed to send to the
1917 * remote endpoint.  This callback is optional.  Returning
1918 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE` will signal the
1919 * entire session failure.
1920 *
1921 * To set this callback to :type:`nghttp2_session_callbacks`, use
1922 * `nghttp2_session_callbacks_set_data_source_read_length_callback()`.
1923 */
1924typedef ssize_t (*nghttp2_data_source_read_length_callback)(
1925    nghttp2_session *session, uint8_t frame_type, int32_t stream_id,
1926    int32_t session_remote_window_size, int32_t stream_remote_window_size,
1927    uint32_t remote_max_frame_size, void *user_data);
1928
1929/**
1930 * @functypedef
1931 *
1932 * Callback function invoked when a frame header is received.  The
1933 * |hd| points to received frame header.
1934 *
1935 * Unlike :type:`nghttp2_on_frame_recv_callback`, this callback will
1936 * also be called when frame header of CONTINUATION frame is received.
1937 *
1938 * If both :type:`nghttp2_on_begin_frame_callback` and
1939 * :type:`nghttp2_on_begin_headers_callback` are set and HEADERS or
1940 * PUSH_PROMISE is received, :type:`nghttp2_on_begin_frame_callback`
1941 * will be called first.
1942 *
1943 * The implementation of this function must return 0 if it succeeds.
1944 * If nonzero value is returned, it is treated as fatal error and
1945 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
1946 * immediately return
1947 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1948 *
1949 * To set this callback to :type:`nghttp2_session_callbacks`, use
1950 * `nghttp2_session_callbacks_set_on_begin_frame_callback()`.
1951 */
1952typedef int (*nghttp2_on_begin_frame_callback)(nghttp2_session *session,
1953                                               const nghttp2_frame_hd *hd,
1954                                               void *user_data);
1955
1956/**
1957 * @functypedef
1958 *
1959 * Callback function invoked when chunk of extension frame payload is
1960 * received.  The |hd| points to frame header.  The received
1961 * chunk is |data| of length |len|.
1962 *
1963 * The implementation of this function must return 0 if it succeeds.
1964 *
1965 * To abort processing this extension frame, return
1966 * :enum:`nghttp2_error.NGHTTP2_ERR_CANCEL`.
1967 *
1968 * If fatal error occurred, application should return
1969 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
1970 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
1971 * immediately return
1972 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If the other
1973 * values are returned, currently they are treated as
1974 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
1975 */
1976typedef int (*nghttp2_on_extension_chunk_recv_callback)(
1977    nghttp2_session *session, const nghttp2_frame_hd *hd, const uint8_t *data,
1978    size_t len, void *user_data);
1979
1980/**
1981 * @functypedef
1982 *
1983 * Callback function invoked when library asks the application to
1984 * unpack extension payload from its wire format.  The extension
1985 * payload has been passed to the application using
1986 * :type:`nghttp2_on_extension_chunk_recv_callback`.  The frame header
1987 * is already unpacked by the library and provided as |hd|.
1988 *
1989 * To receive extension frames, the application must tell desired
1990 * extension frame type to the library using
1991 * `nghttp2_option_set_user_recv_extension_type()`.
1992 *
1993 * The implementation of this function may store the pointer to the
1994 * created object as a result of unpacking in |*payload|, and returns
1995 * 0.  The pointer stored in |*payload| is opaque to the library, and
1996 * the library does not own its pointer.  |*payload| is initialized as
1997 * ``NULL``.  The |*payload| is available as ``frame->ext.payload`` in
1998 * :type:`nghttp2_on_frame_recv_callback`.  Therefore if application
1999 * can free that memory inside :type:`nghttp2_on_frame_recv_callback`
2000 * callback.  Of course, application has a liberty not ot use
2001 * |*payload|, and do its own mechanism to process extension frames.
2002 *
2003 * To abort processing this extension frame, return
2004 * :enum:`nghttp2_error.NGHTTP2_ERR_CANCEL`.
2005 *
2006 * If fatal error occurred, application should return
2007 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
2008 * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
2009 * immediately return
2010 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If the other
2011 * values are returned, currently they are treated as
2012 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
2013 */
2014typedef int (*nghttp2_unpack_extension_callback)(nghttp2_session *session,
2015                                                 void **payload,
2016                                                 const nghttp2_frame_hd *hd,
2017                                                 void *user_data);
2018
2019/**
2020 * @functypedef
2021 *
2022 * Callback function invoked when library asks the application to pack
2023 * extension payload in its wire format.  The frame header will be
2024 * packed by library.  Application must pack payload only.
2025 * ``frame->ext.payload`` is the object passed to
2026 * `nghttp2_submit_extension()` as payload parameter.  Application
2027 * must pack extension payload to the |buf| of its capacity |len|
2028 * bytes.  The |len| is at least 16KiB.
2029 *
2030 * The implementation of this function should return the number of
2031 * bytes written into |buf| when it succeeds.
2032 *
2033 * To abort processing this extension frame, return
2034 * :enum:`nghttp2_error.NGHTTP2_ERR_CANCEL`, and
2035 * :type:`nghttp2_on_frame_not_send_callback` will be invoked.
2036 *
2037 * If fatal error occurred, application should return
2038 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
2039 * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
2040 * immediately return
2041 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If the other
2042 * values are returned, currently they are treated as
2043 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  If the return
2044 * value is strictly larger than |len|, it is treated as
2045 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
2046 */
2047typedef ssize_t (*nghttp2_pack_extension_callback)(nghttp2_session *session,
2048                                                   uint8_t *buf, size_t len,
2049                                                   const nghttp2_frame *frame,
2050                                                   void *user_data);
2051
2052/**
2053 * @functypedef
2054 *
2055 * Callback function invoked when library provides the error message
2056 * intended for human consumption.  This callback is solely for
2057 * debugging purpose.  The |msg| is typically NULL-terminated string
2058 * of length |len|.  |len| does not include the sentinel NULL
2059 * character.
2060 *
2061 * This function is deprecated.  The new application should use
2062 * :type:`nghttp2_error_callback2`.
2063 *
2064 * The format of error message may change between nghttp2 library
2065 * versions.  The application should not depend on the particular
2066 * format.
2067 *
2068 * Normally, application should return 0 from this callback.  If fatal
2069 * error occurred while doing something in this callback, application
2070 * should return :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
2071 * In this case, library will return immediately with return value
2072 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  Currently, if
2073 * nonzero value is returned from this callback, they are treated as
2074 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`, but application
2075 * should not rely on this details.
2076 */
2077typedef int (*nghttp2_error_callback)(nghttp2_session *session, const char *msg,
2078                                      size_t len, void *user_data);
2079
2080/**
2081 * @functypedef
2082 *
2083 * Callback function invoked when library provides the error code, and
2084 * message.  This callback is solely for debugging purpose.
2085 * |lib_error_code| is one of error code defined in
2086 * :enum:`nghttp2_error`.  The |msg| is typically NULL-terminated
2087 * string of length |len|, and intended for human consumption.  |len|
2088 * does not include the sentinel NULL character.
2089 *
2090 * The format of error message may change between nghttp2 library
2091 * versions.  The application should not depend on the particular
2092 * format.
2093 *
2094 * Normally, application should return 0 from this callback.  If fatal
2095 * error occurred while doing something in this callback, application
2096 * should return :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.
2097 * In this case, library will return immediately with return value
2098 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`.  Currently, if
2099 * nonzero value is returned from this callback, they are treated as
2100 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`, but application
2101 * should not rely on this details.
2102 */
2103typedef int (*nghttp2_error_callback2)(nghttp2_session *session,
2104                                       int lib_error_code, const char *msg,
2105                                       size_t len, void *user_data);
2106
2107struct nghttp2_session_callbacks;
2108
2109/**
2110 * @struct
2111 *
2112 * Callback functions for :type:`nghttp2_session`.  The details of
2113 * this structure are intentionally hidden from the public API.
2114 */
2115typedef struct nghttp2_session_callbacks nghttp2_session_callbacks;
2116
2117/**
2118 * @function
2119 *
2120 * Initializes |*callbacks_ptr| with NULL values.
2121 *
2122 * The initialized object can be used when initializing multiple
2123 * :type:`nghttp2_session` objects.
2124 *
2125 * When the application finished using this object, it can use
2126 * `nghttp2_session_callbacks_del()` to free its memory.
2127 *
2128 * This function returns 0 if it succeeds, or one of the following
2129 * negative error codes:
2130 *
2131 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2132 *     Out of memory.
2133 */
2134NGHTTP2_EXTERN int
2135nghttp2_session_callbacks_new(nghttp2_session_callbacks **callbacks_ptr);
2136
2137/**
2138 * @function
2139 *
2140 * Frees any resources allocated for |callbacks|.  If |callbacks| is
2141 * ``NULL``, this function does nothing.
2142 */
2143NGHTTP2_EXTERN void
2144nghttp2_session_callbacks_del(nghttp2_session_callbacks *callbacks);
2145
2146/**
2147 * @function
2148 *
2149 * Sets callback function invoked when a session wants to send data to
2150 * the remote peer.  This callback is not necessary if the application
2151 * uses solely `nghttp2_session_mem_send()` to serialize data to
2152 * transmit.
2153 */
2154NGHTTP2_EXTERN void nghttp2_session_callbacks_set_send_callback(
2155    nghttp2_session_callbacks *cbs, nghttp2_send_callback send_callback);
2156
2157/**
2158 * @function
2159 *
2160 * Sets callback function invoked when the a session wants to receive
2161 * data from the remote peer.  This callback is not necessary if the
2162 * application uses solely `nghttp2_session_mem_recv()` to process
2163 * received data.
2164 */
2165NGHTTP2_EXTERN void nghttp2_session_callbacks_set_recv_callback(
2166    nghttp2_session_callbacks *cbs, nghttp2_recv_callback recv_callback);
2167
2168/**
2169 * @function
2170 *
2171 * Sets callback function invoked by `nghttp2_session_recv()` and
2172 * `nghttp2_session_mem_recv()` when a frame is received.
2173 */
2174NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_recv_callback(
2175    nghttp2_session_callbacks *cbs,
2176    nghttp2_on_frame_recv_callback on_frame_recv_callback);
2177
2178/**
2179 * @function
2180 *
2181 * Sets callback function invoked by `nghttp2_session_recv()` and
2182 * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is
2183 * received.
2184 */
2185NGHTTP2_EXTERN void
2186nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
2187    nghttp2_session_callbacks *cbs,
2188    nghttp2_on_invalid_frame_recv_callback on_invalid_frame_recv_callback);
2189
2190/**
2191 * @function
2192 *
2193 * Sets callback function invoked when a chunk of data in DATA frame
2194 * is received.
2195 */
2196NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
2197    nghttp2_session_callbacks *cbs,
2198    nghttp2_on_data_chunk_recv_callback on_data_chunk_recv_callback);
2199
2200/**
2201 * @function
2202 *
2203 * Sets callback function invoked before a non-DATA frame is sent.
2204 */
2205NGHTTP2_EXTERN void nghttp2_session_callbacks_set_before_frame_send_callback(
2206    nghttp2_session_callbacks *cbs,
2207    nghttp2_before_frame_send_callback before_frame_send_callback);
2208
2209/**
2210 * @function
2211 *
2212 * Sets callback function invoked after a frame is sent.
2213 */
2214NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_send_callback(
2215    nghttp2_session_callbacks *cbs,
2216    nghttp2_on_frame_send_callback on_frame_send_callback);
2217
2218/**
2219 * @function
2220 *
2221 * Sets callback function invoked when a non-DATA frame is not sent
2222 * because of an error.
2223 */
2224NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_not_send_callback(
2225    nghttp2_session_callbacks *cbs,
2226    nghttp2_on_frame_not_send_callback on_frame_not_send_callback);
2227
2228/**
2229 * @function
2230 *
2231 * Sets callback function invoked when the stream is closed.
2232 */
2233NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_stream_close_callback(
2234    nghttp2_session_callbacks *cbs,
2235    nghttp2_on_stream_close_callback on_stream_close_callback);
2236
2237/**
2238 * @function
2239 *
2240 * Sets callback function invoked when the reception of header block
2241 * in HEADERS or PUSH_PROMISE is started.
2242 */
2243NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_begin_headers_callback(
2244    nghttp2_session_callbacks *cbs,
2245    nghttp2_on_begin_headers_callback on_begin_headers_callback);
2246
2247/**
2248 * @function
2249 *
2250 * Sets callback function invoked when a header name/value pair is
2251 * received.  If both
2252 * `nghttp2_session_callbacks_set_on_header_callback()` and
2253 * `nghttp2_session_callbacks_set_on_header_callback2()` are used to
2254 * set callbacks, the latter has the precedence.
2255 */
2256NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_header_callback(
2257    nghttp2_session_callbacks *cbs,
2258    nghttp2_on_header_callback on_header_callback);
2259
2260/**
2261 * @function
2262 *
2263 * Sets callback function invoked when a header name/value pair is
2264 * received.
2265 */
2266NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_header_callback2(
2267    nghttp2_session_callbacks *cbs,
2268    nghttp2_on_header_callback2 on_header_callback2);
2269
2270/**
2271 * @function
2272 *
2273 * Sets callback function invoked when a invalid header name/value
2274 * pair is received.  If both
2275 * `nghttp2_session_callbacks_set_on_invalid_header_callback()` and
2276 * `nghttp2_session_callbacks_set_on_invalid_header_callback2()` are
2277 * used to set callbacks, the latter takes the precedence.
2278 */
2279NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_header_callback(
2280    nghttp2_session_callbacks *cbs,
2281    nghttp2_on_invalid_header_callback on_invalid_header_callback);
2282
2283/**
2284 * @function
2285 *
2286 * Sets callback function invoked when a invalid header name/value
2287 * pair is received.
2288 */
2289NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_header_callback2(
2290    nghttp2_session_callbacks *cbs,
2291    nghttp2_on_invalid_header_callback2 on_invalid_header_callback2);
2292
2293/**
2294 * @function
2295 *
2296 * Sets callback function invoked when the library asks application
2297 * how many padding bytes are required for the transmission of the
2298 * given frame.
2299 */
2300NGHTTP2_EXTERN void nghttp2_session_callbacks_set_select_padding_callback(
2301    nghttp2_session_callbacks *cbs,
2302    nghttp2_select_padding_callback select_padding_callback);
2303
2304/**
2305 * @function
2306 *
2307 * Sets callback function determine the length allowed in
2308 * :type:`nghttp2_data_source_read_callback`.
2309 */
2310NGHTTP2_EXTERN void
2311nghttp2_session_callbacks_set_data_source_read_length_callback(
2312    nghttp2_session_callbacks *cbs,
2313    nghttp2_data_source_read_length_callback data_source_read_length_callback);
2314
2315/**
2316 * @function
2317 *
2318 * Sets callback function invoked when a frame header is received.
2319 */
2320NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_begin_frame_callback(
2321    nghttp2_session_callbacks *cbs,
2322    nghttp2_on_begin_frame_callback on_begin_frame_callback);
2323
2324/**
2325 * @function
2326 *
2327 * Sets callback function invoked when
2328 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_NO_COPY` is used in
2329 * :type:`nghttp2_data_source_read_callback` to avoid data copy.
2330 */
2331NGHTTP2_EXTERN void nghttp2_session_callbacks_set_send_data_callback(
2332    nghttp2_session_callbacks *cbs,
2333    nghttp2_send_data_callback send_data_callback);
2334
2335/**
2336 * @function
2337 *
2338 * Sets callback function invoked when the library asks the
2339 * application to pack extension frame payload in wire format.
2340 */
2341NGHTTP2_EXTERN void nghttp2_session_callbacks_set_pack_extension_callback(
2342    nghttp2_session_callbacks *cbs,
2343    nghttp2_pack_extension_callback pack_extension_callback);
2344
2345/**
2346 * @function
2347 *
2348 * Sets callback function invoked when the library asks the
2349 * application to unpack extension frame payload from wire format.
2350 */
2351NGHTTP2_EXTERN void nghttp2_session_callbacks_set_unpack_extension_callback(
2352    nghttp2_session_callbacks *cbs,
2353    nghttp2_unpack_extension_callback unpack_extension_callback);
2354
2355/**
2356 * @function
2357 *
2358 * Sets callback function invoked when chunk of extension frame
2359 * payload is received.
2360 */
2361NGHTTP2_EXTERN void
2362nghttp2_session_callbacks_set_on_extension_chunk_recv_callback(
2363    nghttp2_session_callbacks *cbs,
2364    nghttp2_on_extension_chunk_recv_callback on_extension_chunk_recv_callback);
2365
2366/**
2367 * @function
2368 *
2369 * Sets callback function invoked when library tells error message to
2370 * the application.
2371 *
2372 * This function is deprecated.  The new application should use
2373 * `nghttp2_session_callbacks_set_error_callback2()`.
2374 *
2375 * If both :type:`nghttp2_error_callback` and
2376 * :type:`nghttp2_error_callback2` are set, the latter takes
2377 * precedence.
2378 */
2379NGHTTP2_EXTERN void nghttp2_session_callbacks_set_error_callback(
2380    nghttp2_session_callbacks *cbs, nghttp2_error_callback error_callback);
2381
2382/**
2383 * @function
2384 *
2385 * Sets callback function invoked when library tells error code, and
2386 * message to the application.
2387 *
2388 * If both :type:`nghttp2_error_callback` and
2389 * :type:`nghttp2_error_callback2` are set, the latter takes
2390 * precedence.
2391 */
2392NGHTTP2_EXTERN void nghttp2_session_callbacks_set_error_callback2(
2393    nghttp2_session_callbacks *cbs, nghttp2_error_callback2 error_callback2);
2394
2395/**
2396 * @functypedef
2397 *
2398 * Custom memory allocator to replace malloc().  The |mem_user_data|
2399 * is the mem_user_data member of :type:`nghttp2_mem` structure.
2400 */
2401typedef void *(*nghttp2_malloc)(size_t size, void *mem_user_data);
2402
2403/**
2404 * @functypedef
2405 *
2406 * Custom memory allocator to replace free().  The |mem_user_data| is
2407 * the mem_user_data member of :type:`nghttp2_mem` structure.
2408 */
2409typedef void (*nghttp2_free)(void *ptr, void *mem_user_data);
2410
2411/**
2412 * @functypedef
2413 *
2414 * Custom memory allocator to replace calloc().  The |mem_user_data|
2415 * is the mem_user_data member of :type:`nghttp2_mem` structure.
2416 */
2417typedef void *(*nghttp2_calloc)(size_t nmemb, size_t size, void *mem_user_data);
2418
2419/**
2420 * @functypedef
2421 *
2422 * Custom memory allocator to replace realloc().  The |mem_user_data|
2423 * is the mem_user_data member of :type:`nghttp2_mem` structure.
2424 */
2425typedef void *(*nghttp2_realloc)(void *ptr, size_t size, void *mem_user_data);
2426
2427/**
2428 * @struct
2429 *
2430 * Custom memory allocator functions and user defined pointer.  The
2431 * |mem_user_data| member is passed to each allocator function.  This
2432 * can be used, for example, to achieve per-session memory pool.
2433 *
2434 * In the following example code, ``my_malloc``, ``my_free``,
2435 * ``my_calloc`` and ``my_realloc`` are the replacement of the
2436 * standard allocators ``malloc``, ``free``, ``calloc`` and
2437 * ``realloc`` respectively::
2438 *
2439 *     void *my_malloc_cb(size_t size, void *mem_user_data) {
2440 *       return my_malloc(size);
2441 *     }
2442 *
2443 *     void my_free_cb(void *ptr, void *mem_user_data) { my_free(ptr); }
2444 *
2445 *     void *my_calloc_cb(size_t nmemb, size_t size, void *mem_user_data) {
2446 *       return my_calloc(nmemb, size);
2447 *     }
2448 *
2449 *     void *my_realloc_cb(void *ptr, size_t size, void *mem_user_data) {
2450 *       return my_realloc(ptr, size);
2451 *     }
2452 *
2453 *     void session_new() {
2454 *       nghttp2_session *session;
2455 *       nghttp2_session_callbacks *callbacks;
2456 *       nghttp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb,
2457 *                          my_realloc_cb};
2458 *
2459 *       ...
2460 *
2461 *       nghttp2_session_client_new3(&session, callbacks, NULL, NULL, &mem);
2462 *
2463 *       ...
2464 *     }
2465 */
2466typedef struct {
2467  /**
2468   * An arbitrary user supplied data.  This is passed to each
2469   * allocator function.
2470   */
2471  void *mem_user_data;
2472  /**
2473   * Custom allocator function to replace malloc().
2474   */
2475  nghttp2_malloc malloc;
2476  /**
2477   * Custom allocator function to replace free().
2478   */
2479  nghttp2_free free;
2480  /**
2481   * Custom allocator function to replace calloc().
2482   */
2483  nghttp2_calloc calloc;
2484  /**
2485   * Custom allocator function to replace realloc().
2486   */
2487  nghttp2_realloc realloc;
2488} nghttp2_mem;
2489
2490struct nghttp2_option;
2491
2492/**
2493 * @struct
2494 *
2495 * Configuration options for :type:`nghttp2_session`.  The details of
2496 * this structure are intentionally hidden from the public API.
2497 */
2498typedef struct nghttp2_option nghttp2_option;
2499
2500/**
2501 * @function
2502 *
2503 * Initializes |*option_ptr| with default values.
2504 *
2505 * When the application finished using this object, it can use
2506 * `nghttp2_option_del()` to free its memory.
2507 *
2508 * This function returns 0 if it succeeds, or one of the following
2509 * negative error codes:
2510 *
2511 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2512 *     Out of memory.
2513 */
2514NGHTTP2_EXTERN int nghttp2_option_new(nghttp2_option **option_ptr);
2515
2516/**
2517 * @function
2518 *
2519 * Frees any resources allocated for |option|.  If |option| is
2520 * ``NULL``, this function does nothing.
2521 */
2522NGHTTP2_EXTERN void nghttp2_option_del(nghttp2_option *option);
2523
2524/**
2525 * @function
2526 *
2527 * This option prevents the library from sending WINDOW_UPDATE for a
2528 * connection automatically.  If this option is set to nonzero, the
2529 * library won't send WINDOW_UPDATE for DATA until application calls
2530 * `nghttp2_session_consume()` to indicate the consumed amount of
2531 * data.  Don't use `nghttp2_submit_window_update()` for this purpose.
2532 * By default, this option is set to zero.
2533 */
2534NGHTTP2_EXTERN void
2535nghttp2_option_set_no_auto_window_update(nghttp2_option *option, int val);
2536
2537/**
2538 * @function
2539 *
2540 * This option sets the SETTINGS_MAX_CONCURRENT_STREAMS value of
2541 * remote endpoint as if it is received in SETTINGS frame.  Without
2542 * specifying this option, the maximum number of outgoing concurrent
2543 * streams is initially limited to 100 to avoid issues when the local
2544 * endpoint submits lots of requests before receiving initial SETTINGS
2545 * frame from the remote endpoint, since sending them at once to the
2546 * remote endpoint could lead to rejection of some of the requests.
2547 * This value will be overwritten when the local endpoint receives
2548 * initial SETTINGS frame from the remote endpoint, either to the
2549 * value advertised in SETTINGS_MAX_CONCURRENT_STREAMS or to the
2550 * default value (unlimited) if none was advertised.
2551 */
2552NGHTTP2_EXTERN void
2553nghttp2_option_set_peer_max_concurrent_streams(nghttp2_option *option,
2554                                               uint32_t val);
2555
2556/**
2557 * @function
2558 *
2559 * By default, nghttp2 library, if configured as server, requires
2560 * first 24 bytes of client magic byte string (MAGIC).  In most cases,
2561 * this will simplify the implementation of server.  But sometimes
2562 * server may want to detect the application protocol based on first
2563 * few bytes on clear text communication.
2564 *
2565 * If this option is used with nonzero |val|, nghttp2 library does not
2566 * handle MAGIC.  It still checks following SETTINGS frame.  This
2567 * means that applications should deal with MAGIC by themselves.
2568 *
2569 * If this option is not used or used with zero value, if MAGIC does
2570 * not match :macro:`NGHTTP2_CLIENT_MAGIC`, `nghttp2_session_recv()`
2571 * and `nghttp2_session_mem_recv()` will return error
2572 * :enum:`nghttp2_error.NGHTTP2_ERR_BAD_CLIENT_MAGIC`, which is fatal
2573 * error.
2574 */
2575NGHTTP2_EXTERN void
2576nghttp2_option_set_no_recv_client_magic(nghttp2_option *option, int val);
2577
2578/**
2579 * @function
2580 *
2581 * By default, nghttp2 library enforces subset of HTTP Messaging rules
2582 * described in `HTTP/2 specification, section 8
2583 * <https://tools.ietf.org/html/rfc7540#section-8>`_.  See
2584 * :ref:`http-messaging` section for details.  For those applications
2585 * who use nghttp2 library as non-HTTP use, give nonzero to |val| to
2586 * disable this enforcement.  Please note that disabling this feature
2587 * does not change the fundamental client and server model of HTTP.
2588 * That is, even if the validation is disabled, only client can send
2589 * requests.
2590 */
2591NGHTTP2_EXTERN void nghttp2_option_set_no_http_messaging(nghttp2_option *option,
2592                                                         int val);
2593
2594/**
2595 * @function
2596 *
2597 * RFC 7540 does not enforce any limit on the number of incoming
2598 * reserved streams (in RFC 7540 terms, streams in reserved (remote)
2599 * state).  This only affects client side, since only server can push
2600 * streams.  Malicious server can push arbitrary number of streams,
2601 * and make client's memory exhausted.  This option can set the
2602 * maximum number of such incoming streams to avoid possible memory
2603 * exhaustion.  If this option is set, and pushed streams are
2604 * automatically closed on reception, without calling user provided
2605 * callback, if they exceed the given limit.  The default value is
2606 * 200.  If session is configured as server side, this option has no
2607 * effect.  Server can control the number of streams to push.
2608 */
2609NGHTTP2_EXTERN void
2610nghttp2_option_set_max_reserved_remote_streams(nghttp2_option *option,
2611                                               uint32_t val);
2612
2613/**
2614 * @function
2615 *
2616 * Sets extension frame type the application is willing to handle with
2617 * user defined callbacks (see
2618 * :type:`nghttp2_on_extension_chunk_recv_callback` and
2619 * :type:`nghttp2_unpack_extension_callback`).  The |type| is
2620 * extension frame type, and must be strictly greater than 0x9.
2621 * Otherwise, this function does nothing.  The application can call
2622 * this function multiple times to set more than one frame type to
2623 * receive.  The application does not have to call this function if it
2624 * just sends extension frames.
2625 */
2626NGHTTP2_EXTERN void
2627nghttp2_option_set_user_recv_extension_type(nghttp2_option *option,
2628                                            uint8_t type);
2629
2630/**
2631 * @function
2632 *
2633 * Sets extension frame type the application is willing to receive
2634 * using builtin handler.  The |type| is the extension frame type to
2635 * receive, and must be strictly greater than 0x9.  Otherwise, this
2636 * function does nothing.  The application can call this function
2637 * multiple times to set more than one frame type to receive.  The
2638 * application does not have to call this function if it just sends
2639 * extension frames.
2640 *
2641 * If same frame type is passed to both
2642 * `nghttp2_option_set_builtin_recv_extension_type()` and
2643 * `nghttp2_option_set_user_recv_extension_type()`, the latter takes
2644 * precedence.
2645 */
2646NGHTTP2_EXTERN void
2647nghttp2_option_set_builtin_recv_extension_type(nghttp2_option *option,
2648                                               uint8_t type);
2649
2650/**
2651 * @function
2652 *
2653 * This option prevents the library from sending PING frame with ACK
2654 * flag set automatically when PING frame without ACK flag set is
2655 * received.  If this option is set to nonzero, the library won't send
2656 * PING frame with ACK flag set in the response for incoming PING
2657 * frame.  The application can send PING frame with ACK flag set using
2658 * `nghttp2_submit_ping()` with :enum:`nghttp2_flag.NGHTTP2_FLAG_ACK`
2659 * as flags parameter.
2660 */
2661NGHTTP2_EXTERN void nghttp2_option_set_no_auto_ping_ack(nghttp2_option *option,
2662                                                        int val);
2663
2664/**
2665 * @function
2666 *
2667 * This option sets the maximum length of header block (a set of
2668 * header fields per one HEADERS frame) to send.  The length of a
2669 * given set of header fields is calculated using
2670 * `nghttp2_hd_deflate_bound()`.  The default value is 64KiB.  If
2671 * application attempts to send header fields larger than this limit,
2672 * the transmission of the frame fails with error code
2673 * :enum:`nghttp2_error.NGHTTP2_ERR_FRAME_SIZE_ERROR`.
2674 */
2675NGHTTP2_EXTERN void
2676nghttp2_option_set_max_send_header_block_length(nghttp2_option *option,
2677                                                size_t val);
2678
2679/**
2680 * @function
2681 *
2682 * This option sets the maximum dynamic table size for deflating
2683 * header fields.  The default value is 4KiB.  In HTTP/2, receiver of
2684 * deflated header block can specify maximum dynamic table size.  The
2685 * actual maximum size is the minimum of the size receiver specified
2686 * and this option value.
2687 */
2688NGHTTP2_EXTERN void
2689nghttp2_option_set_max_deflate_dynamic_table_size(nghttp2_option *option,
2690                                                  size_t val);
2691
2692/**
2693 * @function
2694 *
2695 * This option prevents the library from retaining closed streams to
2696 * maintain the priority tree.  If this option is set to nonzero,
2697 * applications can discard closed stream completely to save memory.
2698 *
2699 * If
2700 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
2701 * of value of 1 is submitted via `nghttp2_submit_settings()`, any
2702 * closed streams are not retained regardless of this option.
2703 */
2704NGHTTP2_EXTERN void nghttp2_option_set_no_closed_streams(nghttp2_option *option,
2705                                                         int val);
2706
2707/**
2708 * @function
2709 *
2710 * This function sets the maximum number of outgoing SETTINGS ACK and
2711 * PING ACK frames retained in :type:`nghttp2_session` object.  If
2712 * more than those frames are retained, the peer is considered to be
2713 * misbehaving and session will be closed.  The default value is 1000.
2714 */
2715NGHTTP2_EXTERN void nghttp2_option_set_max_outbound_ack(nghttp2_option *option,
2716                                                        size_t val);
2717
2718/**
2719 * @function
2720 *
2721 * This function sets the maximum number of SETTINGS entries per
2722 * SETTINGS frame that will be accepted. If more than those entries
2723 * are received, the peer is considered to be misbehaving and session
2724 * will be closed. The default value is 32.
2725 */
2726NGHTTP2_EXTERN void nghttp2_option_set_max_settings(nghttp2_option *option,
2727                                                    size_t val);
2728
2729/**
2730 * @function
2731 *
2732 * This option, if set to nonzero, allows server to fallback to
2733 * :rfc:`7540` priorities if SETTINGS_NO_RFC7540_PRIORITIES was not
2734 * received from client, and server submitted
2735 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
2736 * = 1 via `nghttp2_submit_settings()`.  Most of the advanced
2737 * functionality for RFC 7540 priorities are still disabled.  This
2738 * fallback only enables the minimal feature set of RFC 7540
2739 * priorities to deal with priority signaling from client.
2740 *
2741 * Client session ignores this option.
2742 */
2743NGHTTP2_EXTERN void
2744nghttp2_option_set_server_fallback_rfc7540_priorities(nghttp2_option *option,
2745                                                      int val);
2746
2747/**
2748 * @function
2749 *
2750 * This option, if set to nonzero, turns off RFC 9113 leading and
2751 * trailing white spaces validation against HTTP field value.  Some
2752 * important fields, such as HTTP/2 pseudo header fields, are
2753 * validated more strictly and this option does not apply to them.
2754 */
2755NGHTTP2_EXTERN void
2756nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(
2757    nghttp2_option *option, int val);
2758
2759/**
2760 * @function
2761 *
2762 * This function sets the rate limit for the incoming stream reset
2763 * (RST_STREAM frame).  It is server use only.  It is a token-bucket
2764 * based rate limiter.  |burst| specifies the number of tokens that is
2765 * initially available.  The maximum number of tokens is capped to
2766 * this value.  |rate| specifies the number of tokens that are
2767 * regenerated per second.  An incoming RST_STREAM consumes one token.
2768 * If there is no token available, GOAWAY is sent to tear down the
2769 * connection.  |burst| and |rate| default to 1000 and 33
2770 * respectively.
2771 */
2772NGHTTP2_EXTERN void
2773nghttp2_option_set_stream_reset_rate_limit(nghttp2_option *option,
2774                                           uint64_t burst, uint64_t rate);
2775
2776/**
2777 * @function
2778 *
2779 * Initializes |*session_ptr| for client use.  The all members of
2780 * |callbacks| are copied to |*session_ptr|.  Therefore |*session_ptr|
2781 * does not store |callbacks|.  The |user_data| is an arbitrary user
2782 * supplied data, which will be passed to the callback functions.
2783 *
2784 * The :type:`nghttp2_send_callback` must be specified.  If the
2785 * application code uses `nghttp2_session_recv()`, the
2786 * :type:`nghttp2_recv_callback` must be specified.  The other members
2787 * of |callbacks| can be ``NULL``.
2788 *
2789 * If this function fails, |*session_ptr| is left untouched.
2790 *
2791 * This function returns 0 if it succeeds, or one of the following
2792 * negative error codes:
2793 *
2794 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2795 *     Out of memory.
2796 */
2797NGHTTP2_EXTERN int
2798nghttp2_session_client_new(nghttp2_session **session_ptr,
2799                           const nghttp2_session_callbacks *callbacks,
2800                           void *user_data);
2801
2802/**
2803 * @function
2804 *
2805 * Initializes |*session_ptr| for server use.  The all members of
2806 * |callbacks| are copied to |*session_ptr|. Therefore |*session_ptr|
2807 * does not store |callbacks|.  The |user_data| is an arbitrary user
2808 * supplied data, which will be passed to the callback functions.
2809 *
2810 * The :type:`nghttp2_send_callback` must be specified.  If the
2811 * application code uses `nghttp2_session_recv()`, the
2812 * :type:`nghttp2_recv_callback` must be specified.  The other members
2813 * of |callbacks| can be ``NULL``.
2814 *
2815 * If this function fails, |*session_ptr| is left untouched.
2816 *
2817 * This function returns 0 if it succeeds, or one of the following
2818 * negative error codes:
2819 *
2820 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2821 *     Out of memory.
2822 */
2823NGHTTP2_EXTERN int
2824nghttp2_session_server_new(nghttp2_session **session_ptr,
2825                           const nghttp2_session_callbacks *callbacks,
2826                           void *user_data);
2827
2828/**
2829 * @function
2830 *
2831 * Like `nghttp2_session_client_new()`, but with additional options
2832 * specified in the |option|.
2833 *
2834 * The |option| can be ``NULL`` and the call is equivalent to
2835 * `nghttp2_session_client_new()`.
2836 *
2837 * This function does not take ownership |option|.  The application is
2838 * responsible for freeing |option| if it finishes using the object.
2839 *
2840 * The library code does not refer to |option| after this function
2841 * returns.
2842 *
2843 * This function returns 0 if it succeeds, or one of the following
2844 * negative error codes:
2845 *
2846 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2847 *     Out of memory.
2848 */
2849NGHTTP2_EXTERN int
2850nghttp2_session_client_new2(nghttp2_session **session_ptr,
2851                            const nghttp2_session_callbacks *callbacks,
2852                            void *user_data, const nghttp2_option *option);
2853
2854/**
2855 * @function
2856 *
2857 * Like `nghttp2_session_server_new()`, but with additional options
2858 * specified in the |option|.
2859 *
2860 * The |option| can be ``NULL`` and the call is equivalent to
2861 * `nghttp2_session_server_new()`.
2862 *
2863 * This function does not take ownership |option|.  The application is
2864 * responsible for freeing |option| if it finishes using the object.
2865 *
2866 * The library code does not refer to |option| after this function
2867 * returns.
2868 *
2869 * This function returns 0 if it succeeds, or one of the following
2870 * negative error codes:
2871 *
2872 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2873 *     Out of memory.
2874 */
2875NGHTTP2_EXTERN int
2876nghttp2_session_server_new2(nghttp2_session **session_ptr,
2877                            const nghttp2_session_callbacks *callbacks,
2878                            void *user_data, const nghttp2_option *option);
2879
2880/**
2881 * @function
2882 *
2883 * Like `nghttp2_session_client_new2()`, but with additional custom
2884 * memory allocator specified in the |mem|.
2885 *
2886 * The |mem| can be ``NULL`` and the call is equivalent to
2887 * `nghttp2_session_client_new2()`.
2888 *
2889 * This function does not take ownership |mem|.  The application is
2890 * responsible for freeing |mem|.
2891 *
2892 * The library code does not refer to |mem| pointer after this
2893 * function returns, so the application can safely free it.
2894 *
2895 * This function returns 0 if it succeeds, or one of the following
2896 * negative error codes:
2897 *
2898 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2899 *     Out of memory.
2900 */
2901NGHTTP2_EXTERN int nghttp2_session_client_new3(
2902    nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks,
2903    void *user_data, const nghttp2_option *option, nghttp2_mem *mem);
2904
2905/**
2906 * @function
2907 *
2908 * Like `nghttp2_session_server_new2()`, but with additional custom
2909 * memory allocator specified in the |mem|.
2910 *
2911 * The |mem| can be ``NULL`` and the call is equivalent to
2912 * `nghttp2_session_server_new2()`.
2913 *
2914 * This function does not take ownership |mem|.  The application is
2915 * responsible for freeing |mem|.
2916 *
2917 * The library code does not refer to |mem| pointer after this
2918 * function returns, so the application can safely free it.
2919 *
2920 * This function returns 0 if it succeeds, or one of the following
2921 * negative error codes:
2922 *
2923 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2924 *     Out of memory.
2925 */
2926NGHTTP2_EXTERN int nghttp2_session_server_new3(
2927    nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks,
2928    void *user_data, const nghttp2_option *option, nghttp2_mem *mem);
2929
2930/**
2931 * @function
2932 *
2933 * Frees any resources allocated for |session|.  If |session| is
2934 * ``NULL``, this function does nothing.
2935 */
2936NGHTTP2_EXTERN void nghttp2_session_del(nghttp2_session *session);
2937
2938/**
2939 * @function
2940 *
2941 * Sends pending frames to the remote peer.
2942 *
2943 * This function retrieves the highest prioritized frame from the
2944 * outbound queue and sends it to the remote peer.  It does this as
2945 * many times as possible until the user callback
2946 * :type:`nghttp2_send_callback` returns
2947 * :enum:`nghttp2_error.NGHTTP2_ERR_WOULDBLOCK`, the outbound queue
2948 * becomes empty or flow control is triggered (remote window size
2949 * becomes depleted or maximum number of concurrent streams is
2950 * reached).  This function calls several callback functions which are
2951 * passed when initializing the |session|.  Here is the simple time
2952 * chart which tells when each callback is invoked:
2953 *
2954 * 1. Get the next frame to send from outbound queue.
2955 *
2956 * 2. Prepare transmission of the frame.
2957 *
2958 * 3. If the control frame cannot be sent because some preconditions
2959 *    are not met (e.g., request HEADERS cannot be sent after GOAWAY),
2960 *    :type:`nghttp2_on_frame_not_send_callback` is invoked.  Abort
2961 *    the following steps.
2962 *
2963 * 4. If the frame is HEADERS, PUSH_PROMISE or DATA,
2964 *    :type:`nghttp2_select_padding_callback` is invoked.
2965 *
2966 * 5. If the frame is request HEADERS, the stream is opened here.
2967 *
2968 * 6. :type:`nghttp2_before_frame_send_callback` is invoked.
2969 *
2970 * 7. If :enum:`nghttp2_error.NGHTTP2_ERR_CANCEL` is returned from
2971 *    :type:`nghttp2_before_frame_send_callback`, the current frame
2972 *    transmission is canceled, and
2973 *    :type:`nghttp2_on_frame_not_send_callback` is invoked.  Abort
2974 *    the following steps.
2975 *
2976 * 8. :type:`nghttp2_send_callback` is invoked one or more times to
2977 *    send the frame.
2978 *
2979 * 9. :type:`nghttp2_on_frame_send_callback` is invoked.
2980 *
2981 * 10. If the transmission of the frame triggers closure of the
2982 *     stream, the stream is closed and
2983 *     :type:`nghttp2_on_stream_close_callback` is invoked.
2984 *
2985 * This function returns 0 if it succeeds, or one of the following
2986 * negative error codes:
2987 *
2988 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
2989 *     Out of memory.
2990 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`
2991 *     The callback function failed.
2992 */
2993NGHTTP2_EXTERN int nghttp2_session_send(nghttp2_session *session);
2994
2995/**
2996 * @function
2997 *
2998 * Returns the serialized data to send.
2999 *
3000 * This function behaves like `nghttp2_session_send()` except that it
3001 * does not use :type:`nghttp2_send_callback` to transmit data.
3002 * Instead, it assigns the pointer to the serialized data to the
3003 * |*data_ptr| and returns its length.  The other callbacks are called
3004 * in the same way as they are in `nghttp2_session_send()`.
3005 *
3006 * If no data is available to send, this function returns 0.
3007 *
3008 * This function may not return all serialized data in one invocation.
3009 * To get all data, call this function repeatedly until it returns 0
3010 * or one of negative error codes.
3011 *
3012 * The assigned |*data_ptr| is valid until the next call of
3013 * `nghttp2_session_mem_send()` or `nghttp2_session_send()`.
3014 *
3015 * The caller must send all data before sending the next chunk of
3016 * data.
3017 *
3018 * This function returns the length of the data pointed by the
3019 * |*data_ptr| if it succeeds, or one of the following negative error
3020 * codes:
3021 *
3022 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3023 *     Out of memory.
3024 *
3025 * .. note::
3026 *
3027 *   This function may produce very small byte string.  If that is the
3028 *   case, and application disables Nagle algorithm (``TCP_NODELAY``),
3029 *   then writing this small chunk leads to very small packet, and it
3030 *   is very inefficient.  An application should be responsible to
3031 *   buffer up small chunks of data as necessary to avoid this
3032 *   situation.
3033 */
3034NGHTTP2_EXTERN ssize_t nghttp2_session_mem_send(nghttp2_session *session,
3035                                                const uint8_t **data_ptr);
3036
3037/**
3038 * @function
3039 *
3040 * Receives frames from the remote peer.
3041 *
3042 * This function receives as many frames as possible until the user
3043 * callback :type:`nghttp2_recv_callback` returns
3044 * :enum:`nghttp2_error.NGHTTP2_ERR_WOULDBLOCK`.  This function calls
3045 * several callback functions which are passed when initializing the
3046 * |session|.  Here is the simple time chart which tells when each
3047 * callback is invoked:
3048 *
3049 * 1. :type:`nghttp2_recv_callback` is invoked one or more times to
3050 *    receive frame header.
3051 *
3052 * 2. When frame header is received,
3053 *    :type:`nghttp2_on_begin_frame_callback` is invoked.
3054 *
3055 * 3. If the frame is DATA frame:
3056 *
3057 *    1. :type:`nghttp2_recv_callback` is invoked to receive DATA
3058 *       payload. For each chunk of data,
3059 *       :type:`nghttp2_on_data_chunk_recv_callback` is invoked.
3060 *
3061 *    2. If one DATA frame is completely received,
3062 *       :type:`nghttp2_on_frame_recv_callback` is invoked.  If the
3063 *       reception of the frame triggers the closure of the stream,
3064 *       :type:`nghttp2_on_stream_close_callback` is invoked.
3065 *
3066 * 4. If the frame is the control frame:
3067 *
3068 *    1. :type:`nghttp2_recv_callback` is invoked one or more times to
3069 *       receive whole frame.
3070 *
3071 *    2. If the received frame is valid, then following actions are
3072 *       taken.  If the frame is either HEADERS or PUSH_PROMISE,
3073 *       :type:`nghttp2_on_begin_headers_callback` is invoked.  Then
3074 *       :type:`nghttp2_on_header_callback` is invoked for each header
3075 *       name/value pair.  For invalid header field,
3076 *       :type:`nghttp2_on_invalid_header_callback` is called.  After
3077 *       all name/value pairs are emitted successfully,
3078 *       :type:`nghttp2_on_frame_recv_callback` is invoked.  For other
3079 *       frames, :type:`nghttp2_on_frame_recv_callback` is invoked.
3080 *       If the reception of the frame triggers the closure of the
3081 *       stream, :type:`nghttp2_on_stream_close_callback` is invoked.
3082 *
3083 *    3. If the received frame is unpacked but is interpreted as
3084 *       invalid, :type:`nghttp2_on_invalid_frame_recv_callback` is
3085 *       invoked.
3086 *
3087 * This function returns 0 if it succeeds, or one of the following
3088 * negative error codes:
3089 *
3090 * :enum:`nghttp2_error.NGHTTP2_ERR_EOF`
3091 *     The remote peer did shutdown on the connection.
3092 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3093 *     Out of memory.
3094 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`
3095 *     The callback function failed.
3096 * :enum:`nghttp2_error.NGHTTP2_ERR_BAD_CLIENT_MAGIC`
3097 *     Invalid client magic was detected.  This error only returns
3098 *     when |session| was configured as server and
3099 *     `nghttp2_option_set_no_recv_client_magic()` is not used with
3100 *     nonzero value.
3101 * :enum:`nghttp2_error.NGHTTP2_ERR_FLOODED`
3102 *     Flooding was detected in this HTTP/2 session, and it must be
3103 *     closed.  This is most likely caused by misbehaviour of peer.
3104 */
3105NGHTTP2_EXTERN int nghttp2_session_recv(nghttp2_session *session);
3106
3107/**
3108 * @function
3109 *
3110 * Processes data |in| as an input from the remote endpoint.  The
3111 * |inlen| indicates the number of bytes to receive in the |in|.
3112 *
3113 * This function behaves like `nghttp2_session_recv()` except that it
3114 * does not use :type:`nghttp2_recv_callback` to receive data; the
3115 * |in| is the only data for the invocation of this function.  If all
3116 * bytes are processed, this function returns.  The other callbacks
3117 * are called in the same way as they are in `nghttp2_session_recv()`.
3118 *
3119 * In the current implementation, this function always tries to
3120 * processes |inlen| bytes of input data unless either an error occurs or
3121 * :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE` is returned from
3122 * :type:`nghttp2_on_header_callback` or
3123 * :type:`nghttp2_on_data_chunk_recv_callback`.  If
3124 * :enum:`nghttp2_error.NGHTTP2_ERR_PAUSE` is used, the return value
3125 * includes the number of bytes which was used to produce the data or
3126 * frame for the callback.
3127 *
3128 * This function returns the number of processed bytes, or one of the
3129 * following negative error codes:
3130 *
3131 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3132 *     Out of memory.
3133 * :enum:`nghttp2_error.NGHTTP2_ERR_CALLBACK_FAILURE`
3134 *     The callback function failed.
3135 * :enum:`nghttp2_error.NGHTTP2_ERR_BAD_CLIENT_MAGIC`
3136 *     Invalid client magic was detected.  This error only returns
3137 *     when |session| was configured as server and
3138 *     `nghttp2_option_set_no_recv_client_magic()` is not used with
3139 *     nonzero value.
3140 * :enum:`nghttp2_error.NGHTTP2_ERR_FLOODED`
3141 *     Flooding was detected in this HTTP/2 session, and it must be
3142 *     closed.  This is most likely caused by misbehaviour of peer.
3143 */
3144NGHTTP2_EXTERN ssize_t nghttp2_session_mem_recv(nghttp2_session *session,
3145                                                const uint8_t *in,
3146                                                size_t inlen);
3147
3148/**
3149 * @function
3150 *
3151 * Puts back previously deferred DATA frame in the stream |stream_id|
3152 * to the outbound queue.
3153 *
3154 * This function returns 0 if it succeeds, or one of the following
3155 * negative error codes:
3156 *
3157 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3158 *     The stream does not exist; or no deferred data exist.
3159 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3160 *     Out of memory.
3161 */
3162NGHTTP2_EXTERN int nghttp2_session_resume_data(nghttp2_session *session,
3163                                               int32_t stream_id);
3164
3165/**
3166 * @function
3167 *
3168 * Returns nonzero value if |session| wants to receive data from the
3169 * remote peer.
3170 *
3171 * If both `nghttp2_session_want_read()` and
3172 * `nghttp2_session_want_write()` return 0, the application should
3173 * drop the connection.
3174 */
3175NGHTTP2_EXTERN int nghttp2_session_want_read(nghttp2_session *session);
3176
3177/**
3178 * @function
3179 *
3180 * Returns nonzero value if |session| wants to send data to the remote
3181 * peer.
3182 *
3183 * If both `nghttp2_session_want_read()` and
3184 * `nghttp2_session_want_write()` return 0, the application should
3185 * drop the connection.
3186 */
3187NGHTTP2_EXTERN int nghttp2_session_want_write(nghttp2_session *session);
3188
3189/**
3190 * @function
3191 *
3192 * Returns stream_user_data for the stream |stream_id|.  The
3193 * stream_user_data is provided by `nghttp2_submit_request()`,
3194 * `nghttp2_submit_headers()` or
3195 * `nghttp2_session_set_stream_user_data()`.  Unless it is set using
3196 * `nghttp2_session_set_stream_user_data()`, if the stream is
3197 * initiated by the remote endpoint, stream_user_data is always
3198 * ``NULL``.  If the stream does not exist, this function returns
3199 * ``NULL``.
3200 */
3201NGHTTP2_EXTERN void *
3202nghttp2_session_get_stream_user_data(nghttp2_session *session,
3203                                     int32_t stream_id);
3204
3205/**
3206 * @function
3207 *
3208 * Sets the |stream_user_data| to the stream denoted by the
3209 * |stream_id|.  If a stream user data is already set to the stream,
3210 * it is replaced with the |stream_user_data|.  It is valid to specify
3211 * ``NULL`` in the |stream_user_data|, which nullifies the associated
3212 * data pointer.
3213 *
3214 * It is valid to set the |stream_user_data| to the stream reserved by
3215 * PUSH_PROMISE frame.
3216 *
3217 * This function returns 0 if it succeeds, or one of following
3218 * negative error codes:
3219 *
3220 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3221 *     The stream does not exist
3222 */
3223NGHTTP2_EXTERN int
3224nghttp2_session_set_stream_user_data(nghttp2_session *session,
3225                                     int32_t stream_id, void *stream_user_data);
3226
3227/**
3228 * @function
3229 *
3230 * Sets |user_data| to |session|, overwriting the existing user data
3231 * specified in `nghttp2_session_client_new()`, or
3232 * `nghttp2_session_server_new()`.
3233 */
3234NGHTTP2_EXTERN void nghttp2_session_set_user_data(nghttp2_session *session,
3235                                                  void *user_data);
3236
3237/**
3238 * @function
3239 *
3240 * Returns the number of frames in the outbound queue.  This does not
3241 * include the deferred DATA frames.
3242 */
3243NGHTTP2_EXTERN size_t
3244nghttp2_session_get_outbound_queue_size(nghttp2_session *session);
3245
3246/**
3247 * @function
3248 *
3249 * Returns the number of DATA payload in bytes received without
3250 * WINDOW_UPDATE transmission for the stream |stream_id|.  The local
3251 * (receive) window size can be adjusted by
3252 * `nghttp2_submit_window_update()`.  This function takes into account
3253 * that and returns effective data length.  In particular, if the
3254 * local window size is reduced by submitting negative
3255 * window_size_increment with `nghttp2_submit_window_update()`, this
3256 * function returns the number of bytes less than actually received.
3257 *
3258 * This function returns -1 if it fails.
3259 */
3260NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_effective_recv_data_length(
3261    nghttp2_session *session, int32_t stream_id);
3262
3263/**
3264 * @function
3265 *
3266 * Returns the local (receive) window size for the stream |stream_id|.
3267 * The local window size can be adjusted by
3268 * `nghttp2_submit_window_update()`.  This function takes into account
3269 * that and returns effective window size.
3270 *
3271 * This function does not take into account the amount of received
3272 * data from the remote endpoint.  Use
3273 * `nghttp2_session_get_stream_local_window_size()` to know the amount
3274 * of data the remote endpoint can send without receiving stream level
3275 * WINDOW_UPDATE frame.  Note that each stream is still subject to the
3276 * connection level flow control.
3277 *
3278 * This function returns -1 if it fails.
3279 */
3280NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_effective_local_window_size(
3281    nghttp2_session *session, int32_t stream_id);
3282
3283/**
3284 * @function
3285 *
3286 * Returns the amount of flow-controlled payload (e.g., DATA) that the
3287 * remote endpoint can send without receiving stream level
3288 * WINDOW_UPDATE frame.  It is also subject to the connection level
3289 * flow control.  So the actual amount of data to send is
3290 * min(`nghttp2_session_get_stream_local_window_size()`,
3291 * `nghttp2_session_get_local_window_size()`).
3292 *
3293 * This function returns -1 if it fails.
3294 */
3295NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_local_window_size(
3296    nghttp2_session *session, int32_t stream_id);
3297
3298/**
3299 * @function
3300 *
3301 * Returns the number of DATA payload in bytes received without
3302 * WINDOW_UPDATE transmission for a connection.  The local (receive)
3303 * window size can be adjusted by `nghttp2_submit_window_update()`.
3304 * This function takes into account that and returns effective data
3305 * length.  In particular, if the local window size is reduced by
3306 * submitting negative window_size_increment with
3307 * `nghttp2_submit_window_update()`, this function returns the number
3308 * of bytes less than actually received.
3309 *
3310 * This function returns -1 if it fails.
3311 */
3312NGHTTP2_EXTERN int32_t
3313nghttp2_session_get_effective_recv_data_length(nghttp2_session *session);
3314
3315/**
3316 * @function
3317 *
3318 * Returns the local (receive) window size for a connection.  The
3319 * local window size can be adjusted by
3320 * `nghttp2_submit_window_update()`.  This function takes into account
3321 * that and returns effective window size.
3322 *
3323 * This function does not take into account the amount of received
3324 * data from the remote endpoint.  Use
3325 * `nghttp2_session_get_local_window_size()` to know the amount of
3326 * data the remote endpoint can send without receiving
3327 * connection-level WINDOW_UPDATE frame.  Note that each stream is
3328 * still subject to the stream level flow control.
3329 *
3330 * This function returns -1 if it fails.
3331 */
3332NGHTTP2_EXTERN int32_t
3333nghttp2_session_get_effective_local_window_size(nghttp2_session *session);
3334
3335/**
3336 * @function
3337 *
3338 * Returns the amount of flow-controlled payload (e.g., DATA) that the
3339 * remote endpoint can send without receiving connection level
3340 * WINDOW_UPDATE frame.  Note that each stream is still subject to the
3341 * stream level flow control (see
3342 * `nghttp2_session_get_stream_local_window_size()`).
3343 *
3344 * This function returns -1 if it fails.
3345 */
3346NGHTTP2_EXTERN int32_t
3347nghttp2_session_get_local_window_size(nghttp2_session *session);
3348
3349/**
3350 * @function
3351 *
3352 * Returns the remote window size for a given stream |stream_id|.
3353 *
3354 * This is the amount of flow-controlled payload (e.g., DATA) that the
3355 * local endpoint can send without stream level WINDOW_UPDATE.  There
3356 * is also connection level flow control, so the effective size of
3357 * payload that the local endpoint can actually send is
3358 * min(`nghttp2_session_get_stream_remote_window_size()`,
3359 * `nghttp2_session_get_remote_window_size()`).
3360 *
3361 * This function returns -1 if it fails.
3362 */
3363NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_remote_window_size(
3364    nghttp2_session *session, int32_t stream_id);
3365
3366/**
3367 * @function
3368 *
3369 * Returns the remote window size for a connection.
3370 *
3371 * This function always succeeds.
3372 */
3373NGHTTP2_EXTERN int32_t
3374nghttp2_session_get_remote_window_size(nghttp2_session *session);
3375
3376/**
3377 * @function
3378 *
3379 * Returns 1 if local peer half closed the given stream |stream_id|.
3380 * Returns 0 if it did not.  Returns -1 if no such stream exists.
3381 */
3382NGHTTP2_EXTERN int
3383nghttp2_session_get_stream_local_close(nghttp2_session *session,
3384                                       int32_t stream_id);
3385
3386/**
3387 * @function
3388 *
3389 * Returns 1 if remote peer half closed the given stream |stream_id|.
3390 * Returns 0 if it did not.  Returns -1 if no such stream exists.
3391 */
3392NGHTTP2_EXTERN int
3393nghttp2_session_get_stream_remote_close(nghttp2_session *session,
3394                                        int32_t stream_id);
3395
3396/**
3397 * @function
3398 *
3399 * Returns the current dynamic table size of HPACK inflater, including
3400 * the overhead 32 bytes per entry described in RFC 7541.
3401 */
3402NGHTTP2_EXTERN size_t
3403nghttp2_session_get_hd_inflate_dynamic_table_size(nghttp2_session *session);
3404
3405/**
3406 * @function
3407 *
3408 * Returns the current dynamic table size of HPACK deflater including
3409 * the overhead 32 bytes per entry described in RFC 7541.
3410 */
3411NGHTTP2_EXTERN size_t
3412nghttp2_session_get_hd_deflate_dynamic_table_size(nghttp2_session *session);
3413
3414/**
3415 * @function
3416 *
3417 * Signals the session so that the connection should be terminated.
3418 *
3419 * The last stream ID is the minimum value between the stream ID of a
3420 * stream for which :type:`nghttp2_on_frame_recv_callback` was called
3421 * most recently and the last stream ID we have sent to the peer
3422 * previously.
3423 *
3424 * The |error_code| is the error code of this GOAWAY frame.  The
3425 * pre-defined error code is one of :enum:`nghttp2_error_code`.
3426 *
3427 * After the transmission, both `nghttp2_session_want_read()` and
3428 * `nghttp2_session_want_write()` return 0.
3429 *
3430 * This function should be called when the connection should be
3431 * terminated after sending GOAWAY.  If the remaining streams should
3432 * be processed after GOAWAY, use `nghttp2_submit_goaway()` instead.
3433 *
3434 * This function returns 0 if it succeeds, or one of the following
3435 * negative error codes:
3436 *
3437 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3438 *     Out of memory.
3439 */
3440NGHTTP2_EXTERN int nghttp2_session_terminate_session(nghttp2_session *session,
3441                                                     uint32_t error_code);
3442
3443/**
3444 * @function
3445 *
3446 * Signals the session so that the connection should be terminated.
3447 *
3448 * This function behaves like `nghttp2_session_terminate_session()`,
3449 * but the last stream ID can be specified by the application for fine
3450 * grained control of stream.  The HTTP/2 specification does not allow
3451 * last_stream_id to be increased.  So the actual value sent as
3452 * last_stream_id is the minimum value between the given
3453 * |last_stream_id| and the last_stream_id we have previously sent to
3454 * the peer.
3455 *
3456 * The |last_stream_id| is peer's stream ID or 0.  So if |session| is
3457 * initialized as client, |last_stream_id| must be even or 0.  If
3458 * |session| is initialized as server, |last_stream_id| must be odd or
3459 * 0.
3460 *
3461 * This function returns 0 if it succeeds, or one of the following
3462 * negative error codes:
3463 *
3464 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3465 *     Out of memory.
3466 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3467 *     The |last_stream_id| is invalid.
3468 */
3469NGHTTP2_EXTERN int nghttp2_session_terminate_session2(nghttp2_session *session,
3470                                                      int32_t last_stream_id,
3471                                                      uint32_t error_code);
3472
3473/**
3474 * @function
3475 *
3476 * Signals to the client that the server started graceful shutdown
3477 * procedure.
3478 *
3479 * This function is only usable for server.  If this function is
3480 * called with client side session, this function returns
3481 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`.
3482 *
3483 * To gracefully shutdown HTTP/2 session, server should call this
3484 * function to send GOAWAY with last_stream_id (1u << 31) - 1.  And
3485 * after some delay (e.g., 1 RTT), send another GOAWAY with the stream
3486 * ID that the server has some processing using
3487 * `nghttp2_submit_goaway()`.  See also
3488 * `nghttp2_session_get_last_proc_stream_id()`.
3489 *
3490 * Unlike `nghttp2_submit_goaway()`, this function just sends GOAWAY
3491 * and does nothing more.  This is a mere indication to the client
3492 * that session shutdown is imminent.  The application should call
3493 * `nghttp2_submit_goaway()` with appropriate last_stream_id after
3494 * this call.
3495 *
3496 * If one or more GOAWAY frame have been already sent by either
3497 * `nghttp2_submit_goaway()` or `nghttp2_session_terminate_session()`,
3498 * this function has no effect.
3499 *
3500 * This function returns 0 if it succeeds, or one of the following
3501 * negative error codes:
3502 *
3503 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3504 *     Out of memory.
3505 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
3506 *     The |session| is initialized as client.
3507 */
3508NGHTTP2_EXTERN int nghttp2_submit_shutdown_notice(nghttp2_session *session);
3509
3510/**
3511 * @function
3512 *
3513 * Returns the value of SETTINGS |id| notified by a remote endpoint.
3514 * The |id| must be one of values defined in
3515 * :enum:`nghttp2_settings_id`.
3516 */
3517NGHTTP2_EXTERN uint32_t nghttp2_session_get_remote_settings(
3518    nghttp2_session *session, nghttp2_settings_id id);
3519
3520/**
3521 * @function
3522 *
3523 * Returns the value of SETTINGS |id| of local endpoint acknowledged
3524 * by the remote endpoint.  The |id| must be one of the values defined
3525 * in :enum:`nghttp2_settings_id`.
3526 */
3527NGHTTP2_EXTERN uint32_t nghttp2_session_get_local_settings(
3528    nghttp2_session *session, nghttp2_settings_id id);
3529
3530/**
3531 * @function
3532 *
3533 * Tells the |session| that next stream ID is |next_stream_id|.  The
3534 * |next_stream_id| must be equal or greater than the value returned
3535 * by `nghttp2_session_get_next_stream_id()`.
3536 *
3537 * This function returns 0 if it succeeds, or one of the following
3538 * negative error codes:
3539 *
3540 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3541 *     The |next_stream_id| is strictly less than the value
3542 *     `nghttp2_session_get_next_stream_id()` returns; or
3543 *     |next_stream_id| is invalid (e.g., even integer for client, or
3544 *     odd integer for server).
3545 */
3546NGHTTP2_EXTERN int nghttp2_session_set_next_stream_id(nghttp2_session *session,
3547                                                      int32_t next_stream_id);
3548
3549/**
3550 * @function
3551 *
3552 * Returns the next outgoing stream ID.  Notice that return type is
3553 * uint32_t.  If we run out of stream ID for this session, this
3554 * function returns 1 << 31.
3555 */
3556NGHTTP2_EXTERN uint32_t
3557nghttp2_session_get_next_stream_id(nghttp2_session *session);
3558
3559/**
3560 * @function
3561 *
3562 * Tells the |session| that |size| bytes for a stream denoted by
3563 * |stream_id| were consumed by application and are ready to
3564 * WINDOW_UPDATE.  The consumed bytes are counted towards both
3565 * connection and stream level WINDOW_UPDATE (see
3566 * `nghttp2_session_consume_connection()` and
3567 * `nghttp2_session_consume_stream()` to update consumption
3568 * independently).  This function is intended to be used without
3569 * automatic window update (see
3570 * `nghttp2_option_set_no_auto_window_update()`).
3571 *
3572 * This function returns 0 if it succeeds, or one of the following
3573 * negative error codes:
3574 *
3575 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3576 *     Out of memory.
3577 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3578 *     The |stream_id| is 0.
3579 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
3580 *     Automatic WINDOW_UPDATE is not disabled.
3581 */
3582NGHTTP2_EXTERN int nghttp2_session_consume(nghttp2_session *session,
3583                                           int32_t stream_id, size_t size);
3584
3585/**
3586 * @function
3587 *
3588 * Like `nghttp2_session_consume()`, but this only tells library that
3589 * |size| bytes were consumed only for connection level.  Note that
3590 * HTTP/2 maintains connection and stream level flow control windows
3591 * independently.
3592 *
3593 * This function returns 0 if it succeeds, or one of the following
3594 * negative error codes:
3595 *
3596 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3597 *     Out of memory.
3598 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
3599 *     Automatic WINDOW_UPDATE is not disabled.
3600 */
3601NGHTTP2_EXTERN int nghttp2_session_consume_connection(nghttp2_session *session,
3602                                                      size_t size);
3603
3604/**
3605 * @function
3606 *
3607 * Like `nghttp2_session_consume()`, but this only tells library that
3608 * |size| bytes were consumed only for stream denoted by |stream_id|.
3609 * Note that HTTP/2 maintains connection and stream level flow control
3610 * windows independently.
3611 *
3612 * This function returns 0 if it succeeds, or one of the following
3613 * negative error codes:
3614 *
3615 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3616 *     Out of memory.
3617 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3618 *     The |stream_id| is 0.
3619 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
3620 *     Automatic WINDOW_UPDATE is not disabled.
3621 */
3622NGHTTP2_EXTERN int nghttp2_session_consume_stream(nghttp2_session *session,
3623                                                  int32_t stream_id,
3624                                                  size_t size);
3625
3626/**
3627 * @function
3628 *
3629 * Changes priority of existing stream denoted by |stream_id|.  The
3630 * new priority specification is |pri_spec|.
3631 *
3632 * The priority is changed silently and instantly, and no PRIORITY
3633 * frame will be sent to notify the peer of this change.  This
3634 * function may be useful for server to change the priority of pushed
3635 * stream.
3636 *
3637 * If |session| is initialized as server, and ``pri_spec->stream_id``
3638 * points to the idle stream, the idle stream is created if it does
3639 * not exist.  The created idle stream will depend on root stream
3640 * (stream 0) with weight 16.
3641 *
3642 * Otherwise, if stream denoted by ``pri_spec->stream_id`` is not
3643 * found, we use default priority instead of given |pri_spec|.  That
3644 * is make stream depend on root stream with weight 16.
3645 *
3646 * If
3647 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
3648 * of value of 1 is submitted via `nghttp2_submit_settings()`, this
3649 * function does nothing and returns 0.
3650 *
3651 * This function returns 0 if it succeeds, or one of the following
3652 * negative error codes:
3653 *
3654 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3655 *     Out of memory.
3656 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3657 *     Attempted to depend on itself; or no stream exist for the given
3658 *     |stream_id|; or |stream_id| is 0
3659 */
3660NGHTTP2_EXTERN int
3661nghttp2_session_change_stream_priority(nghttp2_session *session,
3662                                       int32_t stream_id,
3663                                       const nghttp2_priority_spec *pri_spec);
3664
3665/**
3666 * @function
3667 *
3668 * Creates idle stream with the given |stream_id|, and priority
3669 * |pri_spec|.
3670 *
3671 * The stream creation is done without sending PRIORITY frame, which
3672 * means that peer does not know about the existence of this idle
3673 * stream in the local endpoint.
3674 *
3675 * RFC 7540 does not disallow the use of creation of idle stream with
3676 * odd or even stream ID regardless of client or server.  So this
3677 * function can create odd or even stream ID regardless of client or
3678 * server.  But probably it is a bit safer to use the stream ID the
3679 * local endpoint can initiate (in other words, use odd stream ID for
3680 * client, and even stream ID for server), to avoid potential
3681 * collision from peer's instruction.  Also we can use
3682 * `nghttp2_session_set_next_stream_id()` to avoid to open created
3683 * idle streams accidentally if we follow this recommendation.
3684 *
3685 * If |session| is initialized as server, and ``pri_spec->stream_id``
3686 * points to the idle stream, the idle stream is created if it does
3687 * not exist.  The created idle stream will depend on root stream
3688 * (stream 0) with weight 16.
3689 *
3690 * Otherwise, if stream denoted by ``pri_spec->stream_id`` is not
3691 * found, we use default priority instead of given |pri_spec|.  That
3692 * is make stream depend on root stream with weight 16.
3693 *
3694 * If
3695 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
3696 * of value of 1 is submitted via `nghttp2_submit_settings()`, this
3697 * function does nothing and returns 0.
3698 *
3699 * This function returns 0 if it succeeds, or one of the following
3700 * negative error codes:
3701 *
3702 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3703 *     Out of memory.
3704 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3705 *     Attempted to depend on itself; or stream denoted by |stream_id|
3706 *     already exists; or |stream_id| cannot be used to create idle
3707 *     stream (in other words, local endpoint has already opened
3708 *     stream ID greater than or equal to the given stream ID; or
3709 *     |stream_id| is 0
3710 */
3711NGHTTP2_EXTERN int
3712nghttp2_session_create_idle_stream(nghttp2_session *session, int32_t stream_id,
3713                                   const nghttp2_priority_spec *pri_spec);
3714
3715/**
3716 * @function
3717 *
3718 * Performs post-process of HTTP Upgrade request.  This function can
3719 * be called from both client and server, but the behavior is very
3720 * different in each other.
3721 *
3722 * .. warning::
3723 *
3724 *   This function is deprecated in favor of
3725 *   `nghttp2_session_upgrade2()`, because this function lacks the
3726 *   parameter to tell the library the request method used in the
3727 *   original HTTP request.  This information is required for client
3728 *   to validate actual response body length against content-length
3729 *   header field (see `nghttp2_option_set_no_http_messaging()`).  If
3730 *   HEAD is used in request, the length of response body must be 0
3731 *   regardless of value included in content-length header field.
3732 *
3733 * If called from client side, the |settings_payload| must be the
3734 * value sent in ``HTTP2-Settings`` header field and must be decoded
3735 * by base64url decoder.  The |settings_payloadlen| is the length of
3736 * |settings_payload|.  The |settings_payload| is unpacked and its
3737 * setting values will be submitted using `nghttp2_submit_settings()`.
3738 * This means that the client application code does not need to submit
3739 * SETTINGS by itself.  The stream with stream ID=1 is opened and the
3740 * |stream_user_data| is used for its stream_user_data.  The opened
3741 * stream becomes half-closed (local) state.
3742 *
3743 * If called from server side, the |settings_payload| must be the
3744 * value received in ``HTTP2-Settings`` header field and must be
3745 * decoded by base64url decoder.  The |settings_payloadlen| is the
3746 * length of |settings_payload|.  It is treated as if the SETTINGS
3747 * frame with that payload is received.  Thus, callback functions for
3748 * the reception of SETTINGS frame will be invoked.  The stream with
3749 * stream ID=1 is opened.  The |stream_user_data| is ignored.  The
3750 * opened stream becomes half-closed (remote).
3751 *
3752 * This function returns 0 if it succeeds, or one of the following
3753 * negative error codes:
3754 *
3755 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3756 *     Out of memory.
3757 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3758 *     The |settings_payload| is badly formed.
3759 * :enum:`nghttp2_error.NGHTTP2_ERR_PROTO`
3760 *     The stream ID 1 is already used or closed; or is not available.
3761 */
3762NGHTTP2_EXTERN int nghttp2_session_upgrade(nghttp2_session *session,
3763                                           const uint8_t *settings_payload,
3764                                           size_t settings_payloadlen,
3765                                           void *stream_user_data);
3766
3767/**
3768 * @function
3769 *
3770 * Performs post-process of HTTP Upgrade request.  This function can
3771 * be called from both client and server, but the behavior is very
3772 * different in each other.
3773 *
3774 * If called from client side, the |settings_payload| must be the
3775 * value sent in ``HTTP2-Settings`` header field and must be decoded
3776 * by base64url decoder.  The |settings_payloadlen| is the length of
3777 * |settings_payload|.  The |settings_payload| is unpacked and its
3778 * setting values will be submitted using `nghttp2_submit_settings()`.
3779 * This means that the client application code does not need to submit
3780 * SETTINGS by itself.  The stream with stream ID=1 is opened and the
3781 * |stream_user_data| is used for its stream_user_data.  The opened
3782 * stream becomes half-closed (local) state.
3783 *
3784 * If called from server side, the |settings_payload| must be the
3785 * value received in ``HTTP2-Settings`` header field and must be
3786 * decoded by base64url decoder.  The |settings_payloadlen| is the
3787 * length of |settings_payload|.  It is treated as if the SETTINGS
3788 * frame with that payload is received.  Thus, callback functions for
3789 * the reception of SETTINGS frame will be invoked.  The stream with
3790 * stream ID=1 is opened.  The |stream_user_data| is ignored.  The
3791 * opened stream becomes half-closed (remote).
3792 *
3793 * If the request method is HEAD, pass nonzero value to
3794 * |head_request|.  Otherwise, pass 0.
3795 *
3796 * This function returns 0 if it succeeds, or one of the following
3797 * negative error codes:
3798 *
3799 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3800 *     Out of memory.
3801 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3802 *     The |settings_payload| is badly formed.
3803 * :enum:`nghttp2_error.NGHTTP2_ERR_PROTO`
3804 *     The stream ID 1 is already used or closed; or is not available.
3805 */
3806NGHTTP2_EXTERN int nghttp2_session_upgrade2(nghttp2_session *session,
3807                                            const uint8_t *settings_payload,
3808                                            size_t settings_payloadlen,
3809                                            int head_request,
3810                                            void *stream_user_data);
3811
3812/**
3813 * @function
3814 *
3815 * Serializes the SETTINGS values |iv| in the |buf|.  The size of the
3816 * |buf| is specified by |buflen|.  The number of entries in the |iv|
3817 * array is given by |niv|.  The required space in |buf| for the |niv|
3818 * entries is ``6*niv`` bytes and if the given buffer is too small, an
3819 * error is returned.  This function is used mainly for creating a
3820 * SETTINGS payload to be sent with the ``HTTP2-Settings`` header
3821 * field in an HTTP Upgrade request.  The data written in |buf| is NOT
3822 * base64url encoded and the application is responsible for encoding.
3823 *
3824 * This function returns the number of bytes written in |buf|, or one
3825 * of the following negative error codes:
3826 *
3827 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3828 *     The |iv| contains duplicate settings ID or invalid value.
3829 *
3830 * :enum:`nghttp2_error.NGHTTP2_ERR_INSUFF_BUFSIZE`
3831 *     The provided |buflen| size is too small to hold the output.
3832 */
3833NGHTTP2_EXTERN ssize_t nghttp2_pack_settings_payload(
3834    uint8_t *buf, size_t buflen, const nghttp2_settings_entry *iv, size_t niv);
3835
3836/**
3837 * @function
3838 *
3839 * Returns string describing the |lib_error_code|.  The
3840 * |lib_error_code| must be one of the :enum:`nghttp2_error`.
3841 */
3842NGHTTP2_EXTERN const char *nghttp2_strerror(int lib_error_code);
3843
3844/**
3845 * @function
3846 *
3847 * Returns string representation of HTTP/2 error code |error_code|
3848 * (e.g., ``PROTOCOL_ERROR`` is returned if ``error_code ==
3849 * NGHTTP2_PROTOCOL_ERROR``).  If string representation is unknown for
3850 * given |error_code|, this function returns string ``unknown``.
3851 */
3852NGHTTP2_EXTERN const char *nghttp2_http2_strerror(uint32_t error_code);
3853
3854/**
3855 * @function
3856 *
3857 * Initializes |pri_spec| with the |stream_id| of the stream to depend
3858 * on with |weight| and its exclusive flag.  If |exclusive| is
3859 * nonzero, exclusive flag is set.
3860 *
3861 * The |weight| must be in [:macro:`NGHTTP2_MIN_WEIGHT`,
3862 * :macro:`NGHTTP2_MAX_WEIGHT`], inclusive.
3863 */
3864NGHTTP2_EXTERN void nghttp2_priority_spec_init(nghttp2_priority_spec *pri_spec,
3865                                               int32_t stream_id,
3866                                               int32_t weight, int exclusive);
3867
3868/**
3869 * @function
3870 *
3871 * Initializes |pri_spec| with the default values.  The default values
3872 * are: stream_id = 0, weight = :macro:`NGHTTP2_DEFAULT_WEIGHT` and
3873 * exclusive = 0.
3874 */
3875NGHTTP2_EXTERN void
3876nghttp2_priority_spec_default_init(nghttp2_priority_spec *pri_spec);
3877
3878/**
3879 * @function
3880 *
3881 * Returns nonzero if the |pri_spec| is filled with default values.
3882 */
3883NGHTTP2_EXTERN int
3884nghttp2_priority_spec_check_default(const nghttp2_priority_spec *pri_spec);
3885
3886/**
3887 * @function
3888 *
3889 * Submits HEADERS frame and optionally one or more DATA frames.
3890 *
3891 * The |pri_spec| is priority specification of this request.  ``NULL``
3892 * means the default priority (see
3893 * `nghttp2_priority_spec_default_init()`).  To specify the priority,
3894 * use `nghttp2_priority_spec_init()`.  If |pri_spec| is not ``NULL``,
3895 * this function will copy its data members.
3896 *
3897 * The ``pri_spec->weight`` must be in [:macro:`NGHTTP2_MIN_WEIGHT`,
3898 * :macro:`NGHTTP2_MAX_WEIGHT`], inclusive.  If ``pri_spec->weight``
3899 * is strictly less than :macro:`NGHTTP2_MIN_WEIGHT`, it becomes
3900 * :macro:`NGHTTP2_MIN_WEIGHT`.  If it is strictly greater than
3901 * :macro:`NGHTTP2_MAX_WEIGHT`, it becomes
3902 * :macro:`NGHTTP2_MAX_WEIGHT`.
3903 *
3904 * If
3905 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
3906 * of value of 1 is received by a remote endpoint, |pri_spec| is
3907 * ignored, and treated as if ``NULL`` is specified.
3908 *
3909 * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
3910 * |nvlen| elements.  The application is responsible to include
3911 * required pseudo-header fields (header field whose name starts with
3912 * ":") in |nva| and must place pseudo-headers before regular header
3913 * fields.
3914 *
3915 * This function creates copies of all name/value pairs in |nva|.  It
3916 * also lower-cases all names in |nva|.  The order of elements in
3917 * |nva| is preserved.  For header fields with
3918 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME` and
3919 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set,
3920 * header field name and value are not copied respectively.  With
3921 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME`, application
3922 * is responsible to pass header field name in lowercase.  The
3923 * application should maintain the references to them until
3924 * :type:`nghttp2_on_frame_send_callback` or
3925 * :type:`nghttp2_on_frame_not_send_callback` is called.
3926 *
3927 * HTTP/2 specification has requirement about header fields in the
3928 * request HEADERS.  See the specification for more details.
3929 *
3930 * If |data_prd| is not ``NULL``, it provides data which will be sent
3931 * in subsequent DATA frames.  In this case, a method that allows
3932 * request message bodies
3933 * (https://tools.ietf.org/html/rfc7231#section-4) must be specified
3934 * with ``:method`` key in |nva| (e.g. ``POST``).  This function does
3935 * not take ownership of the |data_prd|.  The function copies the
3936 * members of the |data_prd|.  If |data_prd| is ``NULL``, HEADERS have
3937 * END_STREAM set.  The |stream_user_data| is data associated to the
3938 * stream opened by this request and can be an arbitrary pointer,
3939 * which can be retrieved later by
3940 * `nghttp2_session_get_stream_user_data()`.
3941 *
3942 * This function returns assigned stream ID if it succeeds, or one of
3943 * the following negative error codes:
3944 *
3945 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
3946 *     Out of memory.
3947 * :enum:`nghttp2_error.NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
3948 *     No stream ID is available because maximum stream ID was
3949 *     reached.
3950 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
3951 *     Trying to depend on itself (new stream ID equals
3952 *     ``pri_spec->stream_id``).
3953 * :enum:`nghttp2_error.NGHTTP2_ERR_PROTO`
3954 *     The |session| is server session.
3955 *
3956 * .. warning::
3957 *
3958 *   This function returns assigned stream ID if it succeeds.  But
3959 *   that stream is not created yet.  The application must not submit
3960 *   frame to that stream ID before
3961 *   :type:`nghttp2_before_frame_send_callback` is called for this
3962 *   frame.  This means `nghttp2_session_get_stream_user_data()` does
3963 *   not work before the callback.  But
3964 *   `nghttp2_session_set_stream_user_data()` handles this situation
3965 *   specially, and it can set data to a stream during this period.
3966 *
3967 */
3968NGHTTP2_EXTERN int32_t nghttp2_submit_request(
3969    nghttp2_session *session, const nghttp2_priority_spec *pri_spec,
3970    const nghttp2_nv *nva, size_t nvlen, const nghttp2_data_provider *data_prd,
3971    void *stream_user_data);
3972
3973/**
3974 * @function
3975 *
3976 * Submits response HEADERS frame and optionally one or more DATA
3977 * frames against the stream |stream_id|.
3978 *
3979 * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
3980 * |nvlen| elements.  The application is responsible to include
3981 * required pseudo-header fields (header field whose name starts with
3982 * ":") in |nva| and must place pseudo-headers before regular header
3983 * fields.
3984 *
3985 * This function creates copies of all name/value pairs in |nva|.  It
3986 * also lower-cases all names in |nva|.  The order of elements in
3987 * |nva| is preserved.  For header fields with
3988 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME` and
3989 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set,
3990 * header field name and value are not copied respectively.  With
3991 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME`, application
3992 * is responsible to pass header field name in lowercase.  The
3993 * application should maintain the references to them until
3994 * :type:`nghttp2_on_frame_send_callback` or
3995 * :type:`nghttp2_on_frame_not_send_callback` is called.
3996 *
3997 * HTTP/2 specification has requirement about header fields in the
3998 * response HEADERS.  See the specification for more details.
3999 *
4000 * If |data_prd| is not ``NULL``, it provides data which will be sent
4001 * in subsequent DATA frames.  This function does not take ownership
4002 * of the |data_prd|.  The function copies the members of the
4003 * |data_prd|.  If |data_prd| is ``NULL``, HEADERS will have
4004 * END_STREAM flag set.
4005 *
4006 * This method can be used as normal HTTP response and push response.
4007 * When pushing a resource using this function, the |session| must be
4008 * configured using `nghttp2_session_server_new()` or its variants and
4009 * the target stream denoted by the |stream_id| must be reserved using
4010 * `nghttp2_submit_push_promise()`.
4011 *
4012 * To send non-final response headers (e.g., HTTP status 101), don't
4013 * use this function because this function half-closes the outbound
4014 * stream.  Instead, use `nghttp2_submit_headers()` for this purpose.
4015 *
4016 * This function returns 0 if it succeeds, or one of the following
4017 * negative error codes:
4018 *
4019 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4020 *     Out of memory.
4021 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4022 *     The |stream_id| is 0.
4023 * :enum:`nghttp2_error.NGHTTP2_ERR_DATA_EXIST`
4024 *     DATA or HEADERS has been already submitted and not fully
4025 *     processed yet.  Normally, this does not happen, but when
4026 *     application wrongly calls `nghttp2_submit_response()` twice,
4027 *     this may happen.
4028 * :enum:`nghttp2_error.NGHTTP2_ERR_PROTO`
4029 *     The |session| is client session.
4030 *
4031 * .. warning::
4032 *
4033 *   Calling this function twice for the same stream ID may lead to
4034 *   program crash.  It is generally considered to a programming error
4035 *   to commit response twice.
4036 */
4037NGHTTP2_EXTERN int
4038nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
4039                        const nghttp2_nv *nva, size_t nvlen,
4040                        const nghttp2_data_provider *data_prd);
4041
4042/**
4043 * @function
4044 *
4045 * Submits trailer fields HEADERS against the stream |stream_id|.
4046 *
4047 * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
4048 * |nvlen| elements.  The application must not include pseudo-header
4049 * fields (headers whose names starts with ":") in |nva|.
4050 *
4051 * This function creates copies of all name/value pairs in |nva|.  It
4052 * also lower-cases all names in |nva|.  The order of elements in
4053 * |nva| is preserved.  For header fields with
4054 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME` and
4055 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set,
4056 * header field name and value are not copied respectively.  With
4057 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME`, application
4058 * is responsible to pass header field name in lowercase.  The
4059 * application should maintain the references to them until
4060 * :type:`nghttp2_on_frame_send_callback` or
4061 * :type:`nghttp2_on_frame_not_send_callback` is called.
4062 *
4063 * For server, trailer fields must follow response HEADERS or response
4064 * DATA without END_STREAM flat set.  The library does not enforce
4065 * this requirement, and applications should do this for themselves.
4066 * If `nghttp2_submit_trailer()` is called before any response HEADERS
4067 * submission (usually by `nghttp2_submit_response()`), the content of
4068 * |nva| will be sent as response headers, which will result in error.
4069 *
4070 * This function has the same effect with `nghttp2_submit_headers()`,
4071 * with flags = :enum:`nghttp2_flag.NGHTTP2_FLAG_END_STREAM` and both
4072 * pri_spec and stream_user_data to NULL.
4073 *
4074 * To submit trailer fields after `nghttp2_submit_response()` is
4075 * called, the application has to specify
4076 * :type:`nghttp2_data_provider` to `nghttp2_submit_response()`.
4077 * Inside of :type:`nghttp2_data_source_read_callback`, when setting
4078 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_EOF`, also set
4079 * :enum:`nghttp2_data_flag.NGHTTP2_DATA_FLAG_NO_END_STREAM`.  After
4080 * that, the application can send trailer fields using
4081 * `nghttp2_submit_trailer()`.  `nghttp2_submit_trailer()` can be used
4082 * inside :type:`nghttp2_data_source_read_callback`.
4083 *
4084 * This function returns 0 if it succeeds and |stream_id| is -1.
4085 * Otherwise, this function returns 0 if it succeeds, or one of the
4086 * following negative error codes:
4087 *
4088 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4089 *     Out of memory.
4090 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4091 *     The |stream_id| is 0.
4092 */
4093NGHTTP2_EXTERN int nghttp2_submit_trailer(nghttp2_session *session,
4094                                          int32_t stream_id,
4095                                          const nghttp2_nv *nva, size_t nvlen);
4096
4097/**
4098 * @function
4099 *
4100 * Submits HEADERS frame. The |flags| is bitwise OR of the
4101 * following values:
4102 *
4103 * * :enum:`nghttp2_flag.NGHTTP2_FLAG_END_STREAM`
4104 *
4105 * If |flags| includes :enum:`nghttp2_flag.NGHTTP2_FLAG_END_STREAM`,
4106 * this frame has END_STREAM flag set.
4107 *
4108 * The library handles the CONTINUATION frame internally and it
4109 * correctly sets END_HEADERS to the last sequence of the PUSH_PROMISE
4110 * or CONTINUATION frame.
4111 *
4112 * If the |stream_id| is -1, this frame is assumed as request (i.e.,
4113 * request HEADERS frame which opens new stream).  In this case, the
4114 * assigned stream ID will be returned.  Otherwise, specify stream ID
4115 * in |stream_id|.
4116 *
4117 * The |pri_spec| is priority specification of this request.  ``NULL``
4118 * means the default priority (see
4119 * `nghttp2_priority_spec_default_init()`).  To specify the priority,
4120 * use `nghttp2_priority_spec_init()`.  If |pri_spec| is not ``NULL``,
4121 * this function will copy its data members.
4122 *
4123 * The ``pri_spec->weight`` must be in [:macro:`NGHTTP2_MIN_WEIGHT`,
4124 * :macro:`NGHTTP2_MAX_WEIGHT`], inclusive.  If ``pri_spec->weight``
4125 * is strictly less than :macro:`NGHTTP2_MIN_WEIGHT`, it becomes
4126 * :macro:`NGHTTP2_MIN_WEIGHT`.  If it is strictly greater than
4127 * :macro:`NGHTTP2_MAX_WEIGHT`, it becomes :macro:`NGHTTP2_MAX_WEIGHT`.
4128 *
4129 * If
4130 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
4131 * of value of 1 is received by a remote endpoint, |pri_spec| is
4132 * ignored, and treated as if ``NULL`` is specified.
4133 *
4134 * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
4135 * |nvlen| elements.  The application is responsible to include
4136 * required pseudo-header fields (header field whose name starts with
4137 * ":") in |nva| and must place pseudo-headers before regular header
4138 * fields.
4139 *
4140 * This function creates copies of all name/value pairs in |nva|.  It
4141 * also lower-cases all names in |nva|.  The order of elements in
4142 * |nva| is preserved.  For header fields with
4143 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME` and
4144 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set,
4145 * header field name and value are not copied respectively.  With
4146 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME`, application
4147 * is responsible to pass header field name in lowercase.  The
4148 * application should maintain the references to them until
4149 * :type:`nghttp2_on_frame_send_callback` or
4150 * :type:`nghttp2_on_frame_not_send_callback` is called.
4151 *
4152 * The |stream_user_data| is a pointer to an arbitrary data which is
4153 * associated to the stream this frame will open.  Therefore it is
4154 * only used if this frame opens streams, in other words, it changes
4155 * stream state from idle or reserved to open.
4156 *
4157 * This function is low-level in a sense that the application code can
4158 * specify flags directly.  For usual HTTP request,
4159 * `nghttp2_submit_request()` is useful.  Likewise, for HTTP response,
4160 * prefer `nghttp2_submit_response()`.
4161 *
4162 * This function returns newly assigned stream ID if it succeeds and
4163 * |stream_id| is -1.  Otherwise, this function returns 0 if it
4164 * succeeds, or one of the following negative error codes:
4165 *
4166 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4167 *     Out of memory.
4168 * :enum:`nghttp2_error.NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
4169 *     No stream ID is available because maximum stream ID was
4170 *     reached.
4171 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4172 *     The |stream_id| is 0; or trying to depend on itself (stream ID
4173 *     equals ``pri_spec->stream_id``).
4174 * :enum:`nghttp2_error.NGHTTP2_ERR_DATA_EXIST`
4175 *     DATA or HEADERS has been already submitted and not fully
4176 *     processed yet.  This happens if stream denoted by |stream_id|
4177 *     is in reserved state.
4178 * :enum:`nghttp2_error.NGHTTP2_ERR_PROTO`
4179 *     The |stream_id| is -1, and |session| is server session.
4180 *
4181 * .. warning::
4182 *
4183 *   This function returns assigned stream ID if it succeeds and
4184 *   |stream_id| is -1.  But that stream is not opened yet.  The
4185 *   application must not submit frame to that stream ID before
4186 *   :type:`nghttp2_before_frame_send_callback` is called for this
4187 *   frame.
4188 *
4189 */
4190NGHTTP2_EXTERN int32_t nghttp2_submit_headers(
4191    nghttp2_session *session, uint8_t flags, int32_t stream_id,
4192    const nghttp2_priority_spec *pri_spec, const nghttp2_nv *nva, size_t nvlen,
4193    void *stream_user_data);
4194
4195/**
4196 * @function
4197 *
4198 * Submits one or more DATA frames to the stream |stream_id|.  The
4199 * data to be sent are provided by |data_prd|.  If |flags| contains
4200 * :enum:`nghttp2_flag.NGHTTP2_FLAG_END_STREAM`, the last DATA frame
4201 * has END_STREAM flag set.
4202 *
4203 * This function does not take ownership of the |data_prd|.  The
4204 * function copies the members of the |data_prd|.
4205 *
4206 * This function returns 0 if it succeeds, or one of the following
4207 * negative error codes:
4208 *
4209 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4210 *     Out of memory.
4211 * :enum:`nghttp2_error.NGHTTP2_ERR_DATA_EXIST`
4212 *     DATA or HEADERS has been already submitted and not fully
4213 *     processed yet.
4214 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4215 *     The |stream_id| is 0.
4216 * :enum:`nghttp2_error.NGHTTP2_ERR_STREAM_CLOSED`
4217 *     The stream was already closed; or the |stream_id| is invalid.
4218 *
4219 * .. note::
4220 *
4221 *   Currently, only one DATA or HEADERS is allowed for a stream at a
4222 *   time.  Submitting these frames more than once before first DATA
4223 *   or HEADERS is finished results in
4224 *   :enum:`nghttp2_error.NGHTTP2_ERR_DATA_EXIST` error code.  The
4225 *   earliest callback which tells that previous frame is done is
4226 *   :type:`nghttp2_on_frame_send_callback`.  In side that callback,
4227 *   new data can be submitted using `nghttp2_submit_data()`.  Of
4228 *   course, all data except for last one must not have
4229 *   :enum:`nghttp2_flag.NGHTTP2_FLAG_END_STREAM` flag set in |flags|.
4230 *   This sounds a bit complicated, and we recommend to use
4231 *   `nghttp2_submit_request()` and `nghttp2_submit_response()` to
4232 *   avoid this cascading issue.  The experience shows that for HTTP
4233 *   use, these two functions are enough to implement both client and
4234 *   server.
4235 */
4236NGHTTP2_EXTERN int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
4237                                       int32_t stream_id,
4238                                       const nghttp2_data_provider *data_prd);
4239
4240/**
4241 * @function
4242 *
4243 * Submits PRIORITY frame to change the priority of stream |stream_id|
4244 * to the priority specification |pri_spec|.
4245 *
4246 * The |flags| is currently ignored and should be
4247 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4248 *
4249 * The |pri_spec| is priority specification of this request.  ``NULL``
4250 * is not allowed for this function. To specify the priority, use
4251 * `nghttp2_priority_spec_init()`.  This function will copy its data
4252 * members.
4253 *
4254 * The ``pri_spec->weight`` must be in [:macro:`NGHTTP2_MIN_WEIGHT`,
4255 * :macro:`NGHTTP2_MAX_WEIGHT`], inclusive.  If ``pri_spec->weight``
4256 * is strictly less than :macro:`NGHTTP2_MIN_WEIGHT`, it becomes
4257 * :macro:`NGHTTP2_MIN_WEIGHT`.  If it is strictly greater than
4258 * :macro:`NGHTTP2_MAX_WEIGHT`, it becomes
4259 * :macro:`NGHTTP2_MAX_WEIGHT`.
4260 *
4261 * If
4262 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
4263 * of value of 1 is received by a remote endpoint, this function does
4264 * nothing and returns 0.
4265 *
4266 * This function returns 0 if it succeeds, or one of the following
4267 * negative error codes:
4268 *
4269 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4270 *     Out of memory.
4271 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4272 *     The |stream_id| is 0; or the |pri_spec| is NULL; or trying to
4273 *     depend on itself.
4274 */
4275NGHTTP2_EXTERN int
4276nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
4277                        int32_t stream_id,
4278                        const nghttp2_priority_spec *pri_spec);
4279
4280/**
4281 * @macro
4282 *
4283 * :macro:`NGHTTP2_EXTPRI_DEFAULT_URGENCY` is the default urgency
4284 * level for :rfc:`9218` extensible priorities.
4285 */
4286#define NGHTTP2_EXTPRI_DEFAULT_URGENCY 3
4287
4288/**
4289 * @macro
4290 *
4291 * :macro:`NGHTTP2_EXTPRI_URGENCY_HIGH` is the highest urgency level
4292 * for :rfc:`9218` extensible priorities.
4293 */
4294#define NGHTTP2_EXTPRI_URGENCY_HIGH 0
4295
4296/**
4297 * @macro
4298 *
4299 * :macro:`NGHTTP2_EXTPRI_URGENCY_LOW` is the lowest urgency level for
4300 * :rfc:`9218` extensible priorities.
4301 */
4302#define NGHTTP2_EXTPRI_URGENCY_LOW 7
4303
4304/**
4305 * @macro
4306 *
4307 * :macro:`NGHTTP2_EXTPRI_URGENCY_LEVELS` is the number of urgency
4308 * levels for :rfc:`9218` extensible priorities.
4309 */
4310#define NGHTTP2_EXTPRI_URGENCY_LEVELS (NGHTTP2_EXTPRI_URGENCY_LOW + 1)
4311
4312/**
4313 * @struct
4314 *
4315 * :type:`nghttp2_extpri` is :rfc:`9218` extensible priorities
4316 * specification for a stream.
4317 */
4318typedef struct nghttp2_extpri {
4319  /**
4320   * :member:`urgency` is the urgency of a stream, it must be in
4321   * [:macro:`NGHTTP2_EXTPRI_URGENCY_HIGH`,
4322   * :macro:`NGHTTP2_EXTPRI_URGENCY_LOW`], inclusive, and 0 is the
4323   * highest urgency.
4324   */
4325  uint32_t urgency;
4326  /**
4327   * :member:`inc` indicates that a content can be processed
4328   * incrementally or not.  If inc is 0, it cannot be processed
4329   * incrementally.  If inc is 1, it can be processed incrementally.
4330   * Other value is not permitted.
4331   */
4332  int inc;
4333} nghttp2_extpri;
4334
4335/**
4336 * @function
4337 *
4338 * Submits RST_STREAM frame to cancel/reject the stream |stream_id|
4339 * with the error code |error_code|.
4340 *
4341 * The pre-defined error code is one of :enum:`nghttp2_error_code`.
4342 *
4343 * The |flags| is currently ignored and should be
4344 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4345 *
4346 * This function returns 0 if it succeeds, or one of the following
4347 * negative error codes:
4348 *
4349 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4350 *     Out of memory.
4351 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4352 *     The |stream_id| is 0.
4353 */
4354NGHTTP2_EXTERN int nghttp2_submit_rst_stream(nghttp2_session *session,
4355                                             uint8_t flags, int32_t stream_id,
4356                                             uint32_t error_code);
4357
4358/**
4359 * @function
4360 *
4361 * Stores local settings and submits SETTINGS frame.  The |iv| is the
4362 * pointer to the array of :type:`nghttp2_settings_entry`.  The |niv|
4363 * indicates the number of :type:`nghttp2_settings_entry`.
4364 *
4365 * The |flags| is currently ignored and should be
4366 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4367 *
4368 * This function does not take ownership of the |iv|.  This function
4369 * copies all the elements in the |iv|.
4370 *
4371 * While updating individual stream's local window size, if the window
4372 * size becomes strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
4373 * RST_STREAM is issued against such a stream.
4374 *
4375 * SETTINGS with :enum:`nghttp2_flag.NGHTTP2_FLAG_ACK` is
4376 * automatically submitted by the library and application could not
4377 * send it at its will.
4378 *
4379 * This function returns 0 if it succeeds, or one of the following
4380 * negative error codes:
4381 *
4382 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4383 *     The |iv| contains invalid value (e.g., initial window size
4384 *     strictly greater than (1 << 31) - 1.
4385 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4386 *     Out of memory.
4387 */
4388NGHTTP2_EXTERN int nghttp2_submit_settings(nghttp2_session *session,
4389                                           uint8_t flags,
4390                                           const nghttp2_settings_entry *iv,
4391                                           size_t niv);
4392
4393/**
4394 * @function
4395 *
4396 * Submits PUSH_PROMISE frame.
4397 *
4398 * The |flags| is currently ignored.  The library handles the
4399 * CONTINUATION frame internally and it correctly sets END_HEADERS to
4400 * the last sequence of the PUSH_PROMISE or CONTINUATION frame.
4401 *
4402 * The |stream_id| must be client initiated stream ID.
4403 *
4404 * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
4405 * |nvlen| elements.  The application is responsible to include
4406 * required pseudo-header fields (header field whose name starts with
4407 * ":") in |nva| and must place pseudo-headers before regular header
4408 * fields.
4409 *
4410 * This function creates copies of all name/value pairs in |nva|.  It
4411 * also lower-cases all names in |nva|.  The order of elements in
4412 * |nva| is preserved.  For header fields with
4413 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME` and
4414 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set,
4415 * header field name and value are not copied respectively.  With
4416 * :enum:`nghttp2_nv_flag.NGHTTP2_NV_FLAG_NO_COPY_NAME`, application
4417 * is responsible to pass header field name in lowercase.  The
4418 * application should maintain the references to them until
4419 * :type:`nghttp2_on_frame_send_callback` or
4420 * :type:`nghttp2_on_frame_not_send_callback` is called.
4421 *
4422 * The |promised_stream_user_data| is a pointer to an arbitrary data
4423 * which is associated to the promised stream this frame will open and
4424 * make it in reserved state.  It is available using
4425 * `nghttp2_session_get_stream_user_data()`.  The application can
4426 * access it in :type:`nghttp2_before_frame_send_callback` and
4427 * :type:`nghttp2_on_frame_send_callback` of this frame.
4428 *
4429 * The client side is not allowed to use this function.
4430 *
4431 * To submit response headers and data, use
4432 * `nghttp2_submit_response()`.
4433 *
4434 * This function returns assigned promised stream ID if it succeeds,
4435 * or one of the following negative error codes:
4436 *
4437 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4438 *     Out of memory.
4439 * :enum:`nghttp2_error.NGHTTP2_ERR_PROTO`
4440 *     This function was invoked when |session| is initialized as
4441 *     client.
4442 * :enum:`nghttp2_error.NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
4443 *     No stream ID is available because maximum stream ID was
4444 *     reached.
4445 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4446 *     The |stream_id| is 0; The |stream_id| does not designate stream
4447 *     that peer initiated.
4448 * :enum:`nghttp2_error.NGHTTP2_ERR_STREAM_CLOSED`
4449 *     The stream was already closed; or the |stream_id| is invalid.
4450 *
4451 * .. warning::
4452 *
4453 *   This function returns assigned promised stream ID if it succeeds.
4454 *   As of 1.16.0, stream object for pushed resource is created when
4455 *   this function succeeds.  In that case, the application can submit
4456 *   push response for the promised frame.
4457 *
4458 *   In 1.15.0 or prior versions, pushed stream is not opened yet when
4459 *   this function succeeds.  The application must not submit frame to
4460 *   that stream ID before :type:`nghttp2_before_frame_send_callback`
4461 *   is called for this frame.
4462 *
4463 */
4464NGHTTP2_EXTERN int32_t nghttp2_submit_push_promise(
4465    nghttp2_session *session, uint8_t flags, int32_t stream_id,
4466    const nghttp2_nv *nva, size_t nvlen, void *promised_stream_user_data);
4467
4468/**
4469 * @function
4470 *
4471 * Submits PING frame.  You don't have to send PING back when you
4472 * received PING frame.  The library automatically submits PING frame
4473 * in this case.
4474 *
4475 * The |flags| is bitwise OR of 0 or more of the following value.
4476 *
4477 * * :enum:`nghttp2_flag.NGHTTP2_FLAG_ACK`
4478 *
4479 * Unless `nghttp2_option_set_no_auto_ping_ack()` is used, the |flags|
4480 * should be :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4481 *
4482 * If the |opaque_data| is non ``NULL``, then it should point to the 8
4483 * bytes array of memory to specify opaque data to send with PING
4484 * frame.  If the |opaque_data| is ``NULL``, zero-cleared 8 bytes will
4485 * be sent as opaque data.
4486 *
4487 * This function returns 0 if it succeeds, or one of the following
4488 * negative error codes:
4489 *
4490 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4491 *     Out of memory.
4492 */
4493NGHTTP2_EXTERN int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
4494                                       const uint8_t *opaque_data);
4495
4496/**
4497 * @function
4498 *
4499 * Submits GOAWAY frame with the last stream ID |last_stream_id| and
4500 * the error code |error_code|.
4501 *
4502 * The pre-defined error code is one of :enum:`nghttp2_error_code`.
4503 *
4504 * The |flags| is currently ignored and should be
4505 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4506 *
4507 * The |last_stream_id| is peer's stream ID or 0.  So if |session| is
4508 * initialized as client, |last_stream_id| must be even or 0.  If
4509 * |session| is initialized as server, |last_stream_id| must be odd or
4510 * 0.
4511 *
4512 * The HTTP/2 specification says last_stream_id must not be increased
4513 * from the value previously sent.  So the actual value sent as
4514 * last_stream_id is the minimum value between the given
4515 * |last_stream_id| and the last_stream_id previously sent to the
4516 * peer.
4517 *
4518 * If the |opaque_data| is not ``NULL`` and |opaque_data_len| is not
4519 * zero, those data will be sent as additional debug data.  The
4520 * library makes a copy of the memory region pointed by |opaque_data|
4521 * with the length |opaque_data_len|, so the caller does not need to
4522 * keep this memory after the return of this function.  If the
4523 * |opaque_data_len| is 0, the |opaque_data| could be ``NULL``.
4524 *
4525 * After successful transmission of GOAWAY, following things happen.
4526 * All incoming streams having strictly more than |last_stream_id| are
4527 * closed.  All incoming HEADERS which starts new stream are simply
4528 * ignored.  After all active streams are handled, both
4529 * `nghttp2_session_want_read()` and `nghttp2_session_want_write()`
4530 * return 0 and the application can close session.
4531 *
4532 * This function returns 0 if it succeeds, or one of the following
4533 * negative error codes:
4534 *
4535 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4536 *     Out of memory.
4537 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4538 *     The |opaque_data_len| is too large; the |last_stream_id| is
4539 *     invalid.
4540 */
4541NGHTTP2_EXTERN int nghttp2_submit_goaway(nghttp2_session *session,
4542                                         uint8_t flags, int32_t last_stream_id,
4543                                         uint32_t error_code,
4544                                         const uint8_t *opaque_data,
4545                                         size_t opaque_data_len);
4546
4547/**
4548 * @function
4549 *
4550 * Returns the last stream ID of a stream for which
4551 * :type:`nghttp2_on_frame_recv_callback` was invoked most recently.
4552 * The returned value can be used as last_stream_id parameter for
4553 * `nghttp2_submit_goaway()` and
4554 * `nghttp2_session_terminate_session2()`.
4555 *
4556 * This function always succeeds.
4557 */
4558NGHTTP2_EXTERN int32_t
4559nghttp2_session_get_last_proc_stream_id(nghttp2_session *session);
4560
4561/**
4562 * @function
4563 *
4564 * Returns nonzero if new request can be sent from local endpoint.
4565 *
4566 * This function return 0 if request is not allowed for this session.
4567 * There are several reasons why request is not allowed.  Some of the
4568 * reasons are: session is server; stream ID has been spent; GOAWAY
4569 * has been sent or received.
4570 *
4571 * The application can call `nghttp2_submit_request()` without
4572 * consulting this function.  In that case, `nghttp2_submit_request()`
4573 * may return error.  Or, request is failed to sent, and
4574 * :type:`nghttp2_on_stream_close_callback` is called.
4575 */
4576NGHTTP2_EXTERN int
4577nghttp2_session_check_request_allowed(nghttp2_session *session);
4578
4579/**
4580 * @function
4581 *
4582 * Returns nonzero if |session| is initialized as server side session.
4583 */
4584NGHTTP2_EXTERN int
4585nghttp2_session_check_server_session(nghttp2_session *session);
4586
4587/**
4588 * @function
4589 *
4590 * Submits WINDOW_UPDATE frame.
4591 *
4592 * The |flags| is currently ignored and should be
4593 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4594 *
4595 * The |stream_id| is the stream ID to send this WINDOW_UPDATE.  To
4596 * send connection level WINDOW_UPDATE, specify 0 to |stream_id|.
4597 *
4598 * If the |window_size_increment| is positive, the WINDOW_UPDATE with
4599 * that value as window_size_increment is queued.  If the
4600 * |window_size_increment| is larger than the received bytes from the
4601 * remote endpoint, the local window size is increased by that
4602 * difference.  If the sole purpose is to increase the local window
4603 * size, consider to use `nghttp2_session_set_local_window_size()`.
4604 *
4605 * If the |window_size_increment| is negative, the local window size
4606 * is decreased by -|window_size_increment|.  If automatic
4607 * WINDOW_UPDATE is enabled
4608 * (`nghttp2_option_set_no_auto_window_update()`), and the library
4609 * decided that the WINDOW_UPDATE should be submitted, then
4610 * WINDOW_UPDATE is queued with the current received bytes count.  If
4611 * the sole purpose is to decrease the local window size, consider to
4612 * use `nghttp2_session_set_local_window_size()`.
4613 *
4614 * If the |window_size_increment| is 0, the function does nothing and
4615 * returns 0.
4616 *
4617 * This function returns 0 if it succeeds, or one of the following
4618 * negative error codes:
4619 *
4620 * :enum:`nghttp2_error.NGHTTP2_ERR_FLOW_CONTROL`
4621 *     The local window size overflow or gets negative.
4622 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4623 *     Out of memory.
4624 */
4625NGHTTP2_EXTERN int nghttp2_submit_window_update(nghttp2_session *session,
4626                                                uint8_t flags,
4627                                                int32_t stream_id,
4628                                                int32_t window_size_increment);
4629
4630/**
4631 * @function
4632 *
4633 * Set local window size (local endpoints's window size) to the given
4634 * |window_size| for the given stream denoted by |stream_id|.  To
4635 * change connection level window size, specify 0 to |stream_id|.  To
4636 * increase window size, this function may submit WINDOW_UPDATE frame
4637 * to transmission queue.
4638 *
4639 * The |flags| is currently ignored and should be
4640 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4641 *
4642 * This sounds similar to `nghttp2_submit_window_update()`, but there
4643 * are 2 differences.  The first difference is that this function
4644 * takes the absolute value of window size to set, rather than the
4645 * delta.  To change the window size, this may be easier to use since
4646 * the application just declares the intended window size, rather than
4647 * calculating delta.  The second difference is that
4648 * `nghttp2_submit_window_update()` affects the received bytes count
4649 * which has not acked yet.  By the specification of
4650 * `nghttp2_submit_window_update()`, to strictly increase the local
4651 * window size, we have to submit delta including all received bytes
4652 * count, which might not be desirable in some cases.  On the other
4653 * hand, this function does not affect the received bytes count.  It
4654 * just sets the local window size to the given value.
4655 *
4656 * This function returns 0 if it succeeds, or one of the following
4657 * negative error codes:
4658 *
4659 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4660 *     The |stream_id| is negative.
4661 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4662 *     Out of memory.
4663 */
4664NGHTTP2_EXTERN int
4665nghttp2_session_set_local_window_size(nghttp2_session *session, uint8_t flags,
4666                                      int32_t stream_id, int32_t window_size);
4667
4668/**
4669 * @function
4670 *
4671 * Submits extension frame.
4672 *
4673 * Application can pass arbitrary frame flags and stream ID in |flags|
4674 * and |stream_id| respectively.  The |payload| is opaque pointer, and
4675 * it can be accessible though ``frame->ext.payload`` in
4676 * :type:`nghttp2_pack_extension_callback`.  The library will not own
4677 * passed |payload| pointer.
4678 *
4679 * The application must set :type:`nghttp2_pack_extension_callback`
4680 * using `nghttp2_session_callbacks_set_pack_extension_callback()`.
4681 *
4682 * The application should retain the memory pointed by |payload| until
4683 * the transmission of extension frame is done (which is indicated by
4684 * :type:`nghttp2_on_frame_send_callback`), or transmission fails
4685 * (which is indicated by :type:`nghttp2_on_frame_not_send_callback`).
4686 * If application does not touch this memory region after packing it
4687 * into a wire format, application can free it inside
4688 * :type:`nghttp2_pack_extension_callback`.
4689 *
4690 * The standard HTTP/2 frame cannot be sent with this function, so
4691 * |type| must be strictly grater than 0x9.  Otherwise, this function
4692 * will fail with error code
4693 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`.
4694 *
4695 * This function returns 0 if it succeeds, or one of the following
4696 * negative error codes:
4697 *
4698 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
4699 *     If :type:`nghttp2_pack_extension_callback` is not set.
4700 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4701 *     If  |type| specifies  standard  HTTP/2 frame  type.  The  frame
4702 *     types  in the  rage [0x0,  0x9], both  inclusive, are  standard
4703 *     HTTP/2 frame type, and cannot be sent using this function.
4704 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4705 *     Out of memory
4706 */
4707NGHTTP2_EXTERN int nghttp2_submit_extension(nghttp2_session *session,
4708                                            uint8_t type, uint8_t flags,
4709                                            int32_t stream_id, void *payload);
4710
4711/**
4712 * @struct
4713 *
4714 * The payload of ALTSVC frame.  ALTSVC frame is a non-critical
4715 * extension to HTTP/2.  If this frame is received, and
4716 * `nghttp2_option_set_user_recv_extension_type()` is not set, and
4717 * `nghttp2_option_set_builtin_recv_extension_type()` is set for
4718 * :enum:`nghttp2_frame_type.NGHTTP2_ALTSVC`,
4719 * ``nghttp2_extension.payload`` will point to this struct.
4720 *
4721 * It has the following members:
4722 */
4723typedef struct {
4724  /**
4725   * The pointer to origin which this alternative service is
4726   * associated with.  This is not necessarily NULL-terminated.
4727   */
4728  uint8_t *origin;
4729  /**
4730   * The length of the |origin|.
4731   */
4732  size_t origin_len;
4733  /**
4734   * The pointer to Alt-Svc field value contained in ALTSVC frame.
4735   * This is not necessarily NULL-terminated.
4736   */
4737  uint8_t *field_value;
4738  /**
4739   * The length of the |field_value|.
4740   */
4741  size_t field_value_len;
4742} nghttp2_ext_altsvc;
4743
4744/**
4745 * @function
4746 *
4747 * Submits ALTSVC frame.
4748 *
4749 * ALTSVC frame is a non-critical extension to HTTP/2, and defined in
4750 * `RFC 7383 <https://tools.ietf.org/html/rfc7838#section-4>`_.
4751 *
4752 * The |flags| is currently ignored and should be
4753 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4754 *
4755 * The |origin| points to the origin this alternative service is
4756 * associated with.  The |origin_len| is the length of the origin.  If
4757 * |stream_id| is 0, the origin must be specified.  If |stream_id| is
4758 * not zero, the origin must be empty (in other words, |origin_len|
4759 * must be 0).
4760 *
4761 * The ALTSVC frame is only usable from server side.  If this function
4762 * is invoked with client side session, this function returns
4763 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`.
4764 *
4765 * This function returns 0 if it succeeds, or one of the following
4766 * negative error codes:
4767 *
4768 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4769 *     Out of memory
4770 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
4771 *     The function is called from client side session
4772 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4773 *     The sum of |origin_len| and |field_value_len| is larger than
4774 *     16382; or |origin_len| is 0 while |stream_id| is 0; or
4775 *     |origin_len| is not 0 while |stream_id| is not 0.
4776 */
4777NGHTTP2_EXTERN int nghttp2_submit_altsvc(nghttp2_session *session,
4778                                         uint8_t flags, int32_t stream_id,
4779                                         const uint8_t *origin,
4780                                         size_t origin_len,
4781                                         const uint8_t *field_value,
4782                                         size_t field_value_len);
4783
4784/**
4785 * @struct
4786 *
4787 * The single entry of an origin.
4788 */
4789typedef struct {
4790  /**
4791   * The pointer to origin.  No validation is made against this field
4792   * by the library.  This is not necessarily NULL-terminated.
4793   */
4794  uint8_t *origin;
4795  /**
4796   * The length of the |origin|.
4797   */
4798  size_t origin_len;
4799} nghttp2_origin_entry;
4800
4801/**
4802 * @struct
4803 *
4804 * The payload of ORIGIN frame.  ORIGIN frame is a non-critical
4805 * extension to HTTP/2 and defined by `RFC 8336
4806 * <https://tools.ietf.org/html/rfc8336>`_.
4807 *
4808 * If this frame is received, and
4809 * `nghttp2_option_set_user_recv_extension_type()` is not set, and
4810 * `nghttp2_option_set_builtin_recv_extension_type()` is set for
4811 * :enum:`nghttp2_frame_type.NGHTTP2_ORIGIN`,
4812 * ``nghttp2_extension.payload`` will point to this struct.
4813 *
4814 * It has the following members:
4815 */
4816typedef struct {
4817  /**
4818   * The number of origins contained in |ov|.
4819   */
4820  size_t nov;
4821  /**
4822   * The pointer to the array of origins contained in ORIGIN frame.
4823   */
4824  nghttp2_origin_entry *ov;
4825} nghttp2_ext_origin;
4826
4827/**
4828 * @function
4829 *
4830 * Submits ORIGIN frame.
4831 *
4832 * ORIGIN frame is a non-critical extension to HTTP/2 and defined by
4833 * `RFC 8336 <https://tools.ietf.org/html/rfc8336>`_.
4834 *
4835 * The |flags| is currently ignored and should be
4836 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4837 *
4838 * The |ov| points to the array of origins.  The |nov| specifies the
4839 * number of origins included in |ov|.  This function creates copies
4840 * of all elements in |ov|.
4841 *
4842 * The ORIGIN frame is only usable by a server.  If this function is
4843 * invoked with client side session, this function returns
4844 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`.
4845 *
4846 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4847 *     Out of memory
4848 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
4849 *     The function is called from client side session.
4850 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4851 *     There are too many origins, or an origin is too large to fit
4852 *     into a default frame payload.
4853 */
4854NGHTTP2_EXTERN int nghttp2_submit_origin(nghttp2_session *session,
4855                                         uint8_t flags,
4856                                         const nghttp2_origin_entry *ov,
4857                                         size_t nov);
4858
4859/**
4860 * @struct
4861 *
4862 * The payload of PRIORITY_UPDATE frame.  PRIORITY_UPDATE frame is a
4863 * non-critical extension to HTTP/2.  If this frame is received, and
4864 * `nghttp2_option_set_user_recv_extension_type()` is not set, and
4865 * `nghttp2_option_set_builtin_recv_extension_type()` is set for
4866 * :enum:`nghttp2_frame_type.NGHTTP2_PRIORITY_UPDATE`,
4867 * ``nghttp2_extension.payload`` will point to this struct.
4868 *
4869 * It has the following members:
4870 */
4871typedef struct {
4872  /**
4873   * The stream ID of the stream whose priority is updated.
4874   */
4875  int32_t stream_id;
4876  /**
4877   * The pointer to Priority field value.  It is not necessarily
4878   * NULL-terminated.
4879   */
4880  uint8_t *field_value;
4881  /**
4882   * The length of the :member:`field_value`.
4883   */
4884  size_t field_value_len;
4885} nghttp2_ext_priority_update;
4886
4887/**
4888 * @function
4889 *
4890 * Submits PRIORITY_UPDATE frame.
4891 *
4892 * PRIORITY_UPDATE frame is a non-critical extension to HTTP/2, and
4893 * defined in :rfc:`9218#section-7.1`.
4894 *
4895 * The |flags| is currently ignored and should be
4896 * :enum:`nghttp2_flag.NGHTTP2_FLAG_NONE`.
4897 *
4898 * The |stream_id| is the ID of stream which is prioritized.  The
4899 * |field_value| points to the Priority field value.  The
4900 * |field_value_len| is the length of the Priority field value.
4901 *
4902 * If this function is called by server,
4903 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE` is returned.
4904 *
4905 * If
4906 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
4907 * of value of 0 is received by a remote endpoint (or it is omitted),
4908 * this function does nothing and returns 0.
4909 *
4910 * This function returns 0 if it succeeds, or one of the following
4911 * negative error codes:
4912 *
4913 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4914 *     Out of memory
4915 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
4916 *     The function is called from server side session
4917 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4918 *     The |field_value_len| is larger than 16380; or |stream_id| is
4919 *     0.
4920 */
4921NGHTTP2_EXTERN int nghttp2_submit_priority_update(nghttp2_session *session,
4922                                                  uint8_t flags,
4923                                                  int32_t stream_id,
4924                                                  const uint8_t *field_value,
4925                                                  size_t field_value_len);
4926
4927/**
4928 * @function
4929 *
4930 * Changes the priority of the existing stream denoted by |stream_id|.
4931 * The new priority is |extpri|.  This function is meant to be used by
4932 * server for :rfc:`9218` extensible prioritization scheme.
4933 *
4934 * If |session| is initialized as client, this function returns
4935 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`.  For client, use
4936 * `nghttp2_submit_priority_update()` instead.
4937 *
4938 * If :member:`extpri->urgency <nghttp2_extpri.urgency>` is out of
4939 * bound, it is set to :macro:`NGHTTP2_EXTPRI_URGENCY_LOW`.
4940 *
4941 * If |ignore_client_signal| is nonzero, server starts to ignore
4942 * client priority signals for this stream.
4943 *
4944 * If
4945 * :enum:`nghttp2_settings_id.NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES`
4946 * of value of 1 is not submitted via `nghttp2_submit_settings()`,
4947 * this function does nothing and returns 0.
4948 *
4949 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
4950 *     Out of memory.
4951 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
4952 *     The |session| is initialized as client.
4953 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_ARGUMENT`
4954 *     |stream_id| is zero; or a stream denoted by |stream_id| is not
4955 *     found.
4956 */
4957NGHTTP2_EXTERN int nghttp2_session_change_extpri_stream_priority(
4958    nghttp2_session *session, int32_t stream_id, const nghttp2_extpri *extpri,
4959    int ignore_client_signal);
4960
4961/**
4962 * @function
4963 *
4964 * Compares ``lhs->name`` of length ``lhs->namelen`` bytes and
4965 * ``rhs->name`` of length ``rhs->namelen`` bytes.  Returns negative
4966 * integer if ``lhs->name`` is found to be less than ``rhs->name``; or
4967 * returns positive integer if ``lhs->name`` is found to be greater
4968 * than ``rhs->name``; or returns 0 otherwise.
4969 */
4970NGHTTP2_EXTERN int nghttp2_nv_compare_name(const nghttp2_nv *lhs,
4971                                           const nghttp2_nv *rhs);
4972
4973/**
4974 * @function
4975 *
4976 * A helper function for dealing with NPN in client side or ALPN in
4977 * server side.  The |in| contains peer's protocol list in preferable
4978 * order.  The format of |in| is length-prefixed and not
4979 * null-terminated.  For example, ``h2`` and
4980 * ``http/1.1`` stored in |in| like this::
4981 *
4982 *     in[0] = 2
4983 *     in[1..2] = "h2"
4984 *     in[3] = 8
4985 *     in[4..11] = "http/1.1"
4986 *     inlen = 12
4987 *
4988 * The selection algorithm is as follows:
4989 *
4990 * 1. If peer's list contains HTTP/2 protocol the library supports,
4991 *    it is selected and returns 1. The following step is not taken.
4992 *
4993 * 2. If peer's list contains ``http/1.1``, this function selects
4994 *    ``http/1.1`` and returns 0.  The following step is not taken.
4995 *
4996 * 3. This function selects nothing and returns -1 (So called
4997 *    non-overlap case).  In this case, |out| and |outlen| are left
4998 *    untouched.
4999 *
5000 * Selecting ``h2`` means that ``h2`` is written into |*out| and its
5001 * length (which is 2) is assigned to |*outlen|.
5002 *
5003 * For ALPN, refer to https://tools.ietf.org/html/rfc7301
5004 *
5005 * See http://technotes.googlecode.com/git/nextprotoneg.html for more
5006 * details about NPN.
5007 *
5008 * For NPN, to use this method you should do something like::
5009 *
5010 *     static int select_next_proto_cb(SSL* ssl,
5011 *                                     unsigned char **out,
5012 *                                     unsigned char *outlen,
5013 *                                     const unsigned char *in,
5014 *                                     unsigned int inlen,
5015 *                                     void *arg)
5016 *     {
5017 *         int rv;
5018 *         rv = nghttp2_select_next_protocol(out, outlen, in, inlen);
5019 *         if (rv == -1) {
5020 *             return SSL_TLSEXT_ERR_NOACK;
5021 *         }
5022 *         if (rv == 1) {
5023 *             ((MyType*)arg)->http2_selected = 1;
5024 *         }
5025 *         return SSL_TLSEXT_ERR_OK;
5026 *     }
5027 *     ...
5028 *     SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, my_obj);
5029 *
5030 */
5031NGHTTP2_EXTERN int nghttp2_select_next_protocol(unsigned char **out,
5032                                                unsigned char *outlen,
5033                                                const unsigned char *in,
5034                                                unsigned int inlen);
5035
5036/**
5037 * @function
5038 *
5039 * Returns a pointer to a nghttp2_info struct with version information
5040 * about the run-time library in use.  The |least_version| argument
5041 * can be set to a 24 bit numerical value for the least accepted
5042 * version number and if the condition is not met, this function will
5043 * return a ``NULL``.  Pass in 0 to skip the version checking.
5044 */
5045NGHTTP2_EXTERN nghttp2_info *nghttp2_version(int least_version);
5046
5047/**
5048 * @function
5049 *
5050 * Returns nonzero if the :type:`nghttp2_error` library error code
5051 * |lib_error| is fatal.
5052 */
5053NGHTTP2_EXTERN int nghttp2_is_fatal(int lib_error_code);
5054
5055/**
5056 * @function
5057 *
5058 * Returns nonzero if HTTP header field name |name| of length |len| is
5059 * valid according to http://tools.ietf.org/html/rfc7230#section-3.2
5060 *
5061 * Because this is a header field name in HTTP2, the upper cased alphabet
5062 * is treated as error.
5063 */
5064NGHTTP2_EXTERN int nghttp2_check_header_name(const uint8_t *name, size_t len);
5065
5066/**
5067 * @function
5068 *
5069 * Returns nonzero if HTTP header field value |value| of length |len|
5070 * is valid according to
5071 * http://tools.ietf.org/html/rfc7230#section-3.2
5072 *
5073 * This function is considered obsolete, and application should
5074 * consider to use `nghttp2_check_header_value_rfc9113()` instead.
5075 */
5076NGHTTP2_EXTERN int nghttp2_check_header_value(const uint8_t *value, size_t len);
5077
5078/**
5079 * @function
5080 *
5081 * Returns nonzero if HTTP header field value |value| of length |len|
5082 * is valid according to
5083 * http://tools.ietf.org/html/rfc7230#section-3.2, plus
5084 * https://datatracker.ietf.org/doc/html/rfc9113#section-8.2.1
5085 */
5086NGHTTP2_EXTERN int nghttp2_check_header_value_rfc9113(const uint8_t *value,
5087                                                      size_t len);
5088
5089/**
5090 * @function
5091 *
5092 * Returns nonzero if the |value| which is supposed to be the value of
5093 * the :method header field is valid according to
5094 * https://datatracker.ietf.org/doc/html/rfc7231#section-4 and
5095 * https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6
5096 */
5097NGHTTP2_EXTERN int nghttp2_check_method(const uint8_t *value, size_t len);
5098
5099/**
5100 * @function
5101 *
5102 * Returns nonzero if the |value| which is supposed to be the value of
5103 * the :path header field is valid according to
5104 * https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.3
5105 *
5106 * |value| is valid if it merely consists of the allowed characters.
5107 * In particular, it does not check whether |value| follows the syntax
5108 * of path.  The allowed characters are all characters valid by
5109 * `nghttp2_check_header_value` minus SPC and HT.
5110 */
5111NGHTTP2_EXTERN int nghttp2_check_path(const uint8_t *value, size_t len);
5112
5113/**
5114 * @function
5115 *
5116 * Returns nonzero if the |value| which is supposed to be the value of the
5117 * :authority or host header field is valid according to
5118 * https://tools.ietf.org/html/rfc3986#section-3.2
5119 *
5120 * |value| is valid if it merely consists of the allowed characters.
5121 * In particular, it does not check whether |value| follows the syntax
5122 * of authority.
5123 */
5124NGHTTP2_EXTERN int nghttp2_check_authority(const uint8_t *value, size_t len);
5125
5126/* HPACK API */
5127
5128struct nghttp2_hd_deflater;
5129
5130/**
5131 * @struct
5132 *
5133 * HPACK deflater object.
5134 */
5135typedef struct nghttp2_hd_deflater nghttp2_hd_deflater;
5136
5137/**
5138 * @function
5139 *
5140 * Initializes |*deflater_ptr| for deflating name/values pairs.
5141 *
5142 * The |max_deflate_dynamic_table_size| is the upper bound of header
5143 * table size the deflater will use.
5144 *
5145 * If this function fails, |*deflater_ptr| is left untouched.
5146 *
5147 * This function returns 0 if it succeeds, or one of the following
5148 * negative error codes:
5149 *
5150 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5151 *     Out of memory.
5152 */
5153NGHTTP2_EXTERN int
5154nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
5155                       size_t max_deflate_dynamic_table_size);
5156
5157/**
5158 * @function
5159 *
5160 * Like `nghttp2_hd_deflate_new()`, but with additional custom memory
5161 * allocator specified in the |mem|.
5162 *
5163 * The |mem| can be ``NULL`` and the call is equivalent to
5164 * `nghttp2_hd_deflate_new()`.
5165 *
5166 * This function does not take ownership |mem|.  The application is
5167 * responsible for freeing |mem|.
5168 *
5169 * The library code does not refer to |mem| pointer after this
5170 * function returns, so the application can safely free it.
5171 */
5172NGHTTP2_EXTERN int
5173nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
5174                        size_t max_deflate_dynamic_table_size,
5175                        nghttp2_mem *mem);
5176
5177/**
5178 * @function
5179 *
5180 * Deallocates any resources allocated for |deflater|.
5181 */
5182NGHTTP2_EXTERN void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater);
5183
5184/**
5185 * @function
5186 *
5187 * Changes header table size of the |deflater| to
5188 * |settings_max_dynamic_table_size| bytes.  This may trigger eviction
5189 * in the dynamic table.
5190 *
5191 * The |settings_max_dynamic_table_size| should be the value received
5192 * in SETTINGS_HEADER_TABLE_SIZE.
5193 *
5194 * The deflater never uses more memory than
5195 * ``max_deflate_dynamic_table_size`` bytes specified in
5196 * `nghttp2_hd_deflate_new()`.  Therefore, if
5197 * |settings_max_dynamic_table_size| >
5198 * ``max_deflate_dynamic_table_size``, resulting maximum table size
5199 * becomes ``max_deflate_dynamic_table_size``.
5200 *
5201 * This function returns 0 if it succeeds, or one of the following
5202 * negative error codes:
5203 *
5204 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5205 *     Out of memory.
5206 */
5207NGHTTP2_EXTERN int
5208nghttp2_hd_deflate_change_table_size(nghttp2_hd_deflater *deflater,
5209                                     size_t settings_max_dynamic_table_size);
5210
5211/**
5212 * @function
5213 *
5214 * Deflates the |nva|, which has the |nvlen| name/value pairs, into
5215 * the |buf| of length |buflen|.
5216 *
5217 * If |buf| is not large enough to store the deflated header block,
5218 * this function fails with
5219 * :enum:`nghttp2_error.NGHTTP2_ERR_INSUFF_BUFSIZE`.  The caller
5220 * should use `nghttp2_hd_deflate_bound()` to know the upper bound of
5221 * buffer size required to deflate given header name/value pairs.
5222 *
5223 * Once this function fails, subsequent call of this function always
5224 * returns :enum:`nghttp2_error.NGHTTP2_ERR_HEADER_COMP`.
5225 *
5226 * After this function returns, it is safe to delete the |nva|.
5227 *
5228 * This function returns the number of bytes written to |buf| if it
5229 * succeeds, or one of the following negative error codes:
5230 *
5231 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5232 *     Out of memory.
5233 * :enum:`nghttp2_error.NGHTTP2_ERR_HEADER_COMP`
5234 *     Deflation process has failed.
5235 * :enum:`nghttp2_error.NGHTTP2_ERR_INSUFF_BUFSIZE`
5236 *     The provided |buflen| size is too small to hold the output.
5237 */
5238NGHTTP2_EXTERN ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater,
5239                                             uint8_t *buf, size_t buflen,
5240                                             const nghttp2_nv *nva,
5241                                             size_t nvlen);
5242
5243/**
5244 * @function
5245 *
5246 * Deflates the |nva|, which has the |nvlen| name/value pairs, into
5247 * the |veclen| size of buf vector |vec|.  The each size of buffer
5248 * must be set in len field of :type:`nghttp2_vec`.  If and only if
5249 * one chunk is filled up completely, next chunk will be used.  If
5250 * |vec| is not large enough to store the deflated header block, this
5251 * function fails with
5252 * :enum:`nghttp2_error.NGHTTP2_ERR_INSUFF_BUFSIZE`.  The caller
5253 * should use `nghttp2_hd_deflate_bound()` to know the upper bound of
5254 * buffer size required to deflate given header name/value pairs.
5255 *
5256 * Once this function fails, subsequent call of this function always
5257 * returns :enum:`nghttp2_error.NGHTTP2_ERR_HEADER_COMP`.
5258 *
5259 * After this function returns, it is safe to delete the |nva|.
5260 *
5261 * This function returns the number of bytes written to |vec| if it
5262 * succeeds, or one of the following negative error codes:
5263 *
5264 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5265 *     Out of memory.
5266 * :enum:`nghttp2_error.NGHTTP2_ERR_HEADER_COMP`
5267 *     Deflation process has failed.
5268 * :enum:`nghttp2_error.NGHTTP2_ERR_INSUFF_BUFSIZE`
5269 *     The provided |buflen| size is too small to hold the output.
5270 */
5271NGHTTP2_EXTERN ssize_t nghttp2_hd_deflate_hd_vec(nghttp2_hd_deflater *deflater,
5272                                                 const nghttp2_vec *vec,
5273                                                 size_t veclen,
5274                                                 const nghttp2_nv *nva,
5275                                                 size_t nvlen);
5276
5277/**
5278 * @function
5279 *
5280 * Returns an upper bound on the compressed size after deflation of
5281 * |nva| of length |nvlen|.
5282 */
5283NGHTTP2_EXTERN size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater,
5284                                               const nghttp2_nv *nva,
5285                                               size_t nvlen);
5286
5287/**
5288 * @function
5289 *
5290 * Returns the number of entries that header table of |deflater|
5291 * contains.  This is the sum of the number of static table and
5292 * dynamic table, so the return value is at least 61.
5293 */
5294NGHTTP2_EXTERN
5295size_t nghttp2_hd_deflate_get_num_table_entries(nghttp2_hd_deflater *deflater);
5296
5297/**
5298 * @function
5299 *
5300 * Returns the table entry denoted by |idx| from header table of
5301 * |deflater|.  The |idx| is 1-based, and idx=1 returns first entry of
5302 * static table.  idx=62 returns first entry of dynamic table if it
5303 * exists.  Specifying idx=0 is error, and this function returns NULL.
5304 * If |idx| is strictly greater than the number of entries the tables
5305 * contain, this function returns NULL.
5306 */
5307NGHTTP2_EXTERN
5308const nghttp2_nv *
5309nghttp2_hd_deflate_get_table_entry(nghttp2_hd_deflater *deflater, size_t idx);
5310
5311/**
5312 * @function
5313 *
5314 * Returns the used dynamic table size, including the overhead 32
5315 * bytes per entry described in RFC 7541.
5316 */
5317NGHTTP2_EXTERN
5318size_t nghttp2_hd_deflate_get_dynamic_table_size(nghttp2_hd_deflater *deflater);
5319
5320/**
5321 * @function
5322 *
5323 * Returns the maximum dynamic table size.
5324 */
5325NGHTTP2_EXTERN
5326size_t
5327nghttp2_hd_deflate_get_max_dynamic_table_size(nghttp2_hd_deflater *deflater);
5328
5329struct nghttp2_hd_inflater;
5330
5331/**
5332 * @struct
5333 *
5334 * HPACK inflater object.
5335 */
5336typedef struct nghttp2_hd_inflater nghttp2_hd_inflater;
5337
5338/**
5339 * @function
5340 *
5341 * Initializes |*inflater_ptr| for inflating name/values pairs.
5342 *
5343 * If this function fails, |*inflater_ptr| is left untouched.
5344 *
5345 * This function returns 0 if it succeeds, or one of the following
5346 * negative error codes:
5347 *
5348 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5349 *     Out of memory.
5350 */
5351NGHTTP2_EXTERN int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr);
5352
5353/**
5354 * @function
5355 *
5356 * Like `nghttp2_hd_inflate_new()`, but with additional custom memory
5357 * allocator specified in the |mem|.
5358 *
5359 * The |mem| can be ``NULL`` and the call is equivalent to
5360 * `nghttp2_hd_inflate_new()`.
5361 *
5362 * This function does not take ownership |mem|.  The application is
5363 * responsible for freeing |mem|.
5364 *
5365 * The library code does not refer to |mem| pointer after this
5366 * function returns, so the application can safely free it.
5367 */
5368NGHTTP2_EXTERN int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
5369                                           nghttp2_mem *mem);
5370
5371/**
5372 * @function
5373 *
5374 * Deallocates any resources allocated for |inflater|.
5375 */
5376NGHTTP2_EXTERN void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater);
5377
5378/**
5379 * @function
5380 *
5381 * Changes header table size in the |inflater|.  This may trigger
5382 * eviction in the dynamic table.
5383 *
5384 * The |settings_max_dynamic_table_size| should be the value
5385 * transmitted in SETTINGS_HEADER_TABLE_SIZE.
5386 *
5387 * This function must not be called while header block is being
5388 * inflated.  In other words, this function must be called after
5389 * initialization of |inflater|, but before calling
5390 * `nghttp2_hd_inflate_hd2()`, or after
5391 * `nghttp2_hd_inflate_end_headers()`.  Otherwise,
5392 * `NGHTTP2_ERR_INVALID_STATE` was returned.
5393 *
5394 * This function returns 0 if it succeeds, or one of the following
5395 * negative error codes:
5396 *
5397 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5398 *     Out of memory.
5399 * :enum:`nghttp2_error.NGHTTP2_ERR_INVALID_STATE`
5400 *     The function is called while header block is being inflated.
5401 *     Probably, application missed to call
5402 *     `nghttp2_hd_inflate_end_headers()`.
5403 */
5404NGHTTP2_EXTERN int
5405nghttp2_hd_inflate_change_table_size(nghttp2_hd_inflater *inflater,
5406                                     size_t settings_max_dynamic_table_size);
5407
5408/**
5409 * @enum
5410 *
5411 * The flags for header inflation.
5412 */
5413typedef enum {
5414  /**
5415   * No flag set.
5416   */
5417  NGHTTP2_HD_INFLATE_NONE = 0,
5418  /**
5419   * Indicates all headers were inflated.
5420   */
5421  NGHTTP2_HD_INFLATE_FINAL = 0x01,
5422  /**
5423   * Indicates a header was emitted.
5424   */
5425  NGHTTP2_HD_INFLATE_EMIT = 0x02
5426} nghttp2_hd_inflate_flag;
5427
5428/**
5429 * @function
5430 *
5431 * .. warning::
5432 *
5433 *   Deprecated.  Use `nghttp2_hd_inflate_hd2()` instead.
5434 *
5435 * Inflates name/value block stored in |in| with length |inlen|.  This
5436 * function performs decompression.  For each successful emission of
5437 * header name/value pair,
5438 * :enum:`nghttp2_hd_inflate_flag.NGHTTP2_HD_INFLATE_EMIT` is set in
5439 * |*inflate_flags| and name/value pair is assigned to the |nv_out|
5440 * and the function returns.  The caller must not free the members of
5441 * |nv_out|.
5442 *
5443 * The |nv_out| may include pointers to the memory region in the |in|.
5444 * The caller must retain the |in| while the |nv_out| is used.
5445 *
5446 * The application should call this function repeatedly until the
5447 * ``(*inflate_flags) & NGHTTP2_HD_INFLATE_FINAL`` is nonzero and
5448 * return value is non-negative.  This means the all input values are
5449 * processed successfully.  Then the application must call
5450 * `nghttp2_hd_inflate_end_headers()` to prepare for the next header
5451 * block input.
5452 *
5453 * The caller can feed complete compressed header block.  It also can
5454 * feed it in several chunks.  The caller must set |in_final| to
5455 * nonzero if the given input is the last block of the compressed
5456 * header.
5457 *
5458 * This function returns the number of bytes processed if it succeeds,
5459 * or one of the following negative error codes:
5460 *
5461 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5462 *     Out of memory.
5463 * :enum:`nghttp2_error.NGHTTP2_ERR_HEADER_COMP`
5464 *     Inflation process has failed.
5465 * :enum:`nghttp2_error.NGHTTP2_ERR_BUFFER_ERROR`
5466 *     The header field name or value is too large.
5467 *
5468 * Example follows::
5469 *
5470 *     int inflate_header_block(nghttp2_hd_inflater *hd_inflater,
5471 *                              uint8_t *in, size_t inlen, int final)
5472 *     {
5473 *         ssize_t rv;
5474 *
5475 *         for(;;) {
5476 *             nghttp2_nv nv;
5477 *             int inflate_flags = 0;
5478 *
5479 *             rv = nghttp2_hd_inflate_hd(hd_inflater, &nv, &inflate_flags,
5480 *                                        in, inlen, final);
5481 *
5482 *             if(rv < 0) {
5483 *                 fprintf(stderr, "inflate failed with error code %zd", rv);
5484 *                 return -1;
5485 *             }
5486 *
5487 *             in += rv;
5488 *             inlen -= rv;
5489 *
5490 *             if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
5491 *                 fwrite(nv.name, nv.namelen, 1, stderr);
5492 *                 fprintf(stderr, ": ");
5493 *                 fwrite(nv.value, nv.valuelen, 1, stderr);
5494 *                 fprintf(stderr, "\n");
5495 *             }
5496 *             if(inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
5497 *                 nghttp2_hd_inflate_end_headers(hd_inflater);
5498 *                 break;
5499 *             }
5500 *             if((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
5501 *                inlen == 0) {
5502 *                break;
5503 *             }
5504 *         }
5505 *
5506 *         return 0;
5507 *     }
5508 *
5509 */
5510NGHTTP2_EXTERN ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater,
5511                                             nghttp2_nv *nv_out,
5512                                             int *inflate_flags, uint8_t *in,
5513                                             size_t inlen, int in_final);
5514
5515/**
5516 * @function
5517 *
5518 * Inflates name/value block stored in |in| with length |inlen|.  This
5519 * function performs decompression.  For each successful emission of
5520 * header name/value pair,
5521 * :enum:`nghttp2_hd_inflate_flag.NGHTTP2_HD_INFLATE_EMIT` is set in
5522 * |*inflate_flags| and name/value pair is assigned to the |nv_out|
5523 * and the function returns.  The caller must not free the members of
5524 * |nv_out|.
5525 *
5526 * The |nv_out| may include pointers to the memory region in the |in|.
5527 * The caller must retain the |in| while the |nv_out| is used.
5528 *
5529 * The application should call this function repeatedly until the
5530 * ``(*inflate_flags) & NGHTTP2_HD_INFLATE_FINAL`` is nonzero and
5531 * return value is non-negative.  If that happens, all given input
5532 * data (|inlen| bytes) are processed successfully.  Then the
5533 * application must call `nghttp2_hd_inflate_end_headers()` to prepare
5534 * for the next header block input.
5535 *
5536 * In other words, if |in_final| is nonzero, and this function returns
5537 * |inlen|, you can assert that
5538 * :enum:`nghttp2_hd_inflate_final.NGHTTP2_HD_INFLATE_FINAL` is set in
5539 * |*inflate_flags|.
5540 *
5541 * The caller can feed complete compressed header block.  It also can
5542 * feed it in several chunks.  The caller must set |in_final| to
5543 * nonzero if the given input is the last block of the compressed
5544 * header.
5545 *
5546 * This function returns the number of bytes processed if it succeeds,
5547 * or one of the following negative error codes:
5548 *
5549 * :enum:`nghttp2_error.NGHTTP2_ERR_NOMEM`
5550 *     Out of memory.
5551 * :enum:`nghttp2_error.NGHTTP2_ERR_HEADER_COMP`
5552 *     Inflation process has failed.
5553 * :enum:`nghttp2_error.NGHTTP2_ERR_BUFFER_ERROR`
5554 *     The header field name or value is too large.
5555 *
5556 * Example follows::
5557 *
5558 *     int inflate_header_block(nghttp2_hd_inflater *hd_inflater,
5559 *                              uint8_t *in, size_t inlen, int final)
5560 *     {
5561 *         ssize_t rv;
5562 *
5563 *         for(;;) {
5564 *             nghttp2_nv nv;
5565 *             int inflate_flags = 0;
5566 *
5567 *             rv = nghttp2_hd_inflate_hd2(hd_inflater, &nv, &inflate_flags,
5568 *                                         in, inlen, final);
5569 *
5570 *             if(rv < 0) {
5571 *                 fprintf(stderr, "inflate failed with error code %zd", rv);
5572 *                 return -1;
5573 *             }
5574 *
5575 *             in += rv;
5576 *             inlen -= rv;
5577 *
5578 *             if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
5579 *                 fwrite(nv.name, nv.namelen, 1, stderr);
5580 *                 fprintf(stderr, ": ");
5581 *                 fwrite(nv.value, nv.valuelen, 1, stderr);
5582 *                 fprintf(stderr, "\n");
5583 *             }
5584 *             if(inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
5585 *                 nghttp2_hd_inflate_end_headers(hd_inflater);
5586 *                 break;
5587 *             }
5588 *             if((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
5589 *                inlen == 0) {
5590 *                break;
5591 *             }
5592 *         }
5593 *
5594 *         return 0;
5595 *     }
5596 *
5597 */
5598NGHTTP2_EXTERN ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
5599                                              nghttp2_nv *nv_out,
5600                                              int *inflate_flags,
5601                                              const uint8_t *in, size_t inlen,
5602                                              int in_final);
5603
5604/**
5605 * @function
5606 *
5607 * Signals the end of decompression for one header block.
5608 *
5609 * This function returns 0 if it succeeds. Currently this function
5610 * always succeeds.
5611 */
5612NGHTTP2_EXTERN int
5613nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater);
5614
5615/**
5616 * @function
5617 *
5618 * Returns the number of entries that header table of |inflater|
5619 * contains.  This is the sum of the number of static table and
5620 * dynamic table, so the return value is at least 61.
5621 */
5622NGHTTP2_EXTERN
5623size_t nghttp2_hd_inflate_get_num_table_entries(nghttp2_hd_inflater *inflater);
5624
5625/**
5626 * @function
5627 *
5628 * Returns the table entry denoted by |idx| from header table of
5629 * |inflater|.  The |idx| is 1-based, and idx=1 returns first entry of
5630 * static table.  idx=62 returns first entry of dynamic table if it
5631 * exists.  Specifying idx=0 is error, and this function returns NULL.
5632 * If |idx| is strictly greater than the number of entries the tables
5633 * contain, this function returns NULL.
5634 */
5635NGHTTP2_EXTERN
5636const nghttp2_nv *
5637nghttp2_hd_inflate_get_table_entry(nghttp2_hd_inflater *inflater, size_t idx);
5638
5639/**
5640 * @function
5641 *
5642 * Returns the used dynamic table size, including the overhead 32
5643 * bytes per entry described in RFC 7541.
5644 */
5645NGHTTP2_EXTERN
5646size_t nghttp2_hd_inflate_get_dynamic_table_size(nghttp2_hd_inflater *inflater);
5647
5648/**
5649 * @function
5650 *
5651 * Returns the maximum dynamic table size.
5652 */
5653NGHTTP2_EXTERN
5654size_t
5655nghttp2_hd_inflate_get_max_dynamic_table_size(nghttp2_hd_inflater *inflater);
5656
5657struct nghttp2_stream;
5658
5659/**
5660 * @struct
5661 *
5662 * The structure to represent HTTP/2 stream.  The details of this
5663 * structure are intentionally hidden from the public API.
5664 */
5665typedef struct nghttp2_stream nghttp2_stream;
5666
5667/**
5668 * @function
5669 *
5670 * Returns pointer to :type:`nghttp2_stream` object denoted by
5671 * |stream_id|.  If stream was not found, returns NULL.
5672 *
5673 * Returns imaginary root stream (see
5674 * `nghttp2_session_get_root_stream()`) if 0 is given in |stream_id|.
5675 *
5676 * Unless |stream_id| == 0, the returned pointer is valid until next
5677 * call of `nghttp2_session_send()`, `nghttp2_session_mem_send()`,
5678 * `nghttp2_session_recv()`, and `nghttp2_session_mem_recv()`.
5679 */
5680NGHTTP2_EXTERN nghttp2_stream *
5681nghttp2_session_find_stream(nghttp2_session *session, int32_t stream_id);
5682
5683/**
5684 * @enum
5685 *
5686 * State of stream as described in RFC 7540.
5687 */
5688typedef enum {
5689  /**
5690   * idle state.
5691   */
5692  NGHTTP2_STREAM_STATE_IDLE = 1,
5693  /**
5694   * open state.
5695   */
5696  NGHTTP2_STREAM_STATE_OPEN,
5697  /**
5698   * reserved (local) state.
5699   */
5700  NGHTTP2_STREAM_STATE_RESERVED_LOCAL,
5701  /**
5702   * reserved (remote) state.
5703   */
5704  NGHTTP2_STREAM_STATE_RESERVED_REMOTE,
5705  /**
5706   * half closed (local) state.
5707   */
5708  NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL,
5709  /**
5710   * half closed (remote) state.
5711   */
5712  NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE,
5713  /**
5714   * closed state.
5715   */
5716  NGHTTP2_STREAM_STATE_CLOSED
5717} nghttp2_stream_proto_state;
5718
5719/**
5720 * @function
5721 *
5722 * Returns state of |stream|.  The root stream retrieved by
5723 * `nghttp2_session_get_root_stream()` will have stream state
5724 * :enum:`nghttp2_stream_proto_state.NGHTTP2_STREAM_STATE_IDLE`.
5725 */
5726NGHTTP2_EXTERN nghttp2_stream_proto_state
5727nghttp2_stream_get_state(nghttp2_stream *stream);
5728
5729/**
5730 * @function
5731 *
5732 * Returns root of dependency tree, which is imaginary stream with
5733 * stream ID 0.  The returned pointer is valid until |session| is
5734 * freed by `nghttp2_session_del()`.
5735 */
5736NGHTTP2_EXTERN nghttp2_stream *
5737nghttp2_session_get_root_stream(nghttp2_session *session);
5738
5739/**
5740 * @function
5741 *
5742 * Returns the parent stream of |stream| in dependency tree.  Returns
5743 * NULL if there is no such stream.
5744 */
5745NGHTTP2_EXTERN nghttp2_stream *
5746nghttp2_stream_get_parent(nghttp2_stream *stream);
5747
5748NGHTTP2_EXTERN int32_t nghttp2_stream_get_stream_id(nghttp2_stream *stream);
5749
5750/**
5751 * @function
5752 *
5753 * Returns the next sibling stream of |stream| in dependency tree.
5754 * Returns NULL if there is no such stream.
5755 */
5756NGHTTP2_EXTERN nghttp2_stream *
5757nghttp2_stream_get_next_sibling(nghttp2_stream *stream);
5758
5759/**
5760 * @function
5761 *
5762 * Returns the previous sibling stream of |stream| in dependency tree.
5763 * Returns NULL if there is no such stream.
5764 */
5765NGHTTP2_EXTERN nghttp2_stream *
5766nghttp2_stream_get_previous_sibling(nghttp2_stream *stream);
5767
5768/**
5769 * @function
5770 *
5771 * Returns the first child stream of |stream| in dependency tree.
5772 * Returns NULL if there is no such stream.
5773 */
5774NGHTTP2_EXTERN nghttp2_stream *
5775nghttp2_stream_get_first_child(nghttp2_stream *stream);
5776
5777/**
5778 * @function
5779 *
5780 * Returns dependency weight to the parent stream of |stream|.
5781 */
5782NGHTTP2_EXTERN int32_t nghttp2_stream_get_weight(nghttp2_stream *stream);
5783
5784/**
5785 * @function
5786 *
5787 * Returns the sum of the weight for |stream|'s children.
5788 */
5789NGHTTP2_EXTERN int32_t
5790nghttp2_stream_get_sum_dependency_weight(nghttp2_stream *stream);
5791
5792/**
5793 * @functypedef
5794 *
5795 * Callback function invoked when the library outputs debug logging.
5796 * The function is called with arguments suitable for ``vfprintf(3)``
5797 *
5798 * The debug output is only enabled if the library is built with
5799 * ``DEBUGBUILD`` macro defined.
5800 */
5801typedef void (*nghttp2_debug_vprintf_callback)(const char *format,
5802                                               va_list args);
5803
5804/**
5805 * @function
5806 *
5807 * Sets a debug output callback called by the library when built with
5808 * ``DEBUGBUILD`` macro defined.  If this option is not used, debug
5809 * log is written into standard error output.
5810 *
5811 * For builds without ``DEBUGBUILD`` macro defined, this function is
5812 * noop.
5813 *
5814 * Note that building with ``DEBUGBUILD`` may cause significant
5815 * performance penalty to libnghttp2 because of extra processing.  It
5816 * should be used for debugging purpose only.
5817 *
5818 * .. Warning::
5819 *
5820 *   Building with ``DEBUGBUILD`` may cause significant performance
5821 *   penalty to libnghttp2 because of extra processing.  It should be
5822 *   used for debugging purpose only.  We write this two times because
5823 *   this is important.
5824 */
5825NGHTTP2_EXTERN void nghttp2_set_debug_vprintf_callback(
5826    nghttp2_debug_vprintf_callback debug_vprintf_callback);
5827
5828#ifdef __cplusplus
5829}
5830#endif
5831
5832#endif /* NGHTTP2_H */
5833