Coverage Report

Created: 2025-12-14 06:23

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
25
#include "curl_setup.h"
26
27
#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
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 "url.h"
37
#include "select.h"
38
#include "progress.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 "vtls/vtls.h"
45
#include "transfer.h"
46
#include "multiif.h"
47
#include "curlx/strparse.h"
48
49
50
typedef enum {
51
  H1_TUNNEL_INIT,     /* init/default/no tunnel state */
52
  H1_TUNNEL_CONNECT,  /* CONNECT request is being send */
53
  H1_TUNNEL_RECEIVE,  /* CONNECT answer is being received */
54
  H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
55
  H1_TUNNEL_ESTABLISHED,
56
  H1_TUNNEL_FAILED
57
} h1_tunnel_state;
58
59
/* struct for HTTP CONNECT tunneling */
60
struct h1_tunnel_state {
61
  struct dynbuf rcvbuf;
62
  struct dynbuf request_data;
63
  size_t nsent;
64
  size_t headerlines;
65
  struct Curl_chunker ch;
66
  enum keeponval {
67
    KEEPON_DONE,
68
    KEEPON_CONNECT,
69
    KEEPON_IGNORE
70
  } keepon;
71
  curl_off_t cl; /* size of content to read and ignore */
72
  h1_tunnel_state tunnel_state;
73
  BIT(chunked_encoding);
74
  BIT(close_connection);
75
};
76
77
static bool tunnel_is_established(struct h1_tunnel_state *ts)
78
0
{
79
0
  return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
80
0
}
81
82
static bool tunnel_is_failed(struct h1_tunnel_state *ts)
83
0
{
84
0
  return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
85
0
}
86
87
static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
88
                              struct Curl_easy *data,
89
                              struct h1_tunnel_state *ts)
90
0
{
91
0
  (void)data;
92
0
  (void)cf;
93
0
  DEBUGASSERT(ts);
94
0
  curlx_dyn_reset(&ts->rcvbuf);
95
0
  curlx_dyn_reset(&ts->request_data);
96
0
  ts->tunnel_state = H1_TUNNEL_INIT;
97
0
  ts->keepon = KEEPON_CONNECT;
98
0
  ts->cl = 0;
99
0
  ts->close_connection = 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->handler->flags & PROTOPT_NOTCPPROXY) {
110
0
    failf(data, "%s cannot be done over CONNECT", cf->conn->handler->scheme);
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 recv_CONNECT_resp(struct Curl_cfilter *cf,
352
                                  struct Curl_easy *data,
353
                                  struct h1_tunnel_state *ts,
354
                                  bool *done)
355
0
{
356
0
  CURLcode result = CURLE_OK;
357
0
  struct SingleRequest *k = &data->req;
358
0
  char *linep;
359
0
  size_t line_len;
360
0
  int error, writetype;
361
362
0
#define SELECT_OK      0
363
0
#define SELECT_ERROR   1
364
365
0
  error = SELECT_OK;
366
0
  *done = FALSE;
367
368
0
  while(ts->keepon) {
369
0
    size_t nread;
370
0
    char byte;
371
372
    /* Read one byte at a time to avoid a race condition. Wait at most one
373
       second before looping to ensure continuous pgrsUpdates. */
374
0
    result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
375
0
    if(result == CURLE_AGAIN)
376
      /* socket buffer drained, return */
377
0
      return CURLE_OK;
378
379
0
    if(!result)
380
0
      result = Curl_pgrsUpdate(data);
381
382
0
    if(result) {
383
0
      ts->keepon = KEEPON_DONE;
384
0
      break;
385
0
    }
386
387
0
    if(!nread) {
388
0
      if(data->set.proxyauth && data->state.authproxy.avail &&
389
0
         data->state.aptr.proxyuserpwd) {
390
        /* proxy auth was requested and there was proxy auth available,
391
           then deem this as "mere" proxy disconnect */
392
0
        ts->close_connection = TRUE;
393
0
        infof(data, "Proxy CONNECT connection closed");
394
0
      }
395
0
      else {
396
0
        error = SELECT_ERROR;
397
0
        failf(data, "Proxy CONNECT aborted");
398
0
      }
399
0
      ts->keepon = KEEPON_DONE;
400
0
      break;
401
0
    }
402
403
0
    if(ts->keepon == KEEPON_IGNORE) {
404
      /* This means we are currently ignoring a response-body */
405
406
0
      if(ts->cl) {
407
        /* A Content-Length based body: simply count down the counter
408
           and make sure to break out of the loop when we are done! */
409
0
        ts->cl--;
410
0
        if(ts->cl <= 0) {
411
0
          ts->keepon = KEEPON_DONE;
412
0
          break;
413
0
        }
414
0
      }
415
0
      else if(ts->chunked_encoding) {
416
        /* chunked-encoded body, so we need to do the chunked dance
417
           properly to know when the end of the body is reached */
418
0
        size_t consumed = 0;
419
420
        /* now parse the chunked piece of data so that we can
421
           properly tell when the stream ends */
422
0
        result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
423
0
        if(result)
424
0
          return result;
425
0
        if(Curl_httpchunk_is_done(data, &ts->ch)) {
426
          /* we are done reading chunks! */
427
0
          infof(data, "chunk reading DONE");
428
0
          ts->keepon = KEEPON_DONE;
429
0
        }
430
0
      }
431
0
      continue;
432
0
    }
433
434
0
    if(curlx_dyn_addn(&ts->rcvbuf, &byte, 1)) {
435
0
      failf(data, "CONNECT response too large");
436
0
      return CURLE_RECV_ERROR;
437
0
    }
438
439
    /* if this is not the end of a header line then continue */
440
0
    if(byte != 0x0a)
441
0
      continue;
442
443
0
    ts->headerlines++;
444
0
    linep = curlx_dyn_ptr(&ts->rcvbuf);
445
0
    line_len = curlx_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
446
447
    /* output debug if that is requested */
448
0
    Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
449
450
    /* send the header to the callback */
451
0
    writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
452
0
      (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
453
0
    result = Curl_client_write(data, writetype, linep, line_len);
454
0
    if(result)
455
0
      return result;
456
457
0
    result = Curl_bump_headersize(data, line_len, TRUE);
458
0
    if(result)
459
0
      return result;
460
461
    /* Newlines are CRLF, so the CR is ignored as the line is not
462
       really terminated until the LF comes. Treat a following CR
463
       as end-of-headers as well.*/
464
465
0
    if(('\r' == linep[0]) ||
466
0
       ('\n' == linep[0])) {
467
      /* end of response-headers from the proxy */
468
469
0
      if((407 == k->httpcode) && !data->state.authproblem) {
470
        /* If we get a 407 response code with content length
471
           when we have no auth problem, we must ignore the
472
           whole response-body */
473
0
        ts->keepon = KEEPON_IGNORE;
474
475
0
        if(ts->cl) {
476
0
          infof(data, "Ignore %" FMT_OFF_T " bytes of response-body", ts->cl);
477
0
        }
478
0
        else if(ts->chunked_encoding) {
479
0
          infof(data, "Ignore chunked response-body");
480
0
        }
481
0
        else {
482
          /* without content-length or chunked encoding, we
483
             cannot keep the connection alive since the close is
484
             the end signal so we bail out at once instead */
485
0
          CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
486
0
          ts->keepon = KEEPON_DONE;
487
0
        }
488
0
      }
489
0
      else {
490
0
        ts->keepon = KEEPON_DONE;
491
0
      }
492
493
0
      DEBUGASSERT(ts->keepon == KEEPON_IGNORE ||
494
0
                  ts->keepon == KEEPON_DONE);
495
0
      continue;
496
0
    }
497
498
0
    result = on_resp_header(cf, data, ts, linep);
499
0
    if(result)
500
0
      return result;
501
502
0
    curlx_dyn_reset(&ts->rcvbuf);
503
0
  } /* while there is buffer left and loop is requested */
504
505
0
  if(error)
506
0
    result = CURLE_RECV_ERROR;
507
0
  *done = (ts->keepon == KEEPON_DONE);
508
0
  if(!result && *done && data->info.httpproxycode / 100 != 2) {
509
    /* Deal with the possibly already received authenticate
510
       headers. 'newurl' is set to a new URL if we must loop. */
511
0
    result = Curl_http_auth_act(data);
512
0
  }
513
0
  return result;
514
0
}
515
516
static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
517
                           struct Curl_easy *data,
518
                           struct h1_tunnel_state *ts)
519
0
{
520
0
  struct connectdata *conn = cf->conn;
521
0
  CURLcode result;
522
0
  bool done;
523
524
0
  if(tunnel_is_established(ts))
525
0
    return CURLE_OK;
526
0
  if(tunnel_is_failed(ts))
527
0
    return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
528
529
0
  do {
530
531
0
    if(Curl_timeleft_ms(data, NULL, TRUE) < 0) {
532
0
      failf(data, "Proxy CONNECT aborted due to timeout");
533
0
      result = CURLE_OPERATION_TIMEDOUT;
534
0
      goto out;
535
0
    }
536
537
0
    switch(ts->tunnel_state) {
538
0
    case H1_TUNNEL_INIT:
539
      /* Prepare the CONNECT request and make a first attempt to send. */
540
0
      CURL_TRC_CF(data, cf, "CONNECT start");
541
0
      result = start_CONNECT(cf, data, ts);
542
0
      if(result)
543
0
        goto out;
544
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
545
0
      FALLTHROUGH();
546
547
0
    case H1_TUNNEL_CONNECT:
548
      /* see that the request is completely sent */
549
0
      CURL_TRC_CF(data, cf, "CONNECT send");
550
0
      result = send_CONNECT(cf, data, ts, &done);
551
0
      if(result || !done)
552
0
        goto out;
553
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
554
0
      FALLTHROUGH();
555
556
0
    case H1_TUNNEL_RECEIVE:
557
      /* read what is there */
558
0
      CURL_TRC_CF(data, cf, "CONNECT receive");
559
0
      result = recv_CONNECT_resp(cf, data, ts, &done);
560
0
      if(!result)
561
0
        result = Curl_pgrsUpdate(data);
562
      /* error or not complete yet. return for more multi-multi */
563
0
      if(result || !done)
564
0
        goto out;
565
      /* got it */
566
0
      h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
567
0
      FALLTHROUGH();
568
569
0
    case H1_TUNNEL_RESPONSE:
570
0
      CURL_TRC_CF(data, cf, "CONNECT response");
571
0
      if(data->req.newurl) {
572
        /* not the "final" response, we need to do a follow up request.
573
         * If the other side indicated a connection close, or if someone
574
         * else told us to close this connection, do so now.
575
         */
576
0
        Curl_req_soft_reset(&data->req, data);
577
0
        if(ts->close_connection || conn->bits.close) {
578
          /* Close this filter and the sub-chain, re-connect the
579
           * sub-chain and continue. Closing this filter will
580
           * reset our tunnel state. To avoid recursion, we return
581
           * and expect to be called again.
582
           */
583
0
          CURL_TRC_CF(data, cf, "CONNECT need to close+open");
584
0
          infof(data, "Connect me again please");
585
0
          Curl_conn_cf_close(cf, data);
586
0
          result = Curl_conn_cf_connect(cf->next, data, &done);
587
0
          goto out;
588
0
        }
589
0
        else {
590
          /* staying on this connection, reset state */
591
0
          h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
592
0
        }
593
0
      }
594
0
      break;
595
596
0
    default:
597
0
      break;
598
0
    }
599
600
0
  } while(data->req.newurl);
601
602
0
  DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
603
0
  if(data->info.httpproxycode / 100 != 2) {
604
    /* a non-2xx response and we have no next URL to try. */
605
0
    Curl_safefree(data->req.newurl);
606
0
    h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
607
0
    failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
608
0
    return CURLE_RECV_ERROR;
609
0
  }
610
  /* 2xx response, SUCCESS! */
611
0
  h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
612
0
  infof(data, "CONNECT tunnel established, response %d",
613
0
        data->info.httpproxycode);
614
0
  result = CURLE_OK;
615
616
0
out:
617
0
  if(result)
618
0
    h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
619
0
  return result;
620
0
}
621
622
static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
623
                                    struct Curl_easy *data,
624
                                    bool *done)
625
0
{
626
0
  CURLcode result;
627
0
  struct h1_tunnel_state *ts = cf->ctx;
628
629
0
  if(cf->connected) {
630
0
    *done = TRUE;
631
0
    return CURLE_OK;
632
0
  }
633
634
0
  CURL_TRC_CF(data, cf, "connect");
635
0
  result = cf->next->cft->do_connect(cf->next, data, done);
636
0
  if(result || !*done)
637
0
    return result;
638
639
0
  *done = FALSE;
640
0
  if(!ts) {
641
0
    result = tunnel_init(cf, data, &ts);
642
0
    if(result)
643
0
      return result;
644
0
    cf->ctx = ts;
645
0
  }
646
647
  /* We want "seamless" operations through HTTP proxy tunnel */
648
649
0
  result = H1_CONNECT(cf, data, ts);
650
0
  if(result)
651
0
    goto out;
652
0
  Curl_safefree(data->state.aptr.proxyuserpwd);
653
654
0
out:
655
0
  *done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
656
0
  if(*done) {
657
0
    cf->connected = TRUE;
658
    /* The real request will follow the CONNECT, reset request partially */
659
0
    Curl_req_soft_reset(&data->req, data);
660
0
    Curl_client_reset(data);
661
0
    Curl_pgrsReset(data);
662
663
0
    tunnel_free(cf, data);
664
0
  }
665
0
  return result;
666
0
}
667
668
static CURLcode cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
669
                                           struct Curl_easy *data,
670
                                           struct easy_pollset *ps)
671
0
{
672
0
  struct h1_tunnel_state *ts = cf->ctx;
673
0
  CURLcode result = CURLE_OK;
674
675
0
  if(!cf->connected) {
676
    /* If we are not connected, but the filter "below" is
677
     * and not waiting on something, we are tunneling. */
678
0
    curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
679
0
    if(ts) {
680
      /* when we have sent a CONNECT to a proxy, we should rather either
681
         wait for the socket to become readable to be able to get the
682
         response headers or if we are still sending the request, wait
683
         for write. */
684
0
      if(tunnel_want_send(ts))
685
0
        result = Curl_pollset_set_out_only(data, ps, sock);
686
0
      else
687
0
        result = Curl_pollset_set_in_only(data, ps, sock);
688
0
    }
689
0
    else
690
0
      result = Curl_pollset_set_out_only(data, ps, sock);
691
0
  }
692
0
  return result;
693
0
}
694
695
static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
696
                                struct Curl_easy *data)
697
0
{
698
0
  CURL_TRC_CF(data, cf, "destroy");
699
0
  tunnel_free(cf, data);
700
0
}
701
702
static void cf_h1_proxy_close(struct Curl_cfilter *cf,
703
                              struct Curl_easy *data)
704
0
{
705
0
  CURL_TRC_CF(data, cf, "close");
706
0
  if(cf) {
707
0
    cf->connected = FALSE;
708
0
    if(cf->ctx) {
709
0
      h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
710
0
    }
711
0
    if(cf->next)
712
0
      cf->next->cft->do_close(cf->next, data);
713
0
  }
714
0
}
715
716
struct Curl_cftype Curl_cft_h1_proxy = {
717
  "H1-PROXY",
718
  CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
719
  0,
720
  cf_h1_proxy_destroy,
721
  cf_h1_proxy_connect,
722
  cf_h1_proxy_close,
723
  Curl_cf_def_shutdown,
724
  cf_h1_proxy_adjust_pollset,
725
  Curl_cf_def_data_pending,
726
  Curl_cf_def_send,
727
  Curl_cf_def_recv,
728
  Curl_cf_def_cntrl,
729
  Curl_cf_def_conn_is_alive,
730
  Curl_cf_def_conn_keep_alive,
731
  Curl_cf_http_proxy_query,
732
};
733
734
CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
735
                                       struct Curl_easy *data)
736
0
{
737
0
  struct Curl_cfilter *cf;
738
0
  CURLcode result;
739
740
0
  (void)data;
741
0
  result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
742
0
  if(!result)
743
0
    Curl_conn_cf_insert_after(cf_at, cf);
744
0
  return result;
745
0
}
746
747
#endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */