url: protocol handler lookup tidy-up

- rename lookup to what it does
- use ARRAYSIZE instead of NULL check for end
- offer alternate lookup for 0-terminated strings

Closes #12216
This commit is contained in:
Stefan Eissing 2023-10-27 11:53:26 +02:00 committed by Daniel Stenberg
parent a426b5050f
commit 39547ae64d
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
5 changed files with 30 additions and 29 deletions

View File

@ -171,7 +171,7 @@ static CURLcode protocol2num(const char *str, curl_prot_t *val)
str = strchr(str, ',');
tlen = str? (size_t) (str - token): strlen(token);
if(tlen) {
const struct Curl_handler *h = Curl_builtin_scheme(token, tlen);
const struct Curl_handler *h = Curl_getn_scheme_handler(token, tlen);
if(!h)
return CURLE_UNSUPPORTED_PROTOCOL;

View File

@ -1640,7 +1640,7 @@ CURLcode Curl_follow(struct Curl_easy *data,
return Curl_uc_to_curlcode(uc);
}
p = Curl_builtin_scheme(scheme, CURL_ZERO_TERMINATED);
p = Curl_get_scheme_handler(scheme);
if(p && (p->protocol != data->info.conn_protocol)) {
infof(data, "Clear auth, redirects scheme from %s to %s",
data->info.conn_scheme, scheme);

View File

@ -288,8 +288,6 @@ static const struct Curl_handler * const protocols[] = {
#ifndef CURL_DISABLE_DICT
&Curl_handler_dict,
#endif
(struct Curl_handler *) NULL
};
void Curl_freeset(struct Curl_easy *data)
@ -1607,20 +1605,23 @@ error:
return NULL;
}
/* returns the handler if the given scheme is built-in */
const struct Curl_handler *Curl_builtin_scheme(const char *scheme,
size_t schemelen)
const struct Curl_handler *Curl_get_scheme_handler(const char *scheme)
{
const struct Curl_handler * const *pp;
const struct Curl_handler *p;
return Curl_getn_scheme_handler(scheme, strlen(scheme));
}
/* returns the handler if the given scheme is built-in */
const struct Curl_handler *Curl_getn_scheme_handler(const char *scheme,
size_t len)
{
size_t i;
/* Scan protocol handler table and match against 'scheme'. The handler may
be changed later when the protocol specific setup function is called. */
if(schemelen == CURL_ZERO_TERMINATED)
schemelen = strlen(scheme);
for(pp = protocols; (p = *pp) != NULL; pp++)
if(strncasecompare(p->scheme, scheme, schemelen) && !p->scheme[schemelen])
for(i = 0; i < ARRAYSIZE(protocols); ++i)
if(strncasecompare(protocols[i]->scheme, scheme, len) &&
!protocols[i]->scheme[len])
/* Protocol found in table. */
return p;
return protocols[i];
return NULL; /* not found */
}
@ -1629,8 +1630,7 @@ static CURLcode findprotocol(struct Curl_easy *data,
struct connectdata *conn,
const char *protostr)
{
const struct Curl_handler *p = Curl_builtin_scheme(protostr,
CURL_ZERO_TERMINATED);
const struct Curl_handler *p = Curl_get_scheme_handler(protostr);
if(p && /* Protocol found in table. Check if allowed */
(data->set.allowed_protocols & p->protocol)) {
@ -1644,7 +1644,6 @@ static CURLcode findprotocol(struct Curl_easy *data,
else {
/* Perform setup complement if some. */
conn->handler = conn->given = p;
/* 'port' and 'remote_port' are set in setup_connection_internals() */
return CURLE_OK;
}

View File

@ -46,8 +46,13 @@ CURLcode Curl_parse_login_details(const char *login, const size_t len,
char **userptr, char **passwdptr,
char **optionsptr);
const struct Curl_handler *Curl_builtin_scheme(const char *scheme,
size_t schemelen);
/* Get protocol handler for a URI scheme
* @param scheme URI scheme, case-insensitive
* @return NULL of handler not found
*/
const struct Curl_handler *Curl_get_scheme_handler(const char *scheme);
const struct Curl_handler *Curl_getn_scheme_handler(const char *scheme,
size_t len);
#define CURL_DEFAULT_PROXY_PORT 1080 /* default proxy port unless specified */
#define CURL_DEFAULT_HTTPS_PROXY_PORT 443 /* default https proxy port unless

View File

@ -446,7 +446,7 @@ static CURLUcode parse_hostname_login(struct Curl_URL *u,
/* if this is a known scheme, get some details */
if(u->scheme)
h = Curl_builtin_scheme(u->scheme, CURL_ZERO_TERMINATED);
h = Curl_get_scheme_handler(u->scheme);
/* We could use the login information in the URL so extract it. Only parse
options if the handler says we should. Note that 'h' might be NULL! */
@ -1129,7 +1129,7 @@ static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
}
schemep = schemebuf;
if(!Curl_builtin_scheme(schemep, CURL_ZERO_TERMINATED) &&
if(!Curl_get_scheme_handler(schemep) &&
!(flags & CURLU_NON_SUPPORT_SCHEME)) {
result = CURLUE_UNSUPPORTED_SCHEME;
goto fail;
@ -1447,8 +1447,7 @@ CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
/* there's no stored port number, but asked to deliver
a default one for the scheme */
const struct Curl_handler *h =
Curl_builtin_scheme(u->scheme, CURL_ZERO_TERMINATED);
const struct Curl_handler *h = Curl_get_scheme_handler(u->scheme);
if(h) {
msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
ptr = portbuf;
@ -1457,8 +1456,7 @@ CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
else if(ptr && u->scheme) {
/* there is a stored port number, but ask to inhibit if
it matches the default one for the scheme */
const struct Curl_handler *h =
Curl_builtin_scheme(u->scheme, CURL_ZERO_TERMINATED);
const struct Curl_handler *h = Curl_get_scheme_handler(u->scheme);
if(h && (h->defport == u->portnum) &&
(flags & CURLU_NO_DEFAULT_PORT))
ptr = NULL;
@ -1503,7 +1501,7 @@ CURLUcode curl_url_get(const CURLU *u, CURLUPart what,
else
return CURLUE_NO_SCHEME;
h = Curl_builtin_scheme(scheme, CURL_ZERO_TERMINATED);
h = Curl_get_scheme_handler(scheme);
if(!port && (flags & CURLU_DEFAULT_PORT)) {
/* there's no stored port number, but asked to deliver
a default one for the scheme */
@ -1743,9 +1741,8 @@ CURLUcode curl_url_set(CURLU *u, CURLUPart what,
if((plen > MAX_SCHEME_LEN) || (plen < 1))
/* too long or too short */
return CURLUE_BAD_SCHEME;
if(!(flags & CURLU_NON_SUPPORT_SCHEME) &&
/* verify that it is a fine scheme */
!Curl_builtin_scheme(part, CURL_ZERO_TERMINATED))
/* verify that it is a fine scheme */
if(!(flags & CURLU_NON_SUPPORT_SCHEME) && !Curl_get_scheme_handler(part))
return CURLUE_UNSUPPORTED_SCHEME;
storep = &u->scheme;
urlencode = FALSE; /* never */