nghttp2_stream.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_STREAM_H
  26. #define NGHTTP2_STREAM_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #include <nghttp2/nghttp2.h>
  31. #include "nghttp2_outbound_item.h"
  32. #include "nghttp2_map.h"
  33. #include "nghttp2_pq.h"
  34. #include "nghttp2_int.h"
  35. /*
  36. * If local peer is stream initiator:
  37. * NGHTTP2_STREAM_OPENING : upon sending request HEADERS
  38. * NGHTTP2_STREAM_OPENED : upon receiving response HEADERS
  39. * NGHTTP2_STREAM_CLOSING : upon queuing RST_STREAM
  40. *
  41. * If remote peer is stream initiator:
  42. * NGHTTP2_STREAM_OPENING : upon receiving request HEADERS
  43. * NGHTTP2_STREAM_OPENED : upon sending response HEADERS
  44. * NGHTTP2_STREAM_CLOSING : upon queuing RST_STREAM
  45. */
  46. typedef enum {
  47. /* Initial state */
  48. NGHTTP2_STREAM_INITIAL,
  49. /* For stream initiator: request HEADERS has been sent, but response
  50. HEADERS has not been received yet. For receiver: request HEADERS
  51. has been received, but it does not send response HEADERS yet. */
  52. NGHTTP2_STREAM_OPENING,
  53. /* For stream initiator: response HEADERS is received. For receiver:
  54. response HEADERS is sent. */
  55. NGHTTP2_STREAM_OPENED,
  56. /* RST_STREAM is received, but somehow we need to keep stream in
  57. memory. */
  58. NGHTTP2_STREAM_CLOSING,
  59. /* PUSH_PROMISE is received or sent */
  60. NGHTTP2_STREAM_RESERVED,
  61. /* Stream is created in this state if it is used as anchor in
  62. dependency tree. */
  63. NGHTTP2_STREAM_IDLE
  64. } nghttp2_stream_state;
  65. typedef enum {
  66. NGHTTP2_SHUT_NONE = 0,
  67. /* Indicates further receptions will be disallowed. */
  68. NGHTTP2_SHUT_RD = 0x01,
  69. /* Indicates further transmissions will be disallowed. */
  70. NGHTTP2_SHUT_WR = 0x02,
  71. /* Indicates both further receptions and transmissions will be
  72. disallowed. */
  73. NGHTTP2_SHUT_RDWR = NGHTTP2_SHUT_RD | NGHTTP2_SHUT_WR
  74. } nghttp2_shut_flag;
  75. typedef enum {
  76. NGHTTP2_STREAM_FLAG_NONE = 0,
  77. /* Indicates that this stream is pushed stream and not opened
  78. yet. */
  79. NGHTTP2_STREAM_FLAG_PUSH = 0x01,
  80. /* Indicates that this stream was closed */
  81. NGHTTP2_STREAM_FLAG_CLOSED = 0x02,
  82. /* Indicates the item is deferred due to flow control. */
  83. NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL = 0x04,
  84. /* Indicates the item is deferred by user callback */
  85. NGHTTP2_STREAM_FLAG_DEFERRED_USER = 0x08,
  86. /* bitwise OR of NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL and
  87. NGHTTP2_STREAM_FLAG_DEFERRED_USER. */
  88. NGHTTP2_STREAM_FLAG_DEFERRED_ALL = 0x0c,
  89. /* Indicates that this stream is not subject to RFC7540
  90. priorities scheme. */
  91. NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES = 0x10,
  92. /* Ignore client RFC 9218 priority signal. */
  93. NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES = 0x20,
  94. /* Indicates that RFC 9113 leading and trailing white spaces
  95. validation against a field value is not performed. */
  96. NGHTTP2_STREAM_FLAG_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION = 0x40,
  97. } nghttp2_stream_flag;
  98. /* HTTP related flags to enforce HTTP semantics */
  99. typedef enum {
  100. NGHTTP2_HTTP_FLAG_NONE = 0,
  101. /* header field seen so far */
  102. NGHTTP2_HTTP_FLAG__AUTHORITY = 1,
  103. NGHTTP2_HTTP_FLAG__PATH = 1 << 1,
  104. NGHTTP2_HTTP_FLAG__METHOD = 1 << 2,
  105. NGHTTP2_HTTP_FLAG__SCHEME = 1 << 3,
  106. /* host is not pseudo header, but we require either host or
  107. :authority */
  108. NGHTTP2_HTTP_FLAG_HOST = 1 << 4,
  109. NGHTTP2_HTTP_FLAG__STATUS = 1 << 5,
  110. /* required header fields for HTTP request except for CONNECT
  111. method. */
  112. NGHTTP2_HTTP_FLAG_REQ_HEADERS = NGHTTP2_HTTP_FLAG__METHOD |
  113. NGHTTP2_HTTP_FLAG__PATH |
  114. NGHTTP2_HTTP_FLAG__SCHEME,
  115. NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED = 1 << 6,
  116. /* HTTP method flags */
  117. NGHTTP2_HTTP_FLAG_METH_CONNECT = 1 << 7,
  118. NGHTTP2_HTTP_FLAG_METH_HEAD = 1 << 8,
  119. NGHTTP2_HTTP_FLAG_METH_OPTIONS = 1 << 9,
  120. NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND = 1 << 10,
  121. NGHTTP2_HTTP_FLAG_METH_ALL = NGHTTP2_HTTP_FLAG_METH_CONNECT |
  122. NGHTTP2_HTTP_FLAG_METH_HEAD |
  123. NGHTTP2_HTTP_FLAG_METH_OPTIONS |
  124. NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND,
  125. /* :path category */
  126. /* path starts with "/" */
  127. NGHTTP2_HTTP_FLAG_PATH_REGULAR = 1 << 11,
  128. /* path "*" */
  129. NGHTTP2_HTTP_FLAG_PATH_ASTERISK = 1 << 12,
  130. /* scheme */
  131. /* "http" or "https" scheme */
  132. NGHTTP2_HTTP_FLAG_SCHEME_HTTP = 1 << 13,
  133. /* set if final response is expected */
  134. NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE = 1 << 14,
  135. NGHTTP2_HTTP_FLAG__PROTOCOL = 1 << 15,
  136. /* set if priority header field is received */
  137. NGHTTP2_HTTP_FLAG_PRIORITY = 1 << 16,
  138. /* set if an error is encountered while parsing priority header
  139. field */
  140. NGHTTP2_HTTP_FLAG_BAD_PRIORITY = 1 << 17,
  141. } nghttp2_http_flag;
  142. struct nghttp2_stream {
  143. /* Entry for dep_prev->obq */
  144. nghttp2_pq_entry pq_entry;
  145. /* Priority Queue storing direct descendant (nghttp2_stream). Only
  146. streams which itself has some data to send, or has a descendant
  147. which has some data to sent. */
  148. nghttp2_pq obq;
  149. /* Content-Length of request/response body. -1 if unknown. */
  150. int64_t content_length;
  151. /* Received body so far */
  152. int64_t recv_content_length;
  153. /* Base last_cycle for direct descendent streams. */
  154. uint64_t descendant_last_cycle;
  155. /* Next scheduled time to sent item */
  156. uint64_t cycle;
  157. /* Next seq used for direct descendant streams */
  158. uint64_t descendant_next_seq;
  159. /* Secondary key for prioritization to break a tie for cycle. This
  160. value is monotonically increased for single parent stream. */
  161. uint64_t seq;
  162. /* pointers to form dependency tree. If multiple streams depend on
  163. a stream, only one stream (left most) has non-NULL dep_prev which
  164. points to the stream it depends on. The remaining streams are
  165. linked using sib_prev and sib_next. The stream which has
  166. non-NULL dep_prev always NULL sib_prev. The right most stream
  167. has NULL sib_next. If this stream is a root of dependency tree,
  168. dep_prev and sib_prev are NULL. */
  169. nghttp2_stream *dep_prev, *dep_next;
  170. nghttp2_stream *sib_prev, *sib_next;
  171. /* When stream is kept after closure, it may be kept in doubly
  172. linked list pointed by nghttp2_session closed_stream_head.
  173. closed_next points to the next stream object if it is the element
  174. of the list. */
  175. nghttp2_stream *closed_prev, *closed_next;
  176. /* The arbitrary data provided by user for this stream. */
  177. void *stream_user_data;
  178. /* Item to send */
  179. nghttp2_outbound_item *item;
  180. /* Last written length of frame payload */
  181. size_t last_writelen;
  182. /* stream ID */
  183. int32_t stream_id;
  184. /* Current remote window size. This value is computed against the
  185. current initial window size of remote endpoint. */
  186. int32_t remote_window_size;
  187. /* Keep track of the number of bytes received without
  188. WINDOW_UPDATE. This could be negative after submitting negative
  189. value to WINDOW_UPDATE */
  190. int32_t recv_window_size;
  191. /* The number of bytes consumed by the application and now is
  192. subject to WINDOW_UPDATE. This is only used when auto
  193. WINDOW_UPDATE is turned off. */
  194. int32_t consumed_size;
  195. /* The amount of recv_window_size cut using submitting negative
  196. value to WINDOW_UPDATE */
  197. int32_t recv_reduction;
  198. /* window size for local flow control. It is initially set to
  199. NGHTTP2_INITIAL_WINDOW_SIZE and could be increased/decreased by
  200. submitting WINDOW_UPDATE. See nghttp2_submit_window_update(). */
  201. int32_t local_window_size;
  202. /* weight of this stream */
  203. int32_t weight;
  204. /* This is unpaid penalty (offset) when calculating cycle. */
  205. uint32_t pending_penalty;
  206. /* sum of weight of direct descendants */
  207. int32_t sum_dep_weight;
  208. nghttp2_stream_state state;
  209. /* status code from remote server */
  210. int16_t status_code;
  211. /* Bitwise OR of zero or more nghttp2_http_flag values */
  212. uint32_t http_flags;
  213. /* This is bitwise-OR of 0 or more of nghttp2_stream_flag. */
  214. uint8_t flags;
  215. /* Bitwise OR of zero or more nghttp2_shut_flag values */
  216. uint8_t shut_flags;
  217. /* Nonzero if this stream has been queued to stream pointed by
  218. dep_prev. We maintain the invariant that if a stream is queued,
  219. then its ancestors, except for root, are also queued. This
  220. invariant may break in fatal error condition. */
  221. uint8_t queued;
  222. /* This flag is used to reduce excessive queuing of WINDOW_UPDATE to
  223. this stream. The nonzero does not necessarily mean WINDOW_UPDATE
  224. is not queued. */
  225. uint8_t window_update_queued;
  226. /* extpri is a stream priority produced by nghttp2_extpri_to_uint8
  227. used by RFC 9218 extensible priorities. */
  228. uint8_t extpri;
  229. /* http_extpri is a stream priority received in HTTP request header
  230. fields and produced by nghttp2_extpri_to_uint8. */
  231. uint8_t http_extpri;
  232. };
  233. void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id,
  234. uint8_t flags, nghttp2_stream_state initial_state,
  235. int32_t weight, int32_t remote_initial_window_size,
  236. int32_t local_initial_window_size,
  237. void *stream_user_data, nghttp2_mem *mem);
  238. void nghttp2_stream_free(nghttp2_stream *stream);
  239. /*
  240. * Disallow either further receptions or transmissions, or both.
  241. * |flag| is bitwise OR of one or more of nghttp2_shut_flag.
  242. */
  243. void nghttp2_stream_shutdown(nghttp2_stream *stream, nghttp2_shut_flag flag);
  244. /*
  245. * Defer |stream->item|. We won't call this function in the situation
  246. * where |stream->item| == NULL. The |flags| is bitwise OR of zero or
  247. * more of NGHTTP2_STREAM_FLAG_DEFERRED_USER and
  248. * NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL. The |flags| indicates
  249. * the reason of this action.
  250. */
  251. void nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags);
  252. /*
  253. * Put back deferred data in this stream to active state. The |flags|
  254. * are one or more of bitwise OR of the following values:
  255. * NGHTTP2_STREAM_FLAG_DEFERRED_USER and
  256. * NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL and given masks are
  257. * cleared if they are set. So even if this function is called, if
  258. * one of flag is still set, data does not become active.
  259. *
  260. * This function returns 0 if it succeeds, or one of the following
  261. * negative error codes:
  262. *
  263. * NGHTTP2_ERR_NOMEM
  264. * Out of memory
  265. */
  266. int nghttp2_stream_resume_deferred_item(nghttp2_stream *stream, uint8_t flags);
  267. /*
  268. * Returns nonzero if item is deferred by whatever reason.
  269. */
  270. int nghttp2_stream_check_deferred_item(nghttp2_stream *stream);
  271. /*
  272. * Returns nonzero if item is deferred by flow control.
  273. */
  274. int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream);
  275. /*
  276. * Updates the remote window size with the new value
  277. * |new_initial_window_size|. The |old_initial_window_size| is used to
  278. * calculate the current window size.
  279. *
  280. * This function returns 0 if it succeeds or -1. The failure is due to
  281. * overflow.
  282. */
  283. int nghttp2_stream_update_remote_initial_window_size(
  284. nghttp2_stream *stream, int32_t new_initial_window_size,
  285. int32_t old_initial_window_size);
  286. /*
  287. * Updates the local window size with the new value
  288. * |new_initial_window_size|. The |old_initial_window_size| is used to
  289. * calculate the current window size.
  290. *
  291. * This function returns 0 if it succeeds or -1. The failure is due to
  292. * overflow.
  293. */
  294. int nghttp2_stream_update_local_initial_window_size(
  295. nghttp2_stream *stream, int32_t new_initial_window_size,
  296. int32_t old_initial_window_size);
  297. /*
  298. * Call this function if promised stream |stream| is replied with
  299. * HEADERS. This function makes the state of the |stream| to
  300. * NGHTTP2_STREAM_OPENED.
  301. */
  302. void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream);
  303. /*
  304. * Returns nonzero if |target| is an ancestor of |stream|.
  305. */
  306. int nghttp2_stream_dep_find_ancestor(nghttp2_stream *stream,
  307. nghttp2_stream *target);
  308. /*
  309. * Computes distributed weight of a stream of the |weight| under the
  310. * |stream| if |stream| is removed from a dependency tree.
  311. */
  312. int32_t nghttp2_stream_dep_distributed_weight(nghttp2_stream *stream,
  313. int32_t weight);
  314. /*
  315. * Makes the |stream| depend on the |dep_stream|. This dependency is
  316. * exclusive. All existing direct descendants of |dep_stream| become
  317. * the descendants of the |stream|. This function assumes
  318. * |stream->item| is NULL.
  319. *
  320. * This function returns 0 if it succeeds, or one of the following
  321. * negative error codes:
  322. *
  323. * NGHTTP2_ERR_NOMEM
  324. * Out of memory
  325. */
  326. int nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
  327. nghttp2_stream *stream);
  328. /*
  329. * Makes the |stream| depend on the |dep_stream|. This dependency is
  330. * not exclusive. This function assumes |stream->item| is NULL.
  331. */
  332. void nghttp2_stream_dep_add(nghttp2_stream *dep_stream, nghttp2_stream *stream);
  333. /*
  334. * Removes the |stream| from the current dependency tree. This
  335. * function assumes |stream->item| is NULL.
  336. */
  337. int nghttp2_stream_dep_remove(nghttp2_stream *stream);
  338. /*
  339. * Attaches |item| to |stream|.
  340. *
  341. * This function returns 0 if it succeeds, or one of the following
  342. * negative error codes:
  343. *
  344. * NGHTTP2_ERR_NOMEM
  345. * Out of memory
  346. */
  347. int nghttp2_stream_attach_item(nghttp2_stream *stream,
  348. nghttp2_outbound_item *item);
  349. /*
  350. * Detaches |stream->item|. This function does not free
  351. * |stream->item|. The caller must free it.
  352. */
  353. void nghttp2_stream_detach_item(nghttp2_stream *stream);
  354. /*
  355. * Makes the |stream| depend on the |dep_stream|. This dependency is
  356. * exclusive.
  357. *
  358. * This function returns 0 if it succeeds, or one of the following
  359. * negative error codes:
  360. *
  361. * NGHTTP2_ERR_NOMEM
  362. * Out of memory
  363. */
  364. int nghttp2_stream_dep_insert_subtree(nghttp2_stream *dep_stream,
  365. nghttp2_stream *stream);
  366. /*
  367. * Makes the |stream| depend on the |dep_stream|. This dependency is
  368. * not exclusive.
  369. *
  370. * This function returns 0 if it succeeds, or one of the following
  371. * negative error codes:
  372. *
  373. * NGHTTP2_ERR_NOMEM
  374. * Out of memory
  375. */
  376. int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
  377. nghttp2_stream *stream);
  378. /*
  379. * Removes subtree whose root stream is |stream|. The
  380. * effective_weight of streams in removed subtree is not updated.
  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. */
  388. void nghttp2_stream_dep_remove_subtree(nghttp2_stream *stream);
  389. /*
  390. * Returns nonzero if |stream| is in any dependency tree.
  391. */
  392. int nghttp2_stream_in_dep_tree(nghttp2_stream *stream);
  393. /*
  394. * Schedules transmission of |stream|'s item, assuming stream->item is
  395. * attached, and stream->last_writelen was updated.
  396. */
  397. void nghttp2_stream_reschedule(nghttp2_stream *stream);
  398. /*
  399. * Changes |stream|'s weight to |weight|. If |stream| is queued, it
  400. * will be rescheduled based on new weight.
  401. */
  402. void nghttp2_stream_change_weight(nghttp2_stream *stream, int32_t weight);
  403. /*
  404. * Returns a stream which has highest priority, updating
  405. * descendant_last_cycle of selected stream's ancestors.
  406. */
  407. nghttp2_outbound_item *
  408. nghttp2_stream_next_outbound_item(nghttp2_stream *stream);
  409. #endif /* NGHTTP2_STREAM */