8bb8e267fe6254759d7f028147b4ce64ce333340
[privoxy.git] / urlmatch.c
1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.15 2007/01/28 16:11:23 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
5  *
6  * Purpose     :  Declares functions to match URLs against URL
7  *                patterns.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2003, 2006-2007 the SourceForge
10  *                Privoxy team. http://www.privoxy.org/
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
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.
21  *
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.
27  *
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.
33  *
34  * Revisions   :
35  *    $Log: urlmatch.c,v $
36  *    Revision 1.15  2007/01/28 16:11:23  fabiankeil
37  *    Accept WebDAV methods for subversion
38  *    in parse_http_request(). Closes FR 1581425.
39  *
40  *    Revision 1.14  2007/01/06 14:23:56  fabiankeil
41  *    Fix gcc43 warnings. Mark *csp as immutable
42  *    for parse_http_url() and url_match().
43  *    Replace a sprintf call with snprintf.
44  *
45  *    Revision 1.13  2006/12/06 19:50:54  fabiankeil
46  *    parse_http_url() now handles intercepted
47  *    HTTP request lines as well. Moved parts
48  *    of parse_http_url()'s code into
49  *    init_domain_components() so that it can
50  *    be reused in chat().
51  *
52  *    Revision 1.12  2006/07/18 14:48:47  david__schmidt
53  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
54  *    with what was really the latest development (the v_3_0_branch branch)
55  *
56  *    Revision 1.10.2.7  2003/05/17 15:57:24  oes
57  *     - parse_http_url now checks memory allocation failure for
58  *       duplication of "*" URL and rejects "*something" URLs
59  *       Closes bug #736344
60  *     - Added a comment to what might look like a bug in
61  *       create_url_spec (see !bug #736931)
62  *     - Comment cosmetics
63  *
64  *    Revision 1.10.2.6  2003/05/07 12:39:48  oes
65  *    Fix typo: Default port for https URLs is 443, not 143.
66  *    Thanks to Scott Tregear for spotting this one.
67  *
68  *    Revision 1.10.2.5  2003/02/28 13:09:29  oes
69  *    Fixed a rare double free condition as per Bug #694713
70  *
71  *    Revision 1.10.2.4  2003/02/28 12:57:44  oes
72  *    Moved freeing of http request structure to its owner
73  *    as per Dan Price's observations in Bug #694713
74  *
75  *    Revision 1.10.2.3  2002/11/12 16:50:40  oes
76  *    Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
77  *
78  *    Revision 1.10.2.2  2002/09/25 14:53:15  oes
79  *    Added basic support for OPTIONS and TRACE HTTP methods:
80  *    parse_http_url now recognizes the "*" URI as well as
81  *    the OPTIONS and TRACE method keywords.
82  *
83  *    Revision 1.10.2.1  2002/06/06 19:06:44  jongfoster
84  *    Adding support for proprietary Microsoft WebDAV extensions
85  *
86  *    Revision 1.10  2002/05/12 21:40:37  jongfoster
87  *    - Removing some unused code
88  *
89  *    Revision 1.9  2002/04/04 00:36:36  gliptak
90  *    always use pcre for matching
91  *
92  *    Revision 1.8  2002/04/03 23:32:47  jongfoster
93  *    Fixing memory leak on error
94  *
95  *    Revision 1.7  2002/03/26 22:29:55  swa
96  *    we have a new homepage!
97  *
98  *    Revision 1.6  2002/03/24 13:25:43  swa
99  *    name change related issues
100  *
101  *    Revision 1.5  2002/03/13 00:27:05  jongfoster
102  *    Killing warnings
103  *
104  *    Revision 1.4  2002/03/07 03:46:17  oes
105  *    Fixed compiler warnings
106  *
107  *    Revision 1.3  2002/03/03 14:51:11  oes
108  *    Fixed CLF logging: Added ocmd member for client's request to struct http_request
109  *
110  *    Revision 1.2  2002/01/21 00:14:09  jongfoster
111  *    Correcting comment style
112  *    Fixing an uninitialized memory bug in create_url_spec()
113  *
114  *    Revision 1.1  2002/01/17 20:53:46  jongfoster
115  *    Moving all our URL and URL pattern parsing code to the same file - it
116  *    was scattered around in filters.c, loaders.c and parsers.c.
117  *
118  *    Providing a single, simple url_match(pattern,url) function - rather than
119  *    the 3-line match routine which was repeated all over the place.
120  *
121  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
122  *
123  *    Providing parse_http_url() so that URLs can be parsed without faking a
124  *    HTTP request line for parse_http_request() or repeating the parsing
125  *    code (both of which were techniques that were actually in use).
126  *
127  *    Standardizing that struct http_request is used to represent a URL, and
128  *    struct url_spec is used to represent a URL pattern.  (Before, URLs were
129  *    represented as seperate variables and a partially-filled-in url_spec).
130  *
131  *
132  *********************************************************************/
133 \f
134
135 #include "config.h"
136
137 #ifndef _WIN32
138 #include <stdio.h>
139 #include <sys/types.h>
140 #endif
141
142 #include <stdlib.h>
143 #include <ctype.h>
144 #include <assert.h>
145 #include <string.h>
146
147 #if !defined(_WIN32) && !defined(__OS2__)
148 #include <unistd.h>
149 #endif
150
151 #include "project.h"
152 #include "urlmatch.h"
153 #include "ssplit.h"
154 #include "miscutil.h"
155 #include "errlog.h"
156
157 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
158
159
160 /*********************************************************************
161  *
162  * Function    :  free_http_request
163  *
164  * Description :  Freez a http_request structure
165  *
166  * Parameters  :
167  *          1  :  http = points to a http_request structure to free
168  *
169  * Returns     :  N/A
170  *
171  *********************************************************************/
172 void free_http_request(struct http_request *http)
173 {
174    assert(http);
175
176    freez(http->cmd);
177    freez(http->ocmd);
178    freez(http->gpc);
179    freez(http->host);
180    freez(http->url);
181    freez(http->hostport);
182    freez(http->path);
183    freez(http->ver);
184    freez(http->host_ip_addr_str);
185    freez(http->dbuffer);
186    freez(http->dvec);
187    http->dcount = 0;
188 }
189
190 /*********************************************************************
191  *
192  * Function    :  init_domain_components
193  *
194  * Description :  Splits the domain name so we can compare it
195  *                against wildcards. It used to be part of
196  *                parse_http_url, but was separated because the
197  *                same code is required in chat in case of
198  *                intercepted requests.
199  *
200  * Parameters  :
201  *          1  :  http = pointer to the http structure to hold elements.
202  *
203  * Returns     :  JB_ERR_OK on success
204  *                JB_ERR_MEMORY on out of memory
205  *                JB_ERR_PARSE on malformed command/URL
206  *                             or >100 domains deep.
207  *
208  *********************************************************************/
209 jb_err init_domain_components(struct http_request *http)
210 {
211    char *vec[BUFFER_SIZE];
212    size_t size;
213    char *p;
214
215    http->dbuffer = strdup(http->host);
216    if (NULL == http->dbuffer)
217    {
218       return JB_ERR_MEMORY;
219    }
220
221    /* map to lower case */
222    for (p = http->dbuffer; *p ; p++)
223    {
224       *p = (char)tolower((int)(unsigned char)*p);
225    }
226
227    /* split the domain name into components */
228    http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
229
230    if (http->dcount <= 0)
231    {
232       /*
233        * Error: More than SZ(vec) components in domain
234        *    or: no components in domain
235        */
236       log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
237       return JB_ERR_PARSE;
238    }
239
240    /* save a copy of the pointers in dvec */
241    size = (size_t)http->dcount * sizeof(*http->dvec);
242
243    http->dvec = (char **)malloc(size);
244    if (NULL == http->dvec)
245    {
246       return JB_ERR_MEMORY;
247    }
248
249    memcpy(http->dvec, vec, size);
250
251    return JB_ERR_OK;
252 }
253
254 /*********************************************************************
255  *
256  * Function    :  parse_http_url
257  *
258  * Description :  Parse out the host and port from the URL.  Find the
259  *                hostname & path, port (if ':'), and/or password (if '@')
260  *
261  * Parameters  :
262  *          1  :  url = URL (or is it URI?) to break down
263  *          2  :  http = pointer to the http structure to hold elements.
264  *                       Will be zeroed before use.  Note that this
265  *                       function sets the http->gpc and http->ver
266  *                       members to NULL.
267  *          3  :  csp = Current client state (buffers, headers, etc...)
268  *
269  * Returns     :  JB_ERR_OK on success
270  *                JB_ERR_MEMORY on out of memory
271  *                JB_ERR_PARSE on malformed command/URL
272  *                             or >100 domains deep.
273  *
274  *********************************************************************/
275 jb_err parse_http_url(const char * url,
276                       struct http_request *http,
277                       const struct client_state *csp)
278 {
279    int host_available = 1; /* A proxy can dream. */
280
281    /*
282     * Zero out the results structure
283     */
284    memset(http, '\0', sizeof(*http));
285
286
287    /*
288     * Save our initial URL
289     */
290    http->url = strdup(url);
291    if (http->url == NULL)
292    {
293       return JB_ERR_MEMORY;
294    }
295
296
297    /*
298     * Check for * URI. If found, we're done.
299     */  
300    if (*http->url == '*')
301    {
302       if  ( NULL == (http->path = strdup("*"))
303          || NULL == (http->hostport = strdup("")) ) 
304       {
305          return JB_ERR_MEMORY;
306       }
307       if (http->url[1] != '\0')
308       {
309          return JB_ERR_PARSE;
310       }
311       return JB_ERR_OK;
312    }
313
314
315    /*
316     * Split URL into protocol,hostport,path.
317     */
318    {
319       char *buf;
320       char *url_noproto;
321       char *url_path;
322
323       buf = strdup(url);
324       if (buf == NULL)
325       {
326          return JB_ERR_MEMORY;
327       }
328
329       /* Find the start of the URL in our scratch space */
330       url_noproto = buf;
331       if (strncmpic(url_noproto, "http://",  7) == 0)
332       {
333          url_noproto += 7;
334          http->ssl = 0;
335       }
336       else if (strncmpic(url_noproto, "https://", 8) == 0)
337       {
338          url_noproto += 8;
339          http->ssl = 1;
340       }
341       else if (*url_noproto == '/')
342       {
343         /*
344          * Short request line without protocol and host.
345          * Most likely because the client's request
346          * was intercepted and redirected into Privoxy.
347          */
348          http->ssl = 0;
349          http->host = NULL;
350          host_available = 0;
351       }
352       else
353       {
354          http->ssl = 0;
355       }
356
357       url_path = strchr(url_noproto, '/');
358       if (url_path != NULL)
359       {
360          /*
361           * Got a path.
362           *
363           * NOTE: The following line ignores the path for HTTPS URLS.
364           * This means that you get consistent behaviour if you type a
365           * https URL in and it's parsed by the function.  (When the
366           * URL is actually retrieved, SSL hides the path part).
367           */
368          http->path = strdup(http->ssl ? "/" : url_path);
369          *url_path = '\0';
370          http->hostport = strdup(url_noproto);
371       }
372       else
373       {
374          /*
375           * Repair broken HTTP requests that don't contain a path,
376           * or CONNECT requests
377           */
378          http->path = strdup("/");
379          http->hostport = strdup(url_noproto);
380       }
381
382       freez(buf);
383
384       if ( (http->path == NULL)
385         || (http->hostport == NULL))
386       {
387          return JB_ERR_MEMORY;
388       }
389    }
390
391    if (!host_available)
392    {
393       /* Without host, there is nothing left to do here */
394       return JB_ERR_OK;
395    }
396
397    /*
398     * Split hostport into user/password (ignored), host, port.
399     */
400    {
401       char *buf;
402       char *host;
403       char *port;
404
405       buf = strdup(http->hostport);
406       if (buf == NULL)
407       {
408          return JB_ERR_MEMORY;
409       }
410
411       /* check if url contains username and/or password */
412       host = strchr(buf, '@');
413       if (host != NULL)
414       {
415          /* Contains username/password, skip it and the @ sign. */
416          host++;
417       }
418       else
419       {
420          /* No username or password. */
421          host = buf;
422       }
423
424       /* check if url contains port */
425       port = strchr(host, ':');
426       if (port != NULL)
427       {
428          /* Contains port */
429          /* Terminate hostname and point to start of port string */
430          *port++ = '\0';
431          http->port = atoi(port);
432       }
433       else
434       {
435          /* No port specified. */
436          http->port = (http->ssl ? 443 : 80);
437       }
438
439       http->host = strdup(host);
440
441       free(buf);
442
443       if (http->host == NULL)
444       {
445          return JB_ERR_MEMORY;
446       }
447    }
448
449    /*
450     * Split domain name so we can compare it against wildcards
451     */
452    return init_domain_components(http);
453
454 }
455
456
457 /*********************************************************************
458  *
459  * Function    :  parse_http_request
460  *
461  * Description :  Parse out the host and port from the URL.  Find the
462  *                hostname & path, port (if ':'), and/or password (if '@')
463  *
464  * Parameters  :
465  *          1  :  req = HTTP request line to break down
466  *          2  :  http = pointer to the http structure to hold elements
467  *          3  :  csp = Current client state (buffers, headers, etc...)
468  *
469  * Returns     :  JB_ERR_OK on success
470  *                JB_ERR_MEMORY on out of memory
471  *                JB_ERR_CGI_PARAMS on malformed command/URL
472  *                                  or >100 domains deep.
473  *
474  *********************************************************************/
475 jb_err parse_http_request(const char *req,
476                           struct http_request *http,
477                           const struct client_state *csp)
478 {
479    char *buf;
480    char *v[10];
481    int n;
482    jb_err err;
483    int is_connect = 0;
484
485    memset(http, '\0', sizeof(*http));
486
487    buf = strdup(req);
488    if (buf == NULL)
489    {
490       return JB_ERR_MEMORY;
491    }
492
493    n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
494    if (n != 3)
495    {
496       free(buf);
497       return JB_ERR_PARSE;
498    }
499
500    /* this could be a CONNECT request */
501    if (strcmpic(v[0], "connect") == 0)
502    {
503       /* Secure */
504       is_connect = 1;
505    }
506    /* or it could be any other basic HTTP request type */
507    else if ((0 == strcmpic(v[0], "get"))
508          || (0 == strcmpic(v[0], "head"))
509          || (0 == strcmpic(v[0], "post"))
510          || (0 == strcmpic(v[0], "put"))
511          || (0 == strcmpic(v[0], "delete"))
512          || (0 == strcmpic(v[0], "options"))
513          || (0 == strcmpic(v[0], "trace"))
514  
515          /* or a webDAV extension (RFC2518) */
516          || (0 == strcmpic(v[0], "propfind"))
517          || (0 == strcmpic(v[0], "proppatch"))
518          || (0 == strcmpic(v[0], "move"))
519          || (0 == strcmpic(v[0], "copy"))
520          || (0 == strcmpic(v[0], "mkcol"))
521          || (0 == strcmpic(v[0], "lock"))
522          || (0 == strcmpic(v[0], "unlock"))
523
524          /* Or a Microsoft webDAV extension for Exchange 2000.  See: */
525          /*   http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
526          /*   http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */ 
527          || (0 == strcmpic(v[0], "bcopy"))
528          || (0 == strcmpic(v[0], "bmove"))
529          || (0 == strcmpic(v[0], "bdelete"))
530          || (0 == strcmpic(v[0], "bpropfind"))
531          || (0 == strcmpic(v[0], "bproppatch"))
532
533          /* Or 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 */ 
537          || (0 == strcmpic(v[0], "subscribe"))
538          || (0 == strcmpic(v[0], "unsubscribe"))
539          || (0 == strcmpic(v[0], "notify"))
540          || (0 == strcmpic(v[0], "poll"))
541
542          /*
543           * Or yet another WebDAV extension, this time for
544           * Web Distributed Authoring and Versioning (RFC3253)
545           */
546          || (0 == strcmpic(v[0], "version-control"))
547          || (0 == strcmpic(v[0], "report"))
548          || (0 == strcmpic(v[0], "checkout"))
549          || (0 == strcmpic(v[0], "checkin"))
550          || (0 == strcmpic(v[0], "uncheckout"))
551          || (0 == strcmpic(v[0], "mkworkspace"))
552          || (0 == strcmpic(v[0], "update"))
553          || (0 == strcmpic(v[0], "label"))
554          || (0 == strcmpic(v[0], "merge"))
555          || (0 == strcmpic(v[0], "baseline-control"))
556          || (0 == strcmpic(v[0], "mkactivity"))
557          )
558    {
559       /* Normal */
560       is_connect = 0;
561    }
562    else
563    {
564       /* Unknown HTTP method */
565       log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
566       free(buf);
567       return JB_ERR_PARSE;
568    }
569
570    err = parse_http_url(v[1], http, csp);
571    if (err)
572    {
573       free(buf);
574       return err;
575    }
576
577    /*
578     * Copy the details into the structure
579     */
580    http->ssl = is_connect;
581    http->cmd = strdup(req);
582    http->gpc = strdup(v[0]);
583    http->ver = strdup(v[2]);
584
585    if ( (http->cmd == NULL)
586      || (http->gpc == NULL)
587      || (http->ver == NULL) )
588    {
589       free(buf);
590       return JB_ERR_MEMORY;
591    }
592
593    free(buf);
594    return JB_ERR_OK;
595
596 }
597
598
599 /*********************************************************************
600  *
601  * Function    :  simple_domaincmp
602  *
603  * Description :  Domain-wise Compare fqdn's.  The comparison is
604  *                both left- and right-anchored.  The individual
605  *                domain names are compared with simplematch().
606  *                This is only used by domain_match.
607  *
608  * Parameters  :
609  *          1  :  pv = array of patterns to compare
610  *          2  :  fv = array of domain components to compare
611  *          3  :  len = length of the arrays (both arrays are the
612  *                      same length - if they weren't, it couldn't
613  *                      possibly be a match).
614  *
615  * Returns     :  0 => domains are equivalent, else no match.
616  *
617  *********************************************************************/
618 static int simple_domaincmp(char **pv, char **fv, int len)
619 {
620    int n;
621
622    for (n = 0; n < len; n++)
623    {
624       if (simplematch(pv[n], fv[n]))
625       {
626          return 1;
627       }
628    }
629
630    return 0;
631
632 }
633
634
635 /*********************************************************************
636  *
637  * Function    :  domain_match
638  *
639  * Description :  Domain-wise Compare fqdn's. Governed by the bimap in
640  *                pattern->unachored, the comparison is un-, left-,
641  *                right-anchored, or both.
642  *                The individual domain names are compared with
643  *                simplematch().
644  *
645  * Parameters  :
646  *          1  :  pattern = a domain that may contain a '*' as a wildcard.
647  *          2  :  fqdn = domain name against which the patterns are compared.
648  *
649  * Returns     :  0 => domains are equivalent, else no match.
650  *
651  *********************************************************************/
652 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
653 {
654    char **pv, **fv;  /* vectors  */
655    int    plen, flen;
656    int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
657
658    plen = pattern->dcount;
659    flen = fqdn->dcount;
660
661    if (flen < plen)
662    {
663       /* fqdn is too short to match this pattern */
664       return 1;
665    }
666
667    pv   = pattern->dvec;
668    fv   = fqdn->dvec;
669
670    if (unanchored == ANCHOR_LEFT)
671    {
672       /*
673        * Right anchored.
674        *
675        * Convert this into a fully anchored pattern with
676        * the fqdn and pattern the same length
677        */
678       fv += (flen - plen); /* flen - plen >= 0 due to check above */
679       return simple_domaincmp(pv, fv, plen);
680    }
681    else if (unanchored == 0)
682    {
683       /* Fully anchored, check length */
684       if (flen != plen)
685       {
686          return 1;
687       }
688       return simple_domaincmp(pv, fv, plen);
689    }
690    else if (unanchored == ANCHOR_RIGHT)
691    {
692       /* Left anchored, ignore all extra in fqdn */
693       return simple_domaincmp(pv, fv, plen);
694    }
695    else
696    {
697       /* Unanchored */
698       int n;
699       int maxn = flen - plen;
700       for (n = 0; n <= maxn; n++)
701       {
702          if (!simple_domaincmp(pv, fv, plen))
703          {
704             return 0;
705          }
706          /*
707           * Doesn't match from start of fqdn
708           * Try skipping first part of fqdn
709           */
710          fv++;
711       }
712       return 1;
713    }
714
715 }
716
717
718 /*********************************************************************
719  *
720  * Function    :  create_url_spec
721  *
722  * Description :  Creates a "url_spec" structure from a string.
723  *                When finished, free with free_url_spec().
724  *
725  * Parameters  :
726  *          1  :  url = Target url_spec to be filled in.  Will be
727  *                      zeroed before use.
728  *          2  :  buf = Source pattern, null terminated.  NOTE: The
729  *                      contents of this buffer are destroyed by this
730  *                      function.  If this function succeeds, the
731  *                      buffer is copied to url->spec.  If this
732  *                      function fails, the contents of the buffer
733  *                      are lost forever.
734  *
735  * Returns     :  JB_ERR_OK - Success
736  *                JB_ERR_MEMORY - Out of memory
737  *                JB_ERR_PARSE - Cannot parse regex (Detailed message
738  *                               written to system log)
739  *
740  *********************************************************************/
741 jb_err create_url_spec(struct url_spec * url, const char * buf)
742 {
743    char *p;
744
745    assert(url);
746    assert(buf);
747
748    /*
749     * Zero memory
750     */
751    memset(url, '\0', sizeof(*url));
752
753    /*
754     * Save a copy of the orignal specification
755     */
756    if ((url->spec = strdup(buf)) == NULL)
757    {
758       return JB_ERR_MEMORY;
759    }
760
761    if ((p = strchr(buf, '/')) != NULL)
762    {
763       if (NULL == (url->path = strdup(p)))
764       {
765          freez(url->spec);
766          return JB_ERR_MEMORY;
767       }
768       url->pathlen = strlen(url->path);
769       *p = '\0';
770    }
771    else
772    {
773       url->path    = NULL;
774       url->pathlen = 0;
775    }
776    if (url->path)
777    {
778       int errcode;
779       char rebuf[BUFFER_SIZE];
780
781       if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
782       {
783          freez(url->spec);
784          freez(url->path);
785          return JB_ERR_MEMORY;
786       }
787
788       snprintf(rebuf, sizeof(rebuf), "^(%s)", url->path);
789
790       errcode = regcomp(url->preg, rebuf,
791             (REG_EXTENDED|REG_NOSUB|REG_ICASE));
792       if (errcode)
793       {
794          size_t errlen = regerror(errcode,
795             url->preg, rebuf, sizeof(rebuf));
796
797          if (errlen > (sizeof(rebuf) - (size_t)1))
798          {
799             errlen = sizeof(rebuf) - (size_t)1;
800          }
801          rebuf[errlen] = '\0';
802
803          log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
804             url->spec, rebuf);
805
806          freez(url->spec);
807          freez(url->path);
808          regfree(url->preg);
809          freez(url->preg);
810
811          return JB_ERR_PARSE;
812       }
813    }
814    if ((p = strchr(buf, ':')) == NULL)
815    {
816       url->port = 0;
817    }
818    else
819    {
820       *p++ = '\0';
821       url->port = atoi(p);
822    }
823
824    if (buf[0] != '\0')
825    {
826       char *v[150];
827       size_t size;
828
829       /*
830        * Parse domain part
831        */
832       if (buf[strlen(buf) - 1] == '.')
833       {
834          url->unanchored |= ANCHOR_RIGHT;
835       }
836       if (buf[0] == '.')
837       {
838          url->unanchored |= ANCHOR_LEFT;
839       }
840
841       /* 
842        * Split domain into components
843        */
844       url->dbuffer = strdup(buf);
845       if (NULL == url->dbuffer)
846       {
847          freez(url->spec);
848          freez(url->path);
849          regfree(url->preg);
850          freez(url->preg);
851          return JB_ERR_MEMORY;
852       }
853
854       /* 
855        * Map to lower case
856        */
857       for (p = url->dbuffer; *p ; p++)
858       {
859          *p = (char)tolower((int)(unsigned char)*p);
860       }
861
862       /* 
863        * Split the domain name into components
864        */
865       url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
866
867       if (url->dcount < 0)
868       {
869          freez(url->spec);
870          freez(url->path);
871          regfree(url->preg);
872          freez(url->preg);
873          freez(url->dbuffer);
874          url->dcount = 0;
875          return JB_ERR_MEMORY;
876       }
877       else if (url->dcount != 0)
878       {
879
880          /* 
881           * Save a copy of the pointers in dvec
882           */
883          size = (size_t)url->dcount * sizeof(*url->dvec);
884
885          url->dvec = (char **)malloc(size);
886          if (NULL == url->dvec)
887          {
888             freez(url->spec);
889             freez(url->path);
890             regfree(url->preg);
891             freez(url->preg);
892             freez(url->dbuffer);
893             url->dcount = 0;
894             return JB_ERR_MEMORY;
895          }
896
897          memcpy(url->dvec, v, size);
898       }
899       /*
900        * else dcount == 0 in which case we needn't do anything,
901        * since dvec will never be accessed and the pattern will
902        * match all domains.
903        */
904    }
905
906    return JB_ERR_OK;
907
908 }
909
910
911 /*********************************************************************
912  *
913  * Function    :  free_url_spec
914  *
915  * Description :  Called from the "unloaders".  Freez the url
916  *                structure elements.
917  *
918  * Parameters  :
919  *          1  :  url = pointer to a url_spec structure.
920  *
921  * Returns     :  N/A
922  *
923  *********************************************************************/
924 void free_url_spec(struct url_spec *url)
925 {
926    if (url == NULL) return;
927
928    freez(url->spec);
929    freez(url->dbuffer);
930    freez(url->dvec);
931    freez(url->path);
932    if (url->preg)
933    {
934       regfree(url->preg);
935       freez(url->preg);
936    }
937 }
938
939
940 /*********************************************************************
941  *
942  * Function    :  url_match
943  *
944  * Description :  Compare a URL against a URL pattern.
945  *
946  * Parameters  :
947  *          1  :  pattern = a URL pattern
948  *          2  :  url = URL to match
949  *
950  * Returns     :  0 iff the URL matches the pattern, else nonzero.
951  *
952  *********************************************************************/
953 int url_match(const struct url_spec *pattern,
954               const struct http_request *url)
955 {
956    return ((pattern->port == 0) || (pattern->port == url->port))
957        && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
958        && ((pattern->path == NULL) ||
959             (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)
960       );
961 }
962
963
964 /*
965   Local Variables:
966   tab-width: 3
967   end:
968 */