nghttp2_session.h 36 KB

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