2 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <stdio.h> /* for sscanf() */
12 #include <openssl/http.h>
13 #include <openssl/httperr.h>
14 #include <openssl/bio.h> /* for BIO_snprintf() */
15 #include <openssl/err.h>
16 #include "internal/cryptlib.h" /* for ossl_assert() */
18 static void init_pstring(char **pstr)
25 static void init_pint(int *pint)
32 static int copy_substring(char **dest, const char *start, const char *end)
35 || (*dest = OPENSSL_strndup(start, end - start)) != NULL;
38 static void free_pstring(char **pstr)
46 int OSSL_parse_url(const char *url, char **pscheme, char **puser, char **phost,
47 char **pport, int *pport_num,
48 char **ppath, char **pquery, char **pfrag)
51 const char *scheme, *scheme_end;
52 const char *user, *user_end;
53 const char *host, *host_end;
54 const char *port, *port_end;
56 const char *path, *path_end;
57 const char *query, *query_end;
58 const char *frag, *frag_end;
60 init_pstring(pscheme);
70 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
74 /* check for optional prefix "<scheme>://" */
75 scheme = scheme_end = url;
76 p = strstr(url, "://");
81 if (scheme_end == scheme)
86 /* parse optional "userinfo@" */
87 user = user_end = host = p;
88 host = strchr(p, '@');
94 /* parse hostname/address as far as needed here */
96 /* IPv6 literal, which may include ':' */
97 host_end = strchr(host + 1, ']');
102 /* look for start of optional port, path, query, or fragment */
103 host_end = strchr(host, ':');
104 if (host_end == NULL)
105 host_end = strchr(host, '/');
106 if (host_end == NULL)
107 host_end = strchr(host, '?');
108 if (host_end == NULL)
109 host_end = strchr(host, '#');
110 if (host_end == NULL) /* the remaining string is just the hostname */
111 host_end = host + strlen(host);
115 /* parse optional port specification starting with ':' */
116 port = "0"; /* default */
119 /* remaining port spec handling is also done for the default values */
120 /* make sure a decimal port number is given */
121 if (sscanf(port, "%u", &portnum) <= 0 || portnum > 65535) {
122 ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INVALID_PORT_NUMBER, "%s", port);
125 for (port_end = port; '0' <= *port_end && *port_end <= '9'; port_end++)
127 if (port == p) /* port was given explicitly */
128 p += port_end - port;
130 /* check for optional path starting with '/' or '?'. Else must start '#' */
132 if (*path != '\0' && *path != '/' && *path != '?' && *path != '#') {
133 ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_PATH);
136 path_end = query = query_end = frag = frag_end = path + strlen(path);
138 /* parse optional "?query" */
139 tmp = strchr(p, '?');
142 if (pquery != NULL) {
148 /* parse optional "#fragment" */
149 tmp = strchr(p, '#');
151 if (query == path_end) /* we did not record a query component */
157 if (!copy_substring(pscheme, scheme, scheme_end)
158 || !copy_substring(phost, host, host_end)
159 || !copy_substring(pport, port, port_end)
160 || !copy_substring(puser, user, user_end)
161 || !copy_substring(pquery, query, query_end)
162 || !copy_substring(pfrag, frag, frag_end))
164 if (pport_num != NULL)
165 *pport_num = (int)portnum;
167 if (!copy_substring(ppath, path, path_end))
169 } else if (ppath != NULL) { /* must prepend '/' */
170 size_t buflen = 1 + path_end - path + 1;
172 if ((*ppath = OPENSSL_malloc(buflen)) == NULL)
174 BIO_snprintf(*ppath, buflen, "/%s", path);
179 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_URL);
182 free_pstring(pscheme);
187 free_pstring(pquery);
192 int OSSL_HTTP_parse_url(const char *url, int *pssl, char **puser, char **phost,
193 char **pport, int *pport_num,
194 char **ppath, char **pquery, char **pfrag)
197 int ssl = 0, portnum;
202 if (!OSSL_parse_url(url, &scheme, puser, phost, &port, pport_num,
203 ppath, pquery, pfrag))
206 /* check for optional HTTP scheme "http[s]" */
207 if (strcmp(scheme, OSSL_HTTPS_NAME) == 0) {
211 } else if (*scheme != '\0' && strcmp(scheme, OSSL_HTTP_NAME) != 0) {
212 ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_SCHEME);
213 OPENSSL_free(scheme);
217 OPENSSL_free(scheme);
219 if (strcmp(port, "0") == 0) {
220 /* set default port */
222 port = ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
223 if (!ossl_assert(sscanf(port, "%d", &portnum) == 1))
225 if (pport_num != NULL)
226 *pport_num = portnum;
228 *pport = OPENSSL_strdup(port);
244 free_pstring(pquery);
249 /* Respect no_proxy, taking default value from environment variable(s) */
250 static int use_proxy(const char *no_proxy, const char *server)
253 const char *found = NULL;
255 if (!ossl_assert(server != NULL))
260 * using environment variable names, both lowercase and uppercase variants,
261 * compatible with other HTTP client implementations like wget, curl and git
263 if (no_proxy == NULL)
264 no_proxy = ossl_safe_getenv("no_proxy");
265 if (no_proxy == NULL)
266 no_proxy = ossl_safe_getenv(OPENSSL_NO_PROXY);
268 if (no_proxy != NULL)
269 found = strstr(no_proxy, server);
271 && ((found != no_proxy && found[-1] != ' ' && found[-1] != ',')
272 || (found[sl] != '\0' && found[sl] != ' ' && found[sl] != ',')))
273 found = strstr(found + 1, server);
274 return found == NULL;
277 /* Take default value from environment variable(s), respect no_proxy */
278 const char *OSSL_HTTP_adapt_proxy(const char *proxy, const char *no_proxy,
279 const char *server, int use_ssl)
282 * using environment variable names, both lowercase and uppercase variants,
283 * compatible with other HTTP client implementations like wget, curl and git
286 proxy = ossl_safe_getenv(use_ssl ? "https_proxy" : "http_proxy");
288 proxy = ossl_safe_getenv(use_ssl ? OPENSSL_HTTP_PROXY : OPENSSL_HTTPS_PROXY);
290 if (proxy == NULL || *proxy == '\0' || !use_proxy(no_proxy, server))