Coverage Report

Created: 2026-09-04 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
46
0
#define PROXY_H2_CHUNK_SIZE  (16 * 1024)
47
48
0
#define PROXY_HTTP2_HUGE_WINDOW_SIZE (100 * 1024 * 1024)
49
0
#define H2_TUNNEL_WINDOW_SIZE        (10 * 1024 * 1024)
50
51
0
#define PROXY_H2_NW_RECV_CHUNKS   (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
52
0
#define PROXY_H2_NW_SEND_CHUNKS   1
53
54
0
#define H2_TUNNEL_RECV_CHUNKS   (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
55
0
#define H2_TUNNEL_SEND_CHUNKS   ((128 * 1024) / PROXY_H2_CHUNK_SIZE)
56
57
58
typedef enum {
59
  H2_TUNNEL_INIT,     /* init/default/no tunnel state */
60
  H2_TUNNEL_CONNECT,  /* CONNECT request is being send */
61
  H2_TUNNEL_RESPONSE, /* CONNECT response received completely */
62
  H2_TUNNEL_ESTABLISHED,
63
  H2_TUNNEL_FAILED
64
} h2_tunnel_state;
65
66
struct tunnel_stream {
67
  struct http_resp *resp;
68
  struct bufq recvbuf;
69
  struct bufq sendbuf;
70
  char *authority;
71
  int32_t stream_id;
72
  uint32_t error;
73
  h2_tunnel_state state;
74
  BIT(has_final_response);
75
  BIT(closed);
76
  BIT(reset);
77
};
78
79
static CURLcode tunnel_stream_init(struct tunnel_stream *ts,
80
                                   struct Curl_peer *dest)
81
0
{
82
0
  ts->state = H2_TUNNEL_INIT;
83
0
  ts->stream_id = -1;
84
0
  Curl_bufq_init2(&ts->recvbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_RECV_CHUNKS,
85
0
                  BUFQ_OPT_SOFT_LIMIT);
86
0
  Curl_bufq_init(&ts->sendbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_SEND_CHUNKS);
87
88
  /* host:port with IPv6 support */
89
0
  ts->authority = curl_maprintf("%s%s%s:%u", dest->ipv6 ? "[" : "",
90
0
                                dest->hostname,
91
0
                                dest->ipv6 ? "]" : "",
92
0
                                dest->port);
93
0
  if(!ts->authority)
94
0
    return CURLE_OUT_OF_MEMORY;
95
96
0
  return CURLE_OK;
97
0
}
98
99
static void tunnel_stream_reset(struct tunnel_stream *ts)
100
0
{
101
0
  Curl_http_resp_free(ts->resp);
102
0
  ts->resp = NULL;
103
0
  Curl_bufq_reset(&ts->recvbuf);
104
0
  Curl_bufq_reset(&ts->sendbuf);
105
0
  ts->stream_id = -1;
106
0
  ts->error = 0;
107
0
  ts->has_final_response = FALSE;
108
0
  ts->closed = FALSE;
109
0
  ts->reset = FALSE;
110
0
  ts->state = H2_TUNNEL_INIT;
111
0
}
112
113
static void tunnel_stream_clear(struct tunnel_stream *ts)
114
0
{
115
0
  Curl_http_resp_free(ts->resp);
116
0
  Curl_bufq_free(&ts->recvbuf);
117
0
  Curl_bufq_free(&ts->sendbuf);
118
0
  curlx_safefree(ts->authority);
119
0
  memset(ts, 0, sizeof(*ts));
120
0
  ts->state = H2_TUNNEL_INIT;
121
0
}
122
123
static void h2_tunnel_go_state(struct Curl_cfilter *cf,
124
                               struct tunnel_stream *ts,
125
                               h2_tunnel_state new_state,
126
                               struct Curl_easy *data,
127
                               bool udp_tunnel)
128
0
{
129
0
  (void)cf;
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
    /* If a proxy-authorization header was used for the proxy, then we should
172
       make sure that it is not accidentally used for the document request
173
       after we have connected. Let's thus free and clear it here. */
174
0
    curlx_safefree(data->req.hd_proxy_auth);
175
0
    break;
176
0
  }
177
0
}
178
179
struct cf_h2_proxy_ctx {
180
  nghttp2_session *h2;
181
  /* The easy handle used in the current filter call, cleared at return */
182
  struct cf_call_data call_data;
183
184
  struct bufq inbufq;  /* network receive buffer */
185
  struct bufq outbufq; /* network send buffer */
186
187
  struct Curl_peer *dest; /* where to tunnel to */
188
  struct tunnel_stream tunnel; /* our tunnel CONNECT stream */
189
  int32_t goaway_error;
190
  int32_t remote_max_sid;
191
  BIT(conn_closed);
192
  BIT(rcvd_goaway);
193
  BIT(sent_goaway);
194
  BIT(nw_out_blocked);
195
  BIT(udp_tunnel);
196
};
197
198
/* How to access `call_data` from a cf_h2 filter */
199
#undef CF_CTX_CALL_DATA
200
0
#define CF_CTX_CALL_DATA(cf) ((struct cf_h2_proxy_ctx *)(cf)->ctx)->call_data
201
202
static void cf_h2_proxy_ctx_clear(struct cf_h2_proxy_ctx *ctx)
203
0
{
204
0
  struct cf_call_data save = ctx->call_data;
205
206
0
  if(ctx->h2) {
207
0
    nghttp2_session_del(ctx->h2);
208
0
  }
209
0
  Curl_bufq_free(&ctx->inbufq);
210
0
  Curl_bufq_free(&ctx->outbufq);
211
0
  Curl_peer_unlink(&ctx->dest);
212
0
  tunnel_stream_clear(&ctx->tunnel);
213
0
  memset(ctx, 0, sizeof(*ctx));
214
0
  ctx->call_data = save;
215
0
}
216
217
static void cf_h2_proxy_ctx_free(struct cf_h2_proxy_ctx *ctx)
218
0
{
219
0
  if(ctx) {
220
0
    cf_h2_proxy_ctx_clear(ctx);
221
0
    curlx_free(ctx);
222
0
  }
223
0
}
224
225
static void drain_tunnel(struct Curl_cfilter *cf,
226
                         struct Curl_easy *data,
227
                         struct tunnel_stream *tunnel)
228
0
{
229
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
230
0
  (void)cf;
231
0
  if(!tunnel->closed && !tunnel->reset &&
232
0
     (!Curl_bufq_is_empty(&ctx->tunnel.sendbuf) ||
233
0
      !Curl_bufq_is_empty(&ctx->tunnel.recvbuf)))
234
0
    Curl_multi_mark_dirty(data);
235
0
}
236
237
static CURLcode proxy_h2_nw_out_writer(void *writer_ctx,
238
                                       const uint8_t *buf, size_t buflen,
239
                                       size_t *pnwritten)
240
0
{
241
0
  struct Curl_cfilter *cf = writer_ctx;
242
0
  *pnwritten = 0;
243
0
  if(cf) {
244
0
    struct Curl_easy *data = CF_DATA_CURRENT(cf);
245
0
    CURLcode result;
246
0
    result = Curl_conn_cf_send(cf->next, data, buf, buflen, FALSE, pnwritten);
247
0
    CURL_TRC_CF(data, cf, "[0] nw_out_writer(len=%zu) -> %d, %zu",
248
0
                buflen, (int)result, *pnwritten);
249
0
    return result;
250
0
  }
251
0
  return CURLE_FAILED_INIT;
252
0
}
253
254
static int proxy_h2_client_new(struct Curl_cfilter *cf,
255
                               nghttp2_session_callbacks *cbs)
256
0
{
257
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
258
0
  nghttp2_option *o;
259
0
  nghttp2_mem mem = { NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
260
0
                      Curl_nghttp2_calloc, Curl_nghttp2_realloc };
261
262
0
  int rc = nghttp2_option_new(&o);
263
0
  if(rc)
264
0
    return rc;
265
  /* We handle window updates ourself to enforce buffer limits */
266
0
  nghttp2_option_set_no_auto_window_update(o, 1);
267
0
#if NGHTTP2_VERSION_NUM >= 0x013200 /* with 1.50.0 */
268
  /* turn off RFC 9113 leading and trailing white spaces validation against
269
     HTTP field value. */
270
0
  nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
271
0
#endif
272
0
  rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem);
273
0
  nghttp2_option_del(o);
274
0
  return rc;
275
0
}
276
277
static int proxy_h2_should_close_session(struct cf_h2_proxy_ctx *ctx)
278
0
{
279
0
  return !nghttp2_session_want_read(ctx->h2) &&
280
0
    !nghttp2_session_want_write(ctx->h2);
281
0
}
282
283
static CURLcode proxy_h2_nw_out_flush(struct Curl_cfilter *cf,
284
                                      struct Curl_easy *data)
285
0
{
286
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
287
0
  size_t nwritten;
288
0
  CURLcode result;
289
290
0
  if(Curl_bufq_is_empty(&ctx->outbufq))
291
0
    return CURLE_OK;
292
293
0
  result = Curl_bufq_pass(&ctx->outbufq, proxy_h2_nw_out_writer, cf,
294
0
                          &nwritten);
295
0
  if(result) {
296
0
    if(result == CURLE_AGAIN) {
297
0
      CURL_TRC_CF(data, cf, "[0] flush nw send buffer(%zu) -> EAGAIN",
298
0
                  Curl_bufq_len(&ctx->outbufq));
299
0
      ctx->nw_out_blocked = 1;
300
0
    }
301
0
    return result;
302
0
  }
303
0
  CURL_TRC_CF(data, cf, "[0] nw send buffer flushed");
304
0
  return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN;
305
0
}
306
307
/*
308
 * Processes pending input left in network input buffer.
309
 * This function returns 0 if it succeeds, or -1 and error code will
310
 * be assigned to *err.
311
 */
312
static CURLcode proxy_h2_process_pending_input(struct Curl_cfilter *cf,
313
                                               struct Curl_easy *data)
314
0
{
315
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
316
0
  const unsigned char *buf;
317
0
  size_t blen, nread;
318
0
  ssize_t rv;
319
320
0
  while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
321
322
0
    rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen);
323
0
    CURL_TRC_CF(data, cf, "[0] %zu bytes to nghttp2 -> %zd", blen, rv);
324
0
    if(!curlx_sztouz(rv, &nread)) {
325
0
      failf(data,
326
0
            "process_pending_input: nghttp2_session_mem_recv() returned "
327
0
            "%zd:%s", rv, nghttp2_strerror((int)rv));
328
0
      return CURLE_RECV_ERROR;
329
0
    }
330
0
    else if(!nread) {
331
      /* nghttp2 does not want to process more, but has no error. This
332
       * probably cannot happen, but be safe. */
333
0
      break;
334
0
    }
335
0
    Curl_bufq_skip(&ctx->inbufq, nread);
336
0
    if(Curl_bufq_is_empty(&ctx->inbufq)) {
337
0
      CURL_TRC_CF(data, cf, "[0] all data in connection buffer processed");
338
0
      break;
339
0
    }
340
0
    else {
341
0
      CURL_TRC_CF(data, cf, "[0] process_pending_input: %zu bytes left "
342
0
                  "in connection buffer", Curl_bufq_len(&ctx->inbufq));
343
0
    }
344
0
  }
345
0
  return CURLE_OK;
346
0
}
347
348
static CURLcode proxy_h2_progress_ingress(struct Curl_cfilter *cf,
349
                                          struct Curl_easy *data)
350
0
{
351
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
352
0
  CURLcode result = CURLE_OK;
353
0
  size_t nread;
354
355
  /* Process network input buffer first */
356
0
  if(!Curl_bufq_is_empty(&ctx->inbufq)) {
357
0
    CURL_TRC_CF(data, cf, "[0] process %zu bytes in connection buffer",
358
0
                Curl_bufq_len(&ctx->inbufq));
359
0
    result = proxy_h2_process_pending_input(cf, data);
360
0
    if(result)
361
0
      return result;
362
0
  }
363
364
  /* Receive data from the "lower" filters, e.g. network until
365
   * it is time to stop or we have enough data for this stream */
366
0
  while(!ctx->conn_closed &&                /* not closed the connection */
367
0
        !ctx->tunnel.closed &&              /* nor the tunnel */
368
0
        Curl_bufq_is_empty(&ctx->inbufq) && /* and we consumed our input */
369
0
        !Curl_bufq_is_full(&ctx->tunnel.recvbuf)) {
370
371
0
    result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread);
372
0
    CURL_TRC_CF(data, cf, "[0] read %zu bytes nw data -> %d, %zu",
373
0
                Curl_bufq_len(&ctx->inbufq), (int)result, nread);
374
0
    if(result) {
375
0
      if(result != CURLE_AGAIN) {
376
0
        failf(data, "Failed receiving HTTP2 proxy data");
377
0
        return result;
378
0
      }
379
0
      break;
380
0
    }
381
0
    else if(nread == 0) {
382
0
      CURL_TRC_CF(data, cf, "server closed connection");
383
0
      ctx->conn_closed = TRUE;
384
0
      break;
385
0
    }
386
387
0
    result = proxy_h2_process_pending_input(cf, data);
388
0
    if(result)
389
0
      return result;
390
0
  }
391
392
0
  return CURLE_OK;
393
0
}
394
395
static CURLcode proxy_h2_progress_egress(struct Curl_cfilter *cf,
396
                                         struct Curl_easy *data)
397
0
{
398
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
399
0
  int rv = 0;
400
401
0
  ctx->nw_out_blocked = 0;
402
0
  while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2))
403
0
    rv = nghttp2_session_send(ctx->h2);
404
405
0
  if(nghttp2_is_fatal(rv)) {
406
0
    CURL_TRC_CF(data, cf, "[0] nghttp2_session_send error (%s)%d",
407
0
                nghttp2_strerror(rv), rv);
408
0
    return CURLE_SEND_ERROR;
409
0
  }
410
0
  return proxy_h2_nw_out_flush(cf, data);
411
0
}
412
413
static ssize_t on_session_send(nghttp2_session *h2,
414
                               const uint8_t *buf, size_t blen, int flags,
415
                               void *userp)
416
0
{
417
0
  struct Curl_cfilter *cf = userp;
418
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
419
0
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
420
0
  size_t nwritten;
421
0
  CURLcode result = CURLE_OK;
422
423
0
  (void)h2;
424
0
  (void)flags;
425
0
  DEBUGASSERT(data);
426
427
0
  result = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
428
0
                                proxy_h2_nw_out_writer, cf, &nwritten);
429
0
  if(result) {
430
0
    if(result == CURLE_AGAIN) {
431
0
      ctx->nw_out_blocked = 1;
432
0
      return NGHTTP2_ERR_WOULDBLOCK;
433
0
    }
434
0
    failf(data, "Failed sending HTTP2 data");
435
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
436
0
  }
437
438
0
  if(!nwritten)
439
0
    return NGHTTP2_ERR_WOULDBLOCK;
440
441
0
  return (nwritten > SSIZE_MAX) ?
442
0
    NGHTTP2_ERR_CALLBACK_FAILURE : (ssize_t)nwritten;
443
0
}
444
445
#ifdef CURLVERBOSE
446
static int proxy_h2_on_frame_send(nghttp2_session *session,
447
                                  const nghttp2_frame *frame,
448
                                  void *userp)
449
0
{
450
0
  struct Curl_cfilter *cf = userp;
451
0
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
452
453
0
  (void)session;
454
0
  DEBUGASSERT(data);
455
0
  if(Curl_trc_cf_is_verbose(cf, data)) {
456
0
    char buffer[256];
457
0
    int len;
458
0
    len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
459
0
    buffer[len] = 0;
460
0
    CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
461
0
  }
462
0
  return 0;
463
0
}
464
#endif /* CURLVERBOSE */
465
466
static int proxy_h2_on_frame_recv(nghttp2_session *session,
467
                                  const nghttp2_frame *frame,
468
                                  void *userp)
469
0
{
470
0
  struct Curl_cfilter *cf = userp;
471
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
472
0
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
473
0
  int32_t stream_id = frame->hd.stream_id;
474
475
0
  (void)session;
476
0
  DEBUGASSERT(data);
477
0
#ifdef CURLVERBOSE
478
0
  if(Curl_trc_cf_is_verbose(cf, data)) {
479
0
    char buffer[256];
480
0
    int len;
481
0
    len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
482
0
    buffer[len] = 0;
483
0
    CURL_TRC_CF(data, cf, "[%d] <- %s", frame->hd.stream_id, buffer);
484
0
  }
485
0
#endif /* CURLVERBOSE */
486
487
0
  if(!stream_id) {
488
    /* stream ID zero is for connection-oriented stuff */
489
0
    DEBUGASSERT(data);
490
0
    switch(frame->hd.type) {
491
0
    case NGHTTP2_SETTINGS:
492
      /* Since the initial stream window is 64K, a request might be on HOLD,
493
       * due to exhaustion. The (initial) SETTINGS may announce a much larger
494
       * window and *assume* that we treat this like a WINDOW_UPDATE. Some
495
       * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
496
       * To be safe, we UNHOLD a stream in order not to stall. */
497
0
      if(CURL_REQ_WANT_SEND(data)) {
498
0
        drain_tunnel(cf, data, &ctx->tunnel);
499
0
      }
500
0
      break;
501
0
    case NGHTTP2_GOAWAY:
502
0
      ctx->rcvd_goaway = TRUE;
503
0
      ctx->remote_max_sid = frame->goaway.last_stream_id;
504
0
      if(data) {
505
0
        infof(data, "received GOAWAY, error=%u, last_stream=%d",
506
0
              frame->goaway.error_code, ctx->remote_max_sid);
507
0
      }
508
0
      break;
509
0
    default:
510
0
      break;
511
0
    }
512
0
    return 0;
513
0
  }
514
515
0
  if(stream_id != ctx->tunnel.stream_id) {
516
0
    CURL_TRC_CF(data, cf, "[%d] rcvd FRAME not for tunnel", stream_id);
517
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
518
0
  }
519
520
0
  switch(frame->hd.type) {
521
0
  case NGHTTP2_HEADERS:
522
    /* nghttp2 guarantees that :status is received, and we store it to
523
       stream->status_code. Fuzzing has proven this can still be reached
524
       without status code having been set. */
525
0
    if(!ctx->tunnel.resp)
526
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
527
    /* Only final status code signals the end of header */
528
0
    CURL_TRC_CF(data, cf, "[%d] got http status: %d",
529
0
                stream_id, ctx->tunnel.resp->status);
530
0
    if(!ctx->tunnel.has_final_response && ctx->tunnel.resp->status / 100 != 1)
531
0
      ctx->tunnel.has_final_response = TRUE;
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 == CURL_CSTRLEN(HTTP_PSEUDO_STATUS) &&
580
0
     !memcmp(HTTP_PSEUDO_STATUS, name, namelen)) {
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
0
  ctx->remote_max_sid = INT32_MAX;
917
918
0
  if(tunnel_stream_init(&ctx->tunnel, ctx->dest))
919
0
    goto out;
920
921
0
  rc = nghttp2_session_callbacks_new(&cbs);
922
0
  if(rc) {
923
0
    failf(data, "Could not initialize nghttp2 callbacks");
924
0
    goto out;
925
0
  }
926
927
0
  nghttp2_session_callbacks_set_send_callback(cbs, on_session_send);
928
0
  nghttp2_session_callbacks_set_on_frame_recv_callback(
929
0
    cbs, proxy_h2_on_frame_recv);
930
0
#ifdef CURLVERBOSE
931
0
  nghttp2_session_callbacks_set_on_frame_send_callback(cbs,
932
0
                                                       proxy_h2_on_frame_send);
933
0
#endif
934
0
  nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
935
0
    cbs, tunnel_recv_callback);
936
0
  nghttp2_session_callbacks_set_on_stream_close_callback(
937
0
    cbs, proxy_h2_on_stream_close);
938
0
  nghttp2_session_callbacks_set_on_header_callback(cbs, proxy_h2_on_header);
939
940
  /* The nghttp2 session is not yet setup, do it */
941
0
  rc = proxy_h2_client_new(cf, cbs);
942
0
  if(rc) {
943
0
    failf(data, "Could not initialize nghttp2");
944
0
    goto out;
945
0
  }
946
947
0
  {
948
0
    nghttp2_settings_entry iv[3];
949
950
0
    iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
951
0
    iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
952
0
    iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
953
0
    iv[1].value = H2_TUNNEL_WINDOW_SIZE;
954
0
    iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
955
0
    iv[2].value = 0;
956
0
    rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE, iv, 3);
957
0
    if(rc) {
958
0
      failf(data, "nghttp2_submit_settings() failed: %s(%d)",
959
0
            nghttp2_strerror(rc), rc);
960
0
      result = CURLE_HTTP2;
961
0
      goto out;
962
0
    }
963
0
  }
964
965
0
  rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0,
966
0
                                             PROXY_HTTP2_HUGE_WINDOW_SIZE);
967
0
  if(rc) {
968
0
    failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
969
0
          nghttp2_strerror(rc), rc);
970
0
    result = CURLE_HTTP2;
971
0
    goto out;
972
0
  }
973
974
  /* all set, traffic will be send on connect */
975
0
  result = CURLE_OK;
976
977
0
out:
978
0
  if(cbs)
979
0
    nghttp2_session_callbacks_del(cbs);
980
0
  CURL_TRC_CF(data, cf, "[0] init proxy ctx -> %d", (int)result);
981
0
  return result;
982
0
}
983
984
static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf,
985
                                    struct Curl_easy *data,
986
                                    bool *done)
987
0
{
988
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
989
0
  CURLcode result = CURLE_OK;
990
0
  struct cf_call_data save;
991
0
  struct tunnel_stream *ts = &ctx->tunnel;
992
993
0
  if(cf->connected) {
994
0
    *done = TRUE;
995
0
    return CURLE_OK;
996
0
  }
997
998
  /* Connect the lower filters first */
999
0
  if(!cf->next->connected) {
1000
0
    result = Curl_conn_cf_connect(cf->next, data, done);
1001
0
    if(result || !*done)
1002
0
      return result;
1003
0
  }
1004
1005
0
  *done = FALSE;
1006
1007
0
  CF_DATA_SAVE(save, cf, data);
1008
0
  if(!ctx->h2) {
1009
0
    result = cf_h2_proxy_ctx_init(cf, data);
1010
0
    if(result)
1011
0
      goto out;
1012
0
  }
1013
0
  DEBUGASSERT(ts->authority);
1014
1015
0
  if(Curl_timeleft_ms(data) < 0) {
1016
0
    failf(data, "Proxy CONNECT aborted due to timeout");
1017
0
    result = CURLE_OPERATION_TIMEDOUT;
1018
0
    goto out;
1019
0
  }
1020
1021
  /* for the secondary socket (FTP), use the "connect to host"
1022
   * but ignore the "connect to port" (use the secondary port)
1023
   */
1024
0
  result = H2_CONNECT(cf, data, ts);
1025
1026
0
out:
1027
0
  *done = (result == CURLE_OK) && (ts->state == H2_TUNNEL_ESTABLISHED);
1028
0
  if(*done) {
1029
0
    cf->connected = TRUE;
1030
    /* The real request will follow the CONNECT, reset request partially */
1031
0
    Curl_req_soft_reset(&data->req, data);
1032
0
    Curl_client_reset(data);
1033
0
  }
1034
0
  CF_DATA_RESTORE(cf, save);
1035
0
  return result;
1036
0
}
1037
1038
static void cf_h2_proxy_destroy(struct Curl_cfilter *cf,
1039
                                struct Curl_easy *data)
1040
0
{
1041
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1042
1043
0
  (void)data;
1044
0
  if(ctx) {
1045
0
    cf_h2_proxy_ctx_free(ctx);
1046
0
    cf->ctx = NULL;
1047
0
  }
1048
0
}
1049
1050
static CURLcode cf_h2_proxy_shutdown(struct Curl_cfilter *cf,
1051
                                     struct Curl_easy *data, bool *done)
1052
0
{
1053
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1054
0
  struct cf_call_data save;
1055
0
  CURLcode result;
1056
0
  int rv;
1057
1058
0
  if(!cf->connected || !ctx->h2 || cf->shutdown || ctx->conn_closed) {
1059
0
    *done = TRUE;
1060
0
    return CURLE_OK;
1061
0
  }
1062
1063
0
  CF_DATA_SAVE(save, cf, data);
1064
1065
0
  if(!ctx->sent_goaway) {
1066
0
    rv = nghttp2_submit_goaway(ctx->h2, NGHTTP2_FLAG_NONE,
1067
0
                               0, 0,
1068
0
                               (const uint8_t *)"shutdown",
1069
0
                               sizeof("shutdown"));
1070
0
    if(rv) {
1071
0
      failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
1072
0
            nghttp2_strerror(rv), rv);
1073
0
      result = CURLE_SEND_ERROR;
1074
0
      goto out;
1075
0
    }
1076
0
    ctx->sent_goaway = TRUE;
1077
0
  }
1078
  /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
1079
0
  result = CURLE_OK;
1080
0
  if(nghttp2_session_want_write(ctx->h2))
1081
0
    result = proxy_h2_progress_egress(cf, data);
1082
0
  if(!result && nghttp2_session_want_read(ctx->h2))
1083
0
    result = proxy_h2_progress_ingress(cf, data);
1084
1085
0
  *done = (ctx->conn_closed ||
1086
0
           (!result && !nghttp2_session_want_write(ctx->h2) &&
1087
0
            !nghttp2_session_want_read(ctx->h2)));
1088
0
out:
1089
0
  CF_DATA_RESTORE(cf, save);
1090
0
  cf->shutdown = (result || *done);
1091
0
  return result;
1092
0
}
1093
1094
static bool cf_h2_proxy_data_pending(struct Curl_cfilter *cf,
1095
                                     const struct Curl_easy *data)
1096
0
{
1097
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1098
0
  if((ctx && !Curl_bufq_is_empty(&ctx->inbufq)) ||
1099
0
     (ctx && ctx->tunnel.state == H2_TUNNEL_ESTABLISHED &&
1100
0
      !Curl_bufq_is_empty(&ctx->tunnel.recvbuf)))
1101
0
    return TRUE;
1102
0
  return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
1103
0
}
1104
1105
static CURLcode cf_h2_proxy_adjust_pollset(struct Curl_cfilter *cf,
1106
                                           struct Curl_easy *data,
1107
                                           struct easy_pollset *ps)
1108
0
{
1109
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1110
0
  struct cf_call_data save;
1111
0
  curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
1112
0
  bool want_recv, want_send;
1113
0
  CURLcode result = CURLE_OK;
1114
1115
0
  if(!cf->connected && ctx->h2) {
1116
0
    want_send = nghttp2_session_want_write(ctx->h2) ||
1117
0
                !Curl_bufq_is_empty(&ctx->outbufq) ||
1118
0
                !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
1119
0
    want_recv = nghttp2_session_want_read(ctx->h2);
1120
0
  }
1121
0
  else
1122
0
    Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
1123
1124
0
  if(ctx->h2 && (want_recv || want_send)) {
1125
0
    bool c_exhaust, s_exhaust;
1126
1127
0
    CF_DATA_SAVE(save, cf, data);
1128
0
    c_exhaust = !nghttp2_session_get_remote_window_size(ctx->h2);
1129
0
    s_exhaust = ctx->tunnel.stream_id >= 0 &&
1130
0
                !nghttp2_session_get_stream_remote_window_size(
1131
0
                  ctx->h2, ctx->tunnel.stream_id);
1132
0
    want_recv = (want_recv || c_exhaust || s_exhaust);
1133
0
    want_send = (!s_exhaust && want_send) ||
1134
0
                (!c_exhaust && nghttp2_session_want_write(ctx->h2)) ||
1135
0
                !Curl_bufq_is_empty(&ctx->outbufq) ||
1136
0
                !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
1137
1138
0
    result = Curl_pollset_set(data, ps, sock, want_recv, want_send);
1139
0
    CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d -> %d",
1140
0
                want_recv, want_send, (int)result);
1141
0
    CF_DATA_RESTORE(cf, save);
1142
0
  }
1143
0
  else if(ctx->sent_goaway && !cf->shutdown) {
1144
    /* shutdown in progress */
1145
0
    CF_DATA_SAVE(save, cf, data);
1146
0
    want_send = nghttp2_session_want_write(ctx->h2) ||
1147
0
                !Curl_bufq_is_empty(&ctx->outbufq) ||
1148
0
                !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
1149
0
    want_recv = nghttp2_session_want_read(ctx->h2);
1150
0
    result = Curl_pollset_set(data, ps, sock, want_recv, want_send);
1151
0
    CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d -> %d",
1152
0
                want_recv, want_send, (int)result);
1153
0
    CF_DATA_RESTORE(cf, save);
1154
0
  }
1155
0
  return result;
1156
0
}
1157
1158
static CURLcode h2_handle_tunnel_close(struct Curl_cfilter *cf,
1159
                                       struct Curl_easy *data,
1160
                                       size_t *pnread)
1161
0
{
1162
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1163
1164
0
  *pnread = 0;
1165
0
  if(ctx->tunnel.error) {
1166
0
    failf(data, "HTTP/2 stream %d reset by %s (error 0x%x %s)",
1167
0
          ctx->tunnel.stream_id, ctx->tunnel.reset ? "server" : "curl",
1168
0
          ctx->tunnel.error, nghttp2_http2_strerror(ctx->tunnel.error));
1169
0
    return CURLE_RECV_ERROR;
1170
0
  }
1171
1172
0
  CURL_TRC_CF(data, cf, "[%d] handle_tunnel_close -> 0",
1173
0
              ctx->tunnel.stream_id);
1174
0
  return CURLE_OK;
1175
0
}
1176
1177
static CURLcode tunnel_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
1178
                            char *buf, size_t len, size_t *pnread)
1179
0
{
1180
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1181
0
  CURLcode result = CURLE_AGAIN;
1182
1183
0
  *pnread = 0;
1184
0
  if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf))
1185
0
    result = Curl_bufq_cread(&ctx->tunnel.recvbuf, buf, len, pnread);
1186
0
  else {
1187
0
    if(ctx->tunnel.closed) {
1188
0
      result = h2_handle_tunnel_close(cf, data, pnread);
1189
0
    }
1190
0
    else if(ctx->tunnel.reset ||
1191
0
            (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
1192
0
            (ctx->rcvd_goaway &&
1193
0
             ctx->remote_max_sid < ctx->tunnel.stream_id)) {
1194
0
      result = CURLE_RECV_ERROR;
1195
0
    }
1196
0
    else
1197
0
      result = CURLE_AGAIN;
1198
0
  }
1199
1200
0
  CURL_TRC_CF(data, cf, "[%d] tunnel_recv(len=%zu) -> %d, %zu",
1201
0
              ctx->tunnel.stream_id, len, (int)result, *pnread);
1202
0
  return result;
1203
0
}
1204
1205
static CURLcode cf_h2_proxy_recv(struct Curl_cfilter *cf,
1206
                                 struct Curl_easy *data,
1207
                                 char *buf, size_t len,
1208
                                 size_t *pnread)
1209
0
{
1210
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1211
0
  struct cf_call_data save;
1212
0
  CURLcode result;
1213
1214
0
  *pnread = 0;
1215
0
  CF_DATA_SAVE(save, cf, data);
1216
1217
0
  if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
1218
0
    result = CURLE_RECV_ERROR;
1219
0
    goto out;
1220
0
  }
1221
1222
0
  if(Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) {
1223
0
    result = proxy_h2_progress_ingress(cf, data);
1224
0
    if(result)
1225
0
      goto out;
1226
0
  }
1227
1228
0
  result = tunnel_recv(cf, data, buf, len, pnread);
1229
1230
0
  if(!result) {
1231
0
    CURL_TRC_CF(data, cf, "[%d] increase window by %zu",
1232
0
                ctx->tunnel.stream_id, *pnread);
1233
0
    nghttp2_session_consume(ctx->h2, ctx->tunnel.stream_id, *pnread);
1234
0
  }
1235
1236
0
  result = Curl_1st_fatal(result, proxy_h2_progress_egress(cf, data));
1237
1238
0
out:
1239
0
  if((!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) ||
1240
0
      !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) &&
1241
0
     (!result || (result == CURLE_AGAIN))) {
1242
    /* data pending and no fatal error to report. Need to trigger
1243
     * draining to avoid stalling when no socket events happen. */
1244
0
    drain_tunnel(cf, data, &ctx->tunnel);
1245
0
  }
1246
0
  CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %d, %zu",
1247
0
              ctx->tunnel.stream_id, len, (int)result, *pnread);
1248
0
  CF_DATA_RESTORE(cf, save);
1249
0
  return result;
1250
0
}
1251
1252
static CURLcode cf_h2_proxy_send(struct Curl_cfilter *cf,
1253
                                 struct Curl_easy *data,
1254
                                 const uint8_t *buf, size_t len, bool eos,
1255
                                 size_t *pnwritten)
1256
0
{
1257
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1258
0
  struct cf_call_data save;
1259
0
  int rv;
1260
0
  CURLcode result;
1261
1262
0
  (void)eos;
1263
0
  *pnwritten = 0;
1264
0
  CF_DATA_SAVE(save, cf, data);
1265
1266
0
  if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
1267
0
    result = CURLE_SEND_ERROR;
1268
0
    goto out;
1269
0
  }
1270
1271
0
  if(ctx->tunnel.closed) {
1272
0
    result = CURLE_SEND_ERROR;
1273
0
    goto out;
1274
0
  }
1275
1276
0
  result = Curl_bufq_write(&ctx->tunnel.sendbuf, buf, len, pnwritten);
1277
0
  CURL_TRC_CF(data, cf, "cf_send(), bufq_write %d, %zu", (int)result,
1278
0
              *pnwritten);
1279
0
  if(result && (result != CURLE_AGAIN))
1280
0
    goto out;
1281
1282
0
  if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
1283
    /* req body data is buffered, resume the potentially suspended stream */
1284
0
    rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id);
1285
0
    if(nghttp2_is_fatal(rv)) {
1286
0
      result = CURLE_SEND_ERROR;
1287
0
      goto out;
1288
0
    }
1289
0
  }
1290
1291
0
  result = Curl_1st_fatal(result, proxy_h2_progress_ingress(cf, data));
1292
0
  result = Curl_1st_fatal(result, proxy_h2_progress_egress(cf, data));
1293
1294
0
  if(!result && proxy_h2_should_close_session(ctx)) {
1295
    /* nghttp2 thinks this session is done. If the stream has not been
1296
     * closed, this is an error state for out transfer */
1297
0
    if(ctx->tunnel.closed) {
1298
0
      result = CURLE_SEND_ERROR;
1299
0
    }
1300
0
    else {
1301
0
      CURL_TRC_CF(data, cf, "[0] send: nothing to do in this session");
1302
0
      result = CURLE_HTTP2;
1303
0
    }
1304
0
  }
1305
1306
0
out:
1307
0
  if((!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) ||
1308
0
      !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) &&
1309
0
     (!result || (result == CURLE_AGAIN))) {
1310
    /* data pending and no fatal error to report. Need to trigger
1311
     * draining to avoid stalling when no socket events happen. */
1312
0
    drain_tunnel(cf, data, &ctx->tunnel);
1313
0
  }
1314
0
  CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %d, %zu, "
1315
0
              "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
1316
0
              ctx->tunnel.stream_id, len, (int)result, *pnwritten,
1317
0
              nghttp2_session_get_stream_remote_window_size(
1318
0
                ctx->h2, ctx->tunnel.stream_id),
1319
0
              nghttp2_session_get_remote_window_size(ctx->h2),
1320
0
              Curl_bufq_len(&ctx->tunnel.sendbuf),
1321
0
              Curl_bufq_len(&ctx->outbufq));
1322
0
  CF_DATA_RESTORE(cf, save);
1323
0
  return result;
1324
0
}
1325
1326
static CURLcode cf_h2_proxy_flush(struct Curl_cfilter *cf,
1327
                                  struct Curl_easy *data)
1328
0
{
1329
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1330
0
  struct cf_call_data save;
1331
0
  CURLcode result = CURLE_OK;
1332
1333
0
  CF_DATA_SAVE(save, cf, data);
1334
0
  if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
1335
    /* resume the potentially suspended tunnel */
1336
0
    int rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id);
1337
0
    if(nghttp2_is_fatal(rv)) {
1338
0
      result = CURLE_SEND_ERROR;
1339
0
      goto out;
1340
0
    }
1341
0
  }
1342
1343
0
  result = proxy_h2_progress_egress(cf, data);
1344
1345
0
out:
1346
0
  CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
1347
0
              "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
1348
0
              ctx->tunnel.stream_id, (int)result,
1349
0
              nghttp2_session_get_stream_remote_window_size(
1350
0
                ctx->h2, ctx->tunnel.stream_id),
1351
0
              nghttp2_session_get_remote_window_size(ctx->h2),
1352
0
              Curl_bufq_len(&ctx->tunnel.sendbuf),
1353
0
              Curl_bufq_len(&ctx->outbufq));
1354
0
  CF_DATA_RESTORE(cf, save);
1355
0
  return result;
1356
0
}
1357
1358
static bool proxy_h2_connisalive(struct Curl_cfilter *cf,
1359
                                 struct Curl_easy *data,
1360
                                 bool *input_pending)
1361
0
{
1362
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1363
0
  bool alive = TRUE;
1364
1365
0
  *input_pending = FALSE;
1366
0
  if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
1367
0
    return FALSE;
1368
1369
0
  if(*input_pending) {
1370
    /* This happens before we have sent off a request and the connection is
1371
       not in use by any other transfer, there should not be any data here,
1372
       only "protocol frames" */
1373
0
    CURLcode result;
1374
0
    size_t nread;
1375
1376
0
    *input_pending = FALSE;
1377
0
    result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread);
1378
0
    if(!result) {
1379
0
      if(proxy_h2_process_pending_input(cf, data))
1380
        /* immediate error, considered dead */
1381
0
        alive = FALSE;
1382
0
      else {
1383
0
        alive = !proxy_h2_should_close_session(ctx);
1384
0
      }
1385
0
    }
1386
0
    else if(result != CURLE_AGAIN) {
1387
      /* the read failed so let's say this is dead anyway */
1388
0
      alive = FALSE;
1389
0
    }
1390
0
  }
1391
1392
0
  return alive;
1393
0
}
1394
1395
static bool cf_h2_proxy_is_alive(struct Curl_cfilter *cf,
1396
                                 struct Curl_easy *data,
1397
                                 bool *input_pending)
1398
0
{
1399
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1400
0
  bool alive;
1401
0
  struct cf_call_data save;
1402
1403
0
  *input_pending = FALSE;
1404
0
  CF_DATA_SAVE(save, cf, data);
1405
0
  alive = (ctx && ctx->h2 && proxy_h2_connisalive(cf, data, input_pending));
1406
0
  CURL_TRC_CF(data, cf, "[0] conn alive -> %d, input_pending=%d",
1407
0
              alive, *input_pending);
1408
0
  CF_DATA_RESTORE(cf, save);
1409
0
  return alive;
1410
0
}
1411
1412
static CURLcode cf_h2_proxy_query(struct Curl_cfilter *cf,
1413
                                  struct Curl_easy *data,
1414
                                  int query, int *pres1, void *pres2)
1415
0
{
1416
0
  struct cf_h2_proxy_ctx *ctx = cf->ctx;
1417
1418
0
  switch(query) {
1419
0
  case CF_QUERY_HOST_PORT:
1420
0
    *pres1 = (int)ctx->dest->port;
1421
0
    *((const char **)pres2) = ctx->dest->hostname;
1422
0
    return CURLE_OK;
1423
0
  case CF_QUERY_NEED_FLUSH: {
1424
0
    if(!Curl_bufq_is_empty(&ctx->outbufq) ||
1425
0
       !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
1426
0
      CURL_TRC_CF(data, cf, "needs flush");
1427
0
      *pres1 = TRUE;
1428
0
      return CURLE_OK;
1429
0
    }
1430
0
    break;
1431
0
  }
1432
0
  case CF_QUERY_ALPN_NEGOTIATED: {
1433
0
    const char **palpn = pres2;
1434
0
    DEBUGASSERT(palpn);
1435
0
    *palpn = NULL;
1436
0
    return CURLE_OK;
1437
0
  }
1438
0
  default:
1439
0
    break;
1440
0
  }
1441
0
  return cf->next ?
1442
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1443
0
    CURLE_UNKNOWN_OPTION;
1444
0
}
1445
1446
static CURLcode cf_h2_proxy_cntrl(struct Curl_cfilter *cf,
1447
                                  struct Curl_easy *data,
1448
                                  int event, int arg1, void *arg2)
1449
0
{
1450
0
  CURLcode result = CURLE_OK;
1451
0
  struct cf_call_data save;
1452
1453
0
  (void)arg1;
1454
0
  (void)arg2;
1455
1456
0
  switch(event) {
1457
0
  case CF_CTRL_FLUSH:
1458
0
    CF_DATA_SAVE(save, cf, data);
1459
0
    result = cf_h2_proxy_flush(cf, data);
1460
0
    CF_DATA_RESTORE(cf, save);
1461
0
    break;
1462
0
  default:
1463
0
    break;
1464
0
  }
1465
0
  return result;
1466
0
}
1467
1468
struct Curl_cftype Curl_cft_h2_proxy = {
1469
  "H2-PROXY",
1470
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
1471
  CURL_LOG_LVL_NONE,
1472
  cf_h2_proxy_destroy,
1473
  cf_h2_proxy_connect,
1474
  cf_h2_proxy_shutdown,
1475
  cf_h2_proxy_adjust_pollset,
1476
  cf_h2_proxy_data_pending,
1477
  cf_h2_proxy_send,
1478
  cf_h2_proxy_recv,
1479
  cf_h2_proxy_cntrl,
1480
  cf_h2_proxy_is_alive,
1481
  Curl_cf_def_conn_keep_alive,
1482
  cf_h2_proxy_query,
1483
};
1484
1485
CURLcode Curl_cf_h2_proxy_insert_after(struct Curl_cfilter *cf,
1486
                                       struct Curl_easy *data,
1487
                                       struct Curl_peer *dest,
1488
                                       bool udp_tunnel)
1489
0
{
1490
0
  struct Curl_cfilter *cf_h2_proxy = NULL;
1491
0
  struct cf_h2_proxy_ctx *ctx;
1492
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
1493
1494
0
  (void)data;
1495
0
  ctx = curlx_calloc(1, sizeof(*ctx));
1496
0
  if(!ctx)
1497
0
    goto out;
1498
0
  Curl_peer_link(&ctx->dest, dest);
1499
0
  ctx->udp_tunnel = udp_tunnel;
1500
1501
0
  result = Curl_cf_create(&cf_h2_proxy, &Curl_cft_h2_proxy, ctx);
1502
0
  if(result)
1503
0
    goto out;
1504
0
  ctx = NULL;
1505
0
  Curl_conn_cf_insert_after(cf, cf_h2_proxy);
1506
1507
0
out:
1508
0
  cf_h2_proxy_ctx_free(ctx);
1509
0
  return result;
1510
0
}
1511
1512
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY && USE_NGHTTP2 */