Coverage Report

Created: 2026-07-16 06:10

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