2 * Copyright 2001-2020 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 <openssl/http.h>
11 #include <openssl/httperr.h>
12 #include <openssl/err.h>
14 #include "internal/cryptlib.h" /* for ossl_assert() */
16 #include "http_local.h"
19 * Parse a URL and split it up into host, port and path components and
20 * whether it indicates SSL/TLS. Return 1 on success, 0 on error.
23 int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
24 char **ppath, int *pssl)
28 const char *port = OSSL_HTTP_PORT;
29 size_t https_len = strlen(OSSL_HTTPS_NAME);
31 if (!ossl_assert(https_len >= strlen(OSSL_HTTP_NAME)))
34 HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
47 /* dup the buffer since we are going to mess with it */
48 if ((buf = OPENSSL_strdup(url)) == NULL)
51 /* Check for initial colon */
53 if (p == NULL || (size_t)(p - buf) > https_len) {
58 if (strcmp(buf, OSSL_HTTPS_NAME) == 0) {
61 port = OSSL_HTTPS_PORT;
62 } else if (strcmp(buf, OSSL_HTTP_NAME) != 0) {
66 /* Check for double slash */
67 if ((p[0] != '/') || (p[1] != '/'))
73 /* Check for trailing part of path */
75 if (ppath != NULL && (*ppath = OPENSSL_strdup(p == NULL ? "/" : p)) == NULL)
78 *p = '\0'; /* Set start of path to 0 so hostname[:port] is valid */
84 p = strchr(host, ']');
91 /* Look for optional ':' for port number */
92 if ((p = strchr(p, ':'))) {
96 if (phost != NULL && (*phost = OPENSSL_strdup(host)) == NULL)
98 if (pport != NULL && (*pport = OPENSSL_strdup(port)) == NULL)
105 HTTPerr(0, HTTP_R_ERROR_PARSING_URL);
109 OPENSSL_free(*ppath);
113 OPENSSL_free(*pport);
117 OPENSSL_free(*phost);
124 int http_use_proxy(const char *no_proxy, const char *server)
127 const char *found = NULL;
129 if (!ossl_assert(server != NULL))
134 * using environment variable names, both lowercase and uppercase variants,
135 * compatible with other HTTP client implementations like wget, curl and git
137 if (no_proxy == NULL)
138 no_proxy = getenv("no_proxy");
139 if (no_proxy == NULL)
140 no_proxy = getenv(OPENSSL_NO_PROXY);
141 if (no_proxy != NULL)
142 found = strstr(no_proxy, server);
144 && ((found != no_proxy && found[-1] != ' ' && found[-1] != ',')
145 || (found[sl] != '\0' && found[sl] != ' ' && found[sl] != ',')))
146 found = strstr(found + 1, server);
147 return found == NULL;
150 const char *http_adapt_proxy(const char *proxy, const char *no_proxy,
151 const char *server, int use_ssl)
153 const int http_len = strlen(OSSL_HTTP_PREFIX);
154 const int https_len = strlen(OSSL_HTTPS_PREFIX);
157 * using environment variable names, both lowercase and uppercase variants,
158 * compatible with other HTTP client implementations like wget, curl and git
161 proxy = getenv(use_ssl ? "https_proxy" : "http_proxy");
163 proxy = getenv(use_ssl ? OPENSSL_HTTP_PROXY :
164 OPENSSL_HTTPS_PROXY);
168 /* skip any leading "http://" or "https://" */
169 if (strncmp(proxy, OSSL_HTTP_PREFIX, http_len) == 0)
171 else if (strncmp(proxy, OSSL_HTTPS_PREFIX, https_len) == 0)
174 if (*proxy == '\0' || !http_use_proxy(no_proxy, server))