CORE: query for operations only once per provider (unless no_store is true)
[openssl.git] / crypto / http / http_lib.c
1 /*
2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/http.h>
11 #include <openssl/httperr.h>
12 #include <openssl/err.h>
13 #include <string.h>
14 #include "internal/cryptlib.h" /* for ossl_assert() */
15
16 #include "http_local.h"
17
18 /*
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.
21  */
22
23 int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
24                         char **ppath, int *pssl)
25 {
26     char *p, *buf;
27     char *host;
28     const char *port = OSSL_HTTP_PORT;
29     size_t https_len = strlen(OSSL_HTTPS_NAME);
30
31     if (!ossl_assert(https_len >= strlen(OSSL_HTTP_NAME)))
32         return 0;
33     if (url == NULL) {
34         HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
35         return 0;
36     }
37
38     if (phost != NULL)
39         *phost = NULL;
40     if (pport != NULL)
41         *pport = NULL;
42     if (ppath != NULL)
43         *ppath = NULL;
44     if (pssl != NULL)
45         *pssl = 0;
46
47     /* dup the buffer since we are going to mess with it */
48     if ((buf = OPENSSL_strdup(url)) == NULL)
49         goto err;
50
51     /* Check for initial colon */
52     p = strchr(buf, ':');
53     if (p == NULL || (size_t)(p - buf) > https_len) {
54         p = buf;
55     } else {
56         *(p++) = '\0';
57
58         if (strcmp(buf, OSSL_HTTPS_NAME) == 0) {
59             if (pssl != NULL)
60                 *pssl = 1;
61             port = OSSL_HTTPS_PORT;
62         } else if (strcmp(buf, OSSL_HTTP_NAME) != 0) {
63             goto parse_err;
64         }
65
66         /* Check for double slash */
67         if ((p[0] != '/') || (p[1] != '/'))
68             goto parse_err;
69         p += 2;
70     }
71     host = p;
72
73     /* Check for trailing part of path */
74     p = strchr(p, '/');
75     if (ppath != NULL && (*ppath = OPENSSL_strdup(p == NULL ? "/" : p)) == NULL)
76         goto err;
77     if (p != NULL)
78         *p = '\0'; /* Set start of path to 0 so hostname[:port] is valid */
79
80     p = host;
81     if (host[0] == '[') {
82         /* ipv6 literal */
83         host++;
84         p = strchr(host, ']');
85         if (p == NULL)
86             goto parse_err;
87         *p = '\0';
88         p++;
89     }
90
91     /* Look for optional ':' for port number */
92     if ((p = strchr(p, ':'))) {
93         *p = '\0';
94         port = p + 1;
95     }
96     if (phost != NULL && (*phost = OPENSSL_strdup(host)) == NULL)
97         goto err;
98     if (pport != NULL && (*pport = OPENSSL_strdup(port)) == NULL)
99         goto err;
100
101     OPENSSL_free(buf);
102     return 1;
103
104  parse_err:
105     HTTPerr(0, HTTP_R_ERROR_PARSING_URL);
106
107  err:
108     if (ppath != NULL) {
109         OPENSSL_free(*ppath);
110         *ppath = NULL;
111     }
112     if (pport != NULL) {
113         OPENSSL_free(*pport);
114         *pport = NULL;
115     }
116     if (phost != NULL) {
117         OPENSSL_free(*phost);
118         *phost = NULL;
119     }
120     OPENSSL_free(buf);
121     return 0;
122 }
123
124 int http_use_proxy(const char *no_proxy, const char *server)
125 {
126     size_t sl;
127     const char *found = NULL;
128
129     if (!ossl_assert(server != NULL))
130         return 0;
131     sl = strlen(server);
132
133     /*
134      * using environment variable names, both lowercase and uppercase variants,
135      * compatible with other HTTP client implementations like wget, curl and git
136      */
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);
143     while (found != NULL
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;
148 }
149
150 const char *http_adapt_proxy(const char *proxy, const char *no_proxy,
151                              const char *server, int use_ssl)
152 {
153     const int http_len = strlen(OSSL_HTTP_PREFIX);
154     const int https_len = strlen(OSSL_HTTPS_PREFIX);
155
156     /*
157      * using environment variable names, both lowercase and uppercase variants,
158      * compatible with other HTTP client implementations like wget, curl and git
159      */
160     if (proxy == NULL)
161         proxy = getenv(use_ssl ? "https_proxy" : "http_proxy");
162     if (proxy == NULL)
163         proxy = getenv(use_ssl ? OPENSSL_HTTP_PROXY :
164                        OPENSSL_HTTPS_PROXY);
165     if (proxy == NULL)
166         return NULL;
167
168     /* skip any leading "http://" or "https://" */
169     if (strncmp(proxy, OSSL_HTTP_PREFIX, http_len) == 0)
170         proxy += http_len;
171     else if (strncmp(proxy, OSSL_HTTPS_PREFIX, https_len) == 0)
172         proxy += https_len;
173
174     if (*proxy == '\0' || !http_use_proxy(no_proxy, server))
175         return NULL;
176     return proxy;
177 }