1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.59 2009/06/10 13:17:17 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-2009
10 * the 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.
34 *********************************************************************/
41 #include <sys/types.h>
49 #if !defined(_WIN32) && !defined(__OS2__)
59 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
68 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern);
70 /*********************************************************************
72 * Function : free_http_request
74 * Description : Freez a http_request structure
77 * 1 : http = points to a http_request structure to free
81 *********************************************************************/
82 void free_http_request(struct http_request *http)
91 freez(http->hostport);
94 freez(http->host_ip_addr_str);
95 #ifndef FEATURE_EXTENDED_HOST_PATTERNS
103 #ifndef FEATURE_EXTENDED_HOST_PATTERNS
104 /*********************************************************************
106 * Function : init_domain_components
108 * Description : Splits the domain name so we can compare it
109 * against wildcards. It used to be part of
110 * parse_http_url, but was separated because the
111 * same code is required in chat in case of
112 * intercepted requests.
115 * 1 : http = pointer to the http structure to hold elements.
117 * Returns : JB_ERR_OK on success
118 * JB_ERR_MEMORY on out of memory
119 * JB_ERR_PARSE on malformed command/URL
120 * or >100 domains deep.
122 *********************************************************************/
123 jb_err init_domain_components(struct http_request *http)
125 char *vec[BUFFER_SIZE];
129 http->dbuffer = strdup(http->host);
130 if (NULL == http->dbuffer)
132 return JB_ERR_MEMORY;
135 /* map to lower case */
136 for (p = http->dbuffer; *p ; p++)
138 *p = (char)tolower((int)(unsigned char)*p);
141 /* split the domain name into components */
142 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
144 if (http->dcount <= 0)
147 * Error: More than SZ(vec) components in domain
148 * or: no components in domain
150 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
154 /* save a copy of the pointers in dvec */
155 size = (size_t)http->dcount * sizeof(*http->dvec);
157 http->dvec = (char **)malloc(size);
158 if (NULL == http->dvec)
160 return JB_ERR_MEMORY;
163 memcpy(http->dvec, vec, size);
167 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
170 /*********************************************************************
172 * Function : parse_http_url
174 * Description : Parse out the host and port from the URL. Find the
175 * hostname & path, port (if ':'), and/or password (if '@')
178 * 1 : url = URL (or is it URI?) to break down
179 * 2 : http = pointer to the http structure to hold elements.
180 * Must be initialized with valid values (like NULLs).
181 * 3 : require_protocol = Whether or not URLs without
182 * protocol are acceptable.
184 * Returns : JB_ERR_OK on success
185 * JB_ERR_MEMORY on out of memory
186 * JB_ERR_PARSE on malformed command/URL
187 * or >100 domains deep.
189 *********************************************************************/
190 jb_err parse_http_url(const char *url, struct http_request *http, int require_protocol)
192 int host_available = 1; /* A proxy can dream. */
195 * Save our initial URL
197 http->url = strdup(url);
198 if (http->url == NULL)
200 return JB_ERR_MEMORY;
205 * Check for * URI. If found, we're done.
207 if (*http->url == '*')
209 if ( NULL == (http->path = strdup("*"))
210 || NULL == (http->hostport = strdup("")) )
212 return JB_ERR_MEMORY;
214 if (http->url[1] != '\0')
223 * Split URL into protocol,hostport,path.
233 return JB_ERR_MEMORY;
236 /* Find the start of the URL in our scratch space */
238 if (strncmpic(url_noproto, "http://", 7) == 0)
242 else if (strncmpic(url_noproto, "https://", 8) == 0)
245 * Should only happen when called from cgi_show_url_info().
250 else if (*url_noproto == '/')
253 * Short request line without protocol and host.
254 * Most likely because the client's request
255 * was intercepted and redirected into Privoxy.
260 else if (require_protocol)
266 url_path = strchr(url_noproto, '/');
267 if (url_path != NULL)
272 * NOTE: The following line ignores the path for HTTPS URLS.
273 * This means that you get consistent behaviour if you type a
274 * https URL in and it's parsed by the function. (When the
275 * URL is actually retrieved, SSL hides the path part).
277 http->path = strdup(http->ssl ? "/" : url_path);
279 http->hostport = strdup(url_noproto);
284 * Repair broken HTTP requests that don't contain a path,
285 * or CONNECT requests
287 http->path = strdup("/");
288 http->hostport = strdup(url_noproto);
293 if ( (http->path == NULL)
294 || (http->hostport == NULL))
296 return JB_ERR_MEMORY;
302 /* Without host, there is nothing left to do here */
307 * Split hostport into user/password (ignored), host, port.
314 buf = strdup(http->hostport);
317 return JB_ERR_MEMORY;
320 /* check if url contains username and/or password */
321 host = strchr(buf, '@');
324 /* Contains username/password, skip it and the @ sign. */
329 /* No username or password. */
333 /* Move after hostname before port number */
336 /* Numeric IPv6 address delimited by brackets */
338 port = strchr(host, ']');
342 /* Missing closing bracket */
353 else if (*port != ':')
355 /* Garbage after closing bracket */
362 /* Plain non-escaped hostname */
363 port = strchr(host, ':');
366 /* check if url contains port */
370 /* Terminate hostname and point to start of port string */
372 http->port = atoi(port);
376 /* No port specified. */
377 http->port = (http->ssl ? 443 : 80);
380 http->host = strdup(host);
384 if (http->host == NULL)
386 return JB_ERR_MEMORY;
390 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
393 /* Split domain name so we can compare it against wildcards */
394 return init_domain_components(http);
395 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
400 /*********************************************************************
402 * Function : unknown_method
404 * Description : Checks whether a method is unknown.
407 * 1 : method = points to a http method
409 * Returns : TRUE if it's unknown, FALSE otherwise.
411 *********************************************************************/
412 static int unknown_method(const char *method)
414 static const char *known_http_methods[] = {
415 /* Basic HTTP request type */
416 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
417 /* webDAV extensions (RFC2518) */
418 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
420 * Microsoft webDAV extension for Exchange 2000. See:
421 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
422 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
424 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
426 * Another Microsoft webDAV extension for Exchange 2000. See:
427 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
428 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
429 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
431 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
433 * Yet another WebDAV extension, this time for
434 * Web Distributed Authoring and Versioning (RFC3253)
436 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
437 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
441 for (i = 0; i < SZ(known_http_methods); i++)
443 if (0 == strcmpic(method, known_http_methods[i]))
454 /*********************************************************************
456 * Function : parse_http_request
458 * Description : Parse out the host and port from the URL. Find the
459 * hostname & path, port (if ':'), and/or password (if '@')
462 * 1 : req = HTTP request line to break down
463 * 2 : http = pointer to the http structure to hold elements
465 * Returns : JB_ERR_OK on success
466 * JB_ERR_MEMORY on out of memory
467 * JB_ERR_CGI_PARAMS on malformed command/URL
468 * or >100 domains deep.
470 *********************************************************************/
471 jb_err parse_http_request(const char *req, struct http_request *http)
474 char *v[10]; /* XXX: Why 10? We should only need three. */
478 memset(http, '\0', sizeof(*http));
483 return JB_ERR_MEMORY;
486 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
494 * Fail in case of unknown methods
495 * which we might not handle correctly.
497 * XXX: There should be a config option
498 * to forward requests with unknown methods
499 * anyway. Most of them don't need special
502 if (unknown_method(v[0]))
504 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
509 if (strcmpic(v[2], "HTTP/1.1") && strcmpic(v[2], "HTTP/1.0"))
511 log_error(LOG_LEVEL_ERROR, "The only supported HTTP "
512 "versions are 1.0 and 1.1. This rules out: %s", v[2]);
517 http->ssl = !strcmpic(v[0], "CONNECT");
519 err = parse_http_url(v[1], http, !http->ssl);
527 * Copy the details into the structure
529 http->cmd = strdup(req);
530 http->gpc = strdup(v[0]);
531 http->ver = strdup(v[2]);
535 if ( (http->cmd == NULL)
536 || (http->gpc == NULL)
537 || (http->ver == NULL) )
539 return JB_ERR_MEMORY;
547 /*********************************************************************
549 * Function : compile_pattern
551 * Description : Compiles a host, domain or TAG pattern.
554 * 1 : pattern = The pattern to compile.
555 * 2 : anchoring = How the regex should be modified
556 * before compilation. Can be either
557 * one of NO_ANCHORING, LEFT_ANCHORED,
558 * RIGHT_ANCHORED or RIGHT_ANCHORED_HOST.
559 * 3 : url = In case of failures, the spec member is
560 * logged and the structure freed.
561 * 4 : regex = Where the compiled regex should be stored.
563 * Returns : JB_ERR_OK - Success
564 * JB_ERR_MEMORY - Out of memory
565 * JB_ERR_PARSE - Cannot parse regex
567 *********************************************************************/
568 static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchoring,
569 struct url_spec *url, regex_t **regex)
572 char rebuf[BUFFER_SIZE];
573 const char *fmt = NULL;
576 assert(strlen(pattern) < sizeof(rebuf) - 2);
578 if (pattern[0] == '\0')
592 case RIGHT_ANCHORED_HOST:
599 log_error(LOG_LEVEL_FATAL,
600 "Invalid anchoring in compile_pattern %d", anchoring);
603 *regex = zalloc(sizeof(**regex));
607 return JB_ERR_MEMORY;
610 snprintf(rebuf, sizeof(rebuf), fmt, pattern);
612 errcode = regcomp(*regex, rebuf, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
616 size_t errlen = regerror(errcode, *regex, rebuf, sizeof(rebuf));
617 if (errlen > (sizeof(rebuf) - (size_t)1))
619 errlen = sizeof(rebuf) - (size_t)1;
621 rebuf[errlen] = '\0';
622 log_error(LOG_LEVEL_ERROR, "error compiling %s from %s: %s",
623 pattern, url->spec, rebuf);
634 /*********************************************************************
636 * Function : compile_url_pattern
638 * Description : Compiles the three parts of an URL pattern.
641 * 1 : url = Target url_spec to be filled in.
642 * 2 : buf = The url pattern to compile. Will be messed up.
644 * Returns : JB_ERR_OK - Success
645 * JB_ERR_MEMORY - Out of memory
646 * JB_ERR_PARSE - Cannot parse regex
648 *********************************************************************/
649 static jb_err compile_url_pattern(struct url_spec *url, char *buf)
653 p = strchr(buf, '/');
657 * Only compile the regex if it consists of more than
658 * a single slash, otherwise it wouldn't affect the result.
663 * XXX: does it make sense to compile the slash at the beginning?
665 jb_err err = compile_pattern(p, LEFT_ANCHORED, url, &url->preg);
667 if (JB_ERR_OK != err)
676 * IPv6 numeric hostnames can contain colons, thus we need
677 * to delimit the hostname before the real port separator.
678 * As brackets are already used in the hostname pattern,
679 * we use angle brackets ('<', '>') instead.
681 if ((buf[0] == '<') && (NULL != (p = strchr(buf + 1, '>'))))
688 /* IPv6 address without port number */
693 /* Garbage after address delimiter */
699 p = strchr(buf, ':');
705 url->port_list = strdup(p);
706 if (NULL == url->port_list)
708 return JB_ERR_MEMORY;
713 url->port_list = NULL;
718 return compile_host_pattern(url, buf);
726 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
727 /*********************************************************************
729 * Function : compile_host_pattern
731 * Description : Parses and compiles a host pattern..
734 * 1 : url = Target url_spec to be filled in.
735 * 2 : host_pattern = Host pattern to compile.
737 * Returns : JB_ERR_OK - Success
738 * JB_ERR_MEMORY - Out of memory
739 * JB_ERR_PARSE - Cannot parse regex
741 *********************************************************************/
742 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
744 return compile_pattern(host_pattern, RIGHT_ANCHORED_HOST, url, &url->host_regex);
749 /*********************************************************************
751 * Function : compile_host_pattern
753 * Description : Parses and "compiles" an old-school host pattern.
756 * 1 : url = Target url_spec to be filled in.
757 * 2 : host_pattern = Host pattern to parse.
759 * Returns : JB_ERR_OK - Success
760 * JB_ERR_MEMORY - Out of memory
761 * JB_ERR_PARSE - Cannot parse regex
763 *********************************************************************/
764 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
773 if (host_pattern[strlen(host_pattern) - 1] == '.')
775 url->unanchored |= ANCHOR_RIGHT;
777 if (host_pattern[0] == '.')
779 url->unanchored |= ANCHOR_LEFT;
783 * Split domain into components
785 url->dbuffer = strdup(host_pattern);
786 if (NULL == url->dbuffer)
789 return JB_ERR_MEMORY;
795 for (p = url->dbuffer; *p ; p++)
797 *p = (char)tolower((int)(unsigned char)*p);
801 * Split the domain name into components
803 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
808 return JB_ERR_MEMORY;
810 else if (url->dcount != 0)
813 * Save a copy of the pointers in dvec
815 size = (size_t)url->dcount * sizeof(*url->dvec);
817 url->dvec = (char **)malloc(size);
818 if (NULL == url->dvec)
821 return JB_ERR_MEMORY;
824 memcpy(url->dvec, v, size);
827 * else dcount == 0 in which case we needn't do anything,
828 * since dvec will never be accessed and the pattern will
835 /*********************************************************************
837 * Function : simplematch
839 * Description : String matching, with a (greedy) '*' wildcard that
840 * stands for zero or more arbitrary characters and
841 * character classes in [], which take both enumerations
845 * 1 : pattern = pattern for matching
846 * 2 : text = text to be matched
848 * Returns : 0 if match, else nonzero
850 *********************************************************************/
851 static int simplematch(const char *pattern, const char *text)
853 const unsigned char *pat = (const unsigned char *)pattern;
854 const unsigned char *txt = (const unsigned char *)text;
855 const unsigned char *fallback = pat;
858 unsigned char lastchar = 'a';
860 unsigned char charmap[32];
865 /* EOF pattern but !EOF text? */
878 /* '*' in the pattern? */
882 /* The pattern ends afterwards? Speed up the return. */
888 /* Else, set wildcard mode and remember position after '*' */
893 /* Character range specification? */
896 memset(charmap, '\0', sizeof(charmap));
898 while (*++pat != ']')
904 else if (*pat == '-')
906 if ((*++pat == ']') || *pat == '\0')
910 for (i = lastchar; i <= *pat; i++)
912 charmap[i / 8] |= (unsigned char)(1 << (i % 8));
917 charmap[*pat / 8] |= (unsigned char)(1 << (*pat % 8));
921 } /* -END- if Character range specification */
925 * Char match, or char range match?
929 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
939 * No match && no wildcard: No luck
943 else if (pat != fallback)
946 * Increment text pointer if in char range matching
953 * Wildcard mode && nonmatch beyond fallback: Rewind pattern
957 * Restart matching from current text pointer
964 /* Cut off extra '*'s */
965 if(*pat == '*') pat++;
967 /* If this is the pattern's end, fine! */
973 /*********************************************************************
975 * Function : simple_domaincmp
977 * Description : Domain-wise Compare fqdn's. The comparison is
978 * both left- and right-anchored. The individual
979 * domain names are compared with simplematch().
980 * This is only used by domain_match.
983 * 1 : pv = array of patterns to compare
984 * 2 : fv = array of domain components to compare
985 * 3 : len = length of the arrays (both arrays are the
986 * same length - if they weren't, it couldn't
987 * possibly be a match).
989 * Returns : 0 => domains are equivalent, else no match.
991 *********************************************************************/
992 static int simple_domaincmp(char **pv, char **fv, int len)
996 for (n = 0; n < len; n++)
998 if (simplematch(pv[n], fv[n]))
1009 /*********************************************************************
1011 * Function : domain_match
1013 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1014 * pattern->unachored, the comparison is un-, left-,
1015 * right-anchored, or both.
1016 * The individual domain names are compared with
1020 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1021 * 2 : fqdn = domain name against which the patterns are compared.
1023 * Returns : 0 => domains are equivalent, else no match.
1025 *********************************************************************/
1026 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
1028 char **pv, **fv; /* vectors */
1030 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1032 plen = pattern->dcount;
1033 flen = fqdn->dcount;
1037 /* fqdn is too short to match this pattern */
1044 if (unanchored == ANCHOR_LEFT)
1049 * Convert this into a fully anchored pattern with
1050 * the fqdn and pattern the same length
1052 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1053 return simple_domaincmp(pv, fv, plen);
1055 else if (unanchored == 0)
1057 /* Fully anchored, check length */
1062 return simple_domaincmp(pv, fv, plen);
1064 else if (unanchored == ANCHOR_RIGHT)
1066 /* Left anchored, ignore all extra in fqdn */
1067 return simple_domaincmp(pv, fv, plen);
1073 int maxn = flen - plen;
1074 for (n = 0; n <= maxn; n++)
1076 if (!simple_domaincmp(pv, fv, plen))
1081 * Doesn't match from start of fqdn
1082 * Try skipping first part of fqdn
1090 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
1093 /*********************************************************************
1095 * Function : create_url_spec
1097 * Description : Creates a "url_spec" structure from a string.
1098 * When finished, free with free_url_spec().
1101 * 1 : url = Target url_spec to be filled in. Will be
1102 * zeroed before use.
1103 * 2 : buf = Source pattern, null terminated. NOTE: The
1104 * contents of this buffer are destroyed by this
1105 * function. If this function succeeds, the
1106 * buffer is copied to url->spec. If this
1107 * function fails, the contents of the buffer
1110 * Returns : JB_ERR_OK - Success
1111 * JB_ERR_MEMORY - Out of memory
1112 * JB_ERR_PARSE - Cannot parse regex (Detailed message
1113 * written to system log)
1115 *********************************************************************/
1116 jb_err create_url_spec(struct url_spec *url, char *buf)
1121 memset(url, '\0', sizeof(*url));
1123 /* Remember the original specification for the CGI pages. */
1124 url->spec = strdup(buf);
1125 if (NULL == url->spec)
1127 return JB_ERR_MEMORY;
1130 /* Is it a tag pattern? */
1131 if (0 == strncmpic("TAG:", url->spec, 4))
1133 /* The pattern starts with the first character after "TAG:" */
1134 const char *tag_pattern = buf + 4;
1135 return compile_pattern(tag_pattern, NO_ANCHORING, url, &url->tag_regex);
1138 /* If it isn't a tag pattern it must be an URL pattern. */
1139 return compile_url_pattern(url, buf);
1143 /*********************************************************************
1145 * Function : free_url_spec
1147 * Description : Called from the "unloaders". Freez the url
1148 * structure elements.
1151 * 1 : url = pointer to a url_spec structure.
1155 *********************************************************************/
1156 void free_url_spec(struct url_spec *url)
1158 if (url == NULL) return;
1161 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1162 if (url->host_regex)
1164 regfree(url->host_regex);
1165 freez(url->host_regex);
1168 freez(url->dbuffer);
1171 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1172 freez(url->port_list);
1180 regfree(url->tag_regex);
1181 freez(url->tag_regex);
1186 /*********************************************************************
1188 * Function : port_matches
1190 * Description : Compares a port against a port list.
1193 * 1 : port = The port to check.
1194 * 2 : port_list = The list of port to compare with.
1196 * Returns : TRUE for yes, FALSE otherwise.
1198 *********************************************************************/
1199 static int port_matches(const int port, const char *port_list)
1201 return ((NULL == port_list) || match_portlist(port_list, port));
1205 /*********************************************************************
1207 * Function : host_matches
1209 * Description : Compares a host against a host pattern.
1212 * 1 : url = The URL to match
1213 * 2 : pattern = The URL pattern
1215 * Returns : TRUE for yes, FALSE otherwise.
1217 *********************************************************************/
1218 static int host_matches(const struct http_request *http,
1219 const struct url_spec *pattern)
1221 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1222 return ((NULL == pattern->host_regex)
1223 || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)));
1225 return ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)));
1230 /*********************************************************************
1232 * Function : path_matches
1234 * Description : Compares a path against a path pattern.
1237 * 1 : path = The path to match
1238 * 2 : pattern = The URL pattern
1240 * Returns : TRUE for yes, FALSE otherwise.
1242 *********************************************************************/
1243 static int path_matches(const char *path, const struct url_spec *pattern)
1245 return ((NULL == pattern->preg)
1246 || (0 == regexec(pattern->preg, path, 0, NULL, 0)));
1250 /*********************************************************************
1252 * Function : url_match
1254 * Description : Compare a URL against a URL pattern.
1257 * 1 : pattern = a URL pattern
1258 * 2 : url = URL to match
1260 * Returns : Nonzero if the URL matches the pattern, else 0.
1262 *********************************************************************/
1263 int url_match(const struct url_spec *pattern,
1264 const struct http_request *http)
1266 if (pattern->tag_regex != NULL)
1268 /* It's a tag pattern and shouldn't be matched against URLs */
1272 return (port_matches(http->port, pattern->port_list)
1273 && host_matches(http, pattern) && path_matches(http->path, pattern));
1278 /*********************************************************************
1280 * Function : match_portlist
1282 * Description : Check if a given number is covered by a comma
1283 * separated list of numbers and ranges (a,b-c,d,..)
1286 * 1 : portlist = String with list
1287 * 2 : port = port to check
1289 * Returns : 0 => no match
1292 *********************************************************************/
1293 int match_portlist(const char *portlist, int port)
1295 char *min, *max, *next, *portlist_copy;
1297 min = portlist_copy = strdup(portlist);
1300 * Zero-terminate first item and remember offset for next
1302 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1308 * Loop through all items, checking for match
1312 if (NULL == (max = strchr(min, (int) '-')))
1315 * No dash, check for equality
1317 if (port == atoi(min))
1319 freez(portlist_copy);
1326 * This is a range, so check if between min and max,
1327 * or, if max was omitted, between min and 65K
1330 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1332 freez(portlist_copy);
1344 * Zero-terminate next item and remember offset for n+1
1346 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1352 freez(portlist_copy);
1358 /*********************************************************************
1360 * Function : parse_forwarder_address
1362 * Description : Parse out the host and port from a forwarder address.
1365 * 1 : address = The forwarder address to parse.
1366 * 2 : hostname = Used to return the hostname. NULL on error.
1367 * 3 : port = Used to return the port. Untouched if no port
1370 * Returns : JB_ERR_OK on success
1371 * JB_ERR_MEMORY on out of memory
1372 * JB_ERR_PARSE on malformed address.
1374 *********************************************************************/
1375 jb_err parse_forwarder_address(char *address, char **hostname, int *port)
1379 if ((*address == '[') && (NULL == strchr(address, ']')))
1381 /* XXX: Should do some more validity checks here. */
1382 return JB_ERR_PARSE;
1385 *hostname = strdup(address);
1386 if (NULL == *hostname)
1388 return JB_ERR_MEMORY;
1391 if ((**hostname == '[') && (NULL != (p = strchr(*hostname, ']'))))
1394 memmove(*hostname, (*hostname + 1), (size_t)(p - *hostname));
1397 *port = (int)strtol(++p, NULL, 0);
1400 else if (NULL != (p = strchr(*hostname, ':')))
1403 *port = (int)strtol(p, NULL, 0);