1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.7 2002/03/26 22:29:55 swa 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.7 2002/03/26 22:29:55 swa
37 * we have a new homepage!
39 * Revision 1.6 2002/03/24 13:25:43 swa
40 * name change related issues
42 * Revision 1.5 2002/03/13 00:27:05 jongfoster
45 * Revision 1.4 2002/03/07 03:46:17 oes
46 * Fixed compiler warnings
48 * Revision 1.3 2002/03/03 14:51:11 oes
49 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
51 * Revision 1.2 2002/01/21 00:14:09 jongfoster
52 * Correcting comment style
53 * Fixing an uninitialized memory bug in create_url_spec()
55 * Revision 1.1 2002/01/17 20:53:46 jongfoster
56 * Moving all our URL and URL pattern parsing code to the same file - it
57 * was scattered around in filters.c, loaders.c and parsers.c.
59 * Providing a single, simple url_match(pattern,url) function - rather than
60 * the 3-line match routine which was repeated all over the place.
62 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
64 * Providing parse_http_url() so that URLs can be parsed without faking a
65 * HTTP request line for parse_http_request() or repeating the parsing
66 * code (both of which were techniques that were actually in use).
68 * Standardizing that struct http_request is used to represent a URL, and
69 * struct url_spec is used to represent a URL pattern. (Before, URLs were
70 * represented as seperate variables and a partially-filled-in url_spec).
73 *********************************************************************/
80 #include <sys/types.h>
88 #if !defined(_WIN32) && !defined(__OS2__)
98 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
100 /* Fix a problem with Solaris. There should be no effect on other
102 * Solaris's isspace() is a macro which uses it's argument directly
103 * as an array index. Therefore we need to make sure that high-bit
104 * characters generate +ve values, and ideally we also want to make
105 * the argument match the declared parameter type of "int".
107 * Why did they write a character function that can't take a simple
108 * "char" argument? Doh!
110 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
111 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
114 /*********************************************************************
116 * Function : free_http_request
118 * Description : Freez a http_request structure
121 * 1 : http = points to a http_request structure to free
125 *********************************************************************/
126 void free_http_request(struct http_request *http)
135 freez(http->hostport);
138 freez(http->host_ip_addr_str);
139 freez(http->dbuffer);
145 /*********************************************************************
147 * Function : parse_http_url
149 * Description : Parse out the host and port from the URL. Find the
150 * hostname & path, port (if ':'), and/or password (if '@')
153 * 1 : url = URL (or is it URI?) to break down
154 * 2 : http = pointer to the http structure to hold elements.
155 * Will be zeroed before use. Note that this
156 * function sets the http->gpc and http->ver
158 * 3 : csp = Current client state (buffers, headers, etc...)
160 * Returns : JB_ERR_OK on success
161 * JB_ERR_MEMORY on out of memory
162 * JB_ERR_CGI_PARAMS on malformed command/URL
163 * or >100 domains deep.
165 *********************************************************************/
166 jb_err parse_http_url(const char * url,
167 struct http_request *http,
168 struct client_state *csp)
171 * Zero out the results structure
173 memset(http, '\0', sizeof(*http));
177 * Save our initial URL
179 http->url = strdup(url);
180 if (http->url == NULL)
182 return JB_ERR_MEMORY;
187 * Split URL into protocol,hostport,path.
197 return JB_ERR_MEMORY;
200 /* Find the start of the URL in our scratch space */
202 if (strncmpic(url_noproto, "http://", 7) == 0)
207 else if (strncmpic(url_noproto, "https://", 8) == 0)
217 url_path = strchr(url_noproto, '/');
218 if (url_path != NULL)
223 * NOTE: The following line ignores the path for HTTPS URLS.
224 * This means that you get consistent behaviour if you type a
225 * https URL in and it's parsed by the function. (When the
226 * URL is actually retrieved, SSL hides the path part).
228 http->path = strdup(http->ssl ? "/" : url_path);
230 http->hostport = strdup(url_noproto);
235 * Repair broken HTTP requests that don't contain a path,
236 * or CONNECT requests
238 http->path = strdup("/");
239 http->hostport = strdup(url_noproto);
244 if ( (http->path == NULL)
245 || (http->hostport == NULL))
248 free_http_request(http);
249 return JB_ERR_MEMORY;
255 * Split hostport into user/password (ignored), host, port.
262 buf = strdup(http->hostport);
265 free_http_request(http);
266 return JB_ERR_MEMORY;
269 /* check if url contains username and/or password */
270 host = strchr(buf, '@');
273 /* Contains username/password, skip it and the @ sign. */
278 /* No username or password. */
282 /* check if url contains port */
283 port = strchr(host, ':');
287 /* Terminate hostname and point to start of port string */
289 http->port = atoi(port);
293 /* No port specified. */
294 http->port = (http->ssl ? 143 : 80);
297 http->host = strdup(host);
301 if (http->host == NULL)
303 free_http_request(http);
304 return JB_ERR_MEMORY;
310 * Split domain name so we can compare it against wildcards
313 char *vec[BUFFER_SIZE];
317 http->dbuffer = strdup(http->host);
318 if (NULL == http->dbuffer)
320 free_http_request(http);
321 return JB_ERR_MEMORY;
324 /* map to lower case */
325 for (p = http->dbuffer; *p ; p++)
327 *p = tolower((int)(unsigned char)*p);
330 /* split the domain name into components */
331 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
333 if (http->dcount <= 0)
336 * Error: More than SZ(vec) components in domain
337 * or: no components in domain
339 free_http_request(http);
343 /* save a copy of the pointers in dvec */
344 size = http->dcount * sizeof(*http->dvec);
346 http->dvec = (char **)malloc(size);
347 if (NULL == http->dvec)
349 free_http_request(http);
350 return JB_ERR_MEMORY;
353 memcpy(http->dvec, vec, size);
361 /*********************************************************************
363 * Function : parse_http_request
365 * Description : Parse out the host and port from the URL. Find the
366 * hostname & path, port (if ':'), and/or password (if '@')
369 * 1 : req = HTTP request line to break down
370 * 2 : http = pointer to the http structure to hold elements
371 * 3 : csp = Current client state (buffers, headers, etc...)
373 * Returns : JB_ERR_OK on success
374 * JB_ERR_MEMORY on out of memory
375 * JB_ERR_CGI_PARAMS on malformed command/URL
376 * or >100 domains deep.
378 *********************************************************************/
379 jb_err parse_http_request(const char *req,
380 struct http_request *http,
381 struct client_state *csp)
389 memset(http, '\0', sizeof(*http));
394 return JB_ERR_MEMORY;
397 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
404 /* this could be a CONNECT request */
405 if (strcmpic(v[0], "connect") == 0)
410 /* or it could be any other basic HTTP request type */
411 else if ((0 == strcmpic(v[0], "get"))
412 || (0 == strcmpic(v[0], "head"))
413 || (0 == strcmpic(v[0], "post"))
414 || (0 == strcmpic(v[0], "put"))
415 || (0 == strcmpic(v[0], "delete"))
417 /* or a webDAV extension (RFC2518) */
418 || (0 == strcmpic(v[0], "propfind"))
419 || (0 == strcmpic(v[0], "proppatch"))
420 || (0 == strcmpic(v[0], "move"))
421 || (0 == strcmpic(v[0], "copy"))
422 || (0 == strcmpic(v[0], "mkcol"))
423 || (0 == strcmpic(v[0], "lock"))
424 || (0 == strcmpic(v[0], "unlock"))
432 /* Unknown HTTP method */
437 err = parse_http_url(v[1], http, csp);
445 * Copy the details into the structure
447 http->ssl = is_connect;
448 http->cmd = strdup(req);
449 http->gpc = strdup(v[0]);
450 http->ver = strdup(v[2]);
452 if ( (http->cmd == NULL)
453 || (http->gpc == NULL)
454 || (http->ver == NULL) )
457 free_http_request(http);
458 return JB_ERR_MEMORY;
465 /*********************************************************************
467 * Function : simple_domaincmp
469 * Description : Domain-wise Compare fqdn's. The comparison is
470 * both left- and right-anchored. The individual
471 * domain names are compared with simplematch().
472 * This is only used by domain_match.
475 * 1 : pv = array of patterns to compare
476 * 2 : fv = array of domain components to compare
477 * 3 : len = length of the arrays (both arrays are the
478 * same length - if they weren't, it couldn't
479 * possibly be a match).
481 * Returns : 0 => domains are equivalent, else no match.
483 *********************************************************************/
484 static int simple_domaincmp(char **pv, char **fv, int len)
488 for (n = 0; n < len; n++)
490 if (simplematch(pv[n], fv[n]))
501 /*********************************************************************
503 * Function : domain_match
505 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
506 * pattern->unachored, the comparison is un-, left-,
507 * right-anchored, or both.
508 * The individual domain names are compared with
512 * 1 : pattern = a domain that may contain a '*' as a wildcard.
513 * 2 : fqdn = domain name against which the patterns are compared.
515 * Returns : 0 => domains are equivalent, else no match.
517 *********************************************************************/
518 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
520 char **pv, **fv; /* vectors */
522 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
524 plen = pattern->dcount;
529 /* fqdn is too short to match this pattern */
536 if (unanchored == ANCHOR_LEFT)
541 * Convert this into a fully anchored pattern with
542 * the fqdn and pattern the same length
544 fv += (flen - plen); /* flen - plen >= 0 due to check above */
545 return simple_domaincmp(pv, fv, plen);
547 else if (unanchored == 0)
549 /* Fully anchored, check length */
554 return simple_domaincmp(pv, fv, plen);
556 else if (unanchored == ANCHOR_RIGHT)
558 /* Left anchored, ignore all extra in fqdn */
559 return simple_domaincmp(pv, fv, plen);
565 int maxn = flen - plen;
566 for (n = 0; n <= maxn; n++)
568 if (!simple_domaincmp(pv, fv, plen))
573 * Doesn't match from start of fqdn
574 * Try skipping first part of fqdn
584 /*********************************************************************
586 * Function : create_url_spec
588 * Description : Creates a "url_spec" structure from a string.
589 * When finished, free with unload_url().
592 * 1 : url = Target url_spec to be filled in. Will be
594 * 2 : buf = Source pattern, null terminated. NOTE: The
595 * contents of this buffer are destroyed by this
596 * function. If this function succeeds, the
597 * buffer is copied to url->spec. If this
598 * function fails, the contents of the buffer
601 * Returns : JB_ERR_OK - Success
602 * JB_ERR_MEMORY - Out of memory
603 * JB_ERR_PARSE - Cannot parse regex (Detailed message
604 * written to system log)
606 *********************************************************************/
607 jb_err create_url_spec(struct url_spec * url, const char * buf)
615 memset(url, '\0', sizeof(*url));
617 /* save a copy of the orignal specification */
618 if ((url->spec = strdup(buf)) == NULL)
620 return JB_ERR_MEMORY;
623 if ((p = strchr(buf, '/')) != NULL)
625 if (NULL == (url->path = strdup(p)))
628 return JB_ERR_MEMORY;
630 url->pathlen = strlen(url->path);
642 char rebuf[BUFFER_SIZE];
644 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
648 return JB_ERR_MEMORY;
651 sprintf(rebuf, "^(%s)", url->path);
653 errcode = regcomp(url->preg, rebuf,
654 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
657 size_t errlen = regerror(errcode,
658 url->preg, rebuf, sizeof(rebuf));
660 if (errlen > (sizeof(rebuf) - (size_t)1))
662 errlen = sizeof(rebuf) - (size_t)1;
664 rebuf[errlen] = '\0';
666 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
678 if ((p = strchr(buf, ':')) == NULL)
693 /* Parse domain part */
694 if (buf[strlen(buf) - 1] == '.')
696 url->unanchored |= ANCHOR_RIGHT;
700 url->unanchored |= ANCHOR_LEFT;
703 /* split domain into components */
705 url->dbuffer = strdup(buf);
706 if (NULL == url->dbuffer)
713 #endif /* def REGEX */
714 return JB_ERR_MEMORY;
717 /* map to lower case */
718 for (p = url->dbuffer; *p ; p++)
720 *p = tolower((int)(unsigned char)*p);
723 /* split the domain name into components */
724 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
733 #endif /* def REGEX */
736 return JB_ERR_MEMORY;
738 else if (url->dcount != 0)
741 /* save a copy of the pointers in dvec */
742 size = url->dcount * sizeof(*url->dvec);
744 url->dvec = (char **)malloc(size);
745 if (NULL == url->dvec)
752 #endif /* def REGEX */
755 return JB_ERR_MEMORY;
758 memcpy(url->dvec, v, size);
767 /*********************************************************************
769 * Function : free_url_spec
771 * Description : Called from the "unloaders". Freez the url
772 * structure elements.
775 * 1 : url = pointer to a url_spec structure.
779 *********************************************************************/
780 void free_url_spec(struct url_spec *url)
782 if (url == NULL) return;
799 /*********************************************************************
801 * Function : url_match
803 * Description : Compare a URL against a URL pattern.
806 * 1 : pattern = a URL pattern
807 * 2 : url = URL to match
809 * Returns : 0 iff the URL matches the pattern, else nonzero.
811 *********************************************************************/
812 int url_match(const struct url_spec *pattern,
813 const struct http_request *url)
815 return ((pattern->port == 0) || (pattern->port == url->port))
816 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
817 && ((pattern->path == NULL) ||
819 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)
821 (strncmp(pattern->path, url->path, pattern->pathlen) == 0)