]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/http/http_lib.c
crypto/*: Fix various typos, repeated words, align some spelling to LDP.
[thirdparty/openssl.git] / crypto / http / http_lib.c
CommitLineData
29f178bd 1/*
8020d79b 2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
29f178bd
DDO
3 *
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
8 */
9
bf6aeeb4
TM
10#include <stdio.h> /* for sscanf() */
11#include <string.h>
29f178bd
DDO
12#include <openssl/http.h>
13#include <openssl/httperr.h>
ebdb5ccc 14#include <openssl/bio.h> /* for BIO_snprintf() */
29f178bd 15#include <openssl/err.h>
4b1fe471 16#include "internal/cryptlib.h" /* for ossl_assert() */
29f178bd 17
7932982b
DDO
18static void init_pstring(char **pstr)
19{
20 if (pstr != NULL) {
21 *pstr = NULL;
22 }
23}
24
25static int copy_substring(char **dest, const char *start, const char *end)
26{
27 return dest == NULL
28 || (*dest = OPENSSL_strndup(start, end - start)) != NULL;
29}
29f178bd 30
7932982b 31static void free_pstring(char **pstr)
29f178bd 32{
7932982b
DDO
33 if (pstr != NULL) {
34 OPENSSL_free(*pstr);
35 *pstr = NULL;
36 }
37}
38
d546e8e2
DDO
39int OSSL_parse_url(const char *url, char **pscheme, char **puser, char **phost,
40 char **pport, int *pport_num,
41 char **ppath, char **pquery, char **pfrag)
7932982b
DDO
42{
43 const char *p, *tmp;
d546e8e2 44 const char *scheme, *scheme_end;
7932982b
DDO
45 const char *user, *user_end;
46 const char *host, *host_end;
d546e8e2 47 const char *port, *port_end;
7932982b
DDO
48 unsigned int portnum;
49 const char *path, *path_end;
50 const char *query, *query_end;
51 const char *frag, *frag_end;
52
d546e8e2 53 init_pstring(pscheme);
7932982b
DDO
54 init_pstring(puser);
55 init_pstring(phost);
56 init_pstring(pport);
57 init_pstring(ppath);
58 init_pstring(pfrag);
59 init_pstring(pquery);
29f178bd 60
d7fcee3b 61 if (url == NULL) {
9311d0c4 62 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
d7fcee3b
DDO
63 return 0;
64 }
65
d546e8e2
DDO
66 /* check for optional prefix "<scheme>://" */
67 scheme = scheme_end = url;
7932982b 68 p = strstr(url, "://");
d7fcee3b 69 if (p == NULL) {
7932982b 70 p = url;
d546e8e2
DDO
71 } else {
72 scheme_end = p;
73 if (scheme_end == scheme)
74 goto parse_err;
7932982b 75 p += strlen("://");
29f178bd 76 }
7932982b
DDO
77
78 /* parse optional "userinfo@" */
79 user = user_end = host = p;
80 host = strchr(p, '@');
81 if (host != NULL)
82 user_end = host++;
83 else
84 host = p;
29f178bd 85
c7340583 86 /* parse hostname/address as far as needed here */
29f178bd 87 if (host[0] == '[') {
c7340583 88 /* IPv6 literal, which may include ':' */
23183791 89 host_end = strchr(host + 1, ']');
d7fcee3b 90 if (host_end == NULL)
29f178bd 91 goto parse_err;
23183791 92 p = ++host_end;
d7fcee3b 93 } else {
7932982b
DDO
94 /* look for start of optional port, path, query, or fragment */
95 host_end = strchr(host, ':');
96 if (host_end == NULL)
97 host_end = strchr(host, '/');
d7fcee3b 98 if (host_end == NULL)
7932982b 99 host_end = strchr(host, '?');
d7fcee3b 100 if (host_end == NULL)
7932982b
DDO
101 host_end = strchr(host, '#');
102 if (host_end == NULL) /* the remaining string is just the hostname */
d7fcee3b 103 host_end = host + strlen(host);
7932982b 104 p = host_end;
29f178bd
DDO
105 }
106
d7fcee3b 107 /* parse optional port specification starting with ':' */
d546e8e2 108 port = "0"; /* default */
7932982b 109 if (*p == ':')
d7fcee3b 110 port = ++p;
7932982b
DDO
111 /* remaining port spec handling is also done for the default values */
112 /* make sure a decimal port number is given */
d546e8e2 113 if (!sscanf(port, "%u", &portnum) || portnum > 65535) {
79a2bccd 114 ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INVALID_PORT_NUMBER, "%s", port);
7932982b 115 goto err;
d7fcee3b 116 }
7932982b
DDO
117 for (port_end = port; '0' <= *port_end && *port_end <= '9'; port_end++)
118 ;
119 if (port == p) /* port was given explicitly */
120 p += port_end - port;
121
122 /* check for optional path starting with '/' or '?'. Else must start '#' */
123 path = p;
124 if (*path != '\0' && *path != '/' && *path != '?' && *path != '#') {
9311d0c4 125 ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_PATH);
d7fcee3b 126 goto parse_err;
29f178bd 127 }
7932982b
DDO
128 path_end = query = query_end = frag = frag_end = path + strlen(path);
129
130 /* parse optional "?query" */
131 tmp = strchr(p, '?');
132 if (tmp != NULL) {
133 p = tmp;
134 if (pquery != NULL) {
135 path_end = p;
136 query = p + 1;
137 }
138 }
d7fcee3b 139
7932982b
DDO
140 /* parse optional "#fragment" */
141 tmp = strchr(p, '#');
142 if (tmp != NULL) {
143 if (query == path_end) /* we did not record a query component */
144 path_end = tmp;
145 query_end = tmp;
146 frag = tmp + 1;
147 }
148
d546e8e2
DDO
149 if (!copy_substring(pscheme, scheme, scheme_end)
150 || !copy_substring(phost, host, host_end)
7932982b
DDO
151 || !copy_substring(pport, port, port_end)
152 || !copy_substring(puser, user, user_end)
153 || !copy_substring(pquery, query, query_end)
154 || !copy_substring(pfrag, frag, frag_end))
29f178bd 155 goto err;
d7fcee3b
DDO
156 if (pport_num != NULL)
157 *pport_num = (int)portnum;
7932982b
DDO
158 if (*path == '/') {
159 if (!copy_substring(ppath, path, path_end))
160 goto err;
161 } else if (ppath != NULL) { /* must prepend '/' */
162 size_t buflen = 1 + path_end - path + 1;
29f178bd 163
7932982b
DDO
164 if ((*ppath = OPENSSL_malloc(buflen)) == NULL)
165 goto err;
ebdb5ccc 166 BIO_snprintf(*ppath, buflen, "/%s", path);
7932982b 167 }
29f178bd
DDO
168 return 1;
169
170 parse_err:
9311d0c4 171 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_URL);
29f178bd
DDO
172
173 err:
d546e8e2
DDO
174 free_pstring(pscheme);
175 free_pstring(puser);
7932982b
DDO
176 free_pstring(phost);
177 free_pstring(pport);
178 free_pstring(ppath);
179 free_pstring(pquery);
180 free_pstring(pfrag);
29f178bd
DDO
181 return 0;
182}
afe554c2 183
d546e8e2
DDO
184int OSSL_HTTP_parse_url(const char *url, int *pssl, char **puser, char **phost,
185 char **pport, int *pport_num,
186 char **ppath, char **pquery, char **pfrag)
187{
188 char *scheme, *port;
189 int ssl = 0, portnum;
190
191 init_pstring(pport);
192 if (pssl != NULL)
193 *pssl = 0;
194 if (!OSSL_parse_url(url, &scheme, puser, phost, &port, pport_num,
195 ppath, pquery, pfrag))
196 return 0;
197
198 /* check for optional HTTP scheme "http[s]" */
199 if (strcmp(scheme, OSSL_HTTPS_NAME) == 0) {
200 ssl = 1;
201 if (pssl != NULL)
202 *pssl = ssl;
203 } else if (*scheme != '\0' && strcmp(scheme, OSSL_HTTP_NAME) != 0) {
204 ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_SCHEME);
205 OPENSSL_free(scheme);
206 OPENSSL_free(port);
207 goto err;
208 }
209 OPENSSL_free(scheme);
210
211 if (strcmp(port, "0") == 0) {
212 /* set default port */
213 OPENSSL_free(port);
214 port = ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
215 if (!ossl_assert(sscanf(port, "%d", &portnum) == 1))
216 goto err;
217 if (pport_num != NULL)
218 *pport_num = portnum;
219 if (pport != NULL) {
220 *pport = OPENSSL_strdup(port);
221 if (*pport == NULL)
222 goto err;
223 }
224 } else {
225 if (pport != NULL)
226 *pport = port;
227 else
228 OPENSSL_free(port);
229 }
230 return 1;
231
232 err:
233 free_pstring(puser);
234 free_pstring(phost);
235 free_pstring(ppath);
236 free_pstring(pquery);
237 free_pstring(pfrag);
238 return 0;
239}
240
79a2bccd 241/* Respect no_proxy, taking default value from environment variable(s) */
ab9d67ef 242static int use_proxy(const char *no_proxy, const char *server)
afe554c2 243{
4b1fe471 244 size_t sl;
afe554c2
DDO
245 const char *found = NULL;
246
4b1fe471
DDO
247 if (!ossl_assert(server != NULL))
248 return 0;
249 sl = strlen(server);
250
251 /*
252 * using environment variable names, both lowercase and uppercase variants,
253 * compatible with other HTTP client implementations like wget, curl and git
254 */
afe554c2
DDO
255 if (no_proxy == NULL)
256 no_proxy = getenv("no_proxy");
257 if (no_proxy == NULL)
4b1fe471 258 no_proxy = getenv(OPENSSL_NO_PROXY);
79a2bccd 259
afe554c2
DDO
260 if (no_proxy != NULL)
261 found = strstr(no_proxy, server);
262 while (found != NULL
263 && ((found != no_proxy && found[-1] != ' ' && found[-1] != ',')
264 || (found[sl] != '\0' && found[sl] != ' ' && found[sl] != ',')))
265 found = strstr(found + 1, server);
266 return found == NULL;
267}
268
79a2bccd 269/* Take default value from environment variable(s), respect no_proxy */
ab9d67ef 270const char *OSSL_HTTP_adapt_proxy(const char *proxy, const char *no_proxy,
9500c823 271 const char *server, int use_ssl)
afe554c2 272{
4b1fe471
DDO
273 /*
274 * using environment variable names, both lowercase and uppercase variants,
275 * compatible with other HTTP client implementations like wget, curl and git
276 */
afe554c2
DDO
277 if (proxy == NULL)
278 proxy = getenv(use_ssl ? "https_proxy" : "http_proxy");
279 if (proxy == NULL)
4b1fe471
DDO
280 proxy = getenv(use_ssl ? OPENSSL_HTTP_PROXY :
281 OPENSSL_HTTPS_PROXY);
4b1fe471 282
ab9d67ef 283 if (proxy == NULL || *proxy == '\0' || !use_proxy(no_proxy, server))
4b1fe471 284 return NULL;
afe554c2
DDO
285 return proxy;
286}