/src/curl/lib/cf-h2-proxy.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(CURL_DISABLE_PROXY) && \ |
27 | | defined(USE_NGHTTP2) |
28 | | |
29 | | #include <nghttp2/nghttp2.h> |
30 | | |
31 | | #include "urldata.h" |
32 | | #include "url.h" |
33 | | #include "cfilters.h" |
34 | | #include "connect.h" |
35 | | #include "curl_trc.h" |
36 | | #include "bufq.h" |
37 | | #include "curlx/dynbuf.h" |
38 | | #include "dynhds.h" |
39 | | #include "http2.h" |
40 | | #include "http_proxy.h" |
41 | | #include "multiif.h" |
42 | | #include "sendf.h" |
43 | | #include "select.h" |
44 | | #include "cf-h2-proxy.h" |
45 | | #include "capsule.h" |
46 | | |
47 | 0 | #define PROXY_H2_CHUNK_SIZE (16 * 1024) |
48 | | |
49 | 0 | #define PROXY_HTTP2_HUGE_WINDOW_SIZE (100 * 1024 * 1024) |
50 | 0 | #define H2_TUNNEL_WINDOW_SIZE (10 * 1024 * 1024) |
51 | | |
52 | 0 | #define PROXY_H2_NW_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE) |
53 | 0 | #define PROXY_H2_NW_SEND_CHUNKS 1 |
54 | | |
55 | 0 | #define H2_TUNNEL_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE) |
56 | 0 | #define H2_TUNNEL_SEND_CHUNKS ((128 * 1024) / PROXY_H2_CHUNK_SIZE) |
57 | | |
58 | | |
59 | | typedef enum { |
60 | | H2_TUNNEL_INIT, /* init/default/no tunnel state */ |
61 | | H2_TUNNEL_CONNECT, /* CONNECT request is being send */ |
62 | | H2_TUNNEL_RESPONSE, /* CONNECT response received completely */ |
63 | | H2_TUNNEL_ESTABLISHED, |
64 | | H2_TUNNEL_FAILED |
65 | | } h2_tunnel_state; |
66 | | |
67 | | struct tunnel_stream { |
68 | | struct http_resp *resp; |
69 | | struct bufq recvbuf; |
70 | | struct bufq sendbuf; |
71 | | char *authority; |
72 | | int32_t stream_id; |
73 | | uint32_t error; |
74 | | h2_tunnel_state state; |
75 | | BIT(has_final_response); |
76 | | BIT(closed); |
77 | | BIT(reset); |
78 | | }; |
79 | | |
80 | | static CURLcode tunnel_stream_init(struct tunnel_stream *ts, |
81 | | struct Curl_peer *dest) |
82 | 0 | { |
83 | 0 | ts->state = H2_TUNNEL_INIT; |
84 | 0 | ts->stream_id = -1; |
85 | 0 | Curl_bufq_init2(&ts->recvbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_RECV_CHUNKS, |
86 | 0 | BUFQ_OPT_SOFT_LIMIT); |
87 | 0 | Curl_bufq_init(&ts->sendbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_SEND_CHUNKS); |
88 | | |
89 | | /* host:port with IPv6 support */ |
90 | 0 | ts->authority = curl_maprintf("%s%s%s:%u", dest->ipv6 ? "[" : "", |
91 | 0 | dest->hostname, |
92 | 0 | dest->ipv6 ? "]" : "", |
93 | 0 | dest->port); |
94 | 0 | if(!ts->authority) |
95 | 0 | return CURLE_OUT_OF_MEMORY; |
96 | | |
97 | 0 | return CURLE_OK; |
98 | 0 | } |
99 | | |
100 | | static void tunnel_stream_reset(struct tunnel_stream *ts) |
101 | 0 | { |
102 | 0 | Curl_http_resp_free(ts->resp); |
103 | 0 | ts->resp = NULL; |
104 | 0 | Curl_bufq_reset(&ts->recvbuf); |
105 | 0 | Curl_bufq_reset(&ts->sendbuf); |
106 | 0 | ts->stream_id = -1; |
107 | 0 | ts->error = 0; |
108 | 0 | ts->has_final_response = FALSE; |
109 | 0 | ts->closed = FALSE; |
110 | 0 | ts->reset = FALSE; |
111 | 0 | ts->state = H2_TUNNEL_INIT; |
112 | 0 | } |
113 | | |
114 | | static void tunnel_stream_clear(struct tunnel_stream *ts) |
115 | 0 | { |
116 | 0 | Curl_http_resp_free(ts->resp); |
117 | 0 | Curl_bufq_free(&ts->recvbuf); |
118 | 0 | Curl_bufq_free(&ts->sendbuf); |
119 | 0 | curlx_safefree(ts->authority); |
120 | 0 | memset(ts, 0, sizeof(*ts)); |
121 | 0 | ts->state = H2_TUNNEL_INIT; |
122 | 0 | } |
123 | | |
124 | | static void h2_tunnel_go_state(struct Curl_cfilter *cf, |
125 | | struct tunnel_stream *ts, |
126 | | h2_tunnel_state new_state, |
127 | | struct Curl_easy *data, |
128 | | bool udp_tunnel) |
129 | 0 | { |
130 | 0 | (void)cf; |
131 | 0 | (void)udp_tunnel; |
132 | |
|
133 | 0 | if(ts->state == new_state) |
134 | 0 | return; |
135 | | /* leaving this one */ |
136 | 0 | switch(ts->state) { |
137 | 0 | case H2_TUNNEL_CONNECT: |
138 | 0 | data->req.ignorebody = FALSE; |
139 | 0 | break; |
140 | 0 | default: |
141 | 0 | break; |
142 | 0 | } |
143 | | /* entering this one */ |
144 | 0 | switch(new_state) { |
145 | 0 | case H2_TUNNEL_INIT: |
146 | 0 | CURL_TRC_CF(data, cf, "[%d] new tunnel state 'init'", ts->stream_id); |
147 | 0 | tunnel_stream_reset(ts); |
148 | 0 | break; |
149 | | |
150 | 0 | case H2_TUNNEL_CONNECT: |
151 | 0 | CURL_TRC_CF(data, cf, "[%d] new tunnel state 'connect'", ts->stream_id); |
152 | 0 | ts->state = H2_TUNNEL_CONNECT; |
153 | 0 | break; |
154 | | |
155 | 0 | case H2_TUNNEL_RESPONSE: |
156 | 0 | CURL_TRC_CF(data, cf, "[%d] new tunnel state 'response'", ts->stream_id); |
157 | 0 | ts->state = H2_TUNNEL_RESPONSE; |
158 | 0 | break; |
159 | | |
160 | 0 | case H2_TUNNEL_ESTABLISHED: |
161 | 0 | CURL_TRC_CF(data, cf, "[%d] new tunnel state 'established'", |
162 | 0 | ts->stream_id); |
163 | 0 | infof(data, "CONNECT%s phase completed for HTTP/2 proxy", |
164 | 0 | udp_tunnel ? "-UDP" : ""); |
165 | 0 | data->state.authproxy.done = TRUE; |
166 | 0 | data->state.authproxy.multipass = FALSE; |
167 | 0 | FALLTHROUGH(); |
168 | 0 | case H2_TUNNEL_FAILED: |
169 | 0 | if(new_state == H2_TUNNEL_FAILED) |
170 | 0 | CURL_TRC_CF(data, cf, "[%d] new tunnel state 'failed'", ts->stream_id); |
171 | 0 | ts->state = new_state; |
172 | | /* If a proxy-authorization header was used for the proxy, then we should |
173 | | make sure that it is not accidentally used for the document request |
174 | | after we have connected. Let's thus free and clear it here. */ |
175 | 0 | curlx_safefree(data->req.hd_proxy_auth); |
176 | 0 | break; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | struct cf_h2_proxy_ctx { |
181 | | nghttp2_session *h2; |
182 | | /* The easy handle used in the current filter call, cleared at return */ |
183 | | struct cf_call_data call_data; |
184 | | |
185 | | struct bufq inbufq; /* network receive buffer */ |
186 | | struct bufq outbufq; /* network send buffer */ |
187 | | |
188 | | struct Curl_peer *dest; /* where to tunnel to */ |
189 | | struct tunnel_stream tunnel; /* our tunnel CONNECT stream */ |
190 | | int32_t goaway_error; |
191 | | int32_t last_stream_id; |
192 | | BIT(conn_closed); |
193 | | BIT(rcvd_goaway); |
194 | | BIT(sent_goaway); |
195 | | BIT(nw_out_blocked); |
196 | | BIT(udp_tunnel); |
197 | | }; |
198 | | |
199 | | /* How to access `call_data` from a cf_h2 filter */ |
200 | | #undef CF_CTX_CALL_DATA |
201 | 0 | #define CF_CTX_CALL_DATA(cf) ((struct cf_h2_proxy_ctx *)(cf)->ctx)->call_data |
202 | | |
203 | | static void cf_h2_proxy_ctx_clear(struct cf_h2_proxy_ctx *ctx) |
204 | 0 | { |
205 | 0 | struct cf_call_data save = ctx->call_data; |
206 | |
|
207 | 0 | if(ctx->h2) { |
208 | 0 | nghttp2_session_del(ctx->h2); |
209 | 0 | } |
210 | 0 | Curl_bufq_free(&ctx->inbufq); |
211 | 0 | Curl_bufq_free(&ctx->outbufq); |
212 | 0 | Curl_peer_unlink(&ctx->dest); |
213 | 0 | tunnel_stream_clear(&ctx->tunnel); |
214 | 0 | memset(ctx, 0, sizeof(*ctx)); |
215 | 0 | ctx->call_data = save; |
216 | 0 | } |
217 | | |
218 | | static void cf_h2_proxy_ctx_free(struct cf_h2_proxy_ctx *ctx) |
219 | 0 | { |
220 | 0 | if(ctx) { |
221 | 0 | cf_h2_proxy_ctx_clear(ctx); |
222 | 0 | curlx_free(ctx); |
223 | 0 | } |
224 | 0 | } |
225 | | |
226 | | static void drain_tunnel(struct Curl_cfilter *cf, |
227 | | struct Curl_easy *data, |
228 | | struct tunnel_stream *tunnel) |
229 | 0 | { |
230 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
231 | 0 | (void)cf; |
232 | 0 | if(!tunnel->closed && !tunnel->reset && |
233 | 0 | (!Curl_bufq_is_empty(&ctx->tunnel.sendbuf) || |
234 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.recvbuf))) |
235 | 0 | Curl_multi_mark_dirty(data); |
236 | 0 | } |
237 | | |
238 | | static CURLcode proxy_h2_nw_out_writer(void *writer_ctx, |
239 | | const uint8_t *buf, size_t buflen, |
240 | | size_t *pnwritten) |
241 | 0 | { |
242 | 0 | struct Curl_cfilter *cf = writer_ctx; |
243 | 0 | *pnwritten = 0; |
244 | 0 | if(cf) { |
245 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
246 | 0 | CURLcode result; |
247 | 0 | result = Curl_conn_cf_send(cf->next, data, buf, buflen, FALSE, pnwritten); |
248 | 0 | CURL_TRC_CF(data, cf, "[0] nw_out_writer(len=%zu) -> %d, %zu", |
249 | 0 | buflen, result, *pnwritten); |
250 | 0 | return result; |
251 | 0 | } |
252 | 0 | return CURLE_FAILED_INIT; |
253 | 0 | } |
254 | | |
255 | | static int proxy_h2_client_new(struct Curl_cfilter *cf, |
256 | | nghttp2_session_callbacks *cbs) |
257 | 0 | { |
258 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
259 | 0 | nghttp2_option *o; |
260 | 0 | nghttp2_mem mem = { NULL, Curl_nghttp2_malloc, Curl_nghttp2_free, |
261 | 0 | Curl_nghttp2_calloc, Curl_nghttp2_realloc }; |
262 | |
|
263 | 0 | int rc = nghttp2_option_new(&o); |
264 | 0 | if(rc) |
265 | 0 | return rc; |
266 | | /* We handle window updates ourself to enforce buffer limits */ |
267 | 0 | nghttp2_option_set_no_auto_window_update(o, 1); |
268 | 0 | #if NGHTTP2_VERSION_NUM >= 0x013200 |
269 | | /* with 1.50.0 */ |
270 | | /* turn off RFC 9113 leading and trailing white spaces validation against |
271 | | HTTP field value. */ |
272 | 0 | nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1); |
273 | 0 | #endif |
274 | 0 | rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem); |
275 | 0 | nghttp2_option_del(o); |
276 | 0 | return rc; |
277 | 0 | } |
278 | | |
279 | | static int proxy_h2_should_close_session(struct cf_h2_proxy_ctx *ctx) |
280 | 0 | { |
281 | 0 | return !nghttp2_session_want_read(ctx->h2) && |
282 | 0 | !nghttp2_session_want_write(ctx->h2); |
283 | 0 | } |
284 | | |
285 | | static CURLcode proxy_h2_nw_out_flush(struct Curl_cfilter *cf, |
286 | | struct Curl_easy *data) |
287 | 0 | { |
288 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
289 | 0 | size_t nwritten; |
290 | 0 | CURLcode result; |
291 | |
|
292 | 0 | if(Curl_bufq_is_empty(&ctx->outbufq)) |
293 | 0 | return CURLE_OK; |
294 | | |
295 | 0 | result = Curl_bufq_pass(&ctx->outbufq, proxy_h2_nw_out_writer, cf, |
296 | 0 | &nwritten); |
297 | 0 | if(result) { |
298 | 0 | if(result == CURLE_AGAIN) { |
299 | 0 | CURL_TRC_CF(data, cf, "[0] flush nw send buffer(%zu) -> EAGAIN", |
300 | 0 | Curl_bufq_len(&ctx->outbufq)); |
301 | 0 | ctx->nw_out_blocked = 1; |
302 | 0 | } |
303 | 0 | return result; |
304 | 0 | } |
305 | 0 | CURL_TRC_CF(data, cf, "[0] nw send buffer flushed"); |
306 | 0 | return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN; |
307 | 0 | } |
308 | | |
309 | | /* |
310 | | * Processes pending input left in network input buffer. |
311 | | * This function returns 0 if it succeeds, or -1 and error code will |
312 | | * be assigned to *err. |
313 | | */ |
314 | | static CURLcode proxy_h2_process_pending_input(struct Curl_cfilter *cf, |
315 | | struct Curl_easy *data) |
316 | 0 | { |
317 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
318 | 0 | const unsigned char *buf; |
319 | 0 | size_t blen, nread; |
320 | 0 | ssize_t rv; |
321 | |
|
322 | 0 | while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) { |
323 | |
|
324 | 0 | rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen); |
325 | 0 | CURL_TRC_CF(data, cf, "[0] %zu bytes to nghttp2 -> %zd", blen, rv); |
326 | 0 | if(!curlx_sztouz(rv, &nread)) { |
327 | 0 | failf(data, |
328 | 0 | "process_pending_input: nghttp2_session_mem_recv() returned " |
329 | 0 | "%zd:%s", rv, nghttp2_strerror((int)rv)); |
330 | 0 | return CURLE_RECV_ERROR; |
331 | 0 | } |
332 | 0 | else if(!nread) { |
333 | | /* nghttp2 does not want to process more, but has no error. This |
334 | | * probably cannot happen, but be safe. */ |
335 | 0 | break; |
336 | 0 | } |
337 | 0 | Curl_bufq_skip(&ctx->inbufq, nread); |
338 | 0 | if(Curl_bufq_is_empty(&ctx->inbufq)) { |
339 | 0 | CURL_TRC_CF(data, cf, "[0] all data in connection buffer processed"); |
340 | 0 | break; |
341 | 0 | } |
342 | 0 | else { |
343 | 0 | CURL_TRC_CF(data, cf, "[0] process_pending_input: %zu bytes left " |
344 | 0 | "in connection buffer", Curl_bufq_len(&ctx->inbufq)); |
345 | 0 | } |
346 | 0 | } |
347 | 0 | return CURLE_OK; |
348 | 0 | } |
349 | | |
350 | | static CURLcode proxy_h2_progress_ingress(struct Curl_cfilter *cf, |
351 | | struct Curl_easy *data) |
352 | 0 | { |
353 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
354 | 0 | CURLcode result = CURLE_OK; |
355 | 0 | size_t nread; |
356 | | |
357 | | /* Process network input buffer first */ |
358 | 0 | if(!Curl_bufq_is_empty(&ctx->inbufq)) { |
359 | 0 | CURL_TRC_CF(data, cf, "[0] process %zu bytes in connection buffer", |
360 | 0 | Curl_bufq_len(&ctx->inbufq)); |
361 | 0 | result = proxy_h2_process_pending_input(cf, data); |
362 | 0 | if(result) |
363 | 0 | return result; |
364 | 0 | } |
365 | | |
366 | | /* Receive data from the "lower" filters, e.g. network until |
367 | | * it is time to stop or we have enough data for this stream */ |
368 | 0 | while(!ctx->conn_closed && /* not closed the connection */ |
369 | 0 | !ctx->tunnel.closed && /* nor the tunnel */ |
370 | 0 | Curl_bufq_is_empty(&ctx->inbufq) && /* and we consumed our input */ |
371 | 0 | !Curl_bufq_is_full(&ctx->tunnel.recvbuf)) { |
372 | |
|
373 | 0 | result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread); |
374 | 0 | CURL_TRC_CF(data, cf, "[0] read %zu bytes nw data -> %d, %zu", |
375 | 0 | Curl_bufq_len(&ctx->inbufq), result, nread); |
376 | 0 | if(result) { |
377 | 0 | if(result != CURLE_AGAIN) { |
378 | 0 | failf(data, "Failed receiving HTTP2 proxy data"); |
379 | 0 | return result; |
380 | 0 | } |
381 | 0 | break; |
382 | 0 | } |
383 | 0 | else if(nread == 0) { |
384 | 0 | CURL_TRC_CF(data, cf, "server closed connection"); |
385 | 0 | ctx->conn_closed = TRUE; |
386 | 0 | break; |
387 | 0 | } |
388 | | |
389 | 0 | result = proxy_h2_process_pending_input(cf, data); |
390 | 0 | if(result) |
391 | 0 | return result; |
392 | 0 | } |
393 | | |
394 | 0 | return CURLE_OK; |
395 | 0 | } |
396 | | |
397 | | static CURLcode proxy_h2_progress_egress(struct Curl_cfilter *cf, |
398 | | struct Curl_easy *data) |
399 | 0 | { |
400 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
401 | 0 | int rv = 0; |
402 | |
|
403 | 0 | ctx->nw_out_blocked = 0; |
404 | 0 | while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2)) |
405 | 0 | rv = nghttp2_session_send(ctx->h2); |
406 | |
|
407 | 0 | if(nghttp2_is_fatal(rv)) { |
408 | 0 | CURL_TRC_CF(data, cf, "[0] nghttp2_session_send error (%s)%d", |
409 | 0 | nghttp2_strerror(rv), rv); |
410 | 0 | return CURLE_SEND_ERROR; |
411 | 0 | } |
412 | 0 | return proxy_h2_nw_out_flush(cf, data); |
413 | 0 | } |
414 | | |
415 | | static ssize_t on_session_send(nghttp2_session *h2, |
416 | | const uint8_t *buf, size_t blen, int flags, |
417 | | void *userp) |
418 | 0 | { |
419 | 0 | struct Curl_cfilter *cf = userp; |
420 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
421 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
422 | 0 | size_t nwritten; |
423 | 0 | CURLcode result = CURLE_OK; |
424 | |
|
425 | 0 | (void)h2; |
426 | 0 | (void)flags; |
427 | 0 | DEBUGASSERT(data); |
428 | |
|
429 | 0 | result = Curl_bufq_write_pass(&ctx->outbufq, buf, blen, |
430 | 0 | proxy_h2_nw_out_writer, cf, &nwritten); |
431 | 0 | if(result) { |
432 | 0 | if(result == CURLE_AGAIN) { |
433 | 0 | ctx->nw_out_blocked = 1; |
434 | 0 | return NGHTTP2_ERR_WOULDBLOCK; |
435 | 0 | } |
436 | 0 | failf(data, "Failed sending HTTP2 data"); |
437 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
438 | 0 | } |
439 | | |
440 | 0 | if(!nwritten) |
441 | 0 | return NGHTTP2_ERR_WOULDBLOCK; |
442 | | |
443 | 0 | return (nwritten > SSIZE_MAX) ? |
444 | 0 | NGHTTP2_ERR_CALLBACK_FAILURE : (ssize_t)nwritten; |
445 | 0 | } |
446 | | |
447 | | #ifdef CURLVERBOSE |
448 | | static int proxy_h2_on_frame_send(nghttp2_session *session, |
449 | | const nghttp2_frame *frame, |
450 | | void *userp) |
451 | 0 | { |
452 | 0 | struct Curl_cfilter *cf = userp; |
453 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
454 | |
|
455 | 0 | (void)session; |
456 | 0 | DEBUGASSERT(data); |
457 | 0 | if(Curl_trc_cf_is_verbose(cf, data)) { |
458 | 0 | char buffer[256]; |
459 | 0 | int len; |
460 | 0 | len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1); |
461 | 0 | buffer[len] = 0; |
462 | 0 | CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer); |
463 | 0 | } |
464 | 0 | return 0; |
465 | 0 | } |
466 | | #endif /* CURLVERBOSE */ |
467 | | |
468 | | static int proxy_h2_on_frame_recv(nghttp2_session *session, |
469 | | const nghttp2_frame *frame, |
470 | | void *userp) |
471 | 0 | { |
472 | 0 | struct Curl_cfilter *cf = userp; |
473 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
474 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
475 | 0 | int32_t stream_id = frame->hd.stream_id; |
476 | |
|
477 | 0 | (void)session; |
478 | 0 | DEBUGASSERT(data); |
479 | 0 | #ifdef CURLVERBOSE |
480 | 0 | if(Curl_trc_cf_is_verbose(cf, data)) { |
481 | 0 | char buffer[256]; |
482 | 0 | int len; |
483 | 0 | len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1); |
484 | 0 | buffer[len] = 0; |
485 | 0 | CURL_TRC_CF(data, cf, "[%d] <- %s", frame->hd.stream_id, buffer); |
486 | 0 | } |
487 | 0 | #endif /* CURLVERBOSE */ |
488 | |
|
489 | 0 | if(!stream_id) { |
490 | | /* stream ID zero is for connection-oriented stuff */ |
491 | 0 | DEBUGASSERT(data); |
492 | 0 | switch(frame->hd.type) { |
493 | 0 | case NGHTTP2_SETTINGS: |
494 | | /* Since the initial stream window is 64K, a request might be on HOLD, |
495 | | * due to exhaustion. The (initial) SETTINGS may announce a much larger |
496 | | * window and *assume* that we treat this like a WINDOW_UPDATE. Some |
497 | | * servers send an explicit WINDOW_UPDATE, but not all seem to do that. |
498 | | * To be safe, we UNHOLD a stream in order not to stall. */ |
499 | 0 | if(CURL_REQ_WANT_SEND(data)) { |
500 | 0 | drain_tunnel(cf, data, &ctx->tunnel); |
501 | 0 | } |
502 | 0 | break; |
503 | 0 | case NGHTTP2_GOAWAY: |
504 | 0 | ctx->rcvd_goaway = TRUE; |
505 | 0 | break; |
506 | 0 | default: |
507 | 0 | break; |
508 | 0 | } |
509 | 0 | return 0; |
510 | 0 | } |
511 | | |
512 | 0 | if(stream_id != ctx->tunnel.stream_id) { |
513 | 0 | CURL_TRC_CF(data, cf, "[%d] rcvd FRAME not for tunnel", stream_id); |
514 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
515 | 0 | } |
516 | | |
517 | 0 | switch(frame->hd.type) { |
518 | 0 | case NGHTTP2_HEADERS: |
519 | | /* nghttp2 guarantees that :status is received, and we store it to |
520 | | stream->status_code. Fuzzing has proven this can still be reached |
521 | | without status code having been set. */ |
522 | 0 | if(!ctx->tunnel.resp) |
523 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
524 | | /* Only final status code signals the end of header */ |
525 | 0 | CURL_TRC_CF(data, cf, "[%d] got http status: %d", |
526 | 0 | stream_id, ctx->tunnel.resp->status); |
527 | 0 | if(!ctx->tunnel.has_final_response) { |
528 | 0 | if(ctx->tunnel.resp->status / 100 != 1) { |
529 | 0 | ctx->tunnel.has_final_response = TRUE; |
530 | 0 | } |
531 | 0 | } |
532 | 0 | break; |
533 | 0 | case NGHTTP2_WINDOW_UPDATE: |
534 | 0 | if(CURL_REQ_WANT_SEND(data)) { |
535 | 0 | drain_tunnel(cf, data, &ctx->tunnel); |
536 | 0 | } |
537 | 0 | break; |
538 | 0 | case NGHTTP2_RST_STREAM: |
539 | 0 | if(frame->rst_stream.error_code) |
540 | 0 | ctx->tunnel.reset = TRUE; |
541 | 0 | break; |
542 | 0 | default: |
543 | 0 | break; |
544 | 0 | } |
545 | 0 | return 0; |
546 | 0 | } |
547 | | |
548 | | static int proxy_h2_on_header(nghttp2_session *session, |
549 | | const nghttp2_frame *frame, |
550 | | const uint8_t *name, size_t namelen, |
551 | | const uint8_t *value, size_t valuelen, |
552 | | uint8_t flags, |
553 | | void *userp) |
554 | 0 | { |
555 | 0 | struct Curl_cfilter *cf = userp; |
556 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
557 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
558 | 0 | int32_t stream_id = frame->hd.stream_id; |
559 | 0 | CURLcode result; |
560 | |
|
561 | 0 | (void)flags; |
562 | 0 | (void)session; |
563 | 0 | DEBUGASSERT(stream_id); /* should never be a zero stream ID here */ |
564 | 0 | if(stream_id != ctx->tunnel.stream_id) { |
565 | 0 | CURL_TRC_CF(data, cf, "[%d] header for non-tunnel stream: " |
566 | 0 | "%.*s: %.*s", stream_id, |
567 | 0 | (int)namelen, name, (int)valuelen, value); |
568 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
569 | 0 | } |
570 | | |
571 | 0 | if(frame->hd.type == NGHTTP2_PUSH_PROMISE) |
572 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
573 | | |
574 | 0 | if(ctx->tunnel.has_final_response) { |
575 | | /* we do not do anything with trailers for tunnel streams */ |
576 | 0 | return 0; |
577 | 0 | } |
578 | | |
579 | 0 | if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 && |
580 | 0 | memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) { |
581 | 0 | int http_status; |
582 | 0 | struct http_resp *resp; |
583 | | |
584 | | /* status: always comes first, we might get more than one response, |
585 | | * discard previous, interim responses */ |
586 | 0 | result = Curl_http_decode_status(&http_status, |
587 | 0 | (const char *)value, valuelen); |
588 | 0 | if(result) |
589 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
590 | 0 | result = Curl_http_resp_make(&resp, http_status, NULL); |
591 | 0 | if(result) |
592 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
593 | 0 | if(ctx->tunnel.resp) |
594 | 0 | Curl_http_resp_free(ctx->tunnel.resp); |
595 | 0 | ctx->tunnel.resp = resp; |
596 | 0 | CURL_TRC_CF(data, cf, "[%d] status: HTTP/2 %03d", |
597 | 0 | stream_id, ctx->tunnel.resp->status); |
598 | 0 | return 0; |
599 | 0 | } |
600 | | |
601 | 0 | if(!ctx->tunnel.resp) |
602 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
603 | | |
604 | 0 | result = Curl_dynhds_add(&ctx->tunnel.resp->headers, |
605 | 0 | (const char *)name, namelen, |
606 | 0 | (const char *)value, valuelen); |
607 | 0 | if(result) |
608 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
609 | | |
610 | 0 | CURL_TRC_CF(data, cf, "[%d] header: %.*s: %.*s", |
611 | 0 | stream_id, (int)namelen, name, (int)valuelen, value); |
612 | |
|
613 | 0 | return 0; /* 0 is successful */ |
614 | 0 | } |
615 | | |
616 | | static ssize_t tunnel_send_callback(nghttp2_session *session, |
617 | | int32_t stream_id, |
618 | | uint8_t *buf, size_t length, |
619 | | uint32_t *data_flags, |
620 | | nghttp2_data_source *source, |
621 | | void *userp) |
622 | 0 | { |
623 | 0 | struct Curl_cfilter *cf = userp; |
624 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
625 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
626 | 0 | struct tunnel_stream *ts; |
627 | 0 | CURLcode result; |
628 | 0 | size_t nread; |
629 | |
|
630 | 0 | (void)source; |
631 | 0 | (void)ctx; |
632 | |
|
633 | 0 | if(!stream_id) |
634 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
635 | | |
636 | 0 | ts = nghttp2_session_get_stream_user_data(session, stream_id); |
637 | 0 | if(!ts) |
638 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
639 | 0 | DEBUGASSERT(ts == &ctx->tunnel); |
640 | |
|
641 | 0 | result = Curl_bufq_read(&ts->sendbuf, buf, length, &nread); |
642 | 0 | if(result) { |
643 | 0 | if(result != CURLE_AGAIN) |
644 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
645 | 0 | return NGHTTP2_ERR_DEFERRED; |
646 | 0 | } |
647 | 0 | if(ts->closed && Curl_bufq_is_empty(&ts->sendbuf)) |
648 | 0 | *data_flags = NGHTTP2_DATA_FLAG_EOF; |
649 | |
|
650 | 0 | CURL_TRC_CF(data, cf, "[%d] tunnel_send_callback -> %zu", |
651 | 0 | ts->stream_id, nread); |
652 | 0 | return (nread > SSIZE_MAX) ? |
653 | 0 | NGHTTP2_ERR_CALLBACK_FAILURE : (ssize_t)nread; |
654 | 0 | } |
655 | | |
656 | | static int tunnel_recv_callback(nghttp2_session *session, uint8_t flags, |
657 | | int32_t stream_id, |
658 | | const uint8_t *mem, size_t len, void *userp) |
659 | 0 | { |
660 | 0 | struct Curl_cfilter *cf = userp; |
661 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
662 | 0 | size_t nwritten; |
663 | 0 | CURLcode result; |
664 | |
|
665 | 0 | (void)flags; |
666 | 0 | (void)session; |
667 | 0 | DEBUGASSERT(stream_id); /* should never be a zero stream ID here */ |
668 | |
|
669 | 0 | if(stream_id != ctx->tunnel.stream_id) |
670 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
671 | | |
672 | 0 | result = Curl_bufq_write(&ctx->tunnel.recvbuf, mem, len, &nwritten); |
673 | 0 | if(result) { |
674 | 0 | if(result != CURLE_AGAIN) |
675 | 0 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
676 | 0 | #ifdef DEBUGBUILD |
677 | 0 | nwritten = 0; |
678 | 0 | #endif |
679 | 0 | } |
680 | | /* tunnel.recbuf has soft limit, any success MUST add all data */ |
681 | 0 | DEBUGASSERT(nwritten == len); |
682 | 0 | return 0; |
683 | 0 | } |
684 | | |
685 | | static int proxy_h2_on_stream_close(nghttp2_session *session, |
686 | | int32_t stream_id, |
687 | | uint32_t error_code, void *userp) |
688 | 0 | { |
689 | 0 | struct Curl_cfilter *cf = userp; |
690 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
691 | 0 | struct Curl_easy *data = CF_DATA_CURRENT(cf); |
692 | |
|
693 | 0 | (void)session; |
694 | |
|
695 | 0 | if(stream_id != ctx->tunnel.stream_id) |
696 | 0 | return 0; |
697 | | |
698 | 0 | CURL_TRC_CF(data, cf, "[%d] proxy_h2_on_stream_close, %s (err %u)", |
699 | 0 | stream_id, nghttp2_http2_strerror(error_code), error_code); |
700 | 0 | ctx->tunnel.closed = TRUE; |
701 | 0 | ctx->tunnel.error = error_code; |
702 | 0 | if(error_code) |
703 | 0 | ctx->tunnel.reset = TRUE; |
704 | |
|
705 | 0 | return 0; |
706 | 0 | } |
707 | | |
708 | | static CURLcode proxy_h2_submit( |
709 | | int32_t *pstream_id, |
710 | | struct Curl_cfilter *cf, |
711 | | struct Curl_easy *data, |
712 | | nghttp2_session *h2, |
713 | | struct httpreq *req, |
714 | | const nghttp2_priority_spec *pri_spec, |
715 | | void *stream_user_data, |
716 | | nghttp2_data_source_read_callback read_callback, |
717 | | void *read_ctx) |
718 | 0 | { |
719 | 0 | struct dynhds h2_headers; |
720 | 0 | nghttp2_nv *nva = NULL; |
721 | 0 | int32_t stream_id = -1; |
722 | 0 | size_t nheader; |
723 | 0 | CURLcode result; |
724 | |
|
725 | 0 | (void)cf; |
726 | 0 | Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST); |
727 | 0 | result = Curl_http_req_to_h2(&h2_headers, req, data); |
728 | 0 | if(result) |
729 | 0 | goto out; |
730 | | |
731 | 0 | nva = Curl_dynhds_to_nva(&h2_headers, &nheader); |
732 | 0 | if(!nva) { |
733 | 0 | result = CURLE_OUT_OF_MEMORY; |
734 | 0 | goto out; |
735 | 0 | } |
736 | | |
737 | 0 | if(read_callback) { |
738 | 0 | nghttp2_data_provider data_prd; |
739 | |
|
740 | 0 | data_prd.read_callback = read_callback; |
741 | 0 | data_prd.source.ptr = read_ctx; |
742 | 0 | stream_id = nghttp2_submit_request(h2, pri_spec, nva, nheader, |
743 | 0 | &data_prd, stream_user_data); |
744 | 0 | } |
745 | 0 | else { |
746 | 0 | stream_id = nghttp2_submit_request(h2, pri_spec, nva, nheader, |
747 | 0 | NULL, stream_user_data); |
748 | 0 | } |
749 | |
|
750 | 0 | if(stream_id < 0) { |
751 | 0 | failf(data, "nghttp2_session_upgrade2() failed: %s(%d)", |
752 | 0 | nghttp2_strerror(stream_id), stream_id); |
753 | 0 | result = CURLE_SEND_ERROR; |
754 | 0 | goto out; |
755 | 0 | } |
756 | 0 | result = CURLE_OK; |
757 | |
|
758 | 0 | out: |
759 | 0 | curlx_free(nva); |
760 | 0 | Curl_dynhds_free(&h2_headers); |
761 | 0 | *pstream_id = stream_id; |
762 | 0 | return result; |
763 | 0 | } |
764 | | |
765 | | static CURLcode submit_CONNECT(struct Curl_cfilter *cf, |
766 | | struct Curl_easy *data, |
767 | | struct tunnel_stream *ts) |
768 | 0 | { |
769 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
770 | 0 | CURLcode result; |
771 | 0 | struct httpreq *req = NULL; |
772 | |
|
773 | 0 | result = Curl_http_proxy_create_tunnel_request(&req, cf, data, ctx->dest, |
774 | 0 | PROXY_HTTP_V2, |
775 | 0 | (bool)ctx->udp_tunnel); |
776 | 0 | if(result) |
777 | 0 | goto out; |
778 | 0 | result = Curl_creader_set_null(data); |
779 | 0 | if(result) |
780 | 0 | goto out; |
781 | | |
782 | 0 | result = proxy_h2_submit(&ts->stream_id, cf, data, ctx->h2, req, |
783 | 0 | NULL, ts, tunnel_send_callback, cf); |
784 | 0 | if(result) { |
785 | 0 | CURL_TRC_CF(data, cf, "[%d] send, nghttp2_submit_request error: %s", |
786 | 0 | ts->stream_id, nghttp2_strerror(ts->stream_id)); |
787 | 0 | } |
788 | |
|
789 | 0 | out: |
790 | 0 | if(req) |
791 | 0 | Curl_http_req_free(req); |
792 | 0 | if(result) |
793 | 0 | failf(data, "Failed sending CONNECT to proxy"); |
794 | 0 | return result; |
795 | 0 | } |
796 | | |
797 | | static CURLcode inspect_response(struct Curl_cfilter *cf, |
798 | | struct Curl_easy *data, |
799 | | struct tunnel_stream *ts) |
800 | 0 | { |
801 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
802 | 0 | proxy_inspect_result res; |
803 | 0 | CURLcode result; |
804 | |
|
805 | 0 | result = Curl_http_proxy_inspect_tunnel_response( |
806 | 0 | cf, data, ts->resp, (bool)ctx->udp_tunnel, &res); |
807 | 0 | if(result) |
808 | 0 | return result; |
809 | 0 | switch(res) { |
810 | 0 | case PROXY_INSPECT_OK: |
811 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_ESTABLISHED, data, |
812 | 0 | (bool)ctx->udp_tunnel); |
813 | 0 | break; |
814 | 0 | case PROXY_INSPECT_FAILED: |
815 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data, |
816 | 0 | (bool)ctx->udp_tunnel); |
817 | 0 | result = CURLE_COULDNT_CONNECT; |
818 | 0 | break; |
819 | 0 | case PROXY_INSPECT_AUTH_RETRY: |
820 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_INIT, data, |
821 | 0 | (bool)ctx->udp_tunnel); |
822 | 0 | break; |
823 | 0 | } |
824 | 0 | return result; |
825 | 0 | } |
826 | | |
827 | | static CURLcode H2_CONNECT(struct Curl_cfilter *cf, |
828 | | struct Curl_easy *data, |
829 | | struct tunnel_stream *ts) |
830 | 0 | { |
831 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
832 | 0 | CURLcode result = CURLE_OK; |
833 | |
|
834 | 0 | DEBUGASSERT(ts); |
835 | 0 | DEBUGASSERT(ts->authority); |
836 | 0 | if(ctx->conn_closed) { |
837 | 0 | failf(data, "proxy closed connection"); |
838 | 0 | return CURLE_COULDNT_CONNECT; |
839 | 0 | } |
840 | | |
841 | 0 | do { |
842 | 0 | switch(ts->state) { |
843 | 0 | case H2_TUNNEL_INIT: |
844 | | /* Prepare the CONNECT request and make a first attempt to send. */ |
845 | 0 | CURL_TRC_CF(data, cf, "[0] CONNECT start for %s", ts->authority); |
846 | 0 | result = submit_CONNECT(cf, data, ts); |
847 | 0 | if(result) |
848 | 0 | goto out; |
849 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_CONNECT, data, |
850 | 0 | (bool)ctx->udp_tunnel); |
851 | 0 | FALLTHROUGH(); |
852 | |
|
853 | 0 | case H2_TUNNEL_CONNECT: |
854 | | /* see that the request is completely sent */ |
855 | 0 | result = proxy_h2_progress_ingress(cf, data); |
856 | 0 | if(!result) |
857 | 0 | result = proxy_h2_progress_egress(cf, data); |
858 | 0 | if(result && result != CURLE_AGAIN) { |
859 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data, |
860 | 0 | (bool)ctx->udp_tunnel); |
861 | 0 | break; |
862 | 0 | } |
863 | | |
864 | 0 | if(ts->has_final_response) { |
865 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_RESPONSE, data, |
866 | 0 | (bool)ctx->udp_tunnel); |
867 | 0 | } |
868 | 0 | else { |
869 | 0 | result = CURLE_OK; |
870 | 0 | goto out; |
871 | 0 | } |
872 | 0 | FALLTHROUGH(); |
873 | |
|
874 | 0 | case H2_TUNNEL_RESPONSE: |
875 | 0 | DEBUGASSERT(ts->has_final_response); |
876 | 0 | result = inspect_response(cf, data, ts); |
877 | 0 | if(result) |
878 | 0 | goto out; |
879 | 0 | break; |
880 | | |
881 | 0 | case H2_TUNNEL_ESTABLISHED: |
882 | 0 | return CURLE_OK; |
883 | | |
884 | 0 | case H2_TUNNEL_FAILED: |
885 | 0 | return CURLE_RECV_ERROR; |
886 | | |
887 | 0 | default: |
888 | 0 | break; |
889 | 0 | } |
890 | |
|
891 | 0 | } while(ts->state == H2_TUNNEL_INIT); |
892 | | |
893 | 0 | out: |
894 | 0 | if((result && (result != CURLE_AGAIN)) || ctx->tunnel.closed) |
895 | 0 | h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data, |
896 | 0 | (bool)ctx->udp_tunnel); |
897 | 0 | return result; |
898 | 0 | } |
899 | | |
900 | | /* |
901 | | * Initialize the cfilter context |
902 | | */ |
903 | | static CURLcode cf_h2_proxy_ctx_init(struct Curl_cfilter *cf, |
904 | | struct Curl_easy *data) |
905 | 0 | { |
906 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
907 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
908 | 0 | nghttp2_session_callbacks *cbs = NULL; |
909 | 0 | int rc; |
910 | |
|
911 | 0 | DEBUGASSERT(!ctx->h2); |
912 | 0 | memset(&ctx->tunnel, 0, sizeof(ctx->tunnel)); |
913 | |
|
914 | 0 | Curl_bufq_init(&ctx->inbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_RECV_CHUNKS); |
915 | 0 | Curl_bufq_init(&ctx->outbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_SEND_CHUNKS); |
916 | |
|
917 | 0 | if(tunnel_stream_init(&ctx->tunnel, ctx->dest)) |
918 | 0 | goto out; |
919 | | |
920 | 0 | rc = nghttp2_session_callbacks_new(&cbs); |
921 | 0 | if(rc) { |
922 | 0 | failf(data, "Could not initialize nghttp2 callbacks"); |
923 | 0 | goto out; |
924 | 0 | } |
925 | | |
926 | 0 | nghttp2_session_callbacks_set_send_callback(cbs, on_session_send); |
927 | 0 | nghttp2_session_callbacks_set_on_frame_recv_callback( |
928 | 0 | cbs, proxy_h2_on_frame_recv); |
929 | 0 | #ifdef CURLVERBOSE |
930 | 0 | nghttp2_session_callbacks_set_on_frame_send_callback(cbs, |
931 | 0 | proxy_h2_on_frame_send); |
932 | 0 | #endif |
933 | 0 | nghttp2_session_callbacks_set_on_data_chunk_recv_callback( |
934 | 0 | cbs, tunnel_recv_callback); |
935 | 0 | nghttp2_session_callbacks_set_on_stream_close_callback( |
936 | 0 | cbs, proxy_h2_on_stream_close); |
937 | 0 | nghttp2_session_callbacks_set_on_header_callback(cbs, proxy_h2_on_header); |
938 | | |
939 | | /* The nghttp2 session is not yet setup, do it */ |
940 | 0 | rc = proxy_h2_client_new(cf, cbs); |
941 | 0 | if(rc) { |
942 | 0 | failf(data, "Could not initialize nghttp2"); |
943 | 0 | goto out; |
944 | 0 | } |
945 | | |
946 | 0 | { |
947 | 0 | nghttp2_settings_entry iv[3]; |
948 | |
|
949 | 0 | iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; |
950 | 0 | iv[0].value = Curl_multi_max_concurrent_streams(data->multi); |
951 | 0 | iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; |
952 | 0 | iv[1].value = H2_TUNNEL_WINDOW_SIZE; |
953 | 0 | iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH; |
954 | 0 | iv[2].value = 0; |
955 | 0 | rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE, iv, 3); |
956 | 0 | if(rc) { |
957 | 0 | failf(data, "nghttp2_submit_settings() failed: %s(%d)", |
958 | 0 | nghttp2_strerror(rc), rc); |
959 | 0 | result = CURLE_HTTP2; |
960 | 0 | goto out; |
961 | 0 | } |
962 | 0 | } |
963 | | |
964 | 0 | rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0, |
965 | 0 | PROXY_HTTP2_HUGE_WINDOW_SIZE); |
966 | 0 | if(rc) { |
967 | 0 | failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)", |
968 | 0 | nghttp2_strerror(rc), rc); |
969 | 0 | result = CURLE_HTTP2; |
970 | 0 | goto out; |
971 | 0 | } |
972 | | |
973 | | /* all set, traffic will be send on connect */ |
974 | 0 | result = CURLE_OK; |
975 | |
|
976 | 0 | out: |
977 | 0 | if(cbs) |
978 | 0 | nghttp2_session_callbacks_del(cbs); |
979 | 0 | CURL_TRC_CF(data, cf, "[0] init proxy ctx -> %d", result); |
980 | 0 | return result; |
981 | 0 | } |
982 | | |
983 | | static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf, |
984 | | struct Curl_easy *data, |
985 | | bool *done) |
986 | 0 | { |
987 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
988 | 0 | CURLcode result = CURLE_OK; |
989 | 0 | struct cf_call_data save; |
990 | 0 | struct tunnel_stream *ts = &ctx->tunnel; |
991 | |
|
992 | 0 | if(cf->connected) { |
993 | 0 | *done = TRUE; |
994 | 0 | return CURLE_OK; |
995 | 0 | } |
996 | | |
997 | | /* Connect the lower filters first */ |
998 | 0 | if(!cf->next->connected) { |
999 | 0 | result = Curl_conn_cf_connect(cf->next, data, done); |
1000 | 0 | if(result || !*done) |
1001 | 0 | return result; |
1002 | 0 | } |
1003 | | |
1004 | 0 | *done = FALSE; |
1005 | |
|
1006 | 0 | CF_DATA_SAVE(save, cf, data); |
1007 | 0 | if(!ctx->h2) { |
1008 | 0 | result = cf_h2_proxy_ctx_init(cf, data); |
1009 | 0 | if(result) |
1010 | 0 | goto out; |
1011 | 0 | } |
1012 | 0 | DEBUGASSERT(ts->authority); |
1013 | |
|
1014 | 0 | if(Curl_timeleft_ms(data) < 0) { |
1015 | 0 | failf(data, "Proxy CONNECT aborted due to timeout"); |
1016 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1017 | 0 | goto out; |
1018 | 0 | } |
1019 | | |
1020 | | /* for the secondary socket (FTP), use the "connect to host" |
1021 | | * but ignore the "connect to port" (use the secondary port) |
1022 | | */ |
1023 | 0 | result = H2_CONNECT(cf, data, ts); |
1024 | |
|
1025 | 0 | out: |
1026 | 0 | *done = (result == CURLE_OK) && (ts->state == H2_TUNNEL_ESTABLISHED); |
1027 | 0 | if(*done) { |
1028 | 0 | cf->connected = TRUE; |
1029 | | /* The real request will follow the CONNECT, reset request partially */ |
1030 | 0 | Curl_req_soft_reset(&data->req, data); |
1031 | 0 | Curl_client_reset(data); |
1032 | 0 | } |
1033 | 0 | CF_DATA_RESTORE(cf, save); |
1034 | 0 | return result; |
1035 | 0 | } |
1036 | | |
1037 | | static void cf_h2_proxy_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
1038 | 0 | { |
1039 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1040 | |
|
1041 | 0 | if(ctx) { |
1042 | 0 | struct cf_call_data save; |
1043 | |
|
1044 | 0 | CF_DATA_SAVE(save, cf, data); |
1045 | 0 | cf_h2_proxy_ctx_clear(ctx); |
1046 | 0 | CF_DATA_RESTORE(cf, save); |
1047 | 0 | } |
1048 | 0 | if(cf->next) |
1049 | 0 | cf->next->cft->do_close(cf->next, data); |
1050 | 0 | } |
1051 | | |
1052 | | static void cf_h2_proxy_destroy(struct Curl_cfilter *cf, |
1053 | | struct Curl_easy *data) |
1054 | 0 | { |
1055 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1056 | |
|
1057 | 0 | (void)data; |
1058 | 0 | if(ctx) { |
1059 | 0 | cf_h2_proxy_ctx_free(ctx); |
1060 | 0 | cf->ctx = NULL; |
1061 | 0 | } |
1062 | 0 | } |
1063 | | |
1064 | | static CURLcode cf_h2_proxy_shutdown(struct Curl_cfilter *cf, |
1065 | | struct Curl_easy *data, bool *done) |
1066 | 0 | { |
1067 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1068 | 0 | struct cf_call_data save; |
1069 | 0 | CURLcode result; |
1070 | 0 | int rv; |
1071 | |
|
1072 | 0 | if(!cf->connected || !ctx->h2 || cf->shutdown || ctx->conn_closed) { |
1073 | 0 | *done = TRUE; |
1074 | 0 | return CURLE_OK; |
1075 | 0 | } |
1076 | | |
1077 | 0 | CF_DATA_SAVE(save, cf, data); |
1078 | |
|
1079 | 0 | if(!ctx->sent_goaway) { |
1080 | 0 | rv = nghttp2_submit_goaway(ctx->h2, NGHTTP2_FLAG_NONE, |
1081 | 0 | 0, 0, |
1082 | 0 | (const uint8_t *)"shutdown", |
1083 | 0 | sizeof("shutdown")); |
1084 | 0 | if(rv) { |
1085 | 0 | failf(data, "nghttp2_submit_goaway() failed: %s(%d)", |
1086 | 0 | nghttp2_strerror(rv), rv); |
1087 | 0 | result = CURLE_SEND_ERROR; |
1088 | 0 | goto out; |
1089 | 0 | } |
1090 | 0 | ctx->sent_goaway = TRUE; |
1091 | 0 | } |
1092 | | /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */ |
1093 | 0 | result = CURLE_OK; |
1094 | 0 | if(nghttp2_session_want_write(ctx->h2)) |
1095 | 0 | result = proxy_h2_progress_egress(cf, data); |
1096 | 0 | if(!result && nghttp2_session_want_read(ctx->h2)) |
1097 | 0 | result = proxy_h2_progress_ingress(cf, data); |
1098 | |
|
1099 | 0 | *done = (ctx->conn_closed || |
1100 | 0 | (!result && !nghttp2_session_want_write(ctx->h2) && |
1101 | 0 | !nghttp2_session_want_read(ctx->h2))); |
1102 | 0 | out: |
1103 | 0 | CF_DATA_RESTORE(cf, save); |
1104 | 0 | cf->shutdown = (result || *done); |
1105 | 0 | return result; |
1106 | 0 | } |
1107 | | |
1108 | | static bool cf_h2_proxy_data_pending(struct Curl_cfilter *cf, |
1109 | | const struct Curl_easy *data) |
1110 | 0 | { |
1111 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1112 | 0 | if((ctx && !Curl_bufq_is_empty(&ctx->inbufq)) || |
1113 | 0 | (ctx && ctx->tunnel.state == H2_TUNNEL_ESTABLISHED && |
1114 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.recvbuf))) |
1115 | 0 | return TRUE; |
1116 | 0 | return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE; |
1117 | 0 | } |
1118 | | |
1119 | | static CURLcode cf_h2_proxy_adjust_pollset(struct Curl_cfilter *cf, |
1120 | | struct Curl_easy *data, |
1121 | | struct easy_pollset *ps) |
1122 | 0 | { |
1123 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1124 | 0 | struct cf_call_data save; |
1125 | 0 | curl_socket_t sock = Curl_conn_cf_get_socket(cf, data); |
1126 | 0 | bool want_recv, want_send; |
1127 | 0 | CURLcode result = CURLE_OK; |
1128 | |
|
1129 | 0 | if(!cf->connected && ctx->h2) { |
1130 | 0 | want_send = nghttp2_session_want_write(ctx->h2) || |
1131 | 0 | !Curl_bufq_is_empty(&ctx->outbufq) || |
1132 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.sendbuf); |
1133 | 0 | want_recv = nghttp2_session_want_read(ctx->h2); |
1134 | 0 | } |
1135 | 0 | else |
1136 | 0 | Curl_pollset_check(data, ps, sock, &want_recv, &want_send); |
1137 | |
|
1138 | 0 | if(ctx->h2 && (want_recv || want_send)) { |
1139 | 0 | bool c_exhaust, s_exhaust; |
1140 | |
|
1141 | 0 | CF_DATA_SAVE(save, cf, data); |
1142 | 0 | c_exhaust = !nghttp2_session_get_remote_window_size(ctx->h2); |
1143 | 0 | s_exhaust = ctx->tunnel.stream_id >= 0 && |
1144 | 0 | !nghttp2_session_get_stream_remote_window_size( |
1145 | 0 | ctx->h2, ctx->tunnel.stream_id); |
1146 | 0 | want_recv = (want_recv || c_exhaust || s_exhaust); |
1147 | 0 | want_send = (!s_exhaust && want_send) || |
1148 | 0 | (!c_exhaust && nghttp2_session_want_write(ctx->h2)) || |
1149 | 0 | !Curl_bufq_is_empty(&ctx->outbufq) || |
1150 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.sendbuf); |
1151 | |
|
1152 | 0 | result = Curl_pollset_set(data, ps, sock, want_recv, want_send); |
1153 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d -> %d", |
1154 | 0 | want_recv, want_send, result); |
1155 | 0 | CF_DATA_RESTORE(cf, save); |
1156 | 0 | } |
1157 | 0 | else if(ctx->sent_goaway && !cf->shutdown) { |
1158 | | /* shutdown in progress */ |
1159 | 0 | CF_DATA_SAVE(save, cf, data); |
1160 | 0 | want_send = nghttp2_session_want_write(ctx->h2) || |
1161 | 0 | !Curl_bufq_is_empty(&ctx->outbufq) || |
1162 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.sendbuf); |
1163 | 0 | want_recv = nghttp2_session_want_read(ctx->h2); |
1164 | 0 | result = Curl_pollset_set(data, ps, sock, want_recv, want_send); |
1165 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d -> %d", |
1166 | 0 | want_recv, want_send, result); |
1167 | 0 | CF_DATA_RESTORE(cf, save); |
1168 | 0 | } |
1169 | 0 | return result; |
1170 | 0 | } |
1171 | | |
1172 | | static CURLcode h2_handle_tunnel_close(struct Curl_cfilter *cf, |
1173 | | struct Curl_easy *data, |
1174 | | size_t *pnread) |
1175 | 0 | { |
1176 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1177 | |
|
1178 | 0 | *pnread = 0; |
1179 | 0 | if(ctx->tunnel.error) { |
1180 | 0 | failf(data, "HTTP/2 stream %d reset by %s (error 0x%x %s)", |
1181 | 0 | ctx->tunnel.stream_id, ctx->tunnel.reset ? "server" : "curl", |
1182 | 0 | ctx->tunnel.error, nghttp2_http2_strerror(ctx->tunnel.error)); |
1183 | 0 | return CURLE_RECV_ERROR; |
1184 | 0 | } |
1185 | | |
1186 | 0 | CURL_TRC_CF(data, cf, "[%d] handle_tunnel_close -> 0", |
1187 | 0 | ctx->tunnel.stream_id); |
1188 | 0 | return CURLE_OK; |
1189 | 0 | } |
1190 | | |
1191 | | static CURLcode tunnel_recv(struct Curl_cfilter *cf, struct Curl_easy *data, |
1192 | | char *buf, size_t len, size_t *pnread) |
1193 | 0 | { |
1194 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1195 | 0 | CURLcode result = CURLE_AGAIN; |
1196 | |
|
1197 | 0 | *pnread = 0; |
1198 | 0 | if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) |
1199 | 0 | result = Curl_bufq_cread(&ctx->tunnel.recvbuf, buf, len, pnread); |
1200 | 0 | else { |
1201 | 0 | if(ctx->tunnel.closed) { |
1202 | 0 | result = h2_handle_tunnel_close(cf, data, pnread); |
1203 | 0 | } |
1204 | 0 | else if(ctx->tunnel.reset || |
1205 | 0 | (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) || |
1206 | 0 | (ctx->rcvd_goaway && |
1207 | 0 | ctx->last_stream_id < ctx->tunnel.stream_id)) { |
1208 | 0 | result = CURLE_RECV_ERROR; |
1209 | 0 | } |
1210 | 0 | else |
1211 | 0 | result = CURLE_AGAIN; |
1212 | 0 | } |
1213 | |
|
1214 | 0 | CURL_TRC_CF(data, cf, "[%d] tunnel_recv(len=%zu) -> %d, %zu", |
1215 | 0 | ctx->tunnel.stream_id, len, result, *pnread); |
1216 | 0 | return result; |
1217 | 0 | } |
1218 | | |
1219 | | static CURLcode cf_h2_proxy_recv(struct Curl_cfilter *cf, |
1220 | | struct Curl_easy *data, |
1221 | | char *buf, size_t len, |
1222 | | size_t *pnread) |
1223 | 0 | { |
1224 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1225 | 0 | struct cf_call_data save; |
1226 | 0 | CURLcode result; |
1227 | |
|
1228 | 0 | *pnread = 0; |
1229 | 0 | CF_DATA_SAVE(save, cf, data); |
1230 | |
|
1231 | 0 | if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) { |
1232 | 0 | result = CURLE_RECV_ERROR; |
1233 | 0 | goto out; |
1234 | 0 | } |
1235 | | |
1236 | 0 | if(Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) { |
1237 | 0 | result = proxy_h2_progress_ingress(cf, data); |
1238 | 0 | if(result) |
1239 | 0 | goto out; |
1240 | 0 | } |
1241 | | |
1242 | 0 | result = tunnel_recv(cf, data, buf, len, pnread); |
1243 | |
|
1244 | 0 | if(!result) { |
1245 | 0 | CURL_TRC_CF(data, cf, "[%d] increase window by %zu", |
1246 | 0 | ctx->tunnel.stream_id, *pnread); |
1247 | 0 | nghttp2_session_consume(ctx->h2, ctx->tunnel.stream_id, *pnread); |
1248 | 0 | } |
1249 | |
|
1250 | 0 | result = Curl_1st_fatal(result, proxy_h2_progress_egress(cf, data)); |
1251 | |
|
1252 | 0 | out: |
1253 | 0 | if((!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) || |
1254 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) && |
1255 | 0 | (!result || (result == CURLE_AGAIN))) { |
1256 | | /* data pending and no fatal error to report. Need to trigger |
1257 | | * draining to avoid stalling when no socket events happen. */ |
1258 | 0 | drain_tunnel(cf, data, &ctx->tunnel); |
1259 | 0 | } |
1260 | 0 | CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %d, %zu", |
1261 | 0 | ctx->tunnel.stream_id, len, result, *pnread); |
1262 | 0 | CF_DATA_RESTORE(cf, save); |
1263 | 0 | return result; |
1264 | 0 | } |
1265 | | |
1266 | | static CURLcode cf_h2_proxy_send(struct Curl_cfilter *cf, |
1267 | | struct Curl_easy *data, |
1268 | | const uint8_t *buf, size_t len, bool eos, |
1269 | | size_t *pnwritten) |
1270 | 0 | { |
1271 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1272 | 0 | struct cf_call_data save; |
1273 | 0 | int rv; |
1274 | 0 | CURLcode result; |
1275 | |
|
1276 | 0 | (void)eos; |
1277 | 0 | *pnwritten = 0; |
1278 | 0 | CF_DATA_SAVE(save, cf, data); |
1279 | |
|
1280 | 0 | if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) { |
1281 | 0 | result = CURLE_SEND_ERROR; |
1282 | 0 | goto out; |
1283 | 0 | } |
1284 | | |
1285 | 0 | if(ctx->tunnel.closed) { |
1286 | 0 | result = CURLE_SEND_ERROR; |
1287 | 0 | goto out; |
1288 | 0 | } |
1289 | | |
1290 | 0 | result = Curl_bufq_write(&ctx->tunnel.sendbuf, buf, len, pnwritten); |
1291 | 0 | CURL_TRC_CF(data, cf, "cf_send(), bufq_write %d, %zu", result, *pnwritten); |
1292 | 0 | if(result && (result != CURLE_AGAIN)) |
1293 | 0 | goto out; |
1294 | | |
1295 | 0 | if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) { |
1296 | | /* req body data is buffered, resume the potentially suspended stream */ |
1297 | 0 | rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id); |
1298 | 0 | if(nghttp2_is_fatal(rv)) { |
1299 | 0 | result = CURLE_SEND_ERROR; |
1300 | 0 | goto out; |
1301 | 0 | } |
1302 | 0 | } |
1303 | | |
1304 | 0 | result = Curl_1st_fatal(result, proxy_h2_progress_ingress(cf, data)); |
1305 | 0 | result = Curl_1st_fatal(result, proxy_h2_progress_egress(cf, data)); |
1306 | |
|
1307 | 0 | if(!result && proxy_h2_should_close_session(ctx)) { |
1308 | | /* nghttp2 thinks this session is done. If the stream has not been |
1309 | | * closed, this is an error state for out transfer */ |
1310 | 0 | if(ctx->tunnel.closed) { |
1311 | 0 | result = CURLE_SEND_ERROR; |
1312 | 0 | } |
1313 | 0 | else { |
1314 | 0 | CURL_TRC_CF(data, cf, "[0] send: nothing to do in this session"); |
1315 | 0 | result = CURLE_HTTP2; |
1316 | 0 | } |
1317 | 0 | } |
1318 | |
|
1319 | 0 | out: |
1320 | 0 | if((!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) || |
1321 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) && |
1322 | 0 | (!result || (result == CURLE_AGAIN))) { |
1323 | | /* data pending and no fatal error to report. Need to trigger |
1324 | | * draining to avoid stalling when no socket events happen. */ |
1325 | 0 | drain_tunnel(cf, data, &ctx->tunnel); |
1326 | 0 | } |
1327 | 0 | CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %d, %zu, " |
1328 | 0 | "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)", |
1329 | 0 | ctx->tunnel.stream_id, len, result, *pnwritten, |
1330 | 0 | nghttp2_session_get_stream_remote_window_size( |
1331 | 0 | ctx->h2, ctx->tunnel.stream_id), |
1332 | 0 | nghttp2_session_get_remote_window_size(ctx->h2), |
1333 | 0 | Curl_bufq_len(&ctx->tunnel.sendbuf), |
1334 | 0 | Curl_bufq_len(&ctx->outbufq)); |
1335 | 0 | CF_DATA_RESTORE(cf, save); |
1336 | 0 | return result; |
1337 | 0 | } |
1338 | | |
1339 | | static CURLcode cf_h2_proxy_flush(struct Curl_cfilter *cf, |
1340 | | struct Curl_easy *data) |
1341 | 0 | { |
1342 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1343 | 0 | struct cf_call_data save; |
1344 | 0 | CURLcode result = CURLE_OK; |
1345 | |
|
1346 | 0 | CF_DATA_SAVE(save, cf, data); |
1347 | 0 | if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) { |
1348 | | /* resume the potentially suspended tunnel */ |
1349 | 0 | int rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id); |
1350 | 0 | if(nghttp2_is_fatal(rv)) { |
1351 | 0 | result = CURLE_SEND_ERROR; |
1352 | 0 | goto out; |
1353 | 0 | } |
1354 | 0 | } |
1355 | | |
1356 | 0 | result = proxy_h2_progress_egress(cf, data); |
1357 | |
|
1358 | 0 | out: |
1359 | 0 | CURL_TRC_CF(data, cf, "[%d] flush -> %d, " |
1360 | 0 | "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)", |
1361 | 0 | ctx->tunnel.stream_id, result, |
1362 | 0 | nghttp2_session_get_stream_remote_window_size( |
1363 | 0 | ctx->h2, ctx->tunnel.stream_id), |
1364 | 0 | nghttp2_session_get_remote_window_size(ctx->h2), |
1365 | 0 | Curl_bufq_len(&ctx->tunnel.sendbuf), |
1366 | 0 | Curl_bufq_len(&ctx->outbufq)); |
1367 | 0 | CF_DATA_RESTORE(cf, save); |
1368 | 0 | return result; |
1369 | 0 | } |
1370 | | |
1371 | | static bool proxy_h2_connisalive(struct Curl_cfilter *cf, |
1372 | | struct Curl_easy *data, |
1373 | | bool *input_pending) |
1374 | 0 | { |
1375 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1376 | 0 | bool alive = TRUE; |
1377 | |
|
1378 | 0 | *input_pending = FALSE; |
1379 | 0 | if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending)) |
1380 | 0 | return FALSE; |
1381 | | |
1382 | 0 | if(*input_pending) { |
1383 | | /* This happens before we have sent off a request and the connection is |
1384 | | not in use by any other transfer, there should not be any data here, |
1385 | | only "protocol frames" */ |
1386 | 0 | CURLcode result; |
1387 | 0 | size_t nread; |
1388 | |
|
1389 | 0 | *input_pending = FALSE; |
1390 | 0 | result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread); |
1391 | 0 | if(!result) { |
1392 | 0 | if(proxy_h2_process_pending_input(cf, data)) |
1393 | | /* immediate error, considered dead */ |
1394 | 0 | alive = FALSE; |
1395 | 0 | else { |
1396 | 0 | alive = !proxy_h2_should_close_session(ctx); |
1397 | 0 | } |
1398 | 0 | } |
1399 | 0 | else if(result != CURLE_AGAIN) { |
1400 | | /* the read failed so let's say this is dead anyway */ |
1401 | 0 | alive = FALSE; |
1402 | 0 | } |
1403 | 0 | } |
1404 | |
|
1405 | 0 | return alive; |
1406 | 0 | } |
1407 | | |
1408 | | static bool cf_h2_proxy_is_alive(struct Curl_cfilter *cf, |
1409 | | struct Curl_easy *data, |
1410 | | bool *input_pending) |
1411 | 0 | { |
1412 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1413 | 0 | bool alive; |
1414 | 0 | struct cf_call_data save; |
1415 | |
|
1416 | 0 | *input_pending = FALSE; |
1417 | 0 | CF_DATA_SAVE(save, cf, data); |
1418 | 0 | alive = (ctx && ctx->h2 && proxy_h2_connisalive(cf, data, input_pending)); |
1419 | 0 | CURL_TRC_CF(data, cf, "[0] conn alive -> %d, input_pending=%d", |
1420 | 0 | alive, *input_pending); |
1421 | 0 | CF_DATA_RESTORE(cf, save); |
1422 | 0 | return alive; |
1423 | 0 | } |
1424 | | |
1425 | | static CURLcode cf_h2_proxy_query(struct Curl_cfilter *cf, |
1426 | | struct Curl_easy *data, |
1427 | | int query, int *pres1, void *pres2) |
1428 | 0 | { |
1429 | 0 | struct cf_h2_proxy_ctx *ctx = cf->ctx; |
1430 | |
|
1431 | 0 | switch(query) { |
1432 | 0 | case CF_QUERY_HOST_PORT: |
1433 | 0 | *pres1 = (int)ctx->dest->port; |
1434 | 0 | *((const char **)pres2) = ctx->dest->hostname; |
1435 | 0 | return CURLE_OK; |
1436 | 0 | case CF_QUERY_NEED_FLUSH: { |
1437 | 0 | if(!Curl_bufq_is_empty(&ctx->outbufq) || |
1438 | 0 | !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) { |
1439 | 0 | CURL_TRC_CF(data, cf, "needs flush"); |
1440 | 0 | *pres1 = TRUE; |
1441 | 0 | return CURLE_OK; |
1442 | 0 | } |
1443 | 0 | break; |
1444 | 0 | } |
1445 | 0 | case CF_QUERY_ALPN_NEGOTIATED: { |
1446 | 0 | const char **palpn = pres2; |
1447 | 0 | DEBUGASSERT(palpn); |
1448 | 0 | *palpn = NULL; |
1449 | 0 | return CURLE_OK; |
1450 | 0 | } |
1451 | 0 | default: |
1452 | 0 | break; |
1453 | 0 | } |
1454 | 0 | return cf->next ? |
1455 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1456 | 0 | CURLE_UNKNOWN_OPTION; |
1457 | 0 | } |
1458 | | |
1459 | | static CURLcode cf_h2_proxy_cntrl(struct Curl_cfilter *cf, |
1460 | | struct Curl_easy *data, |
1461 | | int event, int arg1, void *arg2) |
1462 | 0 | { |
1463 | 0 | CURLcode result = CURLE_OK; |
1464 | 0 | struct cf_call_data save; |
1465 | |
|
1466 | 0 | (void)arg1; |
1467 | 0 | (void)arg2; |
1468 | |
|
1469 | 0 | switch(event) { |
1470 | 0 | case CF_CTRL_FLUSH: |
1471 | 0 | CF_DATA_SAVE(save, cf, data); |
1472 | 0 | result = cf_h2_proxy_flush(cf, data); |
1473 | 0 | CF_DATA_RESTORE(cf, save); |
1474 | 0 | break; |
1475 | 0 | default: |
1476 | 0 | break; |
1477 | 0 | } |
1478 | 0 | return result; |
1479 | 0 | } |
1480 | | |
1481 | | struct Curl_cftype Curl_cft_h2_proxy = { |
1482 | | "H2-PROXY", |
1483 | | CF_TYPE_IP_CONNECT | CF_TYPE_PROXY, |
1484 | | CURL_LOG_LVL_NONE, |
1485 | | cf_h2_proxy_destroy, |
1486 | | cf_h2_proxy_connect, |
1487 | | cf_h2_proxy_close, |
1488 | | cf_h2_proxy_shutdown, |
1489 | | cf_h2_proxy_adjust_pollset, |
1490 | | cf_h2_proxy_data_pending, |
1491 | | cf_h2_proxy_send, |
1492 | | cf_h2_proxy_recv, |
1493 | | cf_h2_proxy_cntrl, |
1494 | | cf_h2_proxy_is_alive, |
1495 | | Curl_cf_def_conn_keep_alive, |
1496 | | cf_h2_proxy_query, |
1497 | | }; |
1498 | | |
1499 | | CURLcode Curl_cf_h2_proxy_insert_after(struct Curl_cfilter *cf, |
1500 | | struct Curl_easy *data, |
1501 | | struct Curl_peer *dest, |
1502 | | bool udp_tunnel) |
1503 | 0 | { |
1504 | 0 | struct Curl_cfilter *cf_h2_proxy = NULL; |
1505 | 0 | struct cf_h2_proxy_ctx *ctx; |
1506 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
1507 | |
|
1508 | 0 | (void)data; |
1509 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
1510 | 0 | if(!ctx) |
1511 | 0 | goto out; |
1512 | 0 | Curl_peer_link(&ctx->dest, dest); |
1513 | 0 | ctx->udp_tunnel = udp_tunnel; |
1514 | |
|
1515 | 0 | result = Curl_cf_create(&cf_h2_proxy, &Curl_cft_h2_proxy, ctx); |
1516 | 0 | if(result) |
1517 | 0 | goto out; |
1518 | 0 | ctx = NULL; |
1519 | 0 | Curl_conn_cf_insert_after(cf, cf_h2_proxy); |
1520 | |
|
1521 | 0 | out: |
1522 | 0 | cf_h2_proxy_ctx_free(ctx); |
1523 | 0 | return result; |
1524 | 0 | } |
1525 | | |
1526 | | #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY && USE_NGHTTP2 */ |