nghttp2_stream.h 15 KB

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