Bugfix: is_untrusted_url() now depends on FEATURE_TRUST, not FEATURE_COOKIE_JAR
[privoxy.git] / filters.c
1 const char filters_rcs[] = "$Id: filters.c,v 1.53 2002/03/26 22:29:54 swa Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
5  *
6  * Purpose     :  Declares functions to parse/crunch headers and pages.
7  *                Functions declared include:
8  *                   `acl_addr', `add_stats', `block_acl', `block_imageurl',
9  *                   `block_url', `url_actions', `domain_split',
10  *                   `filter_popups', `forward_url', 'redirect_url',
11  *                   `ij_untrusted_url', `intercept_url', `pcrs_filter_respose',
12  *                   'ijb_send_banner', and `trust_url'
13  *
14  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
15  *                Privoxy team. http://www.privoxy.org/
16  *
17  *                Based on the Internet Junkbuster originally written
18  *                by and Copyright (C) 1997 Anonymous Coders and
19  *                Junkbusters Corporation.  http://www.junkbusters.com
20  *
21  *                This program is free software; you can redistribute it
22  *                and/or modify it under the terms of the GNU General
23  *                Public License as published by the Free Software
24  *                Foundation; either version 2 of the License, or (at
25  *                your option) any later version.
26  *
27  *                This program is distributed in the hope that it will
28  *                be useful, but WITHOUT ANY WARRANTY; without even the
29  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
30  *                PARTICULAR PURPOSE.  See the GNU General Public
31  *                License for more details.
32  *
33  *                The GNU General Public License should be included with
34  *                this file.  If not, you can view it at
35  *                http://www.gnu.org/copyleft/gpl.html
36  *                or write to the Free Software Foundation, Inc., 59
37  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38  *
39  * Revisions   :
40  *    $Log: filters.c,v $
41  *    Revision 1.53  2002/03/26 22:29:54  swa
42  *    we have a new homepage!
43  *
44  *    Revision 1.52  2002/03/24 16:35:57  jongfoster
45  *    Removing logo
46  *
47  *    Revision 1.51  2002/03/24 15:23:33  jongfoster
48  *    Name changes
49  *
50  *    Revision 1.50  2002/03/24 13:25:43  swa
51  *    name change related issues
52  *
53  *    Revision 1.49  2002/03/16 20:29:14  oes
54  *    Cosmetics
55  *
56  *    Revision 1.48  2002/03/13 20:25:34  oes
57  *    Better logging for content filters
58  *
59  *    Revision 1.47  2002/03/13 00:30:52  jongfoster
60  *    Killing warnings
61  *    Added option of always sending redirect for imageblock,
62  *    currently disabled with #if 0.
63  *
64  *    Revision 1.46  2002/03/12 01:42:49  oes
65  *    Introduced modular filters
66  *
67  *    Revision 1.45  2002/03/08 16:47:50  oes
68  *    Added choice beween GIF and PNG built-in images
69  *
70  *    Revision 1.44  2002/03/07 03:49:31  oes
71  *     - Fixed compiler warnings etc
72  *     - Changed built-in images from GIF to PNG
73  *       (with regard to Unisys patent issue)
74  *     - Added a 4x4 pattern PNG which is less intrusive
75  *       than the logo but also clearly marks the deleted banners
76  *
77  *    Revision 1.43  2002/01/22 23:51:59  jongfoster
78  *    Replacing strsav() with the safer string_append().
79  *
80  *    Adding missing html_encode() to error message generators.  Where encoded
81  *    and unencoded versions of a string were provided, removing the unencoded
82  *    one.
83  *
84  *    Revision 1.42  2002/01/17 21:00:32  jongfoster
85  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
86  *
87  *    Using a single, simple url_match(pattern,url) function - rather than
88  *    the 3-line match routine which was repeated all over the place.
89  *
90  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
91  *
92  *    Using parse_http_url() to parse URLs without faking a HTTP
93  *    request line for parse_http_request().
94  *
95  *    Revision 1.41  2001/11/13 00:14:07  jongfoster
96  *    Fixing stupid bug now I've figured out what || means.
97  *    (It always returns 0 or 1, not one of it's paramaters.)
98  *
99  *    Revision 1.40  2001/10/26 17:37:55  oes
100  *    - Re-enabled Netscape 200/404 bug workaround in block_url():
101  *      - Removed OS/2 special case
102  *      - Made block_url() independant from sed() having been run
103  *    - Made trust_url independant from sed() having been run
104  *    - Made is_imageurl independant from sed() having been run.
105  *      It now checks User-Agent: and Accept: by itself.
106  *
107  *
108  *    Revision 1.39  2001/10/25 03:40:48  david__schmidt
109  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
110  *    threads to call select() simultaneously.  So, it's time to do a real, live,
111  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
112  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
113  *
114  *    Revision 1.38  2001/10/23 21:32:33  jongfoster
115  *    Adding error-checking to selected functions
116  *
117  *    Revision 1.37  2001/10/22 15:33:56  david__schmidt
118  *    Special-cased OS/2 out of the Netscape-abort-on-404-in-js problem in
119  *    filters.c.  Added a FIXME in front of the offending code.  I'll gladly
120  *    put in a better/more robust fix for all parties if one is presented...
121  *    It seems that just returning 200 instead of 404 would pretty much fix
122  *    it for everyone, but I don't know all the history of the problem.
123  *
124  *    Revision 1.36  2001/10/10 16:44:16  oes
125  *    Added match_portlist function
126  *
127  *    Revision 1.35  2001/10/07 15:41:23  oes
128  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
129  *
130  *    New function remove_chunked_transfer_coding that strips chunked
131  *      transfer coding to plain and is called by pcrs_filter_response
132  *      and gif_deanimate_response if neccessary
133  *
134  *    Improved handling of zero-change re_filter runs
135  *
136  *    pcrs_filter_response and gif_deanimate_response now remove
137  *      chunked transfer codeing before processing the body.
138  *
139  *    Revision 1.34  2001/09/20 15:49:36  steudten
140  *
141  *    Fix BUG: Change int size to size_t size in pcrs_filter_response().
142  *    See cgi.c fill_template().
143  *
144  *    Revision 1.33  2001/09/16 17:05:14  jongfoster
145  *    Removing unused #include showarg.h
146  *
147  *    Revision 1.32  2001/09/16 13:21:27  jongfoster
148  *    Changes to use new list functions.
149  *
150  *    Revision 1.31  2001/09/16 11:38:02  jongfoster
151  *    Splitting fill_template() into 2 functions:
152  *    template_load() loads the file
153  *    template_fill() performs the PCRS regexps.
154  *    This is because the CGI edit interface has a "table row"
155  *    template which is used many times in the page - this
156  *    change means it's only loaded from disk once.
157  *
158  *    Revision 1.30  2001/09/16 11:00:10  jongfoster
159  *    New function alloc_http_response, for symmetry with free_http_response
160  *
161  *    Revision 1.29  2001/09/13 23:32:40  jongfoster
162  *    Moving image data to cgi.c rather than cgi.h
163  *    Fixing a GPF under Win32 (and any other OS that protects global
164  *    constants from being written to).
165  *
166  *    Revision 1.28  2001/09/10 10:18:51  oes
167  *    Silenced compiler warnings
168  *
169  *    Revision 1.27  2001/08/05 16:06:20  jongfoster
170  *    Modifiying "struct map" so that there are now separate header and
171  *    "map_entry" structures.  This means that functions which modify a
172  *    map no longer need to return a pointer to the modified map.
173  *    Also, it no longer reverses the order of the entries (which may be
174  *    important with some advanced template substitutions).
175  *
176  *    Revision 1.26  2001/07/30 22:08:36  jongfoster
177  *    Tidying up #defines:
178  *    - All feature #defines are now of the form FEATURE_xxx
179  *    - Permanently turned off WIN_GUI_EDIT
180  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
181  *
182  *    Revision 1.25  2001/07/26 10:09:46  oes
183  *    Made browser detection a little less naive
184  *
185  *    Revision 1.24  2001/07/25 17:22:51  oes
186  *    Added workaround for Netscape bug that prevents display of page when loading a component fails.
187  *
188  *    Revision 1.23  2001/07/23 13:40:12  oes
189  *    Fixed bug that caused document body to be dropped when pcrs joblist was empty.
190  *
191  *    Revision 1.22  2001/07/18 12:29:34  oes
192  *    - Made gif_deanimate_response respect
193  *      csp->action->string[ACTION_STRING_DEANIMATE]
194  *    - Logging cosmetics
195  *
196  *    Revision 1.21  2001/07/13 13:59:53  oes
197  *     - Introduced gif_deanimate_response which shares the
198  *       generic content modification interface of pcrs_filter_response
199  *       and acts as a wrapper to deanimate.c:gif_deanimate()
200  *     - Renamed re_process_buffer to pcrs_filter_response
201  *     - pcrs_filter_response now returns NULL on failiure
202  *     - Removed all #ifdef PCRS
203  *
204  *    Revision 1.20  2001/07/01 17:01:04  oes
205  *    Added comments and missing return statement in is_untrusted_url()
206  *
207  *    Revision 1.19  2001/06/29 21:45:41  oes
208  *    Indentation, CRLF->LF, Tab-> Space
209  *
210  *    Revision 1.18  2001/06/29 13:27:38  oes
211  *    - Cleaned up, renamed and reorderd functions
212  *      and improved comments
213  *
214  *    - block_url:
215  *      - Ported to CGI platform. Now delivers
216  *        http_response or NULL
217  *      - Unified HTML and GIF generation (moved image detection
218  *        and GIF generation here from jcc.c:chat())
219  *      - Fixed HTTP status to:
220  *       -  403 (Forbidden) for the "blocked" HTML message
221  *       -  200 (OK) for GIF answers
222  *       -  302 (Redirect) for redirect to GIF
223  *
224  *    - trust_url:
225  *      - Ported to CGI platform. Now delivers
226  *        http_response or NULL
227  *      - Separated detection of untrusted URL into
228  *        (bool)is_untrusted_url
229  *      - Added enforcement of untrusted requests
230  *
231  *    - Moved redirect_url() from cgi.c to here
232  *      and ported it to the CGI platform
233  *
234  *    - Removed logentry from cancelled commit
235  *
236  *    Revision 1.17  2001/06/09 10:55:28  jongfoster
237  *    Changing BUFSIZ ==> BUFFER_SIZE
238  *
239  *    Revision 1.16  2001/06/07 23:10:26  jongfoster
240  *    Allowing unanchored domain patterns to back off and retry
241  *    if they partially match.  Optimized right-anchored patterns.
242  *    Moving ACL and forward files into config file.
243  *    Replacing struct gateway with struct forward_spec
244  *
245  *    Revision 1.15  2001/06/03 19:12:00  oes
246  *    extracted-CGI relevant stuff
247  *
248  *    Revision 1.14  2001/06/01 10:30:55  oes
249  *    Added optional left-anchoring to domaincmp
250  *
251  *    Revision 1.13  2001/05/31 21:21:30  jongfoster
252  *    Permissionsfile / actions file changes:
253  *    - Changed "permission" to "action" throughout
254  *    - changes to file format to allow string parameters
255  *    - Moved helper functions to actions.c
256  *
257  *    Revision 1.12  2001/05/31 17:35:20  oes
258  *
259  *     - Enhanced domain part globbing with infix and prefix asterisk
260  *       matching and optional unanchored operation
261  *
262  *    Revision 1.11  2001/05/29 11:53:23  oes
263  *    "See why" link added to "blocked" page
264  *
265  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
266  *    Unified blocklist/imagelist/permissionslist.
267  *    File format is still under discussion, but the internal changes
268  *    are (mostly) done.
269  *
270  *    Also modified interceptor behaviour:
271  *    - We now intercept all URLs beginning with one of the following
272  *      prefixes (and *only* these prefixes):
273  *        * http://i.j.b/
274  *        * http://ijbswa.sf.net/config/
275  *        * http://ijbswa.sourceforge.net/config/
276  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
277  *    - Internal changes so that intercepted and fast redirect pages
278  *      are not replaced with an image.
279  *    - Interceptors now have the option to send a binary page direct
280  *      to the client. (i.e. ijb-send-banner uses this)
281  *    - Implemented show-url-info interceptor.  (Which is why I needed
282  *      the above interceptors changes - a typical URL is
283  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
284  *      The previous mechanism would not have intercepted that, and
285  *      if it had been intercepted then it then it would have replaced
286  *      it with an image.)
287  *
288  *    Revision 1.9  2001/05/27 22:17:04  oes
289  *
290  *    - re_process_buffer no longer writes the modified buffer
291  *      to the client, which was very ugly. It now returns the
292  *      buffer, which it is then written by chat.
293  *
294  *    - content_length now adjusts the Content-Length: header
295  *      for modified documents rather than crunch()ing it.
296  *      (Length info in csp->content_length, which is 0 for
297  *      unmodified documents)
298  *
299  *    - For this to work, sed() is called twice when filtering.
300  *
301  *    Revision 1.8  2001/05/26 17:13:28  jongfoster
302  *    Filled in a function comment.
303  *
304  *    Revision 1.7  2001/05/26 15:26:15  jongfoster
305  *    ACL feature now provides more security by immediately dropping
306  *    connections from untrusted hosts.
307  *
308  *    Revision 1.6  2001/05/26 00:28:36  jongfoster
309  *    Automatic reloading of config file.
310  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
311  *    Most of the global variables have been moved to a new
312  *    struct configuration_spec, accessed through csp->config->globalname
313  *    Most of the globals remaining are used by the Win32 GUI.
314  *
315  *    Revision 1.5  2001/05/25 22:34:30  jongfoster
316  *    Hard tabs->Spaces
317  *
318  *    Revision 1.4  2001/05/22 18:46:04  oes
319  *
320  *    - Enabled filtering banners by size rather than URL
321  *      by adding patterns that replace all standard banner
322  *      sizes with the "Junkbuster" gif to the re_filterfile
323  *
324  *    - Enabled filtering WebBugs by providing a pattern
325  *      which kills all 1x1 images
326  *
327  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
328  *      which is selected by the (nonstandard and therefore
329  *      capital) letter 'U' in the option string.
330  *      It causes the quantifiers to be ungreedy by default.
331  *      Appending a ? turns back to greedy (!).
332  *
333  *    - Added a new interceptor ijb-send-banner, which
334  *      sends back the "Junkbuster" gif. Without imagelist or
335  *      MSIE detection support, or if tinygif = 1, or the
336  *      URL isn't recognized as an imageurl, a lame HTML
337  *      explanation is sent instead.
338  *
339  *    - Added new feature, which permits blocking remote
340  *      script redirects and firing back a local redirect
341  *      to the browser.
342  *      The feature is conditionally compiled, i.e. it
343  *      can be disabled with --disable-fast-redirects,
344  *      plus it must be activated by a "fast-redirects"
345  *      line in the config file, has its own log level
346  *      and of course wants to be displayed by show-proxy-args
347  *      Note: Boy, all the #ifdefs in 1001 locations and
348  *      all the fumbling with configure.in and acconfig.h
349  *      were *way* more work than the feature itself :-(
350  *
351  *    - Because a generic redirect template was needed for
352  *      this, tinygif = 3 now uses the same.
353  *
354  *    - Moved GIFs, and other static HTTP response templates
355  *      to project.h
356  *
357  *    - Some minor fixes
358  *
359  *    - Removed some >400 CRs again (Jon, you really worked
360  *      a lot! ;-)
361  *
362  *    Revision 1.3  2001/05/20 16:44:47  jongfoster
363  *    Removing last hardcoded JunkBusters.com URLs.
364  *
365  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
366  *    Version 2.9.4 checkin.
367  *    - Merged popupfile and cookiefile, and added control over PCRS
368  *      filtering, in new "permissionsfile".
369  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
370  *      file error you now get a message box (in the Win32 GUI) rather
371  *      than the program exiting with no explanation.
372  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
373  *      skipping.
374  *    - Removed tabs from "config"
375  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
376  *    - Bumped up version number.
377  *
378  *    Revision 1.1.1.1  2001/05/15 13:58:52  oes
379  *    Initial import of version 2.9.3 source tree
380  *
381  *
382  *********************************************************************/
383 \f
384
385 #include "config.h"
386
387 #include <stdio.h>
388 #include <sys/types.h>
389 #include <stdlib.h>
390 #include <ctype.h>
391 #include <string.h>
392 #include <assert.h>
393
394 #ifndef _WIN32
395 #ifndef __OS2__
396 #include <unistd.h>
397 #endif /* ndef __OS2__ */
398 #include <netinet/in.h>
399 #else
400 #include <winsock2.h>
401 #endif /* ndef _WIN32 */
402
403 #ifdef __OS2__
404 #include <utils.h>
405 #endif /* def __OS2__ */
406
407 #include "project.h"
408 #include "filters.h"
409 #include "encode.h"
410 #include "parsers.h"
411 #include "ssplit.h"
412 #include "errlog.h"
413 #include "jbsockets.h"
414 #include "miscutil.h"
415 #include "actions.h"
416 #include "cgi.h"
417 #include "list.h"
418 #include "deanimate.h"
419 #include "urlmatch.h"
420
421 #ifdef _WIN32
422 #include "win32.h"
423 #endif
424
425 const char filters_h_rcs[] = FILTERS_H_VERSION;
426
427 /* Fix a problem with Solaris.  There should be no effect on other
428  * platforms.
429  * Solaris's isspace() is a macro which uses it's argument directly
430  * as an array index.  Therefore we need to make sure that high-bit
431  * characters generate +ve values, and ideally we also want to make
432  * the argument match the declared parameter type of "int".
433  */
434 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
435
436
437 #ifdef FEATURE_ACL
438 /*********************************************************************
439  *
440  * Function    :  block_acl
441  *
442  * Description :  Block this request?
443  *                Decide yes or no based on ACL file.
444  *
445  * Parameters  :
446  *          1  :  dst = The proxy or gateway address this is going to.
447  *                      Or NULL to check all possible targets.
448  *          2  :  csp = Current client state (buffers, headers, etc...)
449  *                      Also includes the client IP address.
450  *
451  * Returns     : 0 = FALSE (don't block) and 1 = TRUE (do block)
452  *
453  *********************************************************************/
454 int block_acl(struct access_control_addr *dst, struct client_state *csp)
455 {
456    struct access_control_list *acl = csp->config->acl;
457
458    /* if not using an access control list, then permit the connection */
459    if (acl == NULL)
460    {
461       return(0);
462    }
463
464    /* search the list */
465    while (acl != NULL)
466    {
467       if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
468       {
469          if (dst == NULL)
470          {
471             /* Just want to check if they have any access */
472             if (acl->action == ACL_PERMIT)
473             {
474                return(0);
475             }
476          }
477          else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
478            && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
479          {
480             if (acl->action == ACL_PERMIT)
481             {
482                return(0);
483             }
484             else
485             {
486                return(1);
487             }
488          }
489       }
490       acl = acl->next;
491    }
492
493    return(1);
494
495 }
496
497
498 /*********************************************************************
499  *
500  * Function    :  acl_addr
501  *
502  * Description :  Called from `load_config' to parse an ACL address.
503  *
504  * Parameters  :
505  *          1  :  aspec = String specifying ACL address.
506  *          2  :  aca = struct access_control_addr to fill in.
507  *
508  * Returns     :  0 => Ok, everything else is an error.
509  *
510  *********************************************************************/
511 int acl_addr(char *aspec, struct access_control_addr *aca)
512 {
513    int i, masklength, port;
514    char *p;
515
516    masklength = 32;
517    port       =  0;
518
519    if ((p = strchr(aspec, '/')) != NULL)
520    {
521       *p++ = '\0';
522
523       if (ijb_isdigit(*p) == 0)
524       {
525          return(-1);
526       }
527       masklength = atoi(p);
528    }
529
530    if ((masklength < 0) || (masklength > 32))
531    {
532       return(-1);
533    }
534
535    if ((p = strchr(aspec, ':')) != NULL)
536    {
537       *p++ = '\0';
538
539       if (ijb_isdigit(*p) == 0)
540       {
541          return(-1);
542       }
543       port = atoi(p);
544    }
545
546    aca->port = port;
547
548    aca->addr = ntohl(resolve_hostname_to_ip(aspec));
549
550    if (aca->addr == INADDR_NONE)
551    {
552       return(-1);
553    }
554
555    /* build the netmask */
556    aca->mask = 0;
557    for (i=1; i <= masklength ; i++)
558    {
559       aca->mask |= (1 << (32 - i));
560    }
561
562    /* now mask off the host portion of the ip address
563     * (i.e. save on the network portion of the address).
564     */
565    aca->addr = aca->addr & aca->mask;
566
567    return(0);
568
569 }
570 #endif /* def FEATURE_ACL */
571
572
573 /*********************************************************************
574  *
575  * Function    :  match_portlist
576  *
577  * Description :  Check if a given number is covered by a comma
578  *                separated list of numbers and ranges (a,b-c,d,..)
579  *
580  * Parameters  :
581  *          1  :  portlist = String with list
582  *          2  :  port = port to check
583  *
584  * Returns     :  0 => no match
585  *                1 => match
586  *
587  *********************************************************************/
588 int match_portlist(const char *portlist, int port)
589 {
590    char *min, *max, *next, *portlist_copy;
591
592    min = next = portlist_copy = strdup(portlist);
593
594    /*
595     * Zero-terminate first item and remember offset for next
596     */
597    if (NULL != (next = strchr(portlist_copy, (int) ',')))
598    {
599       *next++ = '\0';
600    }
601
602    /*
603     * Loop through all items, checking for match
604     */
605    while(min)
606    {
607       if (NULL == (max = strchr(min, (int) '-')))
608       {
609          /*
610           * No dash, check for equality
611           */
612          if (port == atoi(min))
613          {
614             free(portlist_copy);
615             return(1);
616          }
617       }
618       else
619       {
620          /*
621           * This is a range, so check if between min and max,
622           * or, if max was omitted, between min and 65K
623           */
624          *max++ = '\0';
625          if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
626          {
627             free(portlist_copy);
628             return(1);
629          }
630
631       }
632
633       /*
634        * Jump to next item
635        */
636       min = next;
637
638       /*
639        * Zero-terminate next item and remember offset for n+1
640        */
641       if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
642       {
643          *next++ = '\0';
644       }
645    }
646
647    free(portlist_copy);
648    return 0;
649
650 }
651
652
653 /*********************************************************************
654  *
655  * Function    :  block_url
656  *
657  * Description :  Called from `chat'.  Check to see if we need to block this.
658  *
659  * Parameters  :
660  *          1  :  csp = Current client state (buffers, headers, etc...)
661  *
662  * Returns     :  NULL => unblocked, else HTTP block response
663  *
664  *********************************************************************/
665 struct http_response *block_url(struct client_state *csp)
666 {
667 #ifdef FEATURE_IMAGE_BLOCKING
668    char *p;
669 #endif /* def FEATURE_IMAGE_BLOCKING */
670    struct http_response *rsp;
671
672    /*
673     * If it's not blocked, don't block it ;-)
674     */
675    if ((csp->action->flags & ACTION_BLOCK) == 0)
676    {
677       return NULL;
678    }
679
680    /*
681     * Else, prepare a response
682     */
683    if (NULL == (rsp = alloc_http_response()))
684    {
685       return cgi_error_memory();
686    }
687
688    /*
689     * If it's an image-url, send back an image or redirect
690     * as specified by the relevant +image action
691     */
692 #ifdef FEATURE_IMAGE_BLOCKING
693    if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
694         && is_imageurl(csp))
695    {
696       /* determine HOW images should be blocked */
697       p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
698
699 #if 1 /* Two alternative strategies, use this one for now: */
700
701       /* and handle accordingly: */
702       if ((p == NULL) || (0 == strcmpic(p, "pattern")))
703       {
704          rsp->body = bindup(image_pattern_data, image_pattern_length);
705          if (rsp->body == NULL)
706          {
707             free_http_response(rsp);
708             return cgi_error_memory();
709          }
710          rsp->content_length = image_pattern_length;
711
712          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
713          {
714             free_http_response(rsp);
715             return cgi_error_memory();
716          }
717       }
718
719       else if (0 == strcmpic(p, "blank"))
720       {
721          rsp->body = bindup(image_blank_data, image_blank_length);
722          if (rsp->body == NULL)
723          {
724             free_http_response(rsp);
725             return cgi_error_memory();
726          }
727          rsp->content_length = image_blank_length;
728
729          if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE))
730          {
731             free_http_response(rsp);
732             return cgi_error_memory();
733          }
734       }
735
736       else
737       {
738          rsp->status = strdup("302 Local Redirect from Privoxy");
739          if (rsp->status == NULL)
740          {
741             free_http_response(rsp);
742             return cgi_error_memory();
743          }
744
745          if (enlist_unique_header(rsp->headers, "Location", p))
746          {
747             free_http_response(rsp);
748             return cgi_error_memory();
749          }
750       }
751
752 #else /* Following code is disabled for now */
753
754       /* and handle accordingly: */
755       if ((p == NULL) || (0 == strcmpic(p, "pattern")))
756       {
757          p = CGI_PREFIX "send-banner?type=pattern";
758       }
759       else if (0 == strcmpic(p, "blank"))
760       {
761          p = CGI_PREFIX "send-banner?type=blank";
762       }
763       rsp->status = strdup("302 Local Redirect from Privoxy");
764       if (rsp->status == NULL)
765       {
766          free_http_response(rsp);
767          return cgi_error_memory();
768       }
769
770       if (enlist_unique_header(rsp->headers, "Location", p))
771       {
772          free_http_response(rsp);
773          return cgi_error_memory();
774       }
775 #endif /* Preceeding code is disabled for now */
776    }
777    else
778 #endif /* def FEATURE_IMAGE_BLOCKING */
779
780    /*
781     * Else, generate an HTML "blocked" message:
782     */
783    {
784       jb_err err;
785       struct map * exports;
786
787       /*
788        * Workaround for stupid Netscape bug which prevents
789        * pages from being displayed if loading a referenced
790        * JavaScript or style sheet fails. So make it appear
791        * as if it succeeded.
792        */
793       if ( NULL != (p = get_header_value(csp->headers, "User-Agent:"))
794            && !strncmpic(p, "mozilla", 7) /* Catch Netscape but */
795            && !strstr(p, "Gecko")         /* save Mozilla, */
796            && !strstr(p, "compatible")    /* MSIE */
797            && !strstr(p, "Opera"))        /* and Opera. */
798       {
799          rsp->status = strdup("200 Request for blocked URL");
800       }
801       else
802       {
803          rsp->status = strdup("404 Request for blocked URL");
804       }
805
806       if (rsp->status == NULL)
807       {
808          free_http_response(rsp);
809          return cgi_error_memory();
810       }
811
812       exports = default_exports(csp, NULL);
813       if (exports == NULL)
814       {
815          free_http_response(rsp);
816          return cgi_error_memory();
817       }
818
819 #ifdef FEATURE_FORCE_LOAD
820       err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
821 #else /* ifndef FEATURE_FORCE_LOAD */
822       err = map_block_killer(exports, "force-support");
823 #endif /* ndef FEATURE_FORCE_LOAD */
824
825       if (!err) err = map(exports, "hostport", 1, html_encode(csp->http->hostport), 0);
826       if (!err) err = map(exports, "path", 1, html_encode(csp->http->path), 0);
827
828       if (err)
829       {
830          free_map(exports);
831          free_http_response(rsp);
832          return cgi_error_memory();
833       }
834
835       err = template_fill_for_cgi(csp, "blocked", exports, rsp);
836       if (err)
837       {
838          free_http_response(rsp);
839          return cgi_error_memory();
840       }
841    }
842
843    return finish_http_response(rsp);
844
845 }
846
847
848 #ifdef FEATURE_TRUST
849 /*********************************************************************
850  *
851  * Function    :  trust_url FIXME: I should be called distrust_url
852  *
853  * Description :  Calls is_untrusted_url to determine if the URL is trusted
854  *                and if not, returns a HTTP 304 response with a reject message.
855  *
856  * Parameters  :
857  *          1  :  csp = Current client state (buffers, headers, etc...)
858  *
859  * Returns     :  NULL => trusted, else http_response.
860  *
861  *********************************************************************/
862 struct http_response *trust_url(struct client_state *csp)
863 {
864    struct http_response *rsp;
865    struct map * exports;
866    char buf[BUFFER_SIZE];
867    char *p;
868    struct url_spec **tl;
869    struct url_spec *t;
870    jb_err err;
871
872    /*
873     * Don't bother to work on trusted URLs
874     */
875    if (!is_untrusted_url(csp))
876    {
877       return NULL;
878    }
879
880    /*
881     * Else, prepare a response:
882     */
883    if (NULL == (rsp = alloc_http_response()))
884    {
885       return cgi_error_memory();
886    }
887
888    exports = default_exports(csp, NULL);
889    if (exports == NULL)
890    {
891       free_http_response(rsp);
892       return cgi_error_memory();
893    }
894
895    /*
896     * Export the host, port, and referrer information
897     */
898    err = map(exports, "hostport", 1, csp->http->hostport, 1);
899    if (!err) err = map(exports, "path", 1, csp->http->path, 1);
900
901    if (NULL != (p = get_header_value(csp->headers, "Referer:")))
902    {
903       if (!err) err = map(exports, "referrer", 1, html_encode(p), 0);
904    }
905    else
906    {
907       if (!err) err = map(exports, "referrer", 1, "unknown", 1);
908    }
909
910    if (err)
911    {
912       free_map(exports);
913       free_http_response(rsp);
914       return cgi_error_memory();
915    }
916
917    /*
918     * Export the trust list
919     */
920    p = strdup("");
921    for (tl = csp->config->trust_list; (t = *tl) != NULL ; tl++)
922    {
923       sprintf(buf, "<li>%s</li>\n", t->spec);
924       string_append(&p, buf);
925    }
926    err = map(exports, "trusted-referrers", 1, p, 0);
927
928    if (err)
929    {
930       free_map(exports);
931       free_http_response(rsp);
932       return cgi_error_memory();
933    }
934
935    /*
936     * Export the trust info, if available
937     */
938    if (csp->config->trust_info->first)
939    {
940       struct list_entry *l;
941
942       p = strdup("");
943       for (l = csp->config->trust_info->first; l ; l = l->next)
944       {
945          sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
946          string_append(&p, buf);
947       }
948       err = map(exports, "trust-info", 1, p, 0);
949    }
950    else
951    {
952       err = map_block_killer(exports, "have-trust-info");
953    }
954
955    if (err)
956    {
957       free_map(exports);
958       free_http_response(rsp);
959       return cgi_error_memory();
960    }
961
962    /*
963     * Export the force prefix or the force conditional block killer
964     */
965 #ifdef FEATURE_FORCE_LOAD
966    err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
967 #else /* ifndef FEATURE_FORCE_LOAD */
968    err = map_block_killer(exports, "force-support");
969 #endif /* ndef FEATURE_FORCE_LOAD */
970
971    if (err)
972    {
973       free_map(exports);
974       free_http_response(rsp);
975       return cgi_error_memory();
976    }
977
978    /*
979     * Build the response
980     */
981    err = template_fill_for_cgi(csp, "untrusted", exports, rsp);
982    if (err)
983    {
984       free_http_response(rsp);
985       return cgi_error_memory();
986    }
987
988    return finish_http_response(rsp);
989 }
990 #endif /* def FEATURE_TRUST */
991
992
993 #ifdef FEATURE_FAST_REDIRECTS
994 /*********************************************************************
995  *
996  * Function    :  redirect_url
997  *
998  * Description :  Checks for redirection URLs and returns a HTTP redirect
999  *                to the destination URL, if necessary
1000  *
1001  * Parameters  :
1002  *          1  :  csp = Current client state (buffers, headers, etc...)
1003  *
1004  * Returns     :  NULL if URL was clean, HTTP redirect otherwise.
1005  *
1006  *********************************************************************/
1007 struct http_response *redirect_url(struct client_state *csp)
1008 {
1009    char *p, *q;
1010    struct http_response *rsp;
1011
1012    p = q = csp->http->path;
1013    log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
1014
1015    /*
1016     * find the last URL encoded in the request
1017     */
1018    while ((p = strstr(p, "http://")) != NULL)
1019    {
1020       q = p++;
1021    }
1022
1023    /*
1024     * if there was any, generate and return a HTTP redirect
1025     */
1026    if (q != csp->http->path)
1027    {
1028       log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
1029
1030       if (NULL == (rsp = alloc_http_response()))
1031       {
1032          return cgi_error_memory();
1033       }
1034
1035       if ( enlist_unique_header(rsp->headers, "Location", q)
1036         || (NULL == (rsp->status = strdup("302 Local Redirect from Privoxy"))) )
1037       {
1038          free_http_response(rsp);
1039          return cgi_error_memory();
1040       }
1041
1042       return finish_http_response(rsp);
1043    }
1044    else
1045    {
1046       return NULL;
1047    }
1048
1049 }
1050 #endif /* def FEATURE_FAST_REDIRECTS */
1051
1052
1053 #ifdef FEATURE_IMAGE_BLOCKING
1054 /*********************************************************************
1055  *
1056  * Function    :  is_imageurl
1057  *
1058  * Description :  Given a URL, decide whether it is an image or not,
1059  *                using either the info from a previous +image action
1060  *                or, #ifdef FEATURE_IMAGE_DETECT_MSIE, the info from
1061  *                the browser's accept header.
1062  *
1063  * Parameters  :
1064  *          1  :  csp = Current client state (buffers, headers, etc...)
1065  *
1066  * Returns     :  True (nonzero) if URL is an image, false (0)
1067  *                otherwise
1068  *
1069  *********************************************************************/
1070 int is_imageurl(struct client_state *csp)
1071 {
1072 #ifdef FEATURE_IMAGE_DETECT_MSIE
1073    char *tmp;
1074
1075    tmp = get_header_value(csp->headers, "User-Agent:");
1076    if (tmp && strstr(tmp, "MSIE"))
1077    {
1078       tmp = get_header_value(csp->headers, "Accept:");
1079       if (tmp && strstr(tmp, "image/gif"))
1080       {
1081          /* Client will accept HTML.  If this seems counterintuitive,
1082           * blame Microsoft.
1083           */
1084          return(0);
1085       }
1086       else
1087       {
1088          return(1);
1089       }
1090    }
1091 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1092
1093    return ((csp->action->flags & ACTION_IMAGE) != 0);
1094
1095 }
1096 #endif /* def FEATURE_IMAGE_BLOCKING */
1097
1098
1099 #ifdef FEATURE_TRUST
1100 /*********************************************************************
1101  *
1102  * Function    :  is_untrusted_url
1103  *
1104  * Description :  Should we "distrust" this URL (and block it)?
1105  *
1106  *                Yes if it matches a line in the trustfile, or if the
1107  *                    referrer matches a line starting with "+" in the
1108  *                    trustfile.
1109  *                No  otherwise.
1110  *
1111  * Parameters  :
1112  *          1  :  csp = Current client state (buffers, headers, etc...)
1113  *
1114  * Returns     :  0 => trusted, 1 => untrusted
1115  *
1116  *********************************************************************/
1117 int is_untrusted_url(struct client_state *csp)
1118 {
1119    struct file_list *fl;
1120    struct block_spec *b;
1121    struct url_spec **trusted_url;
1122    struct http_request rhttp[1];
1123    const char * referer;
1124    jb_err err;
1125
1126    /*
1127     * If we don't have a trustlist, we trust everybody
1128     */
1129    if (((fl = csp->tlist) == NULL) || ((b  = fl->f) == NULL))
1130    {
1131       return 0;
1132    }
1133
1134    memset(rhttp, '\0', sizeof(*rhttp));
1135
1136    /*
1137     * Do we trust the request URL itself?
1138     */
1139    for (b = b->next; b ; b = b->next)
1140    {
1141       if (url_match(b->url, csp->http))
1142       {
1143          return b->reject;
1144       }
1145    }
1146
1147    if (NULL == (referer = get_header_value(csp->headers, "Referer:")))
1148    {
1149       /* no referrer was supplied */
1150       return 1;
1151    }
1152
1153
1154    /*
1155     * If not, do we maybe trust its referrer?
1156     */
1157    err = parse_http_url(referer, rhttp, csp);
1158    if (err)
1159    {
1160       return 1;
1161    }
1162
1163    for (trusted_url = csp->config->trust_list; *trusted_url != NULL; trusted_url++)
1164    {
1165       if (url_match(*trusted_url, rhttp))
1166       {
1167          /* if the URL's referrer is from a trusted referrer, then
1168           * add the target spec to the trustfile as an unblocked
1169           * domain and return NULL (which means it's OK).
1170           */
1171
1172          FILE *fp;
1173
1174          if (NULL != (fp = fopen(csp->config->trustfile, "a")))
1175          {
1176             char * path;
1177             char * path_end;
1178             char * new_entry = strdup("~");
1179
1180             string_append(&new_entry, csp->http->hostport);
1181
1182             path = csp->http->path;
1183             if ( (path[0] == '/')
1184               && (path[1] == '~')
1185               && ((path_end = strchr(path + 2, '/')) != NULL))
1186             {
1187                /* since this path points into a user's home space
1188                 * be sure to include this spec in the trustfile.
1189                 */
1190                int path_len = path_end - path; /* save offset */
1191                path = strdup(path); /* Copy string */
1192                if (path != NULL)
1193                {
1194                   path_end = path + path_len; /* regenerate ptr to new buffer */
1195                   *(path_end + 1) = '\0'; /* Truncate path after '/' */
1196                }
1197                string_join(&new_entry, path);
1198             }
1199
1200             if (new_entry != NULL)
1201             {
1202                fprintf(fp, "%s\n", new_entry);
1203                free(new_entry);
1204             }
1205             else
1206             {
1207                /* FIXME: No way to handle out-of memory, so mostly ignoring it */
1208                log_error(LOG_LEVEL_ERROR, "Out of memory adding pattern to trust file");
1209             }
1210
1211             fclose(fp);
1212          }
1213          return 0;
1214       }
1215    }
1216    return 1;
1217 }
1218 #endif /* def FEATURE_TRUST */
1219
1220
1221 /*********************************************************************
1222  *
1223  * Function    :  pcrs_filter_response
1224  *
1225  * Description :  Ecexute all text substitutions from all applying
1226  *                +filter actions on the text buffer that's been accumulated
1227  *                in csp->iob->buf. If this changes the contents, set
1228  *                csp->content_length to the modified size and raise the
1229  *                CSP_FLAG_MODIFIED flag.
1230  *
1231  * Parameters  :
1232  *          1  :  csp = Current client state (buffers, headers, etc...)
1233  *
1234  * Returns     :  a pointer to the (newly allocated) modified buffer.
1235  *                or NULL if there were no hits or something went wrong
1236  *
1237  *********************************************************************/
1238 char *pcrs_filter_response(struct client_state *csp)
1239 {
1240    int hits=0;
1241    size_t size;
1242
1243    char *old = csp->iob->cur, *new = NULL;
1244    pcrs_job *job;
1245
1246    struct file_list *fl;
1247    struct re_filterfile_spec *b;
1248    struct list_entry *filtername;
1249
1250    /* 
1251     * Sanity first
1252     */
1253    if (csp->iob->cur >= csp->iob->eod)
1254    {
1255       return(NULL);
1256    }
1257    size = csp->iob->eod - csp->iob->cur;
1258
1259    if ( ( NULL == (fl = csp->rlist) ) || ( NULL == fl->f) )
1260    {
1261       log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1262       return(NULL);
1263    }
1264
1265    /*
1266     * If the body has a "chunked" transfer-encoding,
1267     * get rid of it first, adjusting size and iob->eod
1268     */
1269    if (csp->flags & CSP_FLAG_CHUNKED)
1270    {
1271       log_error(LOG_LEVEL_RE_FILTER, "Need to de-chunk first");
1272       if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1273       {
1274          return(NULL);
1275       }
1276       csp->iob->eod = csp->iob->cur + size;
1277       csp->flags |= CSP_FLAG_MODIFIED;
1278    }
1279
1280    /*
1281     * For all applying +filter actions, look if a filter by that
1282     * name exists and if yes, execute it's pcrs_joblist on the
1283     * buffer.
1284     */
1285    for (b = fl->f; b; b = b->next)
1286    {
1287       for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
1288            filtername ; filtername = filtername->next)
1289       {
1290          if (strcmp(b->name, filtername->str) == 0)
1291          {
1292             int current_hits = 0;
1293
1294             if ( NULL == b->joblist )
1295             {
1296                log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1297                return(NULL);
1298             }
1299
1300             log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) with filter %s...",
1301                       csp->http->hostport, csp->http->path, size, b->name);
1302
1303             /* Apply all jobs from the joblist */
1304             for (job = b->joblist; NULL != job; job = job->next)
1305             {
1306                current_hits += pcrs_execute(job, old, size, &new, &size);
1307                if (old != csp->iob->cur) free(old);
1308                old=new;
1309             }
1310
1311             log_error(LOG_LEVEL_RE_FILTER, " ...produced %d hits (new size %d).", current_hits, size);
1312             hits += current_hits;
1313          }
1314       }
1315    }
1316
1317    /*
1318     * If there were no hits, destroy our copy and let
1319     * chat() use the original in csp->iob
1320     */
1321    if (!hits)
1322    {
1323       free(new);
1324       return(NULL);
1325    }
1326
1327    csp->flags |= CSP_FLAG_MODIFIED;
1328    csp->content_length = size;
1329    IOB_RESET(csp);
1330
1331    return(new);
1332
1333 }
1334
1335
1336 /*********************************************************************
1337  *
1338  * Function    :  gif_deanimate_response
1339  *
1340  * Description :  Deanimate the GIF image that has been accumulated in
1341  *                csp->iob->buf, set csp->content_length to the modified
1342  *                size and raise the CSP_FLAG_MODIFIED flag.
1343  *
1344  * Parameters  :
1345  *          1  :  csp = Current client state (buffers, headers, etc...)
1346  *
1347  * Returns     :  a pointer to the (newly allocated) modified buffer.
1348  *                or NULL in case something went wrong.
1349  *
1350  *********************************************************************/
1351 char *gif_deanimate_response(struct client_state *csp)
1352 {
1353    struct binbuffer *in, *out;
1354    char *p;
1355    size_t size = csp->iob->eod - csp->iob->cur;
1356
1357    /*
1358     * If the body has a "chunked" transfer-encoding,
1359     * get rid of it first, adjusting size and iob->eod
1360     */
1361    if (csp->flags & CSP_FLAG_CHUNKED)
1362    {
1363       log_error(LOG_LEVEL_DEANIMATE, "Need to de-chunk first");
1364       if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1365       {
1366          return(NULL);
1367       }
1368       csp->iob->eod = csp->iob->cur + size;
1369       csp->flags |= CSP_FLAG_MODIFIED;
1370    }
1371
1372    if (  (NULL == (in =  (struct binbuffer *)zalloc(sizeof *in )))
1373       || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1374    {
1375       log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1376       return NULL;
1377    }
1378
1379    in->buffer = csp->iob->cur;
1380    in->size = size;
1381
1382    if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1383    {
1384       log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1385       free(in);
1386       buf_free(out);
1387       return(NULL);
1388    }
1389    else
1390    {
1391       if ((int)size == out->offset)
1392       {
1393          log_error(LOG_LEVEL_DEANIMATE, "GIF not changed.");
1394       }
1395       else
1396       {
1397          log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1398       }
1399       csp->content_length = out->offset;
1400       csp->flags |= CSP_FLAG_MODIFIED;
1401       p = out->buffer;
1402       free(in);
1403       free(out);
1404       return(p);
1405    }
1406
1407 }
1408
1409
1410 /*********************************************************************
1411  *
1412  * Function    :  remove_chunked_transfer_coding
1413  *
1414  * Description :  In-situ remove the "chunked" transfer coding as defined
1415  *                in rfc2616 from a buffer.
1416  *
1417  * Parameters  :
1418  *          1  :  buffer = Pointer to the text buffer
1419  *          2  :  size = Number of bytes to be processed
1420  *
1421  * Returns     :  The new size, i.e. the number of bytes from buffer which
1422  *                are occupied by the stripped body, or 0 in case something
1423  *                went wrong
1424  *
1425  *********************************************************************/
1426 int remove_chunked_transfer_coding(char *buffer, const size_t size)
1427 {
1428    size_t newsize = 0;
1429    unsigned int chunksize = 0;
1430    char *from_p, *to_p;
1431
1432    assert(buffer);
1433    from_p = to_p = buffer;
1434
1435    if (sscanf(buffer, "%x", &chunksize) != 1)
1436    {
1437       log_error(LOG_LEVEL_ERROR, "Invalid first chunksize while stripping \"chunked\" transfer coding");
1438       return(0);
1439    }
1440
1441    while (chunksize > 0)
1442    {
1443       if (NULL == (from_p = strstr(from_p, "\r\n")))
1444       {
1445          log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1446          return(0);
1447       }
1448       newsize += chunksize;
1449       from_p += 2;
1450
1451       memmove(to_p, from_p, (size_t) chunksize);
1452       to_p = buffer + newsize;
1453       from_p += chunksize + 2;
1454
1455       if (sscanf(from_p, "%x", &chunksize) != 1)
1456       {
1457          log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1458          return(0);
1459       }
1460    }
1461
1462    /* FIXME: Should this get its own loglevel? */
1463    log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d\n", size, newsize);
1464    return(newsize);
1465
1466 }
1467
1468
1469 /*********************************************************************
1470  *
1471  * Function    :  url_actions
1472  *
1473  * Description :  Gets the actions for this URL.
1474  *
1475  * Parameters  :
1476  *          1  :  http = http_request request for blocked URLs
1477  *          2  :  csp = Current client state (buffers, headers, etc...)
1478  *
1479  * Returns     :  N/A
1480  *
1481  *********************************************************************/
1482 void url_actions(struct http_request *http,
1483                  struct client_state *csp)
1484 {
1485    struct file_list *fl;
1486    struct url_actions *b;
1487
1488    init_current_action(csp->action);
1489
1490    if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1491    {
1492       return;
1493    }
1494
1495    apply_url_actions(csp->action, http, b);
1496
1497 }
1498
1499
1500 /*********************************************************************
1501  *
1502  * Function    :  apply_url_actions
1503  *
1504  * Description :  Applies a list of URL actions.
1505  *
1506  * Parameters  :
1507  *          1  :  action = Destination.
1508  *          2  :  http = Current URL
1509  *          3  :  b = list of URL actions to apply
1510  *
1511  * Returns     :  N/A
1512  *
1513  *********************************************************************/
1514 void apply_url_actions(struct current_action_spec *action,
1515                        struct http_request *http,
1516                        struct url_actions *b)
1517 {
1518    if (b == NULL)
1519    {
1520       /* Should never happen */
1521       return;
1522    }
1523
1524    for (b = b->next; NULL != b; b = b->next)
1525    {
1526       if (url_match(b->url, http))
1527       {
1528          merge_current_action(action, b->action);
1529       }
1530    }
1531 }
1532
1533
1534 /*********************************************************************
1535  *
1536  * Function    :  forward_url
1537  *
1538  * Description :  Should we forward this to another proxy?
1539  *
1540  * Parameters  :
1541  *          1  :  http = http_request request for current URL
1542  *          2  :  csp = Current client state (buffers, headers, etc...)
1543  *
1544  * Returns     :  Pointer to forwarding information.
1545  *
1546  *********************************************************************/
1547 const struct forward_spec * forward_url(struct http_request *http,
1548                                         struct client_state *csp)
1549 {
1550    static const struct forward_spec fwd_default[1] = { FORWARD_SPEC_INITIALIZER };
1551    struct forward_spec *fwd = csp->config->forward;
1552
1553    if (fwd == NULL)
1554    {
1555       return fwd_default;
1556    }
1557
1558    while (fwd != NULL)
1559    {
1560       if (url_match(fwd->url, http))
1561       {
1562          return fwd;
1563       }
1564       fwd = fwd->next;
1565    }
1566
1567    return fwd_default;
1568 }
1569
1570
1571 /*
1572   Local Variables:
1573   tab-width: 3
1574   end:
1575 */