1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include "http_proxy.h"
27 #if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
29 #include <curl/curl.h>
40 #include "vtls/vtls.h"
44 /* The last 3 #include files should be in this order */
45 #include "curl_printf.h"
46 #include "curl_memory.h"
50 * Perform SSL initialization for HTTPS proxy. Sets
51 * proxy_ssl_connected connection bit when complete. Can be
52 * called multiple times.
54 static CURLcode https_proxy_connect(struct Curl_easy *data, int sockindex)
57 struct connectdata *conn = data->conn;
58 CURLcode result = CURLE_OK;
59 DEBUGASSERT(conn->http_proxy.proxytype == CURLPROXY_HTTPS);
60 if(!conn->bits.proxy_ssl_connected[sockindex]) {
61 /* perform SSL initialization for this socket */
63 Curl_ssl_connect_nonblocking(data, conn, TRUE, sockindex,
64 &conn->bits.proxy_ssl_connected[sockindex]);
66 /* a failed connection is marked for closure to prevent (bad) re-use or
68 connclose(conn, "TLS handshake failed");
74 return CURLE_NOT_BUILT_IN;
78 CURLcode Curl_proxy_connect(struct Curl_easy *data, int sockindex)
80 struct connectdata *conn = data->conn;
81 if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) {
82 const CURLcode result = https_proxy_connect(data, sockindex);
85 if(!conn->bits.proxy_ssl_connected[sockindex])
86 return result; /* wait for HTTPS proxy SSL initialization to complete */
89 if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
90 #ifndef CURL_DISABLE_PROXY
91 /* for [protocol] tunneled through HTTP proxy */
96 /* We want "seamless" operations through HTTP proxy tunnel */
98 /* for the secondary socket (FTP), use the "connect to host"
99 * but ignore the "connect to port" (use the secondary port)
102 if(conn->bits.conn_to_host)
103 hostname = conn->conn_to_host.name;
104 else if(sockindex == SECONDARYSOCKET)
105 hostname = conn->secondaryhostname;
107 hostname = conn->host.name;
109 if(sockindex == SECONDARYSOCKET)
110 remote_port = conn->secondary_port;
111 else if(conn->bits.conn_to_port)
112 remote_port = conn->conn_to_port;
114 remote_port = conn->remote_port;
116 result = Curl_proxyCONNECT(data, sockindex, hostname, remote_port);
117 if(CURLE_OK != result)
119 Curl_safefree(data->state.aptr.proxyuserpwd);
121 return CURLE_NOT_BUILT_IN;
124 /* no HTTP tunnel proxy, just return */
128 bool Curl_connect_complete(struct connectdata *conn)
130 return !conn->connect_state ||
131 (conn->connect_state->tunnel_state >= TUNNEL_COMPLETE);
134 bool Curl_connect_ongoing(struct connectdata *conn)
136 return conn->connect_state &&
137 (conn->connect_state->tunnel_state <= TUNNEL_COMPLETE);
140 /* when we've sent a CONNECT to a proxy, we should rather either wait for the
141 socket to become readable to be able to get the response headers or if
142 we're still sending the request, wait for write. */
143 int Curl_connect_getsock(struct connectdata *conn)
147 DEBUGASSERT(conn->connect_state);
148 http = &conn->connect_state->http_proxy;
150 if(http->sending == HTTPSEND_REQUEST)
151 return GETSOCK_WRITESOCK(0);
153 return GETSOCK_READSOCK(0);
156 static CURLcode connect_init(struct Curl_easy *data, bool reinit)
158 struct http_connect_state *s;
159 struct connectdata *conn = data->conn;
160 if(conn->handler->flags & PROTOPT_NOTCPPROXY) {
161 failf(data, "%s cannot be done over CONNECT", conn->handler->scheme);
162 return CURLE_UNSUPPORTED_PROTOCOL;
166 DEBUGASSERT(!conn->connect_state);
167 /* we might need the upload buffer for streaming a partial request */
168 result = Curl_get_upload_buffer(data);
172 s = calloc(1, sizeof(struct http_connect_state));
174 return CURLE_OUT_OF_MEMORY;
175 infof(data, "allocate connect buffer");
176 conn->connect_state = s;
177 Curl_dyn_init(&s->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
179 /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the
180 * member conn->proto.http; we want [protocol] through HTTP and we have
181 * to change the member temporarily for connecting to the HTTP
182 * proxy. After Curl_proxyCONNECT we have to set back the member to the
185 * This function might be called several times in the multi interface case
186 * if the proxy's CONNECT response is not instant.
188 s->prot_save = data->req.p.http;
189 data->req.p.http = &s->http_proxy;
190 connkeep(conn, "HTTP proxy CONNECT");
193 DEBUGASSERT(conn->connect_state);
194 s = conn->connect_state;
195 Curl_dyn_reset(&s->rcvbuf);
197 s->tunnel_state = TUNNEL_INIT;
198 s->keepon = KEEPON_CONNECT;
200 s->close_connection = FALSE;
204 void Curl_connect_done(struct Curl_easy *data)
206 struct connectdata *conn = data->conn;
207 struct http_connect_state *s = conn->connect_state;
208 if(s && (s->tunnel_state != TUNNEL_EXIT)) {
209 s->tunnel_state = TUNNEL_EXIT;
210 Curl_dyn_free(&s->rcvbuf);
211 Curl_dyn_free(&s->req);
213 /* restore the protocol pointer, if not already done */
215 data->req.p.http = s->prot_save;
217 data->info.httpcode = 0; /* clear it as it might've been used for the
219 data->req.ignorebody = FALSE;
221 data->state.hconnect = FALSE;
223 infof(data, "CONNECT phase completed");
227 static CURLcode CONNECT_host(struct Curl_easy *data,
228 struct connectdata *conn,
229 const char *hostname,
234 char *hostheader; /* for CONNECT */
235 char *host = NULL; /* Host: */
236 bool ipv6_ip = conn->bits.ipv6_ip;
238 /* the hostname may be different */
239 if(hostname != conn->host.name)
240 ipv6_ip = (strchr(hostname, ':') != NULL);
241 hostheader = /* host:port with IPv6 support */
242 aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
245 return CURLE_OUT_OF_MEMORY;
247 if(!Curl_checkProxyheaders(data, conn, STRCONST("Host"))) {
248 host = aprintf("Host: %s\r\n", hostheader);
251 return CURLE_OUT_OF_MEMORY;
254 *connecthostp = hostheader;
260 static CURLcode CONNECT(struct Curl_easy *data,
262 const char *hostname,
266 struct SingleRequest *k = &data->req;
268 struct connectdata *conn = data->conn;
269 curl_socket_t tunnelsocket = conn->sock[sockindex];
270 struct http_connect_state *s = conn->connect_state;
271 struct HTTP *http = data->req.p.http;
276 #define SELECT_ERROR 1
278 if(Curl_connect_complete(conn))
279 return CURLE_OK; /* CONNECT is already completed */
281 conn->bits.proxy_connect_closed = FALSE;
285 if(TUNNEL_INIT == s->tunnel_state) {
286 /* BEGIN CONNECT PHASE */
287 struct dynbuf *req = &s->req;
288 char *hostheader = NULL;
291 infof(data, "Establish HTTP proxy tunnel to %s:%d",
292 hostname, remote_port);
294 /* This only happens if we've looped here due to authentication
295 reasons, and we don't really use the newly cloned URL here
296 then. Just free() it. */
297 Curl_safefree(data->req.newurl);
299 /* initialize send-buffer */
300 Curl_dyn_init(req, DYN_HTTP_REQUEST);
302 result = CONNECT_host(data, conn,
303 hostname, remote_port, &hostheader, &host);
307 /* Setup the proxy-authorization header, if any */
308 result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
313 (conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? "1.0" : "1.1";
317 "CONNECT %s HTTP/%s\r\n"
319 "%s", /* Proxy-Authorization */
323 data->state.aptr.proxyuserpwd?
324 data->state.aptr.proxyuserpwd:"");
326 if(!result && !Curl_checkProxyheaders(data,
327 conn, STRCONST("User-Agent")) &&
328 data->set.str[STRING_USERAGENT])
329 result = Curl_dyn_addf(req, "User-Agent: %s\r\n",
330 data->set.str[STRING_USERAGENT]);
332 if(!result && !Curl_checkProxyheaders(data, conn,
333 STRCONST("Proxy-Connection")))
334 result = Curl_dyn_addn(req,
335 STRCONST("Proxy-Connection: Keep-Alive\r\n"));
338 result = Curl_add_custom_headers(data, TRUE, req);
341 /* CRLF terminate the request */
342 result = Curl_dyn_addn(req, STRCONST("\r\n"));
345 /* Send the connect request to the proxy */
346 result = Curl_buffer_send(req, data, &data->info.request_size, 0,
351 failf(data, "Failed sending CONNECT to proxy");
358 s->tunnel_state = TUNNEL_CONNECT;
359 } /* END CONNECT PHASE */
361 check = Curl_timeleft(data, NULL, TRUE);
363 failf(data, "Proxy CONNECT aborted due to timeout");
364 return CURLE_OPERATION_TIMEDOUT;
367 if(!Curl_conn_data_pending(conn, sockindex) && !http->sending)
368 /* return so we'll be called again polling-style */
371 /* at this point, the tunnel_connecting phase is over. */
373 if(http->sending == HTTPSEND_REQUEST) {
376 k->upload_fromhere = data->state.ulbuf;
377 result = Curl_fillreadbuffer(data, data->set.upload_buffer_size,
381 s->nsend = fillcount;
384 ssize_t bytes_written;
385 /* write to socket (send away data) */
386 result = Curl_write(data,
387 conn->writesockfd, /* socket to send to */
388 k->upload_fromhere, /* buffer pointer */
389 s->nsend, /* buffer size */
390 &bytes_written); /* actually sent */
393 /* send to debug callback! */
394 result = Curl_debug(data, CURLINFO_HEADER_OUT,
395 k->upload_fromhere, bytes_written);
397 s->nsend -= bytes_written;
398 k->upload_fromhere += bytes_written;
401 http->sending = HTTPSEND_NADA;
402 /* if nothing left to send, continue */
404 { /* READING RESPONSE PHASE */
405 int error = SELECT_OK;
411 /* Read one byte at a time to avoid a race condition. Wait at most one
412 second before looping to ensure continuous pgrsUpdates. */
413 result = Curl_read(data, tunnelsocket, &byte, 1, &gotbytes);
414 if(result == CURLE_AGAIN)
415 /* socket buffer drained, return */
418 if(Curl_pgrsUpdate(data))
419 return CURLE_ABORTED_BY_CALLBACK;
422 s->keepon = KEEPON_DONE;
425 else if(gotbytes <= 0) {
426 if(data->set.proxyauth && data->state.authproxy.avail &&
427 data->state.aptr.proxyuserpwd) {
428 /* proxy auth was requested and there was proxy auth available,
429 then deem this as "mere" proxy disconnect */
430 conn->bits.proxy_connect_closed = TRUE;
431 infof(data, "Proxy CONNECT connection closed");
434 error = SELECT_ERROR;
435 failf(data, "Proxy CONNECT aborted");
437 s->keepon = KEEPON_DONE;
441 if(s->keepon == KEEPON_IGNORE) {
442 /* This means we are currently ignoring a response-body */
445 /* A Content-Length based body: simply count down the counter
446 and make sure to break out of the loop when we're done! */
449 s->keepon = KEEPON_DONE;
450 s->tunnel_state = TUNNEL_COMPLETE;
455 /* chunked-encoded body, so we need to do the chunked dance
456 properly to know when the end of the body is reached */
459 ssize_t tookcareof = 0;
461 /* now parse the chunked piece of data so that we can
462 properly tell when the stream ends */
463 r = Curl_httpchunk_read(data, &byte, 1, &tookcareof, &extra);
464 if(r == CHUNKE_STOP) {
465 /* we're done reading chunks! */
466 infof(data, "chunk reading DONE");
467 s->keepon = KEEPON_DONE;
468 /* we did the full CONNECT treatment, go COMPLETE */
469 s->tunnel_state = TUNNEL_COMPLETE;
475 if(Curl_dyn_addn(&s->rcvbuf, &byte, 1)) {
476 failf(data, "CONNECT response too large");
477 return CURLE_RECV_ERROR;
480 /* if this is not the end of a header line then continue */
485 linep = Curl_dyn_ptr(&s->rcvbuf);
486 perline = Curl_dyn_len(&s->rcvbuf); /* amount of bytes in this line */
488 /* output debug if that is requested */
489 Curl_debug(data, CURLINFO_HEADER_IN, linep, perline);
491 if(!data->set.suppress_connect_headers) {
492 /* send the header to the callback */
493 int writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
494 (data->set.include_header ? CLIENTWRITE_BODY : 0) |
495 (s->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
497 result = Curl_client_write(data, writetype, linep, perline);
502 data->info.header_size += (long)perline;
504 /* Newlines are CRLF, so the CR is ignored as the line isn't
505 really terminated until the LF comes. Treat a following CR
506 as end-of-headers as well.*/
508 if(('\r' == linep[0]) ||
509 ('\n' == linep[0])) {
510 /* end of response-headers from the proxy */
512 if((407 == k->httpcode) && !data->state.authproblem) {
513 /* If we get a 407 response code with content length
514 when we have no auth problem, we must ignore the
515 whole response-body */
516 s->keepon = KEEPON_IGNORE;
519 infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
520 " bytes of response-body", s->cl);
522 else if(s->chunked_encoding) {
526 infof(data, "Ignore chunked response-body");
528 /* We set ignorebody true here since the chunked decoder
529 function will acknowledge that. Pay attention so that this is
530 cleared again when this function returns! */
531 k->ignorebody = TRUE;
534 /* this can only be a LF if the letter at index 0 was a CR */
537 /* now parse the chunked piece of data so that we can properly
538 tell when the stream ends */
539 r = Curl_httpchunk_read(data, linep + 1, 1, &gotbytes,
541 if(r == CHUNKE_STOP) {
542 /* we're done reading chunks! */
543 infof(data, "chunk reading DONE");
544 s->keepon = KEEPON_DONE;
545 /* we did the full CONNECT treatment, go to COMPLETE */
546 s->tunnel_state = TUNNEL_COMPLETE;
550 /* without content-length or chunked encoding, we
551 can't keep the connection alive since the close is
552 the end signal so we bail out at once instead */
553 s->keepon = KEEPON_DONE;
557 s->keepon = KEEPON_DONE;
559 if(s->keepon == KEEPON_DONE && !s->cl)
560 /* we did the full CONNECT treatment, go to COMPLETE */
561 s->tunnel_state = TUNNEL_COMPLETE;
563 DEBUGASSERT(s->keepon == KEEPON_IGNORE || s->keepon == KEEPON_DONE);
567 if((checkprefix("WWW-Authenticate:", linep) &&
568 (401 == k->httpcode)) ||
569 (checkprefix("Proxy-authenticate:", linep) &&
570 (407 == k->httpcode))) {
572 bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
573 char *auth = Curl_copy_header_value(linep);
575 return CURLE_OUT_OF_MEMORY;
577 result = Curl_http_input_auth(data, proxy, auth);
584 else if(checkprefix("Content-Length:", linep)) {
585 if(k->httpcode/100 == 2) {
586 /* A client MUST ignore any Content-Length or Transfer-Encoding
587 header fields received in a successful response to CONNECT.
588 "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
589 infof(data, "Ignoring Content-Length in CONNECT %03d response",
593 (void)curlx_strtoofft(linep +
594 strlen("Content-Length:"), NULL, 10, &s->cl);
597 else if(Curl_compareheader(linep,
598 STRCONST("Connection:"), STRCONST("close")))
599 s->close_connection = TRUE;
600 else if(checkprefix("Transfer-Encoding:", linep)) {
601 if(k->httpcode/100 == 2) {
602 /* A client MUST ignore any Content-Length or Transfer-Encoding
603 header fields received in a successful response to CONNECT.
604 "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
605 infof(data, "Ignoring Transfer-Encoding in "
606 "CONNECT %03d response", k->httpcode);
608 else if(Curl_compareheader(linep,
609 STRCONST("Transfer-Encoding:"),
610 STRCONST("chunked"))) {
611 infof(data, "CONNECT responded chunked");
612 s->chunked_encoding = TRUE;
613 /* init our chunky engine */
614 Curl_httpchunk_init(data);
617 else if(Curl_compareheader(linep,
618 STRCONST("Proxy-Connection:"),
620 s->close_connection = TRUE;
621 else if(2 == sscanf(linep, "HTTP/1.%d %d",
624 /* store the HTTP code from the proxy */
625 data->info.httpproxycode = k->httpcode;
628 Curl_dyn_reset(&s->rcvbuf);
629 } /* while there's buffer left and loop is requested */
631 if(Curl_pgrsUpdate(data))
632 return CURLE_ABORTED_BY_CALLBACK;
635 return CURLE_RECV_ERROR;
637 if(data->info.httpproxycode/100 != 2) {
638 /* Deal with the possibly already received authenticate
639 headers. 'newurl' is set to a new URL if we must loop. */
640 result = Curl_http_auth_act(data);
645 /* the connection has been marked for closure, most likely in the
646 Curl_http_auth_act() function and thus we can kill it at once
648 s->close_connection = TRUE;
651 if(s->close_connection && data->req.newurl) {
652 /* Connection closed by server. Don't use it anymore */
653 Curl_closesocket(data, conn, conn->sock[sockindex]);
654 conn->sock[sockindex] = CURL_SOCKET_BAD;
657 } /* END READING RESPONSE PHASE */
659 /* If we are supposed to continue and request a new URL, which basically
660 * means the HTTP authentication is still going on so if the tunnel
661 * is complete we start over in INIT state */
662 if(data->req.newurl && (TUNNEL_COMPLETE == s->tunnel_state)) {
663 connect_init(data, TRUE); /* reinit */
666 } while(data->req.newurl);
668 if(data->info.httpproxycode/100 != 2) {
669 if(s->close_connection && data->req.newurl) {
670 conn->bits.proxy_connect_closed = TRUE;
671 infof(data, "Connect me again please");
672 Curl_connect_done(data);
675 free(data->req.newurl);
676 data->req.newurl = NULL;
677 /* failure, close this connection to avoid re-use */
678 streamclose(conn, "proxy CONNECT failure");
681 /* to back to init state */
682 s->tunnel_state = TUNNEL_INIT;
684 if(conn->bits.proxy_connect_closed)
685 /* this is not an error, just part of the connection negotiation */
687 Curl_dyn_free(&s->rcvbuf);
688 failf(data, "Received HTTP code %d from proxy after CONNECT",
690 return CURLE_RECV_ERROR;
693 s->tunnel_state = TUNNEL_COMPLETE;
695 /* If a proxy-authorization header was used for the proxy, then we should
696 make sure that it isn't accidentally used for the document request
697 after we've connected. So let's free and clear it here. */
698 Curl_safefree(data->state.aptr.proxyuserpwd);
699 data->state.aptr.proxyuserpwd = NULL;
701 data->state.authproxy.done = TRUE;
702 data->state.authproxy.multipass = FALSE;
704 infof(data, "Proxy replied %d to CONNECT request",
705 data->info.httpproxycode);
706 data->req.ignorebody = FALSE; /* put it (back) to non-ignore state */
707 conn->bits.rewindaftersend = FALSE; /* make sure this isn't set for the
709 Curl_dyn_free(&s->rcvbuf);
713 /* The Hyper version of CONNECT */
714 static CURLcode CONNECT(struct Curl_easy *data,
716 const char *hostname,
719 struct connectdata *conn = data->conn;
720 struct hyptransfer *h = &data->hyp;
721 curl_socket_t tunnelsocket = conn->sock[sockindex];
722 struct http_connect_state *s = conn->connect_state;
723 CURLcode result = CURLE_OUT_OF_MEMORY;
725 hyper_request *req = NULL;
726 hyper_headers *headers = NULL;
727 hyper_clientconn_options *options = NULL;
728 hyper_task *handshake = NULL;
729 hyper_task *task = NULL; /* for the handshake */
730 hyper_task *sendtask = NULL; /* for the send */
731 hyper_clientconn *client = NULL;
732 hyper_error *hypererr = NULL;
733 char *hostheader = NULL; /* for CONNECT */
734 char *host = NULL; /* Host: */
736 if(Curl_connect_complete(conn))
737 return CURLE_OK; /* CONNECT is already completed */
739 conn->bits.proxy_connect_closed = FALSE;
742 switch(s->tunnel_state) {
744 /* BEGIN CONNECT PHASE */
747 failf(data, "Couldn't create hyper IO");
748 result = CURLE_OUT_OF_MEMORY;
751 /* tell Hyper how to read/write network data */
752 hyper_io_set_userdata(io, data);
753 hyper_io_set_read(io, Curl_hyper_recv);
754 hyper_io_set_write(io, Curl_hyper_send);
755 conn->sockfd = tunnelsocket;
757 data->state.hconnect = TRUE;
759 /* create an executor to poll futures */
761 h->exec = hyper_executor_new();
763 failf(data, "Couldn't create hyper executor");
764 result = CURLE_OUT_OF_MEMORY;
769 options = hyper_clientconn_options_new();
770 hyper_clientconn_options_set_preserve_header_case(options, 1);
771 hyper_clientconn_options_set_preserve_header_order(options, 1);
774 failf(data, "Couldn't create hyper client options");
775 result = CURLE_OUT_OF_MEMORY;
779 hyper_clientconn_options_exec(options, h->exec);
781 /* "Both the `io` and the `options` are consumed in this function
783 handshake = hyper_clientconn_handshake(io, options);
785 failf(data, "Couldn't create hyper client handshake");
786 result = CURLE_OUT_OF_MEMORY;
792 if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
793 failf(data, "Couldn't hyper_executor_push the handshake");
794 result = CURLE_OUT_OF_MEMORY;
797 handshake = NULL; /* ownership passed on */
799 task = hyper_executor_poll(h->exec);
801 failf(data, "Couldn't hyper_executor_poll the handshake");
802 result = CURLE_OUT_OF_MEMORY;
806 client = hyper_task_value(task);
807 hyper_task_free(task);
808 req = hyper_request_new();
810 failf(data, "Couldn't hyper_request_new");
811 result = CURLE_OUT_OF_MEMORY;
814 if(hyper_request_set_method(req, (uint8_t *)"CONNECT",
815 strlen("CONNECT"))) {
816 failf(data, "error setting method");
817 result = CURLE_OUT_OF_MEMORY;
821 infof(data, "Establish HTTP proxy tunnel to %s:%d",
822 hostname, remote_port);
824 /* This only happens if we've looped here due to authentication
825 reasons, and we don't really use the newly cloned URL here
826 then. Just free() it. */
827 Curl_safefree(data->req.newurl);
829 result = CONNECT_host(data, conn, hostname, remote_port,
834 if(hyper_request_set_uri(req, (uint8_t *)hostheader,
835 strlen(hostheader))) {
836 failf(data, "error setting path");
837 result = CURLE_OUT_OF_MEMORY;
840 if(data->set.verbose) {
841 char *se = aprintf("CONNECT %s HTTP/1.1\r\n", hostheader);
843 result = CURLE_OUT_OF_MEMORY;
846 Curl_debug(data, CURLINFO_HEADER_OUT, se, strlen(se));
849 /* Setup the proxy-authorization header, if any */
850 result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
854 Curl_safefree(hostheader);
857 if((conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) &&
858 (HYPERE_OK != hyper_request_set_version(req,
859 HYPER_HTTP_VERSION_1_0))) {
860 failf(data, "error setting HTTP version");
861 result = CURLE_OUT_OF_MEMORY;
865 headers = hyper_request_headers(req);
867 failf(data, "hyper_request_headers");
868 result = CURLE_OUT_OF_MEMORY;
872 result = Curl_hyper_header(data, headers, host);
878 if(data->state.aptr.proxyuserpwd) {
879 result = Curl_hyper_header(data, headers,
880 data->state.aptr.proxyuserpwd);
885 if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent")) &&
886 data->set.str[STRING_USERAGENT]) {
888 Curl_dyn_init(&ua, DYN_HTTP_REQUEST);
889 result = Curl_dyn_addf(&ua, "User-Agent: %s\r\n",
890 data->set.str[STRING_USERAGENT]);
893 result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&ua));
899 if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
900 result = Curl_hyper_header(data, headers,
901 "Proxy-Connection: Keep-Alive");
906 result = Curl_add_custom_headers(data, TRUE, headers);
910 sendtask = hyper_clientconn_send(client, req);
912 failf(data, "hyper_clientconn_send");
913 result = CURLE_OUT_OF_MEMORY;
917 if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
918 failf(data, "Couldn't hyper_executor_push the send");
919 result = CURLE_OUT_OF_MEMORY;
923 hyper_clientconn_free(client);
926 task = hyper_executor_poll(h->exec);
928 bool error = hyper_task_type(task) == HYPER_TASK_ERROR;
930 hypererr = hyper_task_value(task);
931 hyper_task_free(task);
933 /* this could probably use a better error code? */
934 result = CURLE_OUT_OF_MEMORY;
939 s->tunnel_state = TUNNEL_CONNECT;
941 case TUNNEL_CONNECT: {
944 result = Curl_hyper_stream(data, conn, &didwhat, &done,
945 CURL_CSELECT_IN | CURL_CSELECT_OUT);
950 s->tunnel_state = TUNNEL_COMPLETE;
952 hyper_executor_free(h->exec);
956 hyper_waker_free(h->read_waker);
957 h->read_waker = NULL;
960 hyper_waker_free(h->write_waker);
961 h->write_waker = NULL;
970 if(conn->bits.close && data->req.newurl) {
971 /* Connection closed by server. Don't use it anymore */
972 Curl_closesocket(data, conn, conn->sock[sockindex]);
973 conn->sock[sockindex] = CURL_SOCKET_BAD;
977 /* If we are supposed to continue and request a new URL, which basically
978 * means the HTTP authentication is still going on so if the tunnel
979 * is complete we start over in INIT state */
980 if(data->req.newurl && (TUNNEL_COMPLETE == s->tunnel_state)) {
981 infof(data, "CONNECT request done, loop to make another");
982 connect_init(data, TRUE); /* reinit */
984 } while(data->req.newurl);
987 if(s->tunnel_state == TUNNEL_COMPLETE) {
988 if(data->info.httpproxycode/100 != 2) {
989 if(conn->bits.close && data->req.newurl) {
990 conn->bits.proxy_connect_closed = TRUE;
991 infof(data, "Connect me again please");
992 Curl_connect_done(data);
995 free(data->req.newurl);
996 data->req.newurl = NULL;
997 /* failure, close this connection to avoid re-use */
998 streamclose(conn, "proxy CONNECT failure");
999 Curl_closesocket(data, conn, conn->sock[sockindex]);
1000 conn->sock[sockindex] = CURL_SOCKET_BAD;
1003 /* to back to init state */
1004 s->tunnel_state = TUNNEL_INIT;
1006 if(!conn->bits.proxy_connect_closed) {
1007 failf(data, "Received HTTP code %d from proxy after CONNECT",
1008 data->req.httpcode);
1009 result = CURLE_RECV_ERROR;
1020 hyper_clientconn_options_free(options);
1023 hyper_task_free(handshake);
1026 uint8_t errbuf[256];
1027 size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
1028 failf(data, "Hyper: %.*s", (int)errlen, errbuf);
1029 hyper_error_free(hypererr);
1035 void Curl_connect_free(struct Curl_easy *data)
1037 struct connectdata *conn = data->conn;
1038 struct http_connect_state *s = conn->connect_state;
1041 conn->connect_state = NULL;
1046 * Curl_proxyCONNECT() requires that we're connected to a HTTP proxy. This
1047 * function will issue the necessary commands to get a seamless tunnel through
1048 * this proxy. After that, the socket can be used just as a normal socket.
1051 CURLcode Curl_proxyCONNECT(struct Curl_easy *data,
1053 const char *hostname,
1057 struct connectdata *conn = data->conn;
1058 if(!conn->connect_state) {
1059 result = connect_init(data, FALSE);
1063 result = CONNECT(data, sockindex, hostname, remote_port);
1065 if(result || Curl_connect_complete(conn))
1066 Curl_connect_done(data);
1072 void Curl_connect_free(struct Curl_easy *data)
1077 #endif /* CURL_DISABLE_PROXY */