1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.10.2.3 2002/11/12 16:50:40 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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.2.3 2002/11/12 16:50:40 oes
37 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
39 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
40 * Added basic support for OPTIONS and TRACE HTTP methods:
41 * parse_http_url now recognizes the "*" URI as well as
42 * the OPTIONS and TRACE method keywords.
44 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
45 * Adding support for proprietary Microsoft WebDAV extensions
47 * Revision 1.10 2002/05/12 21:40:37 jongfoster
48 * - Removing some unused code
50 * Revision 1.9 2002/04/04 00:36:36 gliptak
51 * always use pcre for matching
53 * Revision 1.8 2002/04/03 23:32:47 jongfoster
54 * Fixing memory leak on error
56 * Revision 1.7 2002/03/26 22:29:55 swa
57 * we have a new homepage!
59 * Revision 1.6 2002/03/24 13:25:43 swa
60 * name change related issues
62 * Revision 1.5 2002/03/13 00:27:05 jongfoster
65 * Revision 1.4 2002/03/07 03:46:17 oes
66 * Fixed compiler warnings
68 * Revision 1.3 2002/03/03 14:51:11 oes
69 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
71 * Revision 1.2 2002/01/21 00:14:09 jongfoster
72 * Correcting comment style
73 * Fixing an uninitialized memory bug in create_url_spec()
75 * Revision 1.1 2002/01/17 20:53:46 jongfoster
76 * Moving all our URL and URL pattern parsing code to the same file - it
77 * was scattered around in filters.c, loaders.c and parsers.c.
79 * Providing a single, simple url_match(pattern,url) function - rather than
80 * the 3-line match routine which was repeated all over the place.
82 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
84 * Providing parse_http_url() so that URLs can be parsed without faking a
85 * HTTP request line for parse_http_request() or repeating the parsing
86 * code (both of which were techniques that were actually in use).
88 * Standardizing that struct http_request is used to represent a URL, and
89 * struct url_spec is used to represent a URL pattern. (Before, URLs were
90 * represented as seperate variables and a partially-filled-in url_spec).
93 *********************************************************************/
100 #include <sys/types.h>
108 #if !defined(_WIN32) && !defined(__OS2__)
113 #include "urlmatch.h"
115 #include "miscutil.h"
118 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
121 /*********************************************************************
123 * Function : free_http_request
125 * Description : Freez a http_request structure
128 * 1 : http = points to a http_request structure to free
132 *********************************************************************/
133 void free_http_request(struct http_request *http)
142 freez(http->hostport);
145 freez(http->host_ip_addr_str);
146 freez(http->dbuffer);
152 /*********************************************************************
154 * Function : parse_http_url
156 * Description : Parse out the host and port from the URL. Find the
157 * hostname & path, port (if ':'), and/or password (if '@')
160 * 1 : url = URL (or is it URI?) to break down
161 * 2 : http = pointer to the http structure to hold elements.
162 * Will be zeroed before use. Note that this
163 * function sets the http->gpc and http->ver
165 * 3 : csp = Current client state (buffers, headers, etc...)
167 * Returns : JB_ERR_OK on success
168 * JB_ERR_MEMORY on out of memory
169 * JB_ERR_PARSE on malformed command/URL
170 * or >100 domains deep.
172 *********************************************************************/
173 jb_err parse_http_url(const char * url,
174 struct http_request *http,
175 struct client_state *csp)
178 * Zero out the results structure
180 memset(http, '\0', sizeof(*http));
184 * Save our initial URL
186 http->url = strdup(url);
187 if (http->url == NULL)
189 return JB_ERR_MEMORY;
194 * Check for * URI. If found, we're done.
196 if (*http->url == '*')
198 http->path = strdup("*");
199 http->hostport = strdup("");
205 * Split URL into protocol,hostport,path.
215 return JB_ERR_MEMORY;
218 /* Find the start of the URL in our scratch space */
220 if (strncmpic(url_noproto, "http://", 7) == 0)
225 else if (strncmpic(url_noproto, "https://", 8) == 0)
235 url_path = strchr(url_noproto, '/');
236 if (url_path != NULL)
241 * NOTE: The following line ignores the path for HTTPS URLS.
242 * This means that you get consistent behaviour if you type a
243 * https URL in and it's parsed by the function. (When the
244 * URL is actually retrieved, SSL hides the path part).
246 http->path = strdup(http->ssl ? "/" : url_path);
248 http->hostport = strdup(url_noproto);
253 * Repair broken HTTP requests that don't contain a path,
254 * or CONNECT requests
256 http->path = strdup("/");
257 http->hostport = strdup(url_noproto);
262 if ( (http->path == NULL)
263 || (http->hostport == NULL))
266 return JB_ERR_MEMORY;
272 * Split hostport into user/password (ignored), host, port.
279 buf = strdup(http->hostport);
282 return JB_ERR_MEMORY;
285 /* check if url contains username and/or password */
286 host = strchr(buf, '@');
289 /* Contains username/password, skip it and the @ sign. */
294 /* No username or password. */
298 /* check if url contains port */
299 port = strchr(host, ':');
303 /* Terminate hostname and point to start of port string */
305 http->port = atoi(port);
309 /* No port specified. */
310 http->port = (http->ssl ? 143 : 80);
313 http->host = strdup(host);
317 if (http->host == NULL)
319 return JB_ERR_MEMORY;
324 * Split domain name so we can compare it against wildcards
328 char *vec[BUFFER_SIZE];
332 http->dbuffer = strdup(http->host);
333 if (NULL == http->dbuffer)
335 return JB_ERR_MEMORY;
338 /* map to lower case */
339 for (p = http->dbuffer; *p ; p++)
341 *p = tolower((int)(unsigned char)*p);
344 /* split the domain name into components */
345 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
347 if (http->dcount <= 0)
350 * Error: More than SZ(vec) components in domain
351 * or: no components in domain
356 /* save a copy of the pointers in dvec */
357 size = http->dcount * sizeof(*http->dvec);
359 http->dvec = (char **)malloc(size);
360 if (NULL == http->dvec)
362 return JB_ERR_MEMORY;
365 memcpy(http->dvec, vec, size);
373 /*********************************************************************
375 * Function : parse_http_request
377 * Description : Parse out the host and port from the URL. Find the
378 * hostname & path, port (if ':'), and/or password (if '@')
381 * 1 : req = HTTP request line to break down
382 * 2 : http = pointer to the http structure to hold elements
383 * 3 : csp = Current client state (buffers, headers, etc...)
385 * Returns : JB_ERR_OK on success
386 * JB_ERR_MEMORY on out of memory
387 * JB_ERR_CGI_PARAMS on malformed command/URL
388 * or >100 domains deep.
390 *********************************************************************/
391 jb_err parse_http_request(const char *req,
392 struct http_request *http,
393 struct client_state *csp)
401 memset(http, '\0', sizeof(*http));
406 return JB_ERR_MEMORY;
409 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
416 /* this could be a CONNECT request */
417 if (strcmpic(v[0], "connect") == 0)
422 /* or it could be any other basic HTTP request type */
423 else if ((0 == strcmpic(v[0], "get"))
424 || (0 == strcmpic(v[0], "head"))
425 || (0 == strcmpic(v[0], "post"))
426 || (0 == strcmpic(v[0], "put"))
427 || (0 == strcmpic(v[0], "delete"))
428 || (0 == strcmpic(v[0], "options"))
429 || (0 == strcmpic(v[0], "trace"))
431 /* or a webDAV extension (RFC2518) */
432 || (0 == strcmpic(v[0], "propfind"))
433 || (0 == strcmpic(v[0], "proppatch"))
434 || (0 == strcmpic(v[0], "move"))
435 || (0 == strcmpic(v[0], "copy"))
436 || (0 == strcmpic(v[0], "mkcol"))
437 || (0 == strcmpic(v[0], "lock"))
438 || (0 == strcmpic(v[0], "unlock"))
440 /* Or a Microsoft webDAV extension for Exchange 2000. See: */
441 /* http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
442 /* http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */
443 || (0 == strcmpic(v[0], "bcopy"))
444 || (0 == strcmpic(v[0], "bmove"))
445 || (0 == strcmpic(v[0], "bdelete"))
446 || (0 == strcmpic(v[0], "bpropfind"))
447 || (0 == strcmpic(v[0], "bproppatch"))
449 /* Or another Microsoft webDAV extension for Exchange 2000. See: */
450 /* http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt */
451 /* http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
452 /* http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */
453 || (0 == strcmpic(v[0], "subscribe"))
454 || (0 == strcmpic(v[0], "unsubscribe"))
455 || (0 == strcmpic(v[0], "notify"))
456 || (0 == strcmpic(v[0], "poll"))
464 /* Unknown HTTP method */
469 err = parse_http_url(v[1], http, csp);
477 * Copy the details into the structure
479 http->ssl = is_connect;
480 http->cmd = strdup(req);
481 http->gpc = strdup(v[0]);
482 http->ver = strdup(v[2]);
484 if ( (http->cmd == NULL)
485 || (http->gpc == NULL)
486 || (http->ver == NULL) )
489 return JB_ERR_MEMORY;
498 /*********************************************************************
500 * Function : simple_domaincmp
502 * Description : Domain-wise Compare fqdn's. The comparison is
503 * both left- and right-anchored. The individual
504 * domain names are compared with simplematch().
505 * This is only used by domain_match.
508 * 1 : pv = array of patterns to compare
509 * 2 : fv = array of domain components to compare
510 * 3 : len = length of the arrays (both arrays are the
511 * same length - if they weren't, it couldn't
512 * possibly be a match).
514 * Returns : 0 => domains are equivalent, else no match.
516 *********************************************************************/
517 static int simple_domaincmp(char **pv, char **fv, int len)
521 for (n = 0; n < len; n++)
523 if (simplematch(pv[n], fv[n]))
534 /*********************************************************************
536 * Function : domain_match
538 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
539 * pattern->unachored, the comparison is un-, left-,
540 * right-anchored, or both.
541 * The individual domain names are compared with
545 * 1 : pattern = a domain that may contain a '*' as a wildcard.
546 * 2 : fqdn = domain name against which the patterns are compared.
548 * Returns : 0 => domains are equivalent, else no match.
550 *********************************************************************/
551 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
553 char **pv, **fv; /* vectors */
555 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
557 plen = pattern->dcount;
562 /* fqdn is too short to match this pattern */
569 if (unanchored == ANCHOR_LEFT)
574 * Convert this into a fully anchored pattern with
575 * the fqdn and pattern the same length
577 fv += (flen - plen); /* flen - plen >= 0 due to check above */
578 return simple_domaincmp(pv, fv, plen);
580 else if (unanchored == 0)
582 /* Fully anchored, check length */
587 return simple_domaincmp(pv, fv, plen);
589 else if (unanchored == ANCHOR_RIGHT)
591 /* Left anchored, ignore all extra in fqdn */
592 return simple_domaincmp(pv, fv, plen);
598 int maxn = flen - plen;
599 for (n = 0; n <= maxn; n++)
601 if (!simple_domaincmp(pv, fv, plen))
606 * Doesn't match from start of fqdn
607 * Try skipping first part of fqdn
617 /*********************************************************************
619 * Function : create_url_spec
621 * Description : Creates a "url_spec" structure from a string.
622 * When finished, free with unload_url().
625 * 1 : url = Target url_spec to be filled in. Will be
627 * 2 : buf = Source pattern, null terminated. NOTE: The
628 * contents of this buffer are destroyed by this
629 * function. If this function succeeds, the
630 * buffer is copied to url->spec. If this
631 * function fails, the contents of the buffer
634 * Returns : JB_ERR_OK - Success
635 * JB_ERR_MEMORY - Out of memory
636 * JB_ERR_PARSE - Cannot parse regex (Detailed message
637 * written to system log)
639 *********************************************************************/
640 jb_err create_url_spec(struct url_spec * url, const char * buf)
648 memset(url, '\0', sizeof(*url));
650 /* save a copy of the orignal specification */
651 if ((url->spec = strdup(buf)) == NULL)
653 return JB_ERR_MEMORY;
656 if ((p = strchr(buf, '/')) != NULL)
658 if (NULL == (url->path = strdup(p)))
661 return JB_ERR_MEMORY;
663 url->pathlen = strlen(url->path);
674 char rebuf[BUFFER_SIZE];
676 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
680 return JB_ERR_MEMORY;
683 sprintf(rebuf, "^(%s)", url->path);
685 errcode = regcomp(url->preg, rebuf,
686 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
689 size_t errlen = regerror(errcode,
690 url->preg, rebuf, sizeof(rebuf));
692 if (errlen > (sizeof(rebuf) - (size_t)1))
694 errlen = sizeof(rebuf) - (size_t)1;
696 rebuf[errlen] = '\0';
698 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
709 if ((p = strchr(buf, ':')) == NULL)
724 /* Parse domain part */
725 if (buf[strlen(buf) - 1] == '.')
727 url->unanchored |= ANCHOR_RIGHT;
731 url->unanchored |= ANCHOR_LEFT;
734 /* split domain into components */
736 url->dbuffer = strdup(buf);
737 if (NULL == url->dbuffer)
743 return JB_ERR_MEMORY;
746 /* map to lower case */
747 for (p = url->dbuffer; *p ; p++)
749 *p = tolower((int)(unsigned char)*p);
752 /* split the domain name into components */
753 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
763 return JB_ERR_MEMORY;
765 else if (url->dcount != 0)
768 /* save a copy of the pointers in dvec */
769 size = url->dcount * sizeof(*url->dvec);
771 url->dvec = (char **)malloc(size);
772 if (NULL == url->dvec)
780 return JB_ERR_MEMORY;
783 memcpy(url->dvec, v, size);
792 /*********************************************************************
794 * Function : free_url_spec
796 * Description : Called from the "unloaders". Freez the url
797 * structure elements.
800 * 1 : url = pointer to a url_spec structure.
804 *********************************************************************/
805 void free_url_spec(struct url_spec *url)
807 if (url == NULL) return;
821 /*********************************************************************
823 * Function : url_match
825 * Description : Compare a URL against a URL pattern.
828 * 1 : pattern = a URL pattern
829 * 2 : url = URL to match
831 * Returns : 0 iff the URL matches the pattern, else nonzero.
833 *********************************************************************/
834 int url_match(const struct url_spec *pattern,
835 const struct http_request *url)
837 return ((pattern->port == 0) || (pattern->port == url->port))
838 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
839 && ((pattern->path == NULL) ||
840 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)