Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/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
#include "urldata.h"
29
#include "curlx/dynbuf.h"
30
#include "sendf.h"
31
#include "http.h"
32
#include "http1.h"
33
#include "http_proxy.h"
34
#include "select.h"
35
#include "progress.h"
36
#include "cfilters.h"
37
#include "cf-h1-proxy.h"
38
#include "connect.h"
39
#include "curl_trc.h"
40
#include "strcase.h"
41
#include "curlx/strparse.h"
42
43
44
typedef enum {
45
  H1_TUNNEL_INIT,     /* init/default/no tunnel state */
46
  H1_TUNNEL_CONNECT,  /* CONNECT request is being send */
47
  H1_TUNNEL_RECEIVE,  /* CONNECT answer is being received */
48
  H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
49
  H1_TUNNEL_ESTABLISHED,
50
  H1_TUNNEL_FAILED
51
} h1_tunnel_state;
52
53
/* struct for HTTP CONNECT tunneling */
54
struct h1_tunnel_state {
55
  struct dynbuf rcvbuf;
56
  struct dynbuf request_data;
57
  size_t nsent;
58
  size_t headerlines;
59
  struct Curl_chunker ch;
60
  enum keeponval {
61
    KEEPON_DONE,
62
    KEEPON_CONNECT,
63
    KEEPON_IGNORE
64
  } keepon;
65
  curl_off_t cl; /* size of content to read and ignore */
66
  h1_tunnel_state tunnel_state;
67
  BIT(chunked_encoding);
68
  BIT(close_connection);
69
  BIT(maybe_folded);
70
  BIT(leading_unfold);
71
};
72
73
static bool tunnel_is_established(struct h1_tunnel_state *ts)
74
0
{
75
0
  return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
76
0
}
77
78
static bool tunnel_is_failed(struct h1_tunnel_state *ts)
79
0
{
80
0
  return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
81
0
}
82
83
static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
84
                              struct Curl_easy *data,
85
                              struct h1_tunnel_state *ts)
86
0
{
87
0
  (void)data;
88
0
  (void)cf;
89
0
  DEBUGASSERT(ts);
90
0
  curlx_dyn_reset(&ts->rcvbuf);
91
0
  curlx_dyn_reset(&ts->request_data);
92
0
  ts->tunnel_state = H1_TUNNEL_INIT;
93
0
  ts->keepon = KEEPON_CONNECT;
94
0
  ts->cl = 0;
95
0
  ts->close_connection = FALSE;
96
0
  ts->maybe_folded = FALSE;
97
0
  ts->leading_unfold = FALSE;
98
0
  return CURLE_OK;
99
0
}
100
101
static CURLcode tunnel_init(struct Curl_cfilter *cf,
102
                            struct Curl_easy *data,
103
                            struct h1_tunnel_state **pts)
104
0
{
105
0
  struct h1_tunnel_state *ts;
106
107
0
  if(cf->conn->scheme->flags & PROTOPT_NOTCPPROXY) {
108
0
    failf(data, "%s cannot be done over CONNECT", cf->conn->scheme->name);
109
0
    return CURLE_UNSUPPORTED_PROTOCOL;
110
0
  }
111
112
0
  ts = curlx_calloc(1, sizeof(*ts));
113
0
  if(!ts)
114
0
    return CURLE_OUT_OF_MEMORY;
115
116
0
  infof(data, "allocate connect buffer");
117
118
0
  curlx_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
119
0
  curlx_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
120
0
  Curl_httpchunk_init(data, &ts->ch, TRUE);
121
122
0
  *pts = ts;
123
0
  return tunnel_reinit(cf, data, ts);
124
0
}
125
126
static void h1_tunnel_go_state(struct Curl_cfilter *cf,
127
                               struct h1_tunnel_state *ts,
128
                               h1_tunnel_state new_state,
129
                               struct Curl_easy *data)
130
0
{
131
0
  if(ts->tunnel_state == new_state)
132
0
    return;
133
  /* entering this one */
134
0
  switch(new_state) {
135
0
  case H1_TUNNEL_INIT:
136
0
    CURL_TRC_CF(data, cf, "new tunnel state 'init'");
137
0
    tunnel_reinit(cf, data, ts);
138
0
    break;
139
140
0
  case H1_TUNNEL_CONNECT:
141
0
    CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
142
0
    ts->tunnel_state = H1_TUNNEL_CONNECT;
143
0
    ts->keepon = KEEPON_CONNECT;
144
0
    curlx_dyn_reset(&ts->rcvbuf);
145
0
    break;
146
147
0
  case H1_TUNNEL_RECEIVE:
148
0
    CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
149
0
    ts->tunnel_state = H1_TUNNEL_RECEIVE;
150
0
    break;
151
152
0
  case H1_TUNNEL_RESPONSE:
153
0
    CURL_TRC_CF(data, cf, "new tunnel state 'response'");
154
0
    ts->tunnel_state = H1_TUNNEL_RESPONSE;
155
0
    break;
156
157
0
  case H1_TUNNEL_ESTABLISHED:
158
0
    CURL_TRC_CF(data, cf, "new tunnel state 'established'");
159
0
    infof(data, "CONNECT phase completed");
160
0
    data->state.authproxy.done = TRUE;
161
0
    data->state.authproxy.multipass = FALSE;
162
0
    FALLTHROUGH();
163
0
  case H1_TUNNEL_FAILED:
164
0
    if(new_state == H1_TUNNEL_FAILED)
165
0
      CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
166
0
    ts->tunnel_state = new_state;
167
0
    curlx_dyn_reset(&ts->rcvbuf);
168
0
    curlx_dyn_reset(&ts->request_data);
169
    /* restore the protocol pointer */
170
0
    data->info.httpcode = 0; /* clear it as it might have been used for the
171
                                proxy */
172
    /* If a proxy-authorization header was used for the proxy, then we should
173
       make sure that it is not accidentally used for the document request
174
       after we have connected. So let's free and clear it here. */
175
0
    Curl_safefree(data->state.aptr.proxyuserpwd);
176
0
    break;
177
0
  }
178
0
}
179
180
static void tunnel_free(struct Curl_cfilter *cf,
181
                        struct Curl_easy *data)
182
0
{
183
0
  if(cf) {
184
0
    struct h1_tunnel_state *ts = cf->ctx;
185
0
    if(ts) {
186
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
187
0
      curlx_dyn_free(&ts->rcvbuf);
188
0
      curlx_dyn_free(&ts->request_data);
189
0
      Curl_httpchunk_free(data, &ts->ch);
190
0
      curlx_free(ts);
191
0
      cf->ctx = NULL;
192
0
    }
193
0
  }
194
0
}
195
196
static bool tunnel_want_send(struct h1_tunnel_state *ts)
197
0
{
198
0
  return ts->tunnel_state == H1_TUNNEL_CONNECT;
199
0
}
200
201
static CURLcode start_CONNECT(struct Curl_cfilter *cf,
202
                              struct Curl_easy *data,
203
                              struct h1_tunnel_state *ts)
204
0
{
205
0
  struct httpreq *req = NULL;
206
0
  int http_minor;
207
0
  CURLcode result;
208
209
  /* This only happens if we have looped here due to authentication reasons,
210
     and we do not really use the newly cloned URL here then. Free it. */
211
0
  Curl_safefree(data->req.newurl);
212
213
0
  result = Curl_http_proxy_create_CONNECT(&req, cf, data, 1);
214
0
  if(result)
215
0
    goto out;
216
217
0
  infof(data, "Establish HTTP proxy tunnel to %s", req->authority);
218
219
0
  curlx_dyn_reset(&ts->request_data);
220
0
  ts->nsent = 0;
221
0
  ts->headerlines = 0;
222
0
  http_minor = (cf->conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? 0 : 1;
223
224
0
  result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
225
0
  if(!result)
226
0
    result = Curl_creader_set_null(data);
227
228
0
out:
229
0
  if(result)
230
0
    failf(data, "Failed sending CONNECT to proxy");
231
0
  if(req)
232
0
    Curl_http_req_free(req);
233
0
  return result;
234
0
}
235
236
static CURLcode send_CONNECT(struct Curl_cfilter *cf,
237
                             struct Curl_easy *data,
238
                             struct h1_tunnel_state *ts,
239
                             bool *done)
240
0
{
241
0
  const uint8_t *buf = curlx_dyn_uptr(&ts->request_data);
242
0
  size_t request_len = curlx_dyn_len(&ts->request_data);
243
0
  size_t blen = request_len;
244
0
  CURLcode result = CURLE_OK;
245
0
  size_t nwritten;
246
247
0
  if(blen <= ts->nsent)
248
0
    goto out;  /* we are done */
249
250
0
  blen -= ts->nsent;
251
0
  buf += ts->nsent;
252
253
0
  result = cf->next->cft->do_send(cf->next, data, buf, blen, FALSE, &nwritten);
254
0
  if(result) {
255
0
    if(result == CURLE_AGAIN)
256
0
      result = CURLE_OK;
257
0
    goto out;
258
0
  }
259
260
0
  DEBUGASSERT(blen >= nwritten);
261
0
  ts->nsent += nwritten;
262
0
  Curl_debug(data, CURLINFO_HEADER_OUT, (const char *)buf, nwritten);
263
264
0
out:
265
0
  if(result)
266
0
    failf(data, "Failed sending CONNECT to proxy");
267
0
  *done = (!result && (ts->nsent >= request_len));
268
0
  return result;
269
0
}
270
271
static CURLcode on_resp_header(struct Curl_cfilter *cf,
272
                               struct Curl_easy *data,
273
                               struct h1_tunnel_state *ts,
274
                               const char *header)
275
0
{
276
0
  CURLcode result = CURLE_OK;
277
0
  struct SingleRequest *k = &data->req;
278
0
  (void)cf;
279
280
0
  if((checkprefix("WWW-Authenticate:", header) && (401 == k->httpcode)) ||
281
0
     (checkprefix("Proxy-authenticate:", header) && (407 == k->httpcode))) {
282
283
0
    bool proxy = (k->httpcode == 407);
284
0
    char *auth = Curl_copy_header_value(header);
285
0
    if(!auth)
286
0
      return CURLE_OUT_OF_MEMORY;
287
288
0
    CURL_TRC_CF(data, cf, "CONNECT: fwd auth header '%s'", header);
289
0
    result = Curl_http_input_auth(data, proxy, auth);
290
291
0
    curlx_free(auth);
292
293
0
    if(result)
294
0
      return result;
295
0
  }
296
0
  else if(checkprefix("Content-Length:", header)) {
297
0
    if(k->httpcode / 100 == 2) {
298
      /* A client MUST ignore any Content-Length or Transfer-Encoding
299
         header fields received in a successful response to CONNECT.
300
         "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
301
0
      infof(data, "Ignoring Content-Length in CONNECT %03d response",
302
0
            k->httpcode);
303
0
    }
304
0
    else {
305
0
      const char *p = header + strlen("Content-Length:");
306
0
      if(curlx_str_numblanks(&p, &ts->cl)) {
307
0
        failf(data, "Unsupported Content-Length value");
308
0
        return CURLE_WEIRD_SERVER_REPLY;
309
0
      }
310
0
    }
311
0
  }
312
0
  else if(Curl_compareheader(header,
313
0
                             STRCONST("Connection:"), STRCONST("close")))
314
0
    ts->close_connection = TRUE;
315
0
  else if(checkprefix("Transfer-Encoding:", header)) {
316
0
    if(k->httpcode / 100 == 2) {
317
      /* A client MUST ignore any Content-Length or Transfer-Encoding
318
         header fields received in a successful response to CONNECT.
319
         "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
320
0
      infof(data, "Ignoring Transfer-Encoding in "
321
0
            "CONNECT %03d response", k->httpcode);
322
0
    }
323
0
    else if(Curl_compareheader(header,
324
0
                               STRCONST("Transfer-Encoding:"),
325
0
                               STRCONST("chunked"))) {
326
0
      infof(data, "CONNECT responded chunked");
327
0
      ts->chunked_encoding = TRUE;
328
      /* reset our chunky engine */
329
0
      Curl_httpchunk_reset(data, &ts->ch, TRUE);
330
0
    }
331
0
  }
332
0
  else if(Curl_compareheader(header,
333
0
                             STRCONST("Proxy-Connection:"),
334
0
                             STRCONST("close")))
335
0
    ts->close_connection = TRUE;
336
0
  else if(!strncmp(header, "HTTP/1.", 7) &&
337
0
          ((header[7] == '0') || (header[7] == '1')) &&
338
0
          (header[8] == ' ') &&
339
0
          ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
340
0
          !ISDIGIT(header[12])) {
341
    /* store the HTTP code from the proxy */
342
0
    data->info.httpproxycode = k->httpcode = ((header[9] - '0') * 100) +
343
0
      ((header[10] - '0') * 10) + (header[11] - '0');
344
0
  }
345
0
  return result;
346
0
}
347
348
static CURLcode single_header(struct Curl_cfilter *cf,
349
                              struct Curl_easy *data,
350
                              struct h1_tunnel_state *ts)
351
0
{
352
0
  CURLcode result = CURLE_OK;
353
0
  const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
354
0
  size_t line_len = curlx_dyn_len(&ts->rcvbuf); /* bytes in this line */
355
0
  const struct SingleRequest *k = &data->req;
356
0
  int writetype;
357
0
  ts->headerlines++;
358
359
  /* output debug if that is requested */
360
0
  Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
361
362
  /* send the header to the callback */
363
0
  writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
364
0
    (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
365
0
  result = Curl_client_write(data, writetype, linep, line_len);
366
0
  if(result)
367
0
    return result;
368
369
0
  result = Curl_bump_headersize(data, line_len, TRUE);
370
0
  if(result)
371
0
    return result;
372
373
  /* Newlines are CRLF, so the CR is ignored as the line is not
374
     really terminated until the LF comes. Treat a following CR
375
     as end-of-headers as well.*/
376
377
0
  if(ISNEWLINE(linep[0])) {
378
    /* end of response-headers from the proxy */
379
380
0
    if((407 == k->httpcode) && !data->state.authproblem) {
381
      /* If we get a 407 response code with content length
382
         when we have no auth problem, we must ignore the
383
         whole response-body */
384
0
      ts->keepon = KEEPON_IGNORE;
385
386
0
      if(ts->cl) {
387
0
        infof(data, "Ignore %" FMT_OFF_T " bytes of response-body", ts->cl);
388
0
      }
389
0
      else if(ts->chunked_encoding) {
390
0
        infof(data, "Ignore chunked response-body");
391
0
      }
392
0
      else {
393
        /* without content-length or chunked encoding, we
394
           cannot keep the connection alive since the close is
395
           the end signal so we bail out at once instead */
396
0
        CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
397
0
        ts->keepon = KEEPON_DONE;
398
0
      }
399
0
    }
400
0
    else {
401
0
      ts->keepon = KEEPON_DONE;
402
0
    }
403
404
0
    DEBUGASSERT(ts->keepon == KEEPON_IGNORE ||
405
0
                ts->keepon == KEEPON_DONE);
406
0
    return result;
407
0
  }
408
409
0
  result = on_resp_header(cf, data, ts, linep);
410
0
  if(result)
411
0
    return result;
412
413
0
  curlx_dyn_reset(&ts->rcvbuf);
414
0
  return result;
415
0
}
416
417
static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
418
                                  struct Curl_easy *data,
419
                                  struct h1_tunnel_state *ts,
420
                                  bool *done)
421
0
{
422
0
  CURLcode result = CURLE_OK;
423
0
  int error;
424
425
0
#define SELECT_OK      0
426
0
#define SELECT_ERROR   1
427
428
0
  error = SELECT_OK;
429
0
  *done = FALSE;
430
431
0
  while(ts->keepon) {
432
0
    size_t nread;
433
0
    char byte;
434
435
    /* Read one byte at a time to avoid a race condition. Wait at most one
436
       second before looping to ensure continuous pgrsUpdates. */
437
0
    result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
438
0
    if(result == CURLE_AGAIN)
439
      /* socket buffer drained, return */
440
0
      return CURLE_OK;
441
442
0
    if(!result)
443
0
      result = Curl_pgrsUpdate(data);
444
445
0
    if(result) {
446
0
      ts->keepon = KEEPON_DONE;
447
0
      break;
448
0
    }
449
450
0
    if(!nread) {
451
0
      if(data->set.proxyauth && data->state.authproxy.avail &&
452
0
         data->state.aptr.proxyuserpwd) {
453
        /* proxy auth was requested and there was proxy auth available,
454
           then deem this as "mere" proxy disconnect */
455
0
        ts->close_connection = TRUE;
456
0
        infof(data, "Proxy CONNECT connection closed");
457
0
      }
458
0
      else {
459
0
        error = SELECT_ERROR;
460
0
        failf(data, "Proxy CONNECT aborted");
461
0
      }
462
0
      ts->keepon = KEEPON_DONE;
463
0
      break;
464
0
    }
465
466
0
    if(ts->keepon == KEEPON_IGNORE) {
467
      /* This means we are currently ignoring a response-body */
468
469
0
      if(ts->cl) {
470
        /* A Content-Length based body: count down the counter
471
           and make sure to break out of the loop when we are done! */
472
0
        ts->cl--;
473
0
        if(ts->cl <= 0) {
474
0
          ts->keepon = KEEPON_DONE;
475
0
          break;
476
0
        }
477
0
      }
478
0
      else if(ts->chunked_encoding) {
479
        /* chunked-encoded body, so we need to do the chunked dance
480
           properly to know when the end of the body is reached */
481
0
        size_t consumed = 0;
482
483
        /* now parse the chunked piece of data so that we can
484
           properly tell when the stream ends */
485
0
        result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
486
0
        if(result)
487
0
          return result;
488
0
        if(Curl_httpchunk_is_done(data, &ts->ch)) {
489
          /* we are done reading chunks! */
490
0
          infof(data, "chunk reading DONE");
491
0
          ts->keepon = KEEPON_DONE;
492
0
        }
493
0
      }
494
0
      continue;
495
0
    }
496
497
0
    if(ts->maybe_folded) {
498
0
      if(ISBLANK(byte)) {
499
0
        Curl_http_to_fold(&ts->rcvbuf);
500
0
        ts->leading_unfold = TRUE;
501
0
      }
502
0
      else {
503
0
        result = single_header(cf, data, ts);
504
0
        if(result)
505
0
          return result;
506
        /* now handle the new byte */
507
0
      }
508
0
      ts->maybe_folded = FALSE;
509
0
    }
510
511
0
    if(ts->leading_unfold) {
512
0
      if(ISBLANK(byte))
513
        /* skip a bit brother */
514
0
        continue;
515
      /* non-blank, insert a space then continue the unfolding */
516
0
      if(curlx_dyn_addn(&ts->rcvbuf, " ", 1)) {
517
0
        failf(data, "CONNECT response too large");
518
0
        return CURLE_RECV_ERROR;
519
0
      }
520
0
      ts->leading_unfold = FALSE;
521
0
    }
522
0
    if(curlx_dyn_addn(&ts->rcvbuf, &byte, 1)) {
523
0
      failf(data, "CONNECT response too large");
524
0
      return CURLE_RECV_ERROR;
525
0
    }
526
527
    /* if this is not the end of a header line then continue */
528
0
    if(byte != 0x0a)
529
0
      continue;
530
0
    else {
531
0
      const char *linep = curlx_dyn_ptr(&ts->rcvbuf);
532
0
      size_t hlen = curlx_dyn_len(&ts->rcvbuf);
533
0
      if(hlen && ISNEWLINE(linep[0])) {
534
        /* end of headers */
535
0
        result = single_header(cf, data, ts);
536
0
        if(result)
537
0
          return result;
538
0
      }
539
0
      else
540
0
        ts->maybe_folded = TRUE;
541
0
    }
542
543
0
  } /* while there is buffer left and loop is requested */
544
545
0
  if(error)
546
0
    result = CURLE_RECV_ERROR;
547
0
  *done = (ts->keepon == KEEPON_DONE);
548
0
  if(!result && *done && data->info.httpproxycode / 100 != 2) {
549
    /* Deal with the possibly already received authenticate
550
       headers. 'newurl' is set to a new URL if we must loop. */
551
0
    result = Curl_http_auth_act(data);
552
0
  }
553
0
  return result;
554
0
}
555
556
static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
557
                           struct Curl_easy *data,
558
                           struct h1_tunnel_state *ts)
559
0
{
560
0
  struct connectdata *conn = cf->conn;
561
0
  CURLcode result;
562
0
  bool done;
563
564
0
  if(tunnel_is_established(ts))
565
0
    return CURLE_OK;
566
0
  if(tunnel_is_failed(ts))
567
0
    return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
568
569
0
  do {
570
571
0
    if(Curl_timeleft_ms(data) < 0) {
572
0
      failf(data, "Proxy CONNECT aborted due to timeout");
573
0
      result = CURLE_OPERATION_TIMEDOUT;
574
0
      goto out;
575
0
    }
576
577
0
    switch(ts->tunnel_state) {
578
0
    case H1_TUNNEL_INIT:
579
      /* Prepare the CONNECT request and make a first attempt to send. */
580
0
      CURL_TRC_CF(data, cf, "CONNECT start");
581
0
      result = start_CONNECT(cf, data, ts);
582
0
      if(result)
583
0
        goto out;
584
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
585
0
      FALLTHROUGH();
586
587
0
    case H1_TUNNEL_CONNECT:
588
      /* see that the request is completely sent */
589
0
      CURL_TRC_CF(data, cf, "CONNECT send");
590
0
      result = send_CONNECT(cf, data, ts, &done);
591
0
      if(result || !done)
592
0
        goto out;
593
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
594
0
      FALLTHROUGH();
595
596
0
    case H1_TUNNEL_RECEIVE:
597
      /* read what is there */
598
0
      CURL_TRC_CF(data, cf, "CONNECT receive");
599
0
      result = recv_CONNECT_resp(cf, data, ts, &done);
600
0
      if(!result)
601
0
        result = Curl_pgrsUpdate(data);
602
      /* error or not complete yet. return for more multi-multi */
603
0
      if(result || !done)
604
0
        goto out;
605
      /* got it */
606
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
607
0
      FALLTHROUGH();
608
609
0
    case H1_TUNNEL_RESPONSE:
610
0
      CURL_TRC_CF(data, cf, "CONNECT response");
611
0
      if(data->req.newurl) {
612
        /* not the "final" response, we need to do a follow up request.
613
         * If the other side indicated a connection close, or if someone
614
         * else told us to close this connection, do so now.
615
         */
616
0
        Curl_req_soft_reset(&data->req, data);
617
0
        if(ts->close_connection || conn->bits.close) {
618
          /* Close this filter and the sub-chain, re-connect the
619
           * sub-chain and continue. Closing this filter will
620
           * reset our tunnel state. To avoid recursion, we return
621
           * and expect to be called again.
622
           */
623
0
          CURL_TRC_CF(data, cf, "CONNECT need to close+open");
624
0
          infof(data, "Connect me again please");
625
0
          Curl_conn_cf_close(cf, data);
626
0
          result = Curl_conn_cf_connect(cf->next, data, &done);
627
0
          goto out;
628
0
        }
629
0
        else {
630
          /* staying on this connection, reset state */
631
0
          h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
632
0
        }
633
0
      }
634
0
      break;
635
636
0
    default:
637
0
      break;
638
0
    }
639
640
0
  } while(data->req.newurl);
641
642
0
  DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
643
0
  if(data->info.httpproxycode / 100 != 2) {
644
    /* a non-2xx response and we have no next URL to try. */
645
0
    Curl_safefree(data->req.newurl);
646
0
    h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
647
0
    failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
648
0
    return CURLE_RECV_ERROR;
649
0
  }
650
  /* 2xx response, SUCCESS! */
651
0
  h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
652
0
  infof(data, "CONNECT tunnel established, response %d",
653
0
        data->info.httpproxycode);
654
0
  result = CURLE_OK;
655
656
0
out:
657
0
  if(result)
658
0
    h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
659
0
  return result;
660
0
}
661
662
static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
663
                                    struct Curl_easy *data,
664
                                    bool *done)
665
0
{
666
0
  CURLcode result;
667
0
  struct h1_tunnel_state *ts = cf->ctx;
668
669
0
  if(cf->connected) {
670
0
    *done = TRUE;
671
0
    return CURLE_OK;
672
0
  }
673
674
0
  CURL_TRC_CF(data, cf, "connect");
675
0
  result = cf->next->cft->do_connect(cf->next, data, done);
676
0
  if(result || !*done)
677
0
    return result;
678
679
0
  *done = FALSE;
680
0
  if(!ts) {
681
0
    result = tunnel_init(cf, data, &ts);
682
0
    if(result)
683
0
      return result;
684
0
    cf->ctx = ts;
685
0
  }
686
687
  /* We want "seamless" operations through HTTP proxy tunnel */
688
689
0
  result = H1_CONNECT(cf, data, ts);
690
0
  if(result)
691
0
    goto out;
692
0
  Curl_safefree(data->state.aptr.proxyuserpwd);
693
694
0
out:
695
0
  *done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
696
0
  if(*done) {
697
0
    cf->connected = TRUE;
698
    /* The real request will follow the CONNECT, reset request partially */
699
0
    Curl_req_soft_reset(&data->req, data);
700
0
    Curl_client_reset(data);
701
0
    Curl_pgrsReset(data);
702
703
0
    tunnel_free(cf, data);
704
0
  }
705
0
  return result;
706
0
}
707
708
static CURLcode cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
709
                                           struct Curl_easy *data,
710
                                           struct easy_pollset *ps)
711
0
{
712
0
  struct h1_tunnel_state *ts = cf->ctx;
713
0
  CURLcode result = CURLE_OK;
714
715
0
  if(!cf->connected) {
716
    /* If we are not connected, but the filter "below" is
717
     * and not waiting on something, we are tunneling. */
718
0
    curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
719
0
    if(ts) {
720
      /* when we have sent a CONNECT to a proxy, we should rather either
721
         wait for the socket to become readable to be able to get the
722
         response headers or if we are still sending the request, wait
723
         for write. */
724
0
      if(tunnel_want_send(ts))
725
0
        result = Curl_pollset_set_out_only(data, ps, sock);
726
0
      else
727
0
        result = Curl_pollset_set_in_only(data, ps, sock);
728
0
    }
729
0
    else
730
0
      result = Curl_pollset_set_out_only(data, ps, sock);
731
0
  }
732
0
  return result;
733
0
}
734
735
static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
736
                                struct Curl_easy *data)
737
0
{
738
0
  CURL_TRC_CF(data, cf, "destroy");
739
0
  tunnel_free(cf, data);
740
0
}
741
742
static void cf_h1_proxy_close(struct Curl_cfilter *cf,
743
                              struct Curl_easy *data)
744
0
{
745
0
  CURL_TRC_CF(data, cf, "close");
746
0
  if(cf) {
747
0
    cf->connected = FALSE;
748
0
    if(cf->ctx) {
749
0
      h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
750
0
    }
751
0
    if(cf->next)
752
0
      cf->next->cft->do_close(cf->next, data);
753
0
  }
754
0
}
755
756
struct Curl_cftype Curl_cft_h1_proxy = {
757
  "H1-PROXY",
758
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
759
  0,
760
  cf_h1_proxy_destroy,
761
  cf_h1_proxy_connect,
762
  cf_h1_proxy_close,
763
  Curl_cf_def_shutdown,
764
  cf_h1_proxy_adjust_pollset,
765
  Curl_cf_def_data_pending,
766
  Curl_cf_def_send,
767
  Curl_cf_def_recv,
768
  Curl_cf_def_cntrl,
769
  Curl_cf_def_conn_is_alive,
770
  Curl_cf_def_conn_keep_alive,
771
  Curl_cf_http_proxy_query,
772
};
773
774
CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
775
                                       struct Curl_easy *data)
776
0
{
777
0
  struct Curl_cfilter *cf;
778
0
  CURLcode result;
779
780
0
  (void)data;
781
0
  result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
782
0
  if(!result)
783
0
    Curl_conn_cf_insert_after(cf_at, cf);
784
0
  return result;
785
0
}
786
787
#endif /* !CURL_DISABLE_PROXY && !CURL_DISABLE_HTTP */