From d1cfbd51b5f6e4246cec86ce3c9b7b1cde2f860a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 2 Mar 2001 15:34:15 +0000 Subject: [PATCH] remade the port number stuff so that following locations work and doing intermixed HTTP and FTP persistant connections also work! --- lib/ftp.c | 2 +- lib/http.c | 6 ++--- lib/transfer.c | 18 +++++++++------ lib/url.c | 62 ++++++++++++++++++++++---------------------------- lib/urldata.h | 14 +++++++----- 5 files changed, 50 insertions(+), 52 deletions(-) diff --git a/lib/ftp.c b/lib/ftp.c index 468f66233..293a20e2c 100644 --- a/lib/ftp.c +++ b/lib/ftp.c @@ -1065,7 +1065,7 @@ again:; he = conn->hp; #endif connectport = - (unsigned short)data->port; /* we connect to the proxy's port */ + (unsigned short)conn->port; /* we connect to the proxy's port */ } else { /* normal, direct, ftp connection */ diff --git a/lib/http.c b/lib/http.c index 641373ae2..a77cc0e45 100644 --- a/lib/http.c +++ b/lib/http.c @@ -465,14 +465,14 @@ CURLcode Curl_http(struct connectdata *conn) /* if ptr_host is already set, it is OK since we only re-use connections to the very same host and port */ - if(((conn->protocol&PROT_HTTPS) && (data->remote_port == PORT_HTTPS)) || - (!(conn->protocol&PROT_HTTPS) && (data->remote_port == PORT_HTTP)) ) + if(((conn->protocol&PROT_HTTPS) && (conn->remote_port == PORT_HTTPS)) || + (!(conn->protocol&PROT_HTTPS) && (conn->remote_port == PORT_HTTP)) ) /* If (HTTPS on port 443) OR (non-HTTPS on port 80) then don't include the port number in the host string */ conn->allocptr.host = aprintf("Host: %s\r\n", host); else conn->allocptr.host = aprintf("Host: %s:%d\r\n", host, - data->remote_port); + conn->remote_port); } if(!checkheaders(data, "Pragma:")) diff --git a/lib/transfer.c b/lib/transfer.c index 1f1af2ad7..5916ea916 100644 --- a/lib/transfer.c +++ b/lib/transfer.c @@ -632,12 +632,13 @@ CURLcode curl_transfer(CURL *curl) CURLcode res; struct UrlData *data = curl; struct connectdata *c_connect=NULL; + bool port=TRUE; /* allow data->use_port to set port to use */ Curl_pgrsStartNow(data); do { Curl_pgrsTime(data, TIMER_STARTSINGLE); - res = curl_connect(curl, (CURLconnect **)&c_connect); + res = curl_connect(curl, (CURLconnect **)&c_connect, port); if(res == CURLE_OK) { res = curl_do(c_connect); if(res == CURLE_OK) { @@ -654,6 +655,9 @@ CURLcode curl_transfer(CURL *curl) char prot[16]; /* URL protocol string storage */ char letter; /* used for a silly sscanf */ + port=TRUE; /* by default we use the user set port number even after + a Location: */ + if (data->maxredirs && (data->followlocation >= data->maxredirs)) { failf(data,"Maximum (%d) redirects followed", data->maxredirs); #ifdef USE_OLD_DISCONNECT @@ -701,9 +705,10 @@ CURLcode curl_transfer(CURL *curl) if(!protsep) protsep=data->url; else { - /* TBD: set the port with curl_setopt() */ - data->port=0; /* we got a full URL and then we should reset the - port number here to re-initiate it later */ + port=FALSE; /* we got a full URL and thus we should not obey the + port number that might have been set by the user + in data->use_port */ + protsep+=2; /* pass the slashes */ } @@ -740,9 +745,8 @@ CURLcode curl_transfer(CURL *curl) data->newurl = newest; } else { - /* This was an absolute URL, clear the port number! */ - /* TBD: set the port with curl_setopt() */ - data->port = 0; + /* This is an absolute URL, don't use the custom port number */ + port = FALSE; } if(data->bits.urlstringalloc) diff --git a/lib/url.c b/lib/url.c index 5f7fe57dc..4281109a6 100644 --- a/lib/url.c +++ b/lib/url.c @@ -378,7 +378,7 @@ CURLcode curl_setopt(CURL *curl, CURLoption option, ...) data->url = va_arg(param, char *); break; case CURLOPT_PORT: - data->port = va_arg(param, long); + data->use_port = va_arg(param, long); break; case CURLOPT_POST: /* Does this option serve a purpose anymore? */ @@ -704,7 +704,7 @@ static CURLcode ConnectPlease(struct UrlData *data, memcpy((char *)&(conn->serv_addr.sin_addr), conn->hp->h_addr, conn->hp->h_length); conn->serv_addr.sin_family = conn->hp->h_addrtype; - conn->serv_addr.sin_port = htons(data->port); + conn->serv_addr.sin_port = htons(conn->port); #else /* IPv6-style */ struct addrinfo *ai; @@ -910,7 +910,9 @@ static CURLcode ConnectPlease(struct UrlData *data, } -static CURLcode _connect(CURL *curl, CURLconnect **in_connect) +static CURLcode _connect(CURL *curl, + CURLconnect **in_connect, + bool allow_port) /* allow data->use_port ? */ { char *tmp; char *buf; @@ -1275,9 +1277,8 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) *************************************************************/ if (strequal(conn->protostr, "HTTP")) { - if(!data->port) - data->port = PORT_HTTP; - data->remote_port = PORT_HTTP; + conn->port = (data->use_port && allow_port)?data->use_port:PORT_HTTP; + conn->remote_port = PORT_HTTP; conn->protocol |= PROT_HTTP; conn->curl_do = Curl_http; conn->curl_done = Curl_http_done; @@ -1285,9 +1286,9 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) } else if (strequal(conn->protostr, "HTTPS")) { #ifdef USE_SSLEAY - if(!data->port) - data->port = PORT_HTTPS; - data->remote_port = PORT_HTTPS; + + conn->port = (data->use_port && allow_port)?data->use_port:PORT_HTTPS; + conn->remote_port = PORT_HTTPS; conn->protocol |= PROT_HTTP; conn->protocol |= PROT_HTTPS; @@ -1302,9 +1303,8 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) #endif /* !USE_SSLEAY */ } else if (strequal(conn->protostr, "GOPHER")) { - if(!data->port) - data->port = PORT_GOPHER; - data->remote_port = PORT_GOPHER; + conn->port = (data->use_port && allow_port)?data->use_port:PORT_GOPHER; + conn->remote_port = PORT_GOPHER; /* Skip // in path if present */ if (isdigit((int)conn->path[1])) { conn->ppath = strchr(&conn->path[1], '/'); @@ -1318,9 +1318,8 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) } else if(strequal(conn->protostr, "FTP")) { char *type; - if(!data->port) - data->port = PORT_FTP; - data->remote_port = PORT_FTP; + conn->port = (data->use_port && allow_port)?data->use_port:PORT_FTP; + conn->remote_port = PORT_FTP; conn->protocol |= PROT_FTP; if(data->bits.httpproxy && @@ -1368,27 +1367,23 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) else if(strequal(conn->protostr, "TELNET")) { /* telnet testing factory */ conn->protocol |= PROT_TELNET; - if(!data->port) - data->port = PORT_TELNET; - data->remote_port = PORT_TELNET; + conn->port = (data->use_port && allow_port)?data->use_port: PORT_TELNET; + conn->remote_port = PORT_TELNET; conn->curl_do = Curl_telnet; conn->curl_done = Curl_telnet_done; - } else if (strequal(conn->protostr, "DICT")) { conn->protocol |= PROT_DICT; - if(!data->port) - data->port = PORT_DICT; - data->remote_port = PORT_DICT; + conn->port = (data->use_port && allow_port)?data->use_port:PORT_DICT; + conn->remote_port = PORT_DICT; conn->curl_do = Curl_dict; conn->curl_done = Curl_dict_done; } else if (strequal(conn->protostr, "LDAP")) { conn->protocol |= PROT_LDAP; - if(!data->port) - data->port = PORT_LDAP; - data->remote_port = PORT_LDAP; + conn->port = (data->use_port && allow_port)?data->use_port:PORT_LDAP; + conn->remote_port = PORT_LDAP; conn->curl_do = Curl_ldap; conn->curl_done = Curl_ldap_done; } @@ -1516,13 +1511,9 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) if (tmp) { *tmp++ = '\0'; /* cut off the name there */ - data->remote_port = atoi(tmp); + conn->remote_port = atoi(tmp); } - /* copy the port-specifics to the connection struct */ - conn->port = data->port; - conn->remote_port = data->remote_port; - /************************************************************* * Check the current list of connections to see if we can * re-use an already existing one or if we have to create a @@ -1564,7 +1555,7 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) if(!data->bits.httpproxy) { /* If not connecting via a proxy, extract the port from the URL, if it is * there, thus overriding any defaults that might have been set above. */ - data->port = data->remote_port; /* it is the same port */ + conn->port = conn->remote_port; /* it is the same port */ /* Resolve target host right on */ if(!conn->hp) { @@ -1621,12 +1612,12 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) *prox_portno = 0x0; /* cut off number from host name */ prox_portno ++; /* now set the local port number */ - data->port = atoi(prox_portno); + conn->port = atoi(prox_portno); } else if(data->proxyport) { /* None given in the proxy string, then get the default one if it is given */ - data->port = data->proxyport; + conn->port = data->proxyport; } /* resolve proxy */ @@ -1741,13 +1732,14 @@ static CURLcode _connect(CURL *curl, CURLconnect **in_connect) return CURLE_OK; } -CURLcode curl_connect(CURL *curl, CURLconnect **in_connect) +CURLcode curl_connect(CURL *curl, CURLconnect **in_connect, + bool allow_port) { CURLcode code; struct connectdata *conn; /* call the stuff that needs to be called */ - code = _connect(curl, in_connect); + code = _connect(curl, in_connect, allow_port); if(CURLE_OK != code) { /* We're not allowed to return failure with memory left allocated diff --git a/lib/urldata.h b/lib/urldata.h index d9139826e..2c2727953 100644 --- a/lib/urldata.h +++ b/lib/urldata.h @@ -468,9 +468,7 @@ struct UrlData { FILE *writeheader; /* write the header to this is non-NULL */ char *url; /* what to get */ char *freethis; /* if non-NULL, an allocated string for the URL */ - long port; /* which port to use (if non-protocol bind) */ - unsigned short remote_port; /* what remote port to connect to, not the proxy - port! */ + long use_port; /* which port to use (when not using default) */ struct Configbits bits; /* new-style (v7) flag data */ struct ssl_config_data ssl; /* this is for ssl-stuff */ @@ -697,15 +695,19 @@ CURLcode curl_write(CURLconnect *c_conn, char *buf, size_t amount, * this connect. This allows multiple connects from the same handle returned * by curl_open(). * + * By setting 'allow_port' to FALSE, the data->use_port will *NOT* be + * respected. + * * EXAMPLE * * CURLCode result; * CURL curl; * CURLconnect connect; - * result = curl_connect(curl, &connect); - */ + * result = curl_connect(curl, &connect); */ -CURLcode curl_connect(CURL *curl, CURLconnect **in_connect); +CURLcode curl_connect(CURL *curl, + CURLconnect **in_connect, + bool allow_port); /* * NAME curl_do()