2004-03-02 21:02:41 +03:00
|
|
|
/*
|
2006-06-27 21:59:32 +04:00
|
|
|
* Copyright 2006 Richard Wilson <info@tinct.net>
|
2005-04-30 18:31:48 +04:00
|
|
|
* Copyright 2005 James Bursa <bursa@users.sourceforge.net>
|
|
|
|
* Copyright 2005 John M Bell <jmb202@ecs.soton.ac.uk>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2004-03-02 21:02:41 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
2014-05-08 03:16:50 +04:00
|
|
|
* \brief Implementation of URL parsing and joining operations.
|
2004-03-02 21:02:41 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2014-05-02 15:26:25 +04:00
|
|
|
#include <curl/curl.h>
|
2011-02-07 17:41:44 +03:00
|
|
|
|
2010-12-05 22:16:49 +03:00
|
|
|
#include "utils/config.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utils.h"
|
2014-05-02 15:26:25 +04:00
|
|
|
#include "utils/url.h"
|
2004-03-02 21:02:41 +03:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
regex_t url_re, url_up_re;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
/* exported interface documented in utils/url.h */
|
2014-10-26 01:06:18 +03:00
|
|
|
nserror url_init(void)
|
2004-03-02 21:02:41 +03:00
|
|
|
{
|
2014-10-26 01:06:18 +03:00
|
|
|
nserror ret;
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
/* regex from RFC 2396 */
|
2014-10-26 01:06:18 +03:00
|
|
|
ret = regcomp_wrapper(&url_re, "^[[:space:]]*"
|
2005-04-30 18:31:48 +04:00
|
|
|
#define URL_RE_SCHEME 2
|
|
|
|
"(([a-zA-Z][-a-zA-Z0-9+.]*):)?"
|
|
|
|
#define URL_RE_AUTHORITY 4
|
|
|
|
"(//([^/?#[:space:]]*))?"
|
|
|
|
#define URL_RE_PATH 5
|
|
|
|
"([^?#[:space:]]*)"
|
|
|
|
#define URL_RE_QUERY 7
|
|
|
|
"(\\?([^#[:space:]]*))?"
|
|
|
|
#define URL_RE_FRAGMENT 9
|
|
|
|
"(#([^[:space:]]*))?"
|
2004-06-08 14:56:21 +04:00
|
|
|
"[[:space:]]*$", REG_EXTENDED);
|
2014-10-26 01:06:18 +03:00
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return regcomp_wrapper(&url_up_re,
|
2006-04-22 13:07:28 +04:00
|
|
|
"/([^/]?|[.][^./]|[^./][.]|[^./][^./]|[^/][^/][^/]+)"
|
2005-04-30 18:31:48 +04:00
|
|
|
"/[.][.](/|$)",
|
2004-03-02 21:02:41 +03:00
|
|
|
REG_EXTENDED);
|
|
|
|
}
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
/* exported interface documented in utils/url.h */
|
2010-12-04 17:47:03 +03:00
|
|
|
bool url_host_is_ip_address(const char *host)
|
|
|
|
{
|
|
|
|
struct in_addr ipv4;
|
2011-01-13 00:48:07 +03:00
|
|
|
size_t host_len = strlen(host);
|
2010-12-04 19:35:48 +03:00
|
|
|
const char *sane_host;
|
|
|
|
const char *slash;
|
2010-12-04 17:47:03 +03:00
|
|
|
#ifndef NO_IPV6
|
|
|
|
struct in6_addr ipv6;
|
2010-12-04 19:35:48 +03:00
|
|
|
char ipv6_addr[64];
|
2010-12-04 17:47:03 +03:00
|
|
|
#endif
|
2014-05-08 03:16:50 +04:00
|
|
|
/** @todo FIXME Some parts of urldb.c (and perhaps other parts of
|
2010-12-04 19:35:48 +03:00
|
|
|
* NetSurf) make confusions between hosts and "prefixes", we can
|
|
|
|
* sometimes be erroneously passed more than just a host. Sometimes
|
|
|
|
* we may be passed trailing slashes, or even whole path segments.
|
|
|
|
* A specific criminal in this class is urldb_iterate_partial, which
|
|
|
|
* takes a prefix to search for, but passes that prefix to functions
|
|
|
|
* that expect only hosts.
|
|
|
|
*
|
|
|
|
* For the time being, we will accept such calls; we check if there
|
|
|
|
* is a / in the host parameter, and if there is, we take a copy and
|
|
|
|
* replace the / with a \0. This is not a permanent solution; we
|
|
|
|
* should search through NetSurf and find all the callers that are
|
|
|
|
* in error and fix them. When doing this task, it might be wise
|
|
|
|
* to replace the hideousness below with code that doesn't have to do
|
|
|
|
* this, and add assert(strchr(host, '/') == NULL); somewhere.
|
|
|
|
* -- rjek - 2010-11-04
|
|
|
|
*/
|
|
|
|
|
2011-01-13 00:48:07 +03:00
|
|
|
slash = strchr(host, '/');
|
2010-12-04 19:35:48 +03:00
|
|
|
if (slash == NULL) {
|
|
|
|
sane_host = host;
|
|
|
|
} else {
|
|
|
|
char *c = strdup(host);
|
|
|
|
c[slash - host] = '\0';
|
|
|
|
sane_host = c;
|
2011-01-13 00:48:07 +03:00
|
|
|
host_len = slash - host - 1;
|
|
|
|
LOG(("WARNING: called with non-host '%s'", host));
|
2010-12-04 19:35:48 +03:00
|
|
|
}
|
|
|
|
|
2011-01-13 00:48:07 +03:00
|
|
|
if (strspn(sane_host, "0123456789abcdefABCDEF[].:") < host_len)
|
2010-12-04 19:35:48 +03:00
|
|
|
goto out_false;
|
2010-11-26 18:36:27 +03:00
|
|
|
|
2011-01-13 00:48:07 +03:00
|
|
|
if (inet_aton(sane_host, &ipv4) != 0) {
|
|
|
|
/* This can only be a sane IPv4 address if it contains 3 dots.
|
|
|
|
* Helpfully, inet_aton is happy to treat "a", "a.b", "a.b.c",
|
|
|
|
* and "a.b.c.d" as valid IPv4 address strings where we only
|
|
|
|
* support the full, dotted-quad, form.
|
|
|
|
*/
|
|
|
|
int num_dots = 0;
|
|
|
|
size_t index;
|
|
|
|
|
|
|
|
for (index = 0; index < host_len; index++) {
|
|
|
|
if (sane_host[index] == '.')
|
|
|
|
num_dots++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_dots == 3)
|
|
|
|
goto out_true;
|
|
|
|
else
|
|
|
|
goto out_false;
|
|
|
|
}
|
|
|
|
|
2010-12-04 17:47:03 +03:00
|
|
|
#ifndef NO_IPV6
|
2011-01-13 00:48:07 +03:00
|
|
|
if (sane_host[0] != '[' || sane_host[host_len] != ']')
|
2010-12-04 19:35:48 +03:00
|
|
|
goto out_false;
|
|
|
|
|
|
|
|
strncpy(ipv6_addr, sane_host + 1, sizeof(ipv6_addr));
|
2011-01-13 00:48:07 +03:00
|
|
|
ipv6_addr[sizeof(ipv6_addr) - 1] = '\0';
|
2010-12-04 19:35:48 +03:00
|
|
|
|
|
|
|
if (inet_pton(AF_INET6, ipv6_addr, &ipv6) == 1)
|
|
|
|
goto out_true;
|
2010-12-04 17:47:03 +03:00
|
|
|
#endif
|
2010-11-26 18:36:27 +03:00
|
|
|
|
2010-12-04 19:35:48 +03:00
|
|
|
out_false:
|
|
|
|
if (slash != NULL) free((void *)sane_host);
|
2010-12-04 17:47:03 +03:00
|
|
|
return false;
|
2010-12-04 19:35:48 +03:00
|
|
|
|
|
|
|
out_true:
|
|
|
|
if (slash != NULL) free((void *)sane_host);
|
|
|
|
return true;
|
2006-12-02 00:09:49 +03:00
|
|
|
}
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
/* exported interface documented in utils/url.h */
|
|
|
|
nserror url_nice(const char *url, char **result,
|
2005-07-24 00:43:37 +04:00
|
|
|
bool remove_extensions)
|
2004-03-28 03:18:52 +04:00
|
|
|
{
|
|
|
|
int m;
|
|
|
|
regmatch_t match[10];
|
2005-07-24 00:43:37 +04:00
|
|
|
regoff_t start, end;
|
|
|
|
size_t i;
|
|
|
|
char *dot;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
*result = 0;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
m = regexec(&url_re, url, 10, match, 0);
|
|
|
|
if (m) {
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOT_FOUND;
|
2004-03-28 03:18:52 +04:00
|
|
|
}
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
/* extract the last component of the path, if possible */
|
|
|
|
if (match[URL_RE_PATH].rm_so == -1 || match[URL_RE_PATH].rm_so ==
|
|
|
|
match[URL_RE_PATH].rm_eo)
|
|
|
|
goto no_path; /* no path, or empty */
|
|
|
|
for (end = match[URL_RE_PATH].rm_eo - 1;
|
|
|
|
end != match[URL_RE_PATH].rm_so && url[end] == '/';
|
|
|
|
end--)
|
|
|
|
;
|
|
|
|
if (end == match[URL_RE_PATH].rm_so)
|
|
|
|
goto no_path; /* path is a string of '/' */
|
|
|
|
end++;
|
|
|
|
for (start = end - 1;
|
|
|
|
start != match[URL_RE_PATH].rm_so && url[start] != '/';
|
|
|
|
start--)
|
|
|
|
;
|
|
|
|
if (url[start] == '/')
|
|
|
|
start++;
|
|
|
|
|
|
|
|
if (!strncasecmp(url + start, "index.", 6) ||
|
|
|
|
!strncasecmp(url + start, "default.", 8)) {
|
|
|
|
/* try again */
|
|
|
|
if (start == match[URL_RE_PATH].rm_so)
|
|
|
|
goto no_path;
|
|
|
|
for (end = start - 1;
|
|
|
|
end != match[URL_RE_PATH].rm_so &&
|
|
|
|
url[end] == '/';
|
|
|
|
end--)
|
|
|
|
;
|
|
|
|
if (end == match[URL_RE_PATH].rm_so)
|
|
|
|
goto no_path;
|
|
|
|
end++;
|
|
|
|
for (start = end - 1;
|
|
|
|
start != match[URL_RE_PATH].rm_so &&
|
|
|
|
url[start] != '/';
|
|
|
|
start--)
|
|
|
|
;
|
|
|
|
if (url[start] == '/')
|
|
|
|
start++;
|
|
|
|
}
|
2004-03-28 03:18:52 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
*result = malloc(end - start + 1);
|
|
|
|
if (!*result) {
|
|
|
|
LOG(("malloc failed"));
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOMEM;
|
2005-07-24 00:43:37 +04:00
|
|
|
}
|
|
|
|
strncpy(*result, url + start, end - start);
|
|
|
|
(*result)[end - start] = 0;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
if (remove_extensions) {
|
|
|
|
dot = strchr(*result, '.');
|
|
|
|
if (dot && dot != *result)
|
|
|
|
*dot = 0;
|
2004-03-28 03:18:52 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_OK;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
no_path:
|
2004-08-09 20:11:58 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
/* otherwise, use the host name, with '.' replaced by '_' */
|
|
|
|
if (match[URL_RE_AUTHORITY].rm_so != -1 &&
|
|
|
|
match[URL_RE_AUTHORITY].rm_so !=
|
|
|
|
match[URL_RE_AUTHORITY].rm_eo) {
|
|
|
|
*result = malloc(match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
match[URL_RE_AUTHORITY].rm_so + 1);
|
|
|
|
if (!*result) {
|
|
|
|
LOG(("malloc failed"));
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOMEM;
|
2005-07-24 00:43:37 +04:00
|
|
|
}
|
|
|
|
strncpy(*result, url + match[URL_RE_AUTHORITY].rm_so,
|
|
|
|
match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
match[URL_RE_AUTHORITY].rm_so);
|
|
|
|
(*result)[match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
match[URL_RE_AUTHORITY].rm_so] = 0;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
for (i = 0; (*result)[i]; i++)
|
|
|
|
if ((*result)[i] == '.')
|
|
|
|
(*result)[i] = '_';
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_OK;
|
2006-02-19 21:26:23 +03:00
|
|
|
}
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOT_FOUND;
|
2004-03-28 03:18:52 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
|
|
|
|
/* exported interface documented in utils/url.h */
|
|
|
|
nserror url_unescape(const char *str, char **result)
|
2010-09-10 01:45:59 +04:00
|
|
|
{
|
|
|
|
char *curlstr;
|
|
|
|
char *retstr;
|
|
|
|
|
|
|
|
curlstr = curl_unescape(str, 0);
|
|
|
|
if (curlstr == NULL) {
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOMEM;
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
retstr = strdup(curlstr);
|
|
|
|
curl_free(curlstr);
|
|
|
|
|
|
|
|
if (retstr == NULL) {
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOMEM;
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = retstr;
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_OK;
|
2010-09-10 01:45:59 +04:00
|
|
|
}
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
/* exported interface documented in utils/url.h */
|
|
|
|
nserror url_escape(const char *unescaped, size_t toskip,
|
2008-04-02 04:43:51 +04:00
|
|
|
bool sptoplus, const char *escexceptions, char **result)
|
2005-06-27 02:18:37 +04:00
|
|
|
{
|
2008-04-02 04:43:51 +04:00
|
|
|
size_t len;
|
|
|
|
char *escaped, *d, *tmpres;
|
2005-06-27 02:18:37 +04:00
|
|
|
const char *c;
|
|
|
|
|
|
|
|
if (!unescaped || !result)
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOT_FOUND;
|
2005-06-27 02:18:37 +04:00
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
len = strlen(unescaped);
|
2008-04-02 04:43:51 +04:00
|
|
|
if (len < toskip)
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOT_FOUND;
|
2008-04-02 04:43:51 +04:00
|
|
|
len -= toskip;
|
2005-06-27 02:18:37 +04:00
|
|
|
|
|
|
|
escaped = malloc(len * 3 + 1);
|
|
|
|
if (!escaped)
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOMEM;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
2008-04-02 04:43:51 +04:00
|
|
|
for (c = unescaped + toskip, d = escaped; *c; c++) {
|
2007-02-12 01:28:00 +03:00
|
|
|
/* Check if we should escape this byte.
|
|
|
|
* '~' is unreserved and should not be percent encoded, if
|
|
|
|
* you believe the spec; however, leaving it unescaped
|
|
|
|
* breaks a bunch of websites, so we escape it anyway. */
|
2008-04-02 04:43:51 +04:00
|
|
|
if (!isascii(*c)
|
|
|
|
|| (strchr(":/?#[]@" /* gen-delims */
|
|
|
|
"!$&'()*+,;=" /* sub-delims */
|
|
|
|
"<>%\"{}|\\^`~" /* others */, *c)
|
|
|
|
&& (!escexceptions || !strchr(escexceptions, *c)))
|
|
|
|
|| *c <= 0x20 || *c == 0x7f) {
|
2007-02-12 01:28:00 +03:00
|
|
|
if (*c == 0x20 && sptoplus) {
|
2006-09-19 14:08:33 +04:00
|
|
|
*d++ = '+';
|
2007-02-12 01:28:00 +03:00
|
|
|
} else {
|
2006-09-19 14:08:33 +04:00
|
|
|
*d++ = '%';
|
|
|
|
*d++ = "0123456789ABCDEF"[((*c >> 4) & 0xf)];
|
|
|
|
*d++ = "0123456789ABCDEF"[(*c & 0xf)];
|
|
|
|
}
|
2007-02-12 01:28:00 +03:00
|
|
|
} else {
|
|
|
|
/* unreserved characters: [a-zA-Z0-9-._] */
|
2005-06-27 02:18:37 +04:00
|
|
|
*d++ = *c;
|
|
|
|
}
|
|
|
|
}
|
2005-06-27 05:57:54 +04:00
|
|
|
*d++ = '\0';
|
|
|
|
|
2008-04-02 04:43:51 +04:00
|
|
|
tmpres = malloc(d - escaped + toskip);
|
|
|
|
if (!tmpres) {
|
2005-06-27 02:18:37 +04:00
|
|
|
free(escaped);
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_NOMEM;
|
2005-06-27 02:18:37 +04:00
|
|
|
}
|
2005-06-27 05:57:54 +04:00
|
|
|
|
2008-04-02 04:43:51 +04:00
|
|
|
memcpy(tmpres, unescaped, toskip);
|
|
|
|
memcpy(tmpres + toskip, escaped, d - escaped);
|
|
|
|
*result = tmpres;
|
2005-06-27 02:18:37 +04:00
|
|
|
|
|
|
|
free(escaped);
|
|
|
|
|
2014-05-08 03:16:50 +04:00
|
|
|
return NSERROR_OK;
|
2005-06-27 02:18:37 +04:00
|
|
|
}
|