Newer
Older
Daniel Stenberg
committed
if(!conn || conn->inuse)
/* Set higher score for the age passed since the connection was used */
score = Curl_tvdiff(now, conn->now);
if(score > highscore) {
highscore = score;
connindex = i;
}
}
if(connindex >= 0) {
/* Set the connection's owner correctly */
conn = data->state.connc->connects[connindex];
conn->data = data;
/* the winner gets the honour of being disconnected */
(void)Curl_disconnect(conn);
/* clean the array entry */
data->state.connc->connects[connindex] = NULL;
}
return connindex; /* return the available index or -1 */
}
/* this connection can now be marked 'idle' */
static void
ConnectionDone(struct connectdata *conn)
{
conn->inuse = FALSE;
}
/*
* The given input connection struct pointer is to be stored. If the "cache"
* is already full, we must clean out the most suitable using the previously
* set policy.
*
* The given connection should be unique. That must've been checked prior to
* this call.
*/
Daniel Stenberg
committed
ConnectionStore(struct SessionHandle *data,
struct connectdata *conn)
{
for(i=0; i< data->state.connc->num; i++) {
if(!data->state.connc->connects[i])
if(i == data->state.connc->num) {
/* there was no room available, kill one */
i = ConnectionKillOne(data);
Daniel Stenberg
committed
if(-1 != i)
Daniel Stenberg
committed
infof(data, "Connection (#%d) was killed to make room (holds %d)\n",
i, data->state.connc->num);
Daniel Stenberg
committed
else
infof(data, "This connection did not fit in the connection cache\n");
conn->connectindex = i; /* Make the child know where the pointer to this
particular data is stored. But note that this -1
if this is not within the cache and this is
probably not checked for everywhere (yet). */
conn->inuse = TRUE;
if(-1 != i) {
/* Only do this if a true index was returned, if -1 was returned there
is no room in the cache for an unknown reason and we cannot store
this there.
TODO: make sure we really can work with more handles than positions in
the cache, or possibly we should (allow to automatically) resize the
connection cache when we add more easy handles to a multi handle!
*/
data->state.connc->connects[i] = conn; /* fill in this */
conn->data = data;
/* after a TCP connection to the proxy has been verified, this function does
the next magic step.
Note: this function (and its sub-functions) calls failf()
*/
CURLcode Curl_connected_proxy(struct connectdata *conn)
{
CURLcode result = CURLE_OK;
struct SessionHandle *data = conn->data;
Daniel Stenberg
committed
if(conn->bits.tcpconnect)
/* allow this to get called again from the multi interface when TCP is
found connected in the state machine, even though it has already been
called if the connection happened "instantly" */
return CURLE_OK;
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
switch(data->set.proxytype) {
#ifndef CURL_DISABLE_PROXY
case CURLPROXY_SOCKS5:
case CURLPROXY_SOCKS5_HOSTNAME:
result = Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
conn->host.name, conn->remote_port,
FIRSTSOCKET, conn);
break;
case CURLPROXY_SOCKS4:
result = Curl_SOCKS4(conn->proxyuser, conn->host.name,
conn->remote_port, FIRSTSOCKET, conn, FALSE);
break;
case CURLPROXY_SOCKS4A:
result = Curl_SOCKS4(conn->proxyuser, conn->host.name,
conn->remote_port, FIRSTSOCKET, conn, TRUE);
break;
#endif /* CURL_DISABLE_PROXY */
case CURLPROXY_HTTP:
case CURLPROXY_HTTP_1_0:
/* do nothing here. handled later. */
break;
default:
Daniel Stenberg
committed
break;
} /* switch proxytype */
return result;
}
static CURLcode ConnectPlease(struct SessionHandle *data,
struct connectdata *conn,
bool *connected)
Daniel Stenberg
committed
Curl_addrinfo *addr;
Daniel Stenberg
committed
char *hostname = conn->bits.proxy?conn->proxy.name:conn->host.name;
Daniel Stenberg
committed
Daniel Stenberg
committed
infof(data, "About to connect() to %s%s port %d (#%d)\n",
Daniel Stenberg
committed
conn->bits.proxy?"proxy ":"",
Daniel Stenberg
committed
hostname, conn->port, conn->connectindex);
/*************************************************************
*************************************************************/
Daniel Stenberg
committed
conn->dns_entry,
Daniel Stenberg
committed
&conn->sock[FIRSTSOCKET],
&addr,
connected);
/* All is cool, we store the current information */
Daniel Stenberg
committed
conn->ip_addr = addr;
Daniel Stenberg
committed
if(*connected)
result = Curl_connected_proxy(conn);
}
Daniel Stenberg
committed
if(result)
Daniel Stenberg
committed
*connected = FALSE; /* mark it as not connected */
* verboseconnect() displays verbose information after a connect
static void verboseconnect(struct connectdata *conn)
Daniel Stenberg
committed
{
Daniel Stenberg
committed
infof(conn->data, "Connected to %s (%s) port %d (#%d)\n",
Daniel Stenberg
committed
conn->bits.proxy ? conn->proxy.dispname : conn->host.dispname,
Daniel Stenberg
committed
conn->ip_addr_str, conn->port, conn->connectindex);
Daniel Stenberg
committed
}
Daniel Stenberg
committed
Daniel Stenberg
committed
int Curl_protocol_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
{
Patrick Monnerat
committed
if(conn->handler->proto_getsock)
return conn->handler->proto_getsock(conn, socks, numsocks);
Daniel Stenberg
committed
return GETSOCK_BLANK;
}
Daniel Stenberg
committed
int Curl_doing_getsock(struct connectdata *conn,
curl_socket_t *socks,
int numsocks)
{
Daniel Stenberg
committed
if(conn && conn->handler->doing_getsock)
Patrick Monnerat
committed
return conn->handler->doing_getsock(conn, socks, numsocks);
Daniel Stenberg
committed
return GETSOCK_BLANK;
}
/*
* We are doing protocol-specific connecting and this is being called over and
* over from the multi interface until the connection phase is done on
* protocol layer.
*/
CURLcode Curl_protocol_connecting(struct connectdata *conn,
bool *done)
{
CURLcode result=CURLE_OK;
Daniel Stenberg
committed
if(conn && conn->handler->connecting) {
*done = FALSE;
Patrick Monnerat
committed
result = conn->handler->connecting(conn, done);
}
else
*done = TRUE;
return result;
}
/*
* We are DOING this is being called over and over from the multi interface
* until the DOING phase is done on protocol layer.
*/
CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
{
CURLcode result=CURLE_OK;
Daniel Stenberg
committed
if(conn && conn->handler->doing) {
*done = FALSE;
Patrick Monnerat
committed
result = conn->handler->doing(conn, done);
}
else
*done = TRUE;
return result;
}
/*
* We have discovered that the TCP connection has been successful, we can now
* proceed with some action.
*
*/
CURLcode Curl_protocol_connect(struct connectdata *conn,
bool *protocol_done)
{
CURLcode result=CURLE_OK;
struct SessionHandle *data = conn->data;
*protocol_done = FALSE;
if(conn->bits.tcpconnect && conn->bits.protoconnstart) {
/* We already are connected, get back. This may happen when the connect
worked fine in the first call, like when we connect to a local server
or proxy. Note that we don't know if the protocol is actually done.
Unless this protocol doesn't have any protocol-connect callback, as
then we know we're done. */
Patrick Monnerat
committed
if(!conn->handler->connecting)
*protocol_done = TRUE;
return CURLE_OK;
}
if(!conn->bits.tcpconnect) {
Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */
if(data->set.verbose)
verboseconnect(conn);
}
if(!conn->bits.protoconnstart) {
Patrick Monnerat
committed
if(conn->handler->connect_it) {
/* is there a protocol-specific connect() procedure? */
/* Set start time here for timeout purposes in the connect procedure, it
is later set again for the progress meter purpose */
conn->now = Curl_tvnow();
/* Call the protocol-specific connect function */
Patrick Monnerat
committed
result = conn->handler->connect_it(conn, protocol_done);
}
else
*protocol_done = TRUE;
/* it has started, possibly even completed but that knowledge isn't stored
in this bit! */
Daniel Stenberg
committed
if(!result)
Daniel Stenberg
committed
conn->bits.protoconnstart = TRUE;
}
return result; /* pass back status */
}
/*
* Helpers for IDNA convertions.
*/
#ifdef USE_LIBIDN
static bool is_ASCII_name(const char *hostname)
{
const unsigned char *ch = (const unsigned char*)hostname;
Daniel Stenberg
committed
while(*ch) {
if(*ch++ & 0x80)
return FALSE;
}
return TRUE;
}
/*
* Check if characters in hostname is allowed in Top Level Domain.
*/
static bool tld_check_name(struct SessionHandle *data,
const char *ace_hostname)
const char *tld_errmsg = "<no msg>";
/* Convert (and downcase) ACE-name back into locale's character set */
rc = idna_to_unicode_lzlz(ace_hostname, &uc_name, 0);
Daniel Stenberg
committed
if(rc != IDNA_SUCCESS)
#ifdef HAVE_TLD_STRERROR
Daniel Stenberg
committed
if(rc != TLD_SUCCESS)
tld_errmsg = tld_strerror((Tld_rc)rc);
#endif
Daniel Stenberg
committed
if(rc == TLD_INVALID)
infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
tld_errmsg, err_pos, uc_name[err_pos],
uc_name[err_pos] & 255);
Daniel Stenberg
committed
else if(rc != TLD_SUCCESS)
infof(data, "WARNING: TLD check for %s failed; %s\n",
uc_name, tld_errmsg);
#endif /* CURL_DISABLE_VERBOSE_STRINGS */
Daniel Stenberg
committed
if(uc_name)
if(rc != TLD_SUCCESS)
return FALSE;
return TRUE;
#endif
/*
* Perform any necessary IDN conversion of hostname
*/
static void fix_hostname(struct SessionHandle *data,
struct connectdata *conn, struct hostname *host)
{
#ifndef USE_LIBIDN
(void)data;
(void)conn;
#elif defined(CURL_DISABLE_VERBOSE_STRINGS)
(void)conn;
#endif
/* set the name we use to display the host name */
#ifdef USE_LIBIDN
/*************************************************************
* Check name for non-ASCII and convert hostname to ACE form.
*************************************************************/
Daniel Stenberg
committed
if(!is_ASCII_name(host->name) &&
Daniel Stenberg
committed
stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
char *ace_hostname = NULL;
int rc = idna_to_ascii_lz(host->name, &ace_hostname, 0);
infof (data, "Input domain encoded as `%s'\n",
stringprep_locale_charset ());
Daniel Stenberg
committed
if(rc != IDNA_SUCCESS)
infof(data, "Failed to convert %s to ACE; %s\n",
host->name, Curl_idn_strerror(conn,rc));
else {
/* tld_check_name() displays a warning if the host name contains
"illegal" characters for this TLD */
(void)tld_check_name(data, ace_hostname);
host->encalloc = ace_hostname;
/* change the name pointer to point to the encoded hostname */
host->name = host->encalloc;
}
}
}
/*
* Allocate and initialize a new connectdata object.
*/
static struct connectdata *allocate_conn(void)
{
struct connectdata *conn;
conn = calloc(1, sizeof(struct connectdata));
if(!conn)
return NULL;
conn->handler = &Curl_handler_dummy; /* Be sure we have a handler defined
already from start to avoid NULL
situations and checks */
/* and we setup a few fields in case we end up actually using this struct */
conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
conn->connectindex = -1; /* no index */
Daniel Stenberg
committed
conn->port = -1; /* unknown at this point */
/* Default protocol-independent behavior doesn't support persistent
connections, so we set this to force-close. Protocols that support
this need to set this to FALSE in their "curl_do" functions. */
conn->bits.close = TRUE;
/* Store creation time to help future close decision making */
conn->created = Curl_tvnow();
return conn;
}
Daniel Stenberg
committed
static CURLcode findprotocol(struct SessionHandle *data,
Daniel Stenberg
committed
struct connectdata *conn,
const char *protostr)
Daniel Stenberg
committed
{
const struct Curl_handler * const *pp;
const struct Curl_handler *p;
/* Scan protocol handler table and match against 'protostr' to set a few
variables based on the URL. Now that the handler may be changed later
when the protocol specific setup function is called. */
for (pp = protocols; (p = *pp) != NULL; pp++) {
Daniel Stenberg
committed
if(Curl_raw_equal(p->scheme, protostr)) {
Daniel Stenberg
committed
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
/* Protocol found in table. Check if allowed */
if(!(data->set.allowed_protocols & p->protocol))
/* nope, get out */
break;
/* it is allowed for "normal" request, now do an extra check if this is
the result of a redirect */
if(data->state.this_is_a_follow &&
!(data->set.redir_protocols & p->protocol))
/* nope, get out */
break;
/* Perform setup complement if some. */
conn->handler = p;
conn->protocol |= p->protocol;
/* 'port' and 'remote_port' are set in setup_connection_internals() */
return CURLE_OK;
}
}
/* The protocol was not found in the table, but we don't have to assign it
to anything since it is already assigned to a dummy-struct in the
create_conn() function when the connectdata struct is allocated. */
failf(data, "Protocol %s not supported or disabled in " LIBCURL_NAME,
Daniel Stenberg
committed
protostr);
Daniel Stenberg
committed
return CURLE_UNSUPPORTED_PROTOCOL;
}
/*
* Parse URL and fill in the relevant members of the connection struct.
Daniel Stenberg
committed
*/
Daniel Stenberg
committed
static CURLcode parseurlandfillconn(struct SessionHandle *data,
struct connectdata *conn,
bool *prot_missing)
Daniel Stenberg
committed
char *at;
Claes Jakobsson
committed
char *fragment;
Daniel Stenberg
committed
char *path = data->state.path;
Claes Jakobsson
committed
char *query;
int rc;
Daniel Stenberg
committed
char protobuf[16];
const char *protop;
/*************************************************************
* Parse the URL.
*
* We need to parse the url even when using the proxy, because we will need
* the hostname and port in case we are trying to SSL connect through the
* proxy -- and we don't know if we will need to use SSL until we parse the
* url ...
************************************************************/
if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
Daniel Stenberg
committed
protobuf, path)) &&
Curl_raw_equal(protobuf, "file")) {
if(path[0] == '/' && path[1] == '/') {
/* Allow omitted hostname (e.g. file:/<path>). This is not strictly
* speaking a valid file: URL by RFC 1738, but treating file:/<path> as
* file://localhost/<path> is similar to how other schemes treat missing
* hostnames. See RFC 1808. */
/* This cannot be done with strcpy() in a portable manner, since the
memory areas overlap! */
memmove(path, path + 2, strlen(path + 2)+1);
}
/*
* we deal with file://<host>/<path> differently since it supports no
* hostname other than "localhost" and "127.0.0.1", which is unique among
* the URL protocols specified in RFC 1738
*/
if(path[0] != '/') {
/* the URL included a host name, we ignore host names in file:// URLs
as the standards don't define what to do with them */
char *ptr=strchr(path, '/');
The rest of the locator consists of data specific to the scheme,
and is known as the "url-path". It supplies the details of how the
specified resource can be accessed. Note that the "/" between the
host (or port) and the url-path is NOT part of the url-path.
As most agents use file://localhost/foo to get '/foo' although the
slash preceding foo is a separator and not a slash for the path,
a URL as file://localhost//foo must be valid as well, to refer to
the same file with an absolute path.
*/
if(ptr[1] && ('/' == ptr[1]))
/* if there was two slashes, we skip the first one as that is then
used truly as a separator */
Daniel Stenberg
committed
/* This cannot be made with strcpy, as the memory chunks overlap! */
memmove(path, ptr, strlen(ptr)+1);
Daniel Stenberg
committed
protop = "file"; /* protocol string */
/* clear path */
path[0]=0;
Daniel Stenberg
committed
if(2 > sscanf(data->change.url,
Daniel Stenberg
committed
"%15[^\n:]://%[^\n/]%[^\n]",
Daniel Stenberg
committed
protobuf,
conn->host.name, path)) {
/*
* The URL was badly formatted, let's try the browser-style _without_
* protocol specified like 'http://'.
*/
if(1 > (rc = sscanf(data->change.url, "%[^\n/]%[^\n]",
conn->host.name, path)) ) {
* djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
* assigned, but the return value is EOF!
#if defined(__DJGPP__) && (DJGPP_MINOR == 4)
if (!(rc == -1 && *conn->host.name))
#endif
{
failf(data, "<url> malformed");
return CURLE_URL_MALFORMAT;
}
/*
* Since there was no protocol part specified, we guess what protocol it
* is based on the first letters of the server name.
*/
/* Note: if you add a new protocol, please update the list in
* lib/version.c too! */
if(checkprefix("FTP.", conn->host.name))
Daniel Stenberg
committed
protop = "ftp";
Daniel Stenberg
committed
else if(checkprefix("DICT.", conn->host.name))
Daniel Stenberg
committed
protop = "DICT";
Daniel Stenberg
committed
else if(checkprefix("LDAP.", conn->host.name))
Daniel Stenberg
committed
protop = "LDAP";
else if(checkprefix("IMAP.", conn->host.name))
Daniel Stenberg
committed
protop = "IMAP";
Daniel Stenberg
committed
protop = "http";
*prot_missing = TRUE; /* not given in URL */
Daniel Stenberg
committed
else
protop = protobuf;
Daniel Stenberg
committed
/* We search for '?' in the host name (but only on the right side of a
* @-letter to allow ?-letters in username and password) to handle things
* like http://example.com?param= (notice the missing '/').
*/
at = strchr(conn->host.name, '@');
if(at)
Claes Jakobsson
committed
query = strchr(at+1, '?');
Daniel Stenberg
committed
else
Claes Jakobsson
committed
query = strchr(conn->host.name, '?');
Daniel Stenberg
committed
Claes Jakobsson
committed
if(query) {
/* We must insert a slash before the '?'-letter in the URL. If the URL had
a slash after the '?', that is where the path currently begins and the
'?string' is still part of the host name.
We must move the trailing part from the host name and put it first in
the path. And have it all prefixed with a slash.
*/
Claes Jakobsson
committed
size_t hostlen = strlen(query);
size_t pathlen = strlen(path);
/* move the existing path plus the zero byte forward, to make room for
the host-name part */
memmove(path+hostlen+1, path, pathlen+1);
/* now copy the trailing host part in front of the existing path */
Claes Jakobsson
committed
memcpy(path+1, query, hostlen);
path[0]='/'; /* prepend the missing slash */
Claes Jakobsson
committed
*query=0; /* now cut off the hostname at the ? */
Daniel Stenberg
committed
}
else if(!path[0]) {
/* if there's no path set, use a single slash */
strcpy(path, "/");
Daniel Stenberg
committed
Daniel Stenberg
committed
/* If the URL is malformatted (missing a '/' after hostname before path) we
* insert a slash here. The only letter except '/' we accept to start a path
* is '?'.
*/
if(path[0] == '?') {
Daniel Stenberg
committed
/* We need this function to deal with overlapping memory areas. We know
that the memory area 'path' points to is 'urllen' bytes big and that
is bigger than the path. Use +1 to move the zero byte too. */
memmove(&path[1], path, strlen(path)+1);
path[0] = '/';
Daniel Stenberg
committed
}
if (conn->host.name[0] == '[') {
Daniel Stenberg
committed
/* This looks like an IPv6 address literal. See if there is an address
scope. */
char *percent = strstr (conn->host.name, "%25");
if (percent) {
char *endp;
unsigned long scope = strtoul (percent + 3, &endp, 10);
Daniel Stenberg
committed
if (*endp == ']') {
/* The address scope was well formed. Knock it out of the hostname. */
memmove(percent, endp, strlen(endp)+1);
if (!data->state.this_is_a_follow)
/* Don't honour a scope given in a Location: header */
} else
infof(data, "Invalid IPv6 address format\n");
Daniel Stenberg
committed
}
}
Claes Jakobsson
committed
if(data->set.scope)
Daniel Stenberg
committed
/* Override any scope that was set above. */
conn->scope = data->set.scope;
Claes Jakobsson
committed
/* Remove the fragment part of the path. Per RFC 2396, this is always the
last part of the URI. We are looking for the first '#' so that we deal gracefully
with non conformant URI such as http://example.com#foo#bar. */
fragment = strchr(path, '#');
if(fragment)
*fragment = 0;
Claes Jakobsson
committed
* So if the URL was A://B/C#D,
Daniel Stenberg
committed
* protop is A
* conn->host.name is B
Daniel Stenberg
committed
* data->state.path is /C
*/
Daniel Stenberg
committed
Daniel Stenberg
committed
return findprotocol(data, conn, protop);
}
static void llist_dtor(void *user, void *element)
{
(void)user;
(void)element;
/* Do nothing */
}
/*
* If we're doing a resumed transfer, we need to setup our stuff
* properly.
*/
Daniel Stenberg
committed
static CURLcode setup_range(struct SessionHandle *data)
{
Daniel Stenberg
committed
struct UrlState *s = &data->state;
s->resume_from = data->set.set_resume_from;
if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
if(s->rangestringalloc)
free(s->range);
if(s->resume_from)
s->range = aprintf("%" FORMAT_OFF_TU "-", s->resume_from);
Daniel Stenberg
committed
else
Daniel Stenberg
committed
s->range = strdup(data->set.str[STRING_SET_RANGE]);
Daniel Stenberg
committed
Daniel Stenberg
committed
s->rangestringalloc = (bool)(s->range?TRUE:FALSE);
Daniel Stenberg
committed
Daniel Stenberg
committed
if(!s->range)
Daniel Stenberg
committed
return CURLE_OUT_OF_MEMORY;
/* tell ourselves to fetch this range */
Daniel Stenberg
committed
s->use_range = TRUE; /* enable range download */
Daniel Stenberg
committed
}
else
Daniel Stenberg
committed
s->use_range = FALSE; /* disable range download */
Daniel Stenberg
committed
return CURLE_OK;
}
/***************************************************************
Daniel Stenberg
committed
* Setup connection internals specific to the requested protocol.
* This MUST get called after proxy magic has been figured out.
***************************************************************/
Daniel Stenberg
committed
static CURLcode setup_connection_internals(struct connectdata *conn)
{
Patrick Monnerat
committed
const struct Curl_handler * p;
CURLcode result;
Patrick Monnerat
committed
conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
Patrick Monnerat
committed
/* Scan protocol handler table. */
Daniel Stenberg
committed
/* Perform setup complement if some. */
p = conn->handler;
Daniel Stenberg
committed
if(p->setup_connection) {
result = (*p->setup_connection)(conn);
Daniel Stenberg
committed
if(result != CURLE_OK)
return result;
Daniel Stenberg
committed
p = conn->handler; /* May have changed. */
}
Daniel Stenberg
committed
if(conn->port < 0)
/* we check for -1 here since if proxy was detected already, this
was very likely already set to the proxy port */
conn->port = p->defport;
conn->remote_port = (unsigned short)p->defport;
conn->protocol |= p->protocol;
Daniel Stenberg
committed
return CURLE_OK;
}
Daniel Stenberg
committed
#ifndef CURL_DISABLE_PROXY
Daniel Stenberg
committed
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
/****************************************************************
* Checks if the host is in the noproxy list. returns true if it matches
* and therefore the proxy should NOT be used.
****************************************************************/
static bool check_noproxy(const char* name, const char* no_proxy)
{
/* no_proxy=domain1.dom,host.domain2.dom
* (a comma-separated list of hosts which should
* not be proxied, or an asterisk to override
* all proxy variables)
*/
size_t tok_start;
size_t tok_end;
const char* separator = ", ";
size_t no_proxy_len;
size_t namelen;
char *endptr;
if(no_proxy && no_proxy[0]) {
if(Curl_raw_equal("*", no_proxy)) {
return TRUE;
}
/* NO_PROXY was specified and it wasn't just an asterisk */
no_proxy_len = strlen(no_proxy);
endptr = strchr(name, ':');
if(endptr)
namelen = endptr - name;
else
namelen = strlen(name);
for (tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
while (tok_start < no_proxy_len &&
strchr(separator, no_proxy[tok_start]) != NULL) {
/* Look for the beginning of the token. */
++tok_start;
}
if(tok_start == no_proxy_len)
break; /* It was all trailing separator chars, no more tokens. */
for (tok_end = tok_start; tok_end < no_proxy_len &&
strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end) {
/* Look for the end of the token. */
}
/* To match previous behaviour, where it was necessary to specify
* ".local.com" to prevent matching "notlocal.com", we will leave
* the '.' off.
*/
if(no_proxy[tok_start] == '.')
++tok_start;
if((tok_end - tok_start) <= namelen) {
/* Match the last part of the name to the domain we are checking. */
const char *checkn = name + namelen - (tok_end - tok_start);
if(Curl_raw_nequal(no_proxy + tok_start, checkn, tok_end - tok_start)) {
if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
/* We either have an exact match, or the previous character is a .
* so it is within the same domain, so no proxy for this host.
*/
return TRUE;
}
}
} /* if((tok_end - tok_start) <= namelen) */
} /* for (tok_start = 0; tok_start < no_proxy_len;
tok_start = tok_end + 1) */
} /* NO_PROXY was specified and it wasn't just an asterisk */
return FALSE;
}
/****************************************************************
* Detect what (if any) proxy to use. Remember that this selects a host
* name and is not limited to HTTP proxies only.
* The returned pointer must be freed by the caller (unless NULL)
****************************************************************/
static char *detect_proxy(struct connectdata *conn)
{
char *proxy = NULL;
Daniel Stenberg
committed
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
#ifndef CURL_DISABLE_HTTP
/* If proxy was not specified, we check for default proxy environment
* variables, to enable i.e Lynx compliance:
*
* http_proxy=http://some.server.dom:port/
* https_proxy=http://some.server.dom:port/
* ftp_proxy=http://some.server.dom:port/
* no_proxy=domain1.dom,host.domain2.dom
* (a comma-separated list of hosts which should
* not be proxied, or an asterisk to override
* all proxy variables)
* all_proxy=http://some.server.dom:port/
* (seems to exist for the CERN www lib. Probably
* the first to check for.)
*
* For compatibility, the all-uppercase versions of these variables are
* checked if the lowercase versions don't exist.
*/
char *no_proxy=NULL;
char proxy_env[128];
no_proxy=curl_getenv("no_proxy");
if(!no_proxy)
no_proxy=curl_getenv("NO_PROXY");
Daniel Stenberg
committed
if(!check_noproxy(conn->host.name, no_proxy)) {
/* It was not listed as without proxy */
Daniel Stenberg
committed
const char *protop = conn->handler->scheme;
Daniel Stenberg
committed
char *envp = proxy_env;
char *prox;
Daniel Stenberg
committed
/* Now, build <protocol>_proxy and check for such a one to use */
while(*protop)
*envp++ = (char)tolower((int)*protop++);
Daniel Stenberg
committed
/* append _proxy */
strcpy(envp, "_proxy");
Daniel Stenberg
committed
/* read the protocol proxy: */
prox=curl_getenv(proxy_env);
Daniel Stenberg
committed
/*
* We don't try the uppercase version of HTTP_PROXY because of
* security reasons:
*
* When curl is used in a webserver application
* environment (cgi or php), this environment variable can
* be controlled by the web server user by setting the
* http header 'Proxy:' to some value.
*
* This can cause 'internal' http/ftp requests to be
* arbitrarily redirected by any external attacker.
*/
if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
/* There was no lowercase variable, try the uppercase version: */
Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
prox=curl_getenv(proxy_env);
}
Daniel Stenberg
committed
if(prox && *prox) { /* don't count "" strings */
proxy = prox; /* use this */
}
else {
proxy = curl_getenv("all_proxy"); /* default proxy to use */
if(!proxy)
proxy=curl_getenv("ALL_PROXY");
}
} /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
non-proxy */
if(no_proxy)
free(no_proxy);
#else /* !CURL_DISABLE_HTTP */
(void)conn;
#endif /* CURL_DISABLE_HTTP */
return proxy;
}
/*
* If this is supposed to use a proxy, we need to figure out the proxy
* host name, so that we can re-use an existing connection
* that may exist registered to the same proxy host.
* proxy will be freed before this function returns.
*/
static CURLcode parse_proxy(struct SessionHandle *data,
struct connectdata *conn, char *proxy)
{
char *prox_portno;
char *endofprot;
/* We use 'proxyptr' to point to the proxy name from now on... */
char *proxyptr;
char *portptr;
char *atsign;
/* We do the proxy host string parsing here. We want the host name and the
* port name. Accept a protocol:// prefix, even though it should just be
* ignored.
*/
/* Skip the protocol part if present */
endofprot = strstr(proxy, "://");
if(endofprot)
proxyptr = endofprot+3;
else
proxyptr = proxy;
/* Is there a username and password given in this proxy url? */
atsign = strchr(proxyptr, '@');
if(atsign) {
char proxyuser[MAX_CURL_USER_LENGTH];
char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
proxypasswd[0] = 0;
if(1 <= sscanf(proxyptr,
"%" MAX_CURL_USER_LENGTH_TXT"[^:@]:"
"%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
proxyuser, proxypasswd)) {
CURLcode res = CURLE_OK;
/* found user and password, rip them out. note that we are
unescaping them, as there is otherwise no way to have a
username or password with reserved characters like ':' in
them. */
Curl_safefree(conn->proxyuser);
conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
if(!conn->proxyuser)
res = CURLE_OUT_OF_MEMORY;
else {
Curl_safefree(conn->proxypasswd);
conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
if(!conn->proxypasswd)
res = CURLE_OUT_OF_MEMORY;
}
if(CURLE_OK == res) {
conn->bits.proxy_user_passwd = TRUE; /* enable it */
atsign = strdup(atsign+1); /* the right side of the @-letter */
if(atsign) {
free(proxy); /* free the former proxy string */
proxy = proxyptr = atsign; /* now use this instead */
}
else
res = CURLE_OUT_OF_MEMORY;
}
if(res) {
free(proxy); /* free the allocated proxy string */
return res;
}