/src/curl/lib/ngh2_shim.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_NGHTTP2) |
27 | | #include <nghttp2/nghttp2.h> |
28 | | |
29 | | #include "urldata.h" |
30 | | #include "ngh2_shim.h" |
31 | | |
32 | | |
33 | | bool Curl_ngh2_valid(struct curl_ngh2 *ngh2) |
34 | 0 | { |
35 | 0 | return ngh2->s && !ngh2->fatal_rv; |
36 | 0 | } |
37 | | |
38 | | bool Curl_ngh2_is_fatal(struct curl_ngh2 *ngh2, int rv) |
39 | 0 | { |
40 | 0 | if(ngh2->fatal_rv) |
41 | 0 | return TRUE; |
42 | 0 | if(nghttp2_is_fatal(rv)) { |
43 | 0 | ngh2->fatal_rv = rv; |
44 | 0 | return TRUE; |
45 | 0 | } |
46 | 0 | return FALSE; |
47 | 0 | } |
48 | | |
49 | | int Curl_ngh2_fatal(struct curl_ngh2 *ngh2) |
50 | 0 | { |
51 | 0 | return ngh2->fatal_rv; |
52 | 0 | } |
53 | | |
54 | | static int ngh2_check(struct curl_ngh2 *ngh2, int rv) |
55 | 0 | { |
56 | 0 | if(nghttp2_is_fatal(rv)) |
57 | 0 | ngh2->fatal_rv = rv; |
58 | 0 | return rv; |
59 | 0 | } |
60 | | |
61 | | static int32_t ngh2_check32(struct curl_ngh2 *ngh2, int32_t rv) |
62 | 0 | { |
63 | 0 | if((rv < 0) && nghttp2_is_fatal((int)rv)) |
64 | 0 | ngh2->fatal_rv = (int)rv; |
65 | 0 | return rv; |
66 | 0 | } |
67 | | |
68 | | static ssize_t ngh2_checks(struct curl_ngh2 *ngh2, ssize_t rv) |
69 | 0 | { |
70 | 0 | if((rv < 0) && nghttp2_is_fatal((int)rv)) |
71 | 0 | ngh2->fatal_rv = (int)rv; |
72 | 0 | return rv; |
73 | 0 | } |
74 | | |
75 | | void Curl_ngh2_del(struct curl_ngh2 *ngh2) |
76 | 0 | { |
77 | 0 | if(ngh2->s) { |
78 | 0 | nghttp2_session_del(ngh2->s); |
79 | 0 | ngh2->s = NULL; |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | int Curl_ngh2_submit_settings(struct curl_ngh2 *ngh2, uint8_t flags, |
84 | | const nghttp2_settings_entry *iv, size_t niv) |
85 | 0 | { |
86 | 0 | if(ngh2->fatal_rv) |
87 | 0 | return ngh2->fatal_rv; |
88 | 0 | return ngh2_check(ngh2, nghttp2_submit_settings(ngh2->s, flags, iv, niv)); |
89 | 0 | } |
90 | | |
91 | | void *Curl_ngh2_session_get_stream_user_data(struct curl_ngh2 *ngh2, |
92 | | int32_t stream_id) |
93 | 0 | { |
94 | 0 | if(ngh2->fatal_rv) |
95 | 0 | return NULL; |
96 | 0 | return nghttp2_session_get_stream_user_data(ngh2->s, stream_id); |
97 | 0 | } |
98 | | |
99 | | int Curl_ngh2_session_set_stream_user_data(struct curl_ngh2 *ngh2, |
100 | | int32_t stream_id, |
101 | | void *stream_user_data) |
102 | 0 | { |
103 | 0 | if(ngh2->fatal_rv) |
104 | 0 | return ngh2->fatal_rv; |
105 | 0 | return ngh2_check(ngh2, |
106 | 0 | nghttp2_session_set_stream_user_data(ngh2->s, stream_id, |
107 | 0 | stream_user_data)); |
108 | 0 | } |
109 | | |
110 | | uint32_t Curl_ngh2_session_get_remote_settings(struct curl_ngh2 *ngh2, |
111 | | nghttp2_settings_id id) |
112 | 0 | { |
113 | 0 | if(!Curl_ngh2_valid(ngh2)) |
114 | 0 | return 0; |
115 | 0 | return nghttp2_session_get_remote_settings(ngh2->s, id); |
116 | 0 | } |
117 | | |
118 | | int32_t Curl_ngh2_session_get_stream_effective_local_window_size( |
119 | | struct curl_ngh2 *ngh2, int32_t stream_id) |
120 | 0 | { |
121 | 0 | if(ngh2->fatal_rv) |
122 | 0 | return 0; |
123 | 0 | return nghttp2_session_get_stream_effective_local_window_size(ngh2->s, |
124 | 0 | stream_id); |
125 | 0 | } |
126 | | |
127 | | int32_t Curl_ngh2_session_get_local_window_size(struct curl_ngh2 *ngh2) |
128 | 0 | { |
129 | 0 | if(ngh2->fatal_rv) |
130 | 0 | return 0; |
131 | 0 | return nghttp2_session_get_local_window_size(ngh2->s); |
132 | 0 | } |
133 | | |
134 | | int32_t Curl_ngh2_session_get_stream_remote_window_size(struct curl_ngh2 *ngh2, |
135 | | int32_t stream_id) |
136 | 0 | { |
137 | 0 | if(ngh2->fatal_rv) |
138 | 0 | return 0; |
139 | 0 | return nghttp2_session_get_stream_remote_window_size(ngh2->s, stream_id); |
140 | 0 | } |
141 | | |
142 | | int32_t Curl_ngh2_session_get_remote_window_size(struct curl_ngh2 *ngh2) |
143 | 0 | { |
144 | 0 | if(ngh2->fatal_rv) |
145 | 0 | return 0; |
146 | 0 | return nghttp2_session_get_remote_window_size(ngh2->s); |
147 | 0 | } |
148 | | |
149 | | int Curl_ngh2_session_set_local_window_size(struct curl_ngh2 *ngh2, |
150 | | uint8_t flags, int32_t stream_id, |
151 | | int32_t window_size) |
152 | 0 | { |
153 | 0 | if(ngh2->fatal_rv) |
154 | 0 | return ngh2->fatal_rv; |
155 | 0 | return ngh2_check(ngh2, nghttp2_session_set_local_window_size(ngh2->s, flags, |
156 | 0 | stream_id, |
157 | 0 | window_size)); |
158 | 0 | } |
159 | | |
160 | | int Curl_ngh2_submit_window_update(struct curl_ngh2 *ngh2, uint8_t flags, |
161 | | int32_t stream_id, |
162 | | int32_t window_size_increment) |
163 | 0 | { |
164 | 0 | if(ngh2->fatal_rv) |
165 | 0 | return ngh2->fatal_rv; |
166 | 0 | return ngh2_check(ngh2, |
167 | 0 | nghttp2_submit_window_update(ngh2->s, flags, stream_id, |
168 | 0 | window_size_increment)); |
169 | 0 | } |
170 | | |
171 | | int Curl_ngh2_submit_rst_stream(struct curl_ngh2 *ngh2, uint8_t flags, |
172 | | int32_t stream_id, uint32_t error_code) |
173 | 0 | { |
174 | 0 | if(ngh2->fatal_rv) |
175 | 0 | return ngh2->fatal_rv; |
176 | 0 | return ngh2_check(ngh2, nghttp2_submit_rst_stream(ngh2->s, flags, stream_id, |
177 | 0 | error_code)); |
178 | 0 | } |
179 | | |
180 | | int Curl_ngh2_session_send(struct curl_ngh2 *ngh2) |
181 | 0 | { |
182 | 0 | int rv; |
183 | 0 | if(ngh2->fatal_rv) |
184 | 0 | return ngh2->fatal_rv; |
185 | 0 | rv = ngh2_check(ngh2, nghttp2_session_send(ngh2->s)); |
186 | | /* a callback might trigger a fatal state without session_send() |
187 | | * noticing. Check again. */ |
188 | 0 | return ngh2->fatal_rv ? ngh2->fatal_rv : rv; |
189 | 0 | } |
190 | | |
191 | | int Curl_ngh2_session_client_new3(struct curl_ngh2 *ngh2, |
192 | | const nghttp2_session_callbacks *callbacks, |
193 | | void *user_data, |
194 | | const nghttp2_option *option, |
195 | | nghttp2_mem *mem) |
196 | 0 | { |
197 | 0 | if(ngh2->s) |
198 | 0 | ngh2->fatal_rv = NGHTTP2_ERR_FATAL; |
199 | 0 | if(ngh2->fatal_rv) |
200 | 0 | return ngh2->fatal_rv; |
201 | 0 | return ngh2_check(ngh2, nghttp2_session_client_new3(&ngh2->s, callbacks, |
202 | 0 | user_data, option, mem)); |
203 | 0 | } |
204 | | |
205 | | int Curl_ngh2_session_want_read(struct curl_ngh2 *ngh2) |
206 | 0 | { |
207 | 0 | if(!Curl_ngh2_valid(ngh2)) |
208 | 0 | return 0; |
209 | 0 | return nghttp2_session_want_read(ngh2->s); |
210 | 0 | } |
211 | | |
212 | | int Curl_ngh2_session_want_write(struct curl_ngh2 *ngh2) |
213 | 0 | { |
214 | 0 | if(!Curl_ngh2_valid(ngh2)) |
215 | 0 | return 0; |
216 | 0 | return nghttp2_session_want_write(ngh2->s); |
217 | 0 | } |
218 | | |
219 | | ssize_t Curl_ngh2_session_mem_recv(struct curl_ngh2 *ngh2, const uint8_t *in, |
220 | | size_t inlen) |
221 | 0 | { |
222 | 0 | ssize_t rv; |
223 | 0 | if(ngh2->fatal_rv) |
224 | 0 | return ngh2->fatal_rv; |
225 | 0 | rv = ngh2_checks(ngh2, nghttp2_session_mem_recv(ngh2->s, in, inlen)); |
226 | | /* the session might encounter a fatal state in a callback, |
227 | | * but session_mem_rec() might not notice and return a non-error. |
228 | | * Check again. */ |
229 | 0 | return ngh2->fatal_rv ? ngh2->fatal_rv : rv; |
230 | 0 | } |
231 | | |
232 | | int Curl_ngh2_session_check_request_allowed(struct curl_ngh2 *ngh2) |
233 | 0 | { |
234 | 0 | if(!Curl_ngh2_valid(ngh2)) |
235 | 0 | return 0; |
236 | 0 | return nghttp2_session_check_request_allowed(ngh2->s); |
237 | 0 | } |
238 | | |
239 | | int Curl_ngh2_submit_ping(struct curl_ngh2 *ngh2, uint8_t flags, |
240 | | const uint8_t *opaque_data) |
241 | 0 | { |
242 | 0 | if(ngh2->fatal_rv) |
243 | 0 | return ngh2->fatal_rv; |
244 | 0 | return ngh2_check(ngh2, nghttp2_submit_ping(ngh2->s, flags, opaque_data)); |
245 | 0 | } |
246 | | |
247 | | int32_t Curl_ngh2_session_get_stream_effective_recv_data_length( |
248 | | struct curl_ngh2 *ngh2, int32_t stream_id) |
249 | 0 | { |
250 | 0 | if(!Curl_ngh2_valid(ngh2)) |
251 | 0 | return 0; |
252 | 0 | return nghttp2_session_get_stream_effective_recv_data_length(ngh2->s, |
253 | 0 | stream_id); |
254 | 0 | } |
255 | | |
256 | | int Curl_ngh2_session_resume_data(struct curl_ngh2 *ngh2, int32_t stream_id) |
257 | 0 | { |
258 | 0 | if(ngh2->fatal_rv) |
259 | 0 | return ngh2->fatal_rv; |
260 | 0 | return ngh2_check(ngh2, nghttp2_session_resume_data(ngh2->s, stream_id)); |
261 | 0 | } |
262 | | |
263 | | int Curl_ngh2_session_consume(struct curl_ngh2 *ngh2, int32_t stream_id, |
264 | | size_t size) |
265 | 0 | { |
266 | 0 | if(ngh2->fatal_rv) |
267 | 0 | return ngh2->fatal_rv; |
268 | 0 | return ngh2_check(ngh2, nghttp2_session_consume(ngh2->s, stream_id, size)); |
269 | 0 | } |
270 | | |
271 | | int Curl_ngh2_submit_priority(struct curl_ngh2 *ngh2, uint8_t flags, |
272 | | int32_t stream_id, |
273 | | const nghttp2_priority_spec *pri_spec) |
274 | 0 | { |
275 | 0 | if(ngh2->fatal_rv) |
276 | 0 | return ngh2->fatal_rv; |
277 | 0 | return ngh2_check(ngh2, nghttp2_submit_priority(ngh2->s, flags, stream_id, |
278 | 0 | pri_spec)); |
279 | 0 | } |
280 | | |
281 | | int32_t Curl_ngh2_submit_request(struct curl_ngh2 *ngh2, |
282 | | const nghttp2_priority_spec *pri_spec, |
283 | | const nghttp2_nv *nva, size_t nvlen, |
284 | | const nghttp2_data_provider *data_prd, |
285 | | void *stream_user_data) |
286 | 0 | { |
287 | 0 | if(ngh2->fatal_rv) |
288 | 0 | return (int32_t)ngh2->fatal_rv; |
289 | 0 | return ngh2_check32(ngh2, |
290 | 0 | nghttp2_submit_request(ngh2->s, pri_spec, nva, nvlen, |
291 | 0 | data_prd, stream_user_data)); |
292 | 0 | } |
293 | | |
294 | | int Curl_ngh2_session_upgrade2(struct curl_ngh2 *ngh2, |
295 | | const uint8_t *settings_payload, |
296 | | size_t settings_payloadlen, int head_request, |
297 | | void *stream_user_data) |
298 | 0 | { |
299 | 0 | if(ngh2->fatal_rv) |
300 | 0 | return ngh2->fatal_rv; |
301 | 0 | return ngh2_check(ngh2, |
302 | 0 | nghttp2_session_upgrade2(ngh2->s, settings_payload, |
303 | 0 | settings_payloadlen, head_request, |
304 | 0 | stream_user_data)); |
305 | 0 | } |
306 | | |
307 | | int Curl_ngh2_submit_goaway(struct curl_ngh2 *ngh2, uint8_t flags, |
308 | | int32_t last_stream_id, uint32_t error_code, |
309 | | const uint8_t *opaque_data, size_t opaque_data_len) |
310 | 0 | { |
311 | 0 | if(ngh2->fatal_rv) |
312 | 0 | return ngh2->fatal_rv; |
313 | 0 | return ngh2_check(ngh2, nghttp2_submit_goaway(ngh2->s, flags, last_stream_id, |
314 | 0 | error_code, opaque_data, |
315 | 0 | opaque_data_len)); |
316 | 0 | } |
317 | | |
318 | | #endif /* !CURL_DISABLE_HTTP && USE_NGHTTP2 */ |