nghttp2_session.h 34 KB

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