1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.8 2002/04/03 23:32:47 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.8 2002/04/03 23:32:47 jongfoster
37 * Fixing memory leak on error
39 * Revision 1.7 2002/03/26 22:29:55 swa
40 * we have a new homepage!
42 * Revision 1.6 2002/03/24 13:25:43 swa
43 * name change related issues
45 * Revision 1.5 2002/03/13 00:27:05 jongfoster
48 * Revision 1.4 2002/03/07 03:46:17 oes
49 * Fixed compiler warnings
51 * Revision 1.3 2002/03/03 14:51:11 oes
52 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
54 * Revision 1.2 2002/01/21 00:14:09 jongfoster
55 * Correcting comment style
56 * Fixing an uninitialized memory bug in create_url_spec()
58 * Revision 1.1 2002/01/17 20:53:46 jongfoster
59 * Moving all our URL and URL pattern parsing code to the same file - it
60 * was scattered around in filters.c, loaders.c and parsers.c.
62 * Providing a single, simple url_match(pattern,url) function - rather than
63 * the 3-line match routine which was repeated all over the place.
65 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
67 * Providing parse_http_url() so that URLs can be parsed without faking a
68 * HTTP request line for parse_http_request() or repeating the parsing
69 * code (both of which were techniques that were actually in use).
71 * Standardizing that struct http_request is used to represent a URL, and
72 * struct url_spec is used to represent a URL pattern. (Before, URLs were
73 * represented as seperate variables and a partially-filled-in url_spec).
76 *********************************************************************/
83 #include <sys/types.h>
91 #if !defined(_WIN32) && !defined(__OS2__)
101 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
103 /* Fix a problem with Solaris. There should be no effect on other
105 * Solaris's isspace() is a macro which uses it's argument directly
106 * as an array index. Therefore we need to make sure that high-bit
107 * characters generate +ve values, and ideally we also want to make
108 * the argument match the declared parameter type of "int".
110 * Why did they write a character function that can't take a simple
111 * "char" argument? Doh!
113 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
114 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
117 /*********************************************************************
119 * Function : free_http_request
121 * Description : Freez a http_request structure
124 * 1 : http = points to a http_request structure to free
128 *********************************************************************/
129 void free_http_request(struct http_request *http)
138 freez(http->hostport);
141 freez(http->host_ip_addr_str);
142 freez(http->dbuffer);
148 /*********************************************************************
150 * Function : parse_http_url
152 * Description : Parse out the host and port from the URL. Find the
153 * hostname & path, port (if ':'), and/or password (if '@')
156 * 1 : url = URL (or is it URI?) to break down
157 * 2 : http = pointer to the http structure to hold elements.
158 * Will be zeroed before use. Note that this
159 * function sets the http->gpc and http->ver
161 * 3 : csp = Current client state (buffers, headers, etc...)
163 * Returns : JB_ERR_OK on success
164 * JB_ERR_MEMORY on out of memory
165 * JB_ERR_CGI_PARAMS on malformed command/URL
166 * or >100 domains deep.
168 *********************************************************************/
169 jb_err parse_http_url(const char * url,
170 struct http_request *http,
171 struct client_state *csp)
174 * Zero out the results structure
176 memset(http, '\0', sizeof(*http));
180 * Save our initial URL
182 http->url = strdup(url);
183 if (http->url == NULL)
185 return JB_ERR_MEMORY;
190 * Split URL into protocol,hostport,path.
200 return JB_ERR_MEMORY;
203 /* Find the start of the URL in our scratch space */
205 if (strncmpic(url_noproto, "http://", 7) == 0)
210 else if (strncmpic(url_noproto, "https://", 8) == 0)
220 url_path = strchr(url_noproto, '/');
221 if (url_path != NULL)
226 * NOTE: The following line ignores the path for HTTPS URLS.
227 * This means that you get consistent behaviour if you type a
228 * https URL in and it's parsed by the function. (When the
229 * URL is actually retrieved, SSL hides the path part).
231 http->path = strdup(http->ssl ? "/" : url_path);
233 http->hostport = strdup(url_noproto);
238 * Repair broken HTTP requests that don't contain a path,
239 * or CONNECT requests
241 http->path = strdup("/");
242 http->hostport = strdup(url_noproto);
247 if ( (http->path == NULL)
248 || (http->hostport == NULL))
251 free_http_request(http);
252 return JB_ERR_MEMORY;
258 * Split hostport into user/password (ignored), host, port.
265 buf = strdup(http->hostport);
268 free_http_request(http);
269 return JB_ERR_MEMORY;
272 /* check if url contains username and/or password */
273 host = strchr(buf, '@');
276 /* Contains username/password, skip it and the @ sign. */
281 /* No username or password. */
285 /* check if url contains port */
286 port = strchr(host, ':');
290 /* Terminate hostname and point to start of port string */
292 http->port = atoi(port);
296 /* No port specified. */
297 http->port = (http->ssl ? 143 : 80);
300 http->host = strdup(host);
304 if (http->host == NULL)
306 free_http_request(http);
307 return JB_ERR_MEMORY;
313 * Split domain name so we can compare it against wildcards
316 char *vec[BUFFER_SIZE];
320 http->dbuffer = strdup(http->host);
321 if (NULL == http->dbuffer)
323 free_http_request(http);
324 return JB_ERR_MEMORY;
327 /* map to lower case */
328 for (p = http->dbuffer; *p ; p++)
330 *p = tolower((int)(unsigned char)*p);
333 /* split the domain name into components */
334 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
336 if (http->dcount <= 0)
339 * Error: More than SZ(vec) components in domain
340 * or: no components in domain
342 free_http_request(http);
346 /* save a copy of the pointers in dvec */
347 size = http->dcount * sizeof(*http->dvec);
349 http->dvec = (char **)malloc(size);
350 if (NULL == http->dvec)
352 free_http_request(http);
353 return JB_ERR_MEMORY;
356 memcpy(http->dvec, vec, size);
364 /*********************************************************************
366 * Function : parse_http_request
368 * Description : Parse out the host and port from the URL. Find the
369 * hostname & path, port (if ':'), and/or password (if '@')
372 * 1 : req = HTTP request line to break down
373 * 2 : http = pointer to the http structure to hold elements
374 * 3 : csp = Current client state (buffers, headers, etc...)
376 * Returns : JB_ERR_OK on success
377 * JB_ERR_MEMORY on out of memory
378 * JB_ERR_CGI_PARAMS on malformed command/URL
379 * or >100 domains deep.
381 *********************************************************************/
382 jb_err parse_http_request(const char *req,
383 struct http_request *http,
384 struct client_state *csp)
392 memset(http, '\0', sizeof(*http));
397 return JB_ERR_MEMORY;
400 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
407 /* this could be a CONNECT request */
408 if (strcmpic(v[0], "connect") == 0)
413 /* or it could be any other basic HTTP request type */
414 else if ((0 == strcmpic(v[0], "get"))
415 || (0 == strcmpic(v[0], "head"))
416 || (0 == strcmpic(v[0], "post"))
417 || (0 == strcmpic(v[0], "put"))
418 || (0 == strcmpic(v[0], "delete"))
420 /* or a webDAV extension (RFC2518) */
421 || (0 == strcmpic(v[0], "propfind"))
422 || (0 == strcmpic(v[0], "proppatch"))
423 || (0 == strcmpic(v[0], "move"))
424 || (0 == strcmpic(v[0], "copy"))
425 || (0 == strcmpic(v[0], "mkcol"))
426 || (0 == strcmpic(v[0], "lock"))
427 || (0 == strcmpic(v[0], "unlock"))
435 /* Unknown HTTP method */
440 err = parse_http_url(v[1], http, csp);
448 * Copy the details into the structure
450 http->ssl = is_connect;
451 http->cmd = strdup(req);
452 http->gpc = strdup(v[0]);
453 http->ver = strdup(v[2]);
455 if ( (http->cmd == NULL)
456 || (http->gpc == NULL)
457 || (http->ver == NULL) )
460 free_http_request(http);
461 return JB_ERR_MEMORY;
468 /*********************************************************************
470 * Function : simple_domaincmp
472 * Description : Domain-wise Compare fqdn's. The comparison is
473 * both left- and right-anchored. The individual
474 * domain names are compared with simplematch().
475 * This is only used by domain_match.
478 * 1 : pv = array of patterns to compare
479 * 2 : fv = array of domain components to compare
480 * 3 : len = length of the arrays (both arrays are the
481 * same length - if they weren't, it couldn't
482 * possibly be a match).
484 * Returns : 0 => domains are equivalent, else no match.
486 *********************************************************************/
487 static int simple_domaincmp(char **pv, char **fv, int len)
491 for (n = 0; n < len; n++)
493 if (simplematch(pv[n], fv[n]))
504 /*********************************************************************
506 * Function : domain_match
508 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
509 * pattern->unachored, the comparison is un-, left-,
510 * right-anchored, or both.
511 * The individual domain names are compared with
515 * 1 : pattern = a domain that may contain a '*' as a wildcard.
516 * 2 : fqdn = domain name against which the patterns are compared.
518 * Returns : 0 => domains are equivalent, else no match.
520 *********************************************************************/
521 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
523 char **pv, **fv; /* vectors */
525 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
527 plen = pattern->dcount;
532 /* fqdn is too short to match this pattern */
539 if (unanchored == ANCHOR_LEFT)
544 * Convert this into a fully anchored pattern with
545 * the fqdn and pattern the same length
547 fv += (flen - plen); /* flen - plen >= 0 due to check above */
548 return simple_domaincmp(pv, fv, plen);
550 else if (unanchored == 0)
552 /* Fully anchored, check length */
557 return simple_domaincmp(pv, fv, plen);
559 else if (unanchored == ANCHOR_RIGHT)
561 /* Left anchored, ignore all extra in fqdn */
562 return simple_domaincmp(pv, fv, plen);
568 int maxn = flen - plen;
569 for (n = 0; n <= maxn; n++)
571 if (!simple_domaincmp(pv, fv, plen))
576 * Doesn't match from start of fqdn
577 * Try skipping first part of fqdn
587 /*********************************************************************
589 * Function : create_url_spec
591 * Description : Creates a "url_spec" structure from a string.
592 * When finished, free with unload_url().
595 * 1 : url = Target url_spec to be filled in. Will be
597 * 2 : buf = Source pattern, null terminated. NOTE: The
598 * contents of this buffer are destroyed by this
599 * function. If this function succeeds, the
600 * buffer is copied to url->spec. If this
601 * function fails, the contents of the buffer
604 * Returns : JB_ERR_OK - Success
605 * JB_ERR_MEMORY - Out of memory
606 * JB_ERR_PARSE - Cannot parse regex (Detailed message
607 * written to system log)
609 *********************************************************************/
610 jb_err create_url_spec(struct url_spec * url, const char * buf)
618 memset(url, '\0', sizeof(*url));
620 /* save a copy of the orignal specification */
621 if ((url->spec = strdup(buf)) == NULL)
623 return JB_ERR_MEMORY;
626 if ((p = strchr(buf, '/')) != NULL)
628 if (NULL == (url->path = strdup(p)))
631 return JB_ERR_MEMORY;
633 url->pathlen = strlen(url->path);
644 char rebuf[BUFFER_SIZE];
646 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
650 return JB_ERR_MEMORY;
653 sprintf(rebuf, "^(%s)", url->path);
655 errcode = regcomp(url->preg, rebuf,
656 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
659 size_t errlen = regerror(errcode,
660 url->preg, rebuf, sizeof(rebuf));
662 if (errlen > (sizeof(rebuf) - (size_t)1))
664 errlen = sizeof(rebuf) - (size_t)1;
666 rebuf[errlen] = '\0';
668 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
679 if ((p = strchr(buf, ':')) == NULL)
694 /* Parse domain part */
695 if (buf[strlen(buf) - 1] == '.')
697 url->unanchored |= ANCHOR_RIGHT;
701 url->unanchored |= ANCHOR_LEFT;
704 /* split domain into components */
706 url->dbuffer = strdup(buf);
707 if (NULL == url->dbuffer)
713 return JB_ERR_MEMORY;
716 /* map to lower case */
717 for (p = url->dbuffer; *p ; p++)
719 *p = tolower((int)(unsigned char)*p);
722 /* split the domain name into components */
723 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
733 return JB_ERR_MEMORY;
735 else if (url->dcount != 0)
738 /* save a copy of the pointers in dvec */
739 size = url->dcount * sizeof(*url->dvec);
741 url->dvec = (char **)malloc(size);
742 if (NULL == url->dvec)
750 return JB_ERR_MEMORY;
753 memcpy(url->dvec, v, size);
762 /*********************************************************************
764 * Function : free_url_spec
766 * Description : Called from the "unloaders". Freez the url
767 * structure elements.
770 * 1 : url = pointer to a url_spec structure.
774 *********************************************************************/
775 void free_url_spec(struct url_spec *url)
777 if (url == NULL) return;
791 /*********************************************************************
793 * Function : url_match
795 * Description : Compare a URL against a URL pattern.
798 * 1 : pattern = a URL pattern
799 * 2 : url = URL to match
801 * Returns : 0 iff the URL matches the pattern, else nonzero.
803 *********************************************************************/
804 int url_match(const struct url_spec *pattern,
805 const struct http_request *url)
807 return ((pattern->port == 0) || (pattern->port == url->port))
808 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
809 && ((pattern->path == NULL) ||
810 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)