1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.29 2008/04/10 04:17:56 fabiankeil 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-2003, 2006-2008 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.29 2008/04/10 04:17:56 fabiankeil
37 * In url_match(), check the right member for NULL when determining
38 * whether there's a path regex to execute. Looking for a plain-text
39 * representation works as well, but it looks "interesting" and that
40 * member will be removed soonish anyway.
42 * Revision 1.28 2008/04/08 16:07:39 fabiankeil
43 * Make it harder to mistake url_match()'s
44 * second parameter for an url_spec.
46 * Revision 1.27 2008/04/08 15:44:33 fabiankeil
47 * Save a bit of memory (and a few cpu cycles) by not bothering to
48 * compile slash-only path regexes that don't affect the result.
50 * Revision 1.26 2008/04/07 16:57:18 fabiankeil
51 * - Use free_url_spec() more consistently.
52 * - Let it reset url->dcount just in case.
54 * Revision 1.25 2008/04/06 15:18:38 fabiankeil
55 * Oh well, rename the --enable-pcre-host-patterns option to
56 * --enable-extended-host-patterns as it's not really PCRE syntax.
58 * Revision 1.24 2008/04/06 14:54:26 fabiankeil
59 * Use PCRE syntax in host patterns when configured
60 * with --enable-pcre-host-patterns.
62 * Revision 1.23 2008/04/05 12:19:20 fabiankeil
63 * Factor compile_host_pattern() out of create_url_spec().
65 * Revision 1.22 2008/03/30 15:02:32 fabiankeil
66 * SZitify unknown_method().
68 * Revision 1.21 2007/12/24 16:34:23 fabiankeil
69 * Band-aid (and micro-optimization) that makes it less likely to run out of
70 * stack space with overly-complex path patterns. Probably masks the problem
71 * reported by Lee in #1856679. Hohoho.
73 * Revision 1.20 2007/09/02 15:31:20 fabiankeil
74 * Move match_portlist() from filter.c to urlmatch.c.
75 * It's used for url matching, not for filtering.
77 * Revision 1.19 2007/09/02 13:42:11 fabiankeil
78 * - Allow port lists in url patterns.
79 * - Ditch unused url_spec member pathlen.
81 * Revision 1.18 2007/07/30 16:42:21 fabiankeil
82 * Move the method check into unknown_method()
83 * and loop through the known methods instead
84 * of using a screen-long OR chain.
86 * Revision 1.17 2007/04/15 16:39:21 fabiankeil
87 * Introduce tags as alternative way to specify which
88 * actions apply to a request. At the moment tags can be
89 * created based on client and server headers.
91 * Revision 1.16 2007/02/13 13:59:24 fabiankeil
92 * Remove redundant log message.
94 * Revision 1.15 2007/01/28 16:11:23 fabiankeil
95 * Accept WebDAV methods for subversion
96 * in parse_http_request(). Closes FR 1581425.
98 * Revision 1.14 2007/01/06 14:23:56 fabiankeil
99 * Fix gcc43 warnings. Mark *csp as immutable
100 * for parse_http_url() and url_match().
101 * Replace a sprintf call with snprintf.
103 * Revision 1.13 2006/12/06 19:50:54 fabiankeil
104 * parse_http_url() now handles intercepted
105 * HTTP request lines as well. Moved parts
106 * of parse_http_url()'s code into
107 * init_domain_components() so that it can
108 * be reused in chat().
110 * Revision 1.12 2006/07/18 14:48:47 david__schmidt
111 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
112 * with what was really the latest development (the v_3_0_branch branch)
114 * Revision 1.10.2.7 2003/05/17 15:57:24 oes
115 * - parse_http_url now checks memory allocation failure for
116 * duplication of "*" URL and rejects "*something" URLs
118 * - Added a comment to what might look like a bug in
119 * create_url_spec (see !bug #736931)
120 * - Comment cosmetics
122 * Revision 1.10.2.6 2003/05/07 12:39:48 oes
123 * Fix typo: Default port for https URLs is 443, not 143.
124 * Thanks to Scott Tregear for spotting this one.
126 * Revision 1.10.2.5 2003/02/28 13:09:29 oes
127 * Fixed a rare double free condition as per Bug #694713
129 * Revision 1.10.2.4 2003/02/28 12:57:44 oes
130 * Moved freeing of http request structure to its owner
131 * as per Dan Price's observations in Bug #694713
133 * Revision 1.10.2.3 2002/11/12 16:50:40 oes
134 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
136 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
137 * Added basic support for OPTIONS and TRACE HTTP methods:
138 * parse_http_url now recognizes the "*" URI as well as
139 * the OPTIONS and TRACE method keywords.
141 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
142 * Adding support for proprietary Microsoft WebDAV extensions
144 * Revision 1.10 2002/05/12 21:40:37 jongfoster
145 * - Removing some unused code
147 * Revision 1.9 2002/04/04 00:36:36 gliptak
148 * always use pcre for matching
150 * Revision 1.8 2002/04/03 23:32:47 jongfoster
151 * Fixing memory leak on error
153 * Revision 1.7 2002/03/26 22:29:55 swa
154 * we have a new homepage!
156 * Revision 1.6 2002/03/24 13:25:43 swa
157 * name change related issues
159 * Revision 1.5 2002/03/13 00:27:05 jongfoster
162 * Revision 1.4 2002/03/07 03:46:17 oes
163 * Fixed compiler warnings
165 * Revision 1.3 2002/03/03 14:51:11 oes
166 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
168 * Revision 1.2 2002/01/21 00:14:09 jongfoster
169 * Correcting comment style
170 * Fixing an uninitialized memory bug in create_url_spec()
172 * Revision 1.1 2002/01/17 20:53:46 jongfoster
173 * Moving all our URL and URL pattern parsing code to the same file - it
174 * was scattered around in filters.c, loaders.c and parsers.c.
176 * Providing a single, simple url_match(pattern,url) function - rather than
177 * the 3-line match routine which was repeated all over the place.
179 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
181 * Providing parse_http_url() so that URLs can be parsed without faking a
182 * HTTP request line for parse_http_request() or repeating the parsing
183 * code (both of which were techniques that were actually in use).
185 * Standardizing that struct http_request is used to represent a URL, and
186 * struct url_spec is used to represent a URL pattern. (Before, URLs were
187 * represented as seperate variables and a partially-filled-in url_spec).
190 *********************************************************************/
197 #include <sys/types.h>
205 #if !defined(_WIN32) && !defined(__OS2__)
210 #include "urlmatch.h"
212 #include "miscutil.h"
215 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
218 /*********************************************************************
220 * Function : free_http_request
222 * Description : Freez a http_request structure
225 * 1 : http = points to a http_request structure to free
229 *********************************************************************/
230 void free_http_request(struct http_request *http)
239 freez(http->hostport);
242 freez(http->host_ip_addr_str);
243 freez(http->dbuffer);
249 /*********************************************************************
251 * Function : init_domain_components
253 * Description : Splits the domain name so we can compare it
254 * against wildcards. It used to be part of
255 * parse_http_url, but was separated because the
256 * same code is required in chat in case of
257 * intercepted requests.
260 * 1 : http = pointer to the http structure to hold elements.
262 * Returns : JB_ERR_OK on success
263 * JB_ERR_MEMORY on out of memory
264 * JB_ERR_PARSE on malformed command/URL
265 * or >100 domains deep.
267 *********************************************************************/
268 jb_err init_domain_components(struct http_request *http)
270 char *vec[BUFFER_SIZE];
274 http->dbuffer = strdup(http->host);
275 if (NULL == http->dbuffer)
277 return JB_ERR_MEMORY;
280 /* map to lower case */
281 for (p = http->dbuffer; *p ; p++)
283 *p = (char)tolower((int)(unsigned char)*p);
286 /* split the domain name into components */
287 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
289 if (http->dcount <= 0)
292 * Error: More than SZ(vec) components in domain
293 * or: no components in domain
295 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
299 /* save a copy of the pointers in dvec */
300 size = (size_t)http->dcount * sizeof(*http->dvec);
302 http->dvec = (char **)malloc(size);
303 if (NULL == http->dvec)
305 return JB_ERR_MEMORY;
308 memcpy(http->dvec, vec, size);
314 /*********************************************************************
316 * Function : parse_http_url
318 * Description : Parse out the host and port from the URL. Find the
319 * hostname & path, port (if ':'), and/or password (if '@')
322 * 1 : url = URL (or is it URI?) to break down
323 * 2 : http = pointer to the http structure to hold elements.
324 * Will be zeroed before use. Note that this
325 * function sets the http->gpc and http->ver
327 * 3 : csp = Current client state (buffers, headers, etc...)
329 * Returns : JB_ERR_OK on success
330 * JB_ERR_MEMORY on out of memory
331 * JB_ERR_PARSE on malformed command/URL
332 * or >100 domains deep.
334 *********************************************************************/
335 jb_err parse_http_url(const char * url,
336 struct http_request *http,
337 const struct client_state *csp)
339 int host_available = 1; /* A proxy can dream. */
342 * Zero out the results structure
344 memset(http, '\0', sizeof(*http));
348 * Save our initial URL
350 http->url = strdup(url);
351 if (http->url == NULL)
353 return JB_ERR_MEMORY;
358 * Check for * URI. If found, we're done.
360 if (*http->url == '*')
362 if ( NULL == (http->path = strdup("*"))
363 || NULL == (http->hostport = strdup("")) )
365 return JB_ERR_MEMORY;
367 if (http->url[1] != '\0')
376 * Split URL into protocol,hostport,path.
386 return JB_ERR_MEMORY;
389 /* Find the start of the URL in our scratch space */
391 if (strncmpic(url_noproto, "http://", 7) == 0)
396 else if (strncmpic(url_noproto, "https://", 8) == 0)
401 else if (*url_noproto == '/')
404 * Short request line without protocol and host.
405 * Most likely because the client's request
406 * was intercepted and redirected into Privoxy.
417 url_path = strchr(url_noproto, '/');
418 if (url_path != NULL)
423 * NOTE: The following line ignores the path for HTTPS URLS.
424 * This means that you get consistent behaviour if you type a
425 * https URL in and it's parsed by the function. (When the
426 * URL is actually retrieved, SSL hides the path part).
428 http->path = strdup(http->ssl ? "/" : url_path);
430 http->hostport = strdup(url_noproto);
435 * Repair broken HTTP requests that don't contain a path,
436 * or CONNECT requests
438 http->path = strdup("/");
439 http->hostport = strdup(url_noproto);
444 if ( (http->path == NULL)
445 || (http->hostport == NULL))
447 return JB_ERR_MEMORY;
453 /* Without host, there is nothing left to do here */
458 * Split hostport into user/password (ignored), host, port.
465 buf = strdup(http->hostport);
468 return JB_ERR_MEMORY;
471 /* check if url contains username and/or password */
472 host = strchr(buf, '@');
475 /* Contains username/password, skip it and the @ sign. */
480 /* No username or password. */
484 /* check if url contains port */
485 port = strchr(host, ':');
489 /* Terminate hostname and point to start of port string */
491 http->port = atoi(port);
495 /* No port specified. */
496 http->port = (http->ssl ? 443 : 80);
499 http->host = strdup(host);
503 if (http->host == NULL)
505 return JB_ERR_MEMORY;
510 * Split domain name so we can compare it against wildcards
512 return init_domain_components(http);
517 /*********************************************************************
519 * Function : unknown_method
521 * Description : Checks whether a method is unknown.
524 * 1 : method = points to a http method
526 * Returns : TRUE if it's unknown, FALSE otherwise.
528 *********************************************************************/
529 static int unknown_method(const char *method)
531 static const char *known_http_methods[] = {
532 /* Basic HTTP request type */
533 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
534 /* webDAV extensions (RFC2518) */
535 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
537 * Microsoft webDAV extension for Exchange 2000. See:
538 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
539 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
541 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
543 * Another Microsoft webDAV extension for Exchange 2000. See:
544 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
545 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
546 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
548 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
550 * Yet another WebDAV extension, this time for
551 * Web Distributed Authoring and Versioning (RFC3253)
553 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
554 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
558 for (i = 0; i < SZ(known_http_methods); i++)
560 if (0 == strcmpic(method, known_http_methods[i]))
571 /*********************************************************************
573 * Function : parse_http_request
575 * Description : Parse out the host and port from the URL. Find the
576 * hostname & path, port (if ':'), and/or password (if '@')
579 * 1 : req = HTTP request line to break down
580 * 2 : http = pointer to the http structure to hold elements
581 * 3 : csp = Current client state (buffers, headers, etc...)
583 * Returns : JB_ERR_OK on success
584 * JB_ERR_MEMORY on out of memory
585 * JB_ERR_CGI_PARAMS on malformed command/URL
586 * or >100 domains deep.
588 *********************************************************************/
589 jb_err parse_http_request(const char *req,
590 struct http_request *http,
591 const struct client_state *csp)
594 char *v[10]; /* XXX: Why 10? We should only need three. */
599 memset(http, '\0', sizeof(*http));
604 return JB_ERR_MEMORY;
607 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
615 * Fail in case of unknown methods
616 * which we might not handle correctly.
618 * XXX: There should be a config option
619 * to forward requests with unknown methods
620 * anyway. Most of them don't need special
623 if (unknown_method(v[0]))
625 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
630 if (strcmpic(v[0], "CONNECT") == 0)
635 err = parse_http_url(v[1], http, csp);
643 * Copy the details into the structure
645 http->ssl = is_connect;
646 http->cmd = strdup(req);
647 http->gpc = strdup(v[0]);
648 http->ver = strdup(v[2]);
650 if ( (http->cmd == NULL)
651 || (http->gpc == NULL)
652 || (http->ver == NULL) )
655 return JB_ERR_MEMORY;
664 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
665 /*********************************************************************
667 * Function : compile_host_pattern
669 * Description : Parses and compiles a PCRE host pattern..
672 * 1 : url = Target url_spec to be filled in.
673 * 2 : host_pattern = Host pattern to compile.
675 * Returns : JB_ERR_OK - Success
676 * JB_ERR_MEMORY - Out of memory
677 * JB_ERR_PARSE - Cannot parse regex
679 *********************************************************************/
680 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
683 char rebuf[BUFFER_SIZE];
685 assert(host_pattern);
686 assert(strlen(host_pattern) < sizeof(rebuf) - 2);
688 url->host_regex = zalloc(sizeof(*url->host_regex));
689 if (NULL == url->host_regex)
692 return JB_ERR_MEMORY;
695 snprintf(rebuf, sizeof(rebuf), "%s$", host_pattern);
697 errcode = regcomp(url->host_regex, rebuf,
698 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
702 size_t errlen = regerror(errcode, url->host_regex, rebuf, sizeof(rebuf));
703 if (errlen > (sizeof(rebuf) - (size_t)1))
705 errlen = sizeof(rebuf) - (size_t)1;
707 rebuf[errlen] = '\0';
708 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
720 /*********************************************************************
722 * Function : compile_host_pattern
724 * Description : Parses and "compiles" an old-school host pattern.
727 * 1 : url = Target url_spec to be filled in.
728 * 2 : host_pattern = Host pattern to parse.
730 * Returns : JB_ERR_OK - Success
731 * JB_ERR_MEMORY - Out of memory
732 * JB_ERR_PARSE - Cannot parse regex
734 *********************************************************************/
735 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
744 if (host_pattern[strlen(host_pattern) - 1] == '.')
746 url->unanchored |= ANCHOR_RIGHT;
748 if (host_pattern[0] == '.')
750 url->unanchored |= ANCHOR_LEFT;
754 * Split domain into components
756 url->dbuffer = strdup(host_pattern);
757 if (NULL == url->dbuffer)
760 return JB_ERR_MEMORY;
766 for (p = url->dbuffer; *p ; p++)
768 *p = (char)tolower((int)(unsigned char)*p);
772 * Split the domain name into components
774 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
779 return JB_ERR_MEMORY;
781 else if (url->dcount != 0)
784 * Save a copy of the pointers in dvec
786 size = (size_t)url->dcount * sizeof(*url->dvec);
788 url->dvec = (char **)malloc(size);
789 if (NULL == url->dvec)
792 return JB_ERR_MEMORY;
795 memcpy(url->dvec, v, size);
798 * else dcount == 0 in which case we needn't do anything,
799 * since dvec will never be accessed and the pattern will
806 /*********************************************************************
808 * Function : simple_domaincmp
810 * Description : Domain-wise Compare fqdn's. The comparison is
811 * both left- and right-anchored. The individual
812 * domain names are compared with simplematch().
813 * This is only used by domain_match.
816 * 1 : pv = array of patterns to compare
817 * 2 : fv = array of domain components to compare
818 * 3 : len = length of the arrays (both arrays are the
819 * same length - if they weren't, it couldn't
820 * possibly be a match).
822 * Returns : 0 => domains are equivalent, else no match.
824 *********************************************************************/
825 static int simple_domaincmp(char **pv, char **fv, int len)
829 for (n = 0; n < len; n++)
831 if (simplematch(pv[n], fv[n]))
842 /*********************************************************************
844 * Function : domain_match
846 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
847 * pattern->unachored, the comparison is un-, left-,
848 * right-anchored, or both.
849 * The individual domain names are compared with
853 * 1 : pattern = a domain that may contain a '*' as a wildcard.
854 * 2 : fqdn = domain name against which the patterns are compared.
856 * Returns : 0 => domains are equivalent, else no match.
858 *********************************************************************/
859 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
861 char **pv, **fv; /* vectors */
863 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
865 plen = pattern->dcount;
870 /* fqdn is too short to match this pattern */
877 if (unanchored == ANCHOR_LEFT)
882 * Convert this into a fully anchored pattern with
883 * the fqdn and pattern the same length
885 fv += (flen - plen); /* flen - plen >= 0 due to check above */
886 return simple_domaincmp(pv, fv, plen);
888 else if (unanchored == 0)
890 /* Fully anchored, check length */
895 return simple_domaincmp(pv, fv, plen);
897 else if (unanchored == ANCHOR_RIGHT)
899 /* Left anchored, ignore all extra in fqdn */
900 return simple_domaincmp(pv, fv, plen);
906 int maxn = flen - plen;
907 for (n = 0; n <= maxn; n++)
909 if (!simple_domaincmp(pv, fv, plen))
914 * Doesn't match from start of fqdn
915 * Try skipping first part of fqdn
923 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
926 /*********************************************************************
928 * Function : create_url_spec
930 * Description : Creates a "url_spec" structure from a string.
931 * When finished, free with free_url_spec().
934 * 1 : url = Target url_spec to be filled in. Will be
936 * 2 : buf = Source pattern, null terminated. NOTE: The
937 * contents of this buffer are destroyed by this
938 * function. If this function succeeds, the
939 * buffer is copied to url->spec. If this
940 * function fails, the contents of the buffer
943 * Returns : JB_ERR_OK - Success
944 * JB_ERR_MEMORY - Out of memory
945 * JB_ERR_PARSE - Cannot parse regex (Detailed message
946 * written to system log)
948 *********************************************************************/
949 jb_err create_url_spec(struct url_spec * url, const char * buf)
954 char rebuf[BUFFER_SIZE];
962 memset(url, '\0', sizeof(*url));
965 * Save a copy of the orignal specification
967 if ((url->spec = strdup(buf)) == NULL)
969 return JB_ERR_MEMORY;
972 /* Is it tag pattern? */
973 if (0 == strncmpic("TAG:", url->spec, 4))
975 if (NULL == (url->tag_regex = zalloc(sizeof(*url->tag_regex))))
978 return JB_ERR_MEMORY;
981 /* buf + 4 to skip "TAG:" */
982 errcode = regcomp(url->tag_regex, buf + 4, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
985 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
986 if (errlen > (sizeof(rebuf) - 1))
988 errlen = sizeof(rebuf) - 1;
990 rebuf[errlen] = '\0';
991 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
999 /* Only reached for URL patterns */
1000 p = strchr(buf, '/');
1004 * Only compile the regex if it consists of more than
1005 * a single slash, otherwise it wouldn't affect the result.
1009 /* XXX: mostly duplicated code, should be factored out. */
1010 url->preg = zalloc(sizeof(*url->preg));
1011 if (NULL == url->preg)
1014 return JB_ERR_MEMORY;
1017 snprintf(rebuf, sizeof(rebuf), "^(%s)", p);
1018 errcode = regcomp(url->preg, rebuf,
1019 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
1022 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
1024 if (errlen > (sizeof(rebuf) - (size_t)1))
1026 errlen = sizeof(rebuf) - (size_t)1;
1028 rebuf[errlen] = '\0';
1029 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
1033 return JB_ERR_PARSE;
1039 p = strchr(buf, ':');
1043 url->port_list = strdup(p);
1044 if (NULL == url->port_list)
1046 return JB_ERR_MEMORY;
1051 url->port_list = NULL;
1056 return compile_host_pattern(url, buf);
1064 /*********************************************************************
1066 * Function : free_url_spec
1068 * Description : Called from the "unloaders". Freez the url
1069 * structure elements.
1072 * 1 : url = pointer to a url_spec structure.
1076 *********************************************************************/
1077 void free_url_spec(struct url_spec *url)
1079 if (url == NULL) return;
1082 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1083 if (url->host_regex)
1085 regfree(url->host_regex);
1086 freez(url->host_regex);
1089 freez(url->dbuffer);
1092 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1094 freez(url->port_list);
1102 regfree(url->tag_regex);
1103 freez(url->tag_regex);
1108 /*********************************************************************
1110 * Function : url_match
1112 * Description : Compare a URL against a URL pattern.
1115 * 1 : pattern = a URL pattern
1116 * 2 : url = URL to match
1118 * Returns : Nonzero if the URL matches the pattern, else 0.
1120 *********************************************************************/
1121 int url_match(const struct url_spec *pattern,
1122 const struct http_request *http)
1124 /* XXX: these should probably be functions. */
1125 #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, http->port))
1126 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1127 #define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
1129 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
1131 #define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
1133 if (pattern->tag_regex != NULL)
1135 /* It's a tag pattern and shouldn't be matched against URLs */
1139 return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
1144 /*********************************************************************
1146 * Function : match_portlist
1148 * Description : Check if a given number is covered by a comma
1149 * separated list of numbers and ranges (a,b-c,d,..)
1152 * 1 : portlist = String with list
1153 * 2 : port = port to check
1155 * Returns : 0 => no match
1158 *********************************************************************/
1159 int match_portlist(const char *portlist, int port)
1161 char *min, *max, *next, *portlist_copy;
1163 min = next = portlist_copy = strdup(portlist);
1166 * Zero-terminate first item and remember offset for next
1168 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1174 * Loop through all items, checking for match
1178 if (NULL == (max = strchr(min, (int) '-')))
1181 * No dash, check for equality
1183 if (port == atoi(min))
1185 free(portlist_copy);
1192 * This is a range, so check if between min and max,
1193 * or, if max was omitted, between min and 65K
1196 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1198 free(portlist_copy);
1210 * Zero-terminate next item and remember offset for n+1
1212 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1218 free(portlist_copy);