/src/nghttp2/lib/nghttp2_stream.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "nghttp2_stream.h" |
26 | | |
27 | | #include <assert.h> |
28 | | |
29 | | #include "nghttp2_session.h" |
30 | | #include "nghttp2_helper.h" |
31 | | #include "nghttp2_debug.h" |
32 | | #include "nghttp2_frame.h" |
33 | | |
34 | | void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id, |
35 | | uint8_t flags, nghttp2_stream_state initial_state, |
36 | | int32_t remote_initial_window_size, |
37 | | int32_t local_initial_window_size, |
38 | 29.6k | void *stream_user_data) { |
39 | 29.6k | stream->stream_id = stream_id; |
40 | 29.6k | stream->flags = flags; |
41 | 29.6k | stream->state = initial_state; |
42 | 29.6k | stream->shut_flags = NGHTTP2_SHUT_NONE; |
43 | 29.6k | stream->stream_user_data = stream_user_data; |
44 | 29.6k | stream->item = NULL; |
45 | 29.6k | stream->remote_window_size = remote_initial_window_size; |
46 | 29.6k | stream->local_window_size = local_initial_window_size; |
47 | 29.6k | stream->recv_window_size = 0; |
48 | 29.6k | stream->consumed_size = 0; |
49 | 29.6k | stream->recv_reduction = 0; |
50 | 29.6k | stream->window_update_queued = 0; |
51 | | |
52 | 29.6k | stream->closed_next = NULL; |
53 | | |
54 | 29.6k | stream->http_flags = NGHTTP2_HTTP_FLAG_NONE; |
55 | 29.6k | stream->content_length = -1; |
56 | 29.6k | stream->recv_content_length = 0; |
57 | 29.6k | stream->status_code = -1; |
58 | | |
59 | 29.6k | stream->queued = 0; |
60 | 29.6k | stream->cycle = 0; |
61 | 29.6k | stream->pending_penalty = 0; |
62 | 29.6k | stream->seq = 0; |
63 | 29.6k | stream->last_writelen = 0; |
64 | | |
65 | 29.6k | stream->extpri = stream->http_extpri = NGHTTP2_EXTPRI_DEFAULT_URGENCY; |
66 | 29.6k | } |
67 | | |
68 | 29.6k | void nghttp2_stream_free(nghttp2_stream *stream) { (void)stream; } |
69 | | |
70 | 5.67k | void nghttp2_stream_shutdown(nghttp2_stream *stream, nghttp2_shut_flag flag) { |
71 | 5.67k | stream->shut_flags = (uint8_t)(stream->shut_flags | flag); |
72 | 5.67k | } |
73 | | |
74 | | void nghttp2_stream_attach_item(nghttp2_stream *stream, |
75 | 0 | nghttp2_outbound_item *item) { |
76 | 0 | assert((stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) == 0); |
77 | 0 | assert(stream->item == NULL); |
78 | | |
79 | 0 | DEBUGF("stream: stream=%d attach item=%p\n", stream->stream_id, item); |
80 | |
|
81 | 0 | stream->item = item; |
82 | 0 | } |
83 | | |
84 | 0 | void nghttp2_stream_detach_item(nghttp2_stream *stream) { |
85 | 0 | DEBUGF("stream: stream=%d detach item=%p\n", stream->stream_id, stream->item); |
86 | |
|
87 | 0 | stream->item = NULL; |
88 | 0 | stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_DEFERRED_ALL); |
89 | 0 | } |
90 | | |
91 | 0 | void nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags) { |
92 | 0 | assert(stream->item); |
93 | | |
94 | 0 | DEBUGF("stream: stream=%d defer item=%p cause=%02x\n", stream->stream_id, |
95 | 0 | stream->item, flags); |
96 | |
|
97 | 0 | stream->flags |= flags; |
98 | 0 | } |
99 | | |
100 | | void nghttp2_stream_resume_deferred_item(nghttp2_stream *stream, |
101 | 0 | uint8_t flags) { |
102 | 0 | assert(stream->item); |
103 | | |
104 | 0 | DEBUGF("stream: stream=%d resume item=%p flags=%02x\n", stream->stream_id, |
105 | 0 | stream->item, flags); |
106 | |
|
107 | 0 | stream->flags = (uint8_t)(stream->flags & ~flags); |
108 | 0 | } |
109 | | |
110 | 0 | int nghttp2_stream_check_deferred_item(nghttp2_stream *stream) { |
111 | 0 | return stream->item && (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL); |
112 | 0 | } |
113 | | |
114 | 1.82k | int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream) { |
115 | 1.82k | return stream->item && |
116 | 1.82k | (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL); |
117 | 1.82k | } |
118 | | |
119 | | static int update_initial_window_size(int32_t *window_size_ptr, |
120 | | int32_t new_initial_window_size, |
121 | 2.10k | int32_t old_initial_window_size) { |
122 | 2.10k | int64_t new_window_size = (int64_t)(*window_size_ptr) + |
123 | 2.10k | new_initial_window_size - old_initial_window_size; |
124 | 2.10k | if (INT32_MIN > new_window_size || |
125 | 2.10k | new_window_size > NGHTTP2_MAX_WINDOW_SIZE) { |
126 | 398 | return -1; |
127 | 398 | } |
128 | 1.70k | *window_size_ptr = (int32_t)new_window_size; |
129 | 1.70k | return 0; |
130 | 2.10k | } |
131 | | |
132 | | int nghttp2_stream_update_remote_initial_window_size( |
133 | | nghttp2_stream *stream, int32_t new_initial_window_size, |
134 | 1.89k | int32_t old_initial_window_size) { |
135 | 1.89k | return update_initial_window_size(&stream->remote_window_size, |
136 | 1.89k | new_initial_window_size, |
137 | 1.89k | old_initial_window_size); |
138 | 1.89k | } |
139 | | |
140 | | int nghttp2_stream_update_local_initial_window_size( |
141 | | nghttp2_stream *stream, int32_t new_initial_window_size, |
142 | 210 | int32_t old_initial_window_size) { |
143 | 210 | return update_initial_window_size(&stream->local_window_size, |
144 | 210 | new_initial_window_size, |
145 | 210 | old_initial_window_size); |
146 | 210 | } |
147 | | |
148 | 0 | void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream) { |
149 | 0 | stream->state = NGHTTP2_STREAM_OPENED; |
150 | 0 | stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_PUSH); |
151 | 0 | } |
152 | | |
153 | 0 | nghttp2_stream_proto_state nghttp2_stream_get_state(nghttp2_stream *stream) { |
154 | 0 | if (stream == &nghttp2_stream_root) { |
155 | 0 | return NGHTTP2_STREAM_STATE_IDLE; |
156 | 0 | } |
157 | | |
158 | 0 | if (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) { |
159 | 0 | return NGHTTP2_STREAM_STATE_CLOSED; |
160 | 0 | } |
161 | | |
162 | 0 | if (stream->flags & NGHTTP2_STREAM_FLAG_PUSH) { |
163 | 0 | if (stream->shut_flags & NGHTTP2_SHUT_RD) { |
164 | 0 | return NGHTTP2_STREAM_STATE_RESERVED_LOCAL; |
165 | 0 | } |
166 | | |
167 | 0 | if (stream->shut_flags & NGHTTP2_SHUT_WR) { |
168 | 0 | return NGHTTP2_STREAM_STATE_RESERVED_REMOTE; |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | 0 | if (stream->shut_flags & NGHTTP2_SHUT_RD) { |
173 | 0 | return NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE; |
174 | 0 | } |
175 | | |
176 | 0 | if (stream->shut_flags & NGHTTP2_SHUT_WR) { |
177 | 0 | return NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL; |
178 | 0 | } |
179 | | |
180 | 0 | if (stream->state == NGHTTP2_STREAM_IDLE) { |
181 | 0 | return NGHTTP2_STREAM_STATE_IDLE; |
182 | 0 | } |
183 | | |
184 | 0 | return NGHTTP2_STREAM_STATE_OPEN; |
185 | 0 | } |
186 | | |
187 | 0 | nghttp2_stream *nghttp2_stream_get_parent(nghttp2_stream *stream) { |
188 | 0 | (void)stream; |
189 | |
|
190 | 0 | return NULL; |
191 | 0 | } |
192 | | |
193 | 0 | nghttp2_stream *nghttp2_stream_get_next_sibling(nghttp2_stream *stream) { |
194 | 0 | (void)stream; |
195 | |
|
196 | 0 | return NULL; |
197 | 0 | } |
198 | | |
199 | 0 | nghttp2_stream *nghttp2_stream_get_previous_sibling(nghttp2_stream *stream) { |
200 | 0 | (void)stream; |
201 | |
|
202 | 0 | return NULL; |
203 | 0 | } |
204 | | |
205 | 0 | nghttp2_stream *nghttp2_stream_get_first_child(nghttp2_stream *stream) { |
206 | 0 | (void)stream; |
207 | |
|
208 | 0 | return NULL; |
209 | 0 | } |
210 | | |
211 | 0 | int32_t nghttp2_stream_get_weight(nghttp2_stream *stream) { |
212 | 0 | (void)stream; |
213 | |
|
214 | 0 | return NGHTTP2_DEFAULT_WEIGHT; |
215 | 0 | } |
216 | | |
217 | 0 | int32_t nghttp2_stream_get_sum_dependency_weight(nghttp2_stream *stream) { |
218 | 0 | (void)stream; |
219 | |
|
220 | 0 | return 0; |
221 | 0 | } |
222 | | |
223 | 0 | int32_t nghttp2_stream_get_stream_id(nghttp2_stream *stream) { |
224 | 0 | return stream->stream_id; |
225 | 0 | } |