1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.10 2002/05/12 21:40:37 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
6 * Purpose : Declares functions to match URLs against URL
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 * $Log: urlmatch.c,v $
36 * Revision 1.10 2002/05/12 21:40:37 jongfoster
37 * - Removing some unused code
39 * Revision 1.9 2002/04/04 00:36:36 gliptak
40 * always use pcre for matching
42 * Revision 1.8 2002/04/03 23:32:47 jongfoster
43 * Fixing memory leak on error
45 * Revision 1.7 2002/03/26 22:29:55 swa
46 * we have a new homepage!
48 * Revision 1.6 2002/03/24 13:25:43 swa
49 * name change related issues
51 * Revision 1.5 2002/03/13 00:27:05 jongfoster
54 * Revision 1.4 2002/03/07 03:46:17 oes
55 * Fixed compiler warnings
57 * Revision 1.3 2002/03/03 14:51:11 oes
58 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
60 * Revision 1.2 2002/01/21 00:14:09 jongfoster
61 * Correcting comment style
62 * Fixing an uninitialized memory bug in create_url_spec()
64 * Revision 1.1 2002/01/17 20:53:46 jongfoster
65 * Moving all our URL and URL pattern parsing code to the same file - it
66 * was scattered around in filters.c, loaders.c and parsers.c.
68 * Providing a single, simple url_match(pattern,url) function - rather than
69 * the 3-line match routine which was repeated all over the place.
71 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
73 * Providing parse_http_url() so that URLs can be parsed without faking a
74 * HTTP request line for parse_http_request() or repeating the parsing
75 * code (both of which were techniques that were actually in use).
77 * Standardizing that struct http_request is used to represent a URL, and
78 * struct url_spec is used to represent a URL pattern. (Before, URLs were
79 * represented as seperate variables and a partially-filled-in url_spec).
82 *********************************************************************/
89 #include <sys/types.h>
97 #if !defined(_WIN32) && !defined(__OS2__)
102 #include "urlmatch.h"
104 #include "miscutil.h"
107 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
110 /*********************************************************************
112 * Function : free_http_request
114 * Description : Freez a http_request structure
117 * 1 : http = points to a http_request structure to free
121 *********************************************************************/
122 void free_http_request(struct http_request *http)
131 freez(http->hostport);
134 freez(http->host_ip_addr_str);
135 freez(http->dbuffer);
141 /*********************************************************************
143 * Function : parse_http_url
145 * Description : Parse out the host and port from the URL. Find the
146 * hostname & path, port (if ':'), and/or password (if '@')
149 * 1 : url = URL (or is it URI?) to break down
150 * 2 : http = pointer to the http structure to hold elements.
151 * Will be zeroed before use. Note that this
152 * function sets the http->gpc and http->ver
154 * 3 : csp = Current client state (buffers, headers, etc...)
156 * Returns : JB_ERR_OK on success
157 * JB_ERR_MEMORY on out of memory
158 * JB_ERR_CGI_PARAMS on malformed command/URL
159 * or >100 domains deep.
161 *********************************************************************/
162 jb_err parse_http_url(const char * url,
163 struct http_request *http,
164 struct client_state *csp)
167 * Zero out the results structure
169 memset(http, '\0', sizeof(*http));
173 * Save our initial URL
175 http->url = strdup(url);
176 if (http->url == NULL)
178 return JB_ERR_MEMORY;
183 * Split URL into protocol,hostport,path.
193 return JB_ERR_MEMORY;
196 /* Find the start of the URL in our scratch space */
198 if (strncmpic(url_noproto, "http://", 7) == 0)
203 else if (strncmpic(url_noproto, "https://", 8) == 0)
213 url_path = strchr(url_noproto, '/');
214 if (url_path != NULL)
219 * NOTE: The following line ignores the path for HTTPS URLS.
220 * This means that you get consistent behaviour if you type a
221 * https URL in and it's parsed by the function. (When the
222 * URL is actually retrieved, SSL hides the path part).
224 http->path = strdup(http->ssl ? "/" : url_path);
226 http->hostport = strdup(url_noproto);
231 * Repair broken HTTP requests that don't contain a path,
232 * or CONNECT requests
234 http->path = strdup("/");
235 http->hostport = strdup(url_noproto);
240 if ( (http->path == NULL)
241 || (http->hostport == NULL))
244 free_http_request(http);
245 return JB_ERR_MEMORY;
251 * Split hostport into user/password (ignored), host, port.
258 buf = strdup(http->hostport);
261 free_http_request(http);
262 return JB_ERR_MEMORY;
265 /* check if url contains username and/or password */
266 host = strchr(buf, '@');
269 /* Contains username/password, skip it and the @ sign. */
274 /* No username or password. */
278 /* check if url contains port */
279 port = strchr(host, ':');
283 /* Terminate hostname and point to start of port string */
285 http->port = atoi(port);
289 /* No port specified. */
290 http->port = (http->ssl ? 143 : 80);
293 http->host = strdup(host);
297 if (http->host == NULL)
299 free_http_request(http);
300 return JB_ERR_MEMORY;
306 * Split domain name so we can compare it against wildcards
309 char *vec[BUFFER_SIZE];
313 http->dbuffer = strdup(http->host);
314 if (NULL == http->dbuffer)
316 free_http_request(http);
317 return JB_ERR_MEMORY;
320 /* map to lower case */
321 for (p = http->dbuffer; *p ; p++)
323 *p = tolower((int)(unsigned char)*p);
326 /* split the domain name into components */
327 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
329 if (http->dcount <= 0)
332 * Error: More than SZ(vec) components in domain
333 * or: no components in domain
335 free_http_request(http);
339 /* save a copy of the pointers in dvec */
340 size = http->dcount * sizeof(*http->dvec);
342 http->dvec = (char **)malloc(size);
343 if (NULL == http->dvec)
345 free_http_request(http);
346 return JB_ERR_MEMORY;
349 memcpy(http->dvec, vec, size);
357 /*********************************************************************
359 * Function : parse_http_request
361 * Description : Parse out the host and port from the URL. Find the
362 * hostname & path, port (if ':'), and/or password (if '@')
365 * 1 : req = HTTP request line to break down
366 * 2 : http = pointer to the http structure to hold elements
367 * 3 : csp = Current client state (buffers, headers, etc...)
369 * Returns : JB_ERR_OK on success
370 * JB_ERR_MEMORY on out of memory
371 * JB_ERR_CGI_PARAMS on malformed command/URL
372 * or >100 domains deep.
374 *********************************************************************/
375 jb_err parse_http_request(const char *req,
376 struct http_request *http,
377 struct client_state *csp)
385 memset(http, '\0', sizeof(*http));
390 return JB_ERR_MEMORY;
393 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
400 /* this could be a CONNECT request */
401 if (strcmpic(v[0], "connect") == 0)
406 /* or it could be any other basic HTTP request type */
407 else if ((0 == strcmpic(v[0], "get"))
408 || (0 == strcmpic(v[0], "head"))
409 || (0 == strcmpic(v[0], "post"))
410 || (0 == strcmpic(v[0], "put"))
411 || (0 == strcmpic(v[0], "delete"))
413 /* or a webDAV extension (RFC2518) */
414 || (0 == strcmpic(v[0], "propfind"))
415 || (0 == strcmpic(v[0], "proppatch"))
416 || (0 == strcmpic(v[0], "move"))
417 || (0 == strcmpic(v[0], "copy"))
418 || (0 == strcmpic(v[0], "mkcol"))
419 || (0 == strcmpic(v[0], "lock"))
420 || (0 == strcmpic(v[0], "unlock"))
428 /* Unknown HTTP method */
433 err = parse_http_url(v[1], http, csp);
441 * Copy the details into the structure
443 http->ssl = is_connect;
444 http->cmd = strdup(req);
445 http->gpc = strdup(v[0]);
446 http->ver = strdup(v[2]);
448 if ( (http->cmd == NULL)
449 || (http->gpc == NULL)
450 || (http->ver == NULL) )
453 free_http_request(http);
454 return JB_ERR_MEMORY;
461 /*********************************************************************
463 * Function : simple_domaincmp
465 * Description : Domain-wise Compare fqdn's. The comparison is
466 * both left- and right-anchored. The individual
467 * domain names are compared with simplematch().
468 * This is only used by domain_match.
471 * 1 : pv = array of patterns to compare
472 * 2 : fv = array of domain components to compare
473 * 3 : len = length of the arrays (both arrays are the
474 * same length - if they weren't, it couldn't
475 * possibly be a match).
477 * Returns : 0 => domains are equivalent, else no match.
479 *********************************************************************/
480 static int simple_domaincmp(char **pv, char **fv, int len)
484 for (n = 0; n < len; n++)
486 if (simplematch(pv[n], fv[n]))
497 /*********************************************************************
499 * Function : domain_match
501 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
502 * pattern->unachored, the comparison is un-, left-,
503 * right-anchored, or both.
504 * The individual domain names are compared with
508 * 1 : pattern = a domain that may contain a '*' as a wildcard.
509 * 2 : fqdn = domain name against which the patterns are compared.
511 * Returns : 0 => domains are equivalent, else no match.
513 *********************************************************************/
514 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
516 char **pv, **fv; /* vectors */
518 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
520 plen = pattern->dcount;
525 /* fqdn is too short to match this pattern */
532 if (unanchored == ANCHOR_LEFT)
537 * Convert this into a fully anchored pattern with
538 * the fqdn and pattern the same length
540 fv += (flen - plen); /* flen - plen >= 0 due to check above */
541 return simple_domaincmp(pv, fv, plen);
543 else if (unanchored == 0)
545 /* Fully anchored, check length */
550 return simple_domaincmp(pv, fv, plen);
552 else if (unanchored == ANCHOR_RIGHT)
554 /* Left anchored, ignore all extra in fqdn */
555 return simple_domaincmp(pv, fv, plen);
561 int maxn = flen - plen;
562 for (n = 0; n <= maxn; n++)
564 if (!simple_domaincmp(pv, fv, plen))
569 * Doesn't match from start of fqdn
570 * Try skipping first part of fqdn
580 /*********************************************************************
582 * Function : create_url_spec
584 * Description : Creates a "url_spec" structure from a string.
585 * When finished, free with unload_url().
588 * 1 : url = Target url_spec to be filled in. Will be
590 * 2 : buf = Source pattern, null terminated. NOTE: The
591 * contents of this buffer are destroyed by this
592 * function. If this function succeeds, the
593 * buffer is copied to url->spec. If this
594 * function fails, the contents of the buffer
597 * Returns : JB_ERR_OK - Success
598 * JB_ERR_MEMORY - Out of memory
599 * JB_ERR_PARSE - Cannot parse regex (Detailed message
600 * written to system log)
602 *********************************************************************/
603 jb_err create_url_spec(struct url_spec * url, const char * buf)
611 memset(url, '\0', sizeof(*url));
613 /* save a copy of the orignal specification */
614 if ((url->spec = strdup(buf)) == NULL)
616 return JB_ERR_MEMORY;
619 if ((p = strchr(buf, '/')) != NULL)
621 if (NULL == (url->path = strdup(p)))
624 return JB_ERR_MEMORY;
626 url->pathlen = strlen(url->path);
637 char rebuf[BUFFER_SIZE];
639 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
643 return JB_ERR_MEMORY;
646 sprintf(rebuf, "^(%s)", url->path);
648 errcode = regcomp(url->preg, rebuf,
649 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
652 size_t errlen = regerror(errcode,
653 url->preg, rebuf, sizeof(rebuf));
655 if (errlen > (sizeof(rebuf) - (size_t)1))
657 errlen = sizeof(rebuf) - (size_t)1;
659 rebuf[errlen] = '\0';
661 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
672 if ((p = strchr(buf, ':')) == NULL)
687 /* Parse domain part */
688 if (buf[strlen(buf) - 1] == '.')
690 url->unanchored |= ANCHOR_RIGHT;
694 url->unanchored |= ANCHOR_LEFT;
697 /* split domain into components */
699 url->dbuffer = strdup(buf);
700 if (NULL == url->dbuffer)
706 return JB_ERR_MEMORY;
709 /* map to lower case */
710 for (p = url->dbuffer; *p ; p++)
712 *p = tolower((int)(unsigned char)*p);
715 /* split the domain name into components */
716 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
726 return JB_ERR_MEMORY;
728 else if (url->dcount != 0)
731 /* save a copy of the pointers in dvec */
732 size = url->dcount * sizeof(*url->dvec);
734 url->dvec = (char **)malloc(size);
735 if (NULL == url->dvec)
743 return JB_ERR_MEMORY;
746 memcpy(url->dvec, v, size);
755 /*********************************************************************
757 * Function : free_url_spec
759 * Description : Called from the "unloaders". Freez the url
760 * structure elements.
763 * 1 : url = pointer to a url_spec structure.
767 *********************************************************************/
768 void free_url_spec(struct url_spec *url)
770 if (url == NULL) return;
784 /*********************************************************************
786 * Function : url_match
788 * Description : Compare a URL against a URL pattern.
791 * 1 : pattern = a URL pattern
792 * 2 : url = URL to match
794 * Returns : 0 iff the URL matches the pattern, else nonzero.
796 *********************************************************************/
797 int url_match(const struct url_spec *pattern,
798 const struct http_request *url)
800 return ((pattern->port == 0) || (pattern->port == url->port))
801 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
802 && ((pattern->path == NULL) ||
803 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)