Coverage Report

Created: 2026-09-01 06:59

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.67k
{
85
1.67k
  return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
86
1.67k
}
87
88
static bool tunnel_is_failed(struct h1_tunnel_state *ts)
89
1.54k
{
90
1.54k
  return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
91
1.54k
}
92
93
static bool h1_proxy_is_udp(struct Curl_cfilter *cf)
94
2.29k
{
95
2.29k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
96
2.29k
  return (pctx->udp_tunnel ? TRUE : FALSE);
97
2.29k
}
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
5.69k
{
149
5.69k
  if(ts->tunnel_state == new_state)
150
1.42k
    return;
151
  /* entering this one */
152
4.27k
  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.38k
  case H1_TUNNEL_CONNECT:
159
1.38k
    CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
160
1.38k
    ts->tunnel_state = H1_TUNNEL_CONNECT;
161
1.38k
    ts->keepon = KEEPON_CONNECT;
162
1.38k
    curlx_dyn_reset(&ts->rcvbuf);
163
1.38k
    break;
164
165
1.38k
  case H1_TUNNEL_RECEIVE:
166
1.38k
    CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
167
1.38k
    ts->tunnel_state = H1_TUNNEL_RECEIVE;
168
1.38k
    break;
169
170
54
  case H1_TUNNEL_RESPONSE:
171
54
    CURL_TRC_CF(data, cf, "new tunnel state 'response'");
172
54
    ts->tunnel_state = H1_TUNNEL_RESPONSE;
173
54
    break;
174
175
9
  case H1_TUNNEL_ESTABLISHED:
176
9
    CURL_TRC_CF(data, cf, "new tunnel state 'established'");
177
9
    infof(data, "CONNECT%s phase completed for HTTP proxy",
178
9
          h1_proxy_is_udp(cf) ? "-UDP" : "");
179
180
9
    data->state.authproxy.done = TRUE;
181
9
    data->state.authproxy.multipass = FALSE;
182
9
    FALLTHROUGH();
183
1.44k
  case H1_TUNNEL_FAILED:
184
1.44k
    if(new_state == H1_TUNNEL_FAILED)
185
1.43k
      CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
186
1.44k
    ts->tunnel_state = new_state;
187
1.44k
    curlx_dyn_reset(&ts->rcvbuf);
188
1.44k
    curlx_dyn_reset(&ts->request_data);
189
    /* restore the protocol pointer */
190
1.44k
    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.44k
    curlx_safefree(data->req.hd_proxy_auth);
196
1.44k
    break;
197
4.27k
  }
198
4.27k
}
199
200
static void tunnel_free(struct h1_tunnel_state *ts,
201
                        struct Curl_easy *data)
202
2.86k
{
203
2.86k
  if(ts) {
204
1.43k
    Curl_peer_unlink(&ts->dest);
205
1.43k
    curlx_dyn_free(&ts->rcvbuf);
206
1.43k
    curlx_dyn_free(&ts->request_data);
207
1.43k
    Curl_httpchunk_free(data, &ts->ch);
208
1.43k
    curlx_free(ts);
209
1.43k
  }
210
2.86k
}
211
212
static void cf_tunnel_free(struct Curl_cfilter *cf,
213
                           struct Curl_easy *data)
214
1.44k
{
215
1.44k
  if(cf) {
216
1.44k
    struct cf_h1_proxy_ctx *pctx = cf->ctx;
217
1.44k
    struct h1_tunnel_state *ts = pctx ? pctx->ts : NULL;
218
1.44k
    if(ts) {
219
1.43k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
220
1.43k
      tunnel_free(ts, data);
221
1.43k
      pctx->ts = NULL;
222
1.43k
    }
223
1.44k
  }
224
1.44k
}
225
226
static bool tunnel_want_send(struct h1_tunnel_state *ts)
227
116
{
228
116
  return ts->tunnel_state == H1_TUNNEL_CONNECT;
229
116
}
230
231
static CURLcode start_CONNECT(struct Curl_cfilter *cf,
232
                              struct Curl_easy *data,
233
                              struct h1_tunnel_state *ts)
234
1.43k
{
235
1.43k
  struct httpreq *req = NULL;
236
1.43k
  int http_minor;
237
1.43k
  CURLcode result;
238
239
1.43k
  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.43k
  curlx_safefree(data->req.newurl);
243
244
1.43k
  result = Curl_http_proxy_create_tunnel_request(&req, cf, data, ts->dest,
245
1.43k
                                                  PROXY_HTTP_V1,
246
1.43k
                                                  h1_proxy_is_udp(cf));
247
1.43k
  if(result)
248
44
    goto out;
249
250
1.38k
  curlx_dyn_reset(&ts->request_data);
251
1.38k
  ts->nsent = 0;
252
1.38k
  ts->headerlines = 0;
253
1.38k
  http_minor = ts->httpversion % 10;
254
255
1.38k
  result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
256
1.38k
  if(!result)
257
1.38k
    result = Curl_creader_set_null(data);
258
259
1.43k
out:
260
1.43k
  if(result)
261
44
    failf(data, "Failed sending CONNECT to proxy");
262
1.43k
  if(req)
263
1.38k
    Curl_http_req_free(req);
264
1.43k
  return result;
265
1.38k
}
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.38k
{
272
1.38k
  const uint8_t *buf = curlx_dyn_uptr(&ts->request_data);
273
1.38k
  size_t request_len = curlx_dyn_len(&ts->request_data);
274
1.38k
  size_t blen = request_len;
275
1.38k
  CURLcode result = CURLE_OK;
276
1.38k
  size_t nwritten;
277
278
1.38k
  if(blen <= ts->nsent)
279
0
    goto out;  /* we are done */
280
281
1.38k
  blen -= ts->nsent;
282
1.38k
  buf += ts->nsent;
283
284
1.38k
  result = cf->next->cft->do_send(cf->next, data, buf, blen, FALSE, &nwritten);
285
1.38k
  if(result) {
286
0
    if(result == CURLE_AGAIN)
287
0
      result = CURLE_OK;
288
0
    goto out;
289
0
  }
290
291
1.38k
  DEBUGASSERT(blen >= nwritten);
292
1.38k
  ts->nsent += nwritten;
293
1.38k
  Curl_debug(data, CURLINFO_HEADER_OUT, (const char *)buf, nwritten);
294
295
1.38k
out:
296
1.38k
  if(result)
297
0
    failf(data, "Failed sending CONNECT to proxy");
298
1.38k
  *done = (!result && (ts->nsent >= request_len));
299
1.38k
  return result;
300
1.38k
}
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
759
{
393
759
  CURLcode result = CURLE_OK;
394
759
  struct SingleRequest *k = &data->req;
395
759
  (void)cf;
396
397
759
  if((checkprefix("WWW-Authenticate:", header) && (401 == k->httpcode)) ||
398
759
     (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
759
  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
759
  else if(Curl_compareheader(header,
430
759
                             STRCONST("Connection:"), STRCONST("close")))
431
0
    ts->close_connection = TRUE;
432
759
  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
759
  else if(Curl_compareheader(header,
450
759
                             STRCONST("Proxy-Connection:"),
451
759
                             STRCONST("close")))
452
0
    ts->close_connection = TRUE;
453
759
  else if(!strncmp(header, "HTTP/1.", 7) &&
454
105
          ((header[7] == '0') || (header[7] == '1')) &&
455
65
          (header[8] == ' ') &&
456
41
          ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
457
21
          !ISDIGIT(header[12])) {
458
    /* store the HTTP code from the proxy */
459
20
    data->info.httpproxycode = k->httpcode = ((header[9] - '0') * 100) +
460
20
      ((header[10] - '0') * 10) + (header[11] - '0');
461
20
  }
462
759
  return result;
463
759
}
464
465
static CURLcode single_header(struct Curl_cfilter *cf,
466
                              struct Curl_easy *data,
467
                              struct h1_tunnel_state *ts)
468
832
{
469
832
  CURLcode result = CURLE_OK;
470
832
  const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
471
832
  size_t line_len = curlx_dyn_len(&ts->rcvbuf); /* bytes in this line */
472
832
  const struct SingleRequest *k = &data->req;
473
832
  int writetype;
474
832
  ts->headerlines++;
475
476
  /* output debug if that is requested */
477
832
  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
832
  result = Curl_verify_header(data, linep, line_len);
482
832
  if(result)
483
18
    return result;
484
485
  /* send the header to the callback */
486
814
  writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
487
814
    (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
488
814
  result = Curl_client_write(data, writetype, linep, line_len);
489
814
  if(result)
490
1
    return result;
491
492
813
  result = Curl_bump_headersize(data, line_len, TRUE);
493
813
  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
813
  if(ISNEWLINE(linep[0])) {
501
    /* end of response-headers from the proxy */
502
503
54
    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
0
      ts->keepon = KEEPON_IGNORE;
508
509
0
      if(ts->cl) {
510
0
        infof(data, "Ignore %" FMT_OFF_T " bytes of response-body", ts->cl);
511
0
      }
512
0
      else if(ts->chunked_encoding) {
513
0
        infof(data, "Ignore chunked response-body");
514
0
      }
515
0
      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
0
        CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
520
0
        ts->keepon = KEEPON_DONE;
521
0
      }
522
0
    }
523
54
    else {
524
54
      ts->keepon = KEEPON_DONE;
525
54
    }
526
527
54
    DEBUGASSERT(ts->keepon == KEEPON_IGNORE ||
528
54
                ts->keepon == KEEPON_DONE);
529
54
    return result;
530
54
  }
531
532
759
  if(h1_proxy_is_udp(cf)) {
533
0
    result = on_resp_header_udp(cf, data, ts, linep);
534
0
  }
535
759
  else {
536
759
    result = on_resp_header(cf, data, ts, linep);
537
759
  }
538
539
759
  if(result)
540
0
    return result;
541
542
759
  curlx_dyn_reset(&ts->rcvbuf);
543
759
  return result;
544
759
}
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.50k
{
551
1.50k
  CURLcode result = CURLE_OK;
552
1.50k
  int error;
553
554
1.50k
#define SELECT_OK      0
555
1.50k
#define SELECT_ERROR   1
556
557
1.50k
  error = SELECT_OK;
558
1.50k
  *done = FALSE;
559
560
83.1k
  while(ts->keepon) {
561
83.0k
    size_t nread;
562
83.0k
    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
83.0k
    result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
567
83.0k
    if(result == CURLE_AGAIN)
568
      /* socket buffer drained, return */
569
116
      return CURLE_OK;
570
571
82.9k
    if(!result)
572
82.9k
      result = Curl_pgrsUpdate(data);
573
574
82.9k
    if(result) {
575
0
      ts->keepon = KEEPON_DONE;
576
0
      break;
577
0
    }
578
579
82.9k
    if(!nread) {
580
1.31k
      if(ts->maybe_folded) {
581
        /* EOF right after LF: finalize the pending header line. */
582
166
        result = single_header(cf, data, ts);
583
166
        if(result)
584
3
          return result;
585
163
        ts->maybe_folded = FALSE;
586
163
      }
587
1.30k
      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
1.30k
      else {
595
1.30k
        error = SELECT_ERROR;
596
1.30k
        failf(data, "Proxy CONNECT aborted");
597
1.30k
      }
598
1.30k
      ts->keepon = KEEPON_DONE;
599
1.30k
      break;
600
1.31k
    }
601
602
81.6k
    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
81.6k
    if(ts->maybe_folded) {
633
796
      if(ISBLANK(byte)) {
634
185
        Curl_http_to_fold(&ts->rcvbuf);
635
185
        ts->leading_unfold = TRUE;
636
185
      }
637
611
      else {
638
611
        result = single_header(cf, data, ts);
639
611
        if(result)
640
15
          return result;
641
        /* now handle the new byte */
642
611
      }
643
781
      ts->maybe_folded = FALSE;
644
781
    }
645
646
81.6k
    if(ts->leading_unfold) {
647
838
      if(ISBLANK(byte))
648
        /* skip a bit brother */
649
665
        continue;
650
      /* non-blank, insert a space then continue the unfolding */
651
173
      if(curlx_dyn_addn(&ts->rcvbuf, " ", 1)) {
652
0
        failf(data, "CONNECT response too large");
653
0
        return CURLE_RECV_ERROR;
654
0
      }
655
173
      ts->leading_unfold = FALSE;
656
173
    }
657
80.9k
    if(curlx_dyn_addn(&ts->rcvbuf, &byte, 1)) {
658
4
      failf(data, "CONNECT response too large");
659
4
      return CURLE_RECV_ERROR;
660
4
    }
661
662
    /* if this is not the end of a header line then continue */
663
80.9k
    if(byte != 0x0a)
664
79.9k
      continue;
665
1.01k
    else {
666
1.01k
      const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
667
1.01k
      size_t hlen = curlx_dyn_len(&ts->rcvbuf);
668
1.01k
      if(hlen && ISNEWLINE(linep[0])) {
669
        /* end of headers */
670
55
        result = single_header(cf, data, ts);
671
55
        if(result)
672
1
          return result;
673
55
      }
674
962
      else
675
962
        ts->maybe_folded = TRUE;
676
1.01k
    }
677
678
1.01k
    if(result)
679
0
      return result;
680
1.01k
  } /* while there is buffer left and loop is requested */
681
682
1.36k
  if(error)
683
1.30k
    result = CURLE_RECV_ERROR;
684
1.36k
  *done = (ts->keepon == KEEPON_DONE);
685
1.36k
  if(!result && *done &&
686
54
     data->info.httpproxycode / 100 != 2 &&
687
45
     !(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
45
    result = Curl_http_auth_act(data);
691
45
  }
692
1.36k
  return result;
693
1.50k
}
694
695
static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
696
                           struct Curl_easy *data,
697
                           struct h1_tunnel_state *ts)
698
1.54k
{
699
1.54k
  struct connectdata *conn = cf->conn;
700
1.54k
  CURLcode result;
701
1.54k
  bool done;
702
703
1.54k
  if(tunnel_is_established(ts))
704
0
    return CURLE_OK;
705
1.54k
  if(tunnel_is_failed(ts))
706
0
    return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
707
708
1.54k
  do {
709
710
1.54k
    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.54k
    switch(ts->tunnel_state) {
717
1.43k
    case H1_TUNNEL_INIT:
718
      /* Prepare the CONNECT request and make a first attempt to send. */
719
1.43k
      CURL_TRC_CF(data, cf, "CONNECT start");
720
1.43k
      result = start_CONNECT(cf, data, ts);
721
1.43k
      if(result)
722
44
        goto out;
723
1.38k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
724
1.38k
      FALLTHROUGH();
725
726
1.38k
    case H1_TUNNEL_CONNECT:
727
      /* see that the request is completely sent */
728
1.38k
      CURL_TRC_CF(data, cf, "CONNECT send");
729
1.38k
      result = send_CONNECT(cf, data, ts, &done);
730
1.38k
      if(result || !done)
731
0
        goto out;
732
1.38k
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
733
1.38k
      FALLTHROUGH();
734
735
1.50k
    case H1_TUNNEL_RECEIVE:
736
      /* read what is there */
737
1.50k
      CURL_TRC_CF(data, cf, "CONNECT receive");
738
1.50k
      result = recv_CONNECT_resp(cf, data, ts, &done);
739
1.50k
      if(result)
740
1.33k
        CURL_TRC_CF(data, cf, "error receiving CONNECT response: %d",
741
1.50k
                    (int)result);
742
1.50k
      if(!result)
743
170
        result = Curl_pgrsUpdate(data);
744
      /* error or not complete yet. return for more multi-multi */
745
1.50k
      if(result || !done)
746
1.44k
        goto out;
747
      /* got it */
748
54
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
749
54
      FALLTHROUGH();
750
751
54
    case H1_TUNNEL_RESPONSE:
752
54
      CURL_TRC_CF(data, cf, "CONNECT response");
753
54
      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
54
      break;
775
776
54
    default:
777
0
      break;
778
1.54k
    }
779
780
1.54k
  } while(data->req.newurl);
781
782
54
  DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
783
54
  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
54
  else {
796
54
    if(data->info.httpproxycode / 100 != 2) {
797
      /* a non-2xx response and we have no next URL to try. */
798
45
      curlx_safefree(data->req.newurl);
799
45
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
800
45
      failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
801
45
      return CURLE_COULDNT_CONNECT;
802
45
    }
803
54
  }
804
  /* 2xx response, SUCCESS! */
805
  /* 101 Switching Protocol for CONNECT-UDP */
806
9
  h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
807
9
  if(h1_proxy_is_udp(cf))
808
0
    infof(data, "CONNECT-UDP tunnel established, response %d",
809
9
                                    data->info.httpproxycode);
810
9
  else
811
9
    infof(data, "CONNECT tunnel established, response %d",
812
9
                                    data->info.httpproxycode);
813
9
  result = CURLE_OK;
814
815
1.50k
out:
816
1.50k
  if(result)
817
1.37k
    h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
818
1.50k
  return result;
819
9
}
820
821
static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
822
                                    struct Curl_easy *data,
823
                                    bool *done)
824
1.54k
{
825
1.54k
  CURLcode result;
826
1.54k
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
827
1.54k
  struct h1_tunnel_state *ts = pctx->ts;
828
829
1.54k
  if(cf->connected) {
830
0
    *done = TRUE;
831
0
    return CURLE_OK;
832
0
  }
833
834
1.54k
  CURL_TRC_CF(data, cf, "connect");
835
1.54k
  result = cf->next->cft->do_connect(cf->next, data, done);
836
1.54k
  if(result || !*done)
837
0
    return result;
838
839
1.54k
  *done = FALSE;
840
1.54k
  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.54k
  result = H1_CONNECT(cf, data, ts);
850
1.54k
  if(result)
851
1.42k
    goto out;
852
125
  curlx_safefree(data->req.hd_proxy_auth);
853
854
1.54k
out:
855
1.54k
  *done = (result == CURLE_OK) && tunnel_is_established(pctx->ts);
856
1.54k
  if(*done) {
857
9
    cf->connected = TRUE;
858
    /* The real request will follow the CONNECT, reset request partially */
859
9
    Curl_req_soft_reset(&data->req, data);
860
9
    Curl_client_reset(data);
861
9
    Curl_pgrsReset(data);
862
9
    cf_tunnel_free(cf, data);
863
9
  }
864
1.54k
  return result;
865
125
}
866
867
static CURLcode cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
868
                                           struct Curl_easy *data,
869
                                           struct easy_pollset *ps)
870
121
{
871
121
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
872
121
  struct h1_tunnel_state *ts = pctx->ts;
873
121
  CURLcode result = CURLE_OK;
874
875
121
  if(!cf->connected) {
876
    /* If we are not connected, but the filter "below" is
877
     * and not waiting on something, we are tunneling. */
878
116
    curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
879
116
    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
116
      if(tunnel_want_send(ts))
885
0
        result = Curl_pollset_set_out_only(data, ps, sock);
886
116
      else
887
116
        result = Curl_pollset_set_in_only(data, ps, sock);
888
116
    }
889
0
    else
890
0
      result = Curl_pollset_set_out_only(data, ps, sock);
891
116
  }
892
5
  else {
893
5
    if(cf->next)
894
5
      result = cf->next->cft->adjust_pollset(cf->next, data, ps);
895
5
  }
896
121
  return result;
897
121
}
898
899
static bool cf_h1_proxy_data_pending(struct Curl_cfilter *cf,
900
                                     const struct Curl_easy *data)
901
37
{
902
37
  return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
903
37
}
904
905
static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
906
                                struct Curl_easy *data)
907
1.43k
{
908
1.43k
  CURL_TRC_CF(data, cf, "destroy");
909
1.43k
  cf_tunnel_free(cf, data);
910
1.43k
  curlx_safefree(cf->ctx);
911
1.43k
}
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
349
{
917
349
  struct cf_h1_proxy_ctx *pctx = cf->ctx;
918
349
  struct h1_tunnel_state *ts = pctx ? pctx->ts : NULL;
919
349
  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
349
  default:
933
349
    break;
934
349
  }
935
349
  return cf->next ?
936
349
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
937
349
    CURLE_UNKNOWN_OPTION;
938
349
}
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.43k
{
963
1.43k
  struct Curl_cfilter *cf;
964
1.43k
  struct cf_h1_proxy_ctx *pctx;
965
1.43k
  struct h1_tunnel_state *ts;
966
1.43k
  CURLcode result;
967
968
1.43k
  (void)data;
969
1.43k
  if(!dest)
970
0
    return CURLE_FAILED_INIT;
971
1.43k
  if((httpversion < 10) || (httpversion >= 20))
972
0
    return CURLE_FAILED_INIT;
973
974
1.43k
  ts = curlx_calloc(1, sizeof(*ts));
975
1.43k
  if(!ts) {
976
0
    result = CURLE_OUT_OF_MEMORY;
977
0
    goto out;
978
0
  }
979
1.43k
  Curl_peer_link(&ts->dest, dest);
980
1.43k
  ts->httpversion = httpversion;
981
1.43k
  curlx_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
982
1.43k
  curlx_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
983
1.43k
  Curl_httpchunk_init(data, &ts->ch, TRUE, TRUE);
984
985
1.43k
  pctx = curlx_calloc(1, sizeof(*pctx));
986
1.43k
  if(!pctx) {
987
0
    result = CURLE_OUT_OF_MEMORY;
988
0
    goto out;
989
0
  }
990
1.43k
  pctx->udp_tunnel = udp_tunnel;
991
1.43k
  pctx->ts = ts;
992
1.43k
  result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, pctx);
993
1.43k
  if(result) {
994
0
    curlx_free(pctx);
995
0
    goto out;
996
0
  }
997
1.43k
  ts = NULL;
998
1.43k
  Curl_conn_cf_insert_after(cf_at, cf);
999
1000
1.43k
out:
1001
1.43k
  tunnel_free(ts, data);
1002
1.43k
  return result;
1003
1.43k
}
1004
1005
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */