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
|
|
|
|
* URL parsing and joining (implementation).
|
|
|
|
*/
|
|
|
|
|
2008-11-28 20:20:50 +03:00
|
|
|
#define _GNU_SOURCE /* for strdup() */
|
Merged revisions 5309-5406,5409-5422 via svnmerge from
svn://svn.netsurf-browser.org/branches/vince/netsurf-fb
........
r5309 | vince | 2008-09-13 10:59:10 +0100 (Sat, 13 Sep 2008) | 2 lines
first stab at framebuffer frontend
........
r5313 | vince | 2008-09-14 15:08:52 +0100 (Sun, 14 Sep 2008) | 2 lines
add line plotters
........
r5314 | vince | 2008-09-14 15:28:12 +0100 (Sun, 14 Sep 2008) | 2 lines
add rectangle plot to 16bpp plotters
........
r5315 | vince | 2008-09-14 19:58:57 +0100 (Sun, 14 Sep 2008) | 2 lines
improve 16bpp image plot
........
r5316 | vince | 2008-09-15 00:35:32 +0100 (Mon, 15 Sep 2008) | 2 lines
abstract the os specific framebuffer init
........
r5317 | vince | 2008-09-15 11:18:51 +0100 (Mon, 15 Sep 2008) | 2 lines
first cut of linux frontend
........
r5318 | vince | 2008-09-15 12:01:00 +0100 (Mon, 15 Sep 2008) | 2 lines
remove junk includes
........
r5319 | vince | 2008-09-15 12:09:02 +0100 (Mon, 15 Sep 2008) | 2 lines
make plotters OS agnostic again
........
r5322 | vince | 2008-09-15 15:55:01 +0100 (Mon, 15 Sep 2008) | 2 lines
Linux frontend operates
........
r5323 | vince | 2008-09-15 16:32:47 +0100 (Mon, 15 Sep 2008) | 2 lines
abstract out OS specific input
........
r5326 | vince | 2008-09-15 19:21:01 +0100 (Mon, 15 Sep 2008) | 2 lines
Improve linux mode setting
........
r5329 | vince | 2008-09-15 21:13:33 +0100 (Mon, 15 Sep 2008) | 2 lines
improve text clipping
........
r5339 | vince | 2008-09-16 00:07:57 +0100 (Tue, 16 Sep 2008) | 2 lines
possibly fix text clipping issue
........
r5342 | vince | 2008-09-16 00:39:36 +0100 (Tue, 16 Sep 2008) | 2 lines
consolidate polygon plotters
........
r5344 | dsilvers | 2008-09-16 10:21:06 +0100 (Tue, 16 Sep 2008) | 1 line
Fix up the framebuffer target makefile a bit more, add some config options for it
........
r5345 | dsilvers | 2008-09-16 10:22:19 +0100 (Tue, 16 Sep 2008) | 1 line
Ensure the appropriate frontend is selected when building framebuffer
........
r5346 | dsilvers | 2008-09-16 10:27:16 +0100 (Tue, 16 Sep 2008) | 1 line
Update build system to support targetting separate framebuffer frontends in different build trees, update executable to be nsfb-blah
........
r5350 | vince | 2008-09-16 17:15:04 +0100 (Tue, 16 Sep 2008) | 1 line
Add -g to provide symbols for framebuffer link
........
r5351 | vince | 2008-09-16 17:17:09 +0100 (Tue, 16 Sep 2008) | 1 line
framebuffer scheduler now works, plotters tweaked, gui tracks window redraw requirements better, keypresses not duplicated under linux fb
........
r5352 | dsilvers | 2008-09-16 17:38:53 +0100 (Tue, 16 Sep 2008) | 1 line
Ensure we only allow one fetcher at a time
........
r5361 | vince | 2008-09-17 11:48:44 +0100 (Wed, 17 Sep 2008) | 2 lines
initial cursor support
........
r5362 | vince | 2008-09-17 13:56:47 +0100 (Wed, 17 Sep 2008) | 2 lines
add mouse handling
........
r5363 | vince | 2008-09-17 14:14:44 +0100 (Wed, 17 Sep 2008) | 2 lines
add framebuffer resources
........
r5364 | vince | 2008-09-17 17:12:21 +0100 (Wed, 17 Sep 2008) | 2 lines
add reasonable pointer
........
r5366 | vince | 2008-09-17 17:17:25 +0100 (Wed, 17 Sep 2008) | 2 lines
fix pointer alpha
........
r5370 | vince | 2008-09-18 13:43:53 +0100 (Thu, 18 Sep 2008) | 2 lines
warning squash and cleanup ready for trunk merge
........
r5375 | vince | 2008-09-19 14:58:43 +0100 (Fri, 19 Sep 2008) | 2 lines
Working mouse navigation
........
r5377 | vince | 2008-09-20 14:06:22 +0100 (Sat, 20 Sep 2008) | 2 lines
Improve scrolling
........
r5378 | vince | 2008-09-20 14:46:46 +0100 (Sat, 20 Sep 2008) | 2 lines
fix redraw issues with scrolling
........
r5380 | vince | 2008-09-20 17:08:43 +0100 (Sat, 20 Sep 2008) | 3 lines
Alter panning to use its own flag so it doesnt cause invalid redraw
operations
........
r5381 | vince | 2008-09-20 21:52:45 +0100 (Sat, 20 Sep 2008) | 2 lines
add dummy framebuffer
........
r5383 | vince | 2008-09-21 00:00:15 +0100 (Sun, 21 Sep 2008) | 2 lines
fix segfault when cursor is off teh bottom of teh screen
........
r5384 | vince | 2008-09-21 00:06:08 +0100 (Sun, 21 Sep 2008) | 2 lines
fix off by one in pointer fix
........
r5385 | vince | 2008-09-21 00:25:09 +0100 (Sun, 21 Sep 2008) | 2 lines
when fixing bloody silly off by one errors remember to fix *both* references
........
r5387 | vince | 2008-09-21 00:38:13 +0100 (Sun, 21 Sep 2008) | 2 lines
last try at stopping the pointer segfault
........
r5388 | vince | 2008-09-21 16:24:18 +0100 (Sun, 21 Sep 2008) | 2 lines
improve vertical text clipping
........
r5392 | vince | 2008-09-21 23:11:51 +0100 (Sun, 21 Sep 2008) | 2 lines
Improve text plotters
........
r5393 | vince | 2008-09-21 23:34:38 +0100 (Sun, 21 Sep 2008) | 2 lines
fix 32bpp line plotting
........
r5394 | vince | 2008-09-22 00:00:03 +0100 (Mon, 22 Sep 2008) | 2 lines
Fix off by one error in line plotting clipping
........
r5397 | vince | 2008-09-22 13:46:22 +0100 (Mon, 22 Sep 2008) | 2 lines
Fix bitmap tileing
........
r5398 | vince | 2008-09-22 17:46:02 +0100 (Mon, 22 Sep 2008) | 2 lines
enable knockout renderer
........
r5399 | vince | 2008-09-22 18:43:48 +0100 (Mon, 22 Sep 2008) | 2 lines
ensure clipping region lies within window, caused by knockout renderer
........
r5400 | vince | 2008-09-22 19:20:25 +0100 (Mon, 22 Sep 2008) | 2 lines
update cursor to one swiped from X windows
........
r5405 | vince | 2008-09-23 09:09:05 +0100 (Tue, 23 Sep 2008) | 2 lines
fix vertical scroll limit
........
r5412 | dsilvers | 2008-09-23 10:53:14 +0100 (Tue, 23 Sep 2008) | 1 line
Revert noisy fetcher patch
........
r5413 | dsilvers | 2008-09-23 10:58:00 +0100 (Tue, 23 Sep 2008) | 1 line
Add header guards
........
r5414 | dsilvers | 2008-09-23 11:31:31 +0100 (Tue, 23 Sep 2008) | 1 line
Tidy the region clipping slightly
........
r5416 | dsilvers | 2008-09-23 12:05:00 +0100 (Tue, 23 Sep 2008) | 1 line
Rationalise how the framebuffer frontend finds resources and options
........
r5418 | dsilvers | 2008-09-23 13:59:00 +0100 (Tue, 23 Sep 2008) | 1 line
Ensure options are overridden after load, and squash an fb_gui.c warning
........
r5419 | dsilvers | 2008-09-23 14:20:07 +0100 (Tue, 23 Sep 2008) | 1 line
Support fb_mode and fb_device options
........
r5420 | dsilvers | 2008-09-23 14:21:48 +0100 (Tue, 23 Sep 2008) | 1 line
Support option_fb_device in the able frontend
........
r5421 | dsilvers | 2008-09-23 14:25:17 +0100 (Tue, 23 Sep 2008) | 1 line
Ensure target_blank is disabled
........
r5422 | dsilvers | 2008-09-23 14:39:00 +0100 (Tue, 23 Sep 2008) | 1 line
Rework linux fb frontend to support scanning and opening all event devices for input
........
svn path=/trunk/netsurf/; revision=5423
2008-09-23 18:00:40 +04:00
|
|
|
#include <sys/types.h>
|
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 <regex.h>
|
Merged revisions 5309-5406,5409-5422 via svnmerge from
svn://svn.netsurf-browser.org/branches/vince/netsurf-fb
........
r5309 | vince | 2008-09-13 10:59:10 +0100 (Sat, 13 Sep 2008) | 2 lines
first stab at framebuffer frontend
........
r5313 | vince | 2008-09-14 15:08:52 +0100 (Sun, 14 Sep 2008) | 2 lines
add line plotters
........
r5314 | vince | 2008-09-14 15:28:12 +0100 (Sun, 14 Sep 2008) | 2 lines
add rectangle plot to 16bpp plotters
........
r5315 | vince | 2008-09-14 19:58:57 +0100 (Sun, 14 Sep 2008) | 2 lines
improve 16bpp image plot
........
r5316 | vince | 2008-09-15 00:35:32 +0100 (Mon, 15 Sep 2008) | 2 lines
abstract the os specific framebuffer init
........
r5317 | vince | 2008-09-15 11:18:51 +0100 (Mon, 15 Sep 2008) | 2 lines
first cut of linux frontend
........
r5318 | vince | 2008-09-15 12:01:00 +0100 (Mon, 15 Sep 2008) | 2 lines
remove junk includes
........
r5319 | vince | 2008-09-15 12:09:02 +0100 (Mon, 15 Sep 2008) | 2 lines
make plotters OS agnostic again
........
r5322 | vince | 2008-09-15 15:55:01 +0100 (Mon, 15 Sep 2008) | 2 lines
Linux frontend operates
........
r5323 | vince | 2008-09-15 16:32:47 +0100 (Mon, 15 Sep 2008) | 2 lines
abstract out OS specific input
........
r5326 | vince | 2008-09-15 19:21:01 +0100 (Mon, 15 Sep 2008) | 2 lines
Improve linux mode setting
........
r5329 | vince | 2008-09-15 21:13:33 +0100 (Mon, 15 Sep 2008) | 2 lines
improve text clipping
........
r5339 | vince | 2008-09-16 00:07:57 +0100 (Tue, 16 Sep 2008) | 2 lines
possibly fix text clipping issue
........
r5342 | vince | 2008-09-16 00:39:36 +0100 (Tue, 16 Sep 2008) | 2 lines
consolidate polygon plotters
........
r5344 | dsilvers | 2008-09-16 10:21:06 +0100 (Tue, 16 Sep 2008) | 1 line
Fix up the framebuffer target makefile a bit more, add some config options for it
........
r5345 | dsilvers | 2008-09-16 10:22:19 +0100 (Tue, 16 Sep 2008) | 1 line
Ensure the appropriate frontend is selected when building framebuffer
........
r5346 | dsilvers | 2008-09-16 10:27:16 +0100 (Tue, 16 Sep 2008) | 1 line
Update build system to support targetting separate framebuffer frontends in different build trees, update executable to be nsfb-blah
........
r5350 | vince | 2008-09-16 17:15:04 +0100 (Tue, 16 Sep 2008) | 1 line
Add -g to provide symbols for framebuffer link
........
r5351 | vince | 2008-09-16 17:17:09 +0100 (Tue, 16 Sep 2008) | 1 line
framebuffer scheduler now works, plotters tweaked, gui tracks window redraw requirements better, keypresses not duplicated under linux fb
........
r5352 | dsilvers | 2008-09-16 17:38:53 +0100 (Tue, 16 Sep 2008) | 1 line
Ensure we only allow one fetcher at a time
........
r5361 | vince | 2008-09-17 11:48:44 +0100 (Wed, 17 Sep 2008) | 2 lines
initial cursor support
........
r5362 | vince | 2008-09-17 13:56:47 +0100 (Wed, 17 Sep 2008) | 2 lines
add mouse handling
........
r5363 | vince | 2008-09-17 14:14:44 +0100 (Wed, 17 Sep 2008) | 2 lines
add framebuffer resources
........
r5364 | vince | 2008-09-17 17:12:21 +0100 (Wed, 17 Sep 2008) | 2 lines
add reasonable pointer
........
r5366 | vince | 2008-09-17 17:17:25 +0100 (Wed, 17 Sep 2008) | 2 lines
fix pointer alpha
........
r5370 | vince | 2008-09-18 13:43:53 +0100 (Thu, 18 Sep 2008) | 2 lines
warning squash and cleanup ready for trunk merge
........
r5375 | vince | 2008-09-19 14:58:43 +0100 (Fri, 19 Sep 2008) | 2 lines
Working mouse navigation
........
r5377 | vince | 2008-09-20 14:06:22 +0100 (Sat, 20 Sep 2008) | 2 lines
Improve scrolling
........
r5378 | vince | 2008-09-20 14:46:46 +0100 (Sat, 20 Sep 2008) | 2 lines
fix redraw issues with scrolling
........
r5380 | vince | 2008-09-20 17:08:43 +0100 (Sat, 20 Sep 2008) | 3 lines
Alter panning to use its own flag so it doesnt cause invalid redraw
operations
........
r5381 | vince | 2008-09-20 21:52:45 +0100 (Sat, 20 Sep 2008) | 2 lines
add dummy framebuffer
........
r5383 | vince | 2008-09-21 00:00:15 +0100 (Sun, 21 Sep 2008) | 2 lines
fix segfault when cursor is off teh bottom of teh screen
........
r5384 | vince | 2008-09-21 00:06:08 +0100 (Sun, 21 Sep 2008) | 2 lines
fix off by one in pointer fix
........
r5385 | vince | 2008-09-21 00:25:09 +0100 (Sun, 21 Sep 2008) | 2 lines
when fixing bloody silly off by one errors remember to fix *both* references
........
r5387 | vince | 2008-09-21 00:38:13 +0100 (Sun, 21 Sep 2008) | 2 lines
last try at stopping the pointer segfault
........
r5388 | vince | 2008-09-21 16:24:18 +0100 (Sun, 21 Sep 2008) | 2 lines
improve vertical text clipping
........
r5392 | vince | 2008-09-21 23:11:51 +0100 (Sun, 21 Sep 2008) | 2 lines
Improve text plotters
........
r5393 | vince | 2008-09-21 23:34:38 +0100 (Sun, 21 Sep 2008) | 2 lines
fix 32bpp line plotting
........
r5394 | vince | 2008-09-22 00:00:03 +0100 (Mon, 22 Sep 2008) | 2 lines
Fix off by one error in line plotting clipping
........
r5397 | vince | 2008-09-22 13:46:22 +0100 (Mon, 22 Sep 2008) | 2 lines
Fix bitmap tileing
........
r5398 | vince | 2008-09-22 17:46:02 +0100 (Mon, 22 Sep 2008) | 2 lines
enable knockout renderer
........
r5399 | vince | 2008-09-22 18:43:48 +0100 (Mon, 22 Sep 2008) | 2 lines
ensure clipping region lies within window, caused by knockout renderer
........
r5400 | vince | 2008-09-22 19:20:25 +0100 (Mon, 22 Sep 2008) | 2 lines
update cursor to one swiped from X windows
........
r5405 | vince | 2008-09-23 09:09:05 +0100 (Tue, 23 Sep 2008) | 2 lines
fix vertical scroll limit
........
r5412 | dsilvers | 2008-09-23 10:53:14 +0100 (Tue, 23 Sep 2008) | 1 line
Revert noisy fetcher patch
........
r5413 | dsilvers | 2008-09-23 10:58:00 +0100 (Tue, 23 Sep 2008) | 1 line
Add header guards
........
r5414 | dsilvers | 2008-09-23 11:31:31 +0100 (Tue, 23 Sep 2008) | 1 line
Tidy the region clipping slightly
........
r5416 | dsilvers | 2008-09-23 12:05:00 +0100 (Tue, 23 Sep 2008) | 1 line
Rationalise how the framebuffer frontend finds resources and options
........
r5418 | dsilvers | 2008-09-23 13:59:00 +0100 (Tue, 23 Sep 2008) | 1 line
Ensure options are overridden after load, and squash an fb_gui.c warning
........
r5419 | dsilvers | 2008-09-23 14:20:07 +0100 (Tue, 23 Sep 2008) | 1 line
Support fb_mode and fb_device options
........
r5420 | dsilvers | 2008-09-23 14:21:48 +0100 (Tue, 23 Sep 2008) | 1 line
Support option_fb_device in the able frontend
........
r5421 | dsilvers | 2008-09-23 14:25:17 +0100 (Tue, 23 Sep 2008) | 1 line
Ensure target_blank is disabled
........
r5422 | dsilvers | 2008-09-23 14:39:00 +0100 (Tue, 23 Sep 2008) | 1 line
Rework linux fb frontend to support scanning and opening all event devices for input
........
svn path=/trunk/netsurf/; revision=5423
2008-09-23 18:00:40 +04:00
|
|
|
#include <unistd.h>
|
2008-02-03 15:04:48 +03:00
|
|
|
#include "curl/curl.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/url.h"
|
|
|
|
#include "utils/utils.h"
|
2004-03-02 21:02:41 +03:00
|
|
|
|
2006-09-02 20:42:37 +04:00
|
|
|
struct url_components_internal {
|
2006-08-17 18:48:28 +04:00
|
|
|
char *buffer; /* buffer used for all the following data */
|
2006-06-27 21:59:32 +04:00
|
|
|
char *scheme;
|
|
|
|
char *authority;
|
|
|
|
char *path;
|
|
|
|
char *query;
|
|
|
|
char *fragment;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-02 00:09:49 +03:00
|
|
|
/**
|
|
|
|
* Check whether a host is an IP address
|
|
|
|
*
|
|
|
|
* \param host a hostname terminated by '\0' or '/'
|
|
|
|
* \return true if the hostname is an IP address, false otherwise
|
|
|
|
*/
|
|
|
|
bool url_host_is_ip_address(const char *host) {
|
|
|
|
int b;
|
|
|
|
bool n;
|
|
|
|
|
|
|
|
assert(host);
|
2007-02-12 01:28:00 +03:00
|
|
|
|
2006-12-02 00:09:49 +03:00
|
|
|
/* an IP address is of the format XXX.XXX.XXX.XXX, ie totally
|
|
|
|
* numeric with 3 full stops between the numbers */
|
|
|
|
b = 0; // number of breaks
|
|
|
|
n = false; // number present
|
|
|
|
do {
|
|
|
|
if (*host == '.') {
|
|
|
|
if (!n)
|
|
|
|
return false;
|
|
|
|
b++;
|
|
|
|
n = false;
|
|
|
|
} else if ((*host == '\0') || (*host == '/')) {
|
|
|
|
if (!n)
|
|
|
|
return false;
|
|
|
|
/* todo: check the values are in 0-255 range */
|
|
|
|
return (b == 3);
|
|
|
|
} else if (*host < '0' || *host > '9') {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
n = true;
|
|
|
|
}
|
2008-01-07 07:54:08 +03:00
|
|
|
host++;
|
2006-12-02 00:09:49 +03:00
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2004-03-02 21:02:41 +03:00
|
|
|
/**
|
|
|
|
* Normalize a URL.
|
|
|
|
*
|
2006-06-27 04:53:39 +04:00
|
|
|
* \param url an absolute URL
|
2008-05-25 19:51:30 +04:00
|
|
|
* \param result pointer to pointer to buffer to hold cleaned up url. Caller
|
|
|
|
* gets ownership of pointer to buffer value. On failure the
|
|
|
|
* pointer to buffer value will be NULL.
|
2005-07-24 00:43:37 +04:00
|
|
|
* \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;
|
2008-05-25 19:04:22 +04:00
|
|
|
size_t i;
|
2004-08-14 19:07:21 +04:00
|
|
|
size_t len;
|
2008-05-25 19:04:22 +04:00
|
|
|
size_t bufsize;
|
|
|
|
char* norm;
|
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
|
|
|
|
2008-05-25 19:04:22 +04:00
|
|
|
/* skip past any leading whitespace (likely if URL was copy-pasted) */
|
|
|
|
while (isspace(*url))
|
|
|
|
url++;
|
|
|
|
|
|
|
|
/* allocate sufficiently large buffer for new URL */
|
|
|
|
len = strlen(url);
|
2008-05-25 19:51:30 +04:00
|
|
|
/* "+ 1" for the terminating NUL character. */
|
|
|
|
bufsize = len + 1 + SLEN("http://") + SLEN("/");
|
2008-05-25 19:04:22 +04:00
|
|
|
/* work out how much extra to leave for internal whitespace */
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
if(isspace(url[i])) bufsize += 2; /* ' ' -> '%20' */
|
|
|
|
}
|
|
|
|
if ((norm = malloc(bufsize)) == NULL) {
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
}
|
|
|
|
*result = norm;
|
|
|
|
strcpy(norm, url);
|
|
|
|
|
|
|
|
/* truncate trailing whitespace (significant should be uriencoded) */
|
|
|
|
for (i = len - 1; (i > 0) && isspace(norm[i]); i--) {
|
|
|
|
norm[i] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encode any remaining (internal) whitespace */
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if(isspace(norm[i])) {
|
|
|
|
char space = norm[i];
|
|
|
|
memmove(norm + i + 2, norm + i, 1 + len - i);
|
|
|
|
len += 2;
|
|
|
|
norm[ i] = '%';
|
|
|
|
norm[++i] = digit2lowcase_hex(space >> 4);
|
|
|
|
norm[++i] = digit2lowcase_hex(space & 0xf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finally verify that it's actually an URL we're working on
|
|
|
|
* (RFC regex too fussy to tolerate above WSP problems) */
|
2009-05-28 20:25:01 +04:00
|
|
|
if (regexec(&url_re, norm, 10, match, 0)) {
|
2004-03-02 21:02:41 +03:00
|
|
|
LOG(("url '%s' failed to match regex", url));
|
2008-05-25 19:51:30 +04:00
|
|
|
free(norm);
|
|
|
|
*result = NULL;
|
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_SCHEME].rm_so == -1) {
|
2004-03-02 21:02:41 +03:00
|
|
|
/* scheme missing: add http:// and reparse */
|
2008-05-25 19:51:30 +04:00
|
|
|
memmove(norm + SLEN("http://"), norm, len + 1);
|
|
|
|
memcpy(norm, "http://", SLEN("http://")); /* do NOT copy NUL */
|
|
|
|
len += SLEN("http://");
|
2009-05-28 20:25:01 +04:00
|
|
|
if (regexec(&url_re, norm, 10, match, 0)) {
|
2008-05-25 19:04:22 +04:00
|
|
|
LOG(("url '%s' failed to match regex", norm));
|
|
|
|
free(norm);
|
2008-05-25 19:51:30 +04:00
|
|
|
*result = NULL;
|
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 (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;
|
2008-05-26 14:34:47 +04:00
|
|
|
(regoff_t) i != match[URL_RE_SCHEME].rm_eo; i++)
|
2008-05-25 19:04:22 +04:00
|
|
|
norm[i] = tolower(norm[i]);
|
2005-04-30 18:31:48 +04:00
|
|
|
if (match[URL_RE_SCHEME].rm_eo == 4
|
2008-05-25 19:04:22 +04:00
|
|
|
&& norm[0] == 'h'
|
|
|
|
&& norm[1] == 't'
|
|
|
|
&& norm[2] == 't'
|
|
|
|
&& norm[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) {
|
2008-05-25 19:04:22 +04:00
|
|
|
memmove(norm + match[URL_RE_PATH].rm_so + 1,
|
|
|
|
norm + match[URL_RE_PATH].rm_so,
|
2005-04-30 18:31:48 +04:00
|
|
|
len - match[URL_RE_PATH].rm_so + 1);
|
2008-05-25 19:04:22 +04:00
|
|
|
norm[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;
|
2008-05-26 14:34:47 +04:00
|
|
|
(regoff_t) i != match[URL_RE_AUTHORITY].rm_eo;
|
|
|
|
i++) {
|
2008-05-25 19:04:22 +04:00
|
|
|
if (norm[i] == ':' && (i + 3) < len) {
|
|
|
|
if (http && norm[i + 1] == '8' &&
|
|
|
|
norm[i + 2] == '0' &&
|
2008-05-26 14:34:47 +04:00
|
|
|
(regoff_t) i + 3 ==
|
2005-04-30 18:31:48 +04:00
|
|
|
match[URL_RE_AUTHORITY].rm_eo) {
|
2008-05-25 19:04:22 +04:00
|
|
|
memmove(norm + i,
|
|
|
|
norm + 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;
|
2008-05-25 19:04:22 +04:00
|
|
|
norm[len] = '\0';
|
2008-05-26 14:34:47 +04:00
|
|
|
} else if ((regoff_t) i + 1 == match[4].rm_eo) {
|
2008-05-25 19:04:22 +04:00
|
|
|
memmove(norm + i,
|
|
|
|
norm + 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--;
|
2008-05-25 19:04:22 +04:00
|
|
|
norm[len] = '\0';
|
2004-03-02 21:02:41 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-05-25 19:04:22 +04:00
|
|
|
norm[i] = tolower(norm[i]);
|
2004-03-02 21:02:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unescape non-"reserved" escaped characters */
|
2008-05-25 19:04:22 +04:00
|
|
|
for (i = 0; i + 2 < len; i++) {
|
|
|
|
if (norm[i] != '%')
|
2004-03-02 21:02:41 +03:00
|
|
|
continue;
|
2008-05-25 19:04:22 +04:00
|
|
|
c = tolower(norm[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;
|
2008-05-25 19:04:22 +04:00
|
|
|
c = tolower(norm[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;
|
|
|
|
}
|
|
|
|
|
2008-05-25 19:04:22 +04:00
|
|
|
norm[i] = m;
|
|
|
|
memmove(norm + i + 1, norm + i + 3, len - i - 2);
|
2004-03-02 21:02:41 +03:00
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
|
2008-05-25 19:04:22 +04:00
|
|
|
/* norm and *result point to same memory, so just return ok */
|
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
|
|
|
{
|
2006-06-29 01:49:41 +04:00
|
|
|
url_func_result status = URL_FUNC_NOMEM;
|
2006-09-02 20:42:37 +04:00
|
|
|
struct url_components_internal base_components = {0,0,0,0,0,0};
|
2008-10-14 03:49:15 +04:00
|
|
|
struct url_components_internal *base_ptr = &base_components;
|
2006-09-02 20:42:37 +04:00
|
|
|
struct url_components_internal rel_components = {0,0,0,0,0,0};
|
2008-10-14 03:49:15 +04:00
|
|
|
struct url_components_internal *rel_ptr = &rel_components;
|
2006-09-02 20:42:37 +04:00
|
|
|
struct url_components_internal merged_components = {0,0,0,0,0,0};
|
2008-10-14 03:49:15 +04:00
|
|
|
struct url_components_internal *merged_ptr = &merged_components;
|
2006-06-29 01:49:41 +04:00
|
|
|
char *merge_path = NULL, *split_point;
|
2006-08-17 18:48:28 +04:00
|
|
|
char *input, *output, *start = NULL;
|
2006-06-29 01:49:41 +04:00
|
|
|
int len, buf_len;
|
2006-06-27 21:59:32 +04:00
|
|
|
|
2004-08-09 20:11:58 +04:00
|
|
|
(*result) = 0;
|
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
assert(base);
|
2006-06-29 01:49:41 +04:00
|
|
|
assert(rel);
|
2006-09-19 14:08:33 +04:00
|
|
|
|
|
|
|
|
2006-07-01 03:09:21 +04:00
|
|
|
/* break down the relative URL (not cached, corruptable) */
|
2008-10-14 03:49:15 +04:00
|
|
|
status = url_get_components(rel, (struct url_components *) rel_ptr);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status != URL_FUNC_OK) {
|
2006-06-29 01:49:41 +04:00
|
|
|
LOG(("relative url '%s' failed to get components", rel));
|
2004-08-09 20:11:58 +04:00
|
|
|
return URL_FUNC_FAILED;
|
2004-03-02 21:02:41 +03:00
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [1] relative URL is absolute, use it entirely */
|
|
|
|
merged_components = rel_components;
|
|
|
|
if (rel_components.scheme)
|
|
|
|
goto url_join_reform_url;
|
|
|
|
|
2006-07-01 03:09:21 +04:00
|
|
|
/* break down the base URL (possibly cached, not corruptable) */
|
2008-10-14 03:49:15 +04:00
|
|
|
status = url_get_components(base, (struct url_components *) base_ptr);
|
2006-06-29 01:49:41 +04:00
|
|
|
if (status != URL_FUNC_OK) {
|
2008-10-14 03:49:15 +04:00
|
|
|
url_destroy_components((struct url_components *) rel_ptr);
|
2006-06-29 01:49:41 +04:00
|
|
|
LOG(("base url '%s' failed to get components", base));
|
|
|
|
return URL_FUNC_FAILED;
|
2004-04-13 00:43:29 +04:00
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [2] relative authority takes presidence */
|
|
|
|
merged_components.scheme = base_components.scheme;
|
|
|
|
if (rel_components.authority)
|
|
|
|
goto url_join_reform_url;
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [3] handle empty paths */
|
|
|
|
merged_components.authority = base_components.authority;
|
|
|
|
if (!rel_components.path) {
|
2006-08-17 18:48:28 +04:00
|
|
|
merged_components.path = base_components.path;
|
2006-06-29 01:49:41 +04:00
|
|
|
if (!rel_components.query)
|
|
|
|
merged_components.query = base_components.query;
|
|
|
|
goto url_join_reform_url;
|
2004-03-02 21:02:41 +03:00
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [4] handle valid paths */
|
|
|
|
if (rel_components.path[0] == '/')
|
|
|
|
merged_components.path = rel_components.path;
|
|
|
|
else {
|
|
|
|
/* 5.2.3 */
|
|
|
|
if ((base_components.authority) && (!base_components.path)) {
|
|
|
|
merge_path = malloc(strlen(rel_components.path) + 2);
|
|
|
|
if (!merge_path) {
|
|
|
|
LOG(("malloc failed"));
|
2006-07-01 03:09:21 +04:00
|
|
|
goto url_join_no_mem;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
sprintf(merge_path, "/%s", rel_components.path);
|
|
|
|
merged_components.path = merge_path;
|
|
|
|
} else {
|
2006-08-17 18:48:28 +04:00
|
|
|
split_point = base_components.path ?
|
|
|
|
strrchr(base_components.path, '/') :
|
|
|
|
NULL;
|
2006-06-29 01:49:41 +04:00
|
|
|
if (!split_point) {
|
|
|
|
merged_components.path = rel_components.path;
|
|
|
|
} else {
|
|
|
|
len = ++split_point - base_components.path;
|
|
|
|
buf_len = len + 1 + strlen(rel_components.path);
|
|
|
|
merge_path = malloc(buf_len);
|
|
|
|
if (!merge_path) {
|
|
|
|
LOG(("malloc failed"));
|
2006-07-01 03:09:21 +04:00
|
|
|
goto url_join_no_mem;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
memcpy(merge_path, base_components.path, len);
|
|
|
|
memcpy(merge_path + len, rel_components.path,
|
|
|
|
strlen(rel_components.path));
|
|
|
|
merge_path[buf_len - 1] = '\0';
|
|
|
|
merged_components.path = merge_path;
|
|
|
|
}
|
|
|
|
}
|
2004-03-02 21:02:41 +03:00
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
url_join_reform_url:
|
|
|
|
/* 5.2.4 */
|
|
|
|
input = merged_components.path;
|
|
|
|
if ((input) && (strchr(input, '.'))) {
|
|
|
|
/* [1] remove all dot references */
|
|
|
|
output = start = malloc(strlen(input) + 1);
|
|
|
|
if (!output) {
|
|
|
|
LOG(("malloc failed"));
|
2006-07-01 03:09:21 +04:00
|
|
|
goto url_join_no_mem;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
merged_components.path = output;
|
|
|
|
*output = '\0';
|
|
|
|
|
|
|
|
while (*input != '\0') {
|
|
|
|
/* [2A] */
|
|
|
|
if (input[0] == '.') {
|
|
|
|
if (input[1] == '/') {
|
|
|
|
input = input + 2;
|
|
|
|
continue;
|
|
|
|
} else if ((input[1] == '.') &&
|
|
|
|
(input[2] == '/')) {
|
|
|
|
input = input + 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [2B] */
|
|
|
|
if ((input[0] == '/') && (input[1] == '.')) {
|
|
|
|
if (input[2] == '/') {
|
|
|
|
input = input + 2;
|
|
|
|
continue;
|
|
|
|
} else if (input[2] == '\0') {
|
|
|
|
input = input + 1;
|
|
|
|
*input = '/';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2C] */
|
|
|
|
if ((input[2] == '.') && ((input[3] == '/') ||
|
|
|
|
(input[3] == '\0'))) {
|
|
|
|
if (input[3] == '/') {
|
|
|
|
input = input + 3;
|
|
|
|
} else {
|
|
|
|
input = input + 2;
|
|
|
|
*input = '/';
|
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
if ((output > start) &&
|
|
|
|
(output[-1] == '/'))
|
|
|
|
*--output = '\0';
|
|
|
|
split_point = strrchr(start, '/');
|
|
|
|
if (!split_point)
|
|
|
|
output = start;
|
|
|
|
else
|
|
|
|
output = split_point;
|
|
|
|
*output = '\0';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [2D] */
|
|
|
|
if (input[0] == '.') {
|
|
|
|
if (input[1] == '\0') {
|
|
|
|
input = input + 1;
|
|
|
|
continue;
|
|
|
|
} else if ((input[1] == '.') &&
|
|
|
|
(input[2] == '\0')) {
|
|
|
|
input = input + 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* [2E] */
|
|
|
|
if (*input == '/')
|
|
|
|
*output++ = *input++;
|
|
|
|
while ((*input != '/') && (*input != '\0'))
|
|
|
|
*output++ = *input++;
|
|
|
|
*output = '\0';
|
|
|
|
}
|
|
|
|
/* [3] */
|
|
|
|
merged_components.path = start;
|
2006-07-02 02:40:41 +04:00
|
|
|
}
|
2006-06-29 01:49:41 +04:00
|
|
|
|
|
|
|
/* 5.3 */
|
2008-10-14 03:49:15 +04:00
|
|
|
*result = url_reform_components((struct url_components *) merged_ptr);
|
2006-08-17 18:48:28 +04:00
|
|
|
if (!(*result))
|
2006-07-01 03:09:21 +04:00
|
|
|
goto url_join_no_mem;
|
2006-06-29 01:49:41 +04:00
|
|
|
|
|
|
|
/* return success */
|
|
|
|
status = URL_FUNC_OK;
|
|
|
|
|
2006-07-01 03:09:21 +04:00
|
|
|
url_join_no_mem:
|
2006-08-17 18:48:28 +04:00
|
|
|
free(start);
|
2006-06-29 01:49:41 +04:00
|
|
|
free(merge_path);
|
2008-10-14 03:49:15 +04:00
|
|
|
url_destroy_components((struct url_components *) base_ptr);
|
|
|
|
url_destroy_components((struct url_components *) rel_ptr);
|
2006-06-29 01:49:41 +04:00
|
|
|
return status;
|
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
|
|
|
{
|
2006-06-27 21:59:32 +04:00
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
2006-09-02 20:42:37 +04:00
|
|
|
const char *host_start, *host_end;
|
2004-08-09 20:11:58 +04:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
assert(url);
|
2004-03-02 21:02:41 +03:00
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url, &components);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status == URL_FUNC_OK) {
|
|
|
|
if (!components.authority) {
|
|
|
|
url_destroy_components(&components);
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
}
|
|
|
|
host_start = strchr(components.authority, '@');
|
|
|
|
host_start = host_start ? host_start + 1 : components.authority;
|
|
|
|
host_end = strchr(host_start, ':');
|
|
|
|
if (!host_end)
|
|
|
|
host_end = components.authority +
|
|
|
|
strlen(components.authority);
|
|
|
|
|
|
|
|
*result = malloc(host_end - host_start + 1);
|
|
|
|
if (!(*result)) {
|
|
|
|
url_destroy_components(&components);
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
}
|
|
|
|
memcpy((*result), host_start, host_end - host_start);
|
|
|
|
(*result)[host_end - host_start] = '\0';
|
2004-03-02 21:02:41 +03:00
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
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 21:59:32 +04:00
|
|
|
*
|
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 21:59:32 +04:00
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
|
|
|
|
2006-06-27 04:53:39 +04:00
|
|
|
assert(url);
|
2006-06-27 21:59:32 +04:00
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url, &components);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status == URL_FUNC_OK) {
|
2006-07-02 02:40:41 +04:00
|
|
|
if (!components.scheme) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else {
|
|
|
|
*result = strdup(components.scheme);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
}
|
2004-10-02 01:31:55 +04:00
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
2004-10-02 01:31:55 +04:00
|
|
|
}
|
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)
|
|
|
|
{
|
2006-06-27 21:59:32 +04:00
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
2006-02-19 21:26:23 +03:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
assert(url);
|
2006-02-19 21:26:23 +03:00
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url, &components);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status == URL_FUNC_OK) {
|
|
|
|
if ((!components.scheme) || (!components.authority)) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else {
|
|
|
|
*result = malloc(strlen(components.scheme) +
|
|
|
|
strlen(components.authority) + 4);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
else
|
|
|
|
sprintf((*result), "%s://%s", components.scheme,
|
|
|
|
components.authority);
|
|
|
|
}
|
2006-02-19 21:26:23 +03:00
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
2006-02-19 21:26:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-07-04 01:07:02 +04:00
|
|
|
* Strip the topmost segment of the path
|
2006-02-19 21:26:23 +03:00
|
|
|
*
|
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
|
|
|
|
*/
|
|
|
|
|
2006-07-04 01:07:02 +04:00
|
|
|
url_func_result url_parent(const char *url, char **result)
|
2006-02-19 21:26:23 +03:00
|
|
|
{
|
2006-06-27 21:59:32 +04:00
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
|
|
|
int len, path_len;
|
2006-02-19 21:26:23 +03:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
assert(url);
|
2006-02-19 21:26:23 +03:00
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url, &components);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status == URL_FUNC_OK) {
|
|
|
|
if ((!components.scheme) || (!components.authority) ||
|
|
|
|
(!components.path)) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else {
|
|
|
|
if (strcmp(components.path, "/")) {
|
|
|
|
path_len = strlen(components.path);
|
|
|
|
if (components.path[path_len - 1] == '/')
|
|
|
|
path_len--;
|
|
|
|
while (components.path[path_len - 1] != '/')
|
|
|
|
path_len--;
|
|
|
|
} else {
|
|
|
|
path_len = 1;
|
|
|
|
}
|
|
|
|
len = strlen(components.scheme) +
|
|
|
|
strlen(components.authority) +
|
|
|
|
path_len + 4;
|
|
|
|
*result = malloc(len);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
else
|
|
|
|
snprintf((*result), len, "%s://%s%s",
|
|
|
|
components.scheme,
|
|
|
|
components.authority,
|
|
|
|
components.path);
|
|
|
|
}
|
2006-02-19 21:26:23 +03:00
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
2006-02-19 21:26:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2006-06-27 21:59:32 +04:00
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
2006-04-10 03:21:13 +04:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
assert(url);
|
2006-04-10 03:21:13 +04:00
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url, &components);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status == URL_FUNC_OK) {
|
2006-07-02 02:40:41 +04:00
|
|
|
if (!components.path) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else if ((components.query) &&
|
|
|
|
(strlen(components.query) > 0)) {
|
2006-06-27 21:59:32 +04:00
|
|
|
*result = malloc(strlen(components.path) +
|
|
|
|
strlen(components.query) + 2);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
else
|
|
|
|
sprintf((*result), "%s?%s", components.path,
|
|
|
|
components.query);
|
|
|
|
} else {
|
|
|
|
*result = strdup(components.path);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
}
|
2006-04-10 03:21:13 +04:00
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
2006-04-10 03:21:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2006-06-27 21:59:32 +04:00
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
2006-06-20 01:49:25 +04:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
assert(url);
|
2006-06-20 01:49:25 +04:00
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url, &components);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (status == URL_FUNC_OK) {
|
|
|
|
if (!components.path) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else {
|
2008-01-07 15:01:11 +03:00
|
|
|
*result = strdup(components.path);
|
2006-06-27 21:59:32 +04:00
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
}
|
2006-06-20 01:49:25 +04:00
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
2006-06-20 01:49:25 +04:00
|
|
|
}
|
|
|
|
|
2008-01-07 07:54:08 +03:00
|
|
|
/**
|
2008-01-07 15:01:11 +03:00
|
|
|
* Extract leafname from an URL
|
2008-01-07 07:54:08 +03:00
|
|
|
*
|
|
|
|
* \param url an absolute URL
|
|
|
|
* \param result pointer to pointer to buffer to hold result
|
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
*/
|
|
|
|
|
|
|
|
url_func_result url_leafname(const char *url, char **result)
|
|
|
|
{
|
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
|
|
|
|
|
|
|
assert(url);
|
|
|
|
|
|
|
|
status = url_get_components(url, &components);
|
|
|
|
if (status == URL_FUNC_OK) {
|
|
|
|
if (!components.path) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else {
|
|
|
|
char *slash = strrchr(components.path, '/');
|
|
|
|
|
|
|
|
assert (slash != NULL);
|
|
|
|
|
|
|
|
*result = strdup(slash + 1);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
|
|
|
}
|
2006-06-20 01:49:25 +04:00
|
|
|
|
2008-02-03 15:04:48 +03:00
|
|
|
/**
|
|
|
|
* Extract fragment from an URL
|
|
|
|
* This will unescape any %xx entities in the fragment
|
|
|
|
*
|
|
|
|
* \param url an absolute URL
|
|
|
|
* \param result pointer to pointer to buffer to hold result
|
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
*/
|
|
|
|
|
|
|
|
url_func_result url_fragment(const char *url, char **result)
|
|
|
|
{
|
|
|
|
url_func_result status;
|
|
|
|
struct url_components components;
|
|
|
|
|
|
|
|
assert(url);
|
|
|
|
|
|
|
|
status = url_get_components(url, &components);
|
|
|
|
if (status == URL_FUNC_OK) {
|
|
|
|
if (!components.fragment) {
|
|
|
|
status = URL_FUNC_FAILED;
|
|
|
|
} else {
|
|
|
|
char *frag = curl_unescape(components.fragment,
|
|
|
|
strlen(components.fragment));
|
|
|
|
if (!frag) {
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
} else {
|
|
|
|
*result = strdup(frag);
|
|
|
|
if (!(*result))
|
|
|
|
status = URL_FUNC_NOMEM;
|
|
|
|
curl_free(frag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url_destroy_components(&components);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2008-04-02 04:43:51 +04:00
|
|
|
* \param unescaped the unescaped string
|
|
|
|
* \param toskip number of bytes to skip in unescaped string
|
|
|
|
* \param sptoplus true iff spaces should be converted to +
|
|
|
|
* \param escexceptions NULL or a string of characters excluded to be escaped
|
|
|
|
* \param result pointer to pointer to buffer to hold escaped string
|
2005-07-24 00:43:37 +04:00
|
|
|
* \return URL_FUNC_OK on success
|
2005-06-27 02:18:37 +04:00
|
|
|
*/
|
2005-07-24 00:43:37 +04:00
|
|
|
|
2008-04-02 04:43:51 +04:00
|
|
|
url_func_result url_escape(const char *unescaped, size_t toskip,
|
|
|
|
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)
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
len = strlen(unescaped);
|
2008-04-02 04:43:51 +04:00
|
|
|
if (len < toskip)
|
|
|
|
return URL_FUNC_FAILED;
|
|
|
|
len -= toskip;
|
2005-06-27 02:18:37 +04:00
|
|
|
|
|
|
|
escaped = malloc(len * 3 + 1);
|
|
|
|
if (!escaped)
|
|
|
|
return URL_FUNC_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);
|
|
|
|
return URL_FUNC_NOMEM;
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
}
|
2004-03-02 21:02:41 +03:00
|
|
|
|
2006-07-03 02:34:04 +04:00
|
|
|
/**
|
|
|
|
* Compare two absolute, normalized URLs
|
|
|
|
*
|
2008-02-03 15:04:48 +03:00
|
|
|
* \param url1 URL 1
|
|
|
|
* \param url2 URL 2
|
|
|
|
* \param nofrag Ignore fragment part in comparison
|
|
|
|
* \param result Pointer to location to store result (true if URLs match)
|
2006-07-03 02:34:04 +04:00
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
*/
|
2008-02-03 15:04:48 +03:00
|
|
|
url_func_result url_compare(const char *url1, const char *url2,
|
|
|
|
bool nofrag, bool *result)
|
2006-07-03 02:34:04 +04:00
|
|
|
{
|
|
|
|
url_func_result status;
|
|
|
|
struct url_components c1, c2;
|
|
|
|
bool res = true;
|
|
|
|
|
|
|
|
assert(url1 && url2 && result);
|
|
|
|
|
|
|
|
/* Decompose URLs */
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url1, &c1);
|
2006-07-03 02:34:04 +04:00
|
|
|
if (status != URL_FUNC_OK) {
|
|
|
|
url_destroy_components(&c1);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
status = url_get_components(url2, &c2);
|
2006-07-03 02:34:04 +04:00
|
|
|
if (status != URL_FUNC_OK) {
|
|
|
|
url_destroy_components(&c2);
|
|
|
|
url_destroy_components(&c1);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((c1.scheme && c2.scheme) || (!c1.scheme && !c2.scheme )) &&
|
|
|
|
((c1.authority && c2.authority) ||
|
|
|
|
(!c1.authority && !c2.authority)) &&
|
|
|
|
((c1.path && c2.path) || (!c1.path && !c2.path)) &&
|
|
|
|
((c1.query && c2.query) ||
|
|
|
|
(!c1.query && !c2.query)) &&
|
2008-02-03 15:04:48 +03:00
|
|
|
(nofrag || (c1.fragment && c2.fragment) ||
|
2006-07-03 02:34:04 +04:00
|
|
|
(!c1.fragment && !c2.fragment))) {
|
|
|
|
|
|
|
|
if (c1.scheme)
|
|
|
|
res &= strcasecmp(c1.scheme, c2.scheme) == 0;
|
|
|
|
|
|
|
|
/** \todo consider each part of the authority separately */
|
|
|
|
if (c1.authority)
|
|
|
|
res &= strcasecmp(c1.authority, c2.authority) == 0;
|
|
|
|
|
|
|
|
if (c1.path)
|
|
|
|
res &= strcmp(c1.path, c2.path) == 0;
|
|
|
|
|
|
|
|
if (c1.query)
|
|
|
|
res &= strcmp(c1.query, c2.query) == 0;
|
|
|
|
|
2008-02-03 15:04:48 +03:00
|
|
|
if (!nofrag && c1.fragment)
|
2006-07-03 02:34:04 +04:00
|
|
|
res &= strcmp(c1.fragment, c2.fragment) == 0;
|
|
|
|
} else {
|
|
|
|
/* Can't match */
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = res;
|
|
|
|
|
|
|
|
url_destroy_components(&c2);
|
|
|
|
url_destroy_components(&c1);
|
|
|
|
|
|
|
|
return URL_FUNC_OK;
|
|
|
|
}
|
2005-07-24 00:43:37 +04:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
/**
|
|
|
|
* Split a URL into separate components
|
|
|
|
*
|
|
|
|
* URLs passed to this function are assumed to be valid and no error checking
|
|
|
|
* or recovery is attempted.
|
|
|
|
*
|
|
|
|
* See RFC 3986 for reference.
|
|
|
|
*
|
2006-06-29 01:49:41 +04:00
|
|
|
* \param url a valid absolute or relative URL
|
|
|
|
* \param result pointer to buffer to hold components
|
2006-06-27 21:59:32 +04:00
|
|
|
* \return URL_FUNC_OK on success
|
|
|
|
*/
|
|
|
|
|
|
|
|
url_func_result url_get_components(const char *url,
|
2006-08-17 18:48:28 +04:00
|
|
|
struct url_components *result)
|
2006-06-27 21:59:32 +04:00
|
|
|
{
|
2006-08-17 18:48:28 +04:00
|
|
|
int storage_length;
|
2006-06-29 01:49:41 +04:00
|
|
|
char *storage_end;
|
2006-06-27 21:59:32 +04:00
|
|
|
const char *scheme;
|
|
|
|
const char *authority;
|
|
|
|
const char *path;
|
|
|
|
const char *query;
|
|
|
|
const char *fragment;
|
2006-09-02 20:42:37 +04:00
|
|
|
struct url_components_internal *internal;
|
2006-06-27 21:59:32 +04:00
|
|
|
|
|
|
|
assert(url);
|
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
/* clear our return value */
|
2006-09-02 20:42:37 +04:00
|
|
|
internal = (struct url_components_internal *)result;
|
2006-06-27 21:59:32 +04:00
|
|
|
memset(result, 0x00, sizeof(struct url_components));
|
|
|
|
|
|
|
|
/* get enough storage space for a URL with termination at each node */
|
2006-08-17 18:48:28 +04:00
|
|
|
storage_length = strlen(url) + 8;
|
2006-09-02 20:42:37 +04:00
|
|
|
internal->buffer = malloc(storage_length);
|
|
|
|
if (!internal->buffer)
|
2006-06-27 21:59:32 +04:00
|
|
|
return URL_FUNC_NOMEM;
|
2006-09-02 20:42:37 +04:00
|
|
|
storage_end = internal->buffer;
|
2006-06-27 21:59:32 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* look for a valid scheme */
|
|
|
|
scheme = url;
|
|
|
|
if (isalpha(*scheme)) {
|
|
|
|
for (scheme = url + 1;
|
|
|
|
((*scheme != ':') && (*scheme != '\0'));
|
2008-03-13 21:26:20 +03:00
|
|
|
scheme++) {
|
2006-06-29 01:49:41 +04:00
|
|
|
if (!isalnum(*scheme) && (*scheme != '+') &&
|
|
|
|
(*scheme != '-') && (*scheme != '.'))
|
|
|
|
break;
|
2008-03-13 21:26:20 +03:00
|
|
|
}
|
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
if (*scheme == ':') {
|
|
|
|
memcpy(storage_end, url, scheme - url);
|
|
|
|
storage_end[scheme - url] = '\0';
|
|
|
|
result->scheme = storage_end;
|
|
|
|
storage_end += scheme - url + 1;
|
|
|
|
scheme++;
|
|
|
|
} else {
|
|
|
|
scheme = url;
|
|
|
|
}
|
2006-06-27 21:59:32 +04:00
|
|
|
}
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-27 21:59:32 +04:00
|
|
|
|
|
|
|
/* look for an authority */
|
2006-06-29 01:49:41 +04:00
|
|
|
authority = scheme;
|
2006-06-27 21:59:32 +04:00
|
|
|
if ((authority[0] == '/') && (authority[1] == '/')) {
|
2006-06-29 01:49:41 +04:00
|
|
|
authority = strpbrk(scheme + 2, "/?#");
|
|
|
|
if (!authority)
|
|
|
|
authority = scheme + strlen(scheme);
|
2006-06-27 21:59:32 +04:00
|
|
|
memcpy(storage_end, scheme + 2, authority - scheme - 2);
|
|
|
|
storage_end[authority - scheme - 2] = '\0';
|
|
|
|
result->authority = storage_end;
|
|
|
|
storage_end += authority - scheme - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* look for a path */
|
2006-06-27 21:59:32 +04:00
|
|
|
path = authority;
|
|
|
|
if ((*path != '?') && (*path != '#') && (*path != '\0')) {
|
|
|
|
path = strpbrk(path, "?#");
|
|
|
|
if (!path)
|
|
|
|
path = authority + strlen(authority);
|
|
|
|
memcpy(storage_end, authority, path - authority);
|
|
|
|
storage_end[path - authority] = '\0';
|
|
|
|
result->path = storage_end;
|
|
|
|
storage_end += path - authority + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* look for a query */
|
|
|
|
query = path;
|
|
|
|
if (*query == '?') {
|
2006-06-29 01:49:41 +04:00
|
|
|
query = strchr(query, '#');
|
|
|
|
if (!query)
|
|
|
|
query = path + strlen(path);
|
2006-06-27 21:59:32 +04:00
|
|
|
memcpy(storage_end, path + 1, query - path - 1);
|
|
|
|
storage_end[query - path - 1] = '\0';
|
|
|
|
result->query = storage_end;
|
|
|
|
storage_end += query - path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* look for a fragment */
|
|
|
|
fragment = query;
|
|
|
|
if (*fragment == '#') {
|
2006-06-29 01:49:41 +04:00
|
|
|
fragment = query + strlen(query);
|
2006-06-27 21:59:32 +04:00
|
|
|
|
|
|
|
/* make a copy of the result for the caller */
|
|
|
|
memcpy(storage_end, query + 1, fragment - query - 1);
|
|
|
|
storage_end[fragment - query - 1] = '\0';
|
|
|
|
result->fragment = storage_end;
|
2006-08-17 18:48:28 +04:00
|
|
|
storage_end += fragment - query;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
|
2006-08-17 18:48:28 +04:00
|
|
|
assert((result->buffer + storage_length) >= storage_end);
|
2006-06-29 01:49:41 +04:00
|
|
|
return URL_FUNC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reform a URL from separate components
|
|
|
|
*
|
|
|
|
* See RFC 3986 for reference.
|
|
|
|
*
|
|
|
|
* \param components the components to reform into a URL
|
|
|
|
* \return a new URL allocated on the stack, or NULL on failure
|
|
|
|
*/
|
|
|
|
|
2006-09-02 20:42:37 +04:00
|
|
|
char *url_reform_components(const struct url_components *components)
|
2006-06-29 01:49:41 +04:00
|
|
|
{
|
|
|
|
int scheme_len = 0, authority_len = 0, path_len = 0, query_len = 0,
|
|
|
|
fragment_len = 0;
|
2006-07-02 02:40:41 +04:00
|
|
|
char *result, *url;
|
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* 5.3 */
|
|
|
|
if (components->scheme)
|
|
|
|
scheme_len = strlen(components->scheme) + 1;
|
|
|
|
if (components->authority)
|
|
|
|
authority_len = strlen(components->authority) + 2;
|
|
|
|
if (components->path)
|
|
|
|
path_len = strlen(components->path);
|
|
|
|
if (components->query)
|
|
|
|
query_len = strlen(components->query) + 1;
|
|
|
|
if (components->fragment)
|
|
|
|
fragment_len = strlen(components->fragment) + 1;
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* claim memory */
|
|
|
|
url = result = malloc(scheme_len + authority_len + path_len +
|
|
|
|
query_len + fragment_len + 1);
|
|
|
|
if (!url) {
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
return NULL;
|
2006-06-27 21:59:32 +04:00
|
|
|
}
|
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
/* rebuild URL */
|
|
|
|
if (components->scheme) {
|
|
|
|
sprintf(url, "%s:", components->scheme);
|
|
|
|
url += scheme_len;
|
|
|
|
}
|
|
|
|
if (components->authority) {
|
|
|
|
sprintf(url, "//%s", components->authority);
|
2006-07-02 02:40:41 +04:00
|
|
|
url += authority_len;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
if (components->path) {
|
|
|
|
sprintf(url, "%s", components->path);
|
2006-07-02 02:40:41 +04:00
|
|
|
url += path_len;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
if (components->query) {
|
|
|
|
sprintf(url, "?%s", components->query);
|
2006-07-02 02:40:41 +04:00
|
|
|
url += query_len;
|
2006-06-29 01:49:41 +04:00
|
|
|
}
|
|
|
|
if (components->fragment)
|
|
|
|
sprintf(url, "#%s", components->fragment);
|
|
|
|
return result;
|
2006-06-27 21:59:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release some url components from memory
|
|
|
|
*
|
|
|
|
* \param result pointer to buffer containing components
|
|
|
|
*/
|
2006-09-02 20:42:37 +04:00
|
|
|
void url_destroy_components(const struct url_components *components)
|
2006-06-27 21:59:32 +04:00
|
|
|
{
|
2006-09-02 20:42:37 +04:00
|
|
|
const struct url_components_internal *internal;
|
|
|
|
|
2006-06-29 01:49:41 +04:00
|
|
|
assert(components);
|
2006-07-02 02:40:41 +04:00
|
|
|
|
2006-09-02 20:42:37 +04:00
|
|
|
internal = (const struct url_components_internal *)components;
|
|
|
|
if (internal->buffer)
|
|
|
|
free(internal->buffer);
|
2006-06-27 21:59:32 +04:00
|
|
|
}
|
|
|
|
|
2006-06-29 01:49:41 +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
|