nghttp2_session.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. /* Next Stream ID. Made unsigned int to detect >= (1 << 31). */
  252. uint32_t next_stream_id;
  253. /* The last stream ID this session initiated. For client session,
  254. this is the last stream ID it has sent. For server session, it
  255. is the last promised stream ID sent in PUSH_PROMISE. */
  256. int32_t last_sent_stream_id;
  257. /* The largest stream ID received so far */
  258. int32_t last_recv_stream_id;
  259. /* The largest stream ID which has been processed in some way. This
  260. value will be used as last-stream-id when sending GOAWAY
  261. frame. */
  262. int32_t last_proc_stream_id;
  263. /* Counter of unique ID of PING. Wraps when it exceeds
  264. NGHTTP2_MAX_UNIQUE_ID */
  265. uint32_t next_unique_id;
  266. /* This is the last-stream-ID we have sent in GOAWAY */
  267. int32_t local_last_stream_id;
  268. /* This is the value in GOAWAY frame received from remote endpoint. */
  269. int32_t remote_last_stream_id;
  270. /* Current sender window size. This value is computed against the
  271. current initial window size of remote endpoint. */
  272. int32_t remote_window_size;
  273. /* Keep track of the number of bytes received without
  274. WINDOW_UPDATE. This could be negative after submitting negative
  275. value to WINDOW_UPDATE. */
  276. int32_t recv_window_size;
  277. /* The number of bytes consumed by the application and now is
  278. subject to WINDOW_UPDATE. This is only used when auto
  279. WINDOW_UPDATE is turned off. */
  280. int32_t consumed_size;
  281. /* The amount of recv_window_size cut using submitting negative
  282. value to WINDOW_UPDATE */
  283. int32_t recv_reduction;
  284. /* window size for local flow control. It is initially set to
  285. NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE and could be
  286. increased/decreased by submitting WINDOW_UPDATE. See
  287. nghttp2_submit_window_update(). */
  288. int32_t local_window_size;
  289. /* This flag is used to indicate that the local endpoint received initial
  290. SETTINGS frame from the remote endpoint. */
  291. uint8_t remote_settings_received;
  292. /* Settings value received from the remote endpoint. */
  293. nghttp2_settings_storage remote_settings;
  294. /* Settings value of the local endpoint. */
  295. nghttp2_settings_storage local_settings;
  296. /* Option flags. This is bitwise-OR of 0 or more of nghttp2_optmask. */
  297. uint32_t opt_flags;
  298. /* Unacked local SETTINGS_MAX_CONCURRENT_STREAMS value. We use this
  299. to refuse the incoming stream if it exceeds this value. */
  300. uint32_t pending_local_max_concurrent_stream;
  301. /* The bitwise OR of zero or more of nghttp2_typemask to indicate
  302. that the default handling of extension frame is enabled. */
  303. uint32_t builtin_recv_ext_types;
  304. /* Unacked local ENABLE_PUSH value. We use this to refuse
  305. PUSH_PROMISE before SETTINGS ACK is received. */
  306. uint8_t pending_enable_push;
  307. /* Unacked local ENABLE_CONNECT_PROTOCOL value. We use this to
  308. accept :protocol header field before SETTINGS_ACK is received. */
  309. uint8_t pending_enable_connect_protocol;
  310. /* Nonzero if the session is server side. */
  311. uint8_t server;
  312. /* Flags indicating GOAWAY is sent and/or received. The flags are
  313. composed by bitwise OR-ing nghttp2_goaway_flag. */
  314. uint8_t goaway_flags;
  315. /* This flag is used to reduce excessive queuing of WINDOW_UPDATE to
  316. this session. The nonzero does not necessarily mean
  317. WINDOW_UPDATE is not queued. */
  318. uint8_t window_update_queued;
  319. /* Bitfield of extension frame types that application is willing to
  320. receive. To designate the bit of given frame type i, use
  321. user_recv_ext_types[i / 8] & (1 << (i & 0x7)). First 10 frame
  322. types are standard frame types and not used in this bitfield. If
  323. bit is set, it indicates that incoming frame with that type is
  324. passed to user defined callbacks, otherwise they are ignored. */
  325. uint8_t user_recv_ext_types[32];
  326. };
  327. /* Struct used when updating initial window size of each active
  328. stream. */
  329. typedef struct {
  330. nghttp2_session *session;
  331. int32_t new_window_size, old_window_size;
  332. } nghttp2_update_window_size_arg;
  333. typedef struct {
  334. nghttp2_session *session;
  335. /* linked list of streams to close */
  336. nghttp2_stream *head;
  337. int32_t last_stream_id;
  338. /* nonzero if GOAWAY is sent to peer, which means we are going to
  339. close incoming streams. zero if GOAWAY is received from peer and
  340. we are going to close outgoing streams. */
  341. int incoming;
  342. } nghttp2_close_stream_on_goaway_arg;
  343. /* TODO stream timeout etc */
  344. /*
  345. * Returns nonzero value if |stream_id| is initiated by local
  346. * endpoint.
  347. */
  348. int nghttp2_session_is_my_stream_id(nghttp2_session *session,
  349. int32_t stream_id);
  350. /*
  351. * Adds |item| to the outbound queue in |session|. When this function
  352. * succeeds, it takes ownership of |item|. So caller must not free it
  353. * on success.
  354. *
  355. * This function returns 0 if it succeeds, or one of the following
  356. * negative error codes:
  357. *
  358. * NGHTTP2_ERR_NOMEM
  359. * Out of memory.
  360. * NGHTTP2_ERR_STREAM_CLOSED
  361. * Stream already closed (DATA and PUSH_PROMISE frame only)
  362. */
  363. int nghttp2_session_add_item(nghttp2_session *session,
  364. nghttp2_outbound_item *item);
  365. /*
  366. * Adds RST_STREAM frame for the stream |stream_id| with the error
  367. * code |error_code|. This is a convenient function built on top of
  368. * nghttp2_session_add_frame() to add RST_STREAM easily.
  369. *
  370. * This function simply returns 0 without adding RST_STREAM frame if
  371. * given stream is in NGHTTP2_STREAM_CLOSING state, because multiple
  372. * RST_STREAM for a stream is redundant.
  373. *
  374. * This function returns 0 if it succeeds, or one of the following
  375. * negative error codes:
  376. *
  377. * NGHTTP2_ERR_NOMEM
  378. * Out of memory.
  379. */
  380. int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
  381. uint32_t error_code);
  382. /*
  383. * Adds PING frame. This is a convenient functin built on top of
  384. * nghttp2_session_add_frame() to add PING easily.
  385. *
  386. * If the |opaque_data| is not NULL, it must point to 8 bytes memory
  387. * region of data. The data pointed by |opaque_data| is copied. It can
  388. * be NULL. In this case, 8 bytes NULL is used.
  389. *
  390. * This function returns 0 if it succeeds, or one of the following
  391. * negative error codes:
  392. *
  393. * NGHTTP2_ERR_NOMEM
  394. * Out of memory.
  395. * NGHTTP2_ERR_FLOODED
  396. * There are too many items in outbound queue; this only happens
  397. * if NGHTTP2_FLAG_ACK is set in |flags|
  398. */
  399. int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
  400. const uint8_t *opaque_data);
  401. /*
  402. * Adds GOAWAY frame with the last-stream-ID |last_stream_id| and the
  403. * error code |error_code|. This is a convenient function built on top
  404. * of nghttp2_session_add_frame() to add GOAWAY easily. The
  405. * |aux_flags| are bitwise-OR of one or more of
  406. * nghttp2_goaway_aux_flag.
  407. *
  408. * This function returns 0 if it succeeds, or one of the following
  409. * negative error codes:
  410. *
  411. * NGHTTP2_ERR_NOMEM
  412. * Out of memory.
  413. * NGHTTP2_ERR_INVALID_ARGUMENT
  414. * The |opaque_data_len| is too large.
  415. */
  416. int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id,
  417. uint32_t error_code, const uint8_t *opaque_data,
  418. size_t opaque_data_len, uint8_t aux_flags);
  419. /*
  420. * Adds WINDOW_UPDATE frame with stream ID |stream_id| and
  421. * window-size-increment |window_size_increment|. This is a convenient
  422. * function built on top of nghttp2_session_add_frame() to add
  423. * WINDOW_UPDATE easily.
  424. *
  425. * This function returns 0 if it succeeds, or one of the following
  426. * negative error codes:
  427. *
  428. * NGHTTP2_ERR_NOMEM
  429. * Out of memory.
  430. */
  431. int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
  432. int32_t stream_id,
  433. int32_t window_size_increment);
  434. /*
  435. * Adds SETTINGS frame.
  436. *
  437. * This function returns 0 if it succeeds, or one of the following
  438. * negative error codes:
  439. *
  440. * NGHTTP2_ERR_NOMEM
  441. * Out of memory.
  442. * NGHTTP2_ERR_FLOODED
  443. * There are too many items in outbound queue; this only happens
  444. * if NGHTTP2_FLAG_ACK is set in |flags|
  445. */
  446. int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
  447. const nghttp2_settings_entry *iv, size_t niv);
  448. /*
  449. * Creates new stream in |session| with stream ID |stream_id|,
  450. * priority |pri_spec| and flags |flags|. The |flags| is bitwise OR
  451. * of nghttp2_stream_flag. Since this function is called when initial
  452. * HEADERS is sent or received, these flags are taken from it. The
  453. * state of stream is set to |initial_state|. The |stream_user_data|
  454. * is a pointer to the arbitrary user supplied data to be associated
  455. * to this stream.
  456. *
  457. * If |initial_state| is NGHTTP2_STREAM_RESERVED, this function sets
  458. * NGHTTP2_STREAM_FLAG_PUSH flag set.
  459. *
  460. * This function returns a pointer to created new stream object, or
  461. * NULL.
  462. *
  463. * This function adjusts neither the number of closed streams or idle
  464. * streams. The caller should manually call
  465. * nghttp2_session_adjust_closed_stream() or
  466. * nghttp2_session_adjust_idle_stream() respectively.
  467. */
  468. nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
  469. int32_t stream_id, uint8_t flags,
  470. nghttp2_priority_spec *pri_spec,
  471. nghttp2_stream_state initial_state,
  472. void *stream_user_data);
  473. /*
  474. * Closes stream whose stream ID is |stream_id|. The reason of closure
  475. * is indicated by the |error_code|. When closing the stream,
  476. * on_stream_close_callback will be called.
  477. *
  478. * If the session is initialized as server and |stream| is incoming
  479. * stream, stream is just marked closed and this function calls
  480. * nghttp2_session_keep_closed_stream() with |stream|. Otherwise,
  481. * |stream| will be deleted from memory.
  482. *
  483. * This function returns 0 if it succeeds, or one the following
  484. * negative error codes:
  485. *
  486. * NGHTTP2_ERR_NOMEM
  487. * Out of memory
  488. * NGHTTP2_ERR_INVALID_ARGUMENT
  489. * The specified stream does not exist.
  490. * NGHTTP2_ERR_CALLBACK_FAILURE
  491. * The callback function failed.
  492. */
  493. int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id,
  494. uint32_t error_code);
  495. /*
  496. * Deletes |stream| from memory. After this function returns, stream
  497. * cannot be accessed.
  498. *
  499. * This function returns 0 if it succeeds, or one the following
  500. * negative error codes:
  501. *
  502. * NGHTTP2_ERR_NOMEM
  503. * Out of memory
  504. */
  505. int nghttp2_session_destroy_stream(nghttp2_session *session,
  506. nghttp2_stream *stream);
  507. /*
  508. * Tries to keep incoming closed stream |stream|. Due to the
  509. * limitation of maximum number of streams in memory, |stream| is not
  510. * closed and just deleted from memory (see
  511. * nghttp2_session_destroy_stream).
  512. */
  513. void nghttp2_session_keep_closed_stream(nghttp2_session *session,
  514. nghttp2_stream *stream);
  515. /*
  516. * Appends |stream| to linked list |session->idle_stream_head|. We
  517. * apply fixed limit for list size. To fit into that limit, one or
  518. * more oldest streams are removed from list as necessary.
  519. */
  520. void nghttp2_session_keep_idle_stream(nghttp2_session *session,
  521. nghttp2_stream *stream);
  522. /*
  523. * Detaches |stream| from idle streams linked list.
  524. */
  525. void nghttp2_session_detach_idle_stream(nghttp2_session *session,
  526. nghttp2_stream *stream);
  527. /*
  528. * Deletes closed stream to ensure that number of incoming streams
  529. * including active and closed is in the maximum number of allowed
  530. * stream.
  531. *
  532. * This function returns 0 if it succeeds, or one the following
  533. * negative error codes:
  534. *
  535. * NGHTTP2_ERR_NOMEM
  536. * Out of memory
  537. */
  538. int nghttp2_session_adjust_closed_stream(nghttp2_session *session);
  539. /*
  540. * Deletes idle stream to ensure that number of idle streams is in
  541. * certain limit.
  542. *
  543. * This function returns 0 if it succeeds, or one the following
  544. * negative error codes:
  545. *
  546. * NGHTTP2_ERR_NOMEM
  547. * Out of memory
  548. */
  549. int nghttp2_session_adjust_idle_stream(nghttp2_session *session);
  550. /*
  551. * If further receptions and transmissions over the stream |stream_id|
  552. * are disallowed, close the stream with error code NGHTTP2_NO_ERROR.
  553. *
  554. * This function returns 0 if it
  555. * succeeds, or one of the following negative error codes:
  556. *
  557. * NGHTTP2_ERR_INVALID_ARGUMENT
  558. * The specified stream does not exist.
  559. */
  560. int nghttp2_session_close_stream_if_shut_rdwr(nghttp2_session *session,
  561. nghttp2_stream *stream);
  562. int nghttp2_session_on_request_headers_received(nghttp2_session *session,
  563. nghttp2_frame *frame);
  564. int nghttp2_session_on_response_headers_received(nghttp2_session *session,
  565. nghttp2_frame *frame,
  566. nghttp2_stream *stream);
  567. int nghttp2_session_on_push_response_headers_received(nghttp2_session *session,
  568. nghttp2_frame *frame,
  569. nghttp2_stream *stream);
  570. /*
  571. * Called when HEADERS is received, assuming |frame| is properly
  572. * initialized. This function does first validate received frame and
  573. * then open stream and call callback functions.
  574. *
  575. * This function returns 0 if it succeeds, or one of the following
  576. * negative error codes:
  577. *
  578. * NGHTTP2_ERR_NOMEM
  579. * Out of memory.
  580. * NGHTTP2_ERR_IGN_HEADER_BLOCK
  581. * Frame was rejected and header block must be decoded but
  582. * result must be ignored.
  583. * NGHTTP2_ERR_CALLBACK_FAILURE
  584. * The read_callback failed
  585. */
  586. int nghttp2_session_on_headers_received(nghttp2_session *session,
  587. nghttp2_frame *frame,
  588. nghttp2_stream *stream);
  589. /*
  590. * Called when PRIORITY is received, assuming |frame| is properly
  591. * initialized.
  592. *
  593. * This function returns 0 if it succeeds, or one of 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. */
  601. int nghttp2_session_on_priority_received(nghttp2_session *session,
  602. nghttp2_frame *frame);
  603. /*
  604. * Called when RST_STREAM is received, assuming |frame| is properly
  605. * initialized.
  606. *
  607. * This function returns 0 if it succeeds, or one the following
  608. * negative error codes:
  609. *
  610. * NGHTTP2_ERR_NOMEM
  611. * Out of memory
  612. * NGHTTP2_ERR_CALLBACK_FAILURE
  613. * The read_callback failed
  614. */
  615. int nghttp2_session_on_rst_stream_received(nghttp2_session *session,
  616. nghttp2_frame *frame);
  617. /*
  618. * Called when SETTINGS is received, assuming |frame| is properly
  619. * initialized. If |noack| is non-zero, SETTINGS with ACK will not be
  620. * submitted. If |frame| has NGHTTP2_FLAG_ACK flag set, no SETTINGS
  621. * with ACK will not be submitted regardless of |noack|.
  622. *
  623. * This function returns 0 if it succeeds, or one the following
  624. * negative error codes:
  625. *
  626. * NGHTTP2_ERR_NOMEM
  627. * Out of memory
  628. * NGHTTP2_ERR_CALLBACK_FAILURE
  629. * The read_callback failed
  630. * NGHTTP2_ERR_FLOODED
  631. * There are too many items in outbound queue, and this is most
  632. * likely caused by misbehaviour of peer.
  633. */
  634. int nghttp2_session_on_settings_received(nghttp2_session *session,
  635. nghttp2_frame *frame, int noack);
  636. /*
  637. * Called when PUSH_PROMISE is received, assuming |frame| is properly
  638. * initialized.
  639. *
  640. * This function returns 0 if it succeeds, or one of the following
  641. * negative error codes:
  642. *
  643. * NGHTTP2_ERR_NOMEM
  644. * Out of memory.
  645. * NGHTTP2_ERR_IGN_HEADER_BLOCK
  646. * Frame was rejected and header block must be decoded but
  647. * result must be ignored.
  648. * NGHTTP2_ERR_CALLBACK_FAILURE
  649. * The read_callback failed
  650. */
  651. int nghttp2_session_on_push_promise_received(nghttp2_session *session,
  652. nghttp2_frame *frame);
  653. /*
  654. * Called when PING is received, assuming |frame| is properly
  655. * initialized.
  656. *
  657. * This function returns 0 if it succeeds, or one of the following
  658. * negative error codes:
  659. *
  660. * NGHTTP2_ERR_NOMEM
  661. * Out of memory.
  662. * NGHTTP2_ERR_CALLBACK_FAILURE
  663. * The callback function failed.
  664. * NGHTTP2_ERR_FLOODED
  665. * There are too many items in outbound queue, and this is most
  666. * likely caused by misbehaviour of peer.
  667. */
  668. int nghttp2_session_on_ping_received(nghttp2_session *session,
  669. nghttp2_frame *frame);
  670. /*
  671. * Called when GOAWAY is received, assuming |frame| is properly
  672. * initialized.
  673. *
  674. * This function returns 0 if it succeeds, or one of the following
  675. * negative error codes:
  676. *
  677. * NGHTTP2_ERR_NOMEM
  678. * Out of memory.
  679. * NGHTTP2_ERR_CALLBACK_FAILURE
  680. * The callback function failed.
  681. */
  682. int nghttp2_session_on_goaway_received(nghttp2_session *session,
  683. nghttp2_frame *frame);
  684. /*
  685. * Called when WINDOW_UPDATE is received, assuming |frame| is properly
  686. * initialized.
  687. *
  688. * This function returns 0 if it succeeds, or one of the following
  689. * negative error codes:
  690. *
  691. * NGHTTP2_ERR_NOMEM
  692. * Out of memory.
  693. * NGHTTP2_ERR_CALLBACK_FAILURE
  694. * The callback function failed.
  695. */
  696. int nghttp2_session_on_window_update_received(nghttp2_session *session,
  697. nghttp2_frame *frame);
  698. /*
  699. * Called when ALTSVC is received, assuming |frame| is properly
  700. * initialized.
  701. *
  702. * This function returns 0 if it succeeds, or one of the following
  703. * negative error codes:
  704. *
  705. * NGHTTP2_ERR_CALLBACK_FAILURE
  706. * The callback function failed.
  707. */
  708. int nghttp2_session_on_altsvc_received(nghttp2_session *session,
  709. nghttp2_frame *frame);
  710. /*
  711. * Called when ORIGIN is received, assuming |frame| is properly
  712. * initialized.
  713. *
  714. * This function returns 0 if it succeeds, or one of the following
  715. * negative error codes:
  716. *
  717. * NGHTTP2_ERR_CALLBACK_FAILURE
  718. * The callback function failed.
  719. */
  720. int nghttp2_session_on_origin_received(nghttp2_session *session,
  721. nghttp2_frame *frame);
  722. /*
  723. * Called when DATA is received, assuming |frame| is properly
  724. * initialized.
  725. *
  726. * This function returns 0 if it succeeds, or one of the following
  727. * negative error codes:
  728. *
  729. * NGHTTP2_ERR_NOMEM
  730. * Out of memory.
  731. * NGHTTP2_ERR_CALLBACK_FAILURE
  732. * The callback function failed.
  733. */
  734. int nghttp2_session_on_data_received(nghttp2_session *session,
  735. nghttp2_frame *frame);
  736. /*
  737. * Returns nghttp2_stream* object whose stream ID is |stream_id|. It
  738. * could be NULL if such stream does not exist. This function returns
  739. * NULL if stream is marked as closed.
  740. */
  741. nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session,
  742. int32_t stream_id);
  743. /*
  744. * This function behaves like nghttp2_session_get_stream(), but it
  745. * returns stream object even if it is marked as closed or in
  746. * NGHTTP2_STREAM_IDLE state.
  747. */
  748. nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session,
  749. int32_t stream_id);
  750. /*
  751. * Packs DATA frame |frame| in wire frame format and stores it in
  752. * |bufs|. Payload will be read using |aux_data->data_prd|. The
  753. * length of payload is at most |datamax| bytes.
  754. *
  755. * This function returns 0 if it succeeds, or one of the following
  756. * negative error codes:
  757. *
  758. * NGHTTP2_ERR_DEFERRED
  759. * The DATA frame is postponed.
  760. * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE
  761. * The read_callback failed (stream error).
  762. * NGHTTP2_ERR_NOMEM
  763. * Out of memory.
  764. * NGHTTP2_ERR_CALLBACK_FAILURE
  765. * The read_callback failed (session error).
  766. */
  767. int nghttp2_session_pack_data(nghttp2_session *session, nghttp2_bufs *bufs,
  768. size_t datamax, nghttp2_frame *frame,
  769. nghttp2_data_aux_data *aux_data,
  770. nghttp2_stream *stream);
  771. /*
  772. * Pops and returns next item to send. If there is no such item,
  773. * returns NULL. This function takes into account max concurrent
  774. * streams. That means if session->ob_syn has item and max concurrent
  775. * streams is reached, the even if other queues contain items, then
  776. * this function returns NULL.
  777. */
  778. nghttp2_outbound_item *
  779. nghttp2_session_pop_next_ob_item(nghttp2_session *session);
  780. /*
  781. * Returns next item to send. If there is no such item, this function
  782. * returns NULL. This function takes into account max concurrent
  783. * streams. That means if session->ob_syn has item and max concurrent
  784. * streams is reached, the even if other queues contain items, then
  785. * this function returns NULL.
  786. */
  787. nghttp2_outbound_item *
  788. nghttp2_session_get_next_ob_item(nghttp2_session *session);
  789. /*
  790. * Updates local settings with the |iv|. The number of elements in the
  791. * array pointed by the |iv| is given by the |niv|. This function
  792. * assumes that the all settings_id member in |iv| are in range 1 to
  793. * NGHTTP2_SETTINGS_MAX, inclusive.
  794. *
  795. * While updating individual stream's local window size, if the window
  796. * size becomes strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
  797. * RST_STREAM is issued against such a stream.
  798. *
  799. * This function returns 0 if it succeeds, or one of the following
  800. * negative error codes:
  801. *
  802. * NGHTTP2_ERR_NOMEM
  803. * Out of memory
  804. */
  805. int nghttp2_session_update_local_settings(nghttp2_session *session,
  806. nghttp2_settings_entry *iv,
  807. size_t niv);
  808. /*
  809. * Re-prioritize |stream|. The new priority specification is
  810. * |pri_spec|. Caller must ensure that stream->hd.stream_id !=
  811. * pri_spec->stream_id.
  812. *
  813. * This function does not adjust the number of idle streams. The
  814. * caller should call nghttp2_session_adjust_idle_stream() later.
  815. *
  816. * This function returns 0 if it succeeds, or one of the following
  817. * negative error codes:
  818. *
  819. * NGHTTP2_ERR_NOMEM
  820. * Out of memory
  821. */
  822. int nghttp2_session_reprioritize_stream(nghttp2_session *session,
  823. nghttp2_stream *stream,
  824. const nghttp2_priority_spec *pri_spec);
  825. /*
  826. * Terminates current |session| with the |error_code|. The |reason|
  827. * is NULL-terminated debug string.
  828. *
  829. * This function returns 0 if it succeeds, or one of the following
  830. * negative error codes:
  831. *
  832. * NGHTTP2_ERR_NOMEM
  833. * Out of memory.
  834. * NGHTTP2_ERR_INVALID_ARGUMENT
  835. * The |reason| is too long.
  836. */
  837. int nghttp2_session_terminate_session_with_reason(nghttp2_session *session,
  838. uint32_t error_code,
  839. const char *reason);
  840. #endif /* NGHTTP2_SESSION_H */