1 /* 2 * nghttp3 3 * 4 * Copyright (c) 2018 nghttp3 contributors 5 * Copyright (c) 2017 ngtcp2 contributors 6 * Copyright (c) 2017 nghttp2 contributors 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining 9 * a copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sublicense, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 #ifndef NGHTTP3_H 28 #define NGHTTP3_H 29 30 /* Define WIN32 when build target is Win32 API (borrowed from 31 libcurl) */ 32 #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32) 33 # define WIN32 34 #endif 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #include <stdlib.h> 41 #if defined(_MSC_VER) && (_MSC_VER < 1800) 42 /* MSVC < 2013 does not have inttypes.h because it is not C99 43 compliant. See compiler macros and version number in 44 https://sourceforge.net/p/predef/wiki/Compilers/ */ 45 # include <stdint.h> 46 #else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */ 47 # include <inttypes.h> 48 #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */ 49 #include <sys/types.h> 50 #include <stdarg.h> 51 #include <stddef.h> 52 53 #include <nghttp3/version.h> 54 55 #ifdef NGHTTP3_STATICLIB 56 # define NGHTTP3_EXTERN 57 #elif defined(WIN32) 58 # ifdef BUILDING_NGHTTP3 59 # define NGHTTP3_EXTERN __declspec(dllexport) 60 # else /* !BUILDING_NGHTTP3 */ 61 # define NGHTTP3_EXTERN __declspec(dllimport) 62 # endif /* !BUILDING_NGHTTP3 */ 63 #else /* !defined(WIN32) */ 64 # ifdef BUILDING_NGHTTP3 65 # define NGHTTP3_EXTERN __attribute__((visibility("default"))) 66 # else /* !BUILDING_NGHTTP3 */ 67 # define NGHTTP3_EXTERN 68 # endif /* !BUILDING_NGHTTP3 */ 69 #endif /* !defined(WIN32) */ 70 71 /** 72 * @typedef 73 * 74 * :type:`nghttp3_ssize` is signed counterpart of size_t. 75 */ 76 typedef ptrdiff_t nghttp3_ssize; 77 78 /** 79 * @macro 80 * 81 * :macro:`NGHTTP3_ALPN_H3` is a serialized form of HTTP/3 ALPN 82 * protocol identifier this library supports. Notice that the first 83 * byte is the length of the following protocol identifier. 84 */ 85 #define NGHTTP3_ALPN_H3 "\x2h3" 86 87 /** 88 * @macrosection 89 * 90 * nghttp3 library error codes 91 */ 92 93 /** 94 * @macro 95 * 96 * :macro:`NGHTTP3_ERR_INVALID_ARGUMENT` indicates that a passed 97 * argument is invalid. 98 */ 99 #define NGHTTP3_ERR_INVALID_ARGUMENT -101 100 /** 101 * @macro 102 * 103 * :macro:`NGHTTP3_ERR_NOBUF` indicates that a provided buffer does 104 * not have enough space to store data. 105 */ 106 #define NGHTTP3_ERR_NOBUF -102 107 /** 108 * @macro 109 * 110 * :macro:`NGHTTP3_ERR_INVALID_STATE` indicates that a requested 111 * operation is not allowed at the current connection state. 112 */ 113 #define NGHTTP3_ERR_INVALID_STATE -103 114 /** 115 * @macro 116 * 117 * :macro:`NGHTTP3_ERR_WOULDBLOCK` indicates that an operation might 118 * block. 119 */ 120 #define NGHTTP3_ERR_WOULDBLOCK -104 121 /** 122 * @macro 123 * 124 * :macro:`NGHTTP3_ERR_STREAM_IN_USE` indicates that a stream ID is 125 * already in use. 126 */ 127 #define NGHTTP3_ERR_STREAM_IN_USE -105 128 /** 129 * @macro 130 * 131 * :macro:`NGHTTP3_ERR_MALFORMED_HTTP_HEADER` indicates that an HTTP 132 * header field is malformed. 133 */ 134 #define NGHTTP3_ERR_MALFORMED_HTTP_HEADER -107 135 /** 136 * @macro 137 * 138 * :macro:`NGHTTP3_ERR_REMOVE_HTTP_HEADER` indicates that an HTTP 139 * header field is discarded. 140 */ 141 #define NGHTTP3_ERR_REMOVE_HTTP_HEADER -108 142 /** 143 * @macro 144 * 145 * :macro:`NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING` indicates that HTTP 146 * messaging is malformed. 147 */ 148 #define NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING -109 149 /** 150 * @macro 151 * 152 * :macro:`NGHTTP3_ERR_QPACK_FATAL` indicates that a fatal error is 153 * occurred during QPACK processing and it cannot be recoverable. 154 */ 155 #define NGHTTP3_ERR_QPACK_FATAL -111 156 /** 157 * @macro 158 * 159 * :macro:`NGHTTP3_ERR_QPACK_HEADER_TOO_LARGE` indicates that a header 160 * field is too large to process. 161 */ 162 #define NGHTTP3_ERR_QPACK_HEADER_TOO_LARGE -112 163 /** 164 * @macro 165 * 166 * :macro:`NGHTTP3_ERR_STREAM_NOT_FOUND` indicates that a stream is 167 * not found. 168 */ 169 #define NGHTTP3_ERR_STREAM_NOT_FOUND -114 170 /** 171 * @macro 172 * 173 * :macro:`NGHTTP3_ERR_CONN_CLOSING` indicates that a connection is 174 * closing state. 175 */ 176 #define NGHTTP3_ERR_CONN_CLOSING -116 177 /** 178 * @macro 179 * 180 * :macro:`NGHTTP3_ERR_STREAM_DATA_OVERFLOW` indicates that the length 181 * of stream data is too long and causes overflow. 182 */ 183 #define NGHTTP3_ERR_STREAM_DATA_OVERFLOW -117 184 /** 185 * @macro 186 * 187 * :macro:`NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED` indicates that a 188 * QPACK decompression failed. 189 */ 190 #define NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED -402 191 /** 192 * @macro 193 * 194 * :macro:`NGHTTP3_ERR_QPACK_ENCODER_STREAM_ERROR` indicates that an 195 * error occurred while reading QPACK encoder stream. 196 */ 197 #define NGHTTP3_ERR_QPACK_ENCODER_STREAM_ERROR -403 198 /** 199 * @macro 200 * 201 * :macro:`NGHTTP3_ERR_QPACK_DECODER_STREAM_ERROR` indicates that an 202 * error occurred while reading QPACK decoder stream. 203 */ 204 #define NGHTTP3_ERR_QPACK_DECODER_STREAM_ERROR -404 205 /** 206 * @macro 207 * 208 * :macro:`NGHTTP3_ERR_H3_FRAME_UNEXPECTED` indicates that an 209 * unexpected HTTP/3 frame is received. 210 */ 211 #define NGHTTP3_ERR_H3_FRAME_UNEXPECTED -408 212 /** 213 * @macro 214 * 215 * :macro:`NGHTTP3_ERR_H3_FRAME_ERROR` indicates that an HTTP/3 frame 216 * is malformed. 217 */ 218 #define NGHTTP3_ERR_H3_FRAME_ERROR -409 219 /** 220 * @macro 221 * 222 * :macro:`NGHTTP3_ERR_H3_MISSING_SETTINGS` indicates that an HTTP/3 223 * SETTINGS frame is missing. 224 */ 225 #define NGHTTP3_ERR_H3_MISSING_SETTINGS -665 226 /** 227 * @macro 228 * 229 * :macro:`NGHTTP3_ERR_H3_INTERNAL_ERROR` indicates an internal error. 230 */ 231 #define NGHTTP3_ERR_H3_INTERNAL_ERROR -667 232 /** 233 * @macro 234 * 235 * :macro:`NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM` indicates that a 236 * critical stream is closed. 237 */ 238 #define NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM -668 239 /** 240 * @macro 241 * 242 * :macro:`NGHTTP3_ERR_H3_GENERAL_PROTOCOL_ERROR` indicates a general 243 * protocol error. This is typically a catch-all error. 244 */ 245 #define NGHTTP3_ERR_H3_GENERAL_PROTOCOL_ERROR -669 246 /** 247 * @macro 248 * 249 * :macro:`NGHTTP3_ERR_H3_ID_ERROR` indicates that an ID related error 250 * occurred. 251 */ 252 #define NGHTTP3_ERR_H3_ID_ERROR -670 253 /** 254 * @macro 255 * 256 * :macro:`NGHTTP3_ERR_H3_SETTINGS_ERROR` indicates that an HTTP/3 257 * SETTINGS frame is malformed. 258 */ 259 #define NGHTTP3_ERR_H3_SETTINGS_ERROR -671 260 /** 261 * @macro 262 * 263 * :macro:`NGHTTP3_ERR_H3_STREAM_CREATION_ERROR` indicates that a 264 * remote endpoint attempts to create a new stream which is not 265 * allowed. 266 */ 267 #define NGHTTP3_ERR_H3_STREAM_CREATION_ERROR -672 268 /** 269 * @macro 270 * 271 * :macro:`NGHTTP3_ERR_FATAL` indicates that error codes less than 272 * this value is fatal error. When this error is returned, an 273 * endpoint should drop connection immediately. 274 */ 275 #define NGHTTP3_ERR_FATAL -900 276 /** 277 * @macro 278 * 279 * :macro:`NGHTTP3_ERR_NOMEM` indicates out of memory. 280 */ 281 #define NGHTTP3_ERR_NOMEM -901 282 /** 283 * @macro 284 * 285 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` indicates that user defined 286 * callback function failed. 287 */ 288 #define NGHTTP3_ERR_CALLBACK_FAILURE -902 289 290 /** 291 * @macrosection 292 * 293 * HTTP/3 application error code 294 */ 295 296 /** 297 * @macro 298 * 299 * :macro:`NGHTTP3_H3_NO_ERROR` is HTTP/3 application error code 300 * ``H3_NO_ERROR``. 301 */ 302 #define NGHTTP3_H3_NO_ERROR 0x0100 303 /** 304 * @macro 305 * 306 * :macro:`NGHTTP3_H3_GENERAL_PROTOCOL_ERROR` is HTTP/3 application 307 * error code ``H3_GENERAL_PROTOCOL_ERROR``. 308 */ 309 #define NGHTTP3_H3_GENERAL_PROTOCOL_ERROR 0x0101 310 /** 311 * @macro 312 * 313 * :macro:`NGHTTP3_H3_INTERNAL_ERROR` is HTTP/3 application error code 314 * ``H3_INTERNAL_ERROR``. 315 */ 316 #define NGHTTP3_H3_INTERNAL_ERROR 0x0102 317 /** 318 * @macro 319 * 320 * :macro:`NGHTTP3_H3_STREAM_CREATION_ERROR` is HTTP/3 application 321 * error code ``H3_STREAM_CREATION_ERROR``. 322 */ 323 #define NGHTTP3_H3_STREAM_CREATION_ERROR 0x0103 324 /** 325 * @macro 326 * 327 * :macro:`NGHTTP3_H3_CLOSED_CRITICAL_STREAM` is HTTP/3 application 328 * error code ``H3_CLOSED_CRITICAL_STREAM``. 329 */ 330 #define NGHTTP3_H3_CLOSED_CRITICAL_STREAM 0x0104 331 /** 332 * @macro 333 * 334 * :macro:`NGHTTP3_H3_FRAME_UNEXPECTED` is HTTP/3 application error 335 * code ``H3_FRAME_UNEXPECTED``. 336 */ 337 #define NGHTTP3_H3_FRAME_UNEXPECTED 0x0105 338 /** 339 * @macro 340 * 341 * :macro:`NGHTTP3_H3_FRAME_ERROR` is HTTP/3 application error code 342 * ``H3_FRAME_ERROR``. 343 */ 344 #define NGHTTP3_H3_FRAME_ERROR 0x0106 345 /** 346 * @macro 347 * 348 * :macro:`NGHTTP3_H3_EXCESSIVE_LOAD` is HTTP/3 application error code 349 * ``H3_EXCESSIVE_LOAD``. 350 */ 351 #define NGHTTP3_H3_EXCESSIVE_LOAD 0x0107 352 /** 353 * @macro 354 * 355 * :macro:`NGHTTP3_H3_ID_ERROR` is HTTP/3 application error code 356 * ``H3_ID_ERROR``. 357 */ 358 #define NGHTTP3_H3_ID_ERROR 0x0108 359 /** 360 * @macro 361 * 362 * :macro:`NGHTTP3_H3_SETTINGS_ERROR` is HTTP/3 application error code 363 * ``H3_SETTINGS_ERROR``. 364 */ 365 #define NGHTTP3_H3_SETTINGS_ERROR 0x0109 366 /** 367 * @macro 368 * 369 * :macro:`NGHTTP3_H3_MISSING_SETTINGS` is HTTP/3 application error 370 * code ``H3_MISSING_SETTINGS``. 371 */ 372 #define NGHTTP3_H3_MISSING_SETTINGS 0x010a 373 /** 374 * @macro 375 * 376 * :macro:`NGHTTP3_H3_REQUEST_REJECTED` is HTTP/3 application error 377 * code ``H3_REQUEST_REJECTED``. 378 */ 379 #define NGHTTP3_H3_REQUEST_REJECTED 0x010b 380 /** 381 * @macro 382 * 383 * :macro:`NGHTTP3_H3_REQUEST_CANCELLED` is HTTP/3 application error 384 * code ``H3_REQUEST_CANCELLED``. 385 */ 386 #define NGHTTP3_H3_REQUEST_CANCELLED 0x010c 387 /** 388 * @macro 389 * 390 * :macro:`NGHTTP3_H3_REQUEST_INCOMPLETE` is HTTP/3 application error 391 * code ``H3_REQUEST_INCOMPLETE``. 392 */ 393 #define NGHTTP3_H3_REQUEST_INCOMPLETE 0x010d 394 /** 395 * @macro 396 * 397 * :macro:`NGHTTP3_H3_MESSAGE_ERROR` is HTTP/3 application error code 398 * ``H3_MESSAGE_ERROR``. 399 */ 400 #define NGHTTP3_H3_MESSAGE_ERROR 0x010e 401 /** 402 * @macro 403 * 404 * :macro:`NGHTTP3_H3_CONNECT_ERROR` is HTTP/3 application error code 405 * ``H3_CONNECT_ERROR``. 406 */ 407 #define NGHTTP3_H3_CONNECT_ERROR 0x010f 408 /** 409 * @macro 410 * 411 * :macro:`NGHTTP3_H3_VERSION_FALLBACK` is HTTP/3 application error 412 * code ``H3_VERSION_FALLBACK``. 413 */ 414 #define NGHTTP3_H3_VERSION_FALLBACK 0x0110 415 /** 416 * @macro 417 * 418 * :macro:`NGHTTP3_QPACK_DECOMPRESSION_FAILED` is HTTP/3 application 419 * error code ``QPACK_DECOMPRESSION_FAILED``. 420 */ 421 #define NGHTTP3_QPACK_DECOMPRESSION_FAILED 0x0200 422 /** 423 * @macro 424 * 425 * :macro:`NGHTTP3_QPACK_ENCODER_STREAM_ERROR` is HTTP/3 application 426 * error code ``QPACK_ENCODER_STREAM_ERROR``. 427 */ 428 #define NGHTTP3_QPACK_ENCODER_STREAM_ERROR 0x0201 429 /** 430 * @macro 431 * 432 * :macro:`NGHTTP3_QPACK_DECODER_STREAM_ERROR` is HTTP/3 application 433 * error code ``QPACK_DECODER_STREAM_ERROR``. 434 */ 435 #define NGHTTP3_QPACK_DECODER_STREAM_ERROR 0x0202 436 437 /** 438 * @functypedef 439 * 440 * :type:`nghttp3_malloc` is a custom memory allocator to replace 441 * :manpage:`malloc(3)`. The |user_data| is the 442 * :member:`nghttp3_mem.user_data`. 443 */ 444 typedef void *(*nghttp3_malloc)(size_t size, void *user_data); 445 446 /** 447 * @functypedef 448 * 449 * :type:`nghttp3_free` is a custom memory allocator to replace 450 * :manpage:`free(3)`. The |user_data| is the 451 * :member:`nghttp3_mem.user_data`. 452 */ 453 typedef void (*nghttp3_free)(void *ptr, void *user_data); 454 455 /** 456 * @functypedef 457 * 458 * :type:`nghttp3_calloc` is a custom memory allocator to replace 459 * :manpage:`calloc(3)`. The |user_data| is the 460 * :member:`nghttp3_mem.user_data`. 461 */ 462 typedef void *(*nghttp3_calloc)(size_t nmemb, size_t size, void *user_data); 463 464 /** 465 * @functypedef 466 * 467 * :type:`nghttp3_realloc` is a custom memory allocator to replace 468 * :manpage:`realloc(3)`. The |user_data| is the 469 * :member:`nghttp3_mem.user_data`. 470 */ 471 typedef void *(*nghttp3_realloc)(void *ptr, size_t size, void *user_data); 472 473 /** 474 * @struct 475 * 476 * :type:`nghttp3_mem` is a custom memory allocator functions and user 477 * defined pointer. The :member:`user_data` field is passed to each 478 * allocator function. This can be used, for example, to achieve 479 * per-session memory pool. 480 * 481 * In the following example code, ``my_malloc``, ``my_free``, 482 * ``my_calloc`` and ``my_realloc`` are the replacement of the 483 * standard allocators :manpage:`malloc(3)`, :manpage:`free(3)`, 484 * :manpage:`calloc(3)` and :manpage:`realloc(3)` respectively:: 485 * 486 * void *my_malloc_cb(size_t size, void *user_data) { 487 * (void)user_data; 488 * return my_malloc(size); 489 * } 490 * 491 * void my_free_cb(void *ptr, void *user_data) { 492 * (void)user_data; 493 * my_free(ptr); 494 * } 495 * 496 * void *my_calloc_cb(size_t nmemb, size_t size, void *user_data) { 497 * (void)user_data; 498 * return my_calloc(nmemb, size); 499 * } 500 * 501 * void *my_realloc_cb(void *ptr, size_t size, void *user_data) { 502 * (void)user_data; 503 * return my_realloc(ptr, size); 504 * } 505 * 506 * void conn_new() { 507 * nghttp3_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb, 508 * my_realloc_cb}; 509 * 510 * ... 511 * } 512 */ 513 typedef struct nghttp3_mem { 514 /** 515 * :member:`user_data` is an arbitrary user supplied data. This 516 * is passed to each allocator function. 517 */ 518 void *user_data; 519 /** 520 * :member:`malloc` is a custom allocator function to replace 521 * :manpage:`malloc(3)`. 522 */ 523 nghttp3_malloc malloc; 524 /** 525 * :member:`free` is a custom allocator function to replace 526 * :manpage:`free(3)`. 527 */ 528 nghttp3_free free; 529 /** 530 * :member:`calloc` is a custom allocator function to replace 531 * :manpage:`calloc(3)`. 532 */ 533 nghttp3_calloc calloc; 534 /** 535 * :member:`realloc` is a custom allocator function to replace 536 * :manpage:`realloc(3)`. 537 */ 538 nghttp3_realloc realloc; 539 } nghttp3_mem; 540 541 /** 542 * @function 543 * 544 * `nghttp3_mem_default` returns the default memory allocator which 545 * uses malloc/calloc/realloc/free. 546 */ 547 NGHTTP3_EXTERN const nghttp3_mem *nghttp3_mem_default(void); 548 549 /** 550 * @struct 551 * 552 * :type:`nghttp3_vec` is ``struct iovec`` compatible structure to 553 * reference arbitrary array of bytes. 554 */ 555 typedef struct nghttp3_vec { 556 /** 557 * :member:`base` points to the data. 558 */ 559 uint8_t *base; 560 /** 561 * :member:`len` is the number of bytes which the buffer pointed by 562 * base contains. 563 */ 564 size_t len; 565 } nghttp3_vec; 566 567 /** 568 * @struct 569 * 570 * :type:`nghttp3_rcbuf` is the object representing reference counted 571 * buffer. The details of this structure are intentionally hidden 572 * from the public API. 573 */ 574 typedef struct nghttp3_rcbuf nghttp3_rcbuf; 575 576 /** 577 * @function 578 * 579 * `nghttp3_rcbuf_incref` increments the reference count of |rcbuf| by 580 * 1. 581 */ 582 NGHTTP3_EXTERN void nghttp3_rcbuf_incref(nghttp3_rcbuf *rcbuf); 583 584 /** 585 * @function 586 * 587 * `nghttp3_rcbuf_decref` decrements the reference count of |rcbuf| by 588 * 1. If the reference count becomes zero, the object pointed by 589 * |rcbuf| will be freed. In this case, application must not use 590 * |rcbuf| again. 591 */ 592 NGHTTP3_EXTERN void nghttp3_rcbuf_decref(nghttp3_rcbuf *rcbuf); 593 594 /** 595 * @function 596 * 597 * `nghttp3_rcbuf_get_buf` returns the underlying buffer managed by 598 * |rcbuf|. 599 */ 600 NGHTTP3_EXTERN nghttp3_vec nghttp3_rcbuf_get_buf(const nghttp3_rcbuf *rcbuf); 601 602 /** 603 * @function 604 * 605 * `nghttp3_rcbuf_is_static` returns nonzero if the underlying buffer 606 * is statically allocated, and 0 otherwise. This can be useful for 607 * language bindings that wish to avoid creating duplicate strings for 608 * these buffers. 609 */ 610 NGHTTP3_EXTERN int nghttp3_rcbuf_is_static(const nghttp3_rcbuf *rcbuf); 611 612 /** 613 * @struct 614 * 615 * :type:`nghttp3_buf` is the variable size buffer. 616 */ 617 typedef struct nghttp3_buf { 618 /** 619 * :member:`begin` points to the beginning of the buffer. 620 */ 621 uint8_t *begin; 622 /** 623 * :member:`end` points to the one beyond of the last byte of the 624 * buffer 625 */ 626 uint8_t *end; 627 /** 628 * :member:`pos` pointers to the start of data. Typically, this 629 * points to the point that next data should be read. Initially, it 630 * points to :member:`begin`. 631 */ 632 uint8_t *pos; 633 /** 634 * :member:`last` points to the one beyond of the last data of the 635 * buffer. Typically, new data is written at this point. 636 * Initially, it points to :member:`begin`. 637 */ 638 uint8_t *last; 639 } nghttp3_buf; 640 641 /** 642 * @function 643 * 644 * `nghttp3_buf_init` initializes empty |buf|. 645 */ 646 NGHTTP3_EXTERN void nghttp3_buf_init(nghttp3_buf *buf); 647 648 /** 649 * @function 650 * 651 * `nghttp3_buf_free` frees resources allocated for |buf| using |mem| 652 * as memory allocator. :member:`buf->begin <nghttp3_buf.begin>` must 653 * be a heap buffer allocated by |mem|. 654 */ 655 NGHTTP3_EXTERN void nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem); 656 657 /** 658 * @function 659 * 660 * `nghttp3_buf_left` returns the number of additional bytes which can 661 * be written to the underlying buffer. In other words, it returns 662 * :member:`buf->end <nghttp3_buf.end>` - :member:`buf->last 663 * <nghttp3_buf.last>`. 664 */ 665 NGHTTP3_EXTERN size_t nghttp3_buf_left(const nghttp3_buf *buf); 666 667 /** 668 * @function 669 * 670 * `nghttp3_buf_len` returns the number of bytes left to read. In 671 * other words, it returns :member:`buf->last <nghttp3_buf.last>` - 672 * :member:`buf->pos <nghttp3_buf.pos>`. 673 */ 674 NGHTTP3_EXTERN size_t nghttp3_buf_len(const nghttp3_buf *buf); 675 676 /** 677 * @function 678 * 679 * `nghttp3_buf_reset` sets :member:`buf->pos <nghttp3_buf.pos>` and 680 * :member:`buf->last <nghttp3_buf.last>` to :member:`buf->begin 681 * <nghttp3_buf.begin>`. 682 */ 683 NGHTTP3_EXTERN void nghttp3_buf_reset(nghttp3_buf *buf); 684 685 /** 686 * @macrosection 687 * 688 * Flags for header field name/value pair 689 */ 690 691 /** 692 * @macro 693 * 694 * :macro:`NGHTTP3_NV_FLAG_NONE` indicates no flag set. 695 */ 696 #define NGHTTP3_NV_FLAG_NONE 0x00u 697 698 /** 699 * @macro 700 * 701 * :macro:`NGHTTP3_NV_FLAG_NEVER_INDEX` indicates that this name/value 702 * pair must not be indexed. Other implementation calls this bit as 703 * "sensitive". 704 */ 705 #define NGHTTP3_NV_FLAG_NEVER_INDEX 0x01u 706 707 /** 708 * @macro 709 * 710 * :macro:`NGHTTP3_NV_FLAG_NO_COPY_NAME` is set solely by application. 711 * If this flag is set, the library does not make a copy of header 712 * field name. This could improve performance. 713 */ 714 #define NGHTTP3_NV_FLAG_NO_COPY_NAME 0x02u 715 716 /** 717 * @macro 718 * 719 * :macro:`NGHTTP3_NV_FLAG_NO_COPY_VALUE` is set solely by 720 * application. If this flag is set, the library does not make a copy 721 * of header field value. This could improve performance. 722 */ 723 #define NGHTTP3_NV_FLAG_NO_COPY_VALUE 0x04u 724 725 /** 726 * @struct 727 * 728 * :type:`nghttp3_nv` is the name/value pair, which mainly used to 729 * represent header fields. 730 */ 731 typedef struct nghttp3_nv { 732 /** 733 * :member:`name` is the header field name. 734 */ 735 uint8_t *name; 736 /** 737 * :member:`value` is the header field value. 738 */ 739 uint8_t *value; 740 /** 741 * :member:`namelen` is the length of the |name|, excluding 742 * terminating NULL. 743 */ 744 size_t namelen; 745 /** 746 * :member:`valuelen` is the length of the |value|, excluding 747 * terminating NULL. 748 */ 749 size_t valuelen; 750 /** 751 * :member:`flags` is bitwise OR of one or more of 752 * :macro:`NGHTTP3_NV_FLAG_* <NGHTTP3_NV_FLAG_NONE>`. 753 */ 754 uint8_t flags; 755 } nghttp3_nv; 756 757 /* Generated by mkstatichdtbl.py */ 758 /** 759 * @enum 760 * 761 * :type:`nghttp3_qpack_token` defines HTTP header field name tokens 762 * to identify field name quickly. It appears in 763 * :member:`nghttp3_qpack_nv.token`. 764 */ 765 typedef enum nghttp3_qpack_token { 766 /** 767 * :enum:`NGHTTP3_QPACK_TOKEN__AUTHORITY` is a token for 768 * ``:authority``. 769 */ 770 NGHTTP3_QPACK_TOKEN__AUTHORITY = 0, 771 /** 772 * :enum:`NGHTTP3_QPACK_TOKEN__PATH` is a token for ``:path``. 773 */ 774 NGHTTP3_QPACK_TOKEN__PATH = 8, 775 /** 776 * :enum:`NGHTTP3_QPACK_TOKEN_AGE` is a token for ``age``. 777 */ 778 NGHTTP3_QPACK_TOKEN_AGE = 43, 779 /** 780 * :enum:`NGHTTP3_QPACK_TOKEN_CONTENT_DISPOSITION` is a token for 781 * ``content-disposition``. 782 */ 783 NGHTTP3_QPACK_TOKEN_CONTENT_DISPOSITION = 52, 784 /** 785 * :enum:`NGHTTP3_QPACK_TOKEN_CONTENT_LENGTH` is a token for 786 * ``content-length``. 787 */ 788 NGHTTP3_QPACK_TOKEN_CONTENT_LENGTH = 55, 789 /** 790 * :enum:`NGHTTP3_QPACK_TOKEN_COOKIE` is a token for ``cookie``. 791 */ 792 NGHTTP3_QPACK_TOKEN_COOKIE = 68, 793 /** 794 * :enum:`NGHTTP3_QPACK_TOKEN_DATE` is a token for ``date``. 795 */ 796 NGHTTP3_QPACK_TOKEN_DATE = 69, 797 /** 798 * :enum:`NGHTTP3_QPACK_TOKEN_ETAG` is a token for ``etag``. 799 */ 800 NGHTTP3_QPACK_TOKEN_ETAG = 71, 801 /** 802 * :enum:`NGHTTP3_QPACK_TOKEN_IF_MODIFIED_SINCE` is a token for 803 * ``if-modified-since``. 804 */ 805 NGHTTP3_QPACK_TOKEN_IF_MODIFIED_SINCE = 74, 806 /** 807 * :enum:`NGHTTP3_QPACK_TOKEN_IF_NONE_MATCH` is a token for 808 * ``if-none-match``. 809 */ 810 NGHTTP3_QPACK_TOKEN_IF_NONE_MATCH = 75, 811 /** 812 * :enum:`NGHTTP3_QPACK_TOKEN_LAST_MODIFIED` is a token for 813 * ``last-modified``. 814 */ 815 NGHTTP3_QPACK_TOKEN_LAST_MODIFIED = 77, 816 /** 817 * :enum:`NGHTTP3_QPACK_TOKEN_LINK` is a token for ``link``. 818 */ 819 NGHTTP3_QPACK_TOKEN_LINK = 78, 820 /** 821 * :enum:`NGHTTP3_QPACK_TOKEN_LOCATION` is a token for ``location``. 822 */ 823 NGHTTP3_QPACK_TOKEN_LOCATION = 79, 824 /** 825 * :enum:`NGHTTP3_QPACK_TOKEN_REFERER` is a token for ``referer``. 826 */ 827 NGHTTP3_QPACK_TOKEN_REFERER = 83, 828 /** 829 * :enum:`NGHTTP3_QPACK_TOKEN_SET_COOKIE` is a token for 830 * ``set-cookie``. 831 */ 832 NGHTTP3_QPACK_TOKEN_SET_COOKIE = 85, 833 /** 834 * :enum:`NGHTTP3_QPACK_TOKEN__METHOD` is a token for ``:method``. 835 */ 836 NGHTTP3_QPACK_TOKEN__METHOD = 1, 837 /** 838 * :enum:`NGHTTP3_QPACK_TOKEN__SCHEME` is a token for ``:scheme``. 839 */ 840 NGHTTP3_QPACK_TOKEN__SCHEME = 9, 841 /** 842 * :enum:`NGHTTP3_QPACK_TOKEN__STATUS` is a token for ``:status``. 843 */ 844 NGHTTP3_QPACK_TOKEN__STATUS = 11, 845 /** 846 * :enum:`NGHTTP3_QPACK_TOKEN_ACCEPT` is a token for ``accept``. 847 */ 848 NGHTTP3_QPACK_TOKEN_ACCEPT = 25, 849 /** 850 * :enum:`NGHTTP3_QPACK_TOKEN_ACCEPT_ENCODING` is a token for 851 * ``accept-encoding``. 852 */ 853 NGHTTP3_QPACK_TOKEN_ACCEPT_ENCODING = 27, 854 /** 855 * :enum:`NGHTTP3_QPACK_TOKEN_ACCEPT_RANGES` is a token for 856 * ``accept-ranges``. 857 */ 858 NGHTTP3_QPACK_TOKEN_ACCEPT_RANGES = 29, 859 /** 860 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_HEADERS` is a 861 * token for ``access-control-allow-headers``. 862 */ 863 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_HEADERS = 32, 864 /** 865 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN` is a 866 * token for ``access-control-allow-origin``. 867 */ 868 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN = 38, 869 /** 870 * :enum:`NGHTTP3_QPACK_TOKEN_CACHE_CONTROL` is a token for 871 * ``cache-control``. 872 */ 873 NGHTTP3_QPACK_TOKEN_CACHE_CONTROL = 46, 874 /** 875 * :enum:`NGHTTP3_QPACK_TOKEN_CONTENT_ENCODING` is a token for 876 * ``content-encoding``. 877 */ 878 NGHTTP3_QPACK_TOKEN_CONTENT_ENCODING = 53, 879 /** 880 * :enum:`NGHTTP3_QPACK_TOKEN_CONTENT_TYPE` is a token for 881 * ``content-type``. 882 */ 883 NGHTTP3_QPACK_TOKEN_CONTENT_TYPE = 57, 884 /** 885 * :enum:`NGHTTP3_QPACK_TOKEN_RANGE` is a token for ``range``. 886 */ 887 NGHTTP3_QPACK_TOKEN_RANGE = 82, 888 /** 889 * :enum:`NGHTTP3_QPACK_TOKEN_STRICT_TRANSPORT_SECURITY` is a token 890 * for ``strict-transport-security``. 891 */ 892 NGHTTP3_QPACK_TOKEN_STRICT_TRANSPORT_SECURITY = 86, 893 /** 894 * :enum:`NGHTTP3_QPACK_TOKEN_VARY` is a token for ``vary``. 895 */ 896 NGHTTP3_QPACK_TOKEN_VARY = 92, 897 /** 898 * :enum:`NGHTTP3_QPACK_TOKEN_X_CONTENT_TYPE_OPTIONS` is a token for 899 * ``x-content-type-options``. 900 */ 901 NGHTTP3_QPACK_TOKEN_X_CONTENT_TYPE_OPTIONS = 94, 902 /** 903 * :enum:`NGHTTP3_QPACK_TOKEN_X_XSS_PROTECTION` is a token for 904 * ``x-xss-protection``. 905 */ 906 NGHTTP3_QPACK_TOKEN_X_XSS_PROTECTION = 98, 907 /** 908 * :enum:`NGHTTP3_QPACK_TOKEN_ACCEPT_LANGUAGE` is a token for 909 * ``accept-language``. 910 */ 911 NGHTTP3_QPACK_TOKEN_ACCEPT_LANGUAGE = 28, 912 /** 913 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_CREDENTIALS` is a 914 * token for ``access-control-allow-credentials``. 915 */ 916 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_CREDENTIALS = 30, 917 /** 918 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_METHODS` is a 919 * token for ``access-control-allow-methods``. 920 */ 921 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_ALLOW_METHODS = 35, 922 /** 923 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_EXPOSE_HEADERS` is a 924 * token for ``access-control-expose-headers``. 925 */ 926 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_EXPOSE_HEADERS = 39, 927 /** 928 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_REQUEST_HEADERS` is a 929 * token for ``access-control-request-headers``. 930 */ 931 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_REQUEST_HEADERS = 40, 932 /** 933 * :enum:`NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_REQUEST_METHOD` is a 934 * token for ``access-control-request-method``. 935 */ 936 NGHTTP3_QPACK_TOKEN_ACCESS_CONTROL_REQUEST_METHOD = 41, 937 /** 938 * :enum:`NGHTTP3_QPACK_TOKEN_ALT_SVC` is a token for ``alt-svc``. 939 */ 940 NGHTTP3_QPACK_TOKEN_ALT_SVC = 44, 941 /** 942 * :enum:`NGHTTP3_QPACK_TOKEN_AUTHORIZATION` is a token for 943 * ``authorization``. 944 */ 945 NGHTTP3_QPACK_TOKEN_AUTHORIZATION = 45, 946 /** 947 * :enum:`NGHTTP3_QPACK_TOKEN_CONTENT_SECURITY_POLICY` is a token 948 * for ``content-security-policy``. 949 */ 950 NGHTTP3_QPACK_TOKEN_CONTENT_SECURITY_POLICY = 56, 951 /** 952 * :enum:`NGHTTP3_QPACK_TOKEN_EARLY_DATA` is a token for 953 * ``early-data``. 954 */ 955 NGHTTP3_QPACK_TOKEN_EARLY_DATA = 70, 956 /** 957 * :enum:`NGHTTP3_QPACK_TOKEN_EXPECT_CT` is a token for 958 * ``expect-ct``. 959 */ 960 NGHTTP3_QPACK_TOKEN_EXPECT_CT = 72, 961 /** 962 * :enum:`NGHTTP3_QPACK_TOKEN_FORWARDED` is a token for 963 * ``forwarded``. 964 */ 965 NGHTTP3_QPACK_TOKEN_FORWARDED = 73, 966 /** 967 * :enum:`NGHTTP3_QPACK_TOKEN_IF_RANGE` is a token for ``if-range``. 968 */ 969 NGHTTP3_QPACK_TOKEN_IF_RANGE = 76, 970 /** 971 * :enum:`NGHTTP3_QPACK_TOKEN_ORIGIN` is a token for ``origin``. 972 */ 973 NGHTTP3_QPACK_TOKEN_ORIGIN = 80, 974 /** 975 * :enum:`NGHTTP3_QPACK_TOKEN_PURPOSE` is a token for ``purpose``. 976 */ 977 NGHTTP3_QPACK_TOKEN_PURPOSE = 81, 978 /** 979 * :enum:`NGHTTP3_QPACK_TOKEN_SERVER` is a token for ``server``. 980 */ 981 NGHTTP3_QPACK_TOKEN_SERVER = 84, 982 /** 983 * :enum:`NGHTTP3_QPACK_TOKEN_TIMING_ALLOW_ORIGIN` is a token for 984 * ``timing-allow-origin``. 985 */ 986 NGHTTP3_QPACK_TOKEN_TIMING_ALLOW_ORIGIN = 89, 987 /** 988 * :enum:`NGHTTP3_QPACK_TOKEN_UPGRADE_INSECURE_REQUESTS` is a token 989 * for ``upgrade-insecure-requests``. 990 */ 991 NGHTTP3_QPACK_TOKEN_UPGRADE_INSECURE_REQUESTS = 90, 992 /** 993 * :enum:`NGHTTP3_QPACK_TOKEN_USER_AGENT` is a token for 994 * ``user-agent``. 995 */ 996 NGHTTP3_QPACK_TOKEN_USER_AGENT = 91, 997 /** 998 * :enum:`NGHTTP3_QPACK_TOKEN_X_FORWARDED_FOR` is a token for 999 * ``x-forwarded-for``. 1000 */ 1001 NGHTTP3_QPACK_TOKEN_X_FORWARDED_FOR = 95, 1002 /** 1003 * :enum:`NGHTTP3_QPACK_TOKEN_X_FRAME_OPTIONS` is a token for 1004 * ``x-frame-options``. 1005 */ 1006 NGHTTP3_QPACK_TOKEN_X_FRAME_OPTIONS = 96, 1007 1008 /* Additional header fields for HTTP messaging validation */ 1009 1010 /** 1011 * :enum:`NGHTTP3_QPACK_TOKEN_HOST` is a token for ``host``. 1012 */ 1013 NGHTTP3_QPACK_TOKEN_HOST = 1000, 1014 /** 1015 * :enum:`NGHTTP3_QPACK_TOKEN_CONNECTION` is a token for 1016 * ``connection``. 1017 */ 1018 NGHTTP3_QPACK_TOKEN_CONNECTION, 1019 /** 1020 * :enum:`NGHTTP3_QPACK_TOKEN_KEEP_ALIVE` is a token for 1021 * ``keep-alive``. 1022 */ 1023 NGHTTP3_QPACK_TOKEN_KEEP_ALIVE, 1024 /** 1025 * :enum:`NGHTTP3_QPACK_TOKEN_PROXY_CONNECTION` is a token for 1026 * ``proxy-connection``. 1027 */ 1028 NGHTTP3_QPACK_TOKEN_PROXY_CONNECTION, 1029 /** 1030 * :enum:`NGHTTP3_QPACK_TOKEN_TRANSFER_ENCODING` is a token for 1031 * ``transfer-encoding``. 1032 */ 1033 NGHTTP3_QPACK_TOKEN_TRANSFER_ENCODING, 1034 /** 1035 * :enum:`NGHTTP3_QPACK_TOKEN_UPGRADE` is a token for ``upgrade``. 1036 */ 1037 NGHTTP3_QPACK_TOKEN_UPGRADE, 1038 /** 1039 * :enum:`NGHTTP3_QPACK_TOKEN_TE` is a token for ``te``. 1040 */ 1041 NGHTTP3_QPACK_TOKEN_TE, 1042 /** 1043 * :enum:`NGHTTP3_QPACK_TOKEN__PROTOCOL` is a token for 1044 * ``:protocol``. 1045 */ 1046 NGHTTP3_QPACK_TOKEN__PROTOCOL, 1047 /** 1048 * :enum:`NGHTTP3_QPACK_TOKEN_PRIORITY` is a token for ``priority``. 1049 */ 1050 NGHTTP3_QPACK_TOKEN_PRIORITY 1051 } nghttp3_qpack_token; 1052 1053 /** 1054 * @struct 1055 * 1056 * :type:`nghttp3_qpack_nv` represents header field name/value pair 1057 * just like :type:`nghttp3_nv`. It is an extended version of 1058 * :type:`nghttp3_nv` and has reference counted buffers and tokens 1059 * which might be useful for applications. 1060 */ 1061 typedef struct nghttp3_qpack_nv { 1062 /** 1063 * :member:`name` is the buffer containing header field name. 1064 * NULL-termination is guaranteed. 1065 */ 1066 nghttp3_rcbuf *name; 1067 /** 1068 * :member:`value` is the buffer containing header field value. 1069 * NULL-termination is guaranteed. 1070 */ 1071 nghttp3_rcbuf *value; 1072 /** 1073 * :member:`token` is :type:`nghttp3_qpack_token` value of 1074 * :member:`name`. It could be -1 if we have no token for that 1075 * header field name. 1076 */ 1077 int32_t token; 1078 /** 1079 * :member:`flags` is a bitwise OR of one or more of 1080 * :macro:`NGHTTP3_NV_FLAG_* <NGHTTP3_NV_FLAG_NONE>`. 1081 */ 1082 uint8_t flags; 1083 } nghttp3_qpack_nv; 1084 1085 /** 1086 * @struct 1087 * 1088 * :type:`nghttp3_qpack_encoder` is QPACK encoder. 1089 */ 1090 typedef struct nghttp3_qpack_encoder nghttp3_qpack_encoder; 1091 1092 /** 1093 * @function 1094 * 1095 * `nghttp3_qpack_encoder_new` initializes QPACK encoder. |pencoder| 1096 * must be non-NULL pointer. |hard_max_dtable_capacity| is the upper 1097 * bound of the dynamic table capacity. |mem| is a memory allocator. 1098 * This function allocates memory for :type:`nghttp3_qpack_encoder` 1099 * itself and assigns its pointer to |*pencoder| if it succeeds. 1100 * 1101 * The maximum dynamic table capacity is still 0. In order to change 1102 * the maximum dynamic table capacity, call 1103 * `nghttp3_qpack_encoder_set_max_dtable_capacity`. 1104 * 1105 * This function returns 0 if it succeeds, or one of the following 1106 * negative error codes: 1107 * 1108 * :macro:`NGHTTP3_ERR_NOMEM` 1109 * Out of memory. 1110 */ 1111 NGHTTP3_EXTERN int nghttp3_qpack_encoder_new(nghttp3_qpack_encoder **pencoder, 1112 size_t hard_max_dtable_capacity, 1113 const nghttp3_mem *mem); 1114 1115 /** 1116 * @function 1117 * 1118 * `nghttp3_qpack_encoder_del` frees memory allocated for |encoder|. 1119 * This function frees memory pointed by |encoder| itself. 1120 */ 1121 NGHTTP3_EXTERN void nghttp3_qpack_encoder_del(nghttp3_qpack_encoder *encoder); 1122 1123 /** 1124 * @function 1125 * 1126 * `nghttp3_qpack_encoder_encode` encodes the list of header fields 1127 * |nva|. |nvlen| is the length of |nva|. |stream_id| is the 1128 * identifier of the stream which this header fields belong to. This 1129 * function writes header block prefix, encoded header fields, and 1130 * encoder stream to |pbuf|, |rbuf|, and |ebuf| respectively. The 1131 * :member:`nghttp3_buf.last` will be adjusted when data is written. 1132 * An application should write |pbuf| and |rbuf| to the request stream 1133 * in this order. 1134 * 1135 * The buffer pointed by |pbuf|, |rbuf|, and |ebuf| can be empty 1136 * buffer. It is fine to pass a buffer initialized by 1137 * `nghttp3_buf_init(buf) <nghttp3_buf_init>`. This function 1138 * allocates memory for these buffers as necessary. In particular, it 1139 * frees and expands buffer if the current capacity of buffer is not 1140 * enough. If :member:`nghttp3_buf.begin` of any buffer is not NULL, 1141 * it must be allocated by the same memory allocator passed to 1142 * `nghttp3_qpack_encoder_new()`. 1143 * 1144 * This function returns 0 if it succeeds, or one of the following 1145 * negative error codes: 1146 * 1147 * :macro:`NGHTTP3_ERR_NOMEM` 1148 * Out of memory 1149 * :macro:`NGHTTP3_ERR_QPACK_FATAL` 1150 * |encoder| is in unrecoverable error state and cannot be used 1151 * anymore. 1152 */ 1153 NGHTTP3_EXTERN int nghttp3_qpack_encoder_encode( 1154 nghttp3_qpack_encoder *encoder, nghttp3_buf *pbuf, nghttp3_buf *rbuf, 1155 nghttp3_buf *ebuf, int64_t stream_id, const nghttp3_nv *nva, size_t nvlen); 1156 1157 /** 1158 * @function 1159 * 1160 * `nghttp3_qpack_encoder_read_decoder` reads decoder stream. The 1161 * buffer pointed by |src| of length |srclen| contains decoder stream. 1162 * 1163 * This function returns the number of bytes read, or one of the 1164 * following negative error codes: 1165 * 1166 * :macro:`NGHTTP3_ERR_NOMEM` 1167 * Out of memory 1168 * :macro:`NGHTTP3_ERR_QPACK_FATAL` 1169 * |encoder| is in unrecoverable error state and cannot be used 1170 * anymore. 1171 * :macro:`NGHTTP3_ERR_QPACK_DECODER_STREAM` 1172 * |encoder| is unable to process input because it is malformed. 1173 */ 1174 NGHTTP3_EXTERN nghttp3_ssize nghttp3_qpack_encoder_read_decoder( 1175 nghttp3_qpack_encoder *encoder, const uint8_t *src, size_t srclen); 1176 1177 /** 1178 * @function 1179 * 1180 * `nghttp3_qpack_encoder_set_max_dtable_capacity` sets max dynamic 1181 * table capacity to |max_dtable_capacity|. If |max_dtable_capacity| is 1182 * larger than ``hard_max_dtable_capacity`` parameter of 1183 * `nghttp3_qpack_encoder_new`, it is truncated to the latter. 1184 */ 1185 NGHTTP3_EXTERN void 1186 nghttp3_qpack_encoder_set_max_dtable_capacity(nghttp3_qpack_encoder *encoder, 1187 size_t max_dtable_capacity); 1188 1189 /** 1190 * @function 1191 * 1192 * `nghttp3_qpack_encoder_set_max_blocked_streams` sets the number of 1193 * streams which can be blocked to |max_blocked_streams|. 1194 */ 1195 NGHTTP3_EXTERN void 1196 nghttp3_qpack_encoder_set_max_blocked_streams(nghttp3_qpack_encoder *encoder, 1197 size_t max_blocked_streams); 1198 1199 /** 1200 * @function 1201 * 1202 * `nghttp3_qpack_encoder_ack_everything` tells |encoder| that all 1203 * encoded header blocks are acknowledged. This function is provided 1204 * for debugging purpose only. In HTTP/3, |encoder| knows this by 1205 * reading decoder stream with `nghttp3_qpack_encoder_read_decoder()`. 1206 */ 1207 NGHTTP3_EXTERN void 1208 nghttp3_qpack_encoder_ack_everything(nghttp3_qpack_encoder *encoder); 1209 1210 /** 1211 * @function 1212 * 1213 * `nghttp3_qpack_encoder_get_num_blocked_streams` returns the number 1214 * of streams which are potentially blocked at decoder side. 1215 */ 1216 NGHTTP3_EXTERN size_t 1217 nghttp3_qpack_encoder_get_num_blocked_streams(nghttp3_qpack_encoder *encoder); 1218 1219 /** 1220 * @struct 1221 * 1222 * :type:`nghttp3_qpack_stream_context` is a decoder context for an 1223 * individual stream. Its state is per header block. In order to 1224 * reuse this object for another header block, call 1225 * `nghttp3_qpack_stream_context_reset`. 1226 */ 1227 typedef struct nghttp3_qpack_stream_context nghttp3_qpack_stream_context; 1228 1229 /** 1230 * @function 1231 * 1232 * `nghttp3_qpack_stream_context_new` initializes stream context. 1233 * |psctx| must be non-NULL pointer. |stream_id| is stream ID. |mem| 1234 * is a memory allocator. This function allocates memory for 1235 * :type:`nghttp3_qpack_stream_context` itself and assigns its pointer 1236 * to |*psctx| if it succeeds. 1237 * 1238 * This function returns 0 if it succeeds, or one of the following 1239 * negative error codes: 1240 * 1241 * :macro:`NGHTTP3_ERR_NOMEM` 1242 * Out of memory. 1243 */ 1244 NGHTTP3_EXTERN int 1245 nghttp3_qpack_stream_context_new(nghttp3_qpack_stream_context **psctx, 1246 int64_t stream_id, const nghttp3_mem *mem); 1247 1248 /** 1249 * @function 1250 * 1251 * `nghttp3_qpack_stream_context_del` frees memory allocated for 1252 * |sctx|. This function frees memory pointed by |sctx| itself. 1253 */ 1254 NGHTTP3_EXTERN void 1255 nghttp3_qpack_stream_context_del(nghttp3_qpack_stream_context *sctx); 1256 1257 /** 1258 * @function 1259 * 1260 * `nghttp3_qpack_stream_context_get_ricnt` returns required insert 1261 * count. 1262 */ 1263 NGHTTP3_EXTERN uint64_t 1264 nghttp3_qpack_stream_context_get_ricnt(nghttp3_qpack_stream_context *sctx); 1265 1266 /** 1267 * @function 1268 * 1269 * `nghttp3_qpack_stream_context_reset` resets the state of |sctx|. 1270 * Then it can be reused for an another header block in the same 1271 * stream. 1272 */ 1273 NGHTTP3_EXTERN 1274 void nghttp3_qpack_stream_context_reset(nghttp3_qpack_stream_context *sctx); 1275 1276 /** 1277 * @struct 1278 * 1279 * :type:`nghttp3_qpack_decoder` is QPACK decoder. 1280 */ 1281 typedef struct nghttp3_qpack_decoder nghttp3_qpack_decoder; 1282 1283 /** 1284 * @function 1285 * 1286 * `nghttp3_qpack_decoder_new` initializes QPACK decoder. |pdecoder| 1287 * must be non-NULL pointer. |hard_max_dtable_capacity| is the upper 1288 * bound of the dynamic table capacity. |max_blocked_streams| is the 1289 * maximum number of streams which can be blocked. |mem| is a memory 1290 * allocator. This function allocates memory for 1291 * :type:`nghttp3_qpack_decoder` itself and assigns its pointer to 1292 * |*pdecoder| if it succeeds. 1293 * 1294 * This function returns 0 if it succeeds, or one of the following 1295 * negative error codes: 1296 * 1297 * :macro:`NGHTTP3_ERR_NOMEM` 1298 * Out of memory. 1299 */ 1300 NGHTTP3_EXTERN int nghttp3_qpack_decoder_new(nghttp3_qpack_decoder **pdecoder, 1301 size_t hard_max_dtable_capacity, 1302 size_t max_blocked_streams, 1303 const nghttp3_mem *mem); 1304 1305 /** 1306 * @function 1307 * 1308 * `nghttp3_qpack_decoder_del` frees memory allocated for |decoder|. 1309 * This function frees memory pointed by |decoder| itself. 1310 */ 1311 NGHTTP3_EXTERN void nghttp3_qpack_decoder_del(nghttp3_qpack_decoder *decoder); 1312 1313 /** 1314 * @function 1315 * 1316 * `nghttp3_qpack_decoder_read_encoder` reads encoder stream. The 1317 * buffer pointed by |src| of length |srclen| contains encoder stream. 1318 * 1319 * This function returns the number of bytes read, or one of the 1320 * following negative error codes: 1321 * 1322 * :macro:`NGHTTP3_ERR_NOMEM` 1323 * Out of memory. 1324 * :macro:`NGHTTP3_ERR_QPACK_FATAL` 1325 * |decoder| is in unrecoverable error state and cannot be used 1326 * anymore. 1327 * :macro:`NGHTTP3_ERR_QPACK_ENCODER_STREAM` 1328 * Could not interpret encoder stream instruction. 1329 */ 1330 NGHTTP3_EXTERN nghttp3_ssize nghttp3_qpack_decoder_read_encoder( 1331 nghttp3_qpack_decoder *decoder, const uint8_t *src, size_t srclen); 1332 1333 /** 1334 * @function 1335 * 1336 * `nghttp3_qpack_decoder_get_icnt` returns insert count. 1337 */ 1338 NGHTTP3_EXTERN uint64_t 1339 nghttp3_qpack_decoder_get_icnt(const nghttp3_qpack_decoder *decoder); 1340 1341 /** 1342 * @macrosection 1343 * 1344 * Flags for QPACK decoder 1345 */ 1346 1347 /** 1348 * @macro 1349 * 1350 * :macro:`NGHTTP3_QPACK_DECODE_FLAG_NONE` indicates that no flag set. 1351 */ 1352 #define NGHTTP3_QPACK_DECODE_FLAG_NONE 0x00u 1353 1354 /** 1355 * @macro 1356 * 1357 * :macro:`NGHTTP3_QPACK_DECODE_FLAG_EMIT` indicates that a header 1358 * field is successfully decoded. 1359 */ 1360 #define NGHTTP3_QPACK_DECODE_FLAG_EMIT 0x01u 1361 1362 /** 1363 * @macro 1364 * 1365 * :macro:`NGHTTP3_QPACK_DECODE_FLAG_FINAL` indicates that all header 1366 * fields have been decoded. 1367 */ 1368 #define NGHTTP3_QPACK_DECODE_FLAG_FINAL 0x02u 1369 1370 /** 1371 * @macro 1372 * 1373 * :macro:`NGHTTP3_QPACK_DECODE_FLAG_BLOCKED` indicates that decoding 1374 * has been blocked. 1375 */ 1376 #define NGHTTP3_QPACK_DECODE_FLAG_BLOCKED 0x04u 1377 1378 /** 1379 * @function 1380 * 1381 * `nghttp3_qpack_decoder_read_request` reads request stream. The 1382 * request stream is given as the buffer pointed by |src| of length 1383 * |srclen|. |sctx| is the stream context and it must be created by 1384 * `nghttp3_qpack_stream_context_new()`. |*pflags| must be non-NULL 1385 * pointer. |nv| must be non-NULL pointer. 1386 * 1387 * If this function succeeds, it assigns flags to |*pflags|. If 1388 * |*pflags| has :macro:`NGHTTP3_QPACK_DECODE_FLAG_EMIT` set, a 1389 * decoded header field is assigned to |nv|. If |*pflags| has 1390 * :macro:`NGHTTP3_QPACK_DECODE_FLAG_FINAL` set, all header fields 1391 * have been successfully decoded. If |*pflags| has 1392 * :macro:`NGHTTP3_QPACK_DECODE_FLAG_BLOCKED` set, decoding is blocked 1393 * due to required insert count. 1394 * 1395 * When a header field is decoded, an application receives it in |nv|. 1396 * :member:`nv->name <nghttp3_qpack_nv.name>` and :member:`nv->value 1397 * <nghttp3_qpack_nv.value>` are reference counted buffer, and their 1398 * reference counts are already incremented for application use. 1399 * Therefore, when application finishes processing the header field, 1400 * it must call `nghttp3_rcbuf_decref(nv->name) 1401 * <nghttp3_rcbuf_decref>` and `nghttp3_rcbuf_decref(nv->value) 1402 * <nghttp3_rcbuf_decref>` or memory leak might occur. These 1403 * :type:`nghttp3_rcbuf` objects hold the pointer to 1404 * :type:`nghttp3_mem` that is passed to `nghttp3_qpack_decoder_new` 1405 * (or either `nghttp3_conn_client_new` or `nghttp3_conn_server_new` 1406 * if it is used indirectly). As long as these objects are alive, the 1407 * pointed :type:`nghttp3_mem` object must be available. Otherwise, 1408 * `nghttp3_rcbuf_decref` will cause undefined behavior. 1409 * 1410 * This function returns the number of bytes read, or one of the 1411 * following negative error codes: 1412 * 1413 * :macro:`NGHTTP3_ERR_NOMEM` 1414 * Out of memory. 1415 * :macro:`NGHTTP3_ERR_QPACK_FATAL` 1416 * |decoder| is in unrecoverable error state and cannot be used 1417 * anymore. 1418 * :macro:`NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED` 1419 * Could not interpret header block instruction. 1420 * :macro:`NGHTTP3_ERR_QPACK_HEADER_TOO_LARGE` 1421 * Header field is too large. 1422 */ 1423 NGHTTP3_EXTERN nghttp3_ssize nghttp3_qpack_decoder_read_request( 1424 nghttp3_qpack_decoder *decoder, nghttp3_qpack_stream_context *sctx, 1425 nghttp3_qpack_nv *nv, uint8_t *pflags, const uint8_t *src, size_t srclen, 1426 int fin); 1427 1428 /** 1429 * @function 1430 * 1431 * `nghttp3_qpack_decoder_write_decoder` writes decoder stream into 1432 * |dbuf|. 1433 * 1434 * The caller must ensure that `nghttp3_buf_left(dbuf) 1435 * <nghttp3_buf_left>` >= 1436 * `nghttp3_qpack_decoder_get_decoder_streamlen(decoder) 1437 * <nghttp3_qpack_decoder_get_decoder_streamlen>`. 1438 */ 1439 NGHTTP3_EXTERN void 1440 nghttp3_qpack_decoder_write_decoder(nghttp3_qpack_decoder *decoder, 1441 nghttp3_buf *dbuf); 1442 1443 /** 1444 * @function 1445 * 1446 * `nghttp3_qpack_decoder_get_decoder_streamlen` returns the length of 1447 * decoder stream. 1448 */ 1449 NGHTTP3_EXTERN size_t 1450 nghttp3_qpack_decoder_get_decoder_streamlen(nghttp3_qpack_decoder *decoder); 1451 1452 /** 1453 * @function 1454 * 1455 * `nghttp3_qpack_decoder_cancel_stream` cancels header decoding for 1456 * stream denoted by |stream_id|. 1457 * 1458 * This function returns 0 if it succeeds, or one of the following 1459 * negative error codes: 1460 * 1461 * :macro:`NGHTTP3_ERR_NOMEM` 1462 * Out of memory. 1463 * :macro:`NGHTTP3_ERR_QPACK_FATAL` 1464 * Decoder stream overflow. 1465 */ 1466 NGHTTP3_EXTERN int 1467 nghttp3_qpack_decoder_cancel_stream(nghttp3_qpack_decoder *decoder, 1468 int64_t stream_id); 1469 1470 /** 1471 * @function 1472 * 1473 * `nghttp3_qpack_decoder_set_max_dtable_capacity` sets 1474 * |max_dtable_capacity| as maximum dynamic table size. 1475 * |max_dtable_capacity| must be equal to or smaller than 1476 * ``hard_max_dtable_capacity`` parameter of 1477 * `nghttp3_qpack_decoder_new`. Normally, the maximum capacity is 1478 * communicated in encoder stream. This function is provided for 1479 * debugging and testing purpose. 1480 * 1481 * This function returns 0 if it succeeds, or one of the 1482 * following negative error codes: 1483 * 1484 * :macro:`NGHTTP3_ERR_INVALID_ARGUMENT` 1485 * |max_dtable_capacity| exceeds the upper bound of the dynamic 1486 * table capacity. 1487 */ 1488 NGHTTP3_EXTERN int 1489 nghttp3_qpack_decoder_set_max_dtable_capacity(nghttp3_qpack_decoder *decoder, 1490 size_t max_dtable_capacity); 1491 1492 /** 1493 * @function 1494 * 1495 * `nghttp3_qpack_decoder_set_max_concurrent_streams` tells |decoder| 1496 * the maximum number of concurrent streams that a remote endpoint can 1497 * open, including both bidirectional and unidirectional streams which 1498 * potentially receive QPACK encoded HEADERS frame. This value is 1499 * used as a hint to limit the length of decoder stream. 1500 */ 1501 NGHTTP3_EXTERN void 1502 nghttp3_qpack_decoder_set_max_concurrent_streams(nghttp3_qpack_decoder *decoder, 1503 size_t max_concurrent_streams); 1504 1505 /** 1506 * @function 1507 * 1508 * `nghttp3_strerror` returns textual representation of |liberr|. 1509 */ 1510 NGHTTP3_EXTERN const char *nghttp3_strerror(int liberr); 1511 1512 /** 1513 * @function 1514 * 1515 * `nghttp3_err_infer_quic_app_error_code` returns a QUIC application 1516 * error code which corresponds to |liberr|. 1517 */ 1518 NGHTTP3_EXTERN uint64_t nghttp3_err_infer_quic_app_error_code(int liberr); 1519 1520 /** 1521 * @functypedef 1522 * 1523 * :type:`nghttp3_debug_vprintf_callback` is a callback function 1524 * invoked when the library outputs debug logging. The function is 1525 * called with arguments suitable for :manpage:`vfprintf(3)`. 1526 * 1527 * The debug output is only enabled if the library is built with 1528 * :macro:`DEBUGBUILD` macro defined. 1529 */ 1530 typedef void (*nghttp3_debug_vprintf_callback)(const char *format, 1531 va_list args); 1532 1533 /** 1534 * @function 1535 * 1536 * `nghttp3_set_debug_vprintf_callback` sets a debug output callback 1537 * called by the library when built with :macro:`DEBUGBUILD` macro 1538 * defined. If this option is not used, debug log is written into 1539 * standard error output. 1540 * 1541 * For builds without :macro:`DEBUGBUILD` macro defined, this function 1542 * is noop. 1543 * 1544 * Note that building with :macro:`DEBUGBUILD` may cause significant 1545 * performance penalty to libnghttp3 because of extra processing. It 1546 * should be used for debugging purpose only. 1547 * 1548 * .. Warning:: 1549 * 1550 * Building with :macro:`DEBUGBUILD` may cause significant 1551 * performance penalty to libnghttp3 because of extra processing. 1552 * It should be used for debugging purpose only. We write this two 1553 * times because this is important. 1554 */ 1555 NGHTTP3_EXTERN void nghttp3_set_debug_vprintf_callback( 1556 nghttp3_debug_vprintf_callback debug_vprintf_callback); 1557 1558 /** 1559 * @macrosection 1560 * 1561 * Shutdown related constants 1562 */ 1563 1564 /** 1565 * @macro 1566 * 1567 * :macro:`NGHTTP3_SHUTDOWN_NOTICE_STREAM_ID` specifies stream id sent 1568 * by a server when it initiates graceful shutdown of the connection 1569 * via `nghttp3_conn_submit_shutdown_notice`. 1570 */ 1571 #define NGHTTP3_SHUTDOWN_NOTICE_STREAM_ID ((1ull << 62) - 4) 1572 1573 /** 1574 * @macro 1575 * 1576 * :macro:`NGHTTP3_SHUTDOWN_NOTICE_PUSH_ID` specifies push id sent 1577 * by a client when it initiates graceful shutdown of the connection 1578 * via `nghttp3_conn_submit_shutdown_notice`. 1579 */ 1580 #define NGHTTP3_SHUTDOWN_NOTICE_PUSH_ID ((1ull << 62) - 1) 1581 1582 /** 1583 * @struct 1584 * 1585 * :type:`nghttp3_conn` represents a single HTTP/3 connection. 1586 */ 1587 typedef struct nghttp3_conn nghttp3_conn; 1588 1589 /** 1590 * @functypedef 1591 * 1592 * :type:`nghttp3_acked_stream_data` is a callback function which is 1593 * invoked when data sent on stream denoted by |stream_id| supplied 1594 * from application is acknowledged by remote endpoint. The number of 1595 * bytes acknowledged is given in |datalen|. 1596 * 1597 * The implementation of this callback must return 0 if it succeeds. 1598 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1599 * caller immediately. Any values other than 0 is treated as 1600 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1601 */ 1602 typedef int (*nghttp3_acked_stream_data)(nghttp3_conn *conn, int64_t stream_id, 1603 uint64_t datalen, void *conn_user_data, 1604 void *stream_user_data); 1605 1606 /** 1607 * @functypedef 1608 * 1609 * :type:`nghttp3_conn_stream_close` is a callback function which is 1610 * invoked when a stream identified by |stream_id| is closed. 1611 * |app_error_code| indicates the reason of this closure. 1612 * 1613 * The implementation of this callback must return 0 if it succeeds. 1614 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1615 * caller immediately. Any values other than 0 is treated as 1616 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1617 */ 1618 typedef int (*nghttp3_stream_close)(nghttp3_conn *conn, int64_t stream_id, 1619 uint64_t app_error_code, 1620 void *conn_user_data, 1621 void *stream_user_data); 1622 1623 /** 1624 * @functypedef 1625 * 1626 * :type:`nghttp3_recv_data` is a callback function which is invoked 1627 * when a part of request or response body on stream identified by 1628 * |stream_id| is received. |data| points to the received data and 1629 * its length is |datalen|. 1630 * 1631 * The application is responsible for increasing flow control credit 1632 * by |datalen| bytes. 1633 * 1634 * The implementation of this callback must return 0 if it succeeds. 1635 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1636 * caller immediately. Any values other than 0 is treated as 1637 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1638 */ 1639 typedef int (*nghttp3_recv_data)(nghttp3_conn *conn, int64_t stream_id, 1640 const uint8_t *data, size_t datalen, 1641 void *conn_user_data, void *stream_user_data); 1642 1643 /** 1644 * @functypedef 1645 * 1646 * :type:`nghttp3_deferred_consume` is a callback function which is 1647 * invoked when the library consumed |consumed| bytes for a stream 1648 * identified by |stream_id|. This callback is used to notify the 1649 * consumed bytes for stream blocked by QPACK decoder. The 1650 * application is responsible for increasing flow control credit by 1651 * |consumed| bytes. 1652 * 1653 * The implementation of this callback must return 0 if it succeeds. 1654 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1655 * caller immediately. Any values other than 0 is treated as 1656 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1657 */ 1658 typedef int (*nghttp3_deferred_consume)(nghttp3_conn *conn, int64_t stream_id, 1659 size_t consumed, void *conn_user_data, 1660 void *stream_user_data); 1661 1662 /** 1663 * @functypedef 1664 * 1665 * :type:`nghttp3_begin_headers` is a callback function which is 1666 * invoked when an incoming header block section is started on a 1667 * stream denoted by |stream_id|. Each header field is passed to 1668 * application by :type:`nghttp3_recv_header` callback. And then 1669 * :type:`nghttp3_end_headers` is called when a whole header block is 1670 * processed. 1671 * 1672 * The implementation of this callback must return 0 if it succeeds. 1673 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1674 * caller immediately. Any values other than 0 is treated as 1675 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1676 */ 1677 typedef int (*nghttp3_begin_headers)(nghttp3_conn *conn, int64_t stream_id, 1678 void *conn_user_data, 1679 void *stream_user_data); 1680 1681 /** 1682 * @functypedef 1683 * 1684 * :type:`nghttp3_recv_header` is a callback function which is invoked 1685 * when a header field is received on a stream denoted by |stream_id|. 1686 * |name| contains a field name and |value| contains a field value. 1687 * |token| is one of token defined in :type:`nghttp3_qpack_token` or 1688 * -1 if no token is defined for |name|. |flags| is bitwise OR of 1689 * zero or more of :macro:`NGHTTP3_NV_FLAG_* <NGHTTP3_NV_FLAG_NONE>`. 1690 * 1691 * The buffers for |name| and |value| are reference counted. If 1692 * application needs to keep them, increment the reference count with 1693 * `nghttp3_rcbuf_incref`. When they are no longer used, call 1694 * `nghttp3_rcbuf_decref`. 1695 * 1696 * The implementation of this callback must return 0 if it succeeds. 1697 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1698 * caller immediately. Any values other than 0 is treated as 1699 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1700 */ 1701 typedef int (*nghttp3_recv_header)(nghttp3_conn *conn, int64_t stream_id, 1702 int32_t token, nghttp3_rcbuf *name, 1703 nghttp3_rcbuf *value, uint8_t flags, 1704 void *conn_user_data, 1705 void *stream_user_data); 1706 1707 /** 1708 * @functypedef 1709 * 1710 * :type:`nghttp3_end_headers` is a callback function which is invoked 1711 * when an incoming header block has ended. 1712 * 1713 * If the stream ends with this header block, |fin| is set to nonzero. 1714 * 1715 * The implementation of this callback must return 0 if it succeeds. 1716 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1717 * caller immediately. Any values other than 0 is treated as 1718 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1719 */ 1720 typedef int (*nghttp3_end_headers)(nghttp3_conn *conn, int64_t stream_id, 1721 int fin, void *conn_user_data, 1722 void *stream_user_data); 1723 1724 /** 1725 * @functypedef 1726 * 1727 * :type:`nghttp3_end_stream` is a callback function which is invoked 1728 * when the receiving side of stream is closed. For server, this 1729 * callback function is invoked when HTTP request is received 1730 * completely. For client, this callback function is invoked when 1731 * HTTP response is received completely. 1732 * 1733 * The implementation of this callback must return 0 if it succeeds. 1734 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1735 * caller immediately. Any values other than 0 is treated as 1736 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1737 */ 1738 typedef int (*nghttp3_end_stream)(nghttp3_conn *conn, int64_t stream_id, 1739 void *conn_user_data, void *stream_user_data); 1740 1741 /** 1742 * @functypedef 1743 * 1744 * :type:`nghttp3_stop_sending` is a callback function which is 1745 * invoked when the library asks application to send STOP_SENDING to 1746 * the stream identified by |stream_id|. |app_error_code| indicates 1747 * the reason for this action. 1748 * 1749 * The implementation of this callback must return 0 if it succeeds. 1750 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1751 * caller immediately. Any values other than 0 is treated as 1752 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1753 */ 1754 typedef int (*nghttp3_stop_sending)(nghttp3_conn *conn, int64_t stream_id, 1755 uint64_t app_error_code, 1756 void *conn_user_data, 1757 void *stream_user_data); 1758 1759 /** 1760 * @functypedef 1761 * 1762 * :type:`nghttp3_reset_stream` is a callback function which is 1763 * invoked when the library asks application to reset stream 1764 * identified by |stream_id|. |app_error_code| indicates the reason 1765 * for this action. 1766 * 1767 * The implementation of this callback must return 0 if it succeeds. 1768 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1769 * caller immediately. Any values other than 0 is treated as 1770 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1771 */ 1772 typedef int (*nghttp3_reset_stream)(nghttp3_conn *conn, int64_t stream_id, 1773 uint64_t app_error_code, 1774 void *conn_user_data, 1775 void *stream_user_data); 1776 1777 /** 1778 * @functypedef 1779 * 1780 * :type:`nghttp3_shutdown` is a callback function which is invoked 1781 * when a shutdown is initiated by the remote endpoint. For client, 1782 * |id| contains a stream id of a client initiated stream, for server, 1783 * it contains a push id. All client streams with stream id or pushes 1784 * with push id equal to or larger than |id| are guaranteed to not be 1785 * processed by the remote endpoint. 1786 * 1787 * Parameter |id| for client can contain a special value 1788 * :macro:`NGHTTP3_SHUTDOWN_NOTICE_STREAM_ID` and for server it can 1789 * contain special value 1790 * :macro:`NGHTTP3_SHUTDOWN_NOTICE_PUSH_ID`. These values signal 1791 * request for graceful shutdown of the connection, triggered by 1792 * remote endpoint's invocation of 1793 * `nghttp3_conn_submit_shutdown_notice`. 1794 * 1795 * It is possible that this callback is invoked multiple times on a 1796 * single connection, however the |id| can only stay the same or 1797 * decrease, never increase. 1798 * 1799 * The implementation of this callback must return 0 if it succeeds. 1800 * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the 1801 * caller immediately. Any values other than 0 is treated as 1802 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 1803 */ 1804 typedef int (*nghttp3_shutdown)(nghttp3_conn *conn, int64_t id, 1805 void *conn_user_data); 1806 1807 #define NGHTTP3_CALLBACKS_VERSION_V1 1 1808 #define NGHTTP3_CALLBACKS_VERSION NGHTTP3_CALLBACKS_VERSION_V1 1809 1810 /** 1811 * @struct 1812 * 1813 * :type:`nghttp3_callbacks` holds a set of callback functions. 1814 */ 1815 typedef struct nghttp3_callbacks { 1816 /** 1817 * :member:`acked_stream_data` is a callback function which is 1818 * invoked when data sent on a particular stream have been 1819 * acknowledged by a remote endpoint. 1820 */ 1821 nghttp3_acked_stream_data acked_stream_data; 1822 /** 1823 * :member:`stream_close` is a callback function which is invoked 1824 * when a particular stream has closed. 1825 */ 1826 nghttp3_stream_close stream_close; 1827 /** 1828 * :member:`recv_data` is a callback function which is invoked when 1829 * stream data is received. 1830 */ 1831 nghttp3_recv_data recv_data; 1832 /** 1833 * :member:`deferred_consume` is a callback function which is 1834 * invoked when the library consumed data for a particular stream 1835 * which had been blocked for synchronization between streams. 1836 */ 1837 nghttp3_deferred_consume deferred_consume; 1838 /** 1839 * :member:`begin_headers` is a callback function which is invoked 1840 * when a header block has started on a particular stream. 1841 */ 1842 nghttp3_begin_headers begin_headers; 1843 /** 1844 * :member:`recv_header` is a callback function which is invoked 1845 * when a single header field is received on a particular stream. 1846 */ 1847 nghttp3_recv_header recv_header; 1848 /** 1849 * :member:`end_headers` is a callback function which is invoked 1850 * when a header block has ended on a particular stream. 1851 */ 1852 nghttp3_end_headers end_headers; 1853 /** 1854 * :member:`begin_trailers` is a callback function which is invoked 1855 * when a trailer block has started on a particular stream. 1856 */ 1857 nghttp3_begin_headers begin_trailers; 1858 /** 1859 * :member:`recv_trailer` is a callback function which is invoked 1860 * when a single trailer field is received on a particular stream. 1861 */ 1862 nghttp3_recv_header recv_trailer; 1863 /** 1864 * :member:`end_trailers` is a callback function which is invoked 1865 * when a trailer block has ended on a particular stream. 1866 */ 1867 nghttp3_end_headers end_trailers; 1868 /** 1869 * :member:`stop_sending` is a callback function which is invoked 1870 * when the library asks application to send STOP_SENDING to a 1871 * particular stream. 1872 */ 1873 nghttp3_stop_sending stop_sending; 1874 /** 1875 * :member:`end_stream` is a callback function which is invoked when 1876 * a receiving side of stream has been closed. 1877 */ 1878 nghttp3_end_stream end_stream; 1879 /** 1880 * :member:`reset_stream` is a callback function which is invoked 1881 * when the library asks application to reset stream (by sending 1882 * RESET_STREAM). 1883 */ 1884 nghttp3_reset_stream reset_stream; 1885 /** 1886 * :member:`shutdown` is a callback function which is invoked when 1887 * the remote endpoint has signalled initiation of connection shutdown. 1888 */ 1889 nghttp3_shutdown shutdown; 1890 } nghttp3_callbacks; 1891 1892 #define NGHTTP3_SETTINGS_VERSION_V1 1 1893 #define NGHTTP3_SETTINGS_VERSION NGHTTP3_SETTINGS_VERSION_V1 1894 1895 /** 1896 * @struct 1897 * 1898 * :type:`nghttp3_settings` defines HTTP/3 settings. 1899 */ 1900 typedef struct nghttp3_settings { 1901 /** 1902 * :member:`max_field_section_size` specifies the maximum header 1903 * section (block) size. 1904 */ 1905 uint64_t max_field_section_size; 1906 /** 1907 * :member:`qpack_max_dtable_capacity` is the maximum size of QPACK 1908 * dynamic table. 1909 */ 1910 size_t qpack_max_dtable_capacity; 1911 /** 1912 * :member:`qpack_encoder_max_dtable_capacity` is the upper bound of 1913 * QPACK dynamic table capacity that the QPACK encoder is willing to 1914 * use. The effective maximum dynamic table capacity is the minimum 1915 * of this field and the value of the received 1916 * SETTINGS_QPACK_MAX_TABLE_CAPACITY. If this field is set to 0, 1917 * the encoder does not use the dynamic table. 1918 */ 1919 size_t qpack_encoder_max_dtable_capacity; 1920 /** 1921 * :member:`qpack_blocked_streams` is the maximum number of streams 1922 * which can be blocked while they are being decoded. 1923 */ 1924 size_t qpack_blocked_streams; 1925 /** 1926 * :member:`enable_connect_protocol`, if set to nonzero, enables 1927 * Extended CONNECT Method (see 1928 * https://www.ietf.org/archive/id/draft-ietf-httpbis-h3-websockets-00.html). 1929 * Client ignores this field. 1930 */ 1931 int enable_connect_protocol; 1932 } nghttp3_settings; 1933 1934 /** 1935 * @function 1936 * 1937 * `nghttp3_settings_default` fills |settings| with the default 1938 * values. 1939 * 1940 * - :member:`max_field_section_size 1941 * <nghttp3_settings.max_field_section_size>` = :expr:`((1ull << 62) - 1)` 1942 * - :member:`qpack_max_dtable_capacity 1943 * <nghttp3_settings.qpack_max_dtable_capacity>` = 0 1944 * - :member:`qpack_encoder_max_dtable_capacity 1945 * <nghttp3_settings.qpack_encoder_max_dtable_capacity>` = 4096 1946 * - :member:`qpack_blocked_streams 1947 * <nghttp3_settings.qpack_blocked_streams>` = 0 1948 * - :member:`enable_connect_protocol 1949 * <nghttp3_settings.enable_connect_protocol>` = 0 1950 */ 1951 NGHTTP3_EXTERN void 1952 nghttp3_settings_default_versioned(int settings_version, 1953 nghttp3_settings *settings); 1954 1955 /** 1956 * @function 1957 * 1958 * `nghttp3_conn_client_new` creates :type:`nghttp3_conn` and 1959 * initializes it for client use. The pointer to the object is stored 1960 * in |*pconn|. If |mem| is ``NULL``, the memory allocator returned 1961 * by `nghttp3_mem_default` is used. 1962 */ 1963 NGHTTP3_EXTERN int 1964 nghttp3_conn_client_new_versioned(nghttp3_conn **pconn, int callbacks_version, 1965 const nghttp3_callbacks *callbacks, 1966 int settings_version, 1967 const nghttp3_settings *settings, 1968 const nghttp3_mem *mem, void *conn_user_data); 1969 1970 /** 1971 * @function 1972 * 1973 * `nghttp3_conn_server_new` creates :type:`nghttp3_conn` and 1974 * initializes it for server use. The pointer to the object is stored 1975 * in |*pconn|. If |mem| is ``NULL``, the memory allocator returned 1976 * by `nghttp3_mem_default` is used. 1977 */ 1978 NGHTTP3_EXTERN int 1979 nghttp3_conn_server_new_versioned(nghttp3_conn **pconn, int callbacks_version, 1980 const nghttp3_callbacks *callbacks, 1981 int settings_version, 1982 const nghttp3_settings *settings, 1983 const nghttp3_mem *mem, void *conn_user_data); 1984 1985 /** 1986 * @function 1987 * 1988 * `nghttp3_conn_del` frees resources allocated for |conn|. 1989 */ 1990 NGHTTP3_EXTERN void nghttp3_conn_del(nghttp3_conn *conn); 1991 1992 /** 1993 * @function 1994 * 1995 * `nghttp3_conn_bind_control_stream` binds stream denoted by 1996 * |stream_id| to outgoing unidirectional control stream. 1997 * 1998 * This function returns 0 if it succeeds, or one of the following 1999 * negative error codes: 2000 * 2001 * :macro:`NGHTTP3_ERR_INVALID_STATE` 2002 * Control stream has already corresponding stream ID. 2003 * 2004 * TBD 2005 */ 2006 NGHTTP3_EXTERN int nghttp3_conn_bind_control_stream(nghttp3_conn *conn, 2007 int64_t stream_id); 2008 2009 /** 2010 * @function 2011 * 2012 * `nghttp3_conn_bind_qpack_streams` binds stream denoted by 2013 * |qenc_stream_id| to outgoing QPACK encoder stream and stream 2014 * denoted by |qdec_stream_id| to outgoing QPACK encoder stream. 2015 * 2016 * This function returns 0 if it succeeds, or one of the following 2017 * negative error codes: 2018 * 2019 * :macro:`NGHTTP3_ERR_INVALID_STATE` 2020 * QPACK encoder/decoder stream have already corresponding stream 2021 * IDs. 2022 * 2023 * TBD 2024 */ 2025 NGHTTP3_EXTERN int nghttp3_conn_bind_qpack_streams(nghttp3_conn *conn, 2026 int64_t qenc_stream_id, 2027 int64_t qdec_stream_id); 2028 2029 /** 2030 * @function 2031 * 2032 * `nghttp3_conn_read_stream` reads data |src| of length |srclen| on 2033 * stream identified by |stream_id|. It returns the number of bytes 2034 * consumed. The "consumed" means that application can increase flow 2035 * control credit (both stream and connection) of underlying QUIC 2036 * connection by that amount. It does not include the amount of data 2037 * carried by DATA frame which contains application data (excluding 2038 * any control or QPACK unidirectional streams) . See 2039 * :type:`nghttp3_recv_data` to handle those bytes. If |fin| is 2040 * nonzero, this is the last data from remote endpoint in this stream. 2041 */ 2042 NGHTTP3_EXTERN nghttp3_ssize nghttp3_conn_read_stream(nghttp3_conn *conn, 2043 int64_t stream_id, 2044 const uint8_t *src, 2045 size_t srclen, int fin); 2046 2047 /** 2048 * @function 2049 * 2050 * `nghttp3_conn_writev_stream` stores stream data to send to |vec| of 2051 * length |veccnt| and returns the number of nghttp3_vec object in 2052 * which it stored data. It stores stream ID to |*pstream_id|. An 2053 * application has to call `nghttp3_conn_add_write_offset` to inform 2054 * |conn| of the actual number of bytes that underlying QUIC stack 2055 * accepted. |*pfin| will be nonzero if this is the last data to 2056 * send. If there is no stream to write data or send fin, this 2057 * function returns 0, and -1 is assigned to |*pstream_id|. This 2058 * function may return 0 and |*pstream_id| is not -1 and |*pfin| is 2059 * nonzero. It means 0 length data to |*pstream_id| and it is the 2060 * last data to the stream. They must be passed to QUIC stack, and 2061 * they are accepted, the application has to call 2062 * `nghttp3_conn_add_write_offset`. 2063 */ 2064 NGHTTP3_EXTERN nghttp3_ssize nghttp3_conn_writev_stream(nghttp3_conn *conn, 2065 int64_t *pstream_id, 2066 int *pfin, 2067 nghttp3_vec *vec, 2068 size_t veccnt); 2069 2070 /** 2071 * @function 2072 * 2073 * `nghttp3_conn_add_write_offset` tells |conn| the number of bytes 2074 * |n| for stream denoted by |stream_id| QUIC stack accepted. 2075 * 2076 * If stream has no data to send but just sends fin (closing the write 2077 * side of a stream), the number of bytes sent is 0. It is important 2078 * to call this function even if |n| is 0 in this case. It is safe to 2079 * call this function if |n| is 0. 2080 * 2081 * `nghttp3_conn_writev_stream` must be called before calling this 2082 * function to get data to send, and those data must be fed into QUIC 2083 * stack. 2084 */ 2085 NGHTTP3_EXTERN int nghttp3_conn_add_write_offset(nghttp3_conn *conn, 2086 int64_t stream_id, size_t n); 2087 2088 /** 2089 * @function 2090 * 2091 * `nghttp3_conn_add_ack_offset` tells |conn| the number of bytes |n| 2092 * for stream denoted by |stream_id| QUIC stack has acknowledged. 2093 */ 2094 NGHTTP3_EXTERN int nghttp3_conn_add_ack_offset(nghttp3_conn *conn, 2095 int64_t stream_id, uint64_t n); 2096 2097 /** 2098 * @function 2099 * 2100 * `nghttp3_conn_block_stream` tells the library that stream 2101 * identified by |stream_id| is blocked due to QUIC flow control. 2102 */ 2103 NGHTTP3_EXTERN void nghttp3_conn_block_stream(nghttp3_conn *conn, 2104 int64_t stream_id); 2105 2106 /** 2107 * @function 2108 * 2109 * `nghttp3_conn_unblock_stream` tells the library that stream 2110 * identified by |stream_id| which was blocked by QUIC flow control is 2111 * unblocked. 2112 */ 2113 NGHTTP3_EXTERN int nghttp3_conn_unblock_stream(nghttp3_conn *conn, 2114 int64_t stream_id); 2115 2116 /** 2117 * @function 2118 * 2119 * `nghttp3_conn_is_stream_writable` returns nonzero if a stream 2120 * identified by |stream_id| is writable. It is not writable if: 2121 * 2122 * - the stream does not exist; or, 2123 * - the stream is closed (e.g., `nghttp3_conn_close_stream` is 2124 * called); or, 2125 * - the stream is QUIC flow control blocked (e.g., 2126 * `nghttp3_conn_block_stream` is called); or, 2127 * - the stream is input data blocked (e.g., 2128 * :macro:`NGHTTP3_ERR_WOULDBLOCK` is returned from 2129 * :type:`nghttp3_read_data_callback`); or, 2130 * - the stream is half-closed local (e.g., 2131 * `nghttp3_conn_shutdown_stream_write` is called). 2132 */ 2133 NGHTTP3_EXTERN int nghttp3_conn_is_stream_writable(nghttp3_conn *conn, 2134 int64_t stream_id); 2135 2136 /** 2137 * @function 2138 * 2139 * `nghttp3_conn_shutdown_stream_write` tells the library that any 2140 * further write operation to stream identified by |stream_id| is 2141 * prohibited. This works like `nghttp3_conn_block_stream`, but it 2142 * cannot be unblocked by `nghttp3_conn_unblock_stream`. 2143 */ 2144 NGHTTP3_EXTERN void nghttp3_conn_shutdown_stream_write(nghttp3_conn *conn, 2145 int64_t stream_id); 2146 2147 /** 2148 * @function 2149 * 2150 * `nghttp3_conn_shutdown_stream_read` tells the library that 2151 * read-side of stream denoted by |stream_id| is abruptly closed and 2152 * any further incoming data and pending stream data should be 2153 * discarded. 2154 */ 2155 NGHTTP3_EXTERN int nghttp3_conn_shutdown_stream_read(nghttp3_conn *conn, 2156 int64_t stream_id); 2157 2158 /** 2159 * @function 2160 * 2161 * `nghttp3_conn_resume_stream` resumes stream identified by 2162 * |stream_id| which was previously unable to provide data. 2163 */ 2164 NGHTTP3_EXTERN int nghttp3_conn_resume_stream(nghttp3_conn *conn, 2165 int64_t stream_id); 2166 2167 /** 2168 * @function 2169 * 2170 * `nghttp3_conn_close_stream` closes stream identified by 2171 * |stream_id|. |app_error_code| is the reason of the closure. 2172 * 2173 * This function returns 0 if it succeeds, or one of the following 2174 * negative error codes: 2175 * 2176 * :macro:`NGHTTP3_ERR_STREAM_NOT_FOUND` 2177 * Stream not found. 2178 * :macro:`NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM` 2179 * A critical stream is closed. 2180 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` 2181 * User callback failed 2182 */ 2183 NGHTTP3_EXTERN int nghttp3_conn_close_stream(nghttp3_conn *conn, 2184 int64_t stream_id, 2185 uint64_t app_error_code); 2186 2187 /** 2188 * @macrosection 2189 * 2190 * Data flags 2191 */ 2192 2193 /** 2194 * @macro 2195 * 2196 * :macro:`NGHTTP3_DATA_FLAG_NONE` indicates no flag set. 2197 */ 2198 #define NGHTTP3_DATA_FLAG_NONE 0x00u 2199 2200 /** 2201 * @macro 2202 * 2203 * :macro:`NGHTTP3_DATA_FLAG_EOF` indicates that all request or 2204 * response body has been provided to the library. It also indicates 2205 * that sending side of stream is closed unless 2206 * :macro:`NGHTTP3_DATA_FLAG_NO_END_STREAM` is given at the same time. 2207 */ 2208 #define NGHTTP3_DATA_FLAG_EOF 0x01u 2209 2210 /** 2211 * @macro 2212 * 2213 * :macro:`NGHTTP3_DATA_FLAG_NO_END_STREAM` indicates that sending 2214 * side of stream is not closed even if :macro:`NGHTTP3_DATA_FLAG_EOF` 2215 * is set. Usually this flag is used to send trailer fields with 2216 * `nghttp3_conn_submit_trailers()`. If 2217 * `nghttp3_conn_submit_trailers()` has been called, regardless of 2218 * this flag, the submitted trailer fields are sent. 2219 */ 2220 #define NGHTTP3_DATA_FLAG_NO_END_STREAM 0x02u 2221 2222 /** 2223 * @function 2224 * 2225 * `nghttp3_conn_set_max_client_streams_bidi` tells |conn| the 2226 * cumulative number of bidirectional streams that client can open. 2227 */ 2228 NGHTTP3_EXTERN void 2229 nghttp3_conn_set_max_client_streams_bidi(nghttp3_conn *conn, 2230 uint64_t max_streams); 2231 2232 /** 2233 * @function 2234 * 2235 * `nghttp3_conn_set_max_concurrent_streams` tells |conn| the maximum 2236 * number of concurrent streams that a remote endpoint can open, 2237 * including both bidirectional and unidirectional streams which 2238 * potentially receive QPACK encoded HEADERS frame. This value is 2239 * used as a hint to limit the internal resource consumption. 2240 */ 2241 NGHTTP3_EXTERN void 2242 nghttp3_conn_set_max_concurrent_streams(nghttp3_conn *conn, 2243 size_t max_concurrent_streams); 2244 2245 /** 2246 * @functypedef 2247 * 2248 * :type:`nghttp3_read_data_callback` is a callback function invoked 2249 * when the library asks an application to provide stream data for a 2250 * stream denoted by |stream_id|. 2251 * 2252 * The library provides |vec| of length |veccnt| to the application. 2253 * The application should fill data and its length to |vec|. It has 2254 * to return the number of the filled objects. The application must 2255 * retain data until they are safe to free. It is notified by 2256 * :type:`nghttp3_acked_stream_data` callback. 2257 * 2258 * If this is the last data to send (or there is no data to send 2259 * because all data have been sent already), set 2260 * :macro:`NGHTTP3_DATA_FLAG_EOF` to |*pflags|. 2261 * 2262 * If the application is unable to provide data temporarily, return 2263 * :macro:`NGHTTP3_ERR_WOULDBLOCK`. When it is ready to provide 2264 * data, call `nghttp3_conn_resume_stream()`. 2265 * 2266 * The callback should return the number of objects in |vec| that the 2267 * application filled if it succeeds, or 2268 * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. 2269 * 2270 * TODO Add NGHTTP3_ERR_TEMPORAL_CALLBACK_FAILURE to reset just this 2271 * stream. 2272 */ 2273 typedef nghttp3_ssize (*nghttp3_read_data_callback)( 2274 nghttp3_conn *conn, int64_t stream_id, nghttp3_vec *vec, size_t veccnt, 2275 uint32_t *pflags, void *conn_user_data, void *stream_user_data); 2276 2277 /** 2278 * @struct 2279 * 2280 * :type:`nghttp3_data_reader` specifies the way how to generate 2281 * request or response body. 2282 */ 2283 typedef struct nghttp3_data_reader { 2284 /** 2285 * :member:`read_data` is a callback function to generate body. 2286 */ 2287 nghttp3_read_data_callback read_data; 2288 } nghttp3_data_reader; 2289 2290 /** 2291 * @function 2292 * 2293 * `nghttp3_conn_submit_request` submits HTTP request header fields 2294 * and body on the stream identified by |stream_id|. |stream_id| must 2295 * be a client initiated bidirectional stream. Only client can submit 2296 * HTTP request. |nva| of length |nvlen| specifies HTTP request 2297 * header fields. |dr| specifies a request body. If there is no 2298 * request body, specify NULL. If |dr| is NULL, it implies the end of 2299 * stream. |stream_user_data| is an opaque pointer attached to the 2300 * stream. 2301 */ 2302 NGHTTP3_EXTERN int nghttp3_conn_submit_request( 2303 nghttp3_conn *conn, int64_t stream_id, const nghttp3_nv *nva, size_t nvlen, 2304 const nghttp3_data_reader *dr, void *stream_user_data); 2305 2306 /** 2307 * @function 2308 * 2309 * `nghttp3_conn_submit_info` submits HTTP non-final response header 2310 * fields on the stream identified by |stream_id|. |nva| of length 2311 * |nvlen| specifies HTTP response header fields. 2312 */ 2313 NGHTTP3_EXTERN int nghttp3_conn_submit_info(nghttp3_conn *conn, 2314 int64_t stream_id, 2315 const nghttp3_nv *nva, 2316 size_t nvlen); 2317 2318 /** 2319 * @function 2320 * 2321 * `nghttp3_conn_submit_response` submits HTTP response header fields 2322 * and body on the stream identified by |stream_id|. |nva| of length 2323 * |nvlen| specifies HTTP response header fields. |dr| specifies a 2324 * response body. If there is no response body, specify NULL. If 2325 * |dr| is NULL, it implies the end of stream. 2326 */ 2327 NGHTTP3_EXTERN int nghttp3_conn_submit_response(nghttp3_conn *conn, 2328 int64_t stream_id, 2329 const nghttp3_nv *nva, 2330 size_t nvlen, 2331 const nghttp3_data_reader *dr); 2332 2333 /** 2334 * @function 2335 * 2336 * `nghttp3_conn_submit_trailers` submits HTTP trailer fields on the 2337 * stream identified by |stream_id|. |nva| of length |nvlen| 2338 * specifies HTTP trailer fields. Calling this function implies the 2339 * end of stream. 2340 */ 2341 NGHTTP3_EXTERN int nghttp3_conn_submit_trailers(nghttp3_conn *conn, 2342 int64_t stream_id, 2343 const nghttp3_nv *nva, 2344 size_t nvlen); 2345 2346 /** 2347 * @function 2348 * 2349 * `nghttp3_conn_submit_shutdown_notice` notifies the other endpoint 2350 * to stop creating new stream. After a couple of RTTs later, call 2351 * `nghttp3_conn_shutdown` to start graceful shutdown. 2352 */ 2353 NGHTTP3_EXTERN int nghttp3_conn_submit_shutdown_notice(nghttp3_conn *conn); 2354 2355 /** 2356 * @function 2357 * 2358 * `nghttp3_conn_shutdown` starts graceful shutdown. It should be 2359 * called after `nghttp3_conn_submit_shutdown_notice` and a couple of 2360 * RTT. After calling this function, the local endpoint starts 2361 * rejecting new incoming streams. The existing streams are processed 2362 * normally. 2363 */ 2364 NGHTTP3_EXTERN int nghttp3_conn_shutdown(nghttp3_conn *conn); 2365 2366 /** 2367 * @function 2368 * 2369 * `nghttp3_conn_set_stream_user_data` sets |stream_user_data| to the 2370 * stream identified by |stream_id|. 2371 */ 2372 NGHTTP3_EXTERN int nghttp3_conn_set_stream_user_data(nghttp3_conn *conn, 2373 int64_t stream_id, 2374 void *stream_user_data); 2375 2376 /** 2377 * @function 2378 * 2379 * `nghttp3_conn_get_frame_payload_left` returns the number of bytes 2380 * left to read current frame payload for a stream denoted by 2381 * |stream_id|. If no such stream is found, it returns 0. 2382 */ 2383 NGHTTP3_EXTERN uint64_t nghttp3_conn_get_frame_payload_left(nghttp3_conn *conn, 2384 int64_t stream_id); 2385 2386 /** 2387 * @macrosection 2388 * 2389 * HTTP stream priority flags 2390 */ 2391 2392 /** 2393 * @macro 2394 * 2395 * :macro:`NGHTTP3_DEFAULT_URGENCY` is the default urgency level. 2396 */ 2397 #define NGHTTP3_DEFAULT_URGENCY 3 2398 2399 /** 2400 * @macro 2401 * 2402 * :macro:`NGHTTP3_URGENCY_HIGH` is the highest urgency level. 2403 */ 2404 #define NGHTTP3_URGENCY_HIGH 0 2405 2406 /** 2407 * @macro 2408 * 2409 * :macro:`NGHTTP3_URGENCY_LOW` is the lowest urgency level. 2410 */ 2411 #define NGHTTP3_URGENCY_LOW 7 2412 2413 /** 2414 * @macro 2415 * 2416 * :macro:`NGHTTP3_URGENCY_LEVELS` is the number of urgency levels. 2417 */ 2418 #define NGHTTP3_URGENCY_LEVELS (NGHTTP3_URGENCY_LOW + 1) 2419 2420 /** 2421 * @struct 2422 * 2423 * :type:`nghttp3_pri` represents HTTP priority. 2424 */ 2425 typedef struct nghttp3_pri { 2426 /** 2427 * :member:`urgency` is the urgency of a stream, it must be in 2428 * [:macro:`NGHTTP3_URGENCY_HIGH`, :macro:`NGHTTP3_URGENCY_LOW`], 2429 * inclusive, and 0 is the highest urgency. 2430 */ 2431 uint32_t urgency; 2432 /** 2433 * :member:`inc` indicates that a content can be processed 2434 * incrementally or not. If inc is 0, it cannot be processed 2435 * incrementally. If inc is 1, it can be processed incrementally. 2436 * Other value is not permitted. 2437 */ 2438 int inc; 2439 } nghttp3_pri; 2440 2441 /** 2442 * @function 2443 * 2444 * `nghttp3_conn_get_stream_priority` stores stream priority of a 2445 * stream denoted by |stream_id| into |*dest|. |stream_id| must 2446 * identify client initiated bidirectional stream. Only server can 2447 * use this function. 2448 * 2449 * This function must not be called if |conn| is initialized as 2450 * client. 2451 * 2452 * This function returns 0 if it succeeds, or one of the following 2453 * negative error codes: 2454 * 2455 * :macro:`NGHTTP3_ERR_STREAM_NOT_FOUND` 2456 * Stream not found. 2457 */ 2458 NGHTTP3_EXTERN int nghttp3_conn_get_stream_priority(nghttp3_conn *conn, 2459 nghttp3_pri *dest, 2460 int64_t stream_id); 2461 2462 /** 2463 * @function 2464 * 2465 * `nghttp3_conn_set_stream_priority` updates priority of a stream 2466 * denoted by |stream_id| with the value pointed by |pri|. 2467 * |stream_id| must identify client initiated bidirectional stream. 2468 * 2469 * Both client and server can update stream priority with this 2470 * function. 2471 * 2472 * If server updates stream priority with this function, it completely 2473 * overrides stream priority set by client and the attempts to update 2474 * priority by client are ignored. 2475 * 2476 * This function returns 0 if it succeeds, or one of the following 2477 * negative error codes: 2478 * 2479 * :macro:`NGHTTP3_ERR_INVALID_ARGUMENT` 2480 * |stream_id| is not a client initiated bidirectional stream ID. 2481 * :macro:`NGHTTP3_ERR_STREAM_NOT_FOUND` 2482 * Stream not found. 2483 * :macro:`NGHTTP3_ERR_NOMEM` 2484 * Out of memory. 2485 */ 2486 NGHTTP3_EXTERN int nghttp3_conn_set_stream_priority(nghttp3_conn *conn, 2487 int64_t stream_id, 2488 const nghttp3_pri *pri); 2489 2490 /** 2491 * @function 2492 * 2493 * `nghttp3_conn_is_remote_qpack_encoder_stream` returns nonzero if a 2494 * stream denoted by |stream_id| is QPACK encoder stream of a remote 2495 * endpoint. 2496 */ 2497 NGHTTP3_EXTERN int 2498 nghttp3_conn_is_remote_qpack_encoder_stream(nghttp3_conn *conn, 2499 int64_t stream_id); 2500 2501 /** 2502 * @function 2503 * 2504 * `nghttp3_vec_len` returns the sum of length in |vec| of |cnt| 2505 * elements. 2506 */ 2507 NGHTTP3_EXTERN uint64_t nghttp3_vec_len(const nghttp3_vec *vec, size_t cnt); 2508 2509 /** 2510 * @function 2511 * 2512 * `nghttp3_check_header_name` returns nonzero if HTTP header field 2513 * name |name| of length |len| is valid according to 2514 * :rfc:`7230#section-3.2`. 2515 * 2516 * Because this is a header field name in HTTP/3, the upper cased 2517 * alphabet is treated as error. 2518 */ 2519 NGHTTP3_EXTERN int nghttp3_check_header_name(const uint8_t *name, size_t len); 2520 2521 /** 2522 * @function 2523 * 2524 * `nghttp3_check_header_value` returns nonzero if HTTP header field 2525 * value |value| of length |len| is valid according to 2526 * :rfc:`7230#section-3.2`. 2527 */ 2528 NGHTTP3_EXTERN int nghttp3_check_header_value(const uint8_t *value, size_t len); 2529 2530 /** 2531 * @function 2532 * 2533 * `nghttp3_http_parse_priority` parses priority HTTP header field 2534 * stored in the buffer pointed by |value| of length |len|. If it 2535 * successfully processed header field value, it stores the result 2536 * into |*dest|. This function just overwrites what it sees in the 2537 * header field value and does not initialize any field in |*dest|. 2538 * 2539 * This function returns 0 if it succeeds, or one of the following 2540 * negative error codes: 2541 * 2542 * :macro:`NGHTTP3_ERR_INVALID_ARGUMENT` 2543 * The function could not parse the provided value. 2544 */ 2545 NGHTTP3_EXTERN int nghttp3_http_parse_priority(nghttp3_pri *dest, 2546 const uint8_t *value, 2547 size_t len); 2548 2549 /** 2550 * @macrosection 2551 * 2552 * nghttp3_info flags 2553 */ 2554 2555 /** 2556 * @macro 2557 * 2558 * :macro:`NGHTTP3_VERSION_AGE` is the age of :type:`nghttp3_info`. 2559 */ 2560 #define NGHTTP3_VERSION_AGE 1 2561 2562 /** 2563 * @struct 2564 * 2565 * :type:`nghttp3_info` is what `nghttp3_version()` returns. It holds 2566 * information about the particular nghttp3 version. 2567 */ 2568 typedef struct nghttp3_info { 2569 /** 2570 * :member:`age` is the age of this struct. This instance of 2571 * nghttp3 sets it to :macro:`NGHTTP3_VERSION_AGE` but a future 2572 * version may bump it and add more struct fields at the bottom 2573 */ 2574 int age; 2575 /** 2576 * :member:`version_num` is the :macro:`NGHTTP3_VERSION_NUM` number 2577 * (since age ==1) 2578 */ 2579 int version_num; 2580 /** 2581 * :member:`version_str` points to the :macro:`NGHTTP3_VERSION` 2582 * string (since age ==1) 2583 */ 2584 const char *version_str; 2585 /* -------- the above fields all exist when age == 1 */ 2586 } nghttp3_info; 2587 2588 /** 2589 * @function 2590 * 2591 * `nghttp3_version` returns a pointer to a :type:`nghttp3_info` 2592 * struct with version information about the run-time library in use. 2593 * The |least_version| argument can be set to a 24 bit numerical value 2594 * for the least accepted version number and if the condition is not 2595 * met, this function will return a ``NULL``. Pass in 0 to skip the 2596 * version checking. 2597 */ 2598 NGHTTP3_EXTERN const nghttp3_info *nghttp3_version(int least_version); 2599 2600 /** 2601 * @function 2602 * 2603 * `nghttp3_err_is_fatal` returns nonzero if |liberr| is a fatal 2604 * error. |liberr| must be one of nghttp3 library error codes (which 2605 * is defined as NGHTTP3_ERR_* macro, such as 2606 * :macro:`NGHTTP3_ERR_NOMEM`). 2607 */ 2608 NGHTTP3_EXTERN int nghttp3_err_is_fatal(int liberr); 2609 2610 /* 2611 * Versioned function wrappers 2612 */ 2613 2614 /* 2615 * `nghttp3_settings_default` is a wrapper around 2616 * `nghttp3_settings_default_versioned` to set the correct struct 2617 * version. 2618 */ 2619 #define nghttp3_settings_default(SETTINGS) \ 2620 nghttp3_settings_default_versioned(NGHTTP3_SETTINGS_VERSION, (SETTINGS)) 2621 2622 /* 2623 * `nghttp3_conn_client_new` is a wrapper around 2624 * `nghttp3_conn_client_new_versioned` to set the correct struct 2625 * version. 2626 */ 2627 #define nghttp3_conn_client_new(PCONN, CALLBACKS, SETTINGS, MEM, USER_DATA) \ 2628 nghttp3_conn_client_new_versioned((PCONN), NGHTTP3_CALLBACKS_VERSION, \ 2629 (CALLBACKS), NGHTTP3_SETTINGS_VERSION, \ 2630 (SETTINGS), (MEM), (USER_DATA)) 2631 2632 /* 2633 * `nghttp3_conn_server_new` is a wrapper around 2634 * `nghttp3_conn_server_new_versioned` to set the correct struct 2635 * version. 2636 */ 2637 #define nghttp3_conn_server_new(PCONN, CALLBACKS, SETTINGS, MEM, USER_DATA) \ 2638 nghttp3_conn_server_new_versioned((PCONN), NGHTTP3_CALLBACKS_VERSION, \ 2639 (CALLBACKS), NGHTTP3_SETTINGS_VERSION, \ 2640 (SETTINGS), (MEM), (USER_DATA)) 2641 2642 #ifdef __cplusplus 2643 } 2644 #endif 2645 2646 #endif /* NGHTTP3_H */ 2647