Coverage Report

Created: 2026-12-01 06:25

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