nghttp2_stream.h 14 KB

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