1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 16:18:48 -05:00

url: Reworked URL parsing to allow overriding by CURLOPT_USERPWD

This commit is contained in:
Steve Holme 2013-04-18 17:02:28 +01:00
parent da06ac7f3f
commit 90c87f311e

View File

@ -143,7 +143,7 @@ static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
static CURLcode do_init(struct connectdata *conn); static CURLcode do_init(struct connectdata *conn);
static CURLcode parse_url_userpass(struct SessionHandle *data, static CURLcode parse_url_userpass(struct SessionHandle *data,
struct connectdata *conn, struct connectdata *conn,
char *user, char *passwd); char *user, char *passwd, char *options);
/* /*
* Protocol table. * Protocol table.
*/ */
@ -3651,8 +3651,7 @@ static CURLcode findprotocol(struct SessionHandle *data,
static CURLcode parseurlandfillconn(struct SessionHandle *data, static CURLcode parseurlandfillconn(struct SessionHandle *data,
struct connectdata *conn, struct connectdata *conn,
bool *prot_missing, bool *prot_missing,
char *user, char *user, char *passwd, char *options)
char *passwd)
{ {
char *at; char *at;
char *fragment; char *fragment;
@ -3870,7 +3869,7 @@ static CURLcode parseurlandfillconn(struct SessionHandle *data,
* Parse a user name and password in the URL and strip it out * Parse a user name and password in the URL and strip it out
* of the host name * of the host name
*************************************************************/ *************************************************************/
result = parse_url_userpass(data, conn, user, passwd); result = parse_url_userpass(data, conn, user, passwd, options);
if(result != CURLE_OK) if(result != CURLE_OK)
return result; return result;
@ -4341,10 +4340,8 @@ static CURLcode parse_proxy_auth(struct SessionHandle *data,
*/ */
static CURLcode parse_url_userpass(struct SessionHandle *data, static CURLcode parse_url_userpass(struct SessionHandle *data,
struct connectdata *conn, struct connectdata *conn,
char *user, char *passwd) char *user, char *passwd, char *options)
{ {
char options[MAX_CURL_OPTIONS_LENGTH];
/* At this point, we're hoping all the other special cases have /* At this point, we're hoping all the other special cases have
* been taken care of, so conn->host.name is at most * been taken care of, so conn->host.name is at most
* [user[:password][;options]]@]hostname * [user[:password][;options]]@]hostname
@ -4440,8 +4437,8 @@ static CURLcode parse_url_userpass(struct SessionHandle *data,
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
if(strlen(newoptions) < MAX_CURL_OPTIONS_LENGTH) if(strlen(newoptions) < MAX_CURL_OPTIONS_LENGTH)
conn->options = newoptions; strcpy(options, newoptions);
else
free(newoptions); free(newoptions);
} }
} }
@ -4610,11 +4607,13 @@ static void override_userpass(struct SessionHandle *data,
* Set password so it's available in the connection. * Set password so it's available in the connection.
*/ */
static CURLcode set_userpass(struct connectdata *conn, static CURLcode set_userpass(struct connectdata *conn,
const char *user, const char *passwd) const char *user, const char *passwd,
const char *options)
{ {
CURLcode result = CURLE_OK;
/* If our protocol needs a password and we have none, use the defaults */ /* If our protocol needs a password and we have none, use the defaults */
if((conn->handler->flags & PROTOPT_NEEDSPWD) && if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd) {
!conn->bits.user_passwd) {
conn->user = strdup(CURL_DEFAULT_USER); conn->user = strdup(CURL_DEFAULT_USER);
if(conn->user) if(conn->user)
@ -4624,17 +4623,28 @@ static CURLcode set_userpass(struct connectdata *conn,
/* This is the default password, so DON'T set conn->bits.user_passwd */ /* This is the default password, so DON'T set conn->bits.user_passwd */
} }
else { else {
/* store user + password, zero-length if not set */ /* Store the user, zero-length if not set */
conn->user = strdup(user); conn->user = strdup(user);
/* Store the password (only if user is present), zero-length if not set */
if(conn->user) if(conn->user)
conn->passwd = strdup(passwd); conn->passwd = strdup(passwd);
else else
conn->passwd = NULL; conn->passwd = NULL;
} }
if(!conn->user || !conn->passwd)
return CURLE_OUT_OF_MEMORY;
return CURLE_OK; if(!conn->user || !conn->passwd)
result = CURLE_OUT_OF_MEMORY;
/* Store the options, null if not set */
if(!result && options[0]) {
conn->options = strdup(options);
if(!conn->options)
result = CURLE_OUT_OF_MEMORY;
}
return result;
} }
/************************************************************* /*************************************************************
@ -4800,12 +4810,13 @@ static CURLcode create_conn(struct SessionHandle *data,
struct connectdata **in_connect, struct connectdata **in_connect,
bool *async) bool *async)
{ {
CURLcode result=CURLE_OK; CURLcode result = CURLE_OK;
struct connectdata *conn; struct connectdata *conn;
struct connectdata *conn_temp = NULL; struct connectdata *conn_temp = NULL;
size_t urllen; size_t urllen;
char user[MAX_CURL_USER_LENGTH]; char user[MAX_CURL_USER_LENGTH];
char passwd[MAX_CURL_PASSWORD_LENGTH]; char passwd[MAX_CURL_PASSWORD_LENGTH];
char options[MAX_CURL_OPTIONS_LENGTH];
bool reuse; bool reuse;
char *proxy = NULL; char *proxy = NULL;
bool prot_missing = FALSE; bool prot_missing = FALSE;
@ -4874,7 +4885,8 @@ static CURLcode create_conn(struct SessionHandle *data,
conn->host.name = conn->host.rawalloc; conn->host.name = conn->host.rawalloc;
conn->host.name[0] = 0; conn->host.name[0] = 0;
result = parseurlandfillconn(data, conn, &prot_missing, user, passwd); result = parseurlandfillconn(data, conn, &prot_missing, user, passwd,
options);
if(result != CURLE_OK) if(result != CURLE_OK)
return result; return result;
@ -5072,7 +5084,7 @@ static CURLcode create_conn(struct SessionHandle *data,
* for use * for use
*************************************************************/ *************************************************************/
override_userpass(data, conn, user, passwd); override_userpass(data, conn, user, passwd);
result = set_userpass(conn, user, passwd); result = set_userpass(conn, user, passwd, options);
if(result != CURLE_OK) if(result != CURLE_OK)
return result; return result;