Coverage Report

Created: 2026-09-01 07:00

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