summaryrefslogtreecommitdiff
path: root/lib/setopt.c
diff options
context:
space:
mode:
authorPatrick Monnerat <patrick@monnerat.net>2022-09-15 13:30:09 +0200
committerDaniel Stenberg <daniel@haxx.se>2022-09-16 23:29:01 +0200
commit9d51329047952ebfc2b944b7448b8f87f9e6ed51 (patch)
tree65e16f03a3efce60410c79d751793260ca172e17 /lib/setopt.c
parent1bbffa08336d6dc647c45a3dbf7462174702bb88 (diff)
downloadcurl-9d51329047952ebfc2b944b7448b8f87f9e6ed51.tar.gz
setopt: use the handler table for protocol name to number conversions
This also returns error CURLE_UNSUPPORTED_PROTOCOL rather than CURLE_BAD_FUNCTION_ARGUMENT when a listed protocol name is not found. A new schemelen parameter is added to Curl_builtin_scheme() to support this extended use. Note that disabled protocols are not recognized anymore. Tests adapted accordingly. Closes #9472
Diffstat (limited to 'lib/setopt.c')
-rw-r--r--lib/setopt.c83
1 files changed, 17 insertions, 66 deletions
diff --git a/lib/setopt.c b/lib/setopt.c
index 7289a4e78..bc98d199f 100644
--- a/lib/setopt.c
+++ b/lib/setopt.c
@@ -148,85 +148,36 @@ static CURLcode setstropt_userpwd(char *option, char **userp, char **passwdp)
#define C_SSLVERSION_VALUE(x) (x & 0xffff)
#define C_SSLVERSION_MAX_VALUE(x) (x & 0xffff0000)
-static CURLcode protocol2num(char *str, curl_prot_t *val)
+static CURLcode protocol2num(const char *str, curl_prot_t *val)
{
- bool found_comma = FALSE;
- static struct scheme {
- const char *name;
- curl_prot_t bit;
- } const protos[] = {
- { "dict", CURLPROTO_DICT },
- { "file", CURLPROTO_FILE },
- { "ftp", CURLPROTO_FTP },
- { "ftps", CURLPROTO_FTPS },
- { "gopher", CURLPROTO_GOPHER },
- { "gophers", CURLPROTO_GOPHERS },
- { "http", CURLPROTO_HTTP },
- { "https", CURLPROTO_HTTPS },
- { "imap", CURLPROTO_IMAP },
- { "imaps", CURLPROTO_IMAPS },
- { "ldap", CURLPROTO_LDAP },
- { "ldaps", CURLPROTO_LDAPS },
- { "mqtt", CURLPROTO_MQTT },
- { "pop3", CURLPROTO_POP3 },
- { "pop3s", CURLPROTO_POP3S },
- { "rtmp", CURLPROTO_RTMP },
- { "rtmpe", CURLPROTO_RTMPE },
- { "rtmps", CURLPROTO_RTMPS },
- { "rtmpt", CURLPROTO_RTMPT },
- { "rtmpte", CURLPROTO_RTMPTE },
- { "rtmpts", CURLPROTO_RTMPTS },
- { "rtsp", CURLPROTO_RTSP },
- { "scp", CURLPROTO_SCP },
- { "sftp", CURLPROTO_SFTP },
- { "smb", CURLPROTO_SMB },
- { "smbs", CURLPROTO_SMBS },
- { "smtp", CURLPROTO_SMTP },
- { "smtps", CURLPROTO_SMTPS },
- { "telnet", CURLPROTO_TELNET },
- { "tftp", CURLPROTO_TFTP },
-#ifdef USE_WEBSOCKETS
- { "ws", CURLPROTO_WS },
- { "wss", CURLPROTO_WSS },
-#endif
- { NULL, 0 }
- };
-
if(!str)
return CURLE_BAD_FUNCTION_ARGUMENT;
- else if(curl_strequal(str, "all")) {
- *val = (curl_prot_t)~0;
+
+ if(curl_strequal(str, "all")) {
+ *val = ~(curl_prot_t) 0;
return CURLE_OK;
}
*val = 0;
do {
+ const char *token = str;
size_t tlen;
- struct scheme const *pp;
- char *token;
- token = strchr(str, ',');
- found_comma = token ? TRUE : FALSE;
- if(!token)
- token = strchr(str, '\0');
- tlen = token - str;
+
+ str = strchr(str, ',');
+ tlen = str? (size_t) (str - token): strlen(token);
if(tlen) {
- for(pp = protos; pp->name; pp++) {
- if((strlen(pp->name) == tlen) &&
- curl_strnequal(str, pp->name, tlen)) {
- *val |= pp->bit;
- break;
- }
- }
- if(!(pp->name))
- /* protocol name didn't match */
- return CURLE_BAD_FUNCTION_ARGUMENT;
+ const struct Curl_handler *h = Curl_builtin_scheme(token, tlen);
+
+ if(!h)
+ return CURLE_UNSUPPORTED_PROTOCOL;
+
+ *val |= h->protocol;
}
- if(found_comma)
- str = token + 1;
- } while(found_comma);
+ } while(str++);
+
if(!*val)
- /* no matching protocol */
+ /* no protocol listed */
return CURLE_BAD_FUNCTION_ARGUMENT;
return CURLE_OK;
}