nghttp2_stream.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 =
  122. NGHTTP2_HTTP_FLAG_METH_CONNECT | NGHTTP2_HTTP_FLAG_METH_HEAD |
  123. NGHTTP2_HTTP_FLAG_METH_OPTIONS | NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND,
  124. /* :path category */
  125. /* path starts with "/" */
  126. NGHTTP2_HTTP_FLAG_PATH_REGULAR = 1 << 11,
  127. /* path "*" */
  128. NGHTTP2_HTTP_FLAG_PATH_ASTERISK = 1 << 12,
  129. /* scheme */
  130. /* "http" or "https" scheme */
  131. NGHTTP2_HTTP_FLAG_SCHEME_HTTP = 1 << 13,
  132. /* set if final response is expected */
  133. NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE = 1 << 14,
  134. NGHTTP2_HTTP_FLAG__PROTOCOL = 1 << 15,
  135. /* set if priority header field is received */
  136. NGHTTP2_HTTP_FLAG_PRIORITY = 1 << 16,
  137. /* set if an error is encountered while parsing priority header
  138. field */
  139. NGHTTP2_HTTP_FLAG_BAD_PRIORITY = 1 << 17,
  140. } nghttp2_http_flag;
  141. struct nghttp2_stream {
  142. /* Entry for dep_prev->obq */
  143. nghttp2_pq_entry pq_entry;
  144. /* Priority Queue storing direct descendant (nghttp2_stream). Only
  145. streams which itself has some data to send, or has a descendant
  146. which has some data to sent. */
  147. nghttp2_pq obq;
  148. /* Content-Length of request/response body. -1 if unknown. */
  149. int64_t content_length;
  150. /* Received body so far */
  151. int64_t recv_content_length;
  152. /* Base last_cycle for direct descendent streams. */
  153. uint64_t descendant_last_cycle;
  154. /* Next scheduled time to sent item */
  155. uint64_t cycle;
  156. /* Next seq used for direct descendant streams */
  157. uint64_t descendant_next_seq;
  158. /* Secondary key for prioritization to break a tie for cycle. This
  159. value is monotonically increased for single parent stream. */
  160. uint64_t seq;
  161. /* pointers to form dependency tree. If multiple streams depend on
  162. a stream, only one stream (left most) has non-NULL dep_prev which
  163. points to the stream it depends on. The remaining streams are
  164. linked using sib_prev and sib_next. The stream which has
  165. non-NULL dep_prev always NULL sib_prev. The right most stream
  166. has NULL sib_next. If this stream is a root of dependency tree,
  167. dep_prev and sib_prev are NULL. */
  168. nghttp2_stream *dep_prev, *dep_next;
  169. nghttp2_stream *sib_prev, *sib_next;
  170. /* When stream is kept after closure, it may be kept in doubly
  171. linked list pointed by nghttp2_session closed_stream_head.
  172. closed_next points to the next stream object if it is the element
  173. of the list. */
  174. nghttp2_stream *closed_prev, *closed_next;
  175. /* The arbitrary data provided by user for this stream. */
  176. void *stream_user_data;
  177. /* Item to send */
  178. nghttp2_outbound_item *item;
  179. /* Last written length of frame payload */
  180. size_t last_writelen;
  181. /* stream ID */
  182. int32_t stream_id;
  183. /* Current remote window size. This value is computed against the
  184. current initial window size of remote endpoint. */
  185. int32_t remote_window_size;
  186. /* Keep track of the number of bytes received without
  187. WINDOW_UPDATE. This could be negative after submitting negative
  188. value to WINDOW_UPDATE */
  189. int32_t recv_window_size;
  190. /* The number of bytes consumed by the application and now is
  191. subject to WINDOW_UPDATE. This is only used when auto
  192. WINDOW_UPDATE is turned off. */
  193. int32_t consumed_size;
  194. /* The amount of recv_window_size cut using submitting negative
  195. value to WINDOW_UPDATE */
  196. int32_t recv_reduction;
  197. /* window size for local flow control. It is initially set to
  198. NGHTTP2_INITIAL_WINDOW_SIZE and could be increased/decreased by
  199. submitting WINDOW_UPDATE. See nghttp2_submit_window_update(). */
  200. int32_t local_window_size;
  201. /* weight of this stream */
  202. int32_t weight;
  203. /* This is unpaid penalty (offset) when calculating cycle. */
  204. uint32_t pending_penalty;
  205. /* sum of weight of direct descendants */
  206. int32_t sum_dep_weight;
  207. nghttp2_stream_state state;
  208. /* status code from remote server */
  209. int16_t status_code;
  210. /* Bitwise OR of zero or more nghttp2_http_flag values */
  211. uint32_t http_flags;
  212. /* This is bitwise-OR of 0 or more of nghttp2_stream_flag. */
  213. uint8_t flags;
  214. /* Bitwise OR of zero or more nghttp2_shut_flag values */
  215. uint8_t shut_flags;
  216. /* Nonzero if this stream has been queued to stream pointed by
  217. dep_prev. We maintain the invariant that if a stream is queued,
  218. then its ancestors, except for root, are also queued. This
  219. invariant may break in fatal error condition. */
  220. uint8_t queued;
  221. /* This flag is used to reduce excessive queuing of WINDOW_UPDATE to
  222. this stream. The nonzero does not necessarily mean WINDOW_UPDATE
  223. is not queued. */
  224. uint8_t window_update_queued;
  225. /* extpri is a stream priority produced by nghttp2_extpri_to_uint8
  226. used by RFC 9218 extensible priorities. */
  227. uint8_t extpri;
  228. /* http_extpri is a stream priority received in HTTP request header
  229. fields and produced by nghttp2_extpri_to_uint8. */
  230. uint8_t http_extpri;
  231. };
  232. void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id,
  233. uint8_t flags, nghttp2_stream_state initial_state,
  234. int32_t weight, int32_t remote_initial_window_size,
  235. int32_t local_initial_window_size,
  236. void *stream_user_data, nghttp2_mem *mem);
  237. void nghttp2_stream_free(nghttp2_stream *stream);
  238. /*
  239. * Disallow either further receptions or transmissions, or both.
  240. * |flag| is bitwise OR of one or more of nghttp2_shut_flag.
  241. */
  242. void nghttp2_stream_shutdown(nghttp2_stream *stream, nghttp2_shut_flag flag);
  243. /*
  244. * Defer |stream->item|. We won't call this function in the situation
  245. * where |stream->item| == NULL. The |flags| is bitwise OR of zero or
  246. * more of NGHTTP2_STREAM_FLAG_DEFERRED_USER and
  247. * NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL. The |flags| indicates
  248. * the reason of this action.
  249. */
  250. void nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags);
  251. /*
  252. * Put back deferred data in this stream to active state. The |flags|
  253. * are one or more of bitwise OR of the following values:
  254. * NGHTTP2_STREAM_FLAG_DEFERRED_USER and
  255. * NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL and given masks are
  256. * cleared if they are set. So even if this function is called, if
  257. * one of flag is still set, data does not become active.
  258. *
  259. * This function returns 0 if it succeeds, or one of the following
  260. * negative error codes:
  261. *
  262. * NGHTTP2_ERR_NOMEM
  263. * Out of memory
  264. */
  265. int nghttp2_stream_resume_deferred_item(nghttp2_stream *stream, uint8_t flags);
  266. /*
  267. * Returns nonzero if item is deferred by whatever reason.
  268. */
  269. int nghttp2_stream_check_deferred_item(nghttp2_stream *stream);
  270. /*
  271. * Returns nonzero if item is deferred by flow control.
  272. */
  273. int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream);
  274. /*
  275. * Updates the remote window size with the new value
  276. * |new_initial_window_size|. The |old_initial_window_size| is used to
  277. * calculate the current window size.
  278. *
  279. * This function returns 0 if it succeeds or -1. The failure is due to
  280. * overflow.
  281. */
  282. int nghttp2_stream_update_remote_initial_window_size(
  283. nghttp2_stream *stream, int32_t new_initial_window_size,
  284. int32_t old_initial_window_size);
  285. /*
  286. * Updates the local window size with the new value
  287. * |new_initial_window_size|. The |old_initial_window_size| is used to
  288. * calculate the current window size.
  289. *
  290. * This function returns 0 if it succeeds or -1. The failure is due to
  291. * overflow.
  292. */
  293. int nghttp2_stream_update_local_initial_window_size(
  294. nghttp2_stream *stream, int32_t new_initial_window_size,
  295. int32_t old_initial_window_size);
  296. /*
  297. * Call this function if promised stream |stream| is replied with
  298. * HEADERS. This function makes the state of the |stream| to
  299. * NGHTTP2_STREAM_OPENED.
  300. */
  301. void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream);
  302. /*
  303. * Returns nonzero if |target| is an ancestor of |stream|.
  304. */
  305. int nghttp2_stream_dep_find_ancestor(nghttp2_stream *stream,
  306. nghttp2_stream *target);
  307. /*
  308. * Computes distributed weight of a stream of the |weight| under the
  309. * |stream| if |stream| is removed from a dependency tree.
  310. */
  311. int32_t nghttp2_stream_dep_distributed_weight(nghttp2_stream *stream,
  312. int32_t weight);
  313. /*
  314. * Makes the |stream| depend on the |dep_stream|. This dependency is
  315. * exclusive. All existing direct descendants of |dep_stream| become
  316. * the descendants of the |stream|. This function assumes
  317. * |stream->item| is NULL.
  318. *
  319. * This function returns 0 if it succeeds, or one of the following
  320. * negative error codes:
  321. *
  322. * NGHTTP2_ERR_NOMEM
  323. * Out of memory
  324. */
  325. int nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
  326. nghttp2_stream *stream);
  327. /*
  328. * Makes the |stream| depend on the |dep_stream|. This dependency is
  329. * not exclusive. This function assumes |stream->item| is NULL.
  330. */
  331. void nghttp2_stream_dep_add(nghttp2_stream *dep_stream, nghttp2_stream *stream);
  332. /*
  333. * Removes the |stream| from the current dependency tree. This
  334. * function assumes |stream->item| is NULL.
  335. */
  336. int nghttp2_stream_dep_remove(nghttp2_stream *stream);
  337. /*
  338. * Attaches |item| to |stream|.
  339. *
  340. * This function returns 0 if it succeeds, or one of the following
  341. * negative error codes:
  342. *
  343. * NGHTTP2_ERR_NOMEM
  344. * Out of memory
  345. */
  346. int nghttp2_stream_attach_item(nghttp2_stream *stream,
  347. nghttp2_outbound_item *item);
  348. /*
  349. * Detaches |stream->item|. This function does not free
  350. * |stream->item|. The caller must free it.
  351. */
  352. void nghttp2_stream_detach_item(nghttp2_stream *stream);
  353. /*
  354. * Makes the |stream| depend on the |dep_stream|. This dependency is
  355. * exclusive.
  356. *
  357. * This function returns 0 if it succeeds, or one of the following
  358. * negative error codes:
  359. *
  360. * NGHTTP2_ERR_NOMEM
  361. * Out of memory
  362. */
  363. int nghttp2_stream_dep_insert_subtree(nghttp2_stream *dep_stream,
  364. nghttp2_stream *stream);
  365. /*
  366. * Makes the |stream| depend on the |dep_stream|. This dependency is
  367. * not exclusive.
  368. *
  369. * This function returns 0 if it succeeds, or one of the following
  370. * negative error codes:
  371. *
  372. * NGHTTP2_ERR_NOMEM
  373. * Out of memory
  374. */
  375. int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
  376. nghttp2_stream *stream);
  377. /*
  378. * Removes subtree whose root stream is |stream|. The
  379. * effective_weight of streams in removed subtree is not updated.
  380. *
  381. * This function returns 0 if it succeeds, or one of the following
  382. * negative error codes:
  383. *
  384. * NGHTTP2_ERR_NOMEM
  385. * Out of memory
  386. */
  387. void nghttp2_stream_dep_remove_subtree(nghttp2_stream *stream);
  388. /*
  389. * Returns nonzero if |stream| is in any dependency tree.
  390. */
  391. int nghttp2_stream_in_dep_tree(nghttp2_stream *stream);
  392. /*
  393. * Schedules transmission of |stream|'s item, assuming stream->item is
  394. * attached, and stream->last_writelen was updated.
  395. */
  396. void nghttp2_stream_reschedule(nghttp2_stream *stream);
  397. /*
  398. * Changes |stream|'s weight to |weight|. If |stream| is queued, it
  399. * will be rescheduled based on new weight.
  400. */
  401. void nghttp2_stream_change_weight(nghttp2_stream *stream, int32_t weight);
  402. /*
  403. * Returns a stream which has highest priority, updating
  404. * descendant_last_cycle of selected stream's ancestors.
  405. */
  406. nghttp2_outbound_item *
  407. nghttp2_stream_next_outbound_item(nghttp2_stream *stream);
  408. #endif /* NGHTTP2_STREAM */