2018-08-05 17:51:07 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2022-01-02 05:33:56 +08:00
|
|
|
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2018-08-05 17:51:07 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2018-08-05 17:51:07 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2018-08-05 17:51:07 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "urlapi-int.h"
|
|
|
|
#include "strcase.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "escape.h"
|
|
|
|
#include "curl_ctype.h"
|
2019-09-05 05:49:30 +08:00
|
|
|
#include "inet_pton.h"
|
2022-01-02 05:33:56 +08:00
|
|
|
#include "inet_ntop.h"
|
2022-09-01 16:16:24 +08:00
|
|
|
#include "strdup.h"
|
2022-12-26 17:58:37 +08:00
|
|
|
#include "idn.h"
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2018-10-01 21:59:24 +08:00
|
|
|
/* MSDOS/Windows style drive prefix, eg c: in c:foo */
|
|
|
|
#define STARTS_WITH_DRIVE_PREFIX(str) \
|
|
|
|
((('a' <= str[0] && str[0] <= 'z') || \
|
|
|
|
('A' <= str[0] && str[0] <= 'Z')) && \
|
|
|
|
(str[1] == ':'))
|
|
|
|
|
|
|
|
/* MSDOS/Windows style drive prefix, optionally with
|
|
|
|
* a '|' instead of ':', followed by a slash or NUL */
|
|
|
|
#define STARTS_WITH_URL_DRIVE_PREFIX(str) \
|
|
|
|
((('a' <= (str)[0] && (str)[0] <= 'z') || \
|
|
|
|
('A' <= (str)[0] && (str)[0] <= 'Z')) && \
|
|
|
|
((str)[1] == ':' || (str)[1] == '|') && \
|
|
|
|
((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
|
|
|
|
|
2021-11-23 21:16:38 +08:00
|
|
|
/* scheme is not URL encoded, the longest libcurl supported ones are... */
|
|
|
|
#define MAX_SCHEME_LEN 40
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/* Internal representation of CURLU. Point to URL-encoded strings. */
|
|
|
|
struct Curl_URL {
|
|
|
|
char *scheme;
|
|
|
|
char *user;
|
|
|
|
char *password;
|
|
|
|
char *options; /* IMAP only? */
|
|
|
|
char *host;
|
2019-05-03 19:18:12 +08:00
|
|
|
char *zoneid; /* for numerical IPv6 addresses */
|
2018-08-05 17:51:07 +08:00
|
|
|
char *port;
|
|
|
|
char *path;
|
|
|
|
char *query;
|
|
|
|
char *fragment;
|
|
|
|
long portnum; /* the numerical version */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEFAULT_SCHEME "https"
|
|
|
|
|
|
|
|
static void free_urlhandle(struct Curl_URL *u)
|
|
|
|
{
|
|
|
|
free(u->scheme);
|
|
|
|
free(u->user);
|
|
|
|
free(u->password);
|
|
|
|
free(u->options);
|
|
|
|
free(u->host);
|
2019-05-03 19:18:12 +08:00
|
|
|
free(u->zoneid);
|
2018-08-05 17:51:07 +08:00
|
|
|
free(u->port);
|
|
|
|
free(u->path);
|
|
|
|
free(u->query);
|
|
|
|
free(u->fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the separator at the end of the host name, or the '?' in cases like
|
|
|
|
* http://www.url.com?id=2380
|
|
|
|
*/
|
|
|
|
static const char *find_host_sep(const char *url)
|
|
|
|
{
|
|
|
|
const char *sep;
|
|
|
|
const char *query;
|
|
|
|
|
|
|
|
/* Find the start of the hostname */
|
|
|
|
sep = strstr(url, "//");
|
|
|
|
if(!sep)
|
|
|
|
sep = url;
|
|
|
|
else
|
|
|
|
sep += 2;
|
|
|
|
|
|
|
|
query = strchr(sep, '?');
|
|
|
|
sep = strchr(sep, '/');
|
|
|
|
|
|
|
|
if(!sep)
|
|
|
|
sep = url + strlen(url);
|
|
|
|
|
|
|
|
if(!query)
|
|
|
|
query = url + strlen(url);
|
|
|
|
|
|
|
|
return sep < query ? sep : query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-10-18 21:54:06 +08:00
|
|
|
* Decide in an encoding-independent manner whether a character in a URL must
|
|
|
|
* be escaped. This is used in urlencode_str().
|
2018-08-05 17:51:07 +08:00
|
|
|
*/
|
|
|
|
static bool urlchar_needs_escaping(int c)
|
|
|
|
{
|
2021-05-31 14:59:24 +08:00
|
|
|
return !(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c));
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2022-10-18 21:54:06 +08:00
|
|
|
/* urlencode_str() writes data into an output dynbuf and URL-encodes the
|
|
|
|
* spaces in the source URL accordingly.
|
2022-09-01 16:16:24 +08:00
|
|
|
*
|
2018-08-05 17:51:07 +08:00
|
|
|
* URL encoding should be skipped for host names, otherwise IDN resolution
|
|
|
|
* will fail.
|
|
|
|
*/
|
2022-10-18 21:54:06 +08:00
|
|
|
static CURLUcode urlencode_str(struct dynbuf *o, const char *url,
|
|
|
|
size_t len, bool relative,
|
|
|
|
bool query)
|
2018-08-05 17:51:07 +08:00
|
|
|
{
|
|
|
|
/* we must add this with whitespace-replacing */
|
2022-10-18 21:54:06 +08:00
|
|
|
bool left = !query;
|
2018-08-05 17:51:07 +08:00
|
|
|
const unsigned char *iptr;
|
|
|
|
const unsigned char *host_sep = (const unsigned char *) url;
|
|
|
|
|
|
|
|
if(!relative)
|
|
|
|
host_sep = (const unsigned char *) find_host_sep(url);
|
|
|
|
|
|
|
|
for(iptr = (unsigned char *)url; /* read from here */
|
2022-10-18 21:54:06 +08:00
|
|
|
len; iptr++, len--) {
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
if(iptr < host_sep) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_addn(o, iptr, 1))
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:00:52 +08:00
|
|
|
if(*iptr == ' ') {
|
2018-08-05 17:51:07 +08:00
|
|
|
if(left) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_addn(o, "%20", 3))
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(Curl_dyn_addn(o, "+", 1))
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
2021-09-29 16:00:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*iptr == '?')
|
|
|
|
left = FALSE;
|
|
|
|
|
|
|
|
if(urlchar_needs_escaping(*iptr)) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_addf(o, "%%%02x", *iptr))
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(Curl_dyn_addn(o, iptr, 1))
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
return CURLUE_OK;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-09-01 16:16:24 +08:00
|
|
|
* Returns the length of the scheme if the given URL is absolute (as opposed
|
|
|
|
* to relative). Stores the scheme in the buffer if TRUE and 'buf' is
|
|
|
|
* non-NULL. The buflen must be larger than MAX_SCHEME_LEN if buf is set.
|
2022-09-14 15:18:30 +08:00
|
|
|
*
|
|
|
|
* If 'guess_scheme' is TRUE, it means the URL might be provided without
|
|
|
|
* scheme.
|
2018-08-05 17:51:07 +08:00
|
|
|
*/
|
2022-09-14 15:18:30 +08:00
|
|
|
size_t Curl_is_absolute_url(const char *url, char *buf, size_t buflen,
|
|
|
|
bool guess_scheme)
|
2018-08-05 17:51:07 +08:00
|
|
|
{
|
2022-05-05 08:22:19 +08:00
|
|
|
int i;
|
2021-11-23 21:16:38 +08:00
|
|
|
DEBUGASSERT(!buf || (buflen > MAX_SCHEME_LEN));
|
|
|
|
(void)buflen; /* only used in debug-builds */
|
|
|
|
if(buf)
|
2021-11-22 19:47:56 +08:00
|
|
|
buf[0] = 0; /* always leave a defined value in buf */
|
2018-10-01 21:59:24 +08:00
|
|
|
#ifdef WIN32
|
2022-09-14 15:18:30 +08:00
|
|
|
if(guess_scheme && STARTS_WITH_DRIVE_PREFIX(url))
|
2022-09-01 16:16:24 +08:00
|
|
|
return 0;
|
2018-10-01 21:59:24 +08:00
|
|
|
#endif
|
2021-11-23 21:16:38 +08:00
|
|
|
for(i = 0; i < MAX_SCHEME_LEN; ++i) {
|
2018-08-05 17:51:07 +08:00
|
|
|
char s = url[i];
|
2021-11-22 19:47:56 +08:00
|
|
|
if(s && (ISALNUM(s) || (s == '+') || (s == '-') || (s == '.') )) {
|
|
|
|
/* RFC 3986 3.1 explains:
|
|
|
|
scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
|
|
|
|
*/
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
2021-11-22 19:47:56 +08:00
|
|
|
else {
|
2018-08-05 17:51:07 +08:00
|
|
|
break;
|
2021-11-22 19:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-14 15:18:30 +08:00
|
|
|
if(i && (url[i] == ':') && ((url[i + 1] == '/') || !guess_scheme)) {
|
|
|
|
/* If this does not guess scheme, the scheme always ends with the colon so
|
|
|
|
that this also detects data: URLs etc. In guessing mode, data: could
|
|
|
|
be the host name "data" with a specified port number. */
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
/* the length of the scheme is the name part only */
|
|
|
|
size_t len = i;
|
2021-11-22 19:47:56 +08:00
|
|
|
if(buf) {
|
|
|
|
buf[i] = 0;
|
|
|
|
while(i--) {
|
2022-09-06 06:17:36 +08:00
|
|
|
buf[i] = Curl_raw_tolower(url[i]);
|
2021-11-22 19:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
return len;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
return 0;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Concatenate a relative URL to a base URL making it absolute.
|
|
|
|
* URL-encodes any spaces.
|
|
|
|
* The returned pointer must be freed by the caller unless NULL
|
|
|
|
* (returns NULL on out of memory).
|
2022-09-01 16:16:24 +08:00
|
|
|
*
|
|
|
|
* Note that this function destroys the 'base' string.
|
2018-08-05 17:51:07 +08:00
|
|
|
*/
|
2022-09-01 16:16:24 +08:00
|
|
|
static char *concat_url(char *base, const char *relurl)
|
2018-08-05 17:51:07 +08:00
|
|
|
{
|
|
|
|
/***
|
|
|
|
TRY to append this new path to the old URL
|
|
|
|
to the right of the host part. Oh crap, this is doomed to cause
|
|
|
|
problems in the future...
|
|
|
|
*/
|
2022-09-01 16:16:24 +08:00
|
|
|
struct dynbuf newest;
|
2018-08-05 17:51:07 +08:00
|
|
|
char *protsep;
|
|
|
|
char *pathsep;
|
|
|
|
bool host_changed = FALSE;
|
|
|
|
const char *useurl = relurl;
|
|
|
|
|
|
|
|
/* protsep points to the start of the host name */
|
2022-09-01 16:16:24 +08:00
|
|
|
protsep = strstr(base, "//");
|
2018-08-05 17:51:07 +08:00
|
|
|
if(!protsep)
|
2022-09-01 16:16:24 +08:00
|
|
|
protsep = base;
|
2018-08-05 17:51:07 +08:00
|
|
|
else
|
|
|
|
protsep += 2; /* pass the slashes */
|
|
|
|
|
|
|
|
if('/' != relurl[0]) {
|
|
|
|
int level = 0;
|
|
|
|
|
|
|
|
/* First we need to find out if there's a ?-letter in the URL,
|
|
|
|
and cut it and the right-side of that off */
|
|
|
|
pathsep = strchr(protsep, '?');
|
|
|
|
if(pathsep)
|
|
|
|
*pathsep = 0;
|
|
|
|
|
|
|
|
/* we have a relative path to append to the last slash if there's one
|
|
|
|
available, or if the new URL is just a query string (starts with a
|
|
|
|
'?') we append the new one at the end of the entire currently worked
|
|
|
|
out URL */
|
|
|
|
if(useurl[0] != '?') {
|
|
|
|
pathsep = strrchr(protsep, '/');
|
|
|
|
if(pathsep)
|
|
|
|
*pathsep = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there's any slash after the host name, and if so, remember
|
|
|
|
that position instead */
|
|
|
|
pathsep = strchr(protsep, '/');
|
|
|
|
if(pathsep)
|
|
|
|
protsep = pathsep + 1;
|
|
|
|
else
|
|
|
|
protsep = NULL;
|
|
|
|
|
|
|
|
/* now deal with one "./" or any amount of "../" in the newurl
|
|
|
|
and act accordingly */
|
|
|
|
|
|
|
|
if((useurl[0] == '.') && (useurl[1] == '/'))
|
|
|
|
useurl += 2; /* just skip the "./" */
|
|
|
|
|
|
|
|
while((useurl[0] == '.') &&
|
|
|
|
(useurl[1] == '.') &&
|
|
|
|
(useurl[2] == '/')) {
|
|
|
|
level++;
|
|
|
|
useurl += 3; /* pass the "../" */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(protsep) {
|
|
|
|
while(level--) {
|
|
|
|
/* cut off one more level from the right of the original URL */
|
|
|
|
pathsep = strrchr(protsep, '/');
|
|
|
|
if(pathsep)
|
|
|
|
*pathsep = 0;
|
|
|
|
else {
|
|
|
|
*protsep = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We got a new absolute path for this server */
|
|
|
|
|
2019-09-19 16:18:28 +08:00
|
|
|
if(relurl[1] == '/') {
|
2018-08-05 17:51:07 +08:00
|
|
|
/* the new URL starts with //, just keep the protocol part from the
|
|
|
|
original one */
|
|
|
|
*protsep = 0;
|
|
|
|
useurl = &relurl[2]; /* we keep the slashes from the original, so we
|
|
|
|
skip the new ones */
|
|
|
|
host_changed = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* cut off the original URL from the first slash, or deal with URLs
|
|
|
|
without slash */
|
|
|
|
pathsep = strchr(protsep, '/');
|
|
|
|
if(pathsep) {
|
|
|
|
/* When people use badly formatted URLs, such as
|
|
|
|
"http://www.url.com?dir=/home/daniel" we must not use the first
|
|
|
|
slash, if there's a ?-letter before it! */
|
|
|
|
char *sep = strchr(protsep, '?');
|
|
|
|
if(sep && (sep < pathsep))
|
|
|
|
pathsep = sep;
|
|
|
|
*pathsep = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* There was no slash. Now, since we might be operating on a badly
|
|
|
|
formatted URL, such as "http://www.url.com?id=2380" which doesn't
|
|
|
|
use a slash separator as it is supposed to, we need to check for a
|
|
|
|
?-letter as well! */
|
|
|
|
pathsep = strchr(protsep, '?');
|
|
|
|
if(pathsep)
|
|
|
|
*pathsep = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
Curl_dyn_init(&newest, CURL_MAX_INPUT_LENGTH);
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/* copy over the root url part */
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_add(&newest, base))
|
|
|
|
return NULL;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/* check if we need to append a slash */
|
|
|
|
if(('/' == useurl[0]) || (protsep && !*protsep) || ('?' == useurl[0]))
|
|
|
|
;
|
2022-09-01 16:16:24 +08:00
|
|
|
else {
|
|
|
|
if(Curl_dyn_addn(&newest, "/", 1))
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/* then append the new piece on the right side */
|
2022-10-18 21:54:06 +08:00
|
|
|
urlencode_str(&newest, useurl, strlen(useurl), !host_changed, FALSE);
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
return Curl_dyn_ptr(&newest);
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2021-11-24 00:07:31 +08:00
|
|
|
/* scan for byte values < 31 or 127 */
|
|
|
|
static bool junkscan(const char *part, unsigned int flags)
|
|
|
|
{
|
|
|
|
if(part) {
|
|
|
|
static const char badbytes[]={
|
|
|
|
/* */ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
|
|
|
0x7f, 0x00 /* null-terminate */
|
|
|
|
};
|
|
|
|
size_t n = strlen(part);
|
|
|
|
size_t nfine = strcspn(part, badbytes);
|
|
|
|
if(nfine != n)
|
|
|
|
/* since we don't know which part is scanned, return a generic error
|
|
|
|
code */
|
|
|
|
return TRUE;
|
|
|
|
if(!(flags & CURLU_ALLOW_SPACE) && strchr(part, ' '))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/*
|
|
|
|
* parse_hostname_login()
|
|
|
|
*
|
|
|
|
* Parse the login details (user name, password and options) from the URL and
|
|
|
|
* strip them out of the host name
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static CURLUcode parse_hostname_login(struct Curl_URL *u,
|
2022-09-01 16:16:24 +08:00
|
|
|
struct dynbuf *host,
|
2018-08-05 17:51:07 +08:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
CURLUcode result = CURLUE_OK;
|
|
|
|
CURLcode ccode;
|
|
|
|
char *userp = NULL;
|
|
|
|
char *passwdp = NULL;
|
|
|
|
char *optionsp = NULL;
|
2020-01-28 00:28:40 +08:00
|
|
|
const struct Curl_handler *h = NULL;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
/* At this point, we assume all the other special cases have been taken
|
|
|
|
* care of, so the host is at most
|
|
|
|
*
|
|
|
|
* [user[:password][;options]]@]hostname
|
2018-08-05 17:51:07 +08:00
|
|
|
*
|
|
|
|
* We need somewhere to put the embedded details, so do that first.
|
|
|
|
*/
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
char *login = Curl_dyn_ptr(host);
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
DEBUGASSERT(login);
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
ptr = strchr(login, '@');
|
2018-08-05 17:51:07 +08:00
|
|
|
if(!ptr)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* We will now try to extract the
|
|
|
|
* possible login information in a string like:
|
|
|
|
* ftp://user:password@ftp.my.site:8021/README */
|
2022-09-01 16:16:24 +08:00
|
|
|
ptr++;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2020-01-28 00:28:40 +08:00
|
|
|
/* if this is a known scheme, get some details */
|
|
|
|
if(u->scheme)
|
2022-09-15 19:30:09 +08:00
|
|
|
h = Curl_builtin_scheme(u->scheme, CURL_ZERO_TERMINATED);
|
2020-01-28 00:28:40 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/* We could use the login information in the URL so extract it. Only parse
|
2018-09-09 04:57:36 +08:00
|
|
|
options if the handler says we should. Note that 'h' might be NULL! */
|
2018-08-05 17:51:07 +08:00
|
|
|
ccode = Curl_parse_login_details(login, ptr - login - 1,
|
|
|
|
&userp, &passwdp,
|
2018-09-09 04:57:36 +08:00
|
|
|
(h && (h->flags & PROTOPT_URLOPTIONS)) ?
|
2018-08-05 17:51:07 +08:00
|
|
|
&optionsp:NULL);
|
|
|
|
if(ccode) {
|
2021-11-24 00:07:31 +08:00
|
|
|
result = CURLUE_BAD_LOGIN;
|
2018-08-05 17:51:07 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(userp) {
|
|
|
|
if(flags & CURLU_DISALLOW_USER) {
|
|
|
|
/* Option DISALLOW_USER is set and url contains username. */
|
|
|
|
result = CURLUE_USER_NOT_ALLOWED;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-11-24 00:07:31 +08:00
|
|
|
if(junkscan(userp, flags)) {
|
|
|
|
result = CURLUE_BAD_USER;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
u->user = userp;
|
|
|
|
}
|
|
|
|
|
2021-11-24 00:07:31 +08:00
|
|
|
if(passwdp) {
|
|
|
|
if(junkscan(passwdp, flags)) {
|
|
|
|
result = CURLUE_BAD_PASSWORD;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
u->password = passwdp;
|
2021-11-24 00:07:31 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2021-11-24 00:07:31 +08:00
|
|
|
if(optionsp) {
|
|
|
|
if(junkscan(optionsp, flags)) {
|
|
|
|
result = CURLUE_BAD_LOGIN;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
u->options = optionsp;
|
2021-11-24 00:07:31 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
/* move the name to the start of the host buffer */
|
|
|
|
if(Curl_dyn_tail(host, strlen(ptr)))
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_OK;
|
|
|
|
out:
|
|
|
|
|
|
|
|
free(userp);
|
|
|
|
free(passwdp);
|
|
|
|
free(optionsp);
|
2021-11-24 00:07:31 +08:00
|
|
|
u->user = NULL;
|
|
|
|
u->password = NULL;
|
|
|
|
u->options = NULL;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
UNITTEST CURLUcode Curl_parse_port(struct Curl_URL *u, struct dynbuf *host,
|
2020-12-05 00:27:57 +08:00
|
|
|
bool has_scheme)
|
2018-08-05 17:51:07 +08:00
|
|
|
{
|
2018-12-11 22:02:19 +08:00
|
|
|
char *portptr = NULL;
|
2018-08-05 17:51:07 +08:00
|
|
|
char endbracket;
|
|
|
|
int len;
|
2022-09-01 16:16:24 +08:00
|
|
|
char *hostname = Curl_dyn_ptr(host);
|
2018-12-11 22:02:19 +08:00
|
|
|
/*
|
|
|
|
* Find the end of an IPv6 address, either on the ']' ending bracket or
|
|
|
|
* a percent-encoded zone index.
|
|
|
|
*/
|
|
|
|
if(1 == sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.]%c%n",
|
|
|
|
&endbracket, &len)) {
|
|
|
|
if(']' == endbracket)
|
|
|
|
portptr = &hostname[len];
|
|
|
|
else if('%' == endbracket) {
|
|
|
|
int zonelen = len;
|
2019-04-30 22:59:08 +08:00
|
|
|
if(1 == sscanf(hostname + zonelen, "%*[^]]%c%n", &endbracket, &len)) {
|
2018-12-31 03:11:57 +08:00
|
|
|
if(']' != endbracket)
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2018-12-31 03:11:57 +08:00
|
|
|
portptr = &hostname[--zonelen + len + 1];
|
|
|
|
}
|
2018-12-11 22:02:19 +08:00
|
|
|
else
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2018-12-11 22:02:19 +08:00
|
|
|
}
|
|
|
|
else
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2018-12-11 22:02:19 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/* this is a RFC2732-style specified IP-address */
|
2018-12-11 22:02:19 +08:00
|
|
|
if(portptr && *portptr) {
|
2018-11-02 22:11:16 +08:00
|
|
|
if(*portptr != ':')
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2018-11-02 22:11:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
portptr = NULL;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
portptr = strchr(hostname, ':');
|
|
|
|
|
|
|
|
if(portptr) {
|
|
|
|
char *rest;
|
|
|
|
long port;
|
|
|
|
char portbuf[7];
|
2022-09-01 16:16:24 +08:00
|
|
|
size_t keep = portptr - hostname;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2018-12-12 18:45:09 +08:00
|
|
|
/* Browser behavior adaptation. If there's a colon with no digits after,
|
|
|
|
just cut off the name there which makes us ignore the colon and just
|
2020-12-05 00:27:57 +08:00
|
|
|
use the default port. Firefox, Chrome and Safari all do that.
|
|
|
|
|
|
|
|
Don't do it if the URL has no scheme, to make something that looks like
|
|
|
|
a scheme not work!
|
|
|
|
*/
|
2022-09-01 16:16:24 +08:00
|
|
|
Curl_dyn_setlen(host, keep);
|
|
|
|
portptr++;
|
|
|
|
if(!*portptr)
|
2020-12-05 00:27:57 +08:00
|
|
|
return has_scheme ? CURLUE_OK : CURLUE_BAD_PORT_NUMBER;
|
2018-12-12 18:45:09 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!ISDIGIT(*portptr))
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_BAD_PORT_NUMBER;
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
port = strtol(portptr, &rest, 10); /* Port number must be decimal */
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2021-12-03 22:56:58 +08:00
|
|
|
if(port > 0xffff)
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_BAD_PORT_NUMBER;
|
|
|
|
|
|
|
|
if(rest[0])
|
|
|
|
return CURLUE_BAD_PORT_NUMBER;
|
|
|
|
|
2018-12-12 18:45:09 +08:00
|
|
|
*rest = 0;
|
|
|
|
/* generate a new port number string to get rid of leading zeroes etc */
|
|
|
|
msnprintf(portbuf, sizeof(portbuf), "%ld", port);
|
|
|
|
u->portnum = port;
|
|
|
|
u->port = strdup(portbuf);
|
|
|
|
if(!u->port)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CURLUE_OK;
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
static CURLUcode hostname_check(struct Curl_URL *u, char *hostname,
|
|
|
|
size_t hlen) /* length of hostname */
|
2018-08-05 17:51:07 +08:00
|
|
|
{
|
|
|
|
size_t len;
|
2022-09-01 16:16:24 +08:00
|
|
|
DEBUGASSERT(hostname);
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!hostname[0])
|
|
|
|
return CURLUE_NO_HOST;
|
|
|
|
else if(hostname[0] == '[') {
|
2019-09-10 17:51:51 +08:00
|
|
|
const char *l = "0123456789abcdefABCDEF:.";
|
2020-05-07 18:24:27 +08:00
|
|
|
if(hlen < 4) /* '[::]' is the shortest possible valid string */
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2018-08-05 17:51:07 +08:00
|
|
|
hostname++;
|
|
|
|
hlen -= 2;
|
|
|
|
|
2019-09-05 05:49:30 +08:00
|
|
|
if(hostname[hlen] != ']')
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2019-09-05 05:49:30 +08:00
|
|
|
|
2018-09-19 16:17:03 +08:00
|
|
|
/* only valid letters are ok */
|
|
|
|
len = strspn(hostname, l);
|
2019-04-30 22:59:08 +08:00
|
|
|
if(hlen != len) {
|
2019-09-05 05:49:30 +08:00
|
|
|
hlen = len;
|
2019-04-30 22:59:08 +08:00
|
|
|
if(hostname[len] == '%') {
|
2019-05-12 03:42:48 +08:00
|
|
|
/* this could now be '%[zone id]' */
|
|
|
|
char zoneid[16];
|
2019-04-30 22:59:08 +08:00
|
|
|
int i = 0;
|
|
|
|
char *h = &hostname[len + 1];
|
|
|
|
/* pass '25' if present and is a url encoded percent sign */
|
|
|
|
if(!strncmp(h, "25", 2) && h[2] && (h[2] != ']'))
|
|
|
|
h += 2;
|
|
|
|
while(*h && (*h != ']') && (i < 15))
|
2019-05-03 19:18:12 +08:00
|
|
|
zoneid[i++] = *h++;
|
2019-04-30 22:59:08 +08:00
|
|
|
if(!i || (']' != *h))
|
2021-11-24 00:07:31 +08:00
|
|
|
/* impossible to reach? */
|
2019-04-30 22:59:08 +08:00
|
|
|
return CURLUE_MALFORMED_INPUT;
|
2019-05-03 19:18:12 +08:00
|
|
|
zoneid[i] = 0;
|
|
|
|
u->zoneid = strdup(zoneid);
|
|
|
|
if(!u->zoneid)
|
2019-04-30 22:59:08 +08:00
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
hostname[len] = ']'; /* insert end bracket */
|
|
|
|
hostname[len + 1] = 0; /* terminate the hostname */
|
|
|
|
}
|
|
|
|
else
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_IPV6;
|
2019-04-30 22:59:08 +08:00
|
|
|
/* hostname is fine */
|
|
|
|
}
|
2019-09-05 05:49:30 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
2022-01-02 05:33:56 +08:00
|
|
|
{
|
|
|
|
char dest[16]; /* fits a binary IPv6 address */
|
|
|
|
char norm[MAX_IPADR_LEN];
|
|
|
|
hostname[hlen] = 0; /* end the address there */
|
|
|
|
if(1 != Curl_inet_pton(AF_INET6, hostname, dest))
|
|
|
|
return CURLUE_BAD_IPV6;
|
|
|
|
|
|
|
|
/* check if it can be done shorter */
|
|
|
|
if(Curl_inet_ntop(AF_INET6, dest, norm, sizeof(norm)) &&
|
|
|
|
(strlen(norm) < hlen)) {
|
|
|
|
strcpy(hostname, norm);
|
|
|
|
hlen = strlen(norm);
|
|
|
|
hostname[hlen + 1] = 0;
|
|
|
|
}
|
|
|
|
hostname[hlen] = ']'; /* restore ending bracket */
|
|
|
|
}
|
2019-09-05 05:49:30 +08:00
|
|
|
#endif
|
2018-09-19 16:17:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2022-05-09 14:19:38 +08:00
|
|
|
/* letters from the second string are not ok */
|
2022-12-15 06:41:08 +08:00
|
|
|
len = strcspn(hostname, " \r\n\t/:#?!@{}[]\\$\'\"^`*<>=;,+&()");
|
2018-09-19 16:17:03 +08:00
|
|
|
if(hlen != len)
|
|
|
|
/* hostname with bad content */
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_HOSTNAME;
|
2018-09-19 16:17:03 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HOSTNAME_END(x) (((x) == '/') || ((x) == '?') || ((x) == '#'))
|
|
|
|
|
2021-04-19 14:34:52 +08:00
|
|
|
/*
|
|
|
|
* Handle partial IPv4 numerical addresses and different bases, like
|
|
|
|
* '16843009', '0x7f', '0x7f.1' '0177.1.1.1' etc.
|
|
|
|
*
|
|
|
|
* If the given input string is syntactically wrong or any part for example is
|
|
|
|
* too big, this function returns FALSE and doesn't create any output.
|
|
|
|
*
|
|
|
|
* Output the "normalized" version of that input string in plain quad decimal
|
|
|
|
* integers and return TRUE.
|
|
|
|
*/
|
|
|
|
static bool ipv4_normalize(const char *hostname, char *outp, size_t olen)
|
|
|
|
{
|
|
|
|
bool done = FALSE;
|
|
|
|
int n = 0;
|
|
|
|
const char *c = hostname;
|
|
|
|
unsigned long parts[4] = {0, 0, 0, 0};
|
|
|
|
|
|
|
|
while(!done) {
|
|
|
|
char *endp;
|
2021-04-20 04:58:54 +08:00
|
|
|
unsigned long l;
|
|
|
|
if((*c < '0') || (*c > '9'))
|
|
|
|
/* most importantly this doesn't allow a leading plus or minus */
|
|
|
|
return FALSE;
|
|
|
|
l = strtoul(c, &endp, 0);
|
2021-04-19 14:34:52 +08:00
|
|
|
|
|
|
|
/* overflow or nothing parsed at all */
|
|
|
|
if(((l == ULONG_MAX) && (errno == ERANGE)) || (endp == c))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
#if SIZEOF_LONG > 4
|
|
|
|
/* a value larger than 32 bits */
|
|
|
|
if(l > UINT_MAX)
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
parts[n] = l;
|
|
|
|
c = endp;
|
|
|
|
|
|
|
|
switch (*c) {
|
|
|
|
case '.' :
|
|
|
|
if(n == 3)
|
|
|
|
return FALSE;
|
|
|
|
n++;
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is deemed a valid IPv4 numerical address */
|
|
|
|
|
|
|
|
switch(n) {
|
|
|
|
case 0: /* a -- 32 bits */
|
|
|
|
msnprintf(outp, olen, "%u.%u.%u.%u",
|
|
|
|
parts[0] >> 24, (parts[0] >> 16) & 0xff,
|
|
|
|
(parts[0] >> 8) & 0xff, parts[0] & 0xff);
|
|
|
|
break;
|
|
|
|
case 1: /* a.b -- 8.24 bits */
|
|
|
|
if((parts[0] > 0xff) || (parts[1] > 0xffffff))
|
|
|
|
return FALSE;
|
|
|
|
msnprintf(outp, olen, "%u.%u.%u.%u",
|
|
|
|
parts[0], (parts[1] >> 16) & 0xff,
|
|
|
|
(parts[1] >> 8) & 0xff, parts[1] & 0xff);
|
|
|
|
break;
|
|
|
|
case 2: /* a.b.c -- 8.8.16 bits */
|
|
|
|
if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xffff))
|
|
|
|
return FALSE;
|
|
|
|
msnprintf(outp, olen, "%u.%u.%u.%u",
|
|
|
|
parts[0], parts[1], (parts[2] >> 8) & 0xff,
|
|
|
|
parts[2] & 0xff);
|
|
|
|
break;
|
|
|
|
case 3: /* a.b.c.d -- 8.8.8.8 bits */
|
|
|
|
if((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff) ||
|
|
|
|
(parts[3] > 0xff))
|
|
|
|
return FALSE;
|
|
|
|
msnprintf(outp, olen, "%u.%u.%u.%u",
|
|
|
|
parts[0], parts[1], parts[2], parts[3]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
/* if necessary, replace the host content with a URL decoded version */
|
|
|
|
static CURLUcode decode_host(struct dynbuf *host)
|
2021-10-09 02:12:26 +08:00
|
|
|
{
|
|
|
|
char *per = NULL;
|
2022-09-01 16:16:24 +08:00
|
|
|
const char *hostname = Curl_dyn_ptr(host);
|
|
|
|
if(hostname[0] == '[')
|
2021-10-09 02:12:26 +08:00
|
|
|
/* only decode if not an ipv6 numerical */
|
2022-09-01 16:16:24 +08:00
|
|
|
return CURLUE_OK;
|
|
|
|
per = strchr(hostname, '%');
|
|
|
|
if(!per)
|
|
|
|
/* nothing to decode */
|
|
|
|
return CURLUE_OK;
|
2021-10-09 02:12:26 +08:00
|
|
|
else {
|
2022-09-01 16:16:24 +08:00
|
|
|
/* encoded */
|
2021-10-09 02:12:26 +08:00
|
|
|
size_t dlen;
|
2022-09-01 16:16:24 +08:00
|
|
|
char *decoded;
|
|
|
|
CURLcode result = Curl_urldecode(hostname, 0, &decoded, &dlen,
|
|
|
|
REJECT_CTRL);
|
2021-10-09 02:12:26 +08:00
|
|
|
if(result)
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_HOSTNAME;
|
2022-09-01 16:16:24 +08:00
|
|
|
Curl_dyn_reset(host);
|
|
|
|
result = Curl_dyn_addn(host, decoded, dlen);
|
|
|
|
free(decoded);
|
|
|
|
if(result)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2021-10-09 02:12:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CURLUE_OK;
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
/*
|
|
|
|
* "Remove Dot Segments"
|
|
|
|
* https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dedotdotify()
|
|
|
|
* @unittest: 1395
|
|
|
|
*
|
|
|
|
* This function gets a null-terminated path with dot and dotdot sequences
|
|
|
|
* passed in and strips them off according to the rules in RFC 3986 section
|
|
|
|
* 5.2.4.
|
|
|
|
*
|
|
|
|
* The function handles a query part ('?' + stuff) appended but it expects
|
|
|
|
* that fragments ('#' + stuff) have already been cut off.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
*
|
|
|
|
* an allocated dedotdotified output string
|
|
|
|
*/
|
|
|
|
UNITTEST char *dedotdotify(const char *input, size_t clen);
|
|
|
|
UNITTEST char *dedotdotify(const char *input, size_t clen)
|
2018-08-05 17:51:07 +08:00
|
|
|
{
|
2022-09-01 16:16:24 +08:00
|
|
|
char *out = malloc(clen + 1);
|
|
|
|
char *outptr;
|
|
|
|
const char *orginput = input;
|
|
|
|
char *queryp;
|
|
|
|
if(!out)
|
|
|
|
return NULL; /* out of memory */
|
|
|
|
|
|
|
|
*out = 0; /* null-terminates, for inputs like "./" */
|
|
|
|
outptr = out;
|
|
|
|
|
|
|
|
if(!*input)
|
|
|
|
/* zero length input string, return that */
|
|
|
|
return out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To handle query-parts properly, we must find it and remove it during the
|
|
|
|
* dotdot-operation and then append it again at the end to the output
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
queryp = strchr(input, '?');
|
|
|
|
|
|
|
|
do {
|
|
|
|
bool dotdot = TRUE;
|
|
|
|
if(*input == '.') {
|
|
|
|
/* A. If the input buffer begins with a prefix of "../" or "./", then
|
|
|
|
remove that prefix from the input buffer; otherwise, */
|
|
|
|
|
|
|
|
if(!strncmp("./", input, 2)) {
|
|
|
|
input += 2;
|
|
|
|
clen -= 2;
|
|
|
|
}
|
|
|
|
else if(!strncmp("../", input, 3)) {
|
|
|
|
input += 3;
|
|
|
|
clen -= 3;
|
|
|
|
}
|
|
|
|
/* D. if the input buffer consists only of "." or "..", then remove
|
|
|
|
that from the input buffer; otherwise, */
|
|
|
|
|
|
|
|
else if(!strcmp(".", input) || !strcmp("..", input) ||
|
|
|
|
!strncmp(".?", input, 2) || !strncmp("..?", input, 3)) {
|
|
|
|
*out = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dotdot = FALSE;
|
|
|
|
}
|
|
|
|
else if(*input == '/') {
|
|
|
|
/* B. if the input buffer begins with a prefix of "/./" or "/.", where
|
|
|
|
"." is a complete path segment, then replace that prefix with "/" in
|
|
|
|
the input buffer; otherwise, */
|
|
|
|
if(!strncmp("/./", input, 3)) {
|
|
|
|
input += 2;
|
|
|
|
clen -= 2;
|
|
|
|
}
|
|
|
|
else if(!strcmp("/.", input) || !strncmp("/.?", input, 3)) {
|
|
|
|
*outptr++ = '/';
|
|
|
|
*outptr = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* C. if the input buffer begins with a prefix of "/../" or "/..",
|
|
|
|
where ".." is a complete path segment, then replace that prefix with
|
|
|
|
"/" in the input buffer and remove the last segment and its
|
|
|
|
preceding "/" (if any) from the output buffer; otherwise, */
|
|
|
|
|
|
|
|
else if(!strncmp("/../", input, 4)) {
|
|
|
|
input += 3;
|
|
|
|
clen -= 3;
|
|
|
|
/* remove the last segment from the output buffer */
|
|
|
|
while(outptr > out) {
|
|
|
|
outptr--;
|
|
|
|
if(*outptr == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*outptr = 0; /* null-terminate where it stops */
|
|
|
|
}
|
|
|
|
else if(!strcmp("/..", input) || !strncmp("/..?", input, 4)) {
|
|
|
|
/* remove the last segment from the output buffer */
|
|
|
|
while(outptr > out) {
|
|
|
|
outptr--;
|
|
|
|
if(*outptr == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*outptr++ = '/';
|
|
|
|
*outptr = 0; /* null-terminate where it stops */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dotdot = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dotdot = FALSE;
|
|
|
|
|
|
|
|
if(!dotdot) {
|
|
|
|
/* E. move the first path segment in the input buffer to the end of
|
|
|
|
the output buffer, including the initial "/" character (if any) and
|
|
|
|
any subsequent characters up to, but not including, the next "/"
|
|
|
|
character or the end of the input buffer. */
|
|
|
|
|
|
|
|
do {
|
|
|
|
*outptr++ = *input++;
|
|
|
|
clen--;
|
|
|
|
} while(*input && (*input != '/') && (*input != '?'));
|
|
|
|
*outptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* continue until end of input string OR, if there is a terminating
|
|
|
|
query part, stop there */
|
|
|
|
} while(*input && (!queryp || (input < queryp)));
|
|
|
|
|
|
|
|
if(queryp) {
|
|
|
|
size_t qlen;
|
|
|
|
/* There was a query part, append that to the output. */
|
|
|
|
size_t oindex = queryp - orginput;
|
|
|
|
qlen = strlen(&orginput[oindex]);
|
|
|
|
memcpy(outptr, &orginput[oindex], qlen + 1); /* include zero byte */
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
size_t pathlen;
|
2021-09-27 14:31:52 +08:00
|
|
|
bool uncpath = FALSE;
|
2018-08-05 17:51:07 +08:00
|
|
|
char *query = NULL;
|
|
|
|
char *fragment = NULL;
|
2019-05-19 07:48:00 +08:00
|
|
|
char schemebuf[MAX_SCHEME_LEN + 1];
|
2020-01-28 00:28:40 +08:00
|
|
|
const char *schemep = NULL;
|
2018-08-05 17:51:07 +08:00
|
|
|
size_t schemelen = 0;
|
|
|
|
size_t urllen;
|
2022-09-01 16:16:24 +08:00
|
|
|
CURLUcode result = CURLUE_OK;
|
|
|
|
size_t fraglen = 0;
|
|
|
|
struct dynbuf host;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2021-08-23 06:34:16 +08:00
|
|
|
DEBUGASSERT(url);
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
Curl_dyn_init(&host, CURL_MAX_INPUT_LENGTH);
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/*************************************************************
|
|
|
|
* Parse the URL.
|
|
|
|
************************************************************/
|
|
|
|
/* allocate scratch area */
|
|
|
|
urllen = strlen(url);
|
2022-09-01 16:16:24 +08:00
|
|
|
if(urllen > CURL_MAX_INPUT_LENGTH) {
|
2019-04-29 14:00:49 +08:00
|
|
|
/* excessive input length */
|
2022-09-01 16:16:24 +08:00
|
|
|
result = CURLUE_MALFORMED_INPUT;
|
|
|
|
goto fail;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:18:30 +08:00
|
|
|
schemelen = Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf),
|
|
|
|
flags & (CURLU_GUESS_SCHEME|
|
|
|
|
CURLU_DEFAULT_SCHEME));
|
2022-09-01 16:16:24 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/* handle the file: scheme */
|
2022-09-01 16:16:24 +08:00
|
|
|
if(schemelen && !strcmp(schemebuf, "file")) {
|
|
|
|
if(urllen <= 6) {
|
2021-11-22 17:11:59 +08:00
|
|
|
/* file:/ is not enough to actually be a complete file: URL */
|
2022-09-01 16:16:24 +08:00
|
|
|
result = CURLUE_BAD_FILE_URL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-11-22 17:11:59 +08:00
|
|
|
|
2018-09-10 03:29:39 +08:00
|
|
|
/* path has been allocated large enough to hold this */
|
2022-09-01 16:16:24 +08:00
|
|
|
path = (char *)&url[5];
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
schemep = u->scheme = strdup("file");
|
|
|
|
if(!u->scheme) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/* Extra handling URLs with an authority component (i.e. that start with
|
|
|
|
* "file://")
|
|
|
|
*
|
|
|
|
* We allow omitted hostname (e.g. file:/<path>) -- valid according to
|
|
|
|
* RFC 8089, but not the (current) WHAT-WG URL spec.
|
|
|
|
*/
|
|
|
|
if(path[0] == '/' && path[1] == '/') {
|
|
|
|
/* swallow the two slashes */
|
2022-09-01 16:16:24 +08:00
|
|
|
const char *ptr = &path[2];
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* According to RFC 8089, a file: URL can be reliably dereferenced if:
|
|
|
|
*
|
|
|
|
* o it has no/blank hostname, or
|
|
|
|
*
|
|
|
|
* o the hostname matches "localhost" (case-insensitively), or
|
|
|
|
*
|
2021-09-27 14:31:52 +08:00
|
|
|
* o the hostname is a FQDN that resolves to this machine, or
|
|
|
|
*
|
|
|
|
* o it is an UNC String transformed to an URI (Windows only, RFC 8089
|
|
|
|
* Appendix E.3).
|
2018-08-05 17:51:07 +08:00
|
|
|
*
|
|
|
|
* For brevity, we only consider URLs with empty, "localhost", or
|
2021-09-27 14:31:52 +08:00
|
|
|
* "127.0.0.1" hostnames as local, otherwise as an UNC String.
|
2018-08-05 17:51:07 +08:00
|
|
|
*
|
|
|
|
* Additionally, there is an exception for URLs with a Windows drive
|
|
|
|
* letter in the authority (which was accidentally omitted from RFC 8089
|
|
|
|
* Appendix E, but believe me, it was meant to be there. --MK)
|
|
|
|
*/
|
|
|
|
if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
|
|
|
|
/* the URL includes a host name, it must match "localhost" or
|
|
|
|
"127.0.0.1" to be valid */
|
2021-09-27 14:31:52 +08:00
|
|
|
if(checkprefix("localhost/", ptr) ||
|
|
|
|
checkprefix("127.0.0.1/", ptr)) {
|
|
|
|
ptr += 9; /* now points to the slash after the host */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if defined(WIN32)
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* the host name, NetBIOS computer name, can not contain disallowed
|
|
|
|
chars, and the delimiting slash character must be appended to the
|
|
|
|
host name */
|
|
|
|
path = strpbrk(ptr, "/\\:*?\"<>|");
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!path || *path != '/') {
|
|
|
|
result = CURLUE_BAD_FILE_URL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-27 14:31:52 +08:00
|
|
|
|
|
|
|
len = path - ptr;
|
|
|
|
if(len) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_addn(&host, ptr, len)) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-09-27 14:31:52 +08:00
|
|
|
uncpath = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr -= 2; /* now points to the // before the host in UNC */
|
|
|
|
#else
|
2018-08-05 17:51:07 +08:00
|
|
|
/* Invalid file://hostname/, expected localhost or 127.0.0.1 or
|
|
|
|
none */
|
2022-09-01 16:16:24 +08:00
|
|
|
result = CURLUE_BAD_FILE_URL;
|
|
|
|
goto fail;
|
2021-09-27 14:31:52 +08:00
|
|
|
#endif
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path = ptr;
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:31:52 +08:00
|
|
|
if(!uncpath)
|
2022-09-01 16:16:24 +08:00
|
|
|
/* no host for file: URLs by default */
|
|
|
|
Curl_dyn_reset(&host);
|
2021-09-27 14:31:52 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
#if !defined(MSDOS) && !defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
/* Don't allow Windows drive letters when not in Windows.
|
|
|
|
* This catches both "file:/c:" and "file:c:" */
|
|
|
|
if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
|
|
|
|
STARTS_WITH_URL_DRIVE_PREFIX(path)) {
|
|
|
|
/* File drive letters are only accepted in MSDOS/Windows */
|
2022-09-01 16:16:24 +08:00
|
|
|
result = CURLUE_BAD_FILE_URL;
|
|
|
|
goto fail;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* If the path starts with a slash and a drive letter, ditch the slash */
|
|
|
|
if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
|
|
|
|
/* This cannot be done with strcpy, as the memory chunks overlap! */
|
2022-09-01 16:16:24 +08:00
|
|
|
path++;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* clear path */
|
|
|
|
const char *p;
|
|
|
|
const char *hostp;
|
|
|
|
size_t len;
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(schemelen) {
|
2018-08-05 17:51:07 +08:00
|
|
|
int i = 0;
|
|
|
|
p = &url[schemelen + 1];
|
|
|
|
while(p && (*p == '/') && (i < 4)) {
|
|
|
|
p++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
schemep = schemebuf;
|
2022-09-15 19:30:09 +08:00
|
|
|
if(!Curl_builtin_scheme(schemep, CURL_ZERO_TERMINATED) &&
|
2022-09-01 16:16:24 +08:00
|
|
|
!(flags & CURLU_NON_SUPPORT_SCHEME)) {
|
|
|
|
result = CURLUE_UNSUPPORTED_SCHEME;
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-14 15:18:30 +08:00
|
|
|
if((i < 1) || (i>3)) {
|
|
|
|
/* less than one or more than three slashes */
|
|
|
|
result = CURLUE_BAD_SLASHES;
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
if(junkscan(schemep, flags)) {
|
|
|
|
result = CURLUE_BAD_SCHEME;
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no scheme! */
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!(flags & (CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME))) {
|
|
|
|
result = CURLUE_BAD_SCHEME;
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-09-19 16:17:03 +08:00
|
|
|
if(flags & CURLU_DEFAULT_SCHEME)
|
2020-01-28 00:28:40 +08:00
|
|
|
schemep = DEFAULT_SCHEME;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The URL was badly formatted, let's try without scheme specified.
|
|
|
|
*/
|
|
|
|
p = url;
|
|
|
|
}
|
|
|
|
hostp = p; /* host name starts here */
|
|
|
|
|
2021-11-24 00:07:31 +08:00
|
|
|
/* find the end of the host name + port number */
|
|
|
|
while(*p && !HOSTNAME_END(*p))
|
2018-08-05 17:51:07 +08:00
|
|
|
p++;
|
|
|
|
|
|
|
|
len = p - hostp;
|
2019-09-19 21:54:53 +08:00
|
|
|
if(len) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_addn(&host, hostp, len)) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-09-19 21:54:53 +08:00
|
|
|
}
|
|
|
|
else {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!(flags & CURLU_NO_AUTHORITY)) {
|
|
|
|
result = CURLUE_NO_HOST;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-09-19 21:54:53 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
path = (char *)p;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2020-01-28 00:28:40 +08:00
|
|
|
if(schemep) {
|
|
|
|
u->scheme = strdup(schemep);
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!u->scheme) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2020-01-28 00:28:40 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:45:27 +08:00
|
|
|
fragment = strchr(path, '#');
|
2019-10-03 19:24:43 +08:00
|
|
|
if(fragment) {
|
2022-09-01 16:16:24 +08:00
|
|
|
fraglen = strlen(fragment);
|
|
|
|
if(fraglen > 1) {
|
|
|
|
/* skip the leading '#' in the copy but include the terminating null */
|
|
|
|
u->fragment = Curl_memdup(fragment + 1, fraglen);
|
|
|
|
if(!u->fragment) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(junkscan(u->fragment, flags)) {
|
|
|
|
result = CURLUE_BAD_FRAGMENT;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-10-03 19:24:43 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 20:45:27 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
query = strchr(path, '?');
|
2022-09-01 16:16:24 +08:00
|
|
|
if(query && (!fragment || (query < fragment))) {
|
|
|
|
size_t qlen = strlen(query) - fraglen; /* includes '?' */
|
|
|
|
pathlen = strlen(path) - qlen - fraglen;
|
|
|
|
if(qlen > 1) {
|
2022-10-18 21:54:06 +08:00
|
|
|
if(qlen && (flags & CURLU_URLENCODE)) {
|
|
|
|
struct dynbuf enc;
|
|
|
|
Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
|
|
|
|
/* skip the leading question mark */
|
|
|
|
if(urlencode_str(&enc, query + 1, qlen - 1, TRUE, TRUE)) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-10-21 15:41:54 +08:00
|
|
|
u->query = Curl_dyn_ptr(&enc);
|
2022-10-18 21:54:06 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
u->query = Curl_memdup(query + 1, qlen);
|
|
|
|
if(!u->query) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
u->query[qlen - 1] = 0;
|
2022-09-01 16:16:24 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(junkscan(u->query, flags)) {
|
|
|
|
result = CURLUE_BAD_QUERY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* single byte query */
|
|
|
|
u->query = strdup("");
|
|
|
|
if(!u->query) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pathlen = strlen(path) - fraglen;
|
2021-11-24 00:07:31 +08:00
|
|
|
|
2022-10-18 21:54:06 +08:00
|
|
|
if(pathlen && (flags & CURLU_URLENCODE)) {
|
|
|
|
struct dynbuf enc;
|
|
|
|
Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
|
|
|
|
if(urlencode_str(&enc, path, pathlen, TRUE, FALSE)) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
pathlen = Curl_dyn_len(&enc);
|
|
|
|
path = u->path = Curl_dyn_ptr(&enc);
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!pathlen) {
|
|
|
|
/* there is no path left, unset */
|
2018-08-05 17:51:07 +08:00
|
|
|
path = NULL;
|
2022-09-01 16:16:24 +08:00
|
|
|
}
|
2019-10-03 19:24:43 +08:00
|
|
|
else {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!u->path) {
|
|
|
|
u->path = Curl_memdup(path, pathlen + 1);
|
|
|
|
if(!u->path) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
u->path[pathlen] = 0;
|
|
|
|
path = u->path;
|
|
|
|
}
|
|
|
|
else if(flags & CURLU_URLENCODE)
|
|
|
|
/* it might have encoded more than just the path so cut it */
|
|
|
|
u->path[pathlen] = 0;
|
|
|
|
|
|
|
|
if(junkscan(u->path, flags)) {
|
|
|
|
result = CURLUE_BAD_PATH;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:24:43 +08:00
|
|
|
if(!(flags & CURLU_PATH_AS_IS)) {
|
|
|
|
/* remove ../ and ./ sequences according to RFC3986 */
|
2022-09-01 16:16:24 +08:00
|
|
|
char *newp = dedotdotify((char *)path, pathlen);
|
|
|
|
if(!newp) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
2019-10-03 19:24:43 +08:00
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
free(u->path);
|
|
|
|
u->path = newp;
|
2019-10-01 15:53:28 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_len(&host)) {
|
2021-04-19 14:34:52 +08:00
|
|
|
char normalized_ipv4[sizeof("255.255.255.255") + 1];
|
2021-11-24 00:07:31 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/*
|
|
|
|
* Parse the login details and strip them out of the host name.
|
|
|
|
*/
|
2022-09-01 16:16:24 +08:00
|
|
|
result = parse_hostname_login(u, &host, flags);
|
|
|
|
if(!result)
|
|
|
|
result = Curl_parse_port(u, &host, schemelen);
|
2018-08-05 17:51:07 +08:00
|
|
|
if(result)
|
2022-09-01 16:16:24 +08:00
|
|
|
goto fail;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(junkscan(Curl_dyn_ptr(&host), flags)) {
|
|
|
|
result = CURLUE_BAD_HOSTNAME;
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-11-24 00:07:31 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
if(ipv4_normalize(Curl_dyn_ptr(&host),
|
|
|
|
normalized_ipv4, sizeof(normalized_ipv4))) {
|
|
|
|
Curl_dyn_reset(&host);
|
|
|
|
if(Curl_dyn_add(&host, normalized_ipv4)) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-09-19 21:54:53 +08:00
|
|
|
}
|
|
|
|
else {
|
2022-09-01 16:16:24 +08:00
|
|
|
result = decode_host(&host);
|
|
|
|
if(!result)
|
|
|
|
result = hostname_check(u, Curl_dyn_ptr(&host), Curl_dyn_len(&host));
|
|
|
|
if(result)
|
|
|
|
goto fail;
|
2019-09-19 21:54:53 +08:00
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
|
2020-01-28 00:28:40 +08:00
|
|
|
if((flags & CURLU_GUESS_SCHEME) && !schemep) {
|
2022-09-01 16:16:24 +08:00
|
|
|
const char *hostname = Curl_dyn_ptr(&host);
|
2020-01-28 00:28:40 +08:00
|
|
|
/* legacy curl-style guess based on host name */
|
|
|
|
if(checkprefix("ftp.", hostname))
|
|
|
|
schemep = "ftp";
|
|
|
|
else if(checkprefix("dict.", hostname))
|
|
|
|
schemep = "dict";
|
|
|
|
else if(checkprefix("ldap.", hostname))
|
|
|
|
schemep = "ldap";
|
|
|
|
else if(checkprefix("imap.", hostname))
|
|
|
|
schemep = "imap";
|
|
|
|
else if(checkprefix("smtp.", hostname))
|
|
|
|
schemep = "smtp";
|
|
|
|
else if(checkprefix("pop3.", hostname))
|
|
|
|
schemep = "pop3";
|
|
|
|
else
|
|
|
|
schemep = "http";
|
|
|
|
|
|
|
|
u->scheme = strdup(schemep);
|
2022-09-01 16:16:24 +08:00
|
|
|
if(!u->scheme) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(flags & CURLU_NO_AUTHORITY) {
|
|
|
|
/* allowed to be empty. */
|
|
|
|
if(Curl_dyn_add(&host, "")) {
|
|
|
|
result = CURLUE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
2020-01-28 00:28:40 +08:00
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
u->host = Curl_dyn_ptr(&host);
|
2018-08-05 17:51:07 +08:00
|
|
|
|
2022-09-01 16:16:24 +08:00
|
|
|
return result;
|
|
|
|
fail:
|
|
|
|
Curl_dyn_free(&host);
|
|
|
|
free_urlhandle(u);
|
2018-08-05 17:51:07 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:43:04 +08:00
|
|
|
/*
|
2022-03-30 16:49:05 +08:00
|
|
|
* Parse the URL and, if successful, replace everything in the Curl_URL struct.
|
2022-02-14 18:43:04 +08:00
|
|
|
*/
|
|
|
|
static CURLUcode parseurl_and_replace(const char *url, CURLU *u,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
CURLUcode result;
|
|
|
|
CURLU tmpurl;
|
|
|
|
memset(&tmpurl, 0, sizeof(tmpurl));
|
|
|
|
result = parseurl(url, &tmpurl, flags);
|
|
|
|
if(!result) {
|
|
|
|
free_urlhandle(u);
|
|
|
|
*u = tmpurl;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
/*
|
|
|
|
*/
|
|
|
|
CURLU *curl_url(void)
|
|
|
|
{
|
|
|
|
return calloc(sizeof(struct Curl_URL), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void curl_url_cleanup(CURLU *u)
|
|
|
|
{
|
|
|
|
if(u) {
|
|
|
|
free_urlhandle(u);
|
|
|
|
free(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 22:43:25 +08:00
|
|
|
#define DUP(dest, src, name) \
|
|
|
|
do { \
|
|
|
|
if(src->name) { \
|
|
|
|
dest->name = strdup(src->name); \
|
|
|
|
if(!dest->name) \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
CURLU *curl_url_dup(CURLU *in)
|
|
|
|
{
|
|
|
|
struct Curl_URL *u = calloc(sizeof(struct Curl_URL), 1);
|
|
|
|
if(u) {
|
|
|
|
DUP(u, in, scheme);
|
|
|
|
DUP(u, in, user);
|
|
|
|
DUP(u, in, password);
|
|
|
|
DUP(u, in, options);
|
|
|
|
DUP(u, in, host);
|
|
|
|
DUP(u, in, port);
|
|
|
|
DUP(u, in, path);
|
|
|
|
DUP(u, in, query);
|
|
|
|
DUP(u, in, fragment);
|
|
|
|
u->portnum = in->portnum;
|
|
|
|
}
|
|
|
|
return u;
|
|
|
|
fail:
|
|
|
|
curl_url_cleanup(u);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLUcode curl_url_get(CURLU *u, CURLUPart what,
|
|
|
|
char **part, unsigned int flags)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
|
|
|
|
char portbuf[7];
|
|
|
|
bool urldecode = (flags & CURLU_URLDECODE)?1:0;
|
2021-10-09 02:12:26 +08:00
|
|
|
bool urlencode = (flags & CURLU_URLENCODE)?1:0;
|
2022-12-26 17:58:37 +08:00
|
|
|
bool punycode = FALSE;
|
2018-08-05 17:51:07 +08:00
|
|
|
bool plusdecode = FALSE;
|
|
|
|
(void)flags;
|
|
|
|
if(!u)
|
|
|
|
return CURLUE_BAD_HANDLE;
|
|
|
|
if(!part)
|
|
|
|
return CURLUE_BAD_PARTPOINTER;
|
|
|
|
*part = NULL;
|
|
|
|
|
|
|
|
switch(what) {
|
|
|
|
case CURLUPART_SCHEME:
|
|
|
|
ptr = u->scheme;
|
|
|
|
ifmissing = CURLUE_NO_SCHEME;
|
|
|
|
urldecode = FALSE; /* never for schemes */
|
|
|
|
break;
|
|
|
|
case CURLUPART_USER:
|
|
|
|
ptr = u->user;
|
|
|
|
ifmissing = CURLUE_NO_USER;
|
|
|
|
break;
|
|
|
|
case CURLUPART_PASSWORD:
|
|
|
|
ptr = u->password;
|
|
|
|
ifmissing = CURLUE_NO_PASSWORD;
|
|
|
|
break;
|
|
|
|
case CURLUPART_OPTIONS:
|
|
|
|
ptr = u->options;
|
|
|
|
ifmissing = CURLUE_NO_OPTIONS;
|
|
|
|
break;
|
|
|
|
case CURLUPART_HOST:
|
|
|
|
ptr = u->host;
|
|
|
|
ifmissing = CURLUE_NO_HOST;
|
2022-12-26 17:58:37 +08:00
|
|
|
punycode = (flags & CURLU_PUNYCODE)?1:0;
|
2018-08-05 17:51:07 +08:00
|
|
|
break;
|
2019-05-03 19:18:12 +08:00
|
|
|
case CURLUPART_ZONEID:
|
|
|
|
ptr = u->zoneid;
|
2021-11-24 00:07:31 +08:00
|
|
|
ifmissing = CURLUE_NO_ZONEID;
|
2019-05-03 19:18:12 +08:00
|
|
|
break;
|
2018-08-05 17:51:07 +08:00
|
|
|
case CURLUPART_PORT:
|
|
|
|
ptr = u->port;
|
|
|
|
ifmissing = CURLUE_NO_PORT;
|
|
|
|
urldecode = FALSE; /* never for port */
|
|
|
|
if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
|
|
|
|
/* there's no stored port number, but asked to deliver
|
|
|
|
a default one for the scheme */
|
|
|
|
const struct Curl_handler *h =
|
2022-09-15 19:30:09 +08:00
|
|
|
Curl_builtin_scheme(u->scheme, CURL_ZERO_TERMINATED);
|
2018-08-05 17:51:07 +08:00
|
|
|
if(h) {
|
2021-06-30 18:56:22 +08:00
|
|
|
msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
|
2018-08-05 17:51:07 +08:00
|
|
|
ptr = portbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ptr && u->scheme) {
|
|
|
|
/* there is a stored port number, but ask to inhibit if
|
|
|
|
it matches the default one for the scheme */
|
|
|
|
const struct Curl_handler *h =
|
2022-09-15 19:30:09 +08:00
|
|
|
Curl_builtin_scheme(u->scheme, CURL_ZERO_TERMINATED);
|
2018-08-05 17:51:07 +08:00
|
|
|
if(h && (h->defport == u->portnum) &&
|
|
|
|
(flags & CURLU_NO_DEFAULT_PORT))
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CURLUPART_PATH:
|
|
|
|
ptr = u->path;
|
|
|
|
if(!ptr) {
|
|
|
|
ptr = u->path = strdup("/");
|
|
|
|
if(!u->path)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CURLUPART_QUERY:
|
|
|
|
ptr = u->query;
|
|
|
|
ifmissing = CURLUE_NO_QUERY;
|
|
|
|
plusdecode = urldecode;
|
|
|
|
break;
|
|
|
|
case CURLUPART_FRAGMENT:
|
|
|
|
ptr = u->fragment;
|
|
|
|
ifmissing = CURLUE_NO_FRAGMENT;
|
|
|
|
break;
|
|
|
|
case CURLUPART_URL: {
|
|
|
|
char *url;
|
|
|
|
char *scheme;
|
|
|
|
char *options = u->options;
|
|
|
|
char *port = u->port;
|
2019-04-30 22:59:08 +08:00
|
|
|
char *allochost = NULL;
|
2022-12-26 17:58:37 +08:00
|
|
|
punycode = (flags & CURLU_PUNYCODE)?1:0;
|
2018-08-05 17:51:07 +08:00
|
|
|
if(u->scheme && strcasecompare("file", u->scheme)) {
|
|
|
|
url = aprintf("file://%s%s%s",
|
|
|
|
u->path,
|
|
|
|
u->fragment? "#": "",
|
|
|
|
u->fragment? u->fragment : "");
|
|
|
|
}
|
|
|
|
else if(!u->host)
|
|
|
|
return CURLUE_NO_HOST;
|
|
|
|
else {
|
|
|
|
const struct Curl_handler *h = NULL;
|
|
|
|
if(u->scheme)
|
|
|
|
scheme = u->scheme;
|
|
|
|
else if(flags & CURLU_DEFAULT_SCHEME)
|
|
|
|
scheme = (char *) DEFAULT_SCHEME;
|
|
|
|
else
|
|
|
|
return CURLUE_NO_SCHEME;
|
|
|
|
|
2022-09-15 19:30:09 +08:00
|
|
|
h = Curl_builtin_scheme(scheme, CURL_ZERO_TERMINATED);
|
2019-09-19 16:19:21 +08:00
|
|
|
if(!port && (flags & CURLU_DEFAULT_PORT)) {
|
|
|
|
/* there's no stored port number, but asked to deliver
|
|
|
|
a default one for the scheme */
|
|
|
|
if(h) {
|
2021-06-30 18:56:22 +08:00
|
|
|
msnprintf(portbuf, sizeof(portbuf), "%u", h->defport);
|
2019-09-19 16:19:21 +08:00
|
|
|
port = portbuf;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 16:19:21 +08:00
|
|
|
else if(port) {
|
|
|
|
/* there is a stored port number, but asked to inhibit if it matches
|
|
|
|
the default one for the scheme */
|
|
|
|
if(h && (h->defport == u->portnum) &&
|
|
|
|
(flags & CURLU_NO_DEFAULT_PORT))
|
|
|
|
port = NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
if(h && !(h->flags & PROTOPT_URLOPTIONS))
|
|
|
|
options = NULL;
|
|
|
|
|
2021-10-09 02:12:26 +08:00
|
|
|
if(u->host[0] == '[') {
|
|
|
|
if(u->zoneid) {
|
|
|
|
/* make it '[ host %25 zoneid ]' */
|
2022-09-01 16:16:24 +08:00
|
|
|
struct dynbuf enc;
|
2021-10-09 02:12:26 +08:00
|
|
|
size_t hostlen = strlen(u->host);
|
2022-09-01 16:16:24 +08:00
|
|
|
Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
|
|
|
|
if(Curl_dyn_addf(&enc, "%.*s%%25%s]", (int)hostlen - 1, u->host,
|
|
|
|
u->zoneid))
|
2021-10-09 02:12:26 +08:00
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2022-09-01 16:16:24 +08:00
|
|
|
allochost = Curl_dyn_ptr(&enc);
|
2021-10-09 02:12:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(urlencode) {
|
2021-10-15 22:29:52 +08:00
|
|
|
allochost = curl_easy_escape(NULL, u->host, 0);
|
2019-04-30 22:59:08 +08:00
|
|
|
if(!allochost)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2021-10-09 02:12:26 +08:00
|
|
|
}
|
2022-12-26 17:58:37 +08:00
|
|
|
else if(punycode) {
|
|
|
|
if(!Curl_is_ASCII_name(u->host)) {
|
|
|
|
#ifndef USE_IDN
|
|
|
|
return CURLUE_LACKS_IDN;
|
|
|
|
#else
|
|
|
|
allochost = Curl_idn_decode(u->host);
|
|
|
|
if(!allochost)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 02:12:26 +08:00
|
|
|
else {
|
|
|
|
/* only encode '%' in output host name */
|
|
|
|
char *host = u->host;
|
2022-09-01 16:16:24 +08:00
|
|
|
bool percent = FALSE;
|
2021-10-09 02:12:26 +08:00
|
|
|
/* first, count number of percents present in the name */
|
|
|
|
while(*host) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(*host == '%') {
|
|
|
|
percent = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2021-10-09 02:12:26 +08:00
|
|
|
host++;
|
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
/* if there were percent(s), encode the host name */
|
|
|
|
if(percent) {
|
|
|
|
struct dynbuf enc;
|
|
|
|
CURLcode result;
|
|
|
|
Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
|
2021-10-09 02:12:26 +08:00
|
|
|
host = u->host;
|
|
|
|
while(*host) {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(*host == '%')
|
|
|
|
result = Curl_dyn_addn(&enc, "%25", 3);
|
|
|
|
else
|
|
|
|
result = Curl_dyn_addn(&enc, host, 1);
|
|
|
|
if(result)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
host++;
|
2021-10-09 02:12:26 +08:00
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
free(u->host);
|
|
|
|
u->host = Curl_dyn_ptr(&enc);
|
2021-10-09 02:12:26 +08:00
|
|
|
}
|
2019-04-30 22:59:08 +08:00
|
|
|
}
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
url = aprintf("%s://%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
|
|
|
|
scheme,
|
|
|
|
u->user ? u->user : "",
|
|
|
|
u->password ? ":": "",
|
|
|
|
u->password ? u->password : "",
|
|
|
|
options ? ";" : "",
|
|
|
|
options ? options : "",
|
|
|
|
(u->user || u->password || options) ? "@": "",
|
2019-04-30 22:59:08 +08:00
|
|
|
allochost ? allochost : u->host,
|
2018-08-05 17:51:07 +08:00
|
|
|
port ? ":": "",
|
|
|
|
port ? port : "",
|
|
|
|
(u->path && (u->path[0] != '/')) ? "/": "",
|
|
|
|
u->path ? u->path : "/",
|
2018-12-12 23:05:45 +08:00
|
|
|
(u->query && u->query[0]) ? "?": "",
|
|
|
|
(u->query && u->query[0]) ? u->query : "",
|
2018-08-05 17:51:07 +08:00
|
|
|
u->fragment? "#": "",
|
|
|
|
u->fragment? u->fragment : "");
|
2019-04-30 22:59:08 +08:00
|
|
|
free(allochost);
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
if(!url)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
*part = url;
|
|
|
|
return CURLUE_OK;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ptr = NULL;
|
2019-02-08 20:49:45 +08:00
|
|
|
break;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
if(ptr) {
|
2022-10-18 21:54:06 +08:00
|
|
|
size_t partlen = strlen(ptr);
|
|
|
|
size_t i = 0;
|
|
|
|
*part = Curl_memdup(ptr, partlen + 1);
|
2018-08-05 17:51:07 +08:00
|
|
|
if(!*part)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
if(plusdecode) {
|
|
|
|
/* convert + to space */
|
2022-10-18 21:54:06 +08:00
|
|
|
char *plus = *part;
|
|
|
|
for(i = 0; i < partlen; ++plus, i++) {
|
2018-08-05 17:51:07 +08:00
|
|
|
if(*plus == '+')
|
|
|
|
*plus = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(urldecode) {
|
|
|
|
char *decoded;
|
|
|
|
size_t dlen;
|
2020-06-23 22:13:50 +08:00
|
|
|
/* this unconditional rejection of control bytes is documented
|
|
|
|
API behavior */
|
2022-02-03 20:04:30 +08:00
|
|
|
CURLcode res = Curl_urldecode(*part, 0, &decoded, &dlen, REJECT_CTRL);
|
2018-08-05 17:51:07 +08:00
|
|
|
free(*part);
|
|
|
|
if(res) {
|
|
|
|
*part = NULL;
|
|
|
|
return CURLUE_URLDECODE;
|
|
|
|
}
|
|
|
|
*part = decoded;
|
2022-10-18 21:54:06 +08:00
|
|
|
partlen = dlen;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
2022-06-08 22:32:46 +08:00
|
|
|
if(urlencode) {
|
2022-09-01 16:16:24 +08:00
|
|
|
struct dynbuf enc;
|
|
|
|
Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
|
2022-10-18 21:54:06 +08:00
|
|
|
if(urlencode_str(&enc, *part, partlen, TRUE,
|
|
|
|
what == CURLUPART_QUERY))
|
2022-06-08 22:32:46 +08:00
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2022-09-01 16:16:24 +08:00
|
|
|
free(*part);
|
|
|
|
*part = Curl_dyn_ptr(&enc);
|
2022-06-08 22:32:46 +08:00
|
|
|
}
|
2022-12-26 17:58:37 +08:00
|
|
|
else if(punycode) {
|
|
|
|
if(!Curl_is_ASCII_name(u->host)) {
|
|
|
|
#ifndef USE_IDN
|
|
|
|
return CURLUE_LACKS_IDN;
|
|
|
|
#else
|
|
|
|
char *allochost = Curl_idn_decode(*part);
|
|
|
|
if(!allochost)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
free(*part);
|
|
|
|
*part = allochost;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 22:32:46 +08:00
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ifmissing;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLUcode curl_url_set(CURLU *u, CURLUPart what,
|
|
|
|
const char *part, unsigned int flags)
|
|
|
|
{
|
|
|
|
char **storep = NULL;
|
|
|
|
long port = 0;
|
|
|
|
bool urlencode = (flags & CURLU_URLENCODE)? 1 : 0;
|
|
|
|
bool plusencode = FALSE;
|
|
|
|
bool urlskipslash = FALSE;
|
|
|
|
bool appendquery = FALSE;
|
2018-11-07 06:48:35 +08:00
|
|
|
bool equalsencode = FALSE;
|
2018-08-05 17:51:07 +08:00
|
|
|
|
|
|
|
if(!u)
|
|
|
|
return CURLUE_BAD_HANDLE;
|
|
|
|
if(!part) {
|
|
|
|
/* setting a part to NULL clears it */
|
|
|
|
switch(what) {
|
|
|
|
case CURLUPART_URL:
|
|
|
|
break;
|
|
|
|
case CURLUPART_SCHEME:
|
|
|
|
storep = &u->scheme;
|
|
|
|
break;
|
|
|
|
case CURLUPART_USER:
|
|
|
|
storep = &u->user;
|
|
|
|
break;
|
|
|
|
case CURLUPART_PASSWORD:
|
|
|
|
storep = &u->password;
|
|
|
|
break;
|
|
|
|
case CURLUPART_OPTIONS:
|
|
|
|
storep = &u->options;
|
|
|
|
break;
|
|
|
|
case CURLUPART_HOST:
|
|
|
|
storep = &u->host;
|
|
|
|
break;
|
2019-05-03 19:18:12 +08:00
|
|
|
case CURLUPART_ZONEID:
|
|
|
|
storep = &u->zoneid;
|
|
|
|
break;
|
2018-08-05 17:51:07 +08:00
|
|
|
case CURLUPART_PORT:
|
2019-04-11 19:20:15 +08:00
|
|
|
u->portnum = 0;
|
2018-08-05 17:51:07 +08:00
|
|
|
storep = &u->port;
|
|
|
|
break;
|
|
|
|
case CURLUPART_PATH:
|
|
|
|
storep = &u->path;
|
|
|
|
break;
|
|
|
|
case CURLUPART_QUERY:
|
|
|
|
storep = &u->query;
|
|
|
|
break;
|
|
|
|
case CURLUPART_FRAGMENT:
|
|
|
|
storep = &u->fragment;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CURLUE_UNKNOWN_PART;
|
|
|
|
}
|
|
|
|
if(storep && *storep) {
|
2020-09-17 03:33:27 +08:00
|
|
|
Curl_safefree(*storep);
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
2022-06-19 22:54:04 +08:00
|
|
|
else if(!storep) {
|
|
|
|
free_urlhandle(u);
|
|
|
|
memset(u, 0, sizeof(struct Curl_URL));
|
|
|
|
}
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(what) {
|
|
|
|
case CURLUPART_SCHEME:
|
2019-05-19 07:48:00 +08:00
|
|
|
if(strlen(part) > MAX_SCHEME_LEN)
|
|
|
|
/* too long */
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_SCHEME;
|
2018-08-05 17:51:07 +08:00
|
|
|
if(!(flags & CURLU_NON_SUPPORT_SCHEME) &&
|
|
|
|
/* verify that it is a fine scheme */
|
2022-09-15 19:30:09 +08:00
|
|
|
!Curl_builtin_scheme(part, CURL_ZERO_TERMINATED))
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_UNSUPPORTED_SCHEME;
|
|
|
|
storep = &u->scheme;
|
|
|
|
urlencode = FALSE; /* never */
|
|
|
|
break;
|
|
|
|
case CURLUPART_USER:
|
|
|
|
storep = &u->user;
|
|
|
|
break;
|
|
|
|
case CURLUPART_PASSWORD:
|
|
|
|
storep = &u->password;
|
|
|
|
break;
|
|
|
|
case CURLUPART_OPTIONS:
|
|
|
|
storep = &u->options;
|
|
|
|
break;
|
2021-10-09 02:12:26 +08:00
|
|
|
case CURLUPART_HOST: {
|
|
|
|
size_t len = strcspn(part, " \r\n");
|
|
|
|
if(strlen(part) != len)
|
|
|
|
/* hostname with bad content */
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_HOSTNAME;
|
2018-08-05 17:51:07 +08:00
|
|
|
storep = &u->host;
|
2020-09-17 03:33:27 +08:00
|
|
|
Curl_safefree(u->zoneid);
|
2019-05-03 19:18:12 +08:00
|
|
|
break;
|
2021-10-09 02:12:26 +08:00
|
|
|
}
|
2019-05-03 19:18:12 +08:00
|
|
|
case CURLUPART_ZONEID:
|
|
|
|
storep = &u->zoneid;
|
2018-08-05 17:51:07 +08:00
|
|
|
break;
|
|
|
|
case CURLUPART_PORT:
|
2019-04-11 19:20:15 +08:00
|
|
|
{
|
|
|
|
char *endp;
|
2018-08-05 17:51:07 +08:00
|
|
|
urlencode = FALSE; /* never */
|
2019-04-11 19:20:15 +08:00
|
|
|
port = strtol(part, &endp, 10); /* Port number must be decimal */
|
2018-08-05 17:51:07 +08:00
|
|
|
if((port <= 0) || (port > 0xffff))
|
|
|
|
return CURLUE_BAD_PORT_NUMBER;
|
2019-04-11 19:20:15 +08:00
|
|
|
if(*endp)
|
|
|
|
/* weirdly provided number, not good! */
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_PORT_NUMBER;
|
2018-08-05 17:51:07 +08:00
|
|
|
storep = &u->port;
|
2019-04-11 19:20:15 +08:00
|
|
|
}
|
|
|
|
break;
|
2018-08-05 17:51:07 +08:00
|
|
|
case CURLUPART_PATH:
|
|
|
|
urlskipslash = TRUE;
|
|
|
|
storep = &u->path;
|
|
|
|
break;
|
|
|
|
case CURLUPART_QUERY:
|
|
|
|
plusencode = urlencode;
|
|
|
|
appendquery = (flags & CURLU_APPENDQUERY)?1:0;
|
2018-11-07 06:48:35 +08:00
|
|
|
equalsencode = appendquery;
|
2018-08-05 17:51:07 +08:00
|
|
|
storep = &u->query;
|
|
|
|
break;
|
|
|
|
case CURLUPART_FRAGMENT:
|
|
|
|
storep = &u->fragment;
|
|
|
|
break;
|
|
|
|
case CURLUPART_URL: {
|
|
|
|
/*
|
|
|
|
* Allow a new URL to replace the existing (if any) contents.
|
|
|
|
*
|
|
|
|
* If the existing contents is enough for a URL, allow a relative URL to
|
|
|
|
* replace it.
|
|
|
|
*/
|
|
|
|
CURLUcode result;
|
|
|
|
char *oldurl;
|
|
|
|
char *redired_url;
|
|
|
|
|
2022-02-14 18:43:04 +08:00
|
|
|
/* if the new thing is absolute or the old one is not
|
|
|
|
* (we could not get an absolute url in 'oldurl'),
|
|
|
|
* then replace the existing with the new. */
|
2022-09-14 15:18:30 +08:00
|
|
|
if(Curl_is_absolute_url(part, NULL, 0,
|
|
|
|
flags & (CURLU_GUESS_SCHEME|
|
|
|
|
CURLU_DEFAULT_SCHEME))
|
2022-02-14 18:43:04 +08:00
|
|
|
|| curl_url_get(u, CURLUPART_URL, &oldurl, flags)) {
|
|
|
|
return parseurl_and_replace(part, u, flags);
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:43:04 +08:00
|
|
|
/* apply the relative part to create a new URL
|
|
|
|
* and replace the existing one with it. */
|
2019-02-08 16:33:42 +08:00
|
|
|
redired_url = concat_url(oldurl, part);
|
2018-08-05 17:51:07 +08:00
|
|
|
free(oldurl);
|
|
|
|
if(!redired_url)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
|
2022-02-14 18:43:04 +08:00
|
|
|
result = parseurl_and_replace(redired_url, u, flags);
|
2018-08-05 17:51:07 +08:00
|
|
|
free(redired_url);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return CURLUE_UNKNOWN_PART;
|
|
|
|
}
|
2019-09-19 16:20:35 +08:00
|
|
|
DEBUGASSERT(storep);
|
|
|
|
{
|
2018-08-05 17:51:07 +08:00
|
|
|
const char *newp = part;
|
|
|
|
size_t nalloc = strlen(part);
|
|
|
|
|
2019-04-29 14:00:49 +08:00
|
|
|
if(nalloc > CURL_MAX_INPUT_LENGTH)
|
|
|
|
/* excessive input length */
|
|
|
|
return CURLUE_MALFORMED_INPUT;
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
if(urlencode) {
|
2019-04-06 19:48:18 +08:00
|
|
|
const unsigned char *i;
|
2022-09-01 16:16:24 +08:00
|
|
|
struct dynbuf enc;
|
|
|
|
|
|
|
|
Curl_dyn_init(&enc, nalloc * 3 + 1);
|
|
|
|
|
|
|
|
for(i = (const unsigned char *)part; *i; i++) {
|
|
|
|
CURLcode result;
|
2020-10-15 18:22:27 +08:00
|
|
|
if((*i == ' ') && plusencode) {
|
2022-09-01 16:16:24 +08:00
|
|
|
result = Curl_dyn_addn(&enc, "+", 1);
|
|
|
|
if(result)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2020-10-15 18:22:27 +08:00
|
|
|
}
|
|
|
|
else if(Curl_isunreserved(*i) ||
|
|
|
|
((*i == '/') && urlskipslash) ||
|
|
|
|
((*i == '=') && equalsencode)) {
|
2018-11-07 06:48:35 +08:00
|
|
|
if((*i == '=') && equalsencode)
|
|
|
|
/* only skip the first equals sign */
|
|
|
|
equalsencode = FALSE;
|
2022-09-01 16:16:24 +08:00
|
|
|
result = Curl_dyn_addn(&enc, i, 1);
|
|
|
|
if(result)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
else {
|
2022-09-01 16:16:24 +08:00
|
|
|
result = Curl_dyn_addf(&enc, "%%%02x", *i);
|
|
|
|
if(result)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
newp = Curl_dyn_ptr(&enc);
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *p;
|
|
|
|
newp = strdup(part);
|
|
|
|
if(!newp)
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
|
|
|
p = (char *)newp;
|
|
|
|
while(*p) {
|
|
|
|
/* make sure percent encoded are lower case */
|
|
|
|
if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
|
|
|
|
(ISUPPER(p[1]) || ISUPPER(p[2]))) {
|
2022-09-06 06:17:36 +08:00
|
|
|
p[1] = Curl_raw_tolower(p[1]);
|
|
|
|
p[2] = Curl_raw_tolower(p[2]);
|
2018-08-05 17:51:07 +08:00
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(appendquery) {
|
2022-09-01 16:16:24 +08:00
|
|
|
/* Append the 'newp' string onto the old query. Add a '&' separator if
|
|
|
|
none is present at the end of the existing query already */
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
size_t querylen = u->query ? strlen(u->query) : 0;
|
|
|
|
bool addamperand = querylen && (u->query[querylen -1] != '&');
|
|
|
|
if(querylen) {
|
2022-09-01 16:16:24 +08:00
|
|
|
struct dynbuf enc;
|
|
|
|
Curl_dyn_init(&enc, CURL_MAX_INPUT_LENGTH);
|
|
|
|
|
|
|
|
if(Curl_dyn_addn(&enc, u->query, querylen)) /* add original query */
|
|
|
|
goto nomem;
|
|
|
|
|
|
|
|
if(addamperand) {
|
|
|
|
if(Curl_dyn_addn(&enc, "&", 1))
|
|
|
|
goto nomem;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
2022-09-01 16:16:24 +08:00
|
|
|
if(Curl_dyn_add(&enc, newp))
|
|
|
|
goto nomem;
|
2018-08-05 17:51:07 +08:00
|
|
|
free((char *)newp);
|
|
|
|
free(*storep);
|
2022-09-01 16:16:24 +08:00
|
|
|
*storep = Curl_dyn_ptr(&enc);
|
2018-08-05 17:51:07 +08:00
|
|
|
return CURLUE_OK;
|
2022-09-01 16:16:24 +08:00
|
|
|
nomem:
|
|
|
|
free((char *)newp);
|
|
|
|
return CURLUE_OUT_OF_MEMORY;
|
2018-08-05 17:51:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 22:59:08 +08:00
|
|
|
if(what == CURLUPART_HOST) {
|
2022-09-01 16:16:24 +08:00
|
|
|
size_t n = strlen(newp);
|
|
|
|
if(!n && (flags & CURLU_NO_AUTHORITY)) {
|
2019-09-19 21:54:53 +08:00
|
|
|
/* Skip hostname check, it's allowed to be empty. */
|
|
|
|
}
|
|
|
|
else {
|
2022-09-01 16:16:24 +08:00
|
|
|
if(hostname_check(u, (char *)newp, n)) {
|
2019-09-19 21:54:53 +08:00
|
|
|
free((char *)newp);
|
2021-11-24 00:07:31 +08:00
|
|
|
return CURLUE_BAD_HOSTNAME;
|
2019-09-19 21:54:53 +08:00
|
|
|
}
|
2019-04-30 22:59:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 17:51:07 +08:00
|
|
|
free(*storep);
|
|
|
|
*storep = (char *)newp;
|
|
|
|
}
|
|
|
|
/* set after the string, to make it not assigned if the allocation above
|
|
|
|
fails */
|
|
|
|
if(port)
|
|
|
|
u->portnum = port;
|
|
|
|
return CURLUE_OK;
|
|
|
|
}
|