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