From 02dbfa21921aded6bba292b99dd224fe450e7254 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 4 Apr 2011 16:24:37 +0200 Subject: http-proxy: move proxy code to http_proxy.c The new http_proxy.* files now host HTTP proxy specific code (500+ lines moved out from http.c), and as a consequence there is a macro introduced for the Curl_proxyCONNECT() function so that code can use it without actually supporting proxy (or HTTP) in builds. --- lib/http_proxy.c | 511 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 511 insertions(+) create mode 100644 lib/http_proxy.c (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c new file mode 100644 index 000000000..7631ae53c --- /dev/null +++ b/lib/http_proxy.c @@ -0,0 +1,511 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * 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. + * + ***************************************************************************/ + +#include "setup.h" + +#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP) + +#include "urldata.h" +#include +#include "http_proxy.h" +#include "sendf.h" +#include "http.h" +#include "url.h" +#include "select.h" +#include "rawstr.h" +#include "progress.h" + +#define _MPRINTF_REPLACE /* use our functions only */ +#include + +#include "curlx.h" + +/* The last #include file should be: */ +#include "memdebug.h" + +/* + * 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. + * + * This badly needs to be rewritten. CONNECT should be sent and dealt with + * like any ordinary HTTP request, and not specially crafted like this. This + * function only remains here like this for now since the rewrite is a bit too + * much work to do at the moment. + * + * This function is BLOCKING which is nasty for all multi interface using apps. + */ + +CURLcode Curl_proxyCONNECT(struct connectdata *conn, + int sockindex, + const char *hostname, + unsigned short remote_port) +{ + int subversion=0; + struct SessionHandle *data=conn->data; + struct SingleRequest *k = &data->req; + CURLcode result; + long timeout = + data->set.timeout?data->set.timeout:PROXY_TIMEOUT; /* in milliseconds */ + curl_socket_t tunnelsocket = conn->sock[sockindex]; + curl_off_t cl=0; + bool closeConnection = FALSE; + bool chunked_encoding = FALSE; + long check; + +#define SELECT_OK 0 +#define SELECT_ERROR 1 +#define SELECT_TIMEOUT 2 + int error = SELECT_OK; + + conn->bits.proxy_connect_closed = FALSE; + + do { + if(!conn->bits.tunnel_connecting) { /* BEGIN CONNECT PHASE */ + char *host_port; + Curl_send_buffer *req_buffer; + + infof(data, "Establish HTTP proxy tunnel to %s:%hu\n", + hostname, remote_port); + + if(data->req.newurl) { + /* 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. */ + free(data->req.newurl); + data->req.newurl = NULL; + } + + /* 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) { + free(req_buffer); + return CURLE_OUT_OF_MEMORY; + } + + /* Setup the proxy-authorization header, if any */ + result = Curl_http_output_auth(conn, "CONNECT", host_port, TRUE); + + if(CURLE_OK == result) { + char *host=(char *)""; + const char *proxyconn=""; + const char *useragent=""; + const char *http = (conn->proxytype == CURLPROXY_HTTP_1_0) ? + "1.0" : "1.1"; + + if(!Curl_checkheaders(data, "Host:")) { + host = aprintf("Host: %s\r\n", host_port); + if(!host) { + free(req_buffer); + free(host_port); + return CURLE_OUT_OF_MEMORY; + } + } + if(!Curl_checkheaders(data, "Proxy-Connection:")) + proxyconn = "Proxy-Connection: Keep-Alive\r\n"; + + if(!Curl_checkheaders(data, "User-Agent:") && + data->set.str[STRING_USERAGENT]) + useragent = conn->allocptr.uagent; + + /* Send the connect request to the proxy */ + /* BLOCKING */ + result = + Curl_add_bufferf(req_buffer, + "CONNECT %s:%hu HTTP/%s\r\n" + "%s" /* Host: */ + "%s" /* Proxy-Authorization */ + "%s" /* User-Agent */ + "%s", /* Proxy-Connection */ + hostname, remote_port, http, + host, + conn->allocptr.proxyuserpwd? + conn->allocptr.proxyuserpwd:"", + useragent, + proxyconn); + + if(host && *host) + free(host); + + if(CURLE_OK == result) + result = Curl_add_custom_headers(conn, req_buffer); + + if(CURLE_OK == result) + /* CRLF terminate the request */ + result = Curl_add_bufferf(req_buffer, "\r\n"); + + if(CURLE_OK == result) { + /* Now send off the request */ + result = Curl_add_buffer_send(req_buffer, conn, + &data->info.request_size, 0, sockindex); + } + req_buffer = NULL; + if(result) + failf(data, "Failed sending CONNECT to proxy"); + } + free(host_port); + Curl_safefree(req_buffer); + if(result) + return result; + + conn->bits.tunnel_connecting = TRUE; + } /* END CONNECT PHASE */ + + /* now we've issued the CONNECT and we're waiting to hear back - + we try not to block here in multi-mode because that might be a LONG + wait if the proxy cannot connect-through to the remote host. */ + + /* if timeout is requested, find out how much remaining time we have */ + check = timeout - /* timeout time */ + Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */ + if(check <= 0) { + failf(data, "Proxy CONNECT aborted due to timeout"); + return CURLE_RECV_ERROR; + } + + /* if we're in multi-mode and we would block, return instead for a retry */ + if(Curl_if_multi == data->state.used_interface) { + if(0 == Curl_socket_ready(tunnelsocket, CURL_SOCKET_BAD, 0)) + /* return so we'll be called again polling-style */ + return CURLE_OK; + else { + DEBUGF(infof(data, + "Multi mode finished polling for response from " + "proxy CONNECT.")); + } + } + else { + DEBUGF(infof(data, "Easy mode waiting response from proxy CONNECT.")); + } + + /* at this point, either: + 1) we're in easy-mode and so it's okay to block waiting for a CONNECT + response + 2) we're in multi-mode and we didn't block - it's either an error or we + now have some data waiting. + In any case, the tunnel_connecting phase is over. */ + conn->bits.tunnel_connecting = FALSE; + + { /* BEGIN NEGOTIATION PHASE */ + size_t nread; /* total size read */ + int perline; /* count bytes per line */ + int keepon=TRUE; + ssize_t gotbytes; + char *ptr; + char *line_start; + + ptr=data->state.buffer; + line_start = ptr; + + nread=0; + perline=0; + keepon=TRUE; + + while((nreadnow); /* spent time */ + if(check <= 0) { + failf(data, "Proxy CONNECT aborted due to timeout"); + error = SELECT_TIMEOUT; /* already too little time */ + break; + } + + /* loop every second at least, less if the timeout is near */ + switch (Curl_socket_ready(tunnelsocket, CURL_SOCKET_BAD, + check<1000L?(int)check:1000)) { + case -1: /* select() error, stop reading */ + error = SELECT_ERROR; + failf(data, "Proxy CONNECT aborted due to select/poll error"); + break; + case 0: /* timeout */ + break; + default: + DEBUGASSERT(ptr+BUFSIZE-nread <= data->state.buffer+BUFSIZE+1); + result = Curl_read(conn, tunnelsocket, ptr, BUFSIZE-nread, + &gotbytes); + if(result==CURLE_AGAIN) + continue; /* go loop yourself */ + else if(result) + keepon = FALSE; + else if(gotbytes <= 0) { + keepon = FALSE; + 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; + } + else { + error = SELECT_ERROR; + failf(data, "Proxy CONNECT aborted"); + } + } + else { + /* + * We got a whole chunk of data, which can be anything from one + * byte to a set of lines and possibly just a piece of the last + * line. + */ + int i; + + nread += gotbytes; + + if(keepon > TRUE) { + /* This means we are currently ignoring a response-body */ + + nread = 0; /* make next read start over in the read buffer */ + ptr=data->state.buffer; + if(cl) { + /* A Content-Length based body: simply count down the counter + and make sure to break out of the loop when we're done! */ + cl -= gotbytes; + if(cl<=0) { + keepon = FALSE; + break; + } + } + else { + /* 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 */ + r = Curl_httpchunk_read(conn, ptr, gotbytes, &tookcareof); + if(r == CHUNKE_STOP) { + /* we're done reading chunks! */ + infof(data, "chunk reading DONE\n"); + keepon = FALSE; + } + else + infof(data, "Read %zd bytes of chunk, continue\n", + tookcareof); + } + } + else + for(i = 0; i < gotbytes; ptr++, i++) { + perline++; /* amount of bytes in this line so far */ + if(*ptr == 0x0a) { + char letter; + int writetype; + +#ifdef CURL_DOES_CONVERSIONS + /* 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; +#endif /* CURL_DOES_CONVERSIONS */ + + /* output debug if that is requested */ + if(data->set.verbose) + Curl_debug(data, CURLINFO_HEADER_IN, + line_start, (size_t)perline, conn); + + /* send the header to the callback */ + writetype = CLIENTWRITE_HEADER; + if(data->set.include_header) + writetype |= CLIENTWRITE_BODY; + + result = Curl_client_write(conn, writetype, line_start, + perline); + if(result) + return result; + + /* 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.*/ + + if(('\r' == line_start[0]) || + ('\n' == line_start[0])) { + /* end of response-headers from the proxy */ + nread = 0; /* make next read start over in the read + buffer */ + ptr=data->state.buffer; + 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 */ + keepon = 2; + + if(cl) { + + infof(data, "Ignore %" FORMAT_OFF_T + " bytes of response-body\n", cl); + /* remove the remaining chunk of what we already + read */ + cl -= (gotbytes - i); + + if(cl<=0) + /* if the whole thing was already read, we are done! + */ + keepon=FALSE; + } + else if(chunked_encoding) { + CHUNKcode r; + /* 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; + infof(data, "%zd bytes of chunk left\n", gotbytes-i); + + if(line_start[1] == '\n') { + /* this can only be a LF if the letter at index 0 + was a CR */ + line_start++; + i++; + } + + /* now parse the chunked piece of data so that we can + properly tell when the stream ends */ + r = Curl_httpchunk_read(conn, line_start+1, + gotbytes -i, &gotbytes); + if(r == CHUNKE_STOP) { + /* we're done reading chunks! */ + infof(data, "chunk reading DONE\n"); + keepon = FALSE; + } + else + infof(data, "Read %zd bytes of chunk, continue\n", + gotbytes); + } + else { + /* 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 */ + keepon=FALSE; + } + } + else + keepon = FALSE; + break; /* breaks out of for-loop, not switch() */ + } + + /* keep a backup of the position we are about to blank */ + letter = line_start[perline]; + line_start[perline]=0; /* zero terminate the buffer */ + if((checkprefix("WWW-Authenticate:", line_start) && + (401 == k->httpcode)) || + (checkprefix("Proxy-authenticate:", line_start) && + (407 == k->httpcode))) { + result = Curl_http_input_auth(conn, k->httpcode, + line_start); + if(result) + return result; + } + else if(checkprefix("Content-Length:", line_start)) { + cl = curlx_strtoofft(line_start + + strlen("Content-Length:"), NULL, 10); + } + else if(Curl_compareheader(line_start, + "Connection:", "close")) + closeConnection = TRUE; + else if(Curl_compareheader(line_start, + "Transfer-Encoding:", + "chunked")) { + infof(data, "CONNECT responded chunked\n"); + chunked_encoding = TRUE; + /* init our chunky engine */ + Curl_httpchunk_init(conn); + } + else if(Curl_compareheader(line_start, + "Proxy-Connection:", "close")) + closeConnection = TRUE; + else if(2 == sscanf(line_start, "HTTP/1.%d %d", + &subversion, + &k->httpcode)) { + /* store the HTTP code from the proxy */ + data->info.httpproxycode = k->httpcode; + } + /* put back the letter we blanked out before */ + line_start[perline]= letter; + + perline=0; /* line starts over here */ + line_start = ptr+1; /* this skips the zero byte we wrote */ + } + } + } + break; + } /* switch */ + if(Curl_pgrsUpdate(conn)) + return CURLE_ABORTED_BY_CALLBACK; + } /* while there's buffer left and loop is requested */ + + 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 + below + */ + closeConnection = TRUE; + } + + if(closeConnection && data->req.newurl) { + /* Connection closed by server. Don't use it anymore */ + sclose(conn->sock[sockindex]); + conn->sock[sockindex] = CURL_SOCKET_BAD; + break; + } + } /* END NEGOTIATION PHASE */ + } while(data->req.newurl); + + if(200 != data->req.httpcode) { + failf(data, "Received HTTP code %d from proxy after CONNECT", + data->req.httpcode); + + if(closeConnection && data->req.newurl) + conn->bits.proxy_connect_closed = TRUE; + + return CURLE_RECV_ERROR; + } + + /* 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; + + infof (data, "Proxy replied OK to CONNECT request\n"); + data->req.ignorebody = FALSE; /* put it (back) to non-ignore state */ + return CURLE_OK; +} +#endif /* CURL_DISABLE_PROXY */ -- cgit v1.2.1 From c828646f60b5bffb2bfcf924eba36da767bf08bf Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 20 Apr 2011 00:48:20 +0200 Subject: CURL_DOES_CONVERSIONS: cleanup Massively reduce #ifdefs all over (23 #ifdef lines less so far) Moved conversion-specific code to non-ascii.c --- lib/http_proxy.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 7631ae53c..99f3f0a70 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -33,6 +33,7 @@ #include "select.h" #include "rawstr.h" #include "progress.h" +#include "non-ascii.h" #define _MPRINTF_REPLACE /* use our functions only */ #include @@ -315,14 +316,12 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, char letter; int writetype; -#ifdef CURL_DOES_CONVERSIONS /* 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; -#endif /* CURL_DOES_CONVERSIONS */ /* output debug if that is requested */ if(data->set.verbose) -- cgit v1.2.1 From 17df5d8caa1009c5ca168fc9643dc9d77c219832 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 21 Apr 2011 14:47:07 -0700 Subject: Include unistd.h to declare close() --- lib/http_proxy.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 99f3f0a70..89cfb72c8 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -24,6 +24,9 @@ #if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP) +#ifdef HAVE_UNISTD_H +#include +#endif #include "urldata.h" #include #include "http_proxy.h" -- cgit v1.2.1 From b903186fa0189ff241d756d25d07fdfe9885ae49 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 20 Apr 2011 15:17:42 +0200 Subject: source cleanup: unify look, style and indent levels By the use of a the new lib/checksrc.pl script that checks that our basic source style rules are followed. --- lib/http_proxy.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 89cfb72c8..523f4aea8 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -164,8 +164,9 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, if(CURLE_OK == result) { /* Now send off the request */ - result = Curl_add_buffer_send(req_buffer, conn, - &data->info.request_size, 0, sockindex); + result = + Curl_add_buffer_send(req_buffer, conn, + &data->info.request_size, 0, sockindex); } req_buffer = NULL; if(result) -- cgit v1.2.1 From b5d170b55110bacc61a4aa8bf99df1afc303c5dc Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 16 May 2011 23:46:43 +0200 Subject: CLOSESOCKETFUNCTION: added Introduced the initial setup to allow closesocket callbacks by making sure sclose() is only ever called from one place in the libcurl source and still run all test cases fine. --- lib/http_proxy.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 523f4aea8..08ca1006f 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -37,6 +37,7 @@ #include "rawstr.h" #include "progress.h" #include "non-ascii.h" +#include "connect.h" #define _MPRINTF_REPLACE /* use our functions only */ #include @@ -482,7 +483,7 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, if(closeConnection && data->req.newurl) { /* Connection closed by server. Don't use it anymore */ - sclose(conn->sock[sockindex]); + Curl_closesocket(conn, conn->sock[sockindex]); conn->sock[sockindex] = CURL_SOCKET_BAD; break; } -- cgit v1.2.1 From 4f170ee8f9c1d067022300df2da331c30dcda9dd Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 4 Jun 2011 21:19:14 +0200 Subject: Curl_socket_ready: make timeout a 'long' It was mostly typecasted to int all over the code so switching to long instead all over should be a net gain. --- lib/http_proxy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 08ca1006f..9fa832e4d 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -244,7 +244,7 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, /* loop every second at least, less if the timeout is near */ switch (Curl_socket_ready(tunnelsocket, CURL_SOCKET_BAD, - check<1000L?(int)check:1000)) { + check<1000L?check:1000)) { case -1: /* select() error, stop reading */ error = SELECT_ERROR; failf(data, "Proxy CONNECT aborted due to select/poll error"); -- cgit v1.2.1 From f1586cb4775681810afd8e6626e7842d459f3b85 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Tue, 26 Jul 2011 17:23:27 +0200 Subject: stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h --- lib/http_proxy.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 9fa832e4d..f3b02c975 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -27,6 +27,7 @@ #ifdef HAVE_UNISTD_H #include #endif + #include "urldata.h" #include #include "http_proxy.h" -- cgit v1.2.1 From 583a9027895d65744f03ce28998adbe6b41c49a7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 17 Dec 2011 23:47:01 +0100 Subject: Curl_proxyCONNECT: use newlines in debug output --- lib/http_proxy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index f3b02c975..fbf23a9d8 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -202,11 +202,11 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, else { DEBUGF(infof(data, "Multi mode finished polling for response from " - "proxy CONNECT.")); + "proxy CONNECT\n")); } } else { - DEBUGF(infof(data, "Easy mode waiting response from proxy CONNECT.")); + DEBUGF(infof(data, "Easy mode waiting response from proxy CONNECT\n")); } /* at this point, either: -- cgit v1.2.1 From 6d62c5a6fcd17b3e8c65326612818b8a8b94d690 Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Sun, 15 Jan 2012 19:21:55 +0100 Subject: Curl_proxyCONNECT() trace known bug #39 --- lib/http_proxy.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index fbf23a9d8..b20034781 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2012, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -409,8 +409,15 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, keepon=FALSE; } } - else + else { keepon = FALSE; + if(200 == data->info.httpproxycode) { + if(gotbytes - (i+1)) + failf(data, "Proxy CONNECT followed by %zd bytes " + "of opaque data. Data ignored (known bug #39)", + gotbytes - (i+1)); + } + } break; /* breaks out of for-loop, not switch() */ } -- cgit v1.2.1 From 466150bc64de0caef08f22cc8a88f7922483a639 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 31 Jan 2012 22:52:10 +0100 Subject: CONNECT: send correct Host: with IPv6 numerical address When the target host was given as a IPv6 numerical address, it was not properly put within square brackets for the Host: header in the CONNECT request. The "normal" request did fine. Reported by: "zooloo" Bug: http://curl.haxx.se/bug/view.cgi?id=3482093 --- lib/http_proxy.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index b20034781..1c2aa59c8 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -116,6 +116,8 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, /* Setup the proxy-authorization header, if any */ result = Curl_http_output_auth(conn, "CONNECT", host_port, TRUE); + free(host_port); + if(CURLE_OK == result) { char *host=(char *)""; const char *proxyconn=""; @@ -124,10 +126,11 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, "1.0" : "1.1"; if(!Curl_checkheaders(data, "Host:")) { - host = aprintf("Host: %s\r\n", host_port); + host = aprintf("Host: %s%s%s:%hu\r\n", conn->bits.ipv6_ip?"[":"", + hostname, conn->bits.ipv6_ip?"]":"", + remote_port); if(!host) { free(req_buffer); - free(host_port); return CURLE_OUT_OF_MEMORY; } } @@ -174,7 +177,7 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, if(result) failf(data, "Failed sending CONNECT to proxy"); } - free(host_port); + Curl_safefree(req_buffer); if(result) return result; -- cgit v1.2.1 From 79b6f4a2d37a17795e61b5ae9579f11bed9ad3cc Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 24 Feb 2012 23:33:14 +0100 Subject: CONNECT: fix ipv6 address in the Request-Line Commit 466150bc64d fixed the Host: header with CONNECT, but I then forgot the preceeding request-line. Now this too uses [brackets] properly if a ipv6 numerical address was given. Bug: http://curl.haxx.se/bug/view.cgi?id=3493129 Reported by: "Blacat" --- lib/http_proxy.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 1c2aa59c8..b12d37011 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -124,11 +124,15 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, const char *useragent=""; const char *http = (conn->proxytype == CURLPROXY_HTTP_1_0) ? "1.0" : "1.1"; + char *hostheader= /* host:port with IPv6 support */ + aprintf("%s%s%s:%hu", conn->bits.ipv6_ip?"[":"", + hostname, conn->bits.ipv6_ip?"]":"", + remote_port); + if(!hostheader) + return CURLE_OUT_OF_MEMORY; if(!Curl_checkheaders(data, "Host:")) { - host = aprintf("Host: %s%s%s:%hu\r\n", conn->bits.ipv6_ip?"[":"", - hostname, conn->bits.ipv6_ip?"]":"", - remote_port); + host = aprintf("Host: %s\r\n", hostheader); if(!host) { free(req_buffer); return CURLE_OUT_OF_MEMORY; @@ -141,24 +145,24 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, data->set.str[STRING_USERAGENT]) useragent = conn->allocptr.uagent; - /* Send the connect request to the proxy */ - /* BLOCKING */ result = Curl_add_bufferf(req_buffer, - "CONNECT %s:%hu HTTP/%s\r\n" - "%s" /* Host: */ - "%s" /* Proxy-Authorization */ - "%s" /* User-Agent */ - "%s", /* Proxy-Connection */ - hostname, remote_port, http, - host, - conn->allocptr.proxyuserpwd? - conn->allocptr.proxyuserpwd:"", - useragent, - proxyconn); + "CONNECT %s HTTP/%s\r\n" + "%s" /* Host: */ + "%s" /* Proxy-Authorization */ + "%s" /* User-Agent */ + "%s", /* Proxy-Connection */ + hostheader, + http, + host, + conn->allocptr.proxyuserpwd? + conn->allocptr.proxyuserpwd:"", + useragent, + proxyconn); if(host && *host) free(host); + free(hostheader); if(CURLE_OK == result) result = Curl_add_custom_headers(conn, req_buffer); @@ -168,7 +172,8 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, result = Curl_add_bufferf(req_buffer, "\r\n"); if(CURLE_OK == result) { - /* Now send off the request */ + /* Send the connect request to the proxy */ + /* BLOCKING */ result = Curl_add_buffer_send(req_buffer, conn, &data->info.request_size, 0, sockindex); -- cgit v1.2.1 From 0c8e36c18afc18b2de4c345c70828cf545a8715b Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Fri, 16 Mar 2012 20:11:22 +0100 Subject: http_proxy.c: fix OOM handling --- lib/http_proxy.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index b12d37011..e2d3869fd 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -128,12 +128,15 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, aprintf("%s%s%s:%hu", conn->bits.ipv6_ip?"[":"", hostname, conn->bits.ipv6_ip?"]":"", remote_port); - if(!hostheader) + if(!hostheader) { + free(req_buffer); return CURLE_OUT_OF_MEMORY; + } if(!Curl_checkheaders(data, "Host:")) { host = aprintf("Host: %s\r\n", hostheader); if(!host) { + free(hostheader); free(req_buffer); return CURLE_OUT_OF_MEMORY; } -- cgit v1.2.1 From c83de6d07625b813e3bbc31f9a0827c3a0007355 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 21 Mar 2012 23:22:39 +0100 Subject: CONNECT: fix multi interface regression The refactoring of HTTP CONNECT handling in commit 41b0237834232 that made it protocol independent broke it for the multi interface. This fix now introduce a better state handling and moved some logic to the http_proxy.c source file. Reported by: Yang Tse Bug: http://curl.haxx.se/mail/lib-2012-03/0162.html --- lib/http_proxy.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index e2d3869fd..01502fd8c 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -48,6 +48,40 @@ /* The last #include file should be: */ #include "memdebug.h" +CURLcode Curl_proxy_connect(struct connectdata *conn) +{ + 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; + 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 + */ + prot_save = conn->data->state.proto.generic; + memset(&http_proxy, 0, sizeof(http_proxy)); + conn->data->state.proto.http = &http_proxy; + result = Curl_proxyCONNECT(conn, FIRSTSOCKET, + conn->host.name, conn->remote_port); + conn->data->state.proto.generic = prot_save; + if(CURLE_OK != result) + return result; +#else + return CURLE_NOT_BUILT_IN; +#endif + } + /* no HTTP tunnel proxy, just return */ + return CURLE_OK; +} + /* * Curl_proxyCONNECT() requires that we're connected to a HTTP proxy. This * function will issue the necessary commands to get a seamless tunnel through @@ -83,10 +117,14 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, #define SELECT_TIMEOUT 2 int error = SELECT_OK; + if(conn->tunnel_state[sockindex] == TUNNEL_COMPLETE) + return CURLE_OK; /* CONNECT is already completed */ + conn->bits.proxy_connect_closed = FALSE; do { - if(!conn->bits.tunnel_connecting) { /* BEGIN CONNECT PHASE */ + if(TUNNEL_INIT == conn->tunnel_state[sockindex]) { + /* BEGIN CONNECT PHASE */ char *host_port; Curl_send_buffer *req_buffer; @@ -190,7 +228,7 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, if(result) return result; - conn->bits.tunnel_connecting = TRUE; + conn->tunnel_state[sockindex] = TUNNEL_CONNECT; } /* END CONNECT PHASE */ /* now we've issued the CONNECT and we're waiting to hear back - @@ -226,7 +264,6 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, 2) we're in multi-mode and we didn't block - it's either an error or we now have some data waiting. In any case, the tunnel_connecting phase is over. */ - conn->bits.tunnel_connecting = FALSE; { /* BEGIN NEGOTIATION PHASE */ size_t nread; /* total size read */ @@ -516,9 +553,14 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, if(closeConnection && data->req.newurl) conn->bits.proxy_connect_closed = TRUE; + /* to back to init state */ + conn->tunnel_state[sockindex] = TUNNEL_INIT; + return CURLE_RECV_ERROR; } + conn->tunnel_state[sockindex] = TUNNEL_COMPLETE; + /* 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. */ -- cgit v1.2.1 From ed7bfeee7aac5ffb8edc074fc704ce888f12ad0f Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 5 Jul 2012 11:10:50 +0200 Subject: http-proxy: keep CONNECT connections alive (for NTLM) When doing CONNECT requests, libcurl must make sure the connection is alive as much as possible. NTLM requires it and it is generally good for other cases as well. NTLM over CONNECT requests has been broken since this regression I introduced in my CONNECT cleanup commits that started with 41b02378342, included since 7.25.0. Bug: http://curl.haxx.se/bug/view.cgi?id=3538625 Reported by: Marcel Raad --- lib/http_proxy.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 01502fd8c..15f011848 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -65,10 +65,14 @@ CURLcode Curl_proxy_connect(struct connectdata *conn) * 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 + * + * This function might be called several times in the multi interface case + * if the proxy's CONNTECT response is not instant. */ prot_save = conn->data->state.proto.generic; memset(&http_proxy, 0, sizeof(http_proxy)); conn->data->state.proto.http = &http_proxy; + conn->bits.close = FALSE; result = Curl_proxyCONNECT(conn, FIRSTSOCKET, conn->host.name, conn->remote_port); conn->data->state.proto.generic = prot_save; @@ -357,6 +361,8 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, /* we're done reading chunks! */ infof(data, "chunk reading DONE\n"); keepon = FALSE; + /* we did the full CONNECT treatment, go COMPLETE */ + conn->tunnel_state[sockindex] = TUNNEL_COMPLETE; } else infof(data, "Read %zd bytes of chunk, continue\n", @@ -445,6 +451,9 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, /* we're done reading chunks! */ infof(data, "chunk reading DONE\n"); keepon = FALSE; + /* we did the full CONNECT treatment, go to + COMPLETE */ + conn->tunnel_state[sockindex] = TUNNEL_COMPLETE; } else infof(data, "Read %zd bytes of chunk, continue\n", @@ -466,6 +475,8 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, gotbytes - (i+1)); } } + /* we did the full CONNECT treatment, go to COMPLETE */ + conn->tunnel_state[sockindex] = TUNNEL_COMPLETE; break; /* breaks out of for-loop, not switch() */ } @@ -544,6 +555,17 @@ CURLcode Curl_proxyCONNECT(struct connectdata *conn, break; } } /* END NEGOTIATION PHASE */ + + /* 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 */ + if(data->req.newurl && + (TUNNEL_COMPLETE == conn->tunnel_state[sockindex])) { + conn->tunnel_state[sockindex] = TUNNEL_INIT; + infof(data, "TUNNEL_STATE switched to: %d\n", + conn->tunnel_state[sockindex]); + } + } while(data->req.newurl); if(200 != data->req.httpcode) { -- cgit v1.2.1 From ee588fe088077785d9ad9263e03e1e525b074261 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 17 Nov 2012 00:59:42 +0100 Subject: mem-include-scan: verify memory #includes If we use memory functions (malloc, free, strdup etc) in C sources in libcurl and we fail to include curl_memory.h or memdebug.h we either fail to properly support user-provided memory callbacks or the memory leak system of the test suite fails. After Ajit's report of a failure in the first category in http_proxy.c, I spotted a few in the second category as well. These problems are now tested for by test 1132 which runs a perl program that scans for and attempts to check that we use the correct include files if a memory related function is used in the source code. Reported by: Ajit Dhumale Bug: http://curl.haxx.se/mail/lib-2012-11/0125.html --- lib/http_proxy.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/http_proxy.c') diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 15f011848..245e1c1f6 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -45,6 +45,7 @@ #include "curlx.h" +#include "curl_memory.h" /* The last #include file should be: */ #include "memdebug.h" -- cgit v1.2.1