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