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>
16 * Parse a URL and split it up into host, port and path components and
17 * whether it indicates SSL/TLS. Return 1 on success, 0 on error.
20 int OSSL_HTTP_parse_url(const char *url
, char **phost
, char **pport
,
21 char **ppath
, int *pssl
)
28 HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER
);
41 /* dup the buffer since we are going to mess with it */
42 if ((buf
= OPENSSL_strdup(url
)) == NULL
)
45 /* Check for initial colon */
47 if (p
== NULL
|| p
- buf
> 5 /* strlen("https") */) {
52 if (strcmp(buf
, "https") == 0) {
56 } else if (strcmp(buf
, "http") != 0) {
60 /* Check for double slash */
61 if ((p
[0] != '/') || (p
[1] != '/'))
67 /* Check for trailing part of path */
69 if (ppath
!= NULL
&& (*ppath
= OPENSSL_strdup(p
== NULL
? "/" : p
)) == NULL
)
72 *p
= '\0'; /* Set start of path to 0 so hostname[:port] is valid */
78 p
= strchr(host
, ']');
85 /* Look for optional ':' for port number */
86 if ((p
= strchr(p
, ':'))) {
90 if (phost
!= NULL
&& (*phost
= OPENSSL_strdup(host
)) == NULL
)
92 if (pport
!= NULL
&& (*pport
= OPENSSL_strdup(port
)) == NULL
)
99 HTTPerr(0, HTTP_R_ERROR_PARSING_URL
);
103 OPENSSL_free(*ppath
);
107 OPENSSL_free(*pport
);
111 OPENSSL_free(*phost
);