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