nghttp2_session.h 35 KB

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