nghttp2_session.h 35 KB

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