nghttp2_stream.h 15 KB

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