1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.6 2002/03/24 13:25:43 swa 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 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.6 2002/03/24 13:25:43 swa
37 * name change related issues
39 * Revision 1.5 2002/03/13 00:27:05 jongfoster
42 * Revision 1.4 2002/03/07 03:46:17 oes
43 * Fixed compiler warnings
45 * Revision 1.3 2002/03/03 14:51:11 oes
46 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
48 * Revision 1.2 2002/01/21 00:14:09 jongfoster
49 * Correcting comment style
50 * Fixing an uninitialized memory bug in create_url_spec()
52 * Revision 1.1 2002/01/17 20:53:46 jongfoster
53 * Moving all our URL and URL pattern parsing code to the same file - it
54 * was scattered around in filters.c, loaders.c and parsers.c.
56 * Providing a single, simple url_match(pattern,url) function - rather than
57 * the 3-line match routine which was repeated all over the place.
59 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
61 * Providing parse_http_url() so that URLs can be parsed without faking a
62 * HTTP request line for parse_http_request() or repeating the parsing
63 * code (both of which were techniques that were actually in use).
65 * Standardizing that struct http_request is used to represent a URL, and
66 * struct url_spec is used to represent a URL pattern. (Before, URLs were
67 * represented as seperate variables and a partially-filled-in url_spec).
70 *********************************************************************/
77 #include <sys/types.h>
85 #if !defined(_WIN32) && !defined(__OS2__)
95 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
97 /* Fix a problem with Solaris. There should be no effect on other
99 * Solaris's isspace() is a macro which uses it's argument directly
100 * as an array index. Therefore we need to make sure that high-bit
101 * characters generate +ve values, and ideally we also want to make
102 * the argument match the declared parameter type of "int".
104 * Why did they write a character function that can't take a simple
105 * "char" argument? Doh!
107 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
108 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
111 /*********************************************************************
113 * Function : free_http_request
115 * Description : Freez a http_request structure
118 * 1 : http = points to a http_request structure to free
122 *********************************************************************/
123 void free_http_request(struct http_request *http)
132 freez(http->hostport);
135 freez(http->host_ip_addr_str);
136 freez(http->dbuffer);
142 /*********************************************************************
144 * Function : parse_http_url
146 * Description : Parse out the host and port from the URL. Find the
147 * hostname & path, port (if ':'), and/or password (if '@')
150 * 1 : url = URL (or is it URI?) to break down
151 * 2 : http = pointer to the http structure to hold elements.
152 * Will be zeroed before use. Note that this
153 * function sets the http->gpc and http->ver
155 * 3 : csp = Current client state (buffers, headers, etc...)
157 * Returns : JB_ERR_OK on success
158 * JB_ERR_MEMORY on out of memory
159 * JB_ERR_CGI_PARAMS on malformed command/URL
160 * or >100 domains deep.
162 *********************************************************************/
163 jb_err parse_http_url(const char * url,
164 struct http_request *http,
165 struct client_state *csp)
168 * Zero out the results structure
170 memset(http, '\0', sizeof(*http));
174 * Save our initial URL
176 http->url = strdup(url);
177 if (http->url == NULL)
179 return JB_ERR_MEMORY;
184 * Split URL into protocol,hostport,path.
194 return JB_ERR_MEMORY;
197 /* Find the start of the URL in our scratch space */
199 if (strncmpic(url_noproto, "http://", 7) == 0)
204 else if (strncmpic(url_noproto, "https://", 8) == 0)
214 url_path = strchr(url_noproto, '/');
215 if (url_path != NULL)
220 * NOTE: The following line ignores the path for HTTPS URLS.
221 * This means that you get consistent behaviour if you type a
222 * https URL in and it's parsed by the function. (When the
223 * URL is actually retrieved, SSL hides the path part).
225 http->path = strdup(http->ssl ? "/" : url_path);
227 http->hostport = strdup(url_noproto);
232 * Repair broken HTTP requests that don't contain a path,
233 * or CONNECT requests
235 http->path = strdup("/");
236 http->hostport = strdup(url_noproto);
241 if ( (http->path == NULL)
242 || (http->hostport == NULL))
245 free_http_request(http);
246 return JB_ERR_MEMORY;
252 * Split hostport into user/password (ignored), host, port.
259 buf = strdup(http->hostport);
262 free_http_request(http);
263 return JB_ERR_MEMORY;
266 /* check if url contains username and/or password */
267 host = strchr(buf, '@');
270 /* Contains username/password, skip it and the @ sign. */
275 /* No username or password. */
279 /* check if url contains port */
280 port = strchr(host, ':');
284 /* Terminate hostname and point to start of port string */
286 http->port = atoi(port);
290 /* No port specified. */
291 http->port = (http->ssl ? 143 : 80);
294 http->host = strdup(host);
298 if (http->host == NULL)
300 free_http_request(http);
301 return JB_ERR_MEMORY;
307 * Split domain name so we can compare it against wildcards
310 char *vec[BUFFER_SIZE];
314 http->dbuffer = strdup(http->host);
315 if (NULL == http->dbuffer)
317 free_http_request(http);
318 return JB_ERR_MEMORY;
321 /* map to lower case */
322 for (p = http->dbuffer; *p ; p++)
324 *p = tolower((int)(unsigned char)*p);
327 /* split the domain name into components */
328 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
330 if (http->dcount <= 0)
333 * Error: More than SZ(vec) components in domain
334 * or: no components in domain
336 free_http_request(http);
340 /* save a copy of the pointers in dvec */
341 size = http->dcount * sizeof(*http->dvec);
343 http->dvec = (char **)malloc(size);
344 if (NULL == http->dvec)
346 free_http_request(http);
347 return JB_ERR_MEMORY;
350 memcpy(http->dvec, vec, size);
358 /*********************************************************************
360 * Function : parse_http_request
362 * Description : Parse out the host and port from the URL. Find the
363 * hostname & path, port (if ':'), and/or password (if '@')
366 * 1 : req = HTTP request line to break down
367 * 2 : http = pointer to the http structure to hold elements
368 * 3 : csp = Current client state (buffers, headers, etc...)
370 * Returns : JB_ERR_OK on success
371 * JB_ERR_MEMORY on out of memory
372 * JB_ERR_CGI_PARAMS on malformed command/URL
373 * or >100 domains deep.
375 *********************************************************************/
376 jb_err parse_http_request(const char *req,
377 struct http_request *http,
378 struct client_state *csp)
386 memset(http, '\0', sizeof(*http));
391 return JB_ERR_MEMORY;
394 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
401 /* this could be a CONNECT request */
402 if (strcmpic(v[0], "connect") == 0)
407 /* or it could be any other basic HTTP request type */
408 else if ((0 == strcmpic(v[0], "get"))
409 || (0 == strcmpic(v[0], "head"))
410 || (0 == strcmpic(v[0], "post"))
411 || (0 == strcmpic(v[0], "put"))
412 || (0 == strcmpic(v[0], "delete"))
414 /* or a webDAV extension (RFC2518) */
415 || (0 == strcmpic(v[0], "propfind"))
416 || (0 == strcmpic(v[0], "proppatch"))
417 || (0 == strcmpic(v[0], "move"))
418 || (0 == strcmpic(v[0], "copy"))
419 || (0 == strcmpic(v[0], "mkcol"))
420 || (0 == strcmpic(v[0], "lock"))
421 || (0 == strcmpic(v[0], "unlock"))
429 /* Unknown HTTP method */
434 err = parse_http_url(v[1], http, csp);
442 * Copy the details into the structure
444 http->ssl = is_connect;
445 http->cmd = strdup(req);
446 http->gpc = strdup(v[0]);
447 http->ver = strdup(v[2]);
449 if ( (http->cmd == NULL)
450 || (http->gpc == NULL)
451 || (http->ver == NULL) )
454 free_http_request(http);
455 return JB_ERR_MEMORY;
462 /*********************************************************************
464 * Function : simple_domaincmp
466 * Description : Domain-wise Compare fqdn's. The comparison is
467 * both left- and right-anchored. The individual
468 * domain names are compared with simplematch().
469 * This is only used by domain_match.
472 * 1 : pv = array of patterns to compare
473 * 2 : fv = array of domain components to compare
474 * 3 : len = length of the arrays (both arrays are the
475 * same length - if they weren't, it couldn't
476 * possibly be a match).
478 * Returns : 0 => domains are equivalent, else no match.
480 *********************************************************************/
481 static int simple_domaincmp(char **pv, char **fv, int len)
485 for (n = 0; n < len; n++)
487 if (simplematch(pv[n], fv[n]))
498 /*********************************************************************
500 * Function : domain_match
502 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
503 * pattern->unachored, the comparison is un-, left-,
504 * right-anchored, or both.
505 * The individual domain names are compared with
509 * 1 : pattern = a domain that may contain a '*' as a wildcard.
510 * 2 : fqdn = domain name against which the patterns are compared.
512 * Returns : 0 => domains are equivalent, else no match.
514 *********************************************************************/
515 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
517 char **pv, **fv; /* vectors */
519 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
521 plen = pattern->dcount;
526 /* fqdn is too short to match this pattern */
533 if (unanchored == ANCHOR_LEFT)
538 * Convert this into a fully anchored pattern with
539 * the fqdn and pattern the same length
541 fv += (flen - plen); /* flen - plen >= 0 due to check above */
542 return simple_domaincmp(pv, fv, plen);
544 else if (unanchored == 0)
546 /* Fully anchored, check length */
551 return simple_domaincmp(pv, fv, plen);
553 else if (unanchored == ANCHOR_RIGHT)
555 /* Left anchored, ignore all extra in fqdn */
556 return simple_domaincmp(pv, fv, plen);
562 int maxn = flen - plen;
563 for (n = 0; n <= maxn; n++)
565 if (!simple_domaincmp(pv, fv, plen))
570 * Doesn't match from start of fqdn
571 * Try skipping first part of fqdn
581 /*********************************************************************
583 * Function : create_url_spec
585 * Description : Creates a "url_spec" structure from a string.
586 * When finished, free with unload_url().
589 * 1 : url = Target url_spec to be filled in. Will be
591 * 2 : buf = Source pattern, null terminated. NOTE: The
592 * contents of this buffer are destroyed by this
593 * function. If this function succeeds, the
594 * buffer is copied to url->spec. If this
595 * function fails, the contents of the buffer
598 * Returns : JB_ERR_OK - Success
599 * JB_ERR_MEMORY - Out of memory
600 * JB_ERR_PARSE - Cannot parse regex (Detailed message
601 * written to system log)
603 *********************************************************************/
604 jb_err create_url_spec(struct url_spec * url, const char * buf)
612 memset(url, '\0', sizeof(*url));
614 /* save a copy of the orignal specification */
615 if ((url->spec = strdup(buf)) == NULL)
617 return JB_ERR_MEMORY;
620 if ((p = strchr(buf, '/')) != NULL)
622 if (NULL == (url->path = strdup(p)))
625 return JB_ERR_MEMORY;
627 url->pathlen = strlen(url->path);
639 char rebuf[BUFFER_SIZE];
641 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
645 return JB_ERR_MEMORY;
648 sprintf(rebuf, "^(%s)", url->path);
650 errcode = regcomp(url->preg, rebuf,
651 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
654 size_t errlen = regerror(errcode,
655 url->preg, rebuf, sizeof(rebuf));
657 if (errlen > (sizeof(rebuf) - (size_t)1))
659 errlen = sizeof(rebuf) - (size_t)1;
661 rebuf[errlen] = '\0';
663 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
674 if ((p = strchr(buf, ':')) == NULL)
689 /* Parse domain part */
690 if (buf[strlen(buf) - 1] == '.')
692 url->unanchored |= ANCHOR_RIGHT;
696 url->unanchored |= ANCHOR_LEFT;
699 /* split domain into components */
701 url->dbuffer = strdup(buf);
702 if (NULL == url->dbuffer)
708 #endif /* def REGEX */
709 return JB_ERR_MEMORY;
712 /* map to lower case */
713 for (p = url->dbuffer; *p ; p++)
715 *p = tolower((int)(unsigned char)*p);
718 /* split the domain name into components */
719 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
727 #endif /* def REGEX */
730 return JB_ERR_MEMORY;
732 else if (url->dcount != 0)
735 /* save a copy of the pointers in dvec */
736 size = url->dcount * sizeof(*url->dvec);
738 url->dvec = (char **)malloc(size);
739 if (NULL == url->dvec)
745 #endif /* def REGEX */
748 return JB_ERR_MEMORY;
751 memcpy(url->dvec, v, size);
760 /*********************************************************************
762 * Function : free_url_spec
764 * Description : Called from the "unloaders". Freez the url
765 * structure elements.
768 * 1 : url = pointer to a url_spec structure.
772 *********************************************************************/
773 void free_url_spec(struct url_spec *url)
775 if (url == NULL) return;
792 /*********************************************************************
794 * Function : url_match
796 * Description : Compare a URL against a URL pattern.
799 * 1 : pattern = a URL pattern
800 * 2 : url = URL to match
802 * Returns : 0 iff the URL matches the pattern, else nonzero.
804 *********************************************************************/
805 int url_match(const struct url_spec *pattern,
806 const struct http_request *url)
808 return ((pattern->port == 0) || (pattern->port == url->port))
809 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
810 && ((pattern->path == NULL) ||
812 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)
814 (strncmp(pattern->path, url->path, pattern->pathlen) == 0)