1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.27 2008/04/08 15:44:33 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.27 2008/04/08 15:44:33 fabiankeil
37 * Save a bit of memory (and a few cpu cycles) by not bothering to
38 * compile slash-only path regexes that don't affect the result.
40 * Revision 1.26 2008/04/07 16:57:18 fabiankeil
41 * - Use free_url_spec() more consistently.
42 * - Let it reset url->dcount just in case.
44 * Revision 1.25 2008/04/06 15:18:38 fabiankeil
45 * Oh well, rename the --enable-pcre-host-patterns option to
46 * --enable-extended-host-patterns as it's not really PCRE syntax.
48 * Revision 1.24 2008/04/06 14:54:26 fabiankeil
49 * Use PCRE syntax in host patterns when configured
50 * with --enable-pcre-host-patterns.
52 * Revision 1.23 2008/04/05 12:19:20 fabiankeil
53 * Factor compile_host_pattern() out of create_url_spec().
55 * Revision 1.22 2008/03/30 15:02:32 fabiankeil
56 * SZitify unknown_method().
58 * Revision 1.21 2007/12/24 16:34:23 fabiankeil
59 * Band-aid (and micro-optimization) that makes it less likely to run out of
60 * stack space with overly-complex path patterns. Probably masks the problem
61 * reported by Lee in #1856679. Hohoho.
63 * Revision 1.20 2007/09/02 15:31:20 fabiankeil
64 * Move match_portlist() from filter.c to urlmatch.c.
65 * It's used for url matching, not for filtering.
67 * Revision 1.19 2007/09/02 13:42:11 fabiankeil
68 * - Allow port lists in url patterns.
69 * - Ditch unused url_spec member pathlen.
71 * Revision 1.18 2007/07/30 16:42:21 fabiankeil
72 * Move the method check into unknown_method()
73 * and loop through the known methods instead
74 * of using a screen-long OR chain.
76 * Revision 1.17 2007/04/15 16:39:21 fabiankeil
77 * Introduce tags as alternative way to specify which
78 * actions apply to a request. At the moment tags can be
79 * created based on client and server headers.
81 * Revision 1.16 2007/02/13 13:59:24 fabiankeil
82 * Remove redundant log message.
84 * Revision 1.15 2007/01/28 16:11:23 fabiankeil
85 * Accept WebDAV methods for subversion
86 * in parse_http_request(). Closes FR 1581425.
88 * Revision 1.14 2007/01/06 14:23:56 fabiankeil
89 * Fix gcc43 warnings. Mark *csp as immutable
90 * for parse_http_url() and url_match().
91 * Replace a sprintf call with snprintf.
93 * Revision 1.13 2006/12/06 19:50:54 fabiankeil
94 * parse_http_url() now handles intercepted
95 * HTTP request lines as well. Moved parts
96 * of parse_http_url()'s code into
97 * init_domain_components() so that it can
98 * be reused in chat().
100 * Revision 1.12 2006/07/18 14:48:47 david__schmidt
101 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
102 * with what was really the latest development (the v_3_0_branch branch)
104 * Revision 1.10.2.7 2003/05/17 15:57:24 oes
105 * - parse_http_url now checks memory allocation failure for
106 * duplication of "*" URL and rejects "*something" URLs
108 * - Added a comment to what might look like a bug in
109 * create_url_spec (see !bug #736931)
110 * - Comment cosmetics
112 * Revision 1.10.2.6 2003/05/07 12:39:48 oes
113 * Fix typo: Default port for https URLs is 443, not 143.
114 * Thanks to Scott Tregear for spotting this one.
116 * Revision 1.10.2.5 2003/02/28 13:09:29 oes
117 * Fixed a rare double free condition as per Bug #694713
119 * Revision 1.10.2.4 2003/02/28 12:57:44 oes
120 * Moved freeing of http request structure to its owner
121 * as per Dan Price's observations in Bug #694713
123 * Revision 1.10.2.3 2002/11/12 16:50:40 oes
124 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
126 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
127 * Added basic support for OPTIONS and TRACE HTTP methods:
128 * parse_http_url now recognizes the "*" URI as well as
129 * the OPTIONS and TRACE method keywords.
131 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
132 * Adding support for proprietary Microsoft WebDAV extensions
134 * Revision 1.10 2002/05/12 21:40:37 jongfoster
135 * - Removing some unused code
137 * Revision 1.9 2002/04/04 00:36:36 gliptak
138 * always use pcre for matching
140 * Revision 1.8 2002/04/03 23:32:47 jongfoster
141 * Fixing memory leak on error
143 * Revision 1.7 2002/03/26 22:29:55 swa
144 * we have a new homepage!
146 * Revision 1.6 2002/03/24 13:25:43 swa
147 * name change related issues
149 * Revision 1.5 2002/03/13 00:27:05 jongfoster
152 * Revision 1.4 2002/03/07 03:46:17 oes
153 * Fixed compiler warnings
155 * Revision 1.3 2002/03/03 14:51:11 oes
156 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
158 * Revision 1.2 2002/01/21 00:14:09 jongfoster
159 * Correcting comment style
160 * Fixing an uninitialized memory bug in create_url_spec()
162 * Revision 1.1 2002/01/17 20:53:46 jongfoster
163 * Moving all our URL and URL pattern parsing code to the same file - it
164 * was scattered around in filters.c, loaders.c and parsers.c.
166 * Providing a single, simple url_match(pattern,url) function - rather than
167 * the 3-line match routine which was repeated all over the place.
169 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
171 * Providing parse_http_url() so that URLs can be parsed without faking a
172 * HTTP request line for parse_http_request() or repeating the parsing
173 * code (both of which were techniques that were actually in use).
175 * Standardizing that struct http_request is used to represent a URL, and
176 * struct url_spec is used to represent a URL pattern. (Before, URLs were
177 * represented as seperate variables and a partially-filled-in url_spec).
180 *********************************************************************/
187 #include <sys/types.h>
195 #if !defined(_WIN32) && !defined(__OS2__)
200 #include "urlmatch.h"
202 #include "miscutil.h"
205 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
208 /*********************************************************************
210 * Function : free_http_request
212 * Description : Freez a http_request structure
215 * 1 : http = points to a http_request structure to free
219 *********************************************************************/
220 void free_http_request(struct http_request *http)
229 freez(http->hostport);
232 freez(http->host_ip_addr_str);
233 freez(http->dbuffer);
239 /*********************************************************************
241 * Function : init_domain_components
243 * Description : Splits the domain name so we can compare it
244 * against wildcards. It used to be part of
245 * parse_http_url, but was separated because the
246 * same code is required in chat in case of
247 * intercepted requests.
250 * 1 : http = pointer to the http structure to hold elements.
252 * Returns : JB_ERR_OK on success
253 * JB_ERR_MEMORY on out of memory
254 * JB_ERR_PARSE on malformed command/URL
255 * or >100 domains deep.
257 *********************************************************************/
258 jb_err init_domain_components(struct http_request *http)
260 char *vec[BUFFER_SIZE];
264 http->dbuffer = strdup(http->host);
265 if (NULL == http->dbuffer)
267 return JB_ERR_MEMORY;
270 /* map to lower case */
271 for (p = http->dbuffer; *p ; p++)
273 *p = (char)tolower((int)(unsigned char)*p);
276 /* split the domain name into components */
277 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
279 if (http->dcount <= 0)
282 * Error: More than SZ(vec) components in domain
283 * or: no components in domain
285 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
289 /* save a copy of the pointers in dvec */
290 size = (size_t)http->dcount * sizeof(*http->dvec);
292 http->dvec = (char **)malloc(size);
293 if (NULL == http->dvec)
295 return JB_ERR_MEMORY;
298 memcpy(http->dvec, vec, size);
304 /*********************************************************************
306 * Function : parse_http_url
308 * Description : Parse out the host and port from the URL. Find the
309 * hostname & path, port (if ':'), and/or password (if '@')
312 * 1 : url = URL (or is it URI?) to break down
313 * 2 : http = pointer to the http structure to hold elements.
314 * Will be zeroed before use. Note that this
315 * function sets the http->gpc and http->ver
317 * 3 : csp = Current client state (buffers, headers, etc...)
319 * Returns : JB_ERR_OK on success
320 * JB_ERR_MEMORY on out of memory
321 * JB_ERR_PARSE on malformed command/URL
322 * or >100 domains deep.
324 *********************************************************************/
325 jb_err parse_http_url(const char * url,
326 struct http_request *http,
327 const struct client_state *csp)
329 int host_available = 1; /* A proxy can dream. */
332 * Zero out the results structure
334 memset(http, '\0', sizeof(*http));
338 * Save our initial URL
340 http->url = strdup(url);
341 if (http->url == NULL)
343 return JB_ERR_MEMORY;
348 * Check for * URI. If found, we're done.
350 if (*http->url == '*')
352 if ( NULL == (http->path = strdup("*"))
353 || NULL == (http->hostport = strdup("")) )
355 return JB_ERR_MEMORY;
357 if (http->url[1] != '\0')
366 * Split URL into protocol,hostport,path.
376 return JB_ERR_MEMORY;
379 /* Find the start of the URL in our scratch space */
381 if (strncmpic(url_noproto, "http://", 7) == 0)
386 else if (strncmpic(url_noproto, "https://", 8) == 0)
391 else if (*url_noproto == '/')
394 * Short request line without protocol and host.
395 * Most likely because the client's request
396 * was intercepted and redirected into Privoxy.
407 url_path = strchr(url_noproto, '/');
408 if (url_path != NULL)
413 * NOTE: The following line ignores the path for HTTPS URLS.
414 * This means that you get consistent behaviour if you type a
415 * https URL in and it's parsed by the function. (When the
416 * URL is actually retrieved, SSL hides the path part).
418 http->path = strdup(http->ssl ? "/" : url_path);
420 http->hostport = strdup(url_noproto);
425 * Repair broken HTTP requests that don't contain a path,
426 * or CONNECT requests
428 http->path = strdup("/");
429 http->hostport = strdup(url_noproto);
434 if ( (http->path == NULL)
435 || (http->hostport == NULL))
437 return JB_ERR_MEMORY;
443 /* Without host, there is nothing left to do here */
448 * Split hostport into user/password (ignored), host, port.
455 buf = strdup(http->hostport);
458 return JB_ERR_MEMORY;
461 /* check if url contains username and/or password */
462 host = strchr(buf, '@');
465 /* Contains username/password, skip it and the @ sign. */
470 /* No username or password. */
474 /* check if url contains port */
475 port = strchr(host, ':');
479 /* Terminate hostname and point to start of port string */
481 http->port = atoi(port);
485 /* No port specified. */
486 http->port = (http->ssl ? 443 : 80);
489 http->host = strdup(host);
493 if (http->host == NULL)
495 return JB_ERR_MEMORY;
500 * Split domain name so we can compare it against wildcards
502 return init_domain_components(http);
507 /*********************************************************************
509 * Function : unknown_method
511 * Description : Checks whether a method is unknown.
514 * 1 : method = points to a http method
516 * Returns : TRUE if it's unknown, FALSE otherwise.
518 *********************************************************************/
519 static int unknown_method(const char *method)
521 static const char *known_http_methods[] = {
522 /* Basic HTTP request type */
523 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
524 /* webDAV extensions (RFC2518) */
525 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
527 * Microsoft webDAV extension for Exchange 2000. See:
528 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
529 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
531 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
533 * Another Microsoft webDAV extension for Exchange 2000. See:
534 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
535 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
536 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
538 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
540 * Yet another WebDAV extension, this time for
541 * Web Distributed Authoring and Versioning (RFC3253)
543 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
544 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
548 for (i = 0; i < SZ(known_http_methods); i++)
550 if (0 == strcmpic(method, known_http_methods[i]))
561 /*********************************************************************
563 * Function : parse_http_request
565 * Description : Parse out the host and port from the URL. Find the
566 * hostname & path, port (if ':'), and/or password (if '@')
569 * 1 : req = HTTP request line to break down
570 * 2 : http = pointer to the http structure to hold elements
571 * 3 : csp = Current client state (buffers, headers, etc...)
573 * Returns : JB_ERR_OK on success
574 * JB_ERR_MEMORY on out of memory
575 * JB_ERR_CGI_PARAMS on malformed command/URL
576 * or >100 domains deep.
578 *********************************************************************/
579 jb_err parse_http_request(const char *req,
580 struct http_request *http,
581 const struct client_state *csp)
584 char *v[10]; /* XXX: Why 10? We should only need three. */
589 memset(http, '\0', sizeof(*http));
594 return JB_ERR_MEMORY;
597 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
605 * Fail in case of unknown methods
606 * which we might not handle correctly.
608 * XXX: There should be a config option
609 * to forward requests with unknown methods
610 * anyway. Most of them don't need special
613 if (unknown_method(v[0]))
615 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
620 if (strcmpic(v[0], "CONNECT") == 0)
625 err = parse_http_url(v[1], http, csp);
633 * Copy the details into the structure
635 http->ssl = is_connect;
636 http->cmd = strdup(req);
637 http->gpc = strdup(v[0]);
638 http->ver = strdup(v[2]);
640 if ( (http->cmd == NULL)
641 || (http->gpc == NULL)
642 || (http->ver == NULL) )
645 return JB_ERR_MEMORY;
654 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
655 /*********************************************************************
657 * Function : compile_host_pattern
659 * Description : Parses and compiles a PCRE host pattern..
662 * 1 : url = Target url_spec to be filled in.
663 * 2 : host_pattern = Host pattern to compile.
665 * Returns : JB_ERR_OK - Success
666 * JB_ERR_MEMORY - Out of memory
667 * JB_ERR_PARSE - Cannot parse regex
669 *********************************************************************/
670 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
673 char rebuf[BUFFER_SIZE];
675 assert(host_pattern);
676 assert(strlen(host_pattern) < sizeof(rebuf) - 2);
678 url->host_regex = zalloc(sizeof(*url->host_regex));
679 if (NULL == url->host_regex)
682 return JB_ERR_MEMORY;
685 snprintf(rebuf, sizeof(rebuf), "%s$", host_pattern);
687 errcode = regcomp(url->host_regex, rebuf,
688 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
692 size_t errlen = regerror(errcode, url->host_regex, rebuf, sizeof(rebuf));
693 if (errlen > (sizeof(rebuf) - (size_t)1))
695 errlen = sizeof(rebuf) - (size_t)1;
697 rebuf[errlen] = '\0';
698 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
710 /*********************************************************************
712 * Function : compile_host_pattern
714 * Description : Parses and "compiles" an old-school host pattern.
717 * 1 : url = Target url_spec to be filled in.
718 * 2 : host_pattern = Host pattern to parse.
720 * Returns : JB_ERR_OK - Success
721 * JB_ERR_MEMORY - Out of memory
722 * JB_ERR_PARSE - Cannot parse regex
724 *********************************************************************/
725 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
734 if (host_pattern[strlen(host_pattern) - 1] == '.')
736 url->unanchored |= ANCHOR_RIGHT;
738 if (host_pattern[0] == '.')
740 url->unanchored |= ANCHOR_LEFT;
744 * Split domain into components
746 url->dbuffer = strdup(host_pattern);
747 if (NULL == url->dbuffer)
750 return JB_ERR_MEMORY;
756 for (p = url->dbuffer; *p ; p++)
758 *p = (char)tolower((int)(unsigned char)*p);
762 * Split the domain name into components
764 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
769 return JB_ERR_MEMORY;
771 else if (url->dcount != 0)
774 * Save a copy of the pointers in dvec
776 size = (size_t)url->dcount * sizeof(*url->dvec);
778 url->dvec = (char **)malloc(size);
779 if (NULL == url->dvec)
782 return JB_ERR_MEMORY;
785 memcpy(url->dvec, v, size);
788 * else dcount == 0 in which case we needn't do anything,
789 * since dvec will never be accessed and the pattern will
796 /*********************************************************************
798 * Function : simple_domaincmp
800 * Description : Domain-wise Compare fqdn's. The comparison is
801 * both left- and right-anchored. The individual
802 * domain names are compared with simplematch().
803 * This is only used by domain_match.
806 * 1 : pv = array of patterns to compare
807 * 2 : fv = array of domain components to compare
808 * 3 : len = length of the arrays (both arrays are the
809 * same length - if they weren't, it couldn't
810 * possibly be a match).
812 * Returns : 0 => domains are equivalent, else no match.
814 *********************************************************************/
815 static int simple_domaincmp(char **pv, char **fv, int len)
819 for (n = 0; n < len; n++)
821 if (simplematch(pv[n], fv[n]))
832 /*********************************************************************
834 * Function : domain_match
836 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
837 * pattern->unachored, the comparison is un-, left-,
838 * right-anchored, or both.
839 * The individual domain names are compared with
843 * 1 : pattern = a domain that may contain a '*' as a wildcard.
844 * 2 : fqdn = domain name against which the patterns are compared.
846 * Returns : 0 => domains are equivalent, else no match.
848 *********************************************************************/
849 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
851 char **pv, **fv; /* vectors */
853 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
855 plen = pattern->dcount;
860 /* fqdn is too short to match this pattern */
867 if (unanchored == ANCHOR_LEFT)
872 * Convert this into a fully anchored pattern with
873 * the fqdn and pattern the same length
875 fv += (flen - plen); /* flen - plen >= 0 due to check above */
876 return simple_domaincmp(pv, fv, plen);
878 else if (unanchored == 0)
880 /* Fully anchored, check length */
885 return simple_domaincmp(pv, fv, plen);
887 else if (unanchored == ANCHOR_RIGHT)
889 /* Left anchored, ignore all extra in fqdn */
890 return simple_domaincmp(pv, fv, plen);
896 int maxn = flen - plen;
897 for (n = 0; n <= maxn; n++)
899 if (!simple_domaincmp(pv, fv, plen))
904 * Doesn't match from start of fqdn
905 * Try skipping first part of fqdn
913 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
916 /*********************************************************************
918 * Function : create_url_spec
920 * Description : Creates a "url_spec" structure from a string.
921 * When finished, free with free_url_spec().
924 * 1 : url = Target url_spec to be filled in. Will be
926 * 2 : buf = Source pattern, null terminated. NOTE: The
927 * contents of this buffer are destroyed by this
928 * function. If this function succeeds, the
929 * buffer is copied to url->spec. If this
930 * function fails, the contents of the buffer
933 * Returns : JB_ERR_OK - Success
934 * JB_ERR_MEMORY - Out of memory
935 * JB_ERR_PARSE - Cannot parse regex (Detailed message
936 * written to system log)
938 *********************************************************************/
939 jb_err create_url_spec(struct url_spec * url, const char * buf)
944 char rebuf[BUFFER_SIZE];
952 memset(url, '\0', sizeof(*url));
955 * Save a copy of the orignal specification
957 if ((url->spec = strdup(buf)) == NULL)
959 return JB_ERR_MEMORY;
962 /* Is it tag pattern? */
963 if (0 == strncmpic("TAG:", url->spec, 4))
965 if (NULL == (url->tag_regex = zalloc(sizeof(*url->tag_regex))))
968 return JB_ERR_MEMORY;
971 /* buf + 4 to skip "TAG:" */
972 errcode = regcomp(url->tag_regex, buf + 4, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
975 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
976 if (errlen > (sizeof(rebuf) - 1))
978 errlen = sizeof(rebuf) - 1;
980 rebuf[errlen] = '\0';
981 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
989 /* Only reached for URL patterns */
990 p = strchr(buf, '/');
995 url->path = strdup(p);
996 if (NULL == url->path)
999 return JB_ERR_MEMORY;
1005 * The path pattern is a single slash and can
1006 * be ignored as it won't affect the result.
1008 assert(NULL == url->path);
1019 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
1022 return JB_ERR_MEMORY;
1025 snprintf(rebuf, sizeof(rebuf), "^(%s)", url->path);
1027 errcode = regcomp(url->preg, rebuf,
1028 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
1031 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
1033 if (errlen > (sizeof(rebuf) - (size_t)1))
1035 errlen = sizeof(rebuf) - (size_t)1;
1037 rebuf[errlen] = '\0';
1038 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
1042 return JB_ERR_PARSE;
1046 p = strchr(buf, ':');
1050 url->port_list = strdup(p);
1051 if (NULL == url->port_list)
1053 return JB_ERR_MEMORY;
1058 url->port_list = NULL;
1063 return compile_host_pattern(url, buf);
1071 /*********************************************************************
1073 * Function : free_url_spec
1075 * Description : Called from the "unloaders". Freez the url
1076 * structure elements.
1079 * 1 : url = pointer to a url_spec structure.
1083 *********************************************************************/
1084 void free_url_spec(struct url_spec *url)
1086 if (url == NULL) return;
1089 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1090 if (url->host_regex)
1092 regfree(url->host_regex);
1093 freez(url->host_regex);
1096 freez(url->dbuffer);
1099 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1101 freez(url->port_list);
1109 regfree(url->tag_regex);
1110 freez(url->tag_regex);
1115 /*********************************************************************
1117 * Function : url_match
1119 * Description : Compare a URL against a URL pattern.
1122 * 1 : pattern = a URL pattern
1123 * 2 : url = URL to match
1125 * Returns : Nonzero if the URL matches the pattern, else 0.
1127 *********************************************************************/
1128 int url_match(const struct url_spec *pattern,
1129 const struct http_request *http)
1131 /* XXX: these should probably be functions. */
1132 #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, http->port))
1133 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1134 #define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
1136 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
1138 #define PATH_MATCHES ((NULL == pattern->path) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
1140 if (pattern->tag_regex != NULL)
1142 /* It's a tag pattern and shouldn't be matched against URLs */
1146 return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
1151 /*********************************************************************
1153 * Function : match_portlist
1155 * Description : Check if a given number is covered by a comma
1156 * separated list of numbers and ranges (a,b-c,d,..)
1159 * 1 : portlist = String with list
1160 * 2 : port = port to check
1162 * Returns : 0 => no match
1165 *********************************************************************/
1166 int match_portlist(const char *portlist, int port)
1168 char *min, *max, *next, *portlist_copy;
1170 min = next = portlist_copy = strdup(portlist);
1173 * Zero-terminate first item and remember offset for next
1175 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1181 * Loop through all items, checking for match
1185 if (NULL == (max = strchr(min, (int) '-')))
1188 * No dash, check for equality
1190 if (port == atoi(min))
1192 free(portlist_copy);
1199 * This is a range, so check if between min and max,
1200 * or, if max was omitted, between min and 65K
1203 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1205 free(portlist_copy);
1217 * Zero-terminate next item and remember offset for n+1
1219 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1225 free(portlist_copy);