host: get the custom Host: name more genericly

When given a custom host name in a Host: header, we can use it for
several different purposes other than just cookies, so we rename it and
use it for SSL SNI etc.
This commit is contained in:
Daniel Stenberg 2010-11-04 14:58:15 +01:00
parent 68cde058f6
commit afecd1aa13
4 changed files with 20 additions and 29 deletions

View File

@ -2254,26 +2254,25 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
ptr = Curl_checkheaders(data, "Host:"); ptr = Curl_checkheaders(data, "Host:");
if(ptr && (!data->state.this_is_a_follow || if(ptr && (!data->state.this_is_a_follow ||
Curl_raw_equal(data->state.first_host, conn->host.name))) { Curl_raw_equal(data->state.first_host, conn->host.name))) {
#if !defined(CURL_DISABLE_COOKIES)
/* If we have a given custom Host: header, we extract the host name in /* If we have a given custom Host: header, we extract the host name in
order to possibly use it for cookie reasons later on. We only allow the order to possibly use it for cookie reasons later on. We only allow the
custom Host: header if this is NOT a redirect, as setting Host: in the custom Host: header if this is NOT a redirect, as setting Host: in the
redirected request is being out on thin ice. Except if the host name redirected request is being out on thin ice. Except if the host name
is the same as the first one! */ is the same as the first one! */
char *cookiehost = Curl_copy_header_value(ptr); char *chost = Curl_copy_header_value(ptr);
if (!cookiehost) if (!chost)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
if (!*cookiehost) if (!*chost)
/* ignore empty data */ /* ignore empty data */
free(cookiehost); free(chost);
else { else {
char *colon = strchr(cookiehost, ':'); char *colon = strchr(chost, ':');
if (colon) if (colon)
*colon = 0; /* The host must not include an embedded port number */ *colon = 0; /* The host must not include an embedded port number */
Curl_safefree(conn->allocptr.cookiehost); Curl_safefree(conn->allocptr.customhost);
conn->allocptr.cookiehost = cookiehost; conn->allocptr.customhost = chost;
} }
#endif
conn->allocptr.host = NULL; conn->allocptr.host = NULL;
} }
@ -2597,8 +2596,8 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
if(data->cookies) { if(data->cookies) {
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE); Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
co = Curl_cookie_getlist(data->cookies, co = Curl_cookie_getlist(data->cookies,
conn->allocptr.cookiehost? conn->allocptr.customhost?
conn->allocptr.cookiehost:host, conn->allocptr.customhost:host,
data->state.path, data->state.path,
(bool)(conn->protocol&PROT_HTTPS?TRUE:FALSE)); (bool)(conn->protocol&PROT_HTTPS?TRUE:FALSE));
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE); Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
@ -3689,8 +3688,8 @@ CURLcode Curl_http_readwrite_headers(struct SessionHandle *data,
data->cookies, TRUE, k->p+11, data->cookies, TRUE, k->p+11,
/* If there is a custom-set Host: name, use it /* If there is a custom-set Host: name, use it
here, or else use real peer host name. */ here, or else use real peer host name. */
conn->allocptr.cookiehost? conn->allocptr.customhost?
conn->allocptr.cookiehost:conn->host.name, conn->allocptr.customhost:conn->host.name,
data->state.path); data->state.path);
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE); Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
} }

View File

@ -1430,7 +1430,6 @@ ossl_connect_step1(struct connectdata *conn,
struct ssl_connect_data *connssl = &conn->ssl[sockindex]; struct ssl_connect_data *connssl = &conn->ssl[sockindex];
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
const char *hostname; const char *hostname;
bool hostname_static;
bool sni; bool sni;
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
struct in6_addr addr; struct in6_addr addr;
@ -1643,18 +1642,13 @@ ossl_connect_step1(struct connectdata *conn,
connssl->server_cert = 0x0; connssl->server_cert = 0x0;
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
hostname = Curl_checkheaders(data, "Host:"); if((!data->state.this_is_a_follow ||
if(hostname && (!data->state.this_is_a_follow || Curl_raw_equal(data->state.first_host, conn->host.name)) &&
Curl_raw_equal(data->state.first_host, conn->host.name))) { conn->allocptr.customhost)
hostname_static = FALSE; hostname = conn->allocptr.customhost;
hostname = Curl_copy_header_value(hostname); else
if(!hostname) {
return CURLE_OUT_OF_MEMORY;
}
} else {
hostname_static = TRUE;
hostname = conn->host.name; hostname = conn->host.name;
}
if ((0 == Curl_inet_pton(AF_INET, hostname, &addr)) && if ((0 == Curl_inet_pton(AF_INET, hostname, &addr)) &&
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
(0 == Curl_inet_pton(AF_INET6, hostname, &addr)) && (0 == Curl_inet_pton(AF_INET6, hostname, &addr)) &&
@ -1663,8 +1657,6 @@ ossl_connect_step1(struct connectdata *conn,
!SSL_set_tlsext_host_name(connssl->handle, hostname)) !SSL_set_tlsext_host_name(connssl->handle, hostname))
infof(data, "WARNING: failed to configure server name indication (SNI) " infof(data, "WARNING: failed to configure server name indication (SNI) "
"TLS extension\n"); "TLS extension\n");
if(!hostname_static)
free((char *) hostname);
#endif #endif
/* Check if there's a cached ID we can/should use here! */ /* Check if there's a cached ID we can/should use here! */

View File

@ -2534,7 +2534,7 @@ static void conn_free(struct connectdata *conn)
Curl_safefree(conn->allocptr.rangeline); Curl_safefree(conn->allocptr.rangeline);
Curl_safefree(conn->allocptr.ref); Curl_safefree(conn->allocptr.ref);
Curl_safefree(conn->allocptr.host); Curl_safefree(conn->allocptr.host);
Curl_safefree(conn->allocptr.cookiehost); Curl_safefree(conn->allocptr.customhost);
Curl_safefree(conn->allocptr.rtsp_transport); Curl_safefree(conn->allocptr.rtsp_transport);
Curl_safefree(conn->trailer); Curl_safefree(conn->trailer);
Curl_safefree(conn->host.rawalloc); /* host name buffer */ Curl_safefree(conn->host.rawalloc); /* host name buffer */

View File

@ -796,7 +796,7 @@ struct connectdata {
char *rangeline; /* free later if not NULL! */ char *rangeline; /* free later if not NULL! */
char *ref; /* free later if not NULL! */ char *ref; /* free later if not NULL! */
char *host; /* free later if not NULL */ char *host; /* free later if not NULL */
char *cookiehost; /* free later if not NULL */ char *customhost; /* free later if not NULL */
char *rtsp_transport; /* free later if not NULL */ char *rtsp_transport; /* free later if not NULL */
} allocptr; } allocptr;