nghttp2_session.h 30 KB

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