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