Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/cf-h1-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_PROXY) && !defined(CURL_DISABLE_HTTP)
27
28
29
#include <curl/curl.h>
30
#include "urldata.h"
31
#include "curlx/dynbuf.h"
32
#include "sendf.h"
33
#include "http.h"
34
#include "http1.h"
35
#include "http_proxy.h"
36
#include "select.h"
37
#include "progress.h"
38
#include "multiif.h"
39
#include "cfilters.h"
40
#include "cf-h1-proxy.h"
41
#include "connect.h"
42
#include "curl_trc.h"
43
#include "strcase.h"
44
#include "curlx/strparse.h"
45
46
typedef enum {
47
  H1_TUNNEL_INIT,     /* init/default/no tunnel state */
48
  H1_TUNNEL_CONNECT,  /* CONNECT request is being send */
49
  H1_TUNNEL_RECEIVE,  /* CONNECT answer is being received */
50
  H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
51
  H1_TUNNEL_ESTABLISHED,
52
  H1_TUNNEL_FAILED
53
} h1_tunnel_state;
54
55
/* struct for HTTP CONNECT tunneling */
56
struct h1_tunnel_state {
57
  struct Curl_peer *dest;
58
  struct dynbuf rcvbuf;
59
  struct dynbuf request_data;
60
  size_t nsent;
61
  size_t headerlines;
62
  struct Curl_chunker ch;
63
  int httpversion;
64
  enum keeponval {
65
    KEEPON_DONE,
66
    KEEPON_CONNECT,
67
    KEEPON_IGNORE
68
  } keepon;
69
  curl_off_t cl; /* size of content to read and ignore */
70
  h1_tunnel_state tunnel_state;
71
  BIT(chunked_encoding);
72
  BIT(close_connection);
73
  BIT(maybe_folded);
74
  BIT(leading_unfold);
75
  BIT(udp_tunnel);
76
};
77
78
/* Persistent context for the H1-PROXY filter */
79
struct cf_h1_proxy_ctx {
80
  struct Curl_peer *peer;
81
  struct h1_tunnel_state *ts;
82
};
83
84
static bool tunnel_is_established(struct h1_tunnel_state *ts)
85
55.1k
{
86
55.1k
  return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
87
55.1k
}
88
89
static bool tunnel_is_failed(struct h1_tunnel_state *ts)
90
29.8k
{
91
29.8k
  return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
92
29.8k
}
93
94
static bool h1_proxy_is_udp(struct Curl_cfilter *cf)
95
34.5k
{
96
34.5k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
97
34.5k
  return (bool)(pctx->ts && pctx->ts->udp_tunnel);
98
34.5k
}
99
100
static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
101
                              struct Curl_easy *data,
102
                              struct h1_tunnel_state *ts)
103
15
{
104
15
  (void)data;
105
15
  (void)cf;
106
15
  DEBUGASSERT(ts);
107
15
  curlx_dyn_reset(&ts->rcvbuf);
108
15
  curlx_dyn_reset(&ts->request_data);
109
15
  ts->tunnel_state = H1_TUNNEL_INIT;
110
15
  ts->keepon = KEEPON_CONNECT;
111
15
  ts->cl = 0;
112
15
  ts->close_connection = FALSE;
113
15
  ts->maybe_folded = FALSE;
114
15
  ts->leading_unfold = FALSE;
115
15
  ts->chunked_encoding = FALSE;
116
15
  ts->nsent = 0;
117
15
  ts->headerlines = 0;
118
15
  return CURLE_OK;
119
15
}
120
121
static CURLcode tunnel_init(struct Curl_cfilter *cf,
122
                            struct Curl_easy *data,
123
                            struct h1_tunnel_state **pts)
124
0
{
125
0
  struct h1_tunnel_state *ts;
126
127
0
  if(cf->conn->scheme->flags & PROTOPT_NOTCPPROXY) {
128
0
    failf(data, "%s cannot be done over CONNECT", cf->conn->scheme->name);
129
0
    return CURLE_UNSUPPORTED_PROTOCOL;
130
0
  }
131
132
0
  ts = curlx_calloc(1, sizeof(*ts));
133
0
  if(!ts)
134
0
    return CURLE_OUT_OF_MEMORY;
135
136
0
  infof(data, "allocate connect buffer");
137
138
0
  curlx_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
139
0
  curlx_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
140
0
  Curl_httpchunk_init(data, &ts->ch, TRUE, TRUE);
141
142
0
  *pts = ts;
143
0
  return tunnel_reinit(cf, data, ts);
144
0
}
145
146
static void h1_tunnel_go_state(struct Curl_cfilter *cf,
147
                               struct h1_tunnel_state *ts,
148
                               h1_tunnel_state new_state,
149
                               struct Curl_easy *data)
150
29.9k
{
151
29.9k
  if(ts->tunnel_state == new_state)
152
4.45k
    return;
153
  /* entering this one */
154
25.4k
  switch(new_state) {
155
15
  case H1_TUNNEL_INIT:
156
15
    CURL_TRC_CF(data, cf, "new tunnel state 'init'");
157
15
    tunnel_reinit(cf, data, ts);
158
15
    break;
159
160
6.74k
  case H1_TUNNEL_CONNECT:
161
6.74k
    CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
162
6.74k
    ts->tunnel_state = H1_TUNNEL_CONNECT;
163
6.74k
    ts->keepon = KEEPON_CONNECT;
164
6.74k
    curlx_dyn_reset(&ts->rcvbuf);
165
6.74k
    break;
166
167
6.74k
  case H1_TUNNEL_RECEIVE:
168
6.74k
    CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
169
6.74k
    ts->tunnel_state = H1_TUNNEL_RECEIVE;
170
6.74k
    break;
171
172
2.61k
  case H1_TUNNEL_RESPONSE:
173
2.61k
    CURL_TRC_CF(data, cf, "new tunnel state 'response'");
174
2.61k
    ts->tunnel_state = H1_TUNNEL_RESPONSE;
175
2.61k
    break;
176
177
2.45k
  case H1_TUNNEL_ESTABLISHED:
178
2.45k
    CURL_TRC_CF(data, cf, "new tunnel state 'established'");
179
2.45k
    infof(data, "CONNECT%s phase completed for HTTP proxy",
180
2.45k
          h1_proxy_is_udp(cf) ? "-UDP" : "");
181
182
2.45k
    data->state.authproxy.done = TRUE;
183
2.45k
    data->state.authproxy.multipass = FALSE;
184
2.45k
    FALLTHROUGH();
185
9.37k
  case H1_TUNNEL_FAILED:
186
9.37k
    if(new_state == H1_TUNNEL_FAILED)
187
6.91k
      CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
188
9.37k
    ts->tunnel_state = new_state;
189
9.37k
    curlx_dyn_reset(&ts->rcvbuf);
190
9.37k
    curlx_dyn_reset(&ts->request_data);
191
    /* restore the protocol pointer */
192
9.37k
    data->info.httpcode = 0; /* clear it as it might have been used for the
193
                                proxy */
194
    /* If a proxy-authorization header was used for the proxy, then we should
195
       make sure that it is not accidentally used for the document request
196
       after we have connected. Let's thus free and clear it here. */
197
9.37k
    curlx_safefree(data->req.hd_proxy_auth);
198
9.37k
    break;
199
25.4k
  }
200
25.4k
}
201
202
static void tunnel_free(struct h1_tunnel_state *ts,
203
                        struct Curl_easy *data)
204
13.8k
{
205
13.8k
  if(ts) {
206
6.92k
    Curl_peer_unlink(&ts->dest);
207
6.92k
    curlx_dyn_free(&ts->rcvbuf);
208
6.92k
    curlx_dyn_free(&ts->request_data);
209
6.92k
    Curl_httpchunk_free(data, &ts->ch);
210
6.92k
    curlx_free(ts);
211
6.92k
  }
212
13.8k
}
213
214
static void cf_tunnel_free(struct Curl_cfilter *cf,
215
                           struct Curl_easy *data)
216
9.37k
{
217
9.37k
  if(cf) {
218
9.37k
    struct cf_h1_proxy_ctx *pctx = cf->ctx;
219
9.37k
    struct h1_tunnel_state *ts = pctx ? pctx->ts : NULL;
220
9.37k
    if(ts) {
221
6.91k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
222
6.91k
      tunnel_free(ts, data);
223
6.91k
      pctx->ts = NULL;
224
6.91k
    }
225
9.37k
  }
226
9.37k
}
227
228
static bool tunnel_want_send(struct h1_tunnel_state *ts)
229
10.4k
{
230
10.4k
  return ts->tunnel_state == H1_TUNNEL_CONNECT;
231
10.4k
}
232
233
static CURLcode start_CONNECT(struct Curl_cfilter *cf,
234
                              struct Curl_easy *data,
235
                              struct h1_tunnel_state *ts)
236
6.93k
{
237
6.93k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
238
6.93k
  struct httpreq *req = NULL;
239
6.93k
  int http_minor;
240
6.93k
  CURLcode result;
241
242
6.93k
  DEBUGASSERT(data);
243
  /* This only happens if we have looped here due to authentication reasons,
244
     and we do not really use the newly cloned URL here then. Free it. */
245
6.93k
  curlx_safefree(data->req.newurl);
246
247
6.93k
  result = Curl_http_proxy_create_tunnel_request(&req, cf, data,
248
6.93k
                                                 pctx->peer, ts->dest,
249
6.93k
                                                 PROXY_HTTP_V1,
250
6.93k
                                                 h1_proxy_is_udp(cf));
251
6.93k
  if(result)
252
166
    goto out;
253
254
6.76k
  curlx_dyn_reset(&ts->request_data);
255
6.76k
  ts->nsent = 0;
256
6.76k
  ts->headerlines = 0;
257
6.76k
  http_minor = ts->httpversion % 10;
258
259
6.76k
  result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
260
6.76k
  if(!result)
261
6.75k
    result = Curl_creader_set_null(data);
262
263
6.93k
out:
264
6.93k
  if(result)
265
183
    failf(data, "Failed sending CONNECT to proxy");
266
6.93k
  if(req)
267
6.76k
    Curl_http_req_free(req);
268
6.93k
  return result;
269
6.76k
}
270
271
static CURLcode send_CONNECT(struct Curl_cfilter *cf,
272
                             struct Curl_easy *data,
273
                             struct h1_tunnel_state *ts,
274
                             bool *done)
275
24.7k
{
276
24.7k
  const uint8_t *buf = curlx_dyn_uptr(&ts->request_data);
277
24.7k
  size_t request_len = curlx_dyn_len(&ts->request_data);
278
24.7k
  size_t blen = request_len;
279
24.7k
  CURLcode result = CURLE_OK;
280
24.7k
  size_t nwritten;
281
282
24.7k
  if(blen <= ts->nsent)
283
0
    goto out;  /* we are done */
284
285
24.7k
  blen -= ts->nsent;
286
24.7k
  buf += ts->nsent;
287
288
24.7k
  result = cf->next->cft->do_send(cf->next, data, buf, blen, FALSE, &nwritten);
289
24.7k
  if(result) {
290
17.6k
    if(result == CURLE_AGAIN)
291
17.6k
      result = CURLE_OK;
292
17.6k
    goto out;
293
17.6k
  }
294
295
7.13k
  DEBUGASSERT(blen >= nwritten);
296
7.13k
  ts->nsent += nwritten;
297
7.13k
  Curl_debug(data, CURLINFO_HEADER_OUT, (const char *)buf, nwritten);
298
299
24.7k
out:
300
24.7k
  if(result)
301
0
    failf(data, "Failed sending CONNECT to proxy");
302
24.7k
  *done = (!result && (ts->nsent >= request_len));
303
24.7k
  return result;
304
7.13k
}
305
306
static CURLcode on_resp_status(struct Curl_cfilter *cf,
307
                               struct Curl_easy *data,
308
                               struct h1_tunnel_state *ts,
309
                               const char *header)
310
3.34k
{
311
3.34k
  struct SingleRequest *k = &data->req;
312
313
3.34k
  (void)ts;
314
3.34k
  if(!strncmp(header, "HTTP/1.", 7) &&
315
3.22k
          ((header[7] == '0') || (header[7] == '1')) &&
316
3.21k
          (header[8] == ' ') &&
317
3.20k
          ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
318
3.18k
          !ISDIGIT(header[12])) {
319
    /* store the HTTP code from the proxy */
320
3.18k
    data->info.httpproxycode = k->httpcode = ((header[9] - '0') * 100) +
321
3.18k
      ((header[10] - '0') * 10) + (header[11] - '0');
322
3.18k
    CURL_TRC_CF(data, cf, "CONNECT%s HTTP status %d",
323
3.18k
                h1_proxy_is_udp(cf) ? "-UDP" : "", k->httpcode);
324
3.18k
    return CURLE_OK;
325
3.18k
  }
326
168
  failf(data, "Invalid response header");
327
168
  return CURLE_WEIRD_SERVER_REPLY;
328
3.34k
}
329
330
static CURLcode on_resp_header(struct Curl_cfilter *cf,
331
                               struct Curl_easy *data,
332
                               struct h1_tunnel_state *ts,
333
                               const char *header)
334
21.3k
{
335
21.3k
  CURLcode result = CURLE_OK;
336
21.3k
  struct SingleRequest *k = &data->req;
337
21.3k
  bool is_udp = h1_proxy_is_udp(cf);
338
339
21.3k
  if(checkprefix("Proxy-authenticate:", header) && (407 == k->httpcode)) {
340
341
90
    bool proxy = (k->httpcode == 407);
342
90
    char *auth = Curl_copy_header_value(header);
343
90
    if(!auth)
344
0
      return CURLE_OUT_OF_MEMORY;
345
346
90
    CURL_TRC_CF(data, cf, "CONNECT%s: fwd auth header '%s'",
347
90
                is_udp ? "-UDP" : "", header);
348
90
    result = Curl_http_input_auth(data, proxy, auth);
349
350
90
    curlx_free(auth);
351
352
90
    if(result)
353
0
      return result;
354
90
  }
355
21.3k
  else if(checkprefix("Content-Length:", header)) {
356
340
    if(k->httpcode < 300) {
357
236
      if(k->httpcode < 200) {
358
        /* Informational 1xx responses cannot carry a body. RFC 9110 15.2 */
359
4
        failf(data, "Invalid Content-Length: in %03d response", k->httpcode);
360
4
        return CURLE_WEIRD_SERVER_REPLY;
361
4
      }
362
      /* A client MUST ignore any Content-Length or Transfer-Encoding
363
         header fields received in a successful response to CONNECT.
364
         "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
365
232
      infof(data, "Ignoring Content-Length in CONNECT%s %03d response",
366
232
            is_udp ? "-UDP" : "", k->httpcode);
367
232
    }
368
104
    else {
369
104
      const char *p = header + CURL_CSTRLEN("Content-Length:");
370
104
      if(curlx_str_numblanks(&p, &ts->cl)) {
371
3
        failf(data, "Unsupported Content-Length value");
372
3
        return CURLE_WEIRD_SERVER_REPLY;
373
3
      }
374
104
    }
375
340
  }
376
20.9k
  else if(Curl_compareheader(header,
377
20.9k
                             STRCONST("Connection:"), STRCONST("close"))) {
378
58
    CURL_TRC_CF(data, cf, "CONNECT%s Connection: close", is_udp ? "-UDP" : "");
379
58
    ts->close_connection = TRUE;
380
58
  }
381
20.9k
  else if(checkprefix("Transfer-Encoding:", header)) {
382
256
    if(k->httpcode < 300) {
383
68
      if(k->httpcode < 200) {
384
        /* Informational 1xx responses cannot carry a body. RFC 9110 15.2 */
385
4
        failf(data, "Invalid Transfer-Encoding: in %03d response",
386
4
              k->httpcode);
387
4
        return CURLE_WEIRD_SERVER_REPLY;
388
4
      }
389
      /* A client MUST ignore any Content-Length or Transfer-Encoding
390
         header fields received in a successful response to CONNECT.
391
         "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
392
64
      infof(data, "Ignoring Transfer-Encoding in "
393
64
            "CONNECT%s %03d response", is_udp ? "-UDP" : "", k->httpcode);
394
64
    }
395
188
    else if(Curl_compareheader(header,
396
188
                               STRCONST("Transfer-Encoding:"),
397
188
                               STRCONST("chunked"))) {
398
152
      CURL_TRC_CF(data, cf, "CONNECT%s response chunked",
399
152
                  is_udp ? "-UDP" : "");
400
152
      ts->chunked_encoding = TRUE;
401
152
      Curl_httpchunk_reset(data, &ts->ch, TRUE);
402
152
    }
403
256
  }
404
20.6k
  else if(is_udp && checkprefix("Capsule-protocol:", header)) {
405
1.44k
    if(Curl_compareheader(header,
406
1.44k
                           STRCONST("Capsule-protocol:"),
407
1.44k
                           STRCONST("?1"))) {
408
1.14k
      CURL_TRC_CF(data, cf, "CONNECT-UDP Response --> Capsule-protocol: ?1");
409
1.14k
    }
410
1.44k
  }
411
19.2k
  else if(Curl_compareheader(header,
412
19.2k
                             STRCONST("Proxy-Connection:"),
413
19.2k
                             STRCONST("close"))) {
414
0
    CURL_TRC_CF(data, cf, "CONNECT%s Proxy-Connection: close",
415
0
                is_udp ? "-UDP" : "");
416
0
    ts->close_connection = TRUE;
417
0
  }
418
21.3k
  return result;
419
21.3k
}
420
421
static CURLcode single_header(struct Curl_cfilter *cf,
422
                              struct Curl_easy *data,
423
                              struct h1_tunnel_state *ts)
424
27.6k
{
425
27.6k
  CURLcode result = CURLE_OK;
426
27.6k
  const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
427
27.6k
  size_t line_len = curlx_dyn_len(&ts->rcvbuf); /* bytes in this line */
428
27.6k
  const struct SingleRequest *k = &data->req;
429
27.6k
  int writetype;
430
27.6k
  ts->headerlines++;
431
432
  /* output debug if that is requested */
433
27.6k
  Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
434
435
  /* a CONNECT response line is handed to the client as a header, so it must
436
     pass the same checks as a regular response header before delivery */
437
27.6k
  result = Curl_verify_header(data, linep, line_len);
438
27.6k
  if(result)
439
96
    return result;
440
441
  /* send the header to the callback */
442
27.5k
  writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
443
27.5k
    (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
444
27.5k
  result = Curl_client_write(data, writetype, linep, line_len);
445
27.5k
  if(result)
446
65
    return result;
447
448
27.5k
  result = Curl_bump_headersize(data, line_len, TRUE);
449
27.5k
  if(result)
450
0
    return result;
451
452
  /* Newlines are CRLF, so the CR is ignored as the line is not
453
     really terminated until the LF comes. Treat a following CR
454
     as end-of-headers as well.*/
455
456
27.5k
  if(ISNEWLINE(linep[0])) {
457
    /* end of response-headers from the proxy */
458
2.76k
    if(ts->headerlines == 1) {
459
12
      ts->keepon = KEEPON_DONE;
460
12
      result = CURLE_WEIRD_SERVER_REPLY;
461
12
    }
462
2.75k
    else if((407 == k->httpcode) && !data->state.authproblem) {
463
      /* If we get a 407 response code with content length
464
         when we have no auth problem, we must ignore the
465
         whole response-body */
466
203
      ts->keepon = KEEPON_IGNORE;
467
468
203
      if(ts->cl) {
469
23
        infof(data, "Ignore %" FMT_OFF_T " bytes of response-body", ts->cl);
470
23
      }
471
180
      else if(ts->chunked_encoding) {
472
119
        infof(data, "Ignore chunked response-body");
473
119
      }
474
61
      else {
475
        /* without content-length or chunked encoding, we
476
           cannot keep the connection alive since the close is
477
           the end signal so we bail out at once instead */
478
61
        CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
479
61
        ts->keepon = KEEPON_DONE;
480
61
      }
481
203
    }
482
2.55k
    else {
483
2.55k
      ts->keepon = KEEPON_DONE;
484
2.55k
    }
485
486
2.76k
    DEBUGASSERT(ts->keepon == KEEPON_IGNORE ||
487
2.76k
                ts->keepon == KEEPON_DONE);
488
2.76k
    return result;
489
2.76k
  }
490
491
24.7k
  if(ts->headerlines == 1)
492
3.34k
    result = on_resp_status(cf, data, ts, linep);
493
21.3k
  else
494
21.3k
    result = on_resp_header(cf, data, ts, linep);
495
24.7k
  if(result)
496
179
    return result;
497
498
24.5k
  curlx_dyn_reset(&ts->rcvbuf);
499
24.5k
  return result;
500
24.7k
}
501
502
static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
503
                                  struct Curl_easy *data,
504
                                  struct h1_tunnel_state *ts,
505
                                  bool *done)
506
11.6k
{
507
11.6k
  CURLcode result = CURLE_OK;
508
11.6k
  int error;
509
510
11.6k
#define SELECT_OK      0
511
11.6k
#define SELECT_ERROR   1
512
513
11.6k
  error = SELECT_OK;
514
11.6k
  *done = FALSE;
515
516
908k
  while(ts->keepon) {
517
905k
    size_t nread;
518
905k
    char byte;
519
520
    /* Read one byte at a time to avoid a race condition. Wait at most one
521
       second before looping to ensure continuous pgrsUpdates. */
522
905k
    result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
523
905k
    if(result == CURLE_AGAIN)
524
      /* socket buffer drained, return */
525
4.90k
      return CURLE_OK;
526
527
901k
    if(!result)
528
901k
      result = Curl_pgrsUpdate(data);
529
530
901k
    if(result) {
531
0
      ts->keepon = KEEPON_DONE;
532
0
      break;
533
0
    }
534
535
901k
    if(!nread) {
536
3.74k
      if(ts->maybe_folded) {
537
        /* EOF right after LF: finalize the pending header line. */
538
209
        result = single_header(cf, data, ts);
539
209
        if(result)
540
59
          return result;
541
150
        ts->maybe_folded = FALSE;
542
150
      }
543
3.69k
      if(data->set.proxyauth && data->state.authproxy.avail &&
544
2
         data->req.hd_proxy_auth) {
545
        /* proxy auth was requested and there was proxy auth available,
546
           then deem this as "mere" proxy disconnect */
547
0
        ts->close_connection = TRUE;
548
0
        infof(data, "Proxy CONNECT connection closed");
549
0
      }
550
3.69k
      else {
551
3.69k
        error = SELECT_ERROR;
552
3.69k
        failf(data, "Proxy CONNECT aborted");
553
3.69k
      }
554
3.69k
      ts->keepon = KEEPON_DONE;
555
3.69k
      break;
556
3.74k
    }
557
558
897k
    if(ts->keepon == KEEPON_IGNORE) {
559
      /* This means we are currently ignoring a response-body */
560
14.3k
      if(ts->chunked_encoding) {
561
        /* chunked-encoded body, so we need to do the chunked dance
562
           properly to know when the end of the body is reached */
563
11.8k
        size_t consumed = 0;
564
565
        /* now parse the chunked piece of data so that we can
566
           properly tell when the stream ends */
567
11.8k
        result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
568
11.8k
        if(result)
569
54
          return result;
570
11.8k
        if(Curl_httpchunk_is_done(data, &ts->ch)) {
571
          /* we are done reading chunks! */
572
12
          infof(data, "chunk reading DONE");
573
12
          ts->keepon = KEEPON_DONE;
574
12
        }
575
11.8k
      }
576
2.49k
      else if(ts->cl) {
577
        /* A Content-Length based body: count down the counter
578
           and make sure to break out of the loop when we are done! */
579
2.49k
        ts->cl--;
580
2.49k
        if(ts->cl <= 0) {
581
10
          ts->keepon = KEEPON_DONE;
582
10
          break;
583
10
        }
584
2.49k
      }
585
14.3k
      continue;
586
14.3k
    }
587
588
882k
    if(ts->maybe_folded) {
589
26.3k
      if(ISBLANK(byte)) {
590
1.70k
        Curl_http_to_fold(&ts->rcvbuf);
591
1.70k
        ts->leading_unfold = TRUE;
592
1.70k
      }
593
24.6k
      else {
594
24.6k
        result = single_header(cf, data, ts);
595
24.6k
        if(result)
596
276
          return result;
597
        /* now handle the new byte */
598
24.6k
      }
599
26.1k
      ts->maybe_folded = FALSE;
600
26.1k
    }
601
602
882k
    if(ts->leading_unfold) {
603
5.28k
      if(ISBLANK(byte))
604
        /* skip a bit brother */
605
3.64k
        continue;
606
      /* non-blank, insert a space then continue the unfolding */
607
1.64k
      if(curlx_dyn_addn(&ts->rcvbuf, " ", 1)) {
608
0
        failf(data, "CONNECT response too large");
609
0
        return CURLE_RECV_ERROR;
610
0
      }
611
1.64k
      ts->leading_unfold = FALSE;
612
1.64k
    }
613
878k
    if(curlx_dyn_addn(&ts->rcvbuf, &byte, 1)) {
614
11
      failf(data, "CONNECT response too large");
615
11
      return CURLE_RECV_ERROR;
616
11
    }
617
618
    /* if this is not the end of a header line then continue */
619
878k
    if(byte != 0x0a)
620
849k
      continue;
621
29.3k
    else {
622
29.3k
      const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
623
29.3k
      size_t hlen = curlx_dyn_len(&ts->rcvbuf);
624
29.3k
      if(hlen && ISNEWLINE(linep[0])) {
625
        /* end of headers */
626
2.77k
        result = single_header(cf, data, ts);
627
2.77k
        if(result)
628
17
          return result;
629
2.77k
      }
630
26.6k
      else
631
26.6k
        ts->maybe_folded = TRUE;
632
29.3k
    }
633
634
29.3k
    if(result)
635
0
      return result;
636
29.3k
  } /* while there is buffer left and loop is requested */
637
638
6.32k
  if(error)
639
3.69k
    result = CURLE_RECV_ERROR;
640
6.32k
  *done = (ts->keepon == KEEPON_DONE);
641
6.32k
  if(!result && *done &&
642
2.63k
     data->info.httpproxycode / 100 != 2 &&
643
1.28k
     !(h1_proxy_is_udp(cf) && data->info.httpproxycode == 101)) {
644
    /* Deal with the possibly already received authenticate
645
       headers. 'newurl' is set to a new URL if we must loop. */
646
178
    result = Curl_http_auth_act(data);
647
178
  }
648
6.32k
  return result;
649
11.6k
}
650
651
static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
652
                           struct Curl_easy *data,
653
                           struct h1_tunnel_state *ts)
654
29.8k
{
655
29.8k
  struct connectdata *conn = cf->conn;
656
29.8k
  CURLcode result;
657
29.8k
  bool done;
658
659
29.8k
  if(tunnel_is_established(ts))
660
0
    return CURLE_OK;
661
29.8k
  if(tunnel_is_failed(ts))
662
0
    return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
663
664
29.8k
  do {
665
666
29.8k
    if(Curl_timeleft_ms(data) < 0) {
667
0
      failf(data, "Proxy CONNECT aborted due to timeout");
668
0
      result = CURLE_OPERATION_TIMEDOUT;
669
0
      goto out;
670
0
    }
671
672
29.8k
    switch(ts->tunnel_state) {
673
6.93k
    case H1_TUNNEL_INIT:
674
      /* Prepare the CONNECT request and make a first attempt to send. */
675
6.93k
      CURL_TRC_CF(data, cf, "CONNECT start");
676
6.93k
      result = start_CONNECT(cf, data, ts);
677
6.93k
      if(result)
678
183
        goto out;
679
6.74k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
680
6.74k
      FALLTHROUGH();
681
682
24.7k
    case H1_TUNNEL_CONNECT:
683
      /* see that the request is completely sent */
684
24.7k
      CURL_TRC_CF(data, cf, "CONNECT send");
685
24.7k
      result = send_CONNECT(cf, data, ts, &done);
686
24.7k
      if(result || !done)
687
17.9k
        goto out;
688
6.74k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
689
6.74k
      FALLTHROUGH();
690
691
11.6k
    case H1_TUNNEL_RECEIVE:
692
      /* read what is there */
693
11.6k
      CURL_TRC_CF(data, cf, "CONNECT receive");
694
11.6k
      result = recv_CONNECT_resp(cf, data, ts, &done);
695
11.6k
      if(result)
696
4.12k
        CURL_TRC_CF(data, cf, "error receiving CONNECT response: %d",
697
11.6k
                    (int)result);
698
11.6k
      if(!result)
699
7.51k
        result = Curl_pgrsUpdate(data);
700
      /* error or not complete yet. return for more multi-multi */
701
11.6k
      if(result || !done)
702
9.03k
        goto out;
703
      /* got it */
704
2.61k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
705
2.61k
      FALLTHROUGH();
706
707
2.61k
    case H1_TUNNEL_RESPONSE:
708
2.61k
      CURL_TRC_CF(data, cf, "CONNECT response");
709
2.61k
      if(data->req.newurl) {
710
        /* not the "final" response, we need to do a follow up request.
711
         * If the other side indicated a connection close, or if someone
712
         * else told us to close this connection, do so now.
713
         */
714
15
        Curl_req_soft_reset(&data->req, data);
715
15
        if(ts->close_connection || conn->bits.close) {
716
          /* Close this filter and the sub-chain, re-connect the
717
           * sub-chain and continue. Closing this filter will
718
           * reset our tunnel state. To avoid recursion, we return
719
           * and expect to be called again.
720
           */
721
0
          CURL_TRC_CF(data, cf, "CONNECT need to close+open");
722
0
          infof(data, "Connect me again please");
723
0
          return CURLE_AGAIN;
724
0
        }
725
15
        else {
726
          /* staying on this connection, reset state */
727
15
          h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
728
15
        }
729
15
      }
730
2.61k
      break;
731
732
2.61k
    default:
733
0
      break;
734
29.8k
    }
735
736
29.8k
  } while(data->req.newurl);
737
738
2.60k
  DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
739
2.60k
  if(h1_proxy_is_udp(cf)) {
740
    /* RFC 9298: Accept 101 Upgrade for HTTP/1.1 and
741
     * 2xx responses for HTTP/2 and HTTP/3 proxies. */
742
1.24k
    if(data->info.httpproxycode / 100 != 2 &&
743
1.14k
       data->info.httpproxycode != 101) {
744
40
      curlx_safefree(data->req.newurl);
745
40
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
746
40
      failf(data, "CONNECT-UDP tunnel failed, response %d",
747
40
            data->req.httpcode);
748
40
      return CURLE_COULDNT_CONNECT;
749
40
    }
750
1.24k
  }
751
1.35k
  else {
752
1.35k
    if(data->info.httpproxycode / 100 != 2) {
753
      /* a non-2xx response and we have no next URL to try. */
754
102
      curlx_safefree(data->req.newurl);
755
102
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
756
102
      failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
757
102
      return CURLE_COULDNT_CONNECT;
758
102
    }
759
1.35k
  }
760
  /* 2xx response, SUCCESS! */
761
  /* 101 Switching Protocol for CONNECT-UDP */
762
2.45k
  h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
763
2.45k
  infof(data, "CONNECT%s tunnel established, response %d",
764
2.45k
        h1_proxy_is_udp(cf) ? "-UDP" : "", data->info.httpproxycode);
765
2.45k
  result = CURLE_OK;
766
767
29.6k
out:
768
29.6k
  if(result)
769
4.31k
    h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
770
29.6k
  return result;
771
2.45k
}
772
773
static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
774
                                    struct Curl_easy *data,
775
                                    bool *done)
776
29.8k
{
777
29.8k
  CURLcode result;
778
29.8k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
779
29.8k
  struct h1_tunnel_state *ts = pctx->ts;
780
781
29.8k
  if(cf->connected) {
782
0
    *done = TRUE;
783
0
    return CURLE_OK;
784
0
  }
785
786
29.8k
  CURL_TRC_CF(data, cf, "connect");
787
29.8k
  result = cf->next->cft->do_connect(cf->next, data, done);
788
29.8k
  if(result || !*done)
789
0
    return result;
790
791
29.8k
  *done = FALSE;
792
29.8k
  if(!ts) {
793
0
    result = tunnel_init(cf, data, &ts);
794
0
    if(result)
795
0
      return result;
796
0
    pctx->ts = ts;
797
0
  }
798
799
  /* We want "seamless" operations through HTTP proxy tunnel */
800
801
29.8k
  result = H1_CONNECT(cf, data, ts);
802
29.8k
  if(result)
803
4.45k
    goto out;
804
25.3k
  curlx_safefree(data->req.hd_proxy_auth);
805
806
29.8k
out:
807
29.8k
  *done = (result == CURLE_OK) && tunnel_is_established(pctx->ts);
808
29.8k
  if(*done) {
809
2.45k
    cf->connected = TRUE;
810
    /* The real request will follow the CONNECT, reset request partially */
811
2.45k
    Curl_req_soft_reset(&data->req, data);
812
2.45k
    Curl_client_reset(data);
813
2.45k
    Curl_pgrsReset(data);
814
2.45k
    cf_tunnel_free(cf, data);
815
2.45k
  }
816
29.8k
  return result;
817
25.3k
}
818
819
static CURLcode cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
820
                                           struct Curl_easy *data,
821
                                           struct easy_pollset *ps)
822
75.7k
{
823
75.7k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
824
75.7k
  struct h1_tunnel_state *ts = pctx->ts;
825
75.7k
  CURLcode result = CURLE_OK;
826
827
75.7k
  if(!cf->connected) {
828
    /* If we are not connected, but the filter "below" is
829
     * and not waiting on something, we are tunneling. */
830
10.4k
    curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
831
10.4k
    if(ts) {
832
      /* when we have sent a CONNECT to a proxy, we should rather either
833
         wait for the socket to become readable to be able to get the
834
         response headers or if we are still sending the request, wait
835
         for write. */
836
10.4k
      if(tunnel_want_send(ts))
837
9.97k
        result = Curl_pollset_set_out_only(data, ps, sock);
838
468
      else
839
468
        result = Curl_pollset_set_in_only(data, ps, sock);
840
10.4k
    }
841
0
    else
842
0
      result = Curl_pollset_set_out_only(data, ps, sock);
843
10.4k
  }
844
65.2k
  else {
845
65.2k
    if(cf->next)
846
65.2k
      result = cf->next->cft->adjust_pollset(cf->next, data, ps);
847
65.2k
  }
848
75.7k
  return result;
849
75.7k
}
850
851
static bool cf_h1_proxy_data_pending(struct Curl_cfilter *cf,
852
                                     const struct Curl_easy *data)
853
7.89k
{
854
7.89k
  return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
855
7.89k
}
856
857
static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
858
                                struct Curl_easy *data)
859
6.91k
{
860
6.91k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
861
862
6.91k
  CURL_TRC_CF(data, cf, "destroy");
863
6.91k
  if(pctx) {
864
6.91k
    cf_tunnel_free(cf, data);
865
6.91k
    Curl_peer_unlink(&pctx->peer);
866
6.91k
    curlx_safefree(cf->ctx);
867
6.91k
  }
868
6.91k
}
869
870
static CURLcode cf_h1_proxy_query(struct Curl_cfilter *cf,
871
                                  struct Curl_easy *data,
872
                                  int query, int *pres1, void *pres2)
873
198k
{
874
198k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
875
198k
  struct h1_tunnel_state *ts = pctx ? pctx->ts : NULL;
876
198k
  switch(query) {
877
0
  case CF_QUERY_HOST_PORT:
878
0
    if(!ts || !ts->dest)
879
0
      break;
880
0
    *pres1 = (int)ts->dest->port;
881
0
    *((const char **)pres2) = ts->dest->hostname;
882
0
    return CURLE_OK;
883
0
  case CF_QUERY_ALPN_NEGOTIATED: {
884
0
    const char **palpn = pres2;
885
0
    DEBUGASSERT(palpn);
886
0
    *palpn = NULL;
887
0
    return CURLE_OK;
888
0
  }
889
198k
  default:
890
198k
    break;
891
198k
  }
892
198k
  return cf->next ?
893
198k
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
894
198k
    CURLE_UNKNOWN_OPTION;
895
198k
}
896
897
struct Curl_cftype Curl_cft_h1_proxy = {
898
  "H1-PROXY",
899
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
900
  0,
901
  cf_h1_proxy_destroy,
902
  cf_h1_proxy_connect,
903
  Curl_cf_def_shutdown,
904
  cf_h1_proxy_adjust_pollset,
905
  cf_h1_proxy_data_pending,
906
  Curl_cf_def_send,
907
  Curl_cf_def_recv,
908
  Curl_cf_def_cntrl,
909
  Curl_cf_def_conn_is_alive,
910
  Curl_cf_def_conn_keep_alive,
911
  cf_h1_proxy_query,
912
};
913
914
CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
915
                                       struct Curl_easy *data,
916
                                       struct Curl_peer *peer,
917
                                       struct Curl_peer *dest,
918
                                       int httpversion,
919
                                       bool udp_tunnel)
920
6.92k
{
921
6.92k
  struct Curl_cfilter *cf;
922
6.92k
  struct cf_h1_proxy_ctx *pctx;
923
6.92k
  struct h1_tunnel_state *ts;
924
6.92k
  CURLcode result;
925
926
6.92k
  (void)data;
927
6.92k
  if(!dest)
928
0
    return CURLE_FAILED_INIT;
929
6.92k
  if((httpversion < 10) || (httpversion >= 20))
930
0
    return CURLE_FAILED_INIT;
931
932
6.92k
  ts = curlx_calloc(1, sizeof(*ts));
933
6.92k
  if(!ts) {
934
5
    result = CURLE_OUT_OF_MEMORY;
935
5
    goto out;
936
5
  }
937
6.92k
  Curl_peer_link(&ts->dest, dest);
938
6.92k
  ts->udp_tunnel = udp_tunnel;
939
6.92k
  ts->httpversion = httpversion;
940
6.92k
  curlx_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
941
6.92k
  curlx_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
942
6.92k
  Curl_httpchunk_init(data, &ts->ch, TRUE, TRUE);
943
944
6.92k
  pctx = curlx_calloc(1, sizeof(*pctx));
945
6.92k
  if(!pctx) {
946
3
    result = CURLE_OUT_OF_MEMORY;
947
3
    goto out;
948
3
  }
949
6.91k
  Curl_peer_link(&pctx->peer, peer);
950
6.91k
  pctx->ts = ts;
951
6.91k
  result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, pctx);
952
6.91k
  if(result) {
953
3
    Curl_peer_unlink(&pctx->peer);
954
3
    curlx_free(pctx);
955
3
    goto out;
956
3
  }
957
6.91k
  ts = NULL;
958
6.91k
  Curl_conn_cf_insert_after(cf_at, cf);
959
960
6.92k
out:
961
6.92k
  tunnel_free(ts, data);
962
6.92k
  return result;
963
6.91k
}
964
965
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */