2011-04-04 10:24:37 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2017-02-09 04:13:28 -05:00
|
|
|
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-04-04 10:24:37 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-04-04 10:24:37 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2017-06-18 06:53:54 -04:00
|
|
|
#include "http_proxy.h"
|
|
|
|
|
2011-04-04 10:24:37 -04:00
|
|
|
#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "sendf.h"
|
|
|
|
#include "http.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "non-ascii.h"
|
|
|
|
#include "connect.h"
|
2011-04-04 10:24:37 -04:00
|
|
|
#include "curlx.h"
|
2016-11-16 12:49:15 -05:00
|
|
|
#include "vtls/vtls.h"
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2012-11-16 18:59:42 -05:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
/*
|
|
|
|
* Perform SSL initialization for HTTPS proxy. Sets
|
|
|
|
* proxy_ssl_connected connection bit when complete. Can be
|
|
|
|
* called multiple times.
|
|
|
|
*/
|
|
|
|
static CURLcode https_proxy_connect(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
#ifdef USE_SSL
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
DEBUGASSERT(conn->http_proxy.proxytype == CURLPROXY_HTTPS);
|
|
|
|
if(!conn->bits.proxy_ssl_connected[sockindex]) {
|
|
|
|
/* perform SSL initialization for this socket */
|
|
|
|
result =
|
|
|
|
Curl_ssl_connect_nonblocking(conn, sockindex,
|
|
|
|
&conn->bits.proxy_ssl_connected[sockindex]);
|
|
|
|
if(result)
|
|
|
|
conn->bits.close = TRUE; /* a failed connection is marked for closure to
|
|
|
|
prevent (bad) re-use or similar */
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
#else
|
2016-11-21 05:01:25 -05:00
|
|
|
(void) conn;
|
|
|
|
(void) sockindex;
|
2016-11-16 12:49:15 -05:00
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_proxy_connect(struct connectdata *conn, int sockindex)
|
2012-03-21 18:22:39 -04:00
|
|
|
{
|
2016-11-16 12:49:15 -05:00
|
|
|
if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) {
|
|
|
|
const CURLcode result = https_proxy_connect(conn, sockindex);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
if(!conn->bits.proxy_ssl_connected[sockindex])
|
|
|
|
return result; /* wait for HTTPS proxy SSL initialization to complete */
|
|
|
|
}
|
|
|
|
|
2012-03-21 18:22:39 -04:00
|
|
|
if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
|
|
|
|
#ifndef CURL_DISABLE_PROXY
|
|
|
|
/* for [protocol] tunneled through HTTP proxy */
|
|
|
|
struct HTTP http_proxy;
|
|
|
|
void *prot_save;
|
2016-01-25 08:37:24 -05:00
|
|
|
const char *hostname;
|
|
|
|
int remote_port;
|
2012-03-21 18:22:39 -04:00
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
/* BLOCKING */
|
|
|
|
/* We want "seamless" operations through HTTP proxy tunnel */
|
|
|
|
|
|
|
|
/* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the
|
|
|
|
* member conn->proto.http; we want [protocol] through HTTP and we have
|
|
|
|
* to change the member temporarily for connecting to the HTTP
|
|
|
|
* proxy. After Curl_proxyCONNECT we have to set back the member to the
|
|
|
|
* original pointer
|
2012-07-05 05:10:50 -04:00
|
|
|
*
|
|
|
|
* This function might be called several times in the multi interface case
|
2017-02-18 07:56:56 -05:00
|
|
|
* if the proxy's CONNECT response is not instant.
|
2012-03-21 18:22:39 -04:00
|
|
|
*/
|
2013-08-05 04:32:08 -04:00
|
|
|
prot_save = conn->data->req.protop;
|
2012-03-21 18:22:39 -04:00
|
|
|
memset(&http_proxy, 0, sizeof(http_proxy));
|
2013-08-05 04:32:08 -04:00
|
|
|
conn->data->req.protop = &http_proxy;
|
2014-05-20 04:32:23 -04:00
|
|
|
connkeep(conn, "HTTP proxy CONNECT");
|
2017-02-18 07:56:56 -05:00
|
|
|
|
|
|
|
/* for the secondary socket (FTP), use the "connect to host"
|
|
|
|
* but ignore the "connect to port" (use the secondary port)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(conn->bits.conn_to_host)
|
2016-01-25 08:37:24 -05:00
|
|
|
hostname = conn->conn_to_host.name;
|
2017-02-18 07:56:56 -05:00
|
|
|
else if(sockindex == SECONDARYSOCKET)
|
|
|
|
hostname = conn->secondaryhostname;
|
2016-01-25 08:37:24 -05:00
|
|
|
else
|
|
|
|
hostname = conn->host.name;
|
2016-11-16 12:49:15 -05:00
|
|
|
|
|
|
|
if(sockindex == SECONDARYSOCKET)
|
|
|
|
remote_port = conn->secondary_port;
|
|
|
|
else if(conn->bits.conn_to_port)
|
2016-01-25 08:37:24 -05:00
|
|
|
remote_port = conn->conn_to_port;
|
|
|
|
else
|
|
|
|
remote_port = conn->remote_port;
|
2017-04-25 18:34:22 -04:00
|
|
|
result = Curl_proxyCONNECT(conn, sockindex, hostname, remote_port);
|
2013-08-05 04:32:08 -04:00
|
|
|
conn->data->req.protop = prot_save;
|
2012-03-21 18:22:39 -04:00
|
|
|
if(CURLE_OK != result)
|
|
|
|
return result;
|
2015-02-11 17:18:32 -05:00
|
|
|
Curl_safefree(conn->allocptr.proxyuserpwd);
|
2012-03-21 18:22:39 -04:00
|
|
|
#else
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* no HTTP tunnel proxy, just return */
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
bool Curl_connect_complete(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
return conn->connect_state &&
|
|
|
|
(conn->connect_state->tunnel_state == TUNNEL_COMPLETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Curl_connect_ongoing(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
return conn->connect_state &&
|
|
|
|
(conn->connect_state->tunnel_state != TUNNEL_COMPLETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode connect_init(struct connectdata *conn, bool reinit)
|
|
|
|
{
|
|
|
|
struct http_connect_state *s;
|
|
|
|
if(!reinit) {
|
|
|
|
DEBUGASSERT(!conn->connect_state);
|
|
|
|
s = calloc(1, sizeof(struct http_connect_state));
|
|
|
|
if(!s)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
infof(conn->data, "allocate connect buffer!\n");
|
|
|
|
conn->connect_state = s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUGASSERT(conn->connect_state);
|
|
|
|
s = conn->connect_state;
|
|
|
|
}
|
|
|
|
s->tunnel_state = TUNNEL_INIT;
|
|
|
|
s->keepon=TRUE;
|
|
|
|
s->line_start = s->connect_buffer;
|
|
|
|
s->ptr = s->line_start;
|
|
|
|
s->cl=0;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connect_done(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
struct http_connect_state *s = conn->connect_state;
|
|
|
|
s->tunnel_state = TUNNEL_COMPLETE;
|
|
|
|
infof(conn->data, "CONNECT phase completed!\n");
|
|
|
|
}
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2017-04-24 19:03:17 -04:00
|
|
|
static CURLcode CONNECT(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
const char *hostname,
|
|
|
|
int remote_port)
|
2011-04-04 10:24:37 -04:00
|
|
|
{
|
|
|
|
int subversion=0;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data=conn->data;
|
2011-04-04 10:24:37 -04:00
|
|
|
struct SingleRequest *k = &data->req;
|
|
|
|
CURLcode result;
|
|
|
|
curl_socket_t tunnelsocket = conn->sock[sockindex];
|
|
|
|
bool closeConnection = FALSE;
|
2016-11-18 04:07:08 -05:00
|
|
|
time_t check;
|
2017-06-07 17:02:26 -04:00
|
|
|
struct http_connect_state *s = conn->connect_state;
|
2011-04-04 10:24:37 -04:00
|
|
|
|
|
|
|
#define SELECT_OK 0
|
|
|
|
#define SELECT_ERROR 1
|
|
|
|
#define SELECT_TIMEOUT 2
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
if(Curl_connect_complete(conn))
|
2012-03-21 18:22:39 -04:00
|
|
|
return CURLE_OK; /* CONNECT is already completed */
|
|
|
|
|
2011-04-04 10:24:37 -04:00
|
|
|
conn->bits.proxy_connect_closed = FALSE;
|
|
|
|
|
|
|
|
do {
|
2017-06-07 17:02:26 -04:00
|
|
|
if(TUNNEL_INIT == s->tunnel_state) {
|
2012-03-21 18:22:39 -04:00
|
|
|
/* BEGIN CONNECT PHASE */
|
2011-04-04 10:24:37 -04:00
|
|
|
char *host_port;
|
|
|
|
Curl_send_buffer *req_buffer;
|
|
|
|
|
|
|
|
infof(data, "Establish HTTP proxy tunnel to %s:%hu\n",
|
|
|
|
hostname, remote_port);
|
|
|
|
|
|
|
|
/* This only happens if we've looped here due to authentication
|
|
|
|
reasons, and we don't really use the newly cloned URL here
|
|
|
|
then. Just free() it. */
|
2015-03-11 12:41:01 -04:00
|
|
|
free(data->req.newurl);
|
|
|
|
data->req.newurl = NULL;
|
2011-04-04 10:24:37 -04:00
|
|
|
|
|
|
|
/* initialize a dynamic send-buffer */
|
|
|
|
req_buffer = Curl_add_buffer_init();
|
|
|
|
|
|
|
|
if(!req_buffer)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
host_port = aprintf("%s:%hu", hostname, remote_port);
|
|
|
|
if(!host_port) {
|
2015-05-08 08:42:59 -04:00
|
|
|
Curl_add_buffer_free(req_buffer);
|
2011-04-04 10:24:37 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the proxy-authorization header, if any */
|
|
|
|
result = Curl_http_output_auth(conn, "CONNECT", host_port, TRUE);
|
|
|
|
|
2012-01-31 16:52:10 -05:00
|
|
|
free(host_port);
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result) {
|
2017-02-09 04:13:28 -05:00
|
|
|
char *host = NULL;
|
2016-08-16 02:36:04 -04:00
|
|
|
const char *proxyconn="";
|
2011-04-04 10:24:37 -04:00
|
|
|
const char *useragent="";
|
2016-11-16 12:49:15 -05:00
|
|
|
const char *http = (conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ?
|
2011-04-04 10:24:37 -04:00
|
|
|
"1.0" : "1.1";
|
2016-01-25 08:37:24 -05:00
|
|
|
bool ipv6_ip = conn->bits.ipv6_ip;
|
|
|
|
char *hostheader;
|
|
|
|
|
|
|
|
/* the hostname may be different */
|
|
|
|
if(hostname != conn->host.name)
|
|
|
|
ipv6_ip = (strchr(hostname, ':') != NULL);
|
|
|
|
hostheader= /* host:port with IPv6 support */
|
|
|
|
aprintf("%s%s%s:%hu", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
|
2012-02-24 17:33:14 -05:00
|
|
|
remote_port);
|
2012-03-16 15:11:22 -04:00
|
|
|
if(!hostheader) {
|
2015-05-08 08:42:59 -04:00
|
|
|
Curl_add_buffer_free(req_buffer);
|
2012-02-24 17:33:14 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2012-03-16 15:11:22 -04:00
|
|
|
}
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2014-01-31 02:10:07 -05:00
|
|
|
if(!Curl_checkProxyheaders(conn, "Host:")) {
|
2012-02-24 17:33:14 -05:00
|
|
|
host = aprintf("Host: %s\r\n", hostheader);
|
2011-04-04 10:24:37 -04:00
|
|
|
if(!host) {
|
2012-03-16 15:11:22 -04:00
|
|
|
free(hostheader);
|
2015-05-08 08:42:59 -04:00
|
|
|
Curl_add_buffer_free(req_buffer);
|
2011-04-04 10:24:37 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 02:36:04 -04:00
|
|
|
if(!Curl_checkProxyheaders(conn, "Proxy-Connection:"))
|
|
|
|
proxyconn = "Proxy-Connection: Keep-Alive\r\n";
|
|
|
|
|
2014-01-31 02:10:07 -05:00
|
|
|
if(!Curl_checkProxyheaders(conn, "User-Agent:") &&
|
2011-04-04 10:24:37 -04:00
|
|
|
data->set.str[STRING_USERAGENT])
|
|
|
|
useragent = conn->allocptr.uagent;
|
|
|
|
|
|
|
|
result =
|
|
|
|
Curl_add_bufferf(req_buffer,
|
2012-02-24 17:33:14 -05:00
|
|
|
"CONNECT %s HTTP/%s\r\n"
|
|
|
|
"%s" /* Host: */
|
|
|
|
"%s" /* Proxy-Authorization */
|
2016-08-16 02:36:04 -04:00
|
|
|
"%s" /* User-Agent */
|
|
|
|
"%s", /* Proxy-Connection */
|
2012-02-24 17:33:14 -05:00
|
|
|
hostheader,
|
|
|
|
http,
|
2017-02-09 04:13:28 -05:00
|
|
|
host?host:"",
|
2012-02-24 17:33:14 -05:00
|
|
|
conn->allocptr.proxyuserpwd?
|
|
|
|
conn->allocptr.proxyuserpwd:"",
|
2016-08-16 02:36:04 -04:00
|
|
|
useragent,
|
|
|
|
proxyconn);
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2017-02-08 13:14:36 -05:00
|
|
|
if(host)
|
2011-04-04 10:24:37 -04:00
|
|
|
free(host);
|
2012-02-24 17:33:14 -05:00
|
|
|
free(hostheader);
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result)
|
2014-01-31 02:10:07 -05:00
|
|
|
result = Curl_add_custom_headers(conn, TRUE, req_buffer);
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result)
|
2011-04-04 10:24:37 -04:00
|
|
|
/* CRLF terminate the request */
|
|
|
|
result = Curl_add_bufferf(req_buffer, "\r\n");
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result) {
|
2012-02-24 17:33:14 -05:00
|
|
|
/* Send the connect request to the proxy */
|
|
|
|
/* BLOCKING */
|
2011-04-20 09:17:42 -04:00
|
|
|
result =
|
|
|
|
Curl_add_buffer_send(req_buffer, conn,
|
|
|
|
&data->info.request_size, 0, sockindex);
|
2011-04-04 10:24:37 -04:00
|
|
|
}
|
|
|
|
req_buffer = NULL;
|
|
|
|
if(result)
|
|
|
|
failf(data, "Failed sending CONNECT to proxy");
|
|
|
|
}
|
2012-01-31 16:52:10 -05:00
|
|
|
|
2015-05-08 08:42:59 -04:00
|
|
|
Curl_add_buffer_free(req_buffer);
|
2011-04-04 10:24:37 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
s->tunnel_state = TUNNEL_CONNECT;
|
2017-06-16 11:20:43 -04:00
|
|
|
s->perline = 0;
|
2014-07-19 17:58:58 -04:00
|
|
|
} /* END CONNECT PHASE */
|
|
|
|
|
|
|
|
check = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
if(check <= 0) {
|
|
|
|
failf(data, "Proxy CONNECT aborted due to timeout");
|
2017-06-07 17:02:26 -04:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
2014-07-19 17:58:58 -04:00
|
|
|
}
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2017-04-25 18:34:22 -04:00
|
|
|
if(!Curl_conn_data_pending(conn, sockindex))
|
|
|
|
/* return so we'll be called again polling-style */
|
|
|
|
return CURLE_OK;
|
2013-02-05 17:01:50 -05:00
|
|
|
|
2014-07-19 17:58:58 -04:00
|
|
|
/* at this point, the tunnel_connecting phase is over. */
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2014-07-19 17:58:58 -04:00
|
|
|
{ /* READING RESPONSE PHASE */
|
2017-06-07 17:02:26 -04:00
|
|
|
int error = SELECT_OK;
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
while(s->keepon && !error) {
|
|
|
|
ssize_t gotbytes;
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
/* make sure we have space to read more data */
|
|
|
|
if(s->ptr >= &s->connect_buffer[CONNECT_BUFFER_SIZE]) {
|
2016-12-08 16:32:36 -05:00
|
|
|
failf(data, "CONNECT response too large!");
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
2011-04-04 10:24:37 -04:00
|
|
|
|
2016-12-08 16:32:36 -05:00
|
|
|
/* Read one byte at a time to avoid a race condition. Wait at most one
|
|
|
|
second before looping to ensure continuous pgrsUpdates. */
|
2017-06-07 17:02:26 -04:00
|
|
|
result = Curl_read(conn, tunnelsocket, s->ptr, 1, &gotbytes);
|
|
|
|
if(result == CURLE_AGAIN)
|
|
|
|
/* socket buffer drained, return */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
if(Curl_pgrsUpdate(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
if(result) {
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = FALSE;
|
2011-04-04 10:24:37 -04:00
|
|
|
break;
|
2016-12-08 16:32:36 -05:00
|
|
|
}
|
|
|
|
else if(gotbytes <= 0) {
|
|
|
|
if(data->set.proxyauth && data->state.authproxy.avail) {
|
|
|
|
/* proxy auth was requested and there was proxy auth available,
|
|
|
|
then deem this as "mere" proxy disconnect */
|
|
|
|
conn->bits.proxy_connect_closed = TRUE;
|
|
|
|
infof(data, "Proxy CONNECT connection closed\n");
|
2016-11-29 18:09:13 -05:00
|
|
|
}
|
2016-12-08 16:32:36 -05:00
|
|
|
else {
|
|
|
|
error = SELECT_ERROR;
|
|
|
|
failf(data, "Proxy CONNECT aborted");
|
|
|
|
}
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = FALSE;
|
2016-12-08 16:32:36 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
if(s->keepon > TRUE) {
|
2016-12-08 16:32:36 -05:00
|
|
|
/* This means we are currently ignoring a response-body */
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
s->ptr = s->connect_buffer;
|
|
|
|
if(s->cl) {
|
2016-12-08 16:32:36 -05:00
|
|
|
/* A Content-Length based body: simply count down the counter
|
|
|
|
and make sure to break out of the loop when we're done! */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->cl--;
|
|
|
|
if(s->cl <= 0) {
|
|
|
|
s->keepon = FALSE;
|
|
|
|
s->tunnel_state = TUNNEL_COMPLETE;
|
2016-12-08 16:32:36 -05:00
|
|
|
break;
|
2011-04-04 10:24:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2016-12-08 16:32:36 -05:00
|
|
|
/* chunked-encoded body, so we need to do the chunked dance
|
|
|
|
properly to know when the end of the body is reached */
|
|
|
|
CHUNKcode r;
|
|
|
|
ssize_t tookcareof = 0;
|
|
|
|
|
|
|
|
/* now parse the chunked piece of data so that we can
|
|
|
|
properly tell when the stream ends */
|
2017-06-07 17:02:26 -04:00
|
|
|
r = Curl_httpchunk_read(conn, s->ptr, 1, &tookcareof);
|
2016-12-08 16:32:36 -05:00
|
|
|
if(r == CHUNKE_STOP) {
|
|
|
|
/* we're done reading chunks! */
|
|
|
|
infof(data, "chunk reading DONE\n");
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = FALSE;
|
2016-12-08 16:32:36 -05:00
|
|
|
/* we did the full CONNECT treatment, go COMPLETE */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->tunnel_state = TUNNEL_COMPLETE;
|
2016-12-08 16:32:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
s->perline++; /* amount of bytes in this line so far */
|
2016-12-08 16:32:36 -05:00
|
|
|
|
|
|
|
/* if this is not the end of a header line then continue */
|
2017-06-07 17:02:26 -04:00
|
|
|
if(*s->ptr != 0x0a) {
|
|
|
|
s->ptr++;
|
2016-12-08 16:32:36 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert from the network encoding */
|
|
|
|
result = Curl_convert_from_network(data, line_start, perline);
|
|
|
|
/* Curl_convert_from_network calls failf if unsuccessful */
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
/* output debug if that is requested */
|
|
|
|
if(data->set.verbose)
|
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN,
|
2017-06-07 17:02:26 -04:00
|
|
|
s->line_start, (size_t)s->perline, conn);
|
2016-12-08 16:32:36 -05:00
|
|
|
|
2016-04-28 05:33:25 -04:00
|
|
|
if(!data->set.suppress_connect_headers) {
|
|
|
|
/* send the header to the callback */
|
|
|
|
int writetype = CLIENTWRITE_HEADER;
|
|
|
|
if(data->set.include_header)
|
|
|
|
writetype |= CLIENTWRITE_BODY;
|
2016-12-08 16:32:36 -05:00
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
result = Curl_client_write(conn, writetype,
|
|
|
|
s->line_start, s->perline);
|
2016-04-28 05:33:25 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
2016-12-08 16:32:36 -05:00
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
data->info.header_size += (long)s->perline;
|
|
|
|
data->req.headerbytecount += (long)s->perline;
|
2016-12-08 16:32:36 -05:00
|
|
|
|
|
|
|
/* Newlines are CRLF, so the CR is ignored as the line isn't
|
|
|
|
really terminated until the LF comes. Treat a following CR
|
|
|
|
as end-of-headers as well.*/
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
if(('\r' == s->line_start[0]) ||
|
|
|
|
('\n' == s->line_start[0])) {
|
2016-12-08 16:32:36 -05:00
|
|
|
/* end of response-headers from the proxy */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->ptr = s->connect_buffer;
|
2016-12-08 16:32:36 -05:00
|
|
|
if((407 == k->httpcode) && !data->state.authproblem) {
|
|
|
|
/* If we get a 407 response code with content length
|
|
|
|
when we have no auth problem, we must ignore the
|
|
|
|
whole response-body */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = 2;
|
2016-12-08 16:32:36 -05:00
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
if(s->cl) {
|
2016-12-08 16:32:36 -05:00
|
|
|
infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
|
2017-06-07 17:02:26 -04:00
|
|
|
" bytes of response-body\n", s->cl);
|
2016-12-08 16:32:36 -05:00
|
|
|
}
|
2017-06-16 11:30:39 -04:00
|
|
|
else if(s->chunked_encoding) {
|
2016-12-08 16:32:36 -05:00
|
|
|
CHUNKcode r;
|
|
|
|
|
|
|
|
infof(data, "Ignore chunked response-body\n");
|
|
|
|
|
|
|
|
/* We set ignorebody true here since the chunked
|
|
|
|
decoder function will acknowledge that. Pay
|
|
|
|
attention so that this is cleared again when this
|
|
|
|
function returns! */
|
|
|
|
k->ignorebody = TRUE;
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
if(s->line_start[1] == '\n') {
|
2016-12-08 16:32:36 -05:00
|
|
|
/* this can only be a LF if the letter at index 0
|
|
|
|
was a CR */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->line_start++;
|
2011-04-04 10:24:37 -04:00
|
|
|
}
|
2016-12-08 16:32:36 -05:00
|
|
|
|
|
|
|
/* now parse the chunked piece of data so that we can
|
|
|
|
properly tell when the stream ends */
|
2017-06-07 17:02:26 -04:00
|
|
|
r = Curl_httpchunk_read(conn, s->line_start + 1, 1, &gotbytes);
|
2016-12-08 16:32:36 -05:00
|
|
|
if(r == CHUNKE_STOP) {
|
|
|
|
/* we're done reading chunks! */
|
|
|
|
infof(data, "chunk reading DONE\n");
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = FALSE;
|
|
|
|
/* we did the full CONNECT treatment, go to COMPLETE */
|
|
|
|
s->tunnel_state = TUNNEL_COMPLETE;
|
2011-04-04 10:24:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 18:31:23 -05:00
|
|
|
else {
|
2016-12-08 16:32:36 -05:00
|
|
|
/* without content-length or chunked encoding, we
|
|
|
|
can't keep the connection alive since the close is
|
|
|
|
the end signal so we bail out at once instead */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = FALSE;
|
2016-11-29 18:31:23 -05:00
|
|
|
}
|
2011-04-04 10:24:37 -04:00
|
|
|
}
|
2016-12-08 16:32:36 -05:00
|
|
|
else
|
2017-06-07 17:02:26 -04:00
|
|
|
s->keepon = FALSE;
|
|
|
|
if(!s->cl)
|
|
|
|
/* we did the full CONNECT treatment, go to COMPLETE */
|
|
|
|
s->tunnel_state = TUNNEL_COMPLETE;
|
2016-12-08 16:32:36 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
s->line_start[s->perline] = 0; /* zero terminate the buffer */
|
|
|
|
if((checkprefix("WWW-Authenticate:", s->line_start) &&
|
2016-12-08 16:32:36 -05:00
|
|
|
(401 == k->httpcode)) ||
|
2017-06-07 17:02:26 -04:00
|
|
|
(checkprefix("Proxy-authenticate:", s->line_start) &&
|
2016-12-08 16:32:36 -05:00
|
|
|
(407 == k->httpcode))) {
|
|
|
|
|
|
|
|
bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
|
2017-06-07 17:02:26 -04:00
|
|
|
char *auth = Curl_copy_header_value(s->line_start);
|
2016-12-08 16:32:36 -05:00
|
|
|
if(!auth)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
result = Curl_http_input_auth(conn, proxy, auth);
|
|
|
|
|
|
|
|
free(auth);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
2017-06-07 17:02:26 -04:00
|
|
|
else if(checkprefix("Content-Length:", s->line_start)) {
|
2016-12-08 16:32:36 -05:00
|
|
|
if(k->httpcode/100 == 2) {
|
2017-03-11 18:21:31 -05:00
|
|
|
/* A client MUST ignore any Content-Length or Transfer-Encoding
|
|
|
|
header fields received in a successful response to CONNECT.
|
|
|
|
"Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
|
|
|
|
infof(data, "Ignoring Content-Length in CONNECT %03d response\n",
|
2016-12-08 16:32:36 -05:00
|
|
|
k->httpcode);
|
|
|
|
}
|
2017-03-11 18:21:31 -05:00
|
|
|
else {
|
2017-08-14 17:33:23 -04:00
|
|
|
(void)curlx_strtoofft(s->line_start +
|
|
|
|
strlen("Content-Length:"), NULL, 10, &s->cl);
|
2017-03-11 18:21:31 -05:00
|
|
|
}
|
2016-12-08 16:32:36 -05:00
|
|
|
}
|
2017-06-07 17:02:26 -04:00
|
|
|
else if(Curl_compareheader(s->line_start, "Connection:", "close"))
|
2016-12-08 16:32:36 -05:00
|
|
|
closeConnection = TRUE;
|
2017-06-07 17:02:26 -04:00
|
|
|
else if(checkprefix("Transfer-Encoding:", s->line_start)) {
|
2016-12-08 16:32:36 -05:00
|
|
|
if(k->httpcode/100 == 2) {
|
2017-03-11 18:21:31 -05:00
|
|
|
/* A client MUST ignore any Content-Length or Transfer-Encoding
|
|
|
|
header fields received in a successful response to CONNECT.
|
|
|
|
"Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
|
|
|
|
infof(data, "Ignoring Transfer-Encoding in "
|
|
|
|
"CONNECT %03d response\n", k->httpcode);
|
|
|
|
}
|
2017-06-07 17:02:26 -04:00
|
|
|
else if(Curl_compareheader(s->line_start,
|
2017-03-11 18:21:31 -05:00
|
|
|
"Transfer-Encoding:", "chunked")) {
|
|
|
|
infof(data, "CONNECT responded chunked\n");
|
2017-06-16 11:30:39 -04:00
|
|
|
s->chunked_encoding = TRUE;
|
2017-03-11 18:21:31 -05:00
|
|
|
/* init our chunky engine */
|
|
|
|
Curl_httpchunk_init(conn);
|
2016-12-08 16:32:36 -05:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 17:02:26 -04:00
|
|
|
else if(Curl_compareheader(s->line_start,
|
|
|
|
"Proxy-Connection:", "close"))
|
2016-12-08 16:32:36 -05:00
|
|
|
closeConnection = TRUE;
|
2017-06-07 17:02:26 -04:00
|
|
|
else if(2 == sscanf(s->line_start, "HTTP/1.%d %d",
|
2016-12-08 16:32:36 -05:00
|
|
|
&subversion,
|
|
|
|
&k->httpcode)) {
|
|
|
|
/* store the HTTP code from the proxy */
|
|
|
|
data->info.httpproxycode = k->httpcode;
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
s->perline = 0; /* line starts over here */
|
|
|
|
s->ptr = s->connect_buffer;
|
|
|
|
s->line_start = s->ptr;
|
2011-04-04 10:24:37 -04:00
|
|
|
} /* while there's buffer left and loop is requested */
|
|
|
|
|
2016-12-08 16:32:36 -05:00
|
|
|
if(Curl_pgrsUpdate(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
|
2011-04-04 10:24:37 -04:00
|
|
|
if(error)
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
|
|
|
|
if(data->info.httpproxycode != 200) {
|
|
|
|
/* Deal with the possibly already received authenticate
|
|
|
|
headers. 'newurl' is set to a new URL if we must loop. */
|
|
|
|
result = Curl_http_auth_act(conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if(conn->bits.close)
|
|
|
|
/* the connection has been marked for closure, most likely in the
|
|
|
|
Curl_http_auth_act() function and thus we can kill it at once
|
2016-12-08 16:32:36 -05:00
|
|
|
below */
|
2011-04-04 10:24:37 -04:00
|
|
|
closeConnection = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(closeConnection && data->req.newurl) {
|
|
|
|
/* Connection closed by server. Don't use it anymore */
|
2011-05-16 17:46:43 -04:00
|
|
|
Curl_closesocket(conn, conn->sock[sockindex]);
|
2011-04-04 10:24:37 -04:00
|
|
|
conn->sock[sockindex] = CURL_SOCKET_BAD;
|
|
|
|
break;
|
|
|
|
}
|
2014-07-19 17:58:58 -04:00
|
|
|
} /* END READING RESPONSE PHASE */
|
2012-07-05 05:10:50 -04:00
|
|
|
|
|
|
|
/* If we are supposed to continue and request a new URL, which basically
|
|
|
|
* means the HTTP authentication is still going on so if the tunnel
|
|
|
|
* is complete we start over in INIT state */
|
2017-06-07 17:02:26 -04:00
|
|
|
if(data->req.newurl && (TUNNEL_COMPLETE == s->tunnel_state)) {
|
|
|
|
connect_init(conn, TRUE); /* reinit */
|
2012-07-05 05:10:50 -04:00
|
|
|
}
|
|
|
|
|
2011-04-04 10:24:37 -04:00
|
|
|
} while(data->req.newurl);
|
|
|
|
|
|
|
|
if(200 != data->req.httpcode) {
|
2014-07-19 17:58:58 -04:00
|
|
|
if(closeConnection && data->req.newurl) {
|
2011-04-04 10:24:37 -04:00
|
|
|
conn->bits.proxy_connect_closed = TRUE;
|
2014-07-19 17:58:58 -04:00
|
|
|
infof(data, "Connect me again please\n");
|
2017-06-07 17:02:26 -04:00
|
|
|
connect_done(conn);
|
2014-07-19 17:58:58 -04:00
|
|
|
}
|
2014-08-25 07:33:34 -04:00
|
|
|
else {
|
2015-03-11 12:41:01 -04:00
|
|
|
free(data->req.newurl);
|
|
|
|
data->req.newurl = NULL;
|
2014-08-25 07:33:34 -04:00
|
|
|
/* failure, close this connection to avoid re-use */
|
2016-08-11 08:00:23 -04:00
|
|
|
streamclose(conn, "proxy CONNECT failure");
|
2014-08-25 07:33:34 -04:00
|
|
|
Curl_closesocket(conn, conn->sock[sockindex]);
|
|
|
|
conn->sock[sockindex] = CURL_SOCKET_BAD;
|
2013-01-17 06:59:23 -05:00
|
|
|
}
|
|
|
|
|
2012-03-21 18:22:39 -04:00
|
|
|
/* to back to init state */
|
2017-06-07 17:02:26 -04:00
|
|
|
s->tunnel_state = TUNNEL_INIT;
|
2012-03-21 18:22:39 -04:00
|
|
|
|
2014-07-19 17:58:58 -04:00
|
|
|
if(conn->bits.proxy_connect_closed)
|
|
|
|
/* this is not an error, just part of the connection negotiation */
|
|
|
|
return CURLE_OK;
|
2017-03-10 08:28:37 -05:00
|
|
|
failf(data, "Received HTTP code %d from proxy after CONNECT",
|
|
|
|
data->req.httpcode);
|
|
|
|
return CURLE_RECV_ERROR;
|
2011-04-04 10:24:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
s->tunnel_state = TUNNEL_COMPLETE;
|
2012-03-21 18:22:39 -04:00
|
|
|
|
2011-04-04 10:24:37 -04:00
|
|
|
/* If a proxy-authorization header was used for the proxy, then we should
|
|
|
|
make sure that it isn't accidentally used for the document request
|
|
|
|
after we've connected. So let's free and clear it here. */
|
|
|
|
Curl_safefree(conn->allocptr.proxyuserpwd);
|
|
|
|
conn->allocptr.proxyuserpwd = NULL;
|
|
|
|
|
|
|
|
data->state.authproxy.done = TRUE;
|
|
|
|
|
2016-12-13 17:34:59 -05:00
|
|
|
infof(data, "Proxy replied OK to CONNECT request\n");
|
2011-04-04 10:24:37 -04:00
|
|
|
data->req.ignorebody = FALSE; /* put it (back) to non-ignore state */
|
2013-03-18 17:40:35 -04:00
|
|
|
conn->bits.rewindaftersend = FALSE; /* make sure this isn't set for the
|
|
|
|
document request */
|
2011-04-04 10:24:37 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2017-04-24 19:03:17 -04:00
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
void Curl_connect_free(struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = data->easy_conn;
|
|
|
|
struct http_connect_state *s = conn->connect_state;
|
|
|
|
if(s) {
|
|
|
|
free(s);
|
|
|
|
conn->connect_state = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-24 19:03:17 -04:00
|
|
|
/*
|
|
|
|
* Curl_proxyCONNECT() requires that we're connected to a HTTP proxy. This
|
|
|
|
* function will issue the necessary commands to get a seamless tunnel through
|
|
|
|
* this proxy. After that, the socket can be used just as a normal socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CURLcode Curl_proxyCONNECT(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
const char *hostname,
|
|
|
|
int remote_port)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
2017-06-07 17:02:26 -04:00
|
|
|
if(!conn->connect_state) {
|
|
|
|
result = connect_init(conn, FALSE);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2017-04-24 19:03:17 -04:00
|
|
|
}
|
|
|
|
result = CONNECT(conn, sockindex, hostname, remote_port);
|
|
|
|
|
2017-06-07 17:02:26 -04:00
|
|
|
if(result || Curl_connect_complete(conn))
|
|
|
|
connect_done(conn);
|
2017-04-24 19:03:17 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:57:31 -04:00
|
|
|
#else
|
|
|
|
void Curl_connect_free(struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
}
|
2017-04-24 19:03:17 -04:00
|
|
|
|
2011-04-04 10:24:37 -04:00
|
|
|
#endif /* CURL_DISABLE_PROXY */
|