nghttp2_session.h 32 KB

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