Coverage Report

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