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
|
|
|
|
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
|
|
|
}
|