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