summaryrefslogtreecommitdiffstats
path: root/crypto/http/http_lib.c
blob: be790bda9023958a5b062c3d29ab0a157d430f95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/http.h>
#include <openssl/httperr.h>
#include <openssl/err.h>
#include <string.h>
#include "internal/cryptlib.h" /* for ossl_assert() */

#include "http_local.h"

/*
 * Parse a URL and split it up into host, port and path components and
 * whether it indicates SSL/TLS. Return 1 on success, 0 on error.
 */

int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
                        int *pport_num, char **ppath, int *pssl)
{
    char *p, *buf;
    char *host, *host_end;
    const char *path, *port = OSSL_HTTP_PORT;
    long portnum = 80;

    if (phost != NULL)
        *phost = NULL;
    if (pport != NULL)
        *pport = NULL;
    if (ppath != NULL)
        *ppath = NULL;
    if (pssl != NULL)
        *pssl = 0;

    if (url == NULL) {
        HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }

    /* dup the buffer since we are going to mess with it */
    if ((buf = OPENSSL_strdup(url)) == NULL)
        goto err;

    /* check for optional prefix "http[s]://" */
    p = strstr(buf, "://");
    if (p == NULL) {
        p = buf;
    } else {
        *p = '\0'; /* p points to end of scheme name */
        if (strcmp(buf, OSSL_HTTPS_NAME) == 0) {
            if (pssl != NULL)
                *pssl = 1;
            port = OSSL_HTTPS_PORT;
            portnum = 443;
        } else if (strcmp(buf, OSSL_HTTP_NAME) != 0) {
            HTTPerr(0, HTTP_R_INVALID_URL_PREFIX);
            goto err;
        }
        p += 3;
    }
    host = p;

    /* parse host name/address as far as needed here */
    if (host[0] == '[') {
        /* ipv6 literal, which may include ':' */
        host++;
        host_end = strchr(host, ']');
        if (host_end == NULL)
            goto parse_err;
        *host_end++ = '\0';
    } else {
        host_end = strchr(host, ':'); /* look for start of optional port */
        if (host_end == NULL)
            host_end = strchr(host, '/'); /* look for start of optional path */
        if (host_end == NULL)
            /* the remaining string is just the hostname */
            host_end = host + strlen(host);
    }

    /* parse optional port specification starting with ':' */
    p = host_end;
    if (*p == ':') {
        port = ++p;
        if (pport_num == NULL) {
            p = strchr(port, '/');
            if (p == NULL)
                p = host_end + 1 + strlen(port);
        } else { /* make sure a numerical port value is given */
            portnum = strtol(port, &p, 10);
            if (p == port || (*p != '\0' && *p != '/'))
                goto parse_err;
            if (portnum <= 0 || portnum >= 65536) {
                HTTPerr(0, HTTP_R_INVALID_PORT_NUMBER);
                goto err;
            }
        }
    }
    *host_end = '\0';
    *p = '\0'; /* terminate port string */

    /* check for optional path at end of url starting with '/' */
    path = url + (p - buf);
    /* cannot use p + 1 because *p is '\0' and path must start with '/' */
    if (*path == '\0') {
        path = "/";
    } else if (*path != '/') {
        HTTPerr(0, HTTP_R_INVALID_URL_PATH);
        goto parse_err;
    }

    if (phost != NULL && (*phost = OPENSSL_strdup(host)) == NULL)
        goto err;
    if (pport != NULL && (*pport = OPENSSL_strdup(port)) == NULL)
        goto err;
    if (pport_num != NULL)
        *pport_num = (int)portnum;
    if (ppath != NULL && (*ppath = OPENSSL_strdup(path)) == NULL)
        goto err;

    OPENSSL_free(buf);
    return 1;

 parse_err:
    HTTPerr(0, HTTP_R_ERROR_PARSING_URL);

 err:
    if (ppath != NULL) {
        OPENSSL_free(*ppath);
        *ppath = NULL;
    }
    if (pport != NULL) {
        OPENSSL_free(*pport);
        *pport = NULL;
    }
    if (phost != NULL) {
        OPENSSL_free(*phost);
        *phost = NULL;
    }
    OPENSSL_free(buf);
    return 0;
}

int http_use_proxy(const char *no_proxy, const char *server)
{
    size_t sl;
    const char *found = NULL;

    if (!ossl_assert(server != NULL))
        return 0;
    sl = strlen(server);

    /*
     * using environment variable names, both lowercase and uppercase variants,
     * compatible with other HTTP client implementations like wget, curl and git
     */
    if (no_proxy == NULL)
        no_proxy = getenv("no_proxy");
    if (no_proxy == NULL)
        no_proxy = getenv(OPENSSL_NO_PROXY);
    if (no_proxy != NULL)
        found = strstr(no_proxy, server);
    while (found != NULL
           && ((found != no_proxy && found[-1] != ' ' && found[-1] != ',')
               || (found[sl] != '\0' && found[sl] != ' ' && found[sl] != ',')))
        found = strstr(found + 1, server);
    return found == NULL;
}

const char *http_adapt_proxy(const char *proxy, const char *no_proxy,
                             const char *server, int use_ssl)
{
    const int http_len = strlen(OSSL_HTTP_PREFIX);
    const int https_len = strlen(OSSL_HTTPS_PREFIX);

    /*
     * using environment variable names, both lowercase and uppercase variants,
     * compatible with other HTTP client implementations like wget, curl and git
     */
    if (proxy == NULL)
        proxy = getenv(use_ssl ? "https_proxy" : "http_proxy");
    if (proxy == NULL)
        proxy = getenv(use_ssl ? OPENSSL_HTTP_PROXY :
                       OPENSSL_HTTPS_PROXY);
    if (proxy == NULL)
        return NULL;

    /* skip any leading "http://" or "https://" */
    if (strncmp(proxy, OSSL_HTTP_PREFIX, http_len) == 0)
        proxy += http_len;
    else if (strncmp(proxy, OSSL_HTTPS_PREFIX, https_len) == 0)
        proxy += https_len;

    if (*proxy == '\0' || !http_use_proxy(no_proxy, server))
        return NULL;
    return proxy;
}