2004-03-02 21:02:41 +03:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
|
* Licensed under the GNU General Public License,
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
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>
|
2004-03-02 21:02:41 +03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* URL parsing and joining (implementation).
|
|
|
|
|
*/
|
|
|
|
|
|
2004-03-28 03:18:52 +04:00
|
|
|
|
#include <assert.h>
|
2004-03-02 21:02:41 +03:00
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2005-07-24 00:43:37 +04:00
|
|
|
|
#include <strings.h>
|
2004-03-02 21:02:41 +03:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <regex.h>
|
|
|
|
|
#include "netsurf/utils/log.h"
|
|
|
|
|
#include "netsurf/utils/url.h"
|
|
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
regex_t url_re, url_up_re;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialise URL routines.
|
|
|
|
|
*
|
|
|
|
|
* Compiles regular expressions required by the url_ functions.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void url_init(void)
|
|
|
|
|
{
|
|
|
|
|
/* regex from RFC 2396 */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
regcomp_wrapper(&url_re, "^[[:space:]]*"
|
|
|
|
|
#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);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalize a URL.
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold cleaned up url
|
|
|
|
|
* \return URL_FUNC_OK on success
|
2004-03-02 21:02:41 +03:00
|
|
|
|
*
|
|
|
|
|
* If there is no scheme, http:// is added. The scheme and host are
|
|
|
|
|
* lower-cased. Default ports are removed (http only). An empty path is
|
|
|
|
|
* replaced with "/". Characters are unescaped if safe.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
url_func_result url_normalize(const char *url, char **result)
|
2004-03-02 21:02:41 +03:00
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
int m;
|
|
|
|
|
int i;
|
2004-08-14 19:07:21 +04:00
|
|
|
|
size_t len;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
bool http = false;
|
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
2004-08-14 19:07:21 +04:00
|
|
|
|
*result = NULL;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
|
2005-01-02 17:41:07 +03:00
|
|
|
|
if ((m = regexec(&url_re, url, 10, match, 0))) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = strlen(url);
|
|
|
|
|
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (match[URL_RE_SCHEME].rm_so == -1) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
/* scheme missing: add http:// and reparse */
|
2005-02-03 16:18:22 +03:00
|
|
|
|
/* LOG(("scheme missing: using http"));*/
|
2004-08-14 19:07:21 +04:00
|
|
|
|
if ((*result = malloc(len + 13)) == NULL) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
LOG(("malloc failed"));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_NOMEM;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2004-08-14 19:07:21 +04:00
|
|
|
|
strcpy(*result, "http://");
|
|
|
|
|
strcpy(*result + sizeof("http://")-1, url);
|
2005-01-02 17:41:07 +03:00
|
|
|
|
if ((m = regexec(&url_re, *result, 10, match, 0))) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
LOG(("url '%s' failed to match regex", (*result)));
|
2004-08-14 19:07:21 +04:00
|
|
|
|
free(*result);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2004-08-14 19:07:21 +04:00
|
|
|
|
len += sizeof("http://")-1;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
} else {
|
2004-08-14 19:07:21 +04:00
|
|
|
|
if ((*result = malloc(len + 6)) == NULL) {
|
2005-02-15 00:20:26 +03:00
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2004-08-14 19:07:21 +04:00
|
|
|
|
strcpy(*result, url);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*for (unsigned int i = 0; i != 10; i++) {
|
|
|
|
|
if (match[i].rm_so == -1)
|
|
|
|
|
continue;
|
|
|
|
|
fprintf(stderr, "%i: '%.*s'\n", i,
|
|
|
|
|
match[i].rm_eo - match[i].rm_so,
|
|
|
|
|
res + match[i].rm_so);
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
/* see RFC 2616 section 3.2.3 */
|
|
|
|
|
/* make scheme lower-case */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (match[URL_RE_SCHEME].rm_so != -1) {
|
|
|
|
|
for (i = match[URL_RE_SCHEME].rm_so;
|
|
|
|
|
i != match[URL_RE_SCHEME].rm_eo; i++)
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i] = tolower((*result)[i]);
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (match[URL_RE_SCHEME].rm_eo == 4
|
2004-08-14 19:07:21 +04:00
|
|
|
|
&& (*result)[0] == 'h'
|
|
|
|
|
&& (*result)[1] == 't'
|
|
|
|
|
&& (*result)[2] == 't'
|
|
|
|
|
&& (*result)[3] == 'p')
|
2004-03-02 21:02:41 +03:00
|
|
|
|
http = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* make empty path into "/" */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (match[URL_RE_PATH].rm_so != -1 &&
|
|
|
|
|
match[URL_RE_PATH].rm_so == match[URL_RE_PATH].rm_eo) {
|
|
|
|
|
memmove((*result) + match[URL_RE_PATH].rm_so + 1,
|
|
|
|
|
(*result) + match[URL_RE_PATH].rm_so,
|
|
|
|
|
len - match[URL_RE_PATH].rm_so + 1);
|
|
|
|
|
(*result)[match[URL_RE_PATH].rm_so] = '/';
|
2004-03-02 21:02:41 +03:00
|
|
|
|
len++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* make host lower-case */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (match[URL_RE_AUTHORITY].rm_so != -1) {
|
|
|
|
|
for (i = match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
i != match[URL_RE_AUTHORITY].rm_eo; i++) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
if ((*result)[i] == ':') {
|
|
|
|
|
if (http && (*result)[i + 1] == '8' &&
|
|
|
|
|
(*result)[i + 2] == '0' &&
|
2005-04-30 18:31:48 +04:00
|
|
|
|
i + 3 ==
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_eo) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
memmove((*result) + i,
|
|
|
|
|
(*result) + i + 3,
|
2005-04-30 18:31:48 +04:00
|
|
|
|
len -
|
|
|
|
|
match[URL_RE_AUTHORITY].
|
|
|
|
|
rm_eo);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
len -= 3;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[len] = '\0';
|
2004-03-02 21:02:41 +03:00
|
|
|
|
} else if (i + 1 == match[4].rm_eo) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
memmove((*result) + i,
|
|
|
|
|
(*result) + i + 1,
|
2005-04-30 18:31:48 +04:00
|
|
|
|
len -
|
|
|
|
|
match[URL_RE_AUTHORITY].
|
|
|
|
|
rm_eo);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
len--;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[len] = '\0';
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i] = tolower((*result)[i]);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* unescape non-"reserved" escaped characters */
|
2005-01-02 07:01:21 +03:00
|
|
|
|
for (i = 0; (unsigned)i != len; i++) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
if ((*result)[i] != '%')
|
2004-03-02 21:02:41 +03:00
|
|
|
|
continue;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
c = tolower((*result)[i + 1]);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
|
m = 16 * (c - '0');
|
|
|
|
|
else if ('a' <= c && c <= 'f')
|
|
|
|
|
m = 16 * (c - 'a' + 10);
|
|
|
|
|
else
|
|
|
|
|
continue;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
c = tolower((*result)[i + 2]);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
|
m += c - '0';
|
|
|
|
|
else if ('a' <= c && c <= 'f')
|
|
|
|
|
m += c - 'a' + 10;
|
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
|
2005-04-16 09:10:08 +04:00
|
|
|
|
if (m <= 0x20 || strchr(";/?:@&=+$," "<>#%\"{}|\\^[]`", m) ||
|
|
|
|
|
m >= 0x7f) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
i += 2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i] = m;
|
|
|
|
|
memmove((*result) + i + 1, (*result) + i + 3, len - i - 2);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
len -= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_OK;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve a relative URL to absolute form.
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param rel relative URL
|
|
|
|
|
* \param base base URL, must be absolute and cleaned as by url_normalize()
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold absolute url
|
|
|
|
|
* \return URL_FUNC_OK on success
|
2004-03-02 21:02:41 +03:00
|
|
|
|
*/
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
url_func_result url_join(const char *rel, const char *base, char **result)
|
2004-03-02 21:02:41 +03:00
|
|
|
|
{
|
|
|
|
|
int m;
|
|
|
|
|
int i, j;
|
|
|
|
|
char *buf = 0;
|
|
|
|
|
const char *scheme = 0, *authority = 0, *path = 0, *query = 0,
|
|
|
|
|
*fragment = 0;
|
|
|
|
|
int scheme_len = 0, authority_len = 0, path_len = 0, query_len = 0,
|
|
|
|
|
fragment_len = 0;
|
|
|
|
|
regmatch_t base_match[10];
|
|
|
|
|
regmatch_t rel_match[10];
|
|
|
|
|
regmatch_t up_match[3];
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result) = 0;
|
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
|
/* see RFC 2396 section 5.2 */
|
|
|
|
|
m = regexec(&url_re, base, 10, base_match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("base url '%s' failed to match regex", base));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
/*for (unsigned int i = 0; i != 10; i++) {
|
|
|
|
|
if (base_match[i].rm_so == -1)
|
|
|
|
|
continue;
|
|
|
|
|
fprintf(stderr, "%i: '%.*s'\n", i,
|
|
|
|
|
base_match[i].rm_eo - base_match[i].rm_so,
|
|
|
|
|
base + base_match[i].rm_so);
|
|
|
|
|
}*/
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (base_match[URL_RE_SCHEME].rm_so == -1) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
LOG(("base url '%s' is not absolute", base));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
scheme = base + base_match[URL_RE_SCHEME].rm_so;
|
|
|
|
|
scheme_len = base_match[URL_RE_SCHEME].rm_eo -
|
|
|
|
|
base_match[URL_RE_SCHEME].rm_so;
|
|
|
|
|
if (base_match[URL_RE_AUTHORITY].rm_so != -1) {
|
|
|
|
|
authority = base + base_match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
authority_len = base_match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
base_match[URL_RE_AUTHORITY].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
path = base + base_match[URL_RE_PATH].rm_so;
|
|
|
|
|
path_len = base_match[URL_RE_PATH].rm_eo -
|
|
|
|
|
base_match[URL_RE_PATH].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
2005-04-24 02:26:05 +04:00
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
|
/* 1) */
|
|
|
|
|
m = regexec(&url_re, rel, 10, rel_match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("relative url '%s' failed to match regex", rel));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 2) */
|
2004-08-07 02:20:36 +04:00
|
|
|
|
/* base + "#s" = (current document)#s (see Appendix C.1) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_FRAGMENT].rm_so != -1) {
|
|
|
|
|
fragment = rel + rel_match[URL_RE_FRAGMENT].rm_so;
|
|
|
|
|
fragment_len = rel_match[URL_RE_FRAGMENT].rm_eo -
|
|
|
|
|
rel_match[URL_RE_FRAGMENT].rm_so;
|
2004-08-07 02:20:36 +04:00
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_PATH].rm_so == rel_match[URL_RE_PATH].rm_eo &&
|
|
|
|
|
rel_match[URL_RE_SCHEME].rm_so == -1 &&
|
|
|
|
|
rel_match[URL_RE_AUTHORITY].rm_so == -1 &&
|
|
|
|
|
rel_match[URL_RE_QUERY].rm_so == -1) {
|
|
|
|
|
if (base_match[URL_RE_QUERY].rm_so != -1) {
|
|
|
|
|
/* normally the base query is discarded, but this is a
|
|
|
|
|
* "reference to the current document", so keep it */
|
|
|
|
|
query = base + base_match[URL_RE_QUERY].rm_so;
|
|
|
|
|
query_len = base_match[URL_RE_QUERY].rm_eo -
|
|
|
|
|
base_match[URL_RE_QUERY].rm_so;
|
2005-04-24 02:26:05 +04:00
|
|
|
|
}
|
2004-03-02 21:02:41 +03:00
|
|
|
|
goto step7;
|
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_QUERY].rm_so != -1) {
|
|
|
|
|
query = rel + rel_match[URL_RE_QUERY].rm_so;
|
|
|
|
|
query_len = rel_match[URL_RE_QUERY].rm_eo -
|
|
|
|
|
rel_match[URL_RE_QUERY].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2005-04-24 02:26:05 +04:00
|
|
|
|
/* base + "?y" = (base - query)?y
|
|
|
|
|
* e.g http://a/b/c/d;p?q + ?y = http://a/b/c/d;p?y */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_PATH].rm_so == rel_match[URL_RE_PATH].rm_eo &&
|
|
|
|
|
rel_match[URL_RE_SCHEME].rm_so == -1 &&
|
|
|
|
|
rel_match[URL_RE_AUTHORITY].rm_so == -1 &&
|
|
|
|
|
rel_match[URL_RE_QUERY].rm_so != -1)
|
2005-04-24 02:26:05 +04:00
|
|
|
|
goto step7;
|
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
|
/* 3) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_SCHEME].rm_so != -1) {
|
|
|
|
|
scheme = rel + rel_match[URL_RE_SCHEME].rm_so;
|
|
|
|
|
scheme_len = rel_match[URL_RE_SCHEME].rm_eo -
|
|
|
|
|
rel_match[URL_RE_SCHEME].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
authority = 0;
|
|
|
|
|
authority_len = 0;
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_AUTHORITY].rm_so != -1) {
|
|
|
|
|
authority = rel + rel_match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
authority_len = rel_match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
rel_match[URL_RE_AUTHORITY].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
path = rel + rel_match[URL_RE_PATH].rm_so;
|
|
|
|
|
path_len = rel_match[URL_RE_PATH].rm_eo -
|
|
|
|
|
rel_match[URL_RE_PATH].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
goto step7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 4) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel_match[URL_RE_AUTHORITY].rm_so != -1) {
|
|
|
|
|
authority = rel + rel_match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
authority_len = rel_match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
rel_match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
path = rel + rel_match[URL_RE_PATH].rm_so;
|
|
|
|
|
path_len = rel_match[URL_RE_PATH].rm_eo -
|
|
|
|
|
rel_match[URL_RE_PATH].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
goto step7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 5) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (rel[rel_match[URL_RE_PATH].rm_so] == '/') {
|
|
|
|
|
path = rel + rel_match[URL_RE_PATH].rm_so;
|
|
|
|
|
path_len = rel_match[URL_RE_PATH].rm_eo -
|
|
|
|
|
rel_match[URL_RE_PATH].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
goto step7;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 6) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
buf = malloc(path_len + rel_match[URL_RE_PATH].rm_eo + 10);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
if (!buf) {
|
|
|
|
|
LOG(("malloc failed"));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_NOMEM;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
/* a) */
|
|
|
|
|
strncpy(buf, path, path_len);
|
|
|
|
|
for (; path_len != 0 && buf[path_len - 1] != '/'; path_len--)
|
|
|
|
|
;
|
|
|
|
|
/* b) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
strncpy(buf + path_len, rel + rel_match[URL_RE_PATH].rm_so,
|
|
|
|
|
rel_match[URL_RE_PATH].rm_eo -
|
|
|
|
|
rel_match[URL_RE_PATH].rm_so);
|
|
|
|
|
path_len += rel_match[URL_RE_PATH].rm_eo - rel_match[URL_RE_PATH].rm_so;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
/* c) */
|
|
|
|
|
buf[path_len] = 0;
|
|
|
|
|
for (i = j = 0; j != path_len; ) {
|
|
|
|
|
if (j && buf[j - 1] == '/' && buf[j] == '.' &&
|
|
|
|
|
buf[j + 1] == '/')
|
|
|
|
|
j += 2;
|
|
|
|
|
else
|
|
|
|
|
buf[i++] = buf[j++];
|
|
|
|
|
}
|
|
|
|
|
path_len = i;
|
|
|
|
|
/* d) */
|
2004-05-07 23:04:59 +04:00
|
|
|
|
if (2 <= path_len && buf[path_len - 2] == '/' &&
|
|
|
|
|
buf[path_len - 1] == '.')
|
2004-03-02 21:02:41 +03:00
|
|
|
|
path_len--;
|
|
|
|
|
/* e) and f) */
|
|
|
|
|
while (1) {
|
|
|
|
|
buf[path_len] = 0;
|
|
|
|
|
m = regexec(&url_up_re, buf, 3, up_match, 0);
|
|
|
|
|
if (m)
|
|
|
|
|
break;
|
|
|
|
|
if (up_match[1].rm_eo + 4 <= path_len) {
|
|
|
|
|
memmove(buf + up_match[1].rm_so,
|
|
|
|
|
buf + up_match[1].rm_eo + 4,
|
|
|
|
|
path_len - up_match[1].rm_eo - 4);
|
|
|
|
|
path_len -= up_match[1].rm_eo - up_match[1].rm_so + 4;
|
|
|
|
|
} else
|
|
|
|
|
path_len -= up_match[1].rm_eo - up_match[1].rm_so + 3;
|
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
/* g) (choose to remove) */
|
2005-04-24 02:26:05 +04:00
|
|
|
|
path = buf;
|
2005-04-30 18:31:48 +04:00
|
|
|
|
while (3 <= path_len && path[1] == '.' && path[2] == '.') {
|
|
|
|
|
path += 3;
|
|
|
|
|
path_len -= 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf[path - buf + path_len] = 0;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
|
|
|
|
step7: /* 7) */
|
2005-04-30 18:31:48 +04:00
|
|
|
|
(*result) = malloc(scheme_len + 1 + 2 + authority_len + path_len + 1 +
|
|
|
|
|
1 + query_len + 1 + fragment_len + 1);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
if (!(*result)) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
free(buf);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_NOMEM;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
strncpy((*result), scheme, scheme_len);
|
|
|
|
|
(*result)[scheme_len] = ':';
|
2004-03-02 21:02:41 +03:00
|
|
|
|
i = scheme_len + 1;
|
|
|
|
|
if (authority) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i++] = '/';
|
|
|
|
|
(*result)[i++] = '/';
|
|
|
|
|
strncpy((*result) + i, authority, authority_len);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
i += authority_len;
|
|
|
|
|
}
|
2004-04-13 00:43:29 +04:00
|
|
|
|
if (path_len) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
strncpy((*result) + i, path, path_len);
|
2004-04-13 00:43:29 +04:00
|
|
|
|
i += path_len;
|
|
|
|
|
} else {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i++] = '/';
|
2004-04-13 00:43:29 +04:00
|
|
|
|
}
|
2004-03-02 21:02:41 +03:00
|
|
|
|
if (query) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i++] = '?';
|
|
|
|
|
strncpy((*result) + i, query, query_len);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
i += query_len;
|
|
|
|
|
}
|
|
|
|
|
if (fragment) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i++] = '#';
|
|
|
|
|
strncpy((*result) + i, fragment, fragment_len);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
i += fragment_len;
|
|
|
|
|
}
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result)[i] = 0;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_OK;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the host name from an URL.
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold host name
|
|
|
|
|
* \return URL_FUNC_OK on success
|
2004-03-02 21:02:41 +03:00
|
|
|
|
*/
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
url_func_result url_host(const char *url, char **result)
|
2004-03-02 21:02:41 +03:00
|
|
|
|
{
|
|
|
|
|
int m;
|
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
(*result) = 0;
|
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
|
m = regexec(&url_re, url, 10, match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
if (match[URL_RE_AUTHORITY].rm_so == -1)
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
2005-04-30 18:31:48 +04:00
|
|
|
|
(*result) = malloc(match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so + 1);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
if (!(*result)) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
LOG(("malloc failed"));
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_NOMEM;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
2005-04-30 18:31:48 +04:00
|
|
|
|
strncpy((*result), url + match[URL_RE_AUTHORITY].rm_so,
|
2005-07-24 00:43:37 +04:00
|
|
|
|
match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so);
|
2005-04-30 18:31:48 +04:00
|
|
|
|
(*result)[match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so] = 0;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
|
return URL_FUNC_OK;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
2004-10-02 01:31:55 +04:00
|
|
|
|
/**
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* Return the scheme name from an URL.
|
2006-06-27 04:53:39 +04:00
|
|
|
|
*
|
|
|
|
|
* See RFC 3986, 3.1 for reference.
|
2004-10-02 01:31:55 +04:00
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold scheme name
|
|
|
|
|
* \return URL_FUNC_OK on success
|
2004-10-02 01:31:55 +04:00
|
|
|
|
*/
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
2004-10-02 01:31:55 +04:00
|
|
|
|
url_func_result url_scheme(const char *url, char **result)
|
|
|
|
|
{
|
2006-06-27 04:53:39 +04:00
|
|
|
|
const char *scheme_end;
|
|
|
|
|
|
|
|
|
|
assert(url);
|
|
|
|
|
|
|
|
|
|
/* ensure the first character is alpha */
|
|
|
|
|
if (!isalpha(*url))
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
|
|
/* continue checking until the end marker (':') of the scheme for
|
|
|
|
|
* the format ALPHA<EFBFBD>*(<EFBFBD>ALPHA<EFBFBD>/<EFBFBD>DIGIT<EFBFBD>/<EFBFBD>"+"<EFBFBD>/<EFBFBD>"-"<EFBFBD>/<EFBFBD>"."<EFBFBD>) */
|
|
|
|
|
for (scheme_end = url;
|
|
|
|
|
((*scheme_end != '\0') && (*scheme_end != ':'));
|
|
|
|
|
scheme_end++) {
|
|
|
|
|
if (!isalnum(*scheme_end) &&
|
|
|
|
|
(*scheme_end != '+') &&
|
|
|
|
|
(*scheme_end != '-') &&
|
|
|
|
|
(*scheme_end != '.'))
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
if (*scheme_end == '\0')
|
2004-10-02 01:31:55 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2006-06-27 04:53:39 +04:00
|
|
|
|
|
|
|
|
|
/* make a copy of the result for the caller */
|
|
|
|
|
(*result) = malloc(scheme_end - url + 1);
|
2004-10-02 01:31:55 +04:00
|
|
|
|
if (!(*result)) {
|
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
2006-06-27 04:53:39 +04:00
|
|
|
|
strncpy((*result), url, scheme_end - url);
|
|
|
|
|
(*result)[scheme_end - url] = '\0';
|
2004-10-02 01:31:55 +04:00
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
|
}
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
2006-02-19 21:26:23 +03:00
|
|
|
|
/**
|
|
|
|
|
* Return the canonical root of an URL
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2006-02-19 21:26:23 +03:00
|
|
|
|
* \param result pointer to pointer to buffer to hold canonical rool URL
|
|
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
url_func_result url_canonical_root(const char *url, char **result)
|
|
|
|
|
{
|
|
|
|
|
int m, scheme_len, authority_len;
|
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
|
|
|
|
(*result) = 0;
|
|
|
|
|
|
|
|
|
|
m = regexec(&url_re, url, 10, match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
if (match[URL_RE_SCHEME].rm_so == -1 ||
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so == -1)
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
|
|
scheme_len = match[URL_RE_SCHEME].rm_eo - match[URL_RE_SCHEME].rm_so;
|
|
|
|
|
authority_len = match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
|
|
|
|
|
(*result) = malloc(scheme_len + 1 + 2 + authority_len + 1);
|
|
|
|
|
if (!(*result)) {
|
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strncpy((*result), url + match[URL_RE_SCHEME].rm_so, scheme_len);
|
|
|
|
|
m = scheme_len;
|
|
|
|
|
(*result)[m++] = ':';
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
strncpy((*result) + m, url + match[URL_RE_AUTHORITY].rm_so,
|
|
|
|
|
authority_len);
|
|
|
|
|
(*result)[m + authority_len] = '\0';
|
|
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Strip leafname, query and fragment segments from an URL
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2006-02-19 21:26:23 +03:00
|
|
|
|
* \param result pointer to pointer to buffer to hold result
|
|
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
url_func_result url_strip_lqf(const char *url, char **result)
|
|
|
|
|
{
|
|
|
|
|
int m, scheme_len, authority_len, path_len = 0;
|
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
|
|
|
|
(*result) = 0;
|
|
|
|
|
|
|
|
|
|
m = regexec(&url_re, url, 10, match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
if (match[URL_RE_SCHEME].rm_so == -1 ||
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so == -1)
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
|
|
scheme_len = match[URL_RE_SCHEME].rm_eo - match[URL_RE_SCHEME].rm_so;
|
|
|
|
|
authority_len = match[URL_RE_AUTHORITY].rm_eo -
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so;
|
|
|
|
|
if (match[URL_RE_PATH].rm_so != -1)
|
|
|
|
|
path_len = match[URL_RE_PATH].rm_eo -
|
|
|
|
|
match[URL_RE_PATH].rm_so;
|
|
|
|
|
|
|
|
|
|
(*result) = malloc(scheme_len + 1 + 2 + authority_len +
|
|
|
|
|
(path_len ? path_len : 1) + 1);
|
|
|
|
|
if (!(*result)) {
|
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strncpy((*result), url + match[URL_RE_SCHEME].rm_so, scheme_len);
|
|
|
|
|
m = scheme_len;
|
|
|
|
|
(*result)[m++] = ':';
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
strncpy((*result) + m, url + match[URL_RE_AUTHORITY].rm_so,
|
|
|
|
|
authority_len);
|
|
|
|
|
m += authority_len;
|
|
|
|
|
|
|
|
|
|
if (path_len) {
|
|
|
|
|
strncpy((*result) + m, url + match[URL_RE_AUTHORITY].rm_so,
|
|
|
|
|
path_len);
|
|
|
|
|
for (; path_len != 0 && (*result)[m + path_len - 1] != '/';
|
|
|
|
|
path_len--)
|
|
|
|
|
/* do nothing */;
|
|
|
|
|
m += path_len;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
|
|
|
|
|
(*result)[m] = '\0';
|
|
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-04-10 03:21:13 +04:00
|
|
|
|
/**
|
|
|
|
|
* Extract path, leafname and query segments from an URL
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2006-04-10 03:21:13 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold result
|
|
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
url_func_result url_plq(const char *url, char **result)
|
|
|
|
|
{
|
|
|
|
|
int m, path_len = 0, query_len = 0;
|
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
|
|
|
|
(*result) = 0;
|
|
|
|
|
|
|
|
|
|
m = regexec(&url_re, url, 10, match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
if (match[URL_RE_SCHEME].rm_so == -1 ||
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so == -1)
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
|
|
if (match[URL_RE_PATH].rm_so != -1)
|
|
|
|
|
path_len = match[URL_RE_PATH].rm_eo -
|
|
|
|
|
match[URL_RE_PATH].rm_so;
|
|
|
|
|
if (match[URL_RE_QUERY].rm_so != -1)
|
|
|
|
|
query_len = match[URL_RE_QUERY].rm_eo -
|
|
|
|
|
match[URL_RE_QUERY].rm_so;
|
|
|
|
|
|
|
|
|
|
(*result) = malloc((path_len ? path_len : 1) + query_len + 1 + 1);
|
|
|
|
|
if (!(*result)) {
|
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m = 0;
|
|
|
|
|
if (path_len) {
|
|
|
|
|
strncpy((*result), url + match[URL_RE_PATH].rm_so,
|
|
|
|
|
path_len);
|
|
|
|
|
m += path_len;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
|
|
|
|
|
if (query_len) {
|
|
|
|
|
(*result)[m++] = '?';
|
|
|
|
|
strncpy((*result) + m, url + match[URL_RE_QUERY].rm_so,
|
|
|
|
|
query_len);
|
|
|
|
|
m += query_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(*result)[m] = '\0';
|
|
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-06-20 01:49:25 +04:00
|
|
|
|
/**
|
|
|
|
|
* Extract path segment from an URL
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2006-06-20 01:49:25 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold result
|
|
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
url_func_result url_path(const char *url, char **result)
|
|
|
|
|
{
|
|
|
|
|
int m, path_len = 0;
|
|
|
|
|
regmatch_t match[10];
|
|
|
|
|
|
|
|
|
|
(*result) = 0;
|
|
|
|
|
|
|
|
|
|
m = regexec(&url_re, url, 10, match, 0);
|
|
|
|
|
if (m) {
|
|
|
|
|
LOG(("url '%s' failed to match regex", url));
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
if (match[URL_RE_SCHEME].rm_so == -1 ||
|
|
|
|
|
match[URL_RE_AUTHORITY].rm_so == -1)
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
|
|
if (match[URL_RE_PATH].rm_so != -1)
|
|
|
|
|
path_len = match[URL_RE_PATH].rm_eo -
|
|
|
|
|
match[URL_RE_PATH].rm_so;
|
|
|
|
|
|
|
|
|
|
(*result) = malloc((path_len ? path_len : 1) + 1);
|
|
|
|
|
if (!(*result)) {
|
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m = 0;
|
|
|
|
|
if (path_len > 1) {
|
|
|
|
|
strncpy((*result), url + match[URL_RE_PATH].rm_so,
|
|
|
|
|
path_len);
|
|
|
|
|
for (; path_len != 0 && (*result)[m + path_len - 1] != '/';
|
|
|
|
|
path_len--)
|
|
|
|
|
/* do nothing */;
|
|
|
|
|
m += path_len;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
(*result)[m++] = '/';
|
|
|
|
|
|
|
|
|
|
(*result)[m] = '\0';
|
|
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-28 03:18:52 +04:00
|
|
|
|
/**
|
|
|
|
|
* Attempt to find a nice filename for a URL.
|
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
|
* \param url an absolute URL
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* \param result pointer to pointer to buffer to hold filename
|
|
|
|
|
* \param remove_extensions remove any extensions from the filename
|
|
|
|
|
* \return URL_FUNC_OK on success
|
2004-03-28 03:18:52 +04:00
|
|
|
|
*/
|
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
url_func_result url_nice(const char *url, char **result,
|
|
|
|
|
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));
|
|
|
|
|
return URL_FUNC_FAILED;
|
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"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
return URL_FUNC_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"));
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
|
|
|
|
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] = '_';
|
|
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
2006-02-19 21:26:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
return URL_FUNC_FAILED;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
2005-06-27 02:18:37 +04:00
|
|
|
|
/**
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* Escape a string suitable for inclusion in an URL.
|
2005-06-27 02:18:37 +04:00
|
|
|
|
*
|
2005-07-24 00:43:37 +04:00
|
|
|
|
* \param unescaped the unescaped string
|
|
|
|
|
* \param result pointer to pointer to buffer to hold escaped string
|
|
|
|
|
* \return URL_FUNC_OK on success
|
2005-06-27 02:18:37 +04:00
|
|
|
|
*/
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
2005-06-27 02:18:37 +04:00
|
|
|
|
url_func_result url_escape(const char *unescaped, char **result)
|
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
char *escaped, *d;
|
|
|
|
|
const char *c;
|
|
|
|
|
|
|
|
|
|
if (!unescaped || !result)
|
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
|
|
len = strlen(unescaped);
|
|
|
|
|
|
|
|
|
|
escaped = malloc(len * 3 + 1);
|
|
|
|
|
if (!escaped)
|
|
|
|
|
return URL_FUNC_NOMEM;
|
2004-03-28 03:18:52 +04:00
|
|
|
|
|
2005-06-27 02:18:37 +04:00
|
|
|
|
for (c = unescaped, d = escaped; *c; c++) {
|
|
|
|
|
if (!isascii(*c) ||
|
|
|
|
|
strchr(";/?:@&=+$," "<>#%\"{}|\\^[]`", *c) ||
|
|
|
|
|
*c <= 0x20 || *c == 0x7f) {
|
|
|
|
|
*d++ = '%';
|
|
|
|
|
*d++ = "0123456789ABCDEF"[((*c >> 4) & 0xf)];
|
|
|
|
|
*d++ = "0123456789ABCDEF"[(*c & 0xf)];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* unreserved characters: [a-zA-Z0-9-_.!~*'()] */
|
|
|
|
|
*d++ = *c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-27 05:57:54 +04:00
|
|
|
|
*d++ = '\0';
|
|
|
|
|
|
|
|
|
|
(*result) = malloc(d - escaped);
|
2005-06-27 02:18:37 +04:00
|
|
|
|
if (!(*result)) {
|
|
|
|
|
free(escaped);
|
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
|
}
|
2005-06-27 05:57:54 +04:00
|
|
|
|
|
|
|
|
|
memcpy((*result), escaped, d - escaped);
|
2005-06-27 02:18:37 +04:00
|
|
|
|
|
|
|
|
|
free(escaped);
|
|
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
|
}
|
2004-03-02 21:02:41 +03:00
|
|
|
|
|
2005-07-24 00:43:37 +04:00
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
|
#ifdef TEST
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
url_func_result res;
|
2004-03-02 21:02:41 +03:00
|
|
|
|
char *s;
|
|
|
|
|
url_init();
|
2004-10-02 01:31:55 +04:00
|
|
|
|
for (i = 1; i != argc; i++) {
|
|
|
|
|
/* printf("==> '%s'\n", argv[i]);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
res = url_normalize(argv[i], &s);
|
|
|
|
|
if (res == URL_FUNC_OK) {
|
|
|
|
|
printf("<== '%s'\n", s);
|
|
|
|
|
free(s);
|
|
|
|
|
}*/
|
2004-03-02 21:02:41 +03:00
|
|
|
|
/* printf("==> '%s'\n", argv[i]);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
res = url_host(argv[i], &s);
|
|
|
|
|
if (res == URL_FUNC_OK) {
|
|
|
|
|
printf("<== '%s'\n", s);
|
|
|
|
|
free(s);
|
|
|
|
|
}*/
|
2006-04-22 13:07:28 +04:00
|
|
|
|
if (1 != i) {
|
2004-08-09 20:11:58 +04:00
|
|
|
|
res = url_join(argv[i], argv[1], &s);
|
|
|
|
|
if (res == URL_FUNC_OK) {
|
2004-03-02 21:02:41 +03:00
|
|
|
|
printf("'%s' + '%s' \t= '%s'\n", argv[1],
|
|
|
|
|
argv[i], s);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
free(s);
|
|
|
|
|
}
|
2006-04-22 13:07:28 +04:00
|
|
|
|
}
|
|
|
|
|
/* printf("'%s' => ", argv[i]);
|
2005-07-24 00:43:37 +04:00
|
|
|
|
res = url_nice(argv[i], &s, true);
|
|
|
|
|
if (res == URL_FUNC_OK) {
|
|
|
|
|
printf("'%s', ", s);
|
|
|
|
|
free(s);
|
|
|
|
|
} else {
|
|
|
|
|
printf("failed %u, ", res);
|
2004-10-02 01:31:55 +04:00
|
|
|
|
}
|
2005-07-24 00:43:37 +04:00
|
|
|
|
res = url_nice(argv[i], &s, false);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
if (res == URL_FUNC_OK) {
|
2005-07-24 00:43:37 +04:00
|
|
|
|
printf("'%s', ", s);
|
2004-08-09 20:11:58 +04:00
|
|
|
|
free(s);
|
2005-07-24 00:43:37 +04:00
|
|
|
|
} else {
|
|
|
|
|
printf("failed %u, ", res);
|
|
|
|
|
}
|
2006-04-22 13:07:28 +04:00
|
|
|
|
printf("\n");*/
|
2004-03-02 21:02:41 +03:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void regcomp_wrapper(regex_t *preg, const char *regex, int cflags)
|
|
|
|
|
{
|
|
|
|
|
char errbuf[200];
|
|
|
|
|
int r;
|
|
|
|
|
r = regcomp(preg, regex, cflags);
|
|
|
|
|
if (r) {
|
|
|
|
|
regerror(r, preg, errbuf, sizeof errbuf);
|
|
|
|
|
fprintf(stderr, "Failed to compile regexp '%s'\n", regex);
|
|
|
|
|
fprintf(stderr, "error: %s\n", errbuf);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|