1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.28 2008/04/08 16:07:39 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.28 2008/04/08 16:07:39 fabiankeil
37 * Make it harder to mistake url_match()'s
38 * second parameter for an url_spec.
40 * Revision 1.27 2008/04/08 15:44:33 fabiankeil
41 * Save a bit of memory (and a few cpu cycles) by not bothering to
42 * compile slash-only path regexes that don't affect the result.
44 * Revision 1.26 2008/04/07 16:57:18 fabiankeil
45 * - Use free_url_spec() more consistently.
46 * - Let it reset url->dcount just in case.
48 * Revision 1.25 2008/04/06 15:18:38 fabiankeil
49 * Oh well, rename the --enable-pcre-host-patterns option to
50 * --enable-extended-host-patterns as it's not really PCRE syntax.
52 * Revision 1.24 2008/04/06 14:54:26 fabiankeil
53 * Use PCRE syntax in host patterns when configured
54 * with --enable-pcre-host-patterns.
56 * Revision 1.23 2008/04/05 12:19:20 fabiankeil
57 * Factor compile_host_pattern() out of create_url_spec().
59 * Revision 1.22 2008/03/30 15:02:32 fabiankeil
60 * SZitify unknown_method().
62 * Revision 1.21 2007/12/24 16:34:23 fabiankeil
63 * Band-aid (and micro-optimization) that makes it less likely to run out of
64 * stack space with overly-complex path patterns. Probably masks the problem
65 * reported by Lee in #1856679. Hohoho.
67 * Revision 1.20 2007/09/02 15:31:20 fabiankeil
68 * Move match_portlist() from filter.c to urlmatch.c.
69 * It's used for url matching, not for filtering.
71 * Revision 1.19 2007/09/02 13:42:11 fabiankeil
72 * - Allow port lists in url patterns.
73 * - Ditch unused url_spec member pathlen.
75 * Revision 1.18 2007/07/30 16:42:21 fabiankeil
76 * Move the method check into unknown_method()
77 * and loop through the known methods instead
78 * of using a screen-long OR chain.
80 * Revision 1.17 2007/04/15 16:39:21 fabiankeil
81 * Introduce tags as alternative way to specify which
82 * actions apply to a request. At the moment tags can be
83 * created based on client and server headers.
85 * Revision 1.16 2007/02/13 13:59:24 fabiankeil
86 * Remove redundant log message.
88 * Revision 1.15 2007/01/28 16:11:23 fabiankeil
89 * Accept WebDAV methods for subversion
90 * in parse_http_request(). Closes FR 1581425.
92 * Revision 1.14 2007/01/06 14:23:56 fabiankeil
93 * Fix gcc43 warnings. Mark *csp as immutable
94 * for parse_http_url() and url_match().
95 * Replace a sprintf call with snprintf.
97 * Revision 1.13 2006/12/06 19:50:54 fabiankeil
98 * parse_http_url() now handles intercepted
99 * HTTP request lines as well. Moved parts
100 * of parse_http_url()'s code into
101 * init_domain_components() so that it can
102 * be reused in chat().
104 * Revision 1.12 2006/07/18 14:48:47 david__schmidt
105 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
106 * with what was really the latest development (the v_3_0_branch branch)
108 * Revision 1.10.2.7 2003/05/17 15:57:24 oes
109 * - parse_http_url now checks memory allocation failure for
110 * duplication of "*" URL and rejects "*something" URLs
112 * - Added a comment to what might look like a bug in
113 * create_url_spec (see !bug #736931)
114 * - Comment cosmetics
116 * Revision 1.10.2.6 2003/05/07 12:39:48 oes
117 * Fix typo: Default port for https URLs is 443, not 143.
118 * Thanks to Scott Tregear for spotting this one.
120 * Revision 1.10.2.5 2003/02/28 13:09:29 oes
121 * Fixed a rare double free condition as per Bug #694713
123 * Revision 1.10.2.4 2003/02/28 12:57:44 oes
124 * Moved freeing of http request structure to its owner
125 * as per Dan Price's observations in Bug #694713
127 * Revision 1.10.2.3 2002/11/12 16:50:40 oes
128 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
130 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
131 * Added basic support for OPTIONS and TRACE HTTP methods:
132 * parse_http_url now recognizes the "*" URI as well as
133 * the OPTIONS and TRACE method keywords.
135 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
136 * Adding support for proprietary Microsoft WebDAV extensions
138 * Revision 1.10 2002/05/12 21:40:37 jongfoster
139 * - Removing some unused code
141 * Revision 1.9 2002/04/04 00:36:36 gliptak
142 * always use pcre for matching
144 * Revision 1.8 2002/04/03 23:32:47 jongfoster
145 * Fixing memory leak on error
147 * Revision 1.7 2002/03/26 22:29:55 swa
148 * we have a new homepage!
150 * Revision 1.6 2002/03/24 13:25:43 swa
151 * name change related issues
153 * Revision 1.5 2002/03/13 00:27:05 jongfoster
156 * Revision 1.4 2002/03/07 03:46:17 oes
157 * Fixed compiler warnings
159 * Revision 1.3 2002/03/03 14:51:11 oes
160 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
162 * Revision 1.2 2002/01/21 00:14:09 jongfoster
163 * Correcting comment style
164 * Fixing an uninitialized memory bug in create_url_spec()
166 * Revision 1.1 2002/01/17 20:53:46 jongfoster
167 * Moving all our URL and URL pattern parsing code to the same file - it
168 * was scattered around in filters.c, loaders.c and parsers.c.
170 * Providing a single, simple url_match(pattern,url) function - rather than
171 * the 3-line match routine which was repeated all over the place.
173 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
175 * Providing parse_http_url() so that URLs can be parsed without faking a
176 * HTTP request line for parse_http_request() or repeating the parsing
177 * code (both of which were techniques that were actually in use).
179 * Standardizing that struct http_request is used to represent a URL, and
180 * struct url_spec is used to represent a URL pattern. (Before, URLs were
181 * represented as seperate variables and a partially-filled-in url_spec).
184 *********************************************************************/
191 #include <sys/types.h>
199 #if !defined(_WIN32) && !defined(__OS2__)
204 #include "urlmatch.h"
206 #include "miscutil.h"
209 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
212 /*********************************************************************
214 * Function : free_http_request
216 * Description : Freez a http_request structure
219 * 1 : http = points to a http_request structure to free
223 *********************************************************************/
224 void free_http_request(struct http_request *http)
233 freez(http->hostport);
236 freez(http->host_ip_addr_str);
237 freez(http->dbuffer);
243 /*********************************************************************
245 * Function : init_domain_components
247 * Description : Splits the domain name so we can compare it
248 * against wildcards. It used to be part of
249 * parse_http_url, but was separated because the
250 * same code is required in chat in case of
251 * intercepted requests.
254 * 1 : http = pointer to the http structure to hold elements.
256 * Returns : JB_ERR_OK on success
257 * JB_ERR_MEMORY on out of memory
258 * JB_ERR_PARSE on malformed command/URL
259 * or >100 domains deep.
261 *********************************************************************/
262 jb_err init_domain_components(struct http_request *http)
264 char *vec[BUFFER_SIZE];
268 http->dbuffer = strdup(http->host);
269 if (NULL == http->dbuffer)
271 return JB_ERR_MEMORY;
274 /* map to lower case */
275 for (p = http->dbuffer; *p ; p++)
277 *p = (char)tolower((int)(unsigned char)*p);
280 /* split the domain name into components */
281 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
283 if (http->dcount <= 0)
286 * Error: More than SZ(vec) components in domain
287 * or: no components in domain
289 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
293 /* save a copy of the pointers in dvec */
294 size = (size_t)http->dcount * sizeof(*http->dvec);
296 http->dvec = (char **)malloc(size);
297 if (NULL == http->dvec)
299 return JB_ERR_MEMORY;
302 memcpy(http->dvec, vec, size);
308 /*********************************************************************
310 * Function : parse_http_url
312 * Description : Parse out the host and port from the URL. Find the
313 * hostname & path, port (if ':'), and/or password (if '@')
316 * 1 : url = URL (or is it URI?) to break down
317 * 2 : http = pointer to the http structure to hold elements.
318 * Will be zeroed before use. Note that this
319 * function sets the http->gpc and http->ver
321 * 3 : csp = Current client state (buffers, headers, etc...)
323 * Returns : JB_ERR_OK on success
324 * JB_ERR_MEMORY on out of memory
325 * JB_ERR_PARSE on malformed command/URL
326 * or >100 domains deep.
328 *********************************************************************/
329 jb_err parse_http_url(const char * url,
330 struct http_request *http,
331 const struct client_state *csp)
333 int host_available = 1; /* A proxy can dream. */
336 * Zero out the results structure
338 memset(http, '\0', sizeof(*http));
342 * Save our initial URL
344 http->url = strdup(url);
345 if (http->url == NULL)
347 return JB_ERR_MEMORY;
352 * Check for * URI. If found, we're done.
354 if (*http->url == '*')
356 if ( NULL == (http->path = strdup("*"))
357 || NULL == (http->hostport = strdup("")) )
359 return JB_ERR_MEMORY;
361 if (http->url[1] != '\0')
370 * Split URL into protocol,hostport,path.
380 return JB_ERR_MEMORY;
383 /* Find the start of the URL in our scratch space */
385 if (strncmpic(url_noproto, "http://", 7) == 0)
390 else if (strncmpic(url_noproto, "https://", 8) == 0)
395 else if (*url_noproto == '/')
398 * Short request line without protocol and host.
399 * Most likely because the client's request
400 * was intercepted and redirected into Privoxy.
411 url_path = strchr(url_noproto, '/');
412 if (url_path != NULL)
417 * NOTE: The following line ignores the path for HTTPS URLS.
418 * This means that you get consistent behaviour if you type a
419 * https URL in and it's parsed by the function. (When the
420 * URL is actually retrieved, SSL hides the path part).
422 http->path = strdup(http->ssl ? "/" : url_path);
424 http->hostport = strdup(url_noproto);
429 * Repair broken HTTP requests that don't contain a path,
430 * or CONNECT requests
432 http->path = strdup("/");
433 http->hostport = strdup(url_noproto);
438 if ( (http->path == NULL)
439 || (http->hostport == NULL))
441 return JB_ERR_MEMORY;
447 /* Without host, there is nothing left to do here */
452 * Split hostport into user/password (ignored), host, port.
459 buf = strdup(http->hostport);
462 return JB_ERR_MEMORY;
465 /* check if url contains username and/or password */
466 host = strchr(buf, '@');
469 /* Contains username/password, skip it and the @ sign. */
474 /* No username or password. */
478 /* check if url contains port */
479 port = strchr(host, ':');
483 /* Terminate hostname and point to start of port string */
485 http->port = atoi(port);
489 /* No port specified. */
490 http->port = (http->ssl ? 443 : 80);
493 http->host = strdup(host);
497 if (http->host == NULL)
499 return JB_ERR_MEMORY;
504 * Split domain name so we can compare it against wildcards
506 return init_domain_components(http);
511 /*********************************************************************
513 * Function : unknown_method
515 * Description : Checks whether a method is unknown.
518 * 1 : method = points to a http method
520 * Returns : TRUE if it's unknown, FALSE otherwise.
522 *********************************************************************/
523 static int unknown_method(const char *method)
525 static const char *known_http_methods[] = {
526 /* Basic HTTP request type */
527 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
528 /* webDAV extensions (RFC2518) */
529 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
531 * Microsoft webDAV extension for Exchange 2000. See:
532 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
533 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
535 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
537 * Another Microsoft webDAV extension for Exchange 2000. See:
538 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
539 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
540 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
542 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
544 * Yet another WebDAV extension, this time for
545 * Web Distributed Authoring and Versioning (RFC3253)
547 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
548 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
552 for (i = 0; i < SZ(known_http_methods); i++)
554 if (0 == strcmpic(method, known_http_methods[i]))
565 /*********************************************************************
567 * Function : parse_http_request
569 * Description : Parse out the host and port from the URL. Find the
570 * hostname & path, port (if ':'), and/or password (if '@')
573 * 1 : req = HTTP request line to break down
574 * 2 : http = pointer to the http structure to hold elements
575 * 3 : csp = Current client state (buffers, headers, etc...)
577 * Returns : JB_ERR_OK on success
578 * JB_ERR_MEMORY on out of memory
579 * JB_ERR_CGI_PARAMS on malformed command/URL
580 * or >100 domains deep.
582 *********************************************************************/
583 jb_err parse_http_request(const char *req,
584 struct http_request *http,
585 const struct client_state *csp)
588 char *v[10]; /* XXX: Why 10? We should only need three. */
593 memset(http, '\0', sizeof(*http));
598 return JB_ERR_MEMORY;
601 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
609 * Fail in case of unknown methods
610 * which we might not handle correctly.
612 * XXX: There should be a config option
613 * to forward requests with unknown methods
614 * anyway. Most of them don't need special
617 if (unknown_method(v[0]))
619 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
624 if (strcmpic(v[0], "CONNECT") == 0)
629 err = parse_http_url(v[1], http, csp);
637 * Copy the details into the structure
639 http->ssl = is_connect;
640 http->cmd = strdup(req);
641 http->gpc = strdup(v[0]);
642 http->ver = strdup(v[2]);
644 if ( (http->cmd == NULL)
645 || (http->gpc == NULL)
646 || (http->ver == NULL) )
649 return JB_ERR_MEMORY;
658 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
659 /*********************************************************************
661 * Function : compile_host_pattern
663 * Description : Parses and compiles a PCRE host pattern..
666 * 1 : url = Target url_spec to be filled in.
667 * 2 : host_pattern = Host pattern to compile.
669 * Returns : JB_ERR_OK - Success
670 * JB_ERR_MEMORY - Out of memory
671 * JB_ERR_PARSE - Cannot parse regex
673 *********************************************************************/
674 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
677 char rebuf[BUFFER_SIZE];
679 assert(host_pattern);
680 assert(strlen(host_pattern) < sizeof(rebuf) - 2);
682 url->host_regex = zalloc(sizeof(*url->host_regex));
683 if (NULL == url->host_regex)
686 return JB_ERR_MEMORY;
689 snprintf(rebuf, sizeof(rebuf), "%s$", host_pattern);
691 errcode = regcomp(url->host_regex, rebuf,
692 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
696 size_t errlen = regerror(errcode, url->host_regex, rebuf, sizeof(rebuf));
697 if (errlen > (sizeof(rebuf) - (size_t)1))
699 errlen = sizeof(rebuf) - (size_t)1;
701 rebuf[errlen] = '\0';
702 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
714 /*********************************************************************
716 * Function : compile_host_pattern
718 * Description : Parses and "compiles" an old-school host pattern.
721 * 1 : url = Target url_spec to be filled in.
722 * 2 : host_pattern = Host pattern to parse.
724 * Returns : JB_ERR_OK - Success
725 * JB_ERR_MEMORY - Out of memory
726 * JB_ERR_PARSE - Cannot parse regex
728 *********************************************************************/
729 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
738 if (host_pattern[strlen(host_pattern) - 1] == '.')
740 url->unanchored |= ANCHOR_RIGHT;
742 if (host_pattern[0] == '.')
744 url->unanchored |= ANCHOR_LEFT;
748 * Split domain into components
750 url->dbuffer = strdup(host_pattern);
751 if (NULL == url->dbuffer)
754 return JB_ERR_MEMORY;
760 for (p = url->dbuffer; *p ; p++)
762 *p = (char)tolower((int)(unsigned char)*p);
766 * Split the domain name into components
768 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
773 return JB_ERR_MEMORY;
775 else if (url->dcount != 0)
778 * Save a copy of the pointers in dvec
780 size = (size_t)url->dcount * sizeof(*url->dvec);
782 url->dvec = (char **)malloc(size);
783 if (NULL == url->dvec)
786 return JB_ERR_MEMORY;
789 memcpy(url->dvec, v, size);
792 * else dcount == 0 in which case we needn't do anything,
793 * since dvec will never be accessed and the pattern will
800 /*********************************************************************
802 * Function : simple_domaincmp
804 * Description : Domain-wise Compare fqdn's. The comparison is
805 * both left- and right-anchored. The individual
806 * domain names are compared with simplematch().
807 * This is only used by domain_match.
810 * 1 : pv = array of patterns to compare
811 * 2 : fv = array of domain components to compare
812 * 3 : len = length of the arrays (both arrays are the
813 * same length - if they weren't, it couldn't
814 * possibly be a match).
816 * Returns : 0 => domains are equivalent, else no match.
818 *********************************************************************/
819 static int simple_domaincmp(char **pv, char **fv, int len)
823 for (n = 0; n < len; n++)
825 if (simplematch(pv[n], fv[n]))
836 /*********************************************************************
838 * Function : domain_match
840 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
841 * pattern->unachored, the comparison is un-, left-,
842 * right-anchored, or both.
843 * The individual domain names are compared with
847 * 1 : pattern = a domain that may contain a '*' as a wildcard.
848 * 2 : fqdn = domain name against which the patterns are compared.
850 * Returns : 0 => domains are equivalent, else no match.
852 *********************************************************************/
853 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
855 char **pv, **fv; /* vectors */
857 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
859 plen = pattern->dcount;
864 /* fqdn is too short to match this pattern */
871 if (unanchored == ANCHOR_LEFT)
876 * Convert this into a fully anchored pattern with
877 * the fqdn and pattern the same length
879 fv += (flen - plen); /* flen - plen >= 0 due to check above */
880 return simple_domaincmp(pv, fv, plen);
882 else if (unanchored == 0)
884 /* Fully anchored, check length */
889 return simple_domaincmp(pv, fv, plen);
891 else if (unanchored == ANCHOR_RIGHT)
893 /* Left anchored, ignore all extra in fqdn */
894 return simple_domaincmp(pv, fv, plen);
900 int maxn = flen - plen;
901 for (n = 0; n <= maxn; n++)
903 if (!simple_domaincmp(pv, fv, plen))
908 * Doesn't match from start of fqdn
909 * Try skipping first part of fqdn
917 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
920 /*********************************************************************
922 * Function : create_url_spec
924 * Description : Creates a "url_spec" structure from a string.
925 * When finished, free with free_url_spec().
928 * 1 : url = Target url_spec to be filled in. Will be
930 * 2 : buf = Source pattern, null terminated. NOTE: The
931 * contents of this buffer are destroyed by this
932 * function. If this function succeeds, the
933 * buffer is copied to url->spec. If this
934 * function fails, the contents of the buffer
937 * Returns : JB_ERR_OK - Success
938 * JB_ERR_MEMORY - Out of memory
939 * JB_ERR_PARSE - Cannot parse regex (Detailed message
940 * written to system log)
942 *********************************************************************/
943 jb_err create_url_spec(struct url_spec * url, const char * buf)
948 char rebuf[BUFFER_SIZE];
956 memset(url, '\0', sizeof(*url));
959 * Save a copy of the orignal specification
961 if ((url->spec = strdup(buf)) == NULL)
963 return JB_ERR_MEMORY;
966 /* Is it tag pattern? */
967 if (0 == strncmpic("TAG:", url->spec, 4))
969 if (NULL == (url->tag_regex = zalloc(sizeof(*url->tag_regex))))
972 return JB_ERR_MEMORY;
975 /* buf + 4 to skip "TAG:" */
976 errcode = regcomp(url->tag_regex, buf + 4, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
979 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
980 if (errlen > (sizeof(rebuf) - 1))
982 errlen = sizeof(rebuf) - 1;
984 rebuf[errlen] = '\0';
985 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
993 /* Only reached for URL patterns */
994 p = strchr(buf, '/');
999 url->path = strdup(p);
1000 if (NULL == url->path)
1003 return JB_ERR_MEMORY;
1009 * The path pattern is a single slash and can
1010 * be ignored as it won't affect the result.
1012 assert(NULL == url->path);
1023 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
1026 return JB_ERR_MEMORY;
1029 snprintf(rebuf, sizeof(rebuf), "^(%s)", url->path);
1031 errcode = regcomp(url->preg, rebuf,
1032 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
1035 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
1037 if (errlen > (sizeof(rebuf) - (size_t)1))
1039 errlen = sizeof(rebuf) - (size_t)1;
1041 rebuf[errlen] = '\0';
1042 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
1046 return JB_ERR_PARSE;
1050 p = strchr(buf, ':');
1054 url->port_list = strdup(p);
1055 if (NULL == url->port_list)
1057 return JB_ERR_MEMORY;
1062 url->port_list = NULL;
1067 return compile_host_pattern(url, buf);
1075 /*********************************************************************
1077 * Function : free_url_spec
1079 * Description : Called from the "unloaders". Freez the url
1080 * structure elements.
1083 * 1 : url = pointer to a url_spec structure.
1087 *********************************************************************/
1088 void free_url_spec(struct url_spec *url)
1090 if (url == NULL) return;
1093 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1094 if (url->host_regex)
1096 regfree(url->host_regex);
1097 freez(url->host_regex);
1100 freez(url->dbuffer);
1103 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1105 freez(url->port_list);
1113 regfree(url->tag_regex);
1114 freez(url->tag_regex);
1119 /*********************************************************************
1121 * Function : url_match
1123 * Description : Compare a URL against a URL pattern.
1126 * 1 : pattern = a URL pattern
1127 * 2 : url = URL to match
1129 * Returns : Nonzero if the URL matches the pattern, else 0.
1131 *********************************************************************/
1132 int url_match(const struct url_spec *pattern,
1133 const struct http_request *http)
1135 /* XXX: these should probably be functions. */
1136 #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, http->port))
1137 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1138 #define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
1140 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
1142 #define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
1144 if (pattern->tag_regex != NULL)
1146 /* It's a tag pattern and shouldn't be matched against URLs */
1150 return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
1155 /*********************************************************************
1157 * Function : match_portlist
1159 * Description : Check if a given number is covered by a comma
1160 * separated list of numbers and ranges (a,b-c,d,..)
1163 * 1 : portlist = String with list
1164 * 2 : port = port to check
1166 * Returns : 0 => no match
1169 *********************************************************************/
1170 int match_portlist(const char *portlist, int port)
1172 char *min, *max, *next, *portlist_copy;
1174 min = next = portlist_copy = strdup(portlist);
1177 * Zero-terminate first item and remember offset for next
1179 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1185 * Loop through all items, checking for match
1189 if (NULL == (max = strchr(min, (int) '-')))
1192 * No dash, check for equality
1194 if (port == atoi(min))
1196 free(portlist_copy);
1203 * This is a range, so check if between min and max,
1204 * or, if max was omitted, between min and 65K
1207 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1209 free(portlist_copy);
1221 * Zero-terminate next item and remember offset for n+1
1223 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1229 free(portlist_copy);