2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #ifndef NGHTTP2_STREAM_H
26 #define NGHTTP2_STREAM_H
30 #endif /* HAVE_CONFIG_H */
32 #include <nghttp2/nghttp2.h>
33 #include "nghttp2_outbound_item.h"
34 #include "nghttp2_map.h"
35 #include "nghttp2_pq.h"
36 #include "nghttp2_int.h"
39 * Maximum number of streams in one dependency tree.
41 #define NGHTTP2_MAX_DEP_TREE_LENGTH 100
44 * If local peer is stream initiator:
45 * NGHTTP2_STREAM_OPENING : upon sending request HEADERS
46 * NGHTTP2_STREAM_OPENED : upon receiving response HEADERS
47 * NGHTTP2_STREAM_CLOSING : upon queuing RST_STREAM
49 * If remote peer is stream initiator:
50 * NGHTTP2_STREAM_OPENING : upon receiving request HEADERS
51 * NGHTTP2_STREAM_OPENED : upon sending response HEADERS
52 * NGHTTP2_STREAM_CLOSING : upon queuing RST_STREAM
56 NGHTTP2_STREAM_INITIAL,
57 /* For stream initiator: request HEADERS has been sent, but response
58 HEADERS has not been received yet. For receiver: request HEADERS
59 has been received, but it does not send response HEADERS yet. */
60 NGHTTP2_STREAM_OPENING,
61 /* For stream initiator: response HEADERS is received. For receiver:
62 response HEADERS is sent. */
63 NGHTTP2_STREAM_OPENED,
64 /* RST_STREAM is received, but somehow we need to keep stream in
66 NGHTTP2_STREAM_CLOSING,
67 /* PUSH_PROMISE is received or sent */
68 NGHTTP2_STREAM_RESERVED,
69 /* Stream is created in this state if it is used as anchor in
72 } nghttp2_stream_state;
75 NGHTTP2_SHUT_NONE = 0,
76 /* Indicates further receptions will be disallowed. */
77 NGHTTP2_SHUT_RD = 0x01,
78 /* Indicates further transmissions will be disallowed. */
79 NGHTTP2_SHUT_WR = 0x02,
80 /* Indicates both further receptions and transmissions will be
82 NGHTTP2_SHUT_RDWR = NGHTTP2_SHUT_RD | NGHTTP2_SHUT_WR
86 NGHTTP2_STREAM_FLAG_NONE = 0,
87 /* Indicates that this stream is pushed stream and not opened
89 NGHTTP2_STREAM_FLAG_PUSH = 0x01,
90 /* Indicates that this stream was closed */
91 NGHTTP2_STREAM_FLAG_CLOSED = 0x02,
92 /* Indicates the item is deferred due to flow control. */
93 NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL = 0x04,
94 /* Indicates the item is deferred by user callback */
95 NGHTTP2_STREAM_FLAG_DEFERRED_USER = 0x08,
96 /* bitwise OR of NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL and
97 NGHTTP2_STREAM_FLAG_DEFERRED_USER. */
98 NGHTTP2_STREAM_FLAG_DEFERRED_ALL = 0x0c
100 } nghttp2_stream_flag;
102 /* HTTP related flags to enforce HTTP semantics */
104 NGHTTP2_HTTP_FLAG_NONE = 0,
105 /* header field seen so far */
106 NGHTTP2_HTTP_FLAG__AUTHORITY = 1,
107 NGHTTP2_HTTP_FLAG__PATH = 1 << 1,
108 NGHTTP2_HTTP_FLAG__METHOD = 1 << 2,
109 NGHTTP2_HTTP_FLAG__SCHEME = 1 << 3,
110 /* host is not pseudo header, but we require either host or
112 NGHTTP2_HTTP_FLAG_HOST = 1 << 4,
113 NGHTTP2_HTTP_FLAG__STATUS = 1 << 5,
114 /* required header fields for HTTP request except for CONNECT
116 NGHTTP2_HTTP_FLAG_REQ_HEADERS = NGHTTP2_HTTP_FLAG__METHOD |
117 NGHTTP2_HTTP_FLAG__PATH |
118 NGHTTP2_HTTP_FLAG__SCHEME,
119 NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED = 1 << 6,
120 /* HTTP method flags */
121 NGHTTP2_HTTP_FLAG_METH_CONNECT = 1 << 7,
122 NGHTTP2_HTTP_FLAG_METH_HEAD = 1 << 8,
123 NGHTTP2_HTTP_FLAG_METH_OPTIONS = 1 << 9,
124 NGHTTP2_HTTP_FLAG_METH_ALL = NGHTTP2_HTTP_FLAG_METH_CONNECT |
125 NGHTTP2_HTTP_FLAG_METH_HEAD |
126 NGHTTP2_HTTP_FLAG_METH_OPTIONS,
128 /* path starts with "/" */
129 NGHTTP2_HTTP_FLAG_PATH_REGULAR = 1 << 10,
131 NGHTTP2_HTTP_FLAG_PATH_ASTERISK = 1 << 11,
133 /* "http" or "https" scheme */
134 NGHTTP2_HTTP_FLAG_SCHEME_HTTP = 1 << 12,
135 /* set if final response is expected */
136 NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE = 1 << 13
140 NGHTTP2_STREAM_DPRI_NONE = 0,
141 NGHTTP2_STREAM_DPRI_NO_ITEM = 0x01,
142 NGHTTP2_STREAM_DPRI_TOP = 0x02,
143 NGHTTP2_STREAM_DPRI_REST = 0x04
144 } nghttp2_stream_dpri;
146 struct nghttp2_stream_roots;
148 typedef struct nghttp2_stream_roots nghttp2_stream_roots;
150 struct nghttp2_stream;
152 typedef struct nghttp2_stream nghttp2_stream;
154 struct nghttp2_stream {
156 nghttp2_map_entry map_entry;
157 /* Content-Length of request/response body. -1 if unknown. */
158 int64_t content_length;
159 /* Received body so far */
160 int64_t recv_content_length;
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 /* pointers to track dependency tree root streams. This is
171 doubly-linked list and first element is pointed by
173 nghttp2_stream *root_prev, *root_next;
174 /* When stream is kept after closure, it may be kept in doubly
175 linked list pointed by nghttp2_session closed_stream_head.
176 closed_next points to the next stream object if it is the element
178 nghttp2_stream *closed_prev, *closed_next;
179 /* pointer to roots, which tracks dependency tree roots */
180 nghttp2_stream_roots *roots;
181 /* The arbitrary data provided by user for this stream. */
182 void *stream_user_data;
184 nghttp2_outbound_item *item;
187 /* categorized priority of this stream. Only stream bearing
188 NGHTTP2_STREAM_DPRI_TOP can send item. */
189 nghttp2_stream_dpri dpri;
190 /* the number of streams in subtree */
191 size_t num_substreams;
192 /* Current remote window size. This value is computed against the
193 current initial window size of remote endpoint. */
194 int32_t remote_window_size;
195 /* Keep track of the number of bytes received without
196 WINDOW_UPDATE. This could be negative after submitting negative
197 value to WINDOW_UPDATE */
198 int32_t recv_window_size;
199 /* The number of bytes consumed by the application and now is
200 subject to WINDOW_UPDATE. This is only used when auto
201 WINDOW_UPDATE is turned off. */
202 int32_t consumed_size;
203 /* The amount of recv_window_size cut using submitting negative
204 value to WINDOW_UPDATE */
205 int32_t recv_reduction;
206 /* window size for local flow control. It is initially set to
207 NGHTTP2_INITIAL_WINDOW_SIZE and could be increased/decreased by
208 submitting WINDOW_UPDATE. See nghttp2_submit_window_update(). */
209 int32_t local_window_size;
210 /* weight of this stream */
212 /* effective weight of this stream in belonging dependency tree */
213 int32_t effective_weight;
214 /* sum of weight (not effective_weight) of direct descendants */
215 int32_t sum_dep_weight;
216 /* sum of weight of direct descendants which have at least one
217 descendant with dpri == NGHTTP2_STREAM_DPRI_TOP. We use this
218 value to calculate effective weight. */
219 int32_t sum_norest_weight;
220 nghttp2_stream_state state;
221 /* status code from remote server */
223 /* Bitwise OR of zero or more nghttp2_http_flag values */
225 /* This is bitwise-OR of 0 or more of nghttp2_stream_flag. */
227 /* Bitwise OR of zero or more nghttp2_shut_flag values */
231 void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id,
232 uint8_t flags, nghttp2_stream_state initial_state,
233 int32_t weight, nghttp2_stream_roots *roots,
234 int32_t remote_initial_window_size,
235 int32_t local_initial_window_size,
236 void *stream_user_data);
238 void nghttp2_stream_free(nghttp2_stream *stream);
241 * Disallow either further receptions or transmissions, or both.
242 * |flag| is bitwise OR of one or more of nghttp2_shut_flag.
244 void nghttp2_stream_shutdown(nghttp2_stream *stream, nghttp2_shut_flag flag);
247 * Defer |stream->item|. We won't call this function in the situation
248 * where |stream->item| == NULL. The |flags| is bitwise OR of zero or
249 * more of NGHTTP2_STREAM_FLAG_DEFERRED_USER and
250 * NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL. The |flags| indicates
251 * the reason of this action.
253 * This function returns 0 if it succeeds, or one of the following
254 * negative error codes:
259 int nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags,
260 nghttp2_session *session);
263 * Put back deferred data in this stream to active state. The |flags|
264 * are one or more of bitwise OR of the following values:
265 * NGHTTP2_STREAM_FLAG_DEFERRED_USER and
266 * NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL and given masks are
267 * cleared if they are set. So even if this function is called, if
268 * one of flag is still set, data does not become active.
270 int nghttp2_stream_resume_deferred_item(nghttp2_stream *stream, uint8_t flags,
271 nghttp2_session *session);
274 * Returns nonzero if item is deferred by whatever reason.
276 int nghttp2_stream_check_deferred_item(nghttp2_stream *stream);
279 * Returns nonzero if item is deferred by flow control.
281 int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream);
284 * Updates the remote window size with the new value
285 * |new_initial_window_size|. The |old_initial_window_size| is used to
286 * calculate the current window size.
288 * This function returns 0 if it succeeds or -1. The failure is due to
291 int nghttp2_stream_update_remote_initial_window_size(
292 nghttp2_stream *stream, int32_t new_initial_window_size,
293 int32_t old_initial_window_size);
296 * Updates the local window size with the new value
297 * |new_initial_window_size|. The |old_initial_window_size| is used to
298 * calculate the current window size.
300 * This function returns 0 if it succeeds or -1. The failure is due to
303 int nghttp2_stream_update_local_initial_window_size(
304 nghttp2_stream *stream, int32_t new_initial_window_size,
305 int32_t old_initial_window_size);
308 * Call this function if promised stream |stream| is replied with
309 * HEADERS. This function makes the state of the |stream| to
310 * NGHTTP2_STREAM_OPENED.
312 void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream);
315 * Returns the stream positioned in root of the dependency tree the
316 * |stream| belongs to.
318 nghttp2_stream *nghttp2_stream_get_dep_root(nghttp2_stream *stream);
321 * Returns nonzero if |target| is found in subtree of |stream|.
323 int nghttp2_stream_dep_subtree_find(nghttp2_stream *stream,
324 nghttp2_stream *target);
327 * Computes distributed weight of a stream of the |weight| under the
328 * |stream| if |stream| is removed from a dependency tree. The result
329 * is computed using stream->weight rather than
330 * stream->effective_weight.
332 int32_t nghttp2_stream_dep_distributed_weight(nghttp2_stream *stream,
336 * Computes effective weight of a stream of the |weight| under the
337 * |stream|. The result is computed using stream->effective_weight
338 * rather than stream->weight. This function is used to determine
339 * weight in dependency tree.
341 int32_t nghttp2_stream_dep_distributed_effective_weight(nghttp2_stream *stream,
345 * Makes the |stream| depend on the |dep_stream|. This dependency is
346 * exclusive. All existing direct descendants of |dep_stream| become
347 * the descendants of the |stream|. This function assumes
348 * |stream->data| is NULL and no dpri members are changed in this
351 void nghttp2_stream_dep_insert(nghttp2_stream *dep_stream,
352 nghttp2_stream *stream);
355 * Makes the |stream| depend on the |dep_stream|. This dependency is
356 * not exclusive. This function assumes |stream->data| is NULL and no
357 * dpri members are changed in this dependency tree.
359 void nghttp2_stream_dep_add(nghttp2_stream *dep_stream, nghttp2_stream *stream);
362 * Removes the |stream| from the current dependency tree. This
363 * function assumes |stream->data| is NULL.
365 void nghttp2_stream_dep_remove(nghttp2_stream *stream);
368 * Attaches |item| to |stream|. Updates dpri members in this
371 * This function returns 0 if it succeeds, or one of the following
372 * negative error codes:
377 int nghttp2_stream_attach_item(nghttp2_stream *stream,
378 nghttp2_outbound_item *item,
379 nghttp2_session *session);
382 * Detaches |stream->item|. Updates dpri members in this dependency
383 * tree. This function does not free |stream->item|. The caller must
386 * This function returns 0 if it succeeds, or one of the following
387 * negative error codes:
392 int nghttp2_stream_detach_item(nghttp2_stream *stream,
393 nghttp2_session *session);
396 * Makes the |stream| depend on the |dep_stream|. This dependency is
397 * exclusive. Updates dpri members in this dependency tree.
399 * This function returns 0 if it succeeds, or one of the following
400 * negative error codes:
405 int nghttp2_stream_dep_insert_subtree(nghttp2_stream *dep_stream,
406 nghttp2_stream *stream,
407 nghttp2_session *session);
410 * Makes the |stream| depend on the |dep_stream|. This dependency is
411 * not exclusive. Updates dpri members in this dependency tree.
413 * This function returns 0 if it succeeds, or one of the following
414 * negative error codes:
419 int nghttp2_stream_dep_add_subtree(nghttp2_stream *dep_stream,
420 nghttp2_stream *stream,
421 nghttp2_session *session);
424 * Removes subtree whose root stream is |stream|. Removing subtree
425 * does not change dpri values. The effective_weight of streams in
426 * removed subtree is not updated.
428 * This function returns 0 if it succeeds, or one of the following
429 * negative error codes:
434 void nghttp2_stream_dep_remove_subtree(nghttp2_stream *stream);
437 * Makes the |stream| as root. Updates dpri members in this
440 * This function returns 0 if it succeeds, or one of the following
441 * negative error codes:
446 int nghttp2_stream_dep_make_root(nghttp2_stream *stream,
447 nghttp2_session *session);
450 * Makes the |stream| as root and all existing root streams become
451 * direct children of |stream|.
453 * This function returns 0 if it succeeds, or one of the following
454 * negative error codes:
460 nghttp2_stream_dep_all_your_stream_are_belong_to_us(nghttp2_stream *stream,
461 nghttp2_session *session);
464 * Returns nonzero if |stream| is in any dependency tree.
466 int nghttp2_stream_in_dep_tree(nghttp2_stream *stream);
468 struct nghttp2_stream_roots {
469 nghttp2_stream *head;
474 void nghttp2_stream_roots_init(nghttp2_stream_roots *roots);
476 void nghttp2_stream_roots_free(nghttp2_stream_roots *roots);
478 void nghttp2_stream_roots_add(nghttp2_stream_roots *roots,
479 nghttp2_stream *stream);
481 void nghttp2_stream_roots_remove(nghttp2_stream_roots *roots,
482 nghttp2_stream *stream);
484 void nghttp2_stream_roots_remove_all(nghttp2_stream_roots *roots);
486 #endif /* NGHTTP2_STREAM */