1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2012 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_SESSION_H 26 #define NGHTTP2_SESSION_H 27 28 #ifdef HAVE_CONFIG_H 29 # include <config.h> 30 #endif /* HAVE_CONFIG_H */ 31 32 #include <nghttp2/nghttp2.h> 33 #include "nghttp2_map.h" 34 #include "nghttp2_frame.h" 35 #include "nghttp2_hd.h" 36 #include "nghttp2_stream.h" 37 #include "nghttp2_outbound_item.h" 38 #include "nghttp2_int.h" 39 #include "nghttp2_buf.h" 40 #include "nghttp2_callbacks.h" 41 #include "nghttp2_mem.h" 42 43 /* The global variable for tests where we want to disable strict 44 preface handling. */ 45 extern int nghttp2_enable_strict_preface; 46 47 /* 48 * Option flags. 49 */ 50 typedef enum { 51 NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE = 1 << 0, 52 NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC = 1 << 1, 53 NGHTTP2_OPTMASK_NO_HTTP_MESSAGING = 1 << 2, 54 NGHTTP2_OPTMASK_NO_AUTO_PING_ACK = 1 << 3, 55 NGHTTP2_OPTMASK_NO_CLOSED_STREAMS = 1 << 4, 56 NGHTTP2_OPTMASK_SERVER_FALLBACK_RFC7540_PRIORITIES = 1 << 5, 57 NGHTTP2_OPTMASK_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION = 1 << 6, 58 } nghttp2_optmask; 59 60 /* 61 * bitmask for built-in type to enable the default handling for that 62 * type of the frame. 63 */ 64 typedef enum { 65 NGHTTP2_TYPEMASK_NONE = 0, 66 NGHTTP2_TYPEMASK_ALTSVC = 1 << 0, 67 NGHTTP2_TYPEMASK_ORIGIN = 1 << 1, 68 NGHTTP2_TYPEMASK_PRIORITY_UPDATE = 1 << 2 69 } nghttp2_typemask; 70 71 typedef enum { 72 NGHTTP2_OB_POP_ITEM, 73 NGHTTP2_OB_SEND_DATA, 74 NGHTTP2_OB_SEND_NO_COPY, 75 NGHTTP2_OB_SEND_CLIENT_MAGIC 76 } nghttp2_outbound_state; 77 78 typedef struct { 79 nghttp2_outbound_item *item; 80 nghttp2_bufs framebufs; 81 nghttp2_outbound_state state; 82 } nghttp2_active_outbound_item; 83 84 /* Buffer length for inbound raw byte stream used in 85 nghttp2_session_recv(). */ 86 #define NGHTTP2_INBOUND_BUFFER_LENGTH 16384 87 88 /* The default maximum number of incoming reserved streams */ 89 #define NGHTTP2_MAX_INCOMING_RESERVED_STREAMS 200 90 91 /* Even if we have less SETTINGS_MAX_CONCURRENT_STREAMS than this 92 number, we keep NGHTTP2_MIN_IDLE_STREAMS streams in idle state */ 93 #define NGHTTP2_MIN_IDLE_STREAMS 16 94 95 /* The maximum number of items in outbound queue, which is considered 96 as flooding caused by peer. All frames are not considered here. 97 We only consider PING + ACK and SETTINGS + ACK. This is because 98 they both are response to the frame initiated by peer and peer can 99 send as many of them as they want. If peer does not read network, 100 response frames are stacked up, which leads to memory exhaustion. 101 The value selected here is arbitrary, but safe value and if we have 102 these frames in this number, it is considered suspicious. */ 103 #define NGHTTP2_DEFAULT_MAX_OBQ_FLOOD_ITEM 1000 104 105 /* The default value of maximum number of concurrent streams. */ 106 #define NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS 0xffffffffu 107 108 /* Internal state when receiving incoming frame */ 109 typedef enum { 110 /* Receiving frame header */ 111 NGHTTP2_IB_READ_CLIENT_MAGIC, 112 NGHTTP2_IB_READ_FIRST_SETTINGS, 113 NGHTTP2_IB_READ_HEAD, 114 NGHTTP2_IB_READ_NBYTE, 115 NGHTTP2_IB_READ_HEADER_BLOCK, 116 NGHTTP2_IB_IGN_HEADER_BLOCK, 117 NGHTTP2_IB_IGN_PAYLOAD, 118 NGHTTP2_IB_FRAME_SIZE_ERROR, 119 NGHTTP2_IB_READ_SETTINGS, 120 NGHTTP2_IB_READ_GOAWAY_DEBUG, 121 NGHTTP2_IB_EXPECT_CONTINUATION, 122 NGHTTP2_IB_IGN_CONTINUATION, 123 NGHTTP2_IB_READ_PAD_DATA, 124 NGHTTP2_IB_READ_DATA, 125 NGHTTP2_IB_IGN_DATA, 126 NGHTTP2_IB_IGN_ALL, 127 NGHTTP2_IB_READ_ALTSVC_PAYLOAD, 128 NGHTTP2_IB_READ_ORIGIN_PAYLOAD, 129 NGHTTP2_IB_READ_EXTENSION_PAYLOAD 130 } nghttp2_inbound_state; 131 132 typedef struct { 133 nghttp2_frame frame; 134 /* Storage for extension frame payload. frame->ext.payload points 135 to this structure to avoid frequent memory allocation. */ 136 nghttp2_ext_frame_payload ext_frame_payload; 137 /* The received SETTINGS entry. For the standard settings entries, 138 we only keep the last seen value. For 139 SETTINGS_HEADER_TABLE_SIZE, we also keep minimum value in the 140 last index. */ 141 nghttp2_settings_entry *iv; 142 /* buffer pointers to small buffer, raw_sbuf */ 143 nghttp2_buf sbuf; 144 /* buffer pointers to large buffer, raw_lbuf */ 145 nghttp2_buf lbuf; 146 /* Large buffer, malloced on demand */ 147 uint8_t *raw_lbuf; 148 /* The number of entry filled in |iv| */ 149 size_t niv; 150 /* The number of entries |iv| can store. */ 151 size_t max_niv; 152 /* How many bytes we still need to receive for current frame */ 153 size_t payloadleft; 154 /* padding length for the current frame */ 155 size_t padlen; 156 nghttp2_inbound_state state; 157 /* Small fixed sized buffer. */ 158 uint8_t raw_sbuf[32]; 159 } nghttp2_inbound_frame; 160 161 typedef struct { 162 uint32_t header_table_size; 163 uint32_t enable_push; 164 uint32_t max_concurrent_streams; 165 uint32_t initial_window_size; 166 uint32_t max_frame_size; 167 uint32_t max_header_list_size; 168 uint32_t enable_connect_protocol; 169 uint32_t no_rfc7540_priorities; 170 } nghttp2_settings_storage; 171 172 typedef enum { 173 NGHTTP2_GOAWAY_NONE = 0, 174 /* Flag means that connection should be terminated after sending GOAWAY. */ 175 NGHTTP2_GOAWAY_TERM_ON_SEND = 0x1, 176 /* Flag means GOAWAY to terminate session has been sent */ 177 NGHTTP2_GOAWAY_TERM_SENT = 0x2, 178 /* Flag means GOAWAY was sent */ 179 NGHTTP2_GOAWAY_SENT = 0x4, 180 /* Flag means GOAWAY was received */ 181 NGHTTP2_GOAWAY_RECV = 0x8 182 } nghttp2_goaway_flag; 183 184 /* nghttp2_inflight_settings stores the SETTINGS entries which local 185 endpoint has sent to the remote endpoint, and has not received ACK 186 yet. */ 187 struct nghttp2_inflight_settings { 188 struct nghttp2_inflight_settings *next; 189 nghttp2_settings_entry *iv; 190 size_t niv; 191 }; 192 193 typedef struct nghttp2_inflight_settings nghttp2_inflight_settings; 194 195 struct nghttp2_session { 196 nghttp2_map /* <nghttp2_stream*> */ streams; 197 /* root of dependency tree*/ 198 nghttp2_stream root; 199 /* Queue for outbound urgent frames (PING and SETTINGS) */ 200 nghttp2_outbound_queue ob_urgent; 201 /* Queue for non-DATA frames */ 202 nghttp2_outbound_queue ob_reg; 203 /* Queue for outbound stream-creating HEADERS (request or push 204 response) frame, which are subject to 205 SETTINGS_MAX_CONCURRENT_STREAMS limit. */ 206 nghttp2_outbound_queue ob_syn; 207 /* Queues for DATA frames which is used when 208 SETTINGS_NO_RFC7540_PRIORITIES is enabled. This implements RFC 209 9218 extensible prioritization scheme. */ 210 struct { 211 nghttp2_pq ob_data; 212 } sched[NGHTTP2_EXTPRI_URGENCY_LEVELS]; 213 nghttp2_active_outbound_item aob; 214 nghttp2_inbound_frame iframe; 215 nghttp2_hd_deflater hd_deflater; 216 nghttp2_hd_inflater hd_inflater; 217 nghttp2_session_callbacks callbacks; 218 /* Memory allocator */ 219 nghttp2_mem mem; 220 void *user_data; 221 /* Points to the latest incoming closed stream. NULL if there is no 222 closed stream. Only used when session is initialized as 223 server. */ 224 nghttp2_stream *closed_stream_head; 225 /* Points to the oldest incoming closed stream. NULL if there is no 226 closed stream. Only used when session is initialized as 227 server. */ 228 nghttp2_stream *closed_stream_tail; 229 /* Points to the latest idle stream. NULL if there is no idle 230 stream. Only used when session is initialized as server .*/ 231 nghttp2_stream *idle_stream_head; 232 /* Points to the oldest idle stream. NULL if there is no idle 233 stream. Only used when session is initialized as erver. */ 234 nghttp2_stream *idle_stream_tail; 235 /* Queue of In-flight SETTINGS values. SETTINGS bearing ACK is not 236 considered as in-flight. */ 237 nghttp2_inflight_settings *inflight_settings_head; 238 /* Sequential number across all streams to process streams in 239 FIFO. */ 240 uint64_t stream_seq; 241 /* The number of outgoing streams. This will be capped by 242 remote_settings.max_concurrent_streams. */ 243 size_t num_outgoing_streams; 244 /* The number of incoming streams. This will be capped by 245 local_settings.max_concurrent_streams. */ 246 size_t num_incoming_streams; 247 /* The number of incoming reserved streams. This is the number of 248 streams in reserved (remote) state. RFC 7540 does not limit this 249 number. nghttp2 offers 250 nghttp2_option_set_max_reserved_remote_streams() to achieve this. 251 If it is used, num_incoming_streams is capped by 252 max_incoming_reserved_streams. Client application should 253 consider to set this because without that server can send 254 arbitrary number of PUSH_PROMISE, and exhaust client's memory. */ 255 size_t num_incoming_reserved_streams; 256 /* The maximum number of incoming reserved streams (reserved 257 (remote) state). RST_STREAM will be sent for the pushed stream 258 which exceeds this limit. */ 259 size_t max_incoming_reserved_streams; 260 /* The number of closed streams still kept in |streams| hash. The 261 closed streams can be accessed through single linked list 262 |closed_stream_head|. The current implementation only keeps 263 incoming streams and session is initialized as server. */ 264 size_t num_closed_streams; 265 /* The number of idle streams kept in |streams| hash. The idle 266 streams can be accessed through doubly linked list 267 |idle_stream_head|. The current implementation only keeps idle 268 streams if session is initialized as server. */ 269 size_t num_idle_streams; 270 /* The number of bytes allocated for nvbuf */ 271 size_t nvbuflen; 272 /* Counter for detecting flooding in outbound queue. If it exceeds 273 max_outbound_ack, session will be closed. */ 274 size_t obq_flood_counter_; 275 /* The maximum number of outgoing SETTINGS ACK and PING ACK in 276 outbound queue. */ 277 size_t max_outbound_ack; 278 /* The maximum length of header block to send. Calculated by the 279 same way as nghttp2_hd_deflate_bound() does. */ 280 size_t max_send_header_block_length; 281 /* The maximum number of settings accepted per SETTINGS frame. */ 282 size_t max_settings; 283 /* Next Stream ID. Made unsigned int to detect >= (1 << 31). */ 284 uint32_t next_stream_id; 285 /* The last stream ID this session initiated. For client session, 286 this is the last stream ID it has sent. For server session, it 287 is the last promised stream ID sent in PUSH_PROMISE. */ 288 int32_t last_sent_stream_id; 289 /* The largest stream ID received so far */ 290 int32_t last_recv_stream_id; 291 /* The largest stream ID which has been processed in some way. This 292 value will be used as last-stream-id when sending GOAWAY 293 frame. */ 294 int32_t last_proc_stream_id; 295 /* Counter of unique ID of PING. Wraps when it exceeds 296 NGHTTP2_MAX_UNIQUE_ID */ 297 uint32_t next_unique_id; 298 /* This is the last-stream-ID we have sent in GOAWAY */ 299 int32_t local_last_stream_id; 300 /* This is the value in GOAWAY frame received from remote endpoint. */ 301 int32_t remote_last_stream_id; 302 /* Current sender window size. This value is computed against the 303 current initial window size of remote endpoint. */ 304 int32_t remote_window_size; 305 /* Keep track of the number of bytes received without 306 WINDOW_UPDATE. This could be negative after submitting negative 307 value to WINDOW_UPDATE. */ 308 int32_t recv_window_size; 309 /* The number of bytes consumed by the application and now is 310 subject to WINDOW_UPDATE. This is only used when auto 311 WINDOW_UPDATE is turned off. */ 312 int32_t consumed_size; 313 /* The amount of recv_window_size cut using submitting negative 314 value to WINDOW_UPDATE */ 315 int32_t recv_reduction; 316 /* window size for local flow control. It is initially set to 317 NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE and could be 318 increased/decreased by submitting WINDOW_UPDATE. See 319 nghttp2_submit_window_update(). */ 320 int32_t local_window_size; 321 /* This flag is used to indicate that the local endpoint received initial 322 SETTINGS frame from the remote endpoint. */ 323 uint8_t remote_settings_received; 324 /* Settings value received from the remote endpoint. */ 325 nghttp2_settings_storage remote_settings; 326 /* Settings value of the local endpoint. */ 327 nghttp2_settings_storage local_settings; 328 /* Option flags. This is bitwise-OR of 0 or more of nghttp2_optmask. */ 329 uint32_t opt_flags; 330 /* Unacked local SETTINGS_MAX_CONCURRENT_STREAMS value. We use this 331 to refuse the incoming stream if it exceeds this value. */ 332 uint32_t pending_local_max_concurrent_stream; 333 /* The bitwise OR of zero or more of nghttp2_typemask to indicate 334 that the default handling of extension frame is enabled. */ 335 uint32_t builtin_recv_ext_types; 336 /* Unacked local ENABLE_PUSH value. We use this to refuse 337 PUSH_PROMISE before SETTINGS ACK is received. */ 338 uint8_t pending_enable_push; 339 /* Unacked local ENABLE_CONNECT_PROTOCOL value. We use this to 340 accept :protocol header field before SETTINGS_ACK is received. */ 341 uint8_t pending_enable_connect_protocol; 342 /* Unacked local SETTINGS_NO_RFC7540_PRIORITIES value, which is 343 effective before it is acknowledged. */ 344 uint8_t pending_no_rfc7540_priorities; 345 /* Turn on fallback to RFC 7540 priorities; for server use only. */ 346 uint8_t fallback_rfc7540_priorities; 347 /* Nonzero if the session is server side. */ 348 uint8_t server; 349 /* Flags indicating GOAWAY is sent and/or received. The flags are 350 composed by bitwise OR-ing nghttp2_goaway_flag. */ 351 uint8_t goaway_flags; 352 /* This flag is used to reduce excessive queuing of WINDOW_UPDATE to 353 this session. The nonzero does not necessarily mean 354 WINDOW_UPDATE is not queued. */ 355 uint8_t window_update_queued; 356 /* Bitfield of extension frame types that application is willing to 357 receive. To designate the bit of given frame type i, use 358 user_recv_ext_types[i / 8] & (1 << (i & 0x7)). First 10 frame 359 types are standard frame types and not used in this bitfield. If 360 bit is set, it indicates that incoming frame with that type is 361 passed to user defined callbacks, otherwise they are ignored. */ 362 uint8_t user_recv_ext_types[32]; 363 }; 364 365 /* Struct used when updating initial window size of each active 366 stream. */ 367 typedef struct { 368 nghttp2_session *session; 369 int32_t new_window_size, old_window_size; 370 } nghttp2_update_window_size_arg; 371 372 typedef struct { 373 nghttp2_session *session; 374 /* linked list of streams to close */ 375 nghttp2_stream *head; 376 int32_t last_stream_id; 377 /* nonzero if GOAWAY is sent to peer, which means we are going to 378 close incoming streams. zero if GOAWAY is received from peer and 379 we are going to close outgoing streams. */ 380 int incoming; 381 } nghttp2_close_stream_on_goaway_arg; 382 383 /* TODO stream timeout etc */ 384 385 /* 386 * Returns nonzero value if |stream_id| is initiated by local 387 * endpoint. 388 */ 389 int nghttp2_session_is_my_stream_id(nghttp2_session *session, 390 int32_t stream_id); 391 392 /* 393 * Adds |item| to the outbound queue in |session|. When this function 394 * succeeds, it takes ownership of |item|. So caller must not free it 395 * on success. 396 * 397 * This function returns 0 if it succeeds, or one of the following 398 * negative error codes: 399 * 400 * NGHTTP2_ERR_NOMEM 401 * Out of memory. 402 * NGHTTP2_ERR_STREAM_CLOSED 403 * Stream already closed (DATA and PUSH_PROMISE frame only) 404 */ 405 int nghttp2_session_add_item(nghttp2_session *session, 406 nghttp2_outbound_item *item); 407 408 /* 409 * Adds RST_STREAM frame for the stream |stream_id| with the error 410 * code |error_code|. This is a convenient function built on top of 411 * nghttp2_session_add_frame() to add RST_STREAM easily. 412 * 413 * This function simply returns 0 without adding RST_STREAM frame if 414 * given stream is in NGHTTP2_STREAM_CLOSING state, because multiple 415 * RST_STREAM for a stream is redundant. 416 * 417 * This function returns 0 if it succeeds, or one of the following 418 * negative error codes: 419 * 420 * NGHTTP2_ERR_NOMEM 421 * Out of memory. 422 */ 423 int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id, 424 uint32_t error_code); 425 426 /* 427 * Adds PING frame. This is a convenient function built on top of 428 * nghttp2_session_add_frame() to add PING easily. 429 * 430 * If the |opaque_data| is not NULL, it must point to 8 bytes memory 431 * region of data. The data pointed by |opaque_data| is copied. It can 432 * be NULL. In this case, 8 bytes NULL is used. 433 * 434 * This function returns 0 if it succeeds, or one of the following 435 * negative error codes: 436 * 437 * NGHTTP2_ERR_NOMEM 438 * Out of memory. 439 * NGHTTP2_ERR_FLOODED 440 * There are too many items in outbound queue; this only happens 441 * if NGHTTP2_FLAG_ACK is set in |flags| 442 */ 443 int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags, 444 const uint8_t *opaque_data); 445 446 /* 447 * Adds GOAWAY frame with the last-stream-ID |last_stream_id| and the 448 * error code |error_code|. This is a convenient function built on top 449 * of nghttp2_session_add_frame() to add GOAWAY easily. The 450 * |aux_flags| are bitwise-OR of one or more of 451 * nghttp2_goaway_aux_flag. 452 * 453 * This function returns 0 if it succeeds, or one of the following 454 * negative error codes: 455 * 456 * NGHTTP2_ERR_NOMEM 457 * Out of memory. 458 * NGHTTP2_ERR_INVALID_ARGUMENT 459 * The |opaque_data_len| is too large. 460 */ 461 int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id, 462 uint32_t error_code, const uint8_t *opaque_data, 463 size_t opaque_data_len, uint8_t aux_flags); 464 465 /* 466 * Adds WINDOW_UPDATE frame with stream ID |stream_id| and 467 * window-size-increment |window_size_increment|. This is a convenient 468 * function built on top of nghttp2_session_add_frame() to add 469 * WINDOW_UPDATE easily. 470 * 471 * This function returns 0 if it succeeds, or one of the following 472 * negative error codes: 473 * 474 * NGHTTP2_ERR_NOMEM 475 * Out of memory. 476 */ 477 int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags, 478 int32_t stream_id, 479 int32_t window_size_increment); 480 481 /* 482 * Adds SETTINGS frame. 483 * 484 * This function returns 0 if it succeeds, or one of the following 485 * negative error codes: 486 * 487 * NGHTTP2_ERR_NOMEM 488 * Out of memory. 489 * NGHTTP2_ERR_FLOODED 490 * There are too many items in outbound queue; this only happens 491 * if NGHTTP2_FLAG_ACK is set in |flags| 492 */ 493 int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags, 494 const nghttp2_settings_entry *iv, size_t niv); 495 496 /* 497 * Creates new stream in |session| with stream ID |stream_id|, 498 * priority |pri_spec| and flags |flags|. The |flags| is bitwise OR 499 * of nghttp2_stream_flag. Since this function is called when initial 500 * HEADERS is sent or received, these flags are taken from it. The 501 * state of stream is set to |initial_state|. The |stream_user_data| 502 * is a pointer to the arbitrary user supplied data to be associated 503 * to this stream. 504 * 505 * If |initial_state| is NGHTTP2_STREAM_RESERVED, this function sets 506 * NGHTTP2_STREAM_FLAG_PUSH flag set. 507 * 508 * This function returns a pointer to created new stream object, or 509 * NULL. 510 * 511 * This function adjusts neither the number of closed streams or idle 512 * streams. The caller should manually call 513 * nghttp2_session_adjust_closed_stream() or 514 * nghttp2_session_adjust_idle_stream() respectively. 515 */ 516 nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session, 517 int32_t stream_id, uint8_t flags, 518 nghttp2_priority_spec *pri_spec, 519 nghttp2_stream_state initial_state, 520 void *stream_user_data); 521 522 /* 523 * Closes stream whose stream ID is |stream_id|. The reason of closure 524 * is indicated by the |error_code|. When closing the stream, 525 * on_stream_close_callback will be called. 526 * 527 * If the session is initialized as server and |stream| is incoming 528 * stream, stream is just marked closed and this function calls 529 * nghttp2_session_keep_closed_stream() with |stream|. Otherwise, 530 * |stream| will be deleted from memory. 531 * 532 * This function returns 0 if it succeeds, or one the following 533 * negative error codes: 534 * 535 * NGHTTP2_ERR_NOMEM 536 * Out of memory 537 * NGHTTP2_ERR_INVALID_ARGUMENT 538 * The specified stream does not exist. 539 * NGHTTP2_ERR_CALLBACK_FAILURE 540 * The callback function failed. 541 */ 542 int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id, 543 uint32_t error_code); 544 545 /* 546 * Deletes |stream| from memory. After this function returns, stream 547 * cannot be accessed. 548 * 549 * This function returns 0 if it succeeds, or one the following 550 * negative error codes: 551 * 552 * NGHTTP2_ERR_NOMEM 553 * Out of memory 554 */ 555 int nghttp2_session_destroy_stream(nghttp2_session *session, 556 nghttp2_stream *stream); 557 558 /* 559 * Tries to keep incoming closed stream |stream|. Due to the 560 * limitation of maximum number of streams in memory, |stream| is not 561 * closed and just deleted from memory (see 562 * nghttp2_session_destroy_stream). 563 */ 564 void nghttp2_session_keep_closed_stream(nghttp2_session *session, 565 nghttp2_stream *stream); 566 567 /* 568 * Appends |stream| to linked list |session->idle_stream_head|. We 569 * apply fixed limit for list size. To fit into that limit, one or 570 * more oldest streams are removed from list as necessary. 571 */ 572 void nghttp2_session_keep_idle_stream(nghttp2_session *session, 573 nghttp2_stream *stream); 574 575 /* 576 * Detaches |stream| from idle streams linked list. 577 */ 578 void nghttp2_session_detach_idle_stream(nghttp2_session *session, 579 nghttp2_stream *stream); 580 581 /* 582 * Deletes closed stream to ensure that number of incoming streams 583 * including active and closed is in the maximum number of allowed 584 * stream. 585 * 586 * This function returns 0 if it succeeds, or one the following 587 * negative error codes: 588 * 589 * NGHTTP2_ERR_NOMEM 590 * Out of memory 591 */ 592 int nghttp2_session_adjust_closed_stream(nghttp2_session *session); 593 594 /* 595 * Deletes idle stream to ensure that number of idle streams is in 596 * certain limit. 597 * 598 * This function returns 0 if it succeeds, or one the following 599 * negative error codes: 600 * 601 * NGHTTP2_ERR_NOMEM 602 * Out of memory 603 */ 604 int nghttp2_session_adjust_idle_stream(nghttp2_session *session); 605 606 /* 607 * If further receptions and transmissions over the stream |stream_id| 608 * are disallowed, close the stream with error code NGHTTP2_NO_ERROR. 609 * 610 * This function returns 0 if it 611 * succeeds, or one of the following negative error codes: 612 * 613 * NGHTTP2_ERR_INVALID_ARGUMENT 614 * The specified stream does not exist. 615 */ 616 int nghttp2_session_close_stream_if_shut_rdwr(nghttp2_session *session, 617 nghttp2_stream *stream); 618 619 int nghttp2_session_on_request_headers_received(nghttp2_session *session, 620 nghttp2_frame *frame); 621 622 int nghttp2_session_on_response_headers_received(nghttp2_session *session, 623 nghttp2_frame *frame, 624 nghttp2_stream *stream); 625 626 int nghttp2_session_on_push_response_headers_received(nghttp2_session *session, 627 nghttp2_frame *frame, 628 nghttp2_stream *stream); 629 630 /* 631 * Called when HEADERS is received, assuming |frame| is properly 632 * initialized. This function does first validate received frame and 633 * then open stream and call callback functions. 634 * 635 * This function returns 0 if it succeeds, or one of the following 636 * negative error codes: 637 * 638 * NGHTTP2_ERR_NOMEM 639 * Out of memory. 640 * NGHTTP2_ERR_IGN_HEADER_BLOCK 641 * Frame was rejected and header block must be decoded but 642 * result must be ignored. 643 * NGHTTP2_ERR_CALLBACK_FAILURE 644 * The read_callback failed 645 */ 646 int nghttp2_session_on_headers_received(nghttp2_session *session, 647 nghttp2_frame *frame, 648 nghttp2_stream *stream); 649 650 /* 651 * Called when PRIORITY is received, assuming |frame| is properly 652 * initialized. 653 * 654 * This function returns 0 if it succeeds, or one of the following 655 * negative error codes: 656 * 657 * NGHTTP2_ERR_NOMEM 658 * Out of memory. 659 * NGHTTP2_ERR_CALLBACK_FAILURE 660 * The read_callback failed 661 */ 662 int nghttp2_session_on_priority_received(nghttp2_session *session, 663 nghttp2_frame *frame); 664 665 /* 666 * Called when RST_STREAM is received, assuming |frame| is properly 667 * initialized. 668 * 669 * This function returns 0 if it succeeds, or one the following 670 * negative error codes: 671 * 672 * NGHTTP2_ERR_NOMEM 673 * Out of memory 674 * NGHTTP2_ERR_CALLBACK_FAILURE 675 * The read_callback failed 676 */ 677 int nghttp2_session_on_rst_stream_received(nghttp2_session *session, 678 nghttp2_frame *frame); 679 680 /* 681 * Called when SETTINGS is received, assuming |frame| is properly 682 * initialized. If |noack| is non-zero, SETTINGS with ACK will not be 683 * submitted. If |frame| has NGHTTP2_FLAG_ACK flag set, no SETTINGS 684 * with ACK will not be submitted regardless of |noack|. 685 * 686 * This function returns 0 if it succeeds, or one the following 687 * negative error codes: 688 * 689 * NGHTTP2_ERR_NOMEM 690 * Out of memory 691 * NGHTTP2_ERR_CALLBACK_FAILURE 692 * The read_callback failed 693 * NGHTTP2_ERR_FLOODED 694 * There are too many items in outbound queue, and this is most 695 * likely caused by misbehaviour of peer. 696 */ 697 int nghttp2_session_on_settings_received(nghttp2_session *session, 698 nghttp2_frame *frame, int noack); 699 700 /* 701 * Called when PUSH_PROMISE is received, assuming |frame| is properly 702 * initialized. 703 * 704 * This function returns 0 if it succeeds, or one of the following 705 * negative error codes: 706 * 707 * NGHTTP2_ERR_NOMEM 708 * Out of memory. 709 * NGHTTP2_ERR_IGN_HEADER_BLOCK 710 * Frame was rejected and header block must be decoded but 711 * result must be ignored. 712 * NGHTTP2_ERR_CALLBACK_FAILURE 713 * The read_callback failed 714 */ 715 int nghttp2_session_on_push_promise_received(nghttp2_session *session, 716 nghttp2_frame *frame); 717 718 /* 719 * Called when PING is received, assuming |frame| is properly 720 * initialized. 721 * 722 * This function returns 0 if it succeeds, or one of the following 723 * negative error codes: 724 * 725 * NGHTTP2_ERR_NOMEM 726 * Out of memory. 727 * NGHTTP2_ERR_CALLBACK_FAILURE 728 * The callback function failed. 729 * NGHTTP2_ERR_FLOODED 730 * There are too many items in outbound queue, and this is most 731 * likely caused by misbehaviour of peer. 732 */ 733 int nghttp2_session_on_ping_received(nghttp2_session *session, 734 nghttp2_frame *frame); 735 736 /* 737 * Called when GOAWAY is received, assuming |frame| is properly 738 * initialized. 739 * 740 * This function returns 0 if it succeeds, or one of the following 741 * negative error codes: 742 * 743 * NGHTTP2_ERR_NOMEM 744 * Out of memory. 745 * NGHTTP2_ERR_CALLBACK_FAILURE 746 * The callback function failed. 747 */ 748 int nghttp2_session_on_goaway_received(nghttp2_session *session, 749 nghttp2_frame *frame); 750 751 /* 752 * Called when WINDOW_UPDATE is received, assuming |frame| is properly 753 * initialized. 754 * 755 * This function returns 0 if it succeeds, or one of the following 756 * negative error codes: 757 * 758 * NGHTTP2_ERR_NOMEM 759 * Out of memory. 760 * NGHTTP2_ERR_CALLBACK_FAILURE 761 * The callback function failed. 762 */ 763 int nghttp2_session_on_window_update_received(nghttp2_session *session, 764 nghttp2_frame *frame); 765 766 /* 767 * Called when ALTSVC is received, assuming |frame| is properly 768 * initialized. 769 * 770 * This function returns 0 if it succeeds, or one of the following 771 * negative error codes: 772 * 773 * NGHTTP2_ERR_CALLBACK_FAILURE 774 * The callback function failed. 775 */ 776 int nghttp2_session_on_altsvc_received(nghttp2_session *session, 777 nghttp2_frame *frame); 778 779 /* 780 * Called when ORIGIN is received, assuming |frame| is properly 781 * initialized. 782 * 783 * This function returns 0 if it succeeds, or one of the following 784 * negative error codes: 785 * 786 * NGHTTP2_ERR_CALLBACK_FAILURE 787 * The callback function failed. 788 */ 789 int nghttp2_session_on_origin_received(nghttp2_session *session, 790 nghttp2_frame *frame); 791 792 /* 793 * Called when PRIORITY_UPDATE is received, assuming |frame| is 794 * properly initialized. 795 * 796 * This function returns 0 if it succeeds, or one of the following 797 * negative error codes: 798 * 799 * NGHTTP2_ERR_CALLBACK_FAILURE 800 * The callback function failed. 801 */ 802 int nghttp2_session_on_priority_update_received(nghttp2_session *session, 803 nghttp2_frame *frame); 804 805 /* 806 * Called when DATA is received, assuming |frame| is properly 807 * initialized. 808 * 809 * This function returns 0 if it succeeds, or one of the following 810 * negative error codes: 811 * 812 * NGHTTP2_ERR_NOMEM 813 * Out of memory. 814 * NGHTTP2_ERR_CALLBACK_FAILURE 815 * The callback function failed. 816 */ 817 int nghttp2_session_on_data_received(nghttp2_session *session, 818 nghttp2_frame *frame); 819 820 /* 821 * Returns nghttp2_stream* object whose stream ID is |stream_id|. It 822 * could be NULL if such stream does not exist. This function returns 823 * NULL if stream is marked as closed. 824 */ 825 nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session, 826 int32_t stream_id); 827 828 /* 829 * This function behaves like nghttp2_session_get_stream(), but it 830 * returns stream object even if it is marked as closed or in 831 * NGHTTP2_STREAM_IDLE state. 832 */ 833 nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session, 834 int32_t stream_id); 835 836 /* 837 * Packs DATA frame |frame| in wire frame format and stores it in 838 * |bufs|. Payload will be read using |aux_data->data_prd|. The 839 * length of payload is at most |datamax| bytes. 840 * 841 * This function returns 0 if it succeeds, or one of the following 842 * negative error codes: 843 * 844 * NGHTTP2_ERR_DEFERRED 845 * The DATA frame is postponed. 846 * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE 847 * The read_callback failed (stream error). 848 * NGHTTP2_ERR_NOMEM 849 * Out of memory. 850 * NGHTTP2_ERR_CALLBACK_FAILURE 851 * The read_callback failed (session error). 852 */ 853 int nghttp2_session_pack_data(nghttp2_session *session, nghttp2_bufs *bufs, 854 size_t datamax, nghttp2_frame *frame, 855 nghttp2_data_aux_data *aux_data, 856 nghttp2_stream *stream); 857 858 /* 859 * Pops and returns next item to send. If there is no such item, 860 * returns NULL. This function takes into account max concurrent 861 * streams. That means if session->ob_syn has item and max concurrent 862 * streams is reached, the even if other queues contain items, then 863 * this function returns NULL. 864 */ 865 nghttp2_outbound_item * 866 nghttp2_session_pop_next_ob_item(nghttp2_session *session); 867 868 /* 869 * Returns next item to send. If there is no such item, this function 870 * returns NULL. This function takes into account max concurrent 871 * streams. That means if session->ob_syn has item and max concurrent 872 * streams is reached, the even if other queues contain items, then 873 * this function returns NULL. 874 */ 875 nghttp2_outbound_item * 876 nghttp2_session_get_next_ob_item(nghttp2_session *session); 877 878 /* 879 * Updates local settings with the |iv|. The number of elements in the 880 * array pointed by the |iv| is given by the |niv|. This function 881 * assumes that the all settings_id member in |iv| are in range 1 to 882 * NGHTTP2_SETTINGS_MAX, inclusive. 883 * 884 * While updating individual stream's local window size, if the window 885 * size becomes strictly larger than NGHTTP2_MAX_WINDOW_SIZE, 886 * RST_STREAM is issued against such a stream. 887 * 888 * This function returns 0 if it succeeds, or one of the following 889 * negative error codes: 890 * 891 * NGHTTP2_ERR_NOMEM 892 * Out of memory 893 */ 894 int nghttp2_session_update_local_settings(nghttp2_session *session, 895 nghttp2_settings_entry *iv, 896 size_t niv); 897 898 /* 899 * Re-prioritize |stream|. The new priority specification is 900 * |pri_spec|. Caller must ensure that stream->hd.stream_id != 901 * pri_spec->stream_id. 902 * 903 * This function does not adjust the number of idle streams. The 904 * caller should call nghttp2_session_adjust_idle_stream() later. 905 * 906 * This function returns 0 if it succeeds, or one of the following 907 * negative error codes: 908 * 909 * NGHTTP2_ERR_NOMEM 910 * Out of memory 911 */ 912 int nghttp2_session_reprioritize_stream(nghttp2_session *session, 913 nghttp2_stream *stream, 914 const nghttp2_priority_spec *pri_spec); 915 916 /* 917 * Terminates current |session| with the |error_code|. The |reason| 918 * is NULL-terminated debug string. 919 * 920 * This function returns 0 if it succeeds, or one of the following 921 * negative error codes: 922 * 923 * NGHTTP2_ERR_NOMEM 924 * Out of memory. 925 * NGHTTP2_ERR_INVALID_ARGUMENT 926 * The |reason| is too long. 927 */ 928 int nghttp2_session_terminate_session_with_reason(nghttp2_session *session, 929 uint32_t error_code, 930 const char *reason); 931 932 /* 933 * Accumulates received bytes |delta_size| for connection-level flow 934 * control and decides whether to send WINDOW_UPDATE to the 935 * connection. If NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE is set, 936 * WINDOW_UPDATE will not be sent. 937 * 938 * This function returns 0 if it succeeds, or one of the following 939 * negative error codes: 940 * 941 * NGHTTP2_ERR_NOMEM 942 * Out of memory. 943 */ 944 int nghttp2_session_update_recv_connection_window_size(nghttp2_session *session, 945 size_t delta_size); 946 947 /* 948 * Accumulates received bytes |delta_size| for stream-level flow 949 * control and decides whether to send WINDOW_UPDATE to that stream. 950 * If NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE is set, WINDOW_UPDATE will not 951 * be sent. 952 * 953 * This function returns 0 if it succeeds, or one of the following 954 * negative error codes: 955 * 956 * NGHTTP2_ERR_NOMEM 957 * Out of memory. 958 */ 959 int nghttp2_session_update_recv_stream_window_size(nghttp2_session *session, 960 nghttp2_stream *stream, 961 size_t delta_size, 962 int send_window_update); 963 964 #endif /* NGHTTP2_SESSION_H */ 965