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