Coverage Report

Created: 2026-09-14 07:06

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