4b0e78e3ade6e6fb913d9e0d2fe7384ae2a38430
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.46 2007/01/02 12:49:46 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
5  *
6  * Purpose     :  Simple CGIs to get information about Privoxy's
7  *                status.
8  *                
9  *                Functions declared include:
10  * 
11  *
12  * Copyright   :  Written by and Copyright (C) 2001-2006 the SourceForge
13  *                Privoxy team. http://www.privoxy.org/
14  *
15  *                Based on the Internet Junkbuster originally written
16  *                by and Copyright (C) 1997 Anonymous Coders and 
17  *                Junkbusters Corporation.  http://www.junkbusters.com
18  *
19  *                This program is free software; you can redistribute it 
20  *                and/or modify it under the terms of the GNU General
21  *                Public License as published by the Free Software
22  *                Foundation; either version 2 of the License, or (at
23  *                your option) any later version.
24  *
25  *                This program is distributed in the hope that it will
26  *                be useful, but WITHOUT ANY WARRANTY; without even the
27  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
28  *                PARTICULAR PURPOSE.  See the GNU General Public
29  *                License for more details.
30  *
31  *                The GNU General Public License should be included with
32  *                this file.  If not, you can view it at
33  *                http://www.gnu.org/copyleft/gpl.html
34  *                or write to the Free Software Foundation, Inc., 59
35  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36  *
37  * Revisions   :
38  *    $Log: cgisimple.c,v $
39  *    Revision 1.46  2007/01/02 12:49:46  fabiankeil
40  *    Add FEATURE_ZLIB to the list of conditional
41  *    defines at the show-status page.
42  *
43  *    Revision 1.45  2006/12/28 18:16:41  fabiankeil
44  *    Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
45  *    body memory before using it, replaced sprintf calls with snprintf.
46  *
47  *    Revision 1.44  2006/12/22 14:19:27  fabiankeil
48  *    Removed checks whether or not AF_FILES have
49  *    data structures associated with them in cgi_show_status.
50  *    It doesn't matter as we're only interested in the file names.
51  *
52  *    For the action files the checks were always true,
53  *    but they prevented empty filter files from being
54  *    listed. Fixes parts of BR 1619208.
55  *
56  *    Revision 1.43  2006/12/17 17:57:56  fabiankeil
57  *    - Added FEATURE_GRACEFUL_TERMINATION to the
58  *      "conditional #defines" section
59  *    - Escaped ampersands in generated HTML.
60  *    - Renamed re-filter-filename to re-filter-filenames
61  *
62  *    Revision 1.42  2006/11/21 15:43:12  fabiankeil
63  *    Add special treatment for WIN32 to make sure
64  *    cgi_send_user_manual opens the files in binary mode.
65  *    Fixes BR 1600411 and unbreaks image delivery.
66  *
67  *    Remove outdated comment.
68  *
69  *    Revision 1.41  2006/10/09 19:18:28  roro
70  *    Redirect http://p.p/user-manual (without trailing slash) to
71  *    http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
72  *
73  *    Revision 1.40  2006/09/09 13:05:33  fabiankeil
74  *    Modified cgi_send_user_manual to serve binary
75  *    content without destroying it first. Should also be
76  *    faster now. Added ".jpg" check for Content-Type guessing.
77  *
78  *    Revision 1.39  2006/09/08 09:49:23  fabiankeil
79  *    Deliver documents in the user-manual directory
80  *    with "Content-Type text/css" if their filename
81  *    ends with ".css".
82  *
83  *    Revision 1.38  2006/09/06 18:45:03  fabiankeil
84  *    Incorporate modified version of Roland Rosenfeld's patch to
85  *    optionally access the user-manual via Privoxy. Closes patch 679075.
86  *
87  *    Formatting changed to Privoxy style, added call to
88  *    cgi_error_no_template if the requested file doesn't
89  *    exist and modified check whether or not Privoxy itself
90  *    should serve the manual. Should work cross-platform now.
91  *
92  *    Revision 1.37  2006/07/18 14:48:45  david__schmidt
93  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
94  *    with what was really the latest development (the v_3_0_branch branch)
95  *
96  *    Revision 1.35.2.7  2006/01/29 23:10:56  david__schmidt
97  *    Multiple filter file support
98  *
99  *    Revision 1.35.2.6  2005/07/04 03:13:43  david__schmidt
100  *    Undo some damaging memory leak patches
101  *
102  *    Revision 1.35.2.5  2005/05/07 21:50:55  david__schmidt
103  *    A few memory leaks plugged (mostly on error paths)
104  *
105  *    Revision 1.35.2.4  2005/04/04 02:21:24  david__schmidt
106  *    Another instance of:
107  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
108  *    Thanks to Magnus Holmgren for the patch
109  *
110  *    Revision 1.35.2.3  2003/12/17 16:34:15  oes
111  *     - Prevent line wrap beween "View/Edit" link buttons on status page
112  *     - Some (mostly irrelevant) fixes for Out-of-mem-case handling
113  *
114  *    Revision 1.35.2.2  2003/04/03 13:48:28  oes
115  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
116  *
117  *    Revision 1.35.2.1  2002/07/04 15:02:38  oes
118  *    Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
119  *
120  *    Revision 1.35.2.1  2002/07/01 17:32:04  morcego
121  *    Applying patch from Andreas as provided by Hal on the list.
122  *    Message-ID: <20020701121218.V1606@feenix.burgiss.net>
123  *
124  *    Revision 1.35  2002/05/12 21:44:44  jongfoster
125  *    Adding amiga.[ch] revision information, if on an amiga.
126  *
127  *    Revision 1.34  2002/04/30 12:06:12  oes
128  *    Deleted unused code from default_cgi
129  *
130  *    Revision 1.33  2002/04/30 11:14:52  oes
131  *    Made csp the first parameter in *action_to_html
132  *
133  *    Revision 1.32  2002/04/26 18:29:13  jongfoster
134  *    Fixing this Visual C++ warning:
135  *    cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
136  *
137  *    Revision 1.31  2002/04/26 12:54:36  oes
138  *     - Kill obsolete REDIRECT_URL code
139  *     - Error handling fixes
140  *     - Style sheet related HTML snipplet changes
141  *     - cgi_show_url_info:
142  *       - Matches now in table, actions on single lines,
143  *         linked to help
144  *       - standard.action suppressed
145  *       - Buttons to View and Edit AFs
146  *
147  *    Revision 1.30  2002/04/24 02:18:08  oes
148  *     - show-status is now the starting point for editing
149  *       the actions files, generate list of all AFs with buttons
150  *       for viewing and editing, new look for file list (Jon:
151  *       buttons now aligned ;-P ), view mode now supports multiple
152  *       AFs, name changes, no view links for unspecified files,
153  *       no edit link for standard.action.
154  *
155  *     - Jon's multiple AF patch: cgi_show_url_info now uses all
156  *       AFs and marks the output accordingly
157  *
158  *    Revision 1.29  2002/04/10 13:38:35  oes
159  *    load_template signature changed
160  *
161  *    Revision 1.28  2002/04/07 15:42:12  jongfoster
162  *    Fixing send-banner?type=auto when the image-blocker is
163  *    a redirect to send-banner
164  *
165  *    Revision 1.27  2002/04/05 15:50:48  oes
166  *    added send-stylesheet CGI
167  *
168  *    Revision 1.26  2002/04/04 00:36:36  gliptak
169  *    always use pcre for matching
170  *
171  *    Revision 1.25  2002/04/03 22:28:03  gliptak
172  *    Removed references to gnu_regex
173  *
174  *    Revision 1.24  2002/04/02 16:12:47  oes
175  *    Fix: moving misplaced lines into #ifdef FEATURE_FORCE
176  *
177  *    Revision 1.23  2002/03/26 22:29:54  swa
178  *    we have a new homepage!
179  *
180  *    Revision 1.22  2002/03/24 16:18:15  jongfoster
181  *    Removing old logo
182  *
183  *    Revision 1.21  2002/03/24 15:23:33  jongfoster
184  *    Name changes
185  *
186  *    Revision 1.20  2002/03/24 13:25:43  swa
187  *    name change related issues
188  *
189  *    Revision 1.19  2002/03/16 23:54:06  jongfoster
190  *    Adding graceful termination feature, to help look for memory leaks.
191  *    If you enable this (which, by design, has to be done by hand
192  *    editing config.h) and then go to http://i.j.b/die, then the program
193  *    will exit cleanly after the *next* request.  It should free all the
194  *    memory that was used.
195  *
196  *    Revision 1.18  2002/03/12 01:44:49  oes
197  *    Changed default for "blocked" image from jb logo to checkboard pattern
198  *
199  *    Revision 1.17  2002/03/08 16:43:18  oes
200  *    Added choice beween GIF and PNG built-in images
201  *
202  *    Revision 1.16  2002/03/07 03:48:38  oes
203  *     - Changed built-in images from GIF to PNG
204  *       (with regard to Unisys patent issue)
205  *     - Added a 4x4 pattern PNG which is less intrusive
206  *       than the logo but also clearly marks the deleted banners
207  *
208  *    Revision 1.15  2002/03/06 22:54:35  jongfoster
209  *    Automated function-comment nitpicking.
210  *
211  *    Revision 1.14  2002/03/02 04:14:50  david__schmidt
212  *    Clean up a little CRLF unpleasantness that suddenly appeared
213  *
214  *    Revision 1.13  2002/02/21 00:10:37  jongfoster
215  *    Adding send-banner?type=auto option
216  *
217  *    Revision 1.12  2002/01/23 01:03:32  jongfoster
218  *    Fixing gcc [CygWin] compiler warnings
219  *
220  *    Revision 1.11  2002/01/23 00:01:04  jongfoster
221  *    Adding cgi_transparent_gif() for http://i.j.b/t
222  *    Adding missing html_encode() to many CGI functions.
223  *    Adding urlmatch.[ch] to http://i.j.b/show-version
224  *
225  *    Revision 1.10  2002/01/17 21:10:37  jongfoster
226  *    Changes to cgi_show_url_info to use new matching code from urlmatch.c.
227  *    Also fixing a problem in the same function with improperly quoted URLs
228  *    in output HTML, and adding code to handle https:// URLs correctly.
229  *
230  *    Revision 1.9  2001/11/30 23:09:15  jongfoster
231  *    Now reports on FEATURE_CGI_EDIT_ACTIONS
232  *    Removing FEATURE_DENY_GZIP from template
233  *
234  *    Revision 1.8  2001/11/13 00:14:07  jongfoster
235  *    Fixing stupid bug now I've figured out what || means.
236  *    (It always returns 0 or 1, not one of it's paramaters.)
237  *
238  *    Revision 1.7  2001/10/23 21:48:19  jongfoster
239  *    Cleaning up error handling in CGI functions - they now send back
240  *    a HTML error page and should never cause a FATAL error.  (Fixes one
241  *    potential source of "denial of service" attacks).
242  *
243  *    CGI actions file editor that works and is actually useful.
244  *
245  *    Ability to toggle JunkBuster remotely using a CGI call.
246  *
247  *    You can turn off both the above features in the main configuration
248  *    file, e.g. if you are running a multi-user proxy.
249  *
250  *    Revision 1.6  2001/10/14 22:00:32  jongfoster
251  *    Adding support for a 404 error when an invalid CGI page is requested.
252  *
253  *    Revision 1.5  2001/10/07 15:30:41  oes
254  *    Removed FEATURE_DENY_GZIP
255  *
256  *    Revision 1.4  2001/10/02 15:31:12  oes
257  *    Introduced show-request cgi
258  *
259  *    Revision 1.3  2001/09/22 16:34:44  jongfoster
260  *    Removing unneeded #includes
261  *
262  *    Revision 1.2  2001/09/19 18:01:11  oes
263  *    Fixed comments; cosmetics
264  *
265  *    Revision 1.1  2001/09/16 17:08:54  jongfoster
266  *    Moving simple CGI functions from cgi.c to new file cgisimple.c
267  *
268  *
269  **********************************************************************/
270 \f
271
272 #include "config.h"
273
274 #include <stdio.h>
275 #include <sys/types.h>
276 #include <stdlib.h>
277 #include <ctype.h>
278 #include <string.h>
279 #include <assert.h>
280
281 #ifdef _WIN32
282 #define snprintf _snprintf
283 #endif /* def _WIN32 */
284
285 #include "project.h"
286 #include "cgi.h"
287 #include "cgisimple.h"
288 #include "list.h"
289 #include "encode.h"
290 #include "jcc.h"
291 #include "filters.h"
292 #include "actions.h"
293 #include "miscutil.h"
294 #include "loadcfg.h"
295 #include "parsers.h"
296 #include "urlmatch.h"
297 #include "errlog.h"
298
299 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
300
301
302 static char *show_rcs(void);
303 static jb_err show_defines(struct map *exports);
304
305
306 /*********************************************************************
307  *
308  * Function    :  cgi_default
309  *
310  * Description :  CGI function that is called for the CGI_SITE_1_HOST
311  *                and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
312  *                Boring - only exports the default exports.
313  *               
314  * Parameters  :
315  *          1  :  csp = Current client state (buffers, headers, etc...)
316  *          2  :  rsp = http_response data structure for output
317  *          3  :  parameters = map of cgi parameters
318  *
319  * CGI Parameters : none
320  *
321  * Returns     :  JB_ERR_OK on success
322  *                JB_ERR_MEMORY on out-of-memory
323  *
324  *********************************************************************/
325 jb_err cgi_default(struct client_state *csp,
326                    struct http_response *rsp,
327                    const struct map *parameters)
328 {
329    struct map *exports;
330
331    assert(csp);
332    assert(rsp);
333
334    if (NULL == (exports = default_exports(csp, "")))
335    {
336       return JB_ERR_MEMORY;
337    }
338
339    return template_fill_for_cgi(csp, "default", exports, rsp);
340 }
341
342
343 /*********************************************************************
344  *
345  * Function    :  cgi_error_404
346  *
347  * Description :  CGI function that is called if an unknown action was
348  *                given.
349  *               
350  * Parameters  :
351  *          1  :  csp = Current client state (buffers, headers, etc...)
352  *          2  :  rsp = http_response data structure for output
353  *          3  :  parameters = map of cgi parameters
354  *
355  * CGI Parameters : none
356  *
357  * Returns     :  JB_ERR_OK on success
358  *                JB_ERR_MEMORY on out-of-memory error.  
359  *
360  *********************************************************************/
361 jb_err cgi_error_404(struct client_state *csp,
362                      struct http_response *rsp,
363                      const struct map *parameters)
364 {
365    struct map *exports;
366
367    assert(csp);
368    assert(rsp);
369    assert(parameters);
370
371    if (NULL == (exports = default_exports(csp, NULL)))
372    {
373       return JB_ERR_MEMORY;
374    }
375
376    rsp->status = strdup("404 Privoxy configuration page not found");
377    if (rsp->status == NULL)
378    {
379       free_map(exports);
380       return JB_ERR_MEMORY;
381    }
382
383    return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
384 }
385
386
387 #ifdef FEATURE_GRACEFUL_TERMINATION
388 /*********************************************************************
389  *
390  * Function    :  cgi_die
391  *
392  * Description :  CGI function to shut down Privoxy.
393  *                NOTE: Turning this on in a production build
394  *                would be a BAD idea.  An EXTREMELY BAD idea.
395  *                In short, don't do it.
396  *               
397  * Parameters  :
398  *          1  :  csp = Current client state (buffers, headers, etc...)
399  *          2  :  rsp = http_response data structure for output
400  *          3  :  parameters = map of cgi parameters
401  *
402  * CGI Parameters : none
403  *
404  * Returns     :  JB_ERR_OK on success
405  *                JB_ERR_MEMORY on out-of-memory error.  
406  *
407  *********************************************************************/
408 jb_err cgi_die (struct client_state *csp,
409                 struct http_response *rsp,
410                 const struct map *parameters)
411 {
412    assert(csp);
413    assert(rsp);
414    assert(parameters);
415
416    /* quit */
417    g_terminate = 1;
418
419    /*
420     * I don't really care what gets sent back to the browser.
421     * Take the easy option - "out of memory" page.
422     */
423
424    return JB_ERR_MEMORY;
425 }
426 #endif /* def FEATURE_GRACEFUL_TERMINATION */
427
428
429 /*********************************************************************
430  *
431  * Function    :  cgi_show_request
432  *
433  * Description :  Show the client's request and what sed() would have
434  *                made of it.
435  *               
436  * Parameters  :
437  *          1  :  csp = Current client state (buffers, headers, etc...)
438  *          2  :  rsp = http_response data structure for output
439  *          3  :  parameters = map of cgi parameters
440  *
441  * CGI Parameters : none
442  *
443  * Returns     :  JB_ERR_OK on success
444  *                JB_ERR_MEMORY on out-of-memory error.  
445  *
446  *********************************************************************/
447 jb_err cgi_show_request(struct client_state *csp,
448                         struct http_response *rsp,
449                         const struct map *parameters)
450 {
451    char *p;
452    struct map *exports;
453
454    assert(csp);
455    assert(rsp);
456    assert(parameters);
457
458    if (NULL == (exports = default_exports(csp, "show-request")))
459    {
460       return JB_ERR_MEMORY;
461    }
462    
463    /*
464     * Repair the damage done to the IOB by get_header()
465     */
466    for (p = csp->iob->buf; p < csp->iob->eod; p++)
467    {
468       if (*p == '\0') *p = '\n';
469    }
470
471    /*
472     * Export the original client's request and the one we would
473     * be sending to the server if this wasn't a CGI call
474     */
475
476    if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
477    {
478       free_map(exports);
479       return JB_ERR_MEMORY;
480    }
481
482    if (map(exports, "processed-request", 1, html_encode_and_free_original(
483       sed(client_patterns, add_client_headers, csp)), 0))
484    {
485       free_map(exports);
486       return JB_ERR_MEMORY;
487    }
488
489    return template_fill_for_cgi(csp, "show-request", exports, rsp);
490 }
491
492
493 /*********************************************************************
494  *
495  * Function    :  cgi_send_banner
496  *
497  * Description :  CGI function that returns a banner. 
498  *
499  * Parameters  :
500  *          1  :  csp = Current client state (buffers, headers, etc...)
501  *          2  :  rsp = http_response data structure for output
502  *          3  :  parameters = map of cgi parameters
503  *
504  * CGI Parameters :
505  *           type : Selects the type of banner between "trans", "logo",
506  *                  and "auto". Defaults to "logo" if absent or invalid.
507  *                  "auto" means to select as if we were image-blocking.
508  *                  (Only the first character really counts; b and t are
509  *                  equivalent).
510  *
511  * Returns     :  JB_ERR_OK on success
512  *                JB_ERR_MEMORY on out-of-memory error.  
513  *
514  *********************************************************************/
515 jb_err cgi_send_banner(struct client_state *csp,
516                        struct http_response *rsp,
517                        const struct map *parameters)
518 {
519    char imagetype = lookup(parameters, "type")[0];
520
521    /*
522     * If type is auto, then determine the right thing
523     * to do from the set-image-blocker action
524     */
525    if (imagetype == 'a') 
526    {
527       /*
528        * Default to pattern
529        */
530       imagetype = 'p';
531
532 #ifdef FEATURE_IMAGE_BLOCKING
533       if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
534       {
535          static const char prefix1[] = CGI_PREFIX "send-banner?type=";
536          static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
537          const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
538
539          if (p == NULL)
540          {
541             /* Use default - nothing to do here. */
542          }
543          else if (0 == strcmpic(p, "blank"))
544          {
545             imagetype = 'b';
546          }
547          else if (0 == strcmpic(p, "pattern"))
548          {
549             imagetype = 'p';
550          }
551
552          /*
553           * If the action is to call this CGI, determine
554           * the argument:
555           */
556          else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
557          {
558             imagetype = p[sizeof(prefix1) - 1];
559          }
560          else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
561          {
562             imagetype = p[sizeof(prefix2) - 1];
563          }
564
565          /*
566           * Everything else must (should) be a URL to
567           * redirect to.
568           */
569          else
570          {
571             imagetype = 'r';
572          }
573       }
574 #endif /* def FEATURE_IMAGE_BLOCKING */
575    }
576       
577    /*
578     * Now imagetype is either the non-auto type we were called with,
579     * or it was auto and has since been determined. In any case, we
580     * can proceed to actually answering the request by sending a redirect
581     * or an image as appropriate:
582     */
583    if (imagetype == 'r') 
584    {
585       rsp->status = strdup("302 Local Redirect from Privoxy");
586       if (rsp->status == NULL)
587       {
588          return JB_ERR_MEMORY;
589       }
590       if (enlist_unique_header(rsp->headers, "Location",
591                                csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
592       {
593          return JB_ERR_MEMORY;
594       }
595    }
596    else
597    {
598       if ((imagetype == 'b') || (imagetype == 't')) 
599       {
600          rsp->body = bindup(image_blank_data, image_blank_length);
601          rsp->content_length = image_blank_length;
602       }
603       else
604       {
605          rsp->body = bindup(image_pattern_data, image_pattern_length);
606          rsp->content_length = image_pattern_length;
607       }
608
609       if (rsp->body == NULL)
610       {
611          return JB_ERR_MEMORY;
612       }
613       if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
614       {
615          return JB_ERR_MEMORY;
616       }
617
618       rsp->is_static = 1;
619    }
620
621    return JB_ERR_OK;
622
623 }
624
625
626 /*********************************************************************
627  *
628  * Function    :  cgi_transparent_image
629  *
630  * Description :  CGI function that sends a 1x1 transparent image.
631  *
632  * Parameters  :
633  *          1  :  csp = Current client state (buffers, headers, etc...)
634  *          2  :  rsp = http_response data structure for output
635  *          3  :  parameters = map of cgi parameters
636  *
637  * CGI Parameters : None
638  *
639  * Returns     :  JB_ERR_OK on success
640  *                JB_ERR_MEMORY on out-of-memory error.  
641  *
642  *********************************************************************/
643 jb_err cgi_transparent_image(struct client_state *csp,
644                              struct http_response *rsp,
645                              const struct map *parameters)
646 {
647    rsp->body = bindup(image_blank_data, image_blank_length);
648    rsp->content_length = image_blank_length;
649
650    if (rsp->body == NULL)
651    {
652       return JB_ERR_MEMORY;
653    }
654
655    if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
656    {
657       return JB_ERR_MEMORY;
658    }
659
660    rsp->is_static = 1;
661
662    return JB_ERR_OK;
663
664 }
665
666
667 /*********************************************************************
668  *
669  * Function    :  cgi_send_stylesheet
670  *
671  * Description :  CGI function that sends a css stylesheet found
672  *                in the cgi-style.css template
673  *
674  * Parameters  :
675  *          1  :  csp = Current client state (buffers, headers, etc...)
676  *          2  :  rsp = http_response data structure for output
677  *          3  :  parameters = map of cgi parameters
678  *
679  * CGI Parameters : None
680  *
681  * Returns     :  JB_ERR_OK on success
682  *                JB_ERR_MEMORY on out-of-memory error.  
683  *
684  *********************************************************************/
685 jb_err cgi_send_stylesheet(struct client_state *csp,
686                            struct http_response *rsp,
687                            const struct map *parameters)
688 {
689    jb_err err;
690    
691    assert(csp);
692    assert(rsp);
693
694    err = template_load(csp, &rsp->body, "cgi-style.css", 0);
695
696    if (err == JB_ERR_FILE)
697    {
698       /*
699        * No way to tell user; send empty stylesheet
700        */
701       log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
702    }
703    else if (err)
704    {
705       return err; /* JB_ERR_MEMORY */
706    }
707
708    if (enlist(rsp->headers, "Content-Type: text/css"))
709    {
710       return JB_ERR_MEMORY;
711    }
712
713    return JB_ERR_OK;
714
715 }
716 /*********************************************************************
717  *
718  * Function    :  cgi_send_user_manual
719  *
720  * Description :  CGI function that sends a file in the user
721  *                manual directory.
722  *
723  * Parameters  :
724  *          1  :  csp = Current client state (buffers, headers, etc...)
725  *          2  :  rsp = http_response data structure for output
726  *          3  :  parameters = map of cgi parameters
727  *
728  * CGI Parameters : file=name.html, the name of the HTML file
729  *                  (relative to user-manual from config)
730  *
731  * Returns     :  JB_ERR_OK on success
732  *                JB_ERR_MEMORY on out-of-memory error.  
733  *
734  *********************************************************************/
735 jb_err cgi_send_user_manual(struct client_state *csp,
736                             struct http_response *rsp,
737                             const struct map *parameters)
738 {
739    const char * filename;
740    char *full_path;
741    FILE *fp;
742    jb_err err = JB_ERR_OK;
743    size_t length;
744
745    assert(csp);
746    assert(rsp);
747    assert(parameters);
748
749    if (!parameters->first)
750    {
751       /* requested http://p.p/user-manual (without trailing slash) */
752       return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
753    }
754
755    get_string_param(parameters, "file", &filename);
756    /* Check paramter for hack attempts */
757    if (filename && strchr(filename, '/'))
758    {
759       return JB_ERR_CGI_PARAMS;
760    }
761    if (filename && strstr(filename, ".."))
762    {
763       return JB_ERR_CGI_PARAMS;
764    }
765
766    full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
767    if (full_path == NULL)
768    {
769       return JB_ERR_MEMORY;
770    }
771
772    /* Open user-manual file */
773 #ifdef WIN32
774    /*
775     * XXX: Do we support other operating systems that
776     * require special treatment to fopen in binary mode?
777     */
778    if (NULL == (fp = fopen(full_path, "rb")))
779 #else
780    if (NULL == (fp = fopen(full_path, "r")))
781 #endif /* def WIN32 */
782    {
783       log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
784       err = cgi_error_no_template(csp, rsp, full_path);
785       free(full_path);
786       return err;
787    }
788
789    /* Get file length */
790    fseek(fp, 0, SEEK_END);
791    length = (size_t)ftell(fp);
792    fseek(fp, 0, SEEK_SET);
793
794    /* Allocate memory and load the file directly into the body */
795    rsp->body = (char *)zalloc(length+1);
796    if (!rsp->body)
797    {
798       fclose(fp);
799       free(full_path);
800       return JB_ERR_MEMORY;
801    }
802    if (!fread(rsp->body, length, 1, fp))
803    {
804       /*
805        * This happens if we didn't fopen in binary mode.
806        * If it does, we just log it and serve what we got.
807        */
808       log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
809    }
810    fclose(fp);
811    free(full_path);
812
813    rsp->content_length = length;
814
815    /* Guess correct Content-Type based on the filename's ending */
816    if (filename)
817    {
818       length = strlen(filename);
819    }
820    else
821    {
822       length = 0;
823    } 
824    if((length>=4) && !strcmp(&filename[length-4], ".css"))
825    {
826       err = enlist(rsp->headers, "Content-Type: text/css");
827    }
828    else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
829    {
830       err = enlist(rsp->headers, "Content-Type: image/jpeg");
831    }
832    else
833    {
834       err = enlist(rsp->headers, "Content-Type: text/html");
835    }
836
837    return err;
838 }
839
840
841 /*********************************************************************
842  *
843  * Function    :  cgi_show_version
844  *
845  * Description :  CGI function that returns a a web page describing the
846  *                file versions of Privoxy.
847  *
848  * Parameters  :
849  *          1  :  csp = Current client state (buffers, headers, etc...)
850  *          2  :  rsp = http_response data structure for output
851  *          3  :  parameters = map of cgi parameters
852  *
853  * CGI Parameters : none
854  *
855  * Returns     :  JB_ERR_OK on success
856  *                JB_ERR_MEMORY on out-of-memory error.  
857  *
858  *********************************************************************/
859 jb_err cgi_show_version(struct client_state *csp,
860                         struct http_response *rsp,
861                         const struct map *parameters)
862 {
863    struct map *exports;
864
865    assert(csp);
866    assert(rsp);
867    assert(parameters);
868
869    if (NULL == (exports = default_exports(csp, "show-version")))
870    {
871       return JB_ERR_MEMORY;
872    }
873
874    if (map(exports, "sourceversions", 1, show_rcs(), 0))
875    {
876       free_map(exports);
877       return JB_ERR_MEMORY;
878    }
879
880    return template_fill_for_cgi(csp, "show-version", exports, rsp);
881 }
882
883  
884 /*********************************************************************
885  *
886  * Function    :  cgi_show_status
887  *
888  * Description :  CGI function that returns a web page describing the
889  *                current status of Privoxy.
890  *
891  * Parameters  :
892  *          1  :  csp = Current client state (buffers, headers, etc...)
893  *          2  :  rsp = http_response data structure for output
894  *          3  :  parameters = map of cgi parameters
895  *
896  * CGI Parameters :
897  *        file :  Which file to show.  Only first letter is checked,
898  *                valid values are:
899  *                - "p"ermissions (actions) file
900  *                - "r"egex
901  *                - "t"rust
902  *                Default is to show menu and other information.
903  *
904  * Returns     :  JB_ERR_OK on success
905  *                JB_ERR_MEMORY on out-of-memory error.  
906  *
907  *********************************************************************/
908 jb_err cgi_show_status(struct client_state *csp,
909                        struct http_response *rsp,
910                        const struct map *parameters)
911 {
912    char *s = NULL;
913    unsigned i;
914    int j;
915
916    FILE * fp;
917    char buf[BUFFER_SIZE];
918    const char * filename = NULL;
919    char * file_description = NULL;
920 #ifdef FEATURE_STATISTICS
921    float perc_rej;   /* Percentage of http requests rejected */
922    int local_urls_read;
923    int local_urls_rejected;
924 #endif /* ndef FEATURE_STATISTICS */
925    jb_err err = JB_ERR_OK;
926
927    struct map *exports;
928
929    assert(csp);
930    assert(rsp);
931    assert(parameters);
932
933    if (NULL == (exports = default_exports(csp, "show-status")))
934    {
935       return JB_ERR_MEMORY;
936    }
937
938    switch (*(lookup(parameters, "file")))
939    {
940    case 'a':
941       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
942       {
943          filename = csp->actions_list[i]->filename;
944          file_description = "Actions File";
945       }
946       break;
947
948    case 'f':
949       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
950       {
951          filename = csp->rlist[i]->filename;
952          file_description = "Filter File";
953       }
954       break;
955
956 #ifdef FEATURE_TRUST
957    case 't':
958       if (csp->tlist)
959       {
960          filename = csp->tlist->filename;
961          file_description = "Trust File";
962       }
963       break;
964 #endif /* def FEATURE_TRUST */
965    }
966
967    if (NULL != filename)
968    {
969       if ( map(exports, "file-description", 1, file_description, 1)
970         || map(exports, "filepath", 1, html_encode(filename), 0) )
971       {
972          free_map(exports);
973          return JB_ERR_MEMORY;
974       }
975
976       if ((fp = fopen(filename, "r")) == NULL)
977       {
978          if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
979          {
980             free_map(exports);
981             return JB_ERR_MEMORY;
982          }
983       }
984       else
985       {
986          s = strdup("");
987          while ((s != NULL) && fgets(buf, sizeof(buf), fp))
988          {
989             string_join  (&s, html_encode(buf));
990          }
991          fclose(fp);
992
993          if (map(exports, "contents", 1, s, 0))
994          {
995             free_map(exports);
996             return JB_ERR_MEMORY;
997          }
998       }
999
1000       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1001    }
1002
1003    s = strdup("");
1004    for (j = 0; (s != NULL) && (j < Argc); j++)
1005    {
1006       if (!err) err = string_join  (&s, html_encode(Argv[j]));
1007       if (!err) err = string_append(&s, " ");
1008    }
1009    if (!err) err = map(exports, "invocation", 1, s, 0);
1010
1011    if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1012    if (!err) err = show_defines(exports);
1013
1014    if (err) 
1015    {
1016       free_map(exports);
1017       return JB_ERR_MEMORY;
1018    }
1019
1020 #ifdef FEATURE_STATISTICS
1021    local_urls_read     = urls_read;
1022    local_urls_rejected = urls_rejected;
1023
1024    /*
1025     * Need to alter the stats not to include the fetch of this
1026     * page.
1027     *
1028     * Can't do following thread safely! doh!
1029     *
1030     * urls_read--;
1031     * urls_rejected--; * This will be incremented subsequently *
1032     */
1033
1034    if (local_urls_read == 0)
1035    {
1036       if (!err) err = map_block_killer(exports, "have-stats");
1037    }
1038    else
1039    {
1040       if (!err) err = map_block_killer(exports, "have-no-stats");
1041
1042       perc_rej = (float)local_urls_rejected * 100.0F /
1043             (float)local_urls_read;
1044
1045       snprintf(buf, sizeof(buf), "%d", local_urls_read);
1046       if (!err) err = map(exports, "requests-received", 1, buf, 1);
1047
1048       snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1049       if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1050
1051       snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1052       if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1053    }
1054
1055 #else /* ndef FEATURE_STATISTICS */
1056    err = err || map_block_killer(exports, "statistics");
1057 #endif /* ndef FEATURE_STATISTICS */
1058    
1059    /* 
1060     * List all action files in use, together with view and edit links,
1061     * except for standard.action, which should only be viewable. (Not
1062     * enforced in the editor itself)
1063     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1064     */
1065    s = strdup("");
1066    for (i = 0; i < MAX_AF_FILES; i++)
1067    {
1068       if (csp->actions_list[i] != NULL)
1069       {
1070          if (!err) err = string_append(&s, "<tr><td>");
1071          if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1072          snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&amp;index=%d\">View</a>", i);
1073          if (!err) err = string_append(&s, buf);
1074
1075 #ifdef FEATURE_CGI_EDIT_ACTIONS
1076          if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1077          {
1078             snprintf(buf, 100, "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1079             if (!err) err = string_append(&s, buf);
1080          }
1081 #endif
1082
1083          if (!err) err = string_append(&s, "</td></tr>\n");
1084       }
1085    }
1086    if (*s != '\0')   
1087    {
1088       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1089    }
1090    else
1091    {
1092       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1093    }
1094
1095    /* 
1096     * List all re_filterfiles in use, together with view options.
1097     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1098     */
1099    s = strdup("");
1100    for (i = 0; i < MAX_AF_FILES; i++)
1101    {
1102       if (csp->rlist[i] != NULL)
1103       {
1104          if (!err) err = string_append(&s, "<tr><td>");
1105          if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1106          snprintf(buf, 100,
1107             "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&amp;index=%d\">View</a>", i);
1108          if (!err) err = string_append(&s, buf);
1109          if (!err) err = string_append(&s, "</td></tr>\n");
1110       }
1111    }
1112    if (*s != '\0')   
1113    {
1114       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1115    }
1116    else
1117    {
1118       if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1119       if (!err) err = map_block_killer(exports, "have-filterfile");
1120    }
1121
1122 #ifdef FEATURE_TRUST
1123    if (csp->tlist)
1124    {
1125       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1126    }
1127    else
1128    {
1129       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1130       if (!err) err = map_block_killer(exports, "have-trustfile");
1131    }
1132 #else
1133    if (!err) err = map_block_killer(exports, "trust-support");
1134 #endif /* ndef FEATURE_TRUST */
1135
1136    if (err)
1137    {
1138       free_map(exports);
1139       return JB_ERR_MEMORY;
1140    }
1141
1142    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1143 }
1144
1145  
1146 /*********************************************************************
1147  *
1148  * Function    :  cgi_show_url_info
1149  *
1150  * Description :  CGI function that determines and shows which actions
1151  *                Privoxy will perform for a given url, and which
1152  *                matches starting from the defaults have lead to that.
1153  *
1154  * Parameters  :
1155  *          1  :  csp = Current client state (buffers, headers, etc...)
1156  *          2  :  rsp = http_response data structure for output
1157  *          3  :  parameters = map of cgi parameters
1158  *
1159  * CGI Parameters :
1160  *            url : The url whose actions are to be determined.
1161  *                  If url is unset, the url-given conditional will be
1162  *                  set, so that all but the form can be suppressed in
1163  *                  the template.
1164  *
1165  * Returns     :  JB_ERR_OK on success
1166  *                JB_ERR_MEMORY on out-of-memory error.  
1167  *
1168  *********************************************************************/
1169 jb_err cgi_show_url_info(struct client_state *csp,
1170                          struct http_response *rsp,
1171                          const struct map *parameters)
1172 {
1173    char *url_param;
1174    struct map *exports;
1175    char buf[150];
1176
1177    assert(csp);
1178    assert(rsp);
1179    assert(parameters);
1180
1181    if (NULL == (exports = default_exports(csp, "show-url-info")))
1182    {
1183       return JB_ERR_MEMORY;
1184    }
1185
1186    /*
1187     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1188     */
1189    url_param = strdup(lookup(parameters, "url"));
1190    if (url_param == NULL)
1191    {
1192       free_map(exports);
1193       return JB_ERR_MEMORY;
1194    }
1195    chomp(url_param);
1196
1197    /*
1198     * Handle prefixes.  4 possibilities:
1199     * 1) "http://" or "https://" prefix present and followed by URL - OK
1200     * 2) Only the "http://" or "https://" part is present, no URL - change
1201     *    to empty string so it will be detected later as "no URL".
1202     * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1203     *    "http://" prefix.
1204     * 4) Parameter not specified or is empty string - let this fall through
1205     *    for now, next block of code will handle it.
1206     */
1207    if (0 == strncmp(url_param, "http://", 7))
1208    {
1209       if (url_param[7] == '\0')
1210       {
1211          /*
1212           * Empty URL (just prefix).
1213           * Make it totally empty so it's caught by the next if()
1214           */
1215          url_param[0] = '\0';
1216       }
1217    }
1218    else if (0 == strncmp(url_param, "https://", 8))
1219    {
1220       if (url_param[8] == '\0')
1221       {
1222          /*
1223           * Empty URL (just prefix).
1224           * Make it totally empty so it's caught by the next if()
1225           */
1226          url_param[0] = '\0';
1227       }
1228    }
1229    else if (url_param[0] != '\0')
1230    {
1231       /*
1232        * Unknown prefix - assume http://
1233        */
1234       char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1235       if (NULL == url_param_prefixed)
1236       {
1237          free(url_param);
1238          free_map(exports);
1239          return JB_ERR_MEMORY;
1240       }
1241       strcpy(url_param_prefixed, "http://");
1242       strcpy(url_param_prefixed + 7, url_param);
1243       free(url_param);
1244       url_param = url_param_prefixed;
1245    }
1246
1247
1248    if (url_param[0] == '\0')
1249    {
1250       /* URL paramater not specified, display query form only. */
1251       free(url_param);
1252       if (map_block_killer(exports, "url-given")
1253         || map(exports, "url", 1, "", 1))
1254       {
1255          free_map(exports);
1256          return JB_ERR_MEMORY;
1257       }
1258    }
1259    else
1260    {
1261       /* Given a URL, so query it. */
1262       jb_err err;
1263       char *matches;
1264       char *s;
1265       int hits = 0;
1266       struct file_list *fl;
1267       struct url_actions *b;
1268       struct http_request url_to_query[1];
1269       struct current_action_spec action[1];
1270       int i;
1271       
1272       if (map(exports, "url", 1, html_encode(url_param), 0))
1273       {
1274          free(url_param);
1275          free_map(exports);
1276          return JB_ERR_MEMORY;
1277       }
1278
1279       init_current_action(action);
1280
1281       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1282       {
1283          free_current_action(action);
1284          free(url_param);
1285          free_map(exports);
1286          return JB_ERR_MEMORY;
1287       }
1288
1289       err = parse_http_url(url_param, url_to_query, csp);
1290
1291       free(url_param);
1292
1293       if (err == JB_ERR_MEMORY)
1294       {
1295          free_http_request(url_to_query);
1296          free_current_action(action);
1297          free_map(exports);
1298          return JB_ERR_MEMORY;
1299       }
1300       else if (err)
1301       {
1302          /* Invalid URL */
1303
1304          err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1305          if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1306
1307          free_current_action(action);
1308          free_http_request(url_to_query);
1309
1310          if (err)
1311          {
1312             free_map(exports);
1313             return JB_ERR_MEMORY;
1314          }
1315
1316          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1317       }
1318
1319       /*
1320        * We have a warning about SSL paths.  Hide it for insecure sites.
1321        */
1322       if (!url_to_query->ssl)
1323       {
1324          if (map_block_killer(exports, "https"))
1325          {
1326             free_current_action(action);
1327             free_map(exports);
1328             free_http_request(url_to_query);
1329             return JB_ERR_MEMORY;
1330          }
1331       }
1332
1333       matches = strdup("<table class=\"transparent\">");
1334
1335       for (i = 0; i < MAX_AF_FILES; i++)
1336       {
1337          if (NULL == csp->config->actions_file_short[i]
1338              || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1339
1340          b = NULL;
1341          hits = 1;
1342          if ((fl = csp->actions_list[i]) != NULL)
1343          {
1344             if ((b = fl->f) != NULL)
1345             {
1346                /* FIXME: Hardcoded HTML! */
1347                string_append(&matches, "<tr><th>In file: ");
1348                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1349                snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&amp;index=%d\">", i);
1350                string_append(&matches, buf);
1351                string_append(&matches, "View</a>");
1352 #ifdef FEATURE_CGI_EDIT_ACTIONS
1353                string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1354                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1355                string_append(&matches, "\">Edit</a>");
1356 #endif
1357                string_append(&matches, "</th></tr>\n");
1358
1359                hits = 0;
1360                b = b->next;
1361             }
1362          }
1363
1364          for (; (b != NULL) && (matches != NULL); b = b->next)
1365          {
1366             if (url_match(b->url, url_to_query))
1367             {
1368                string_append(&matches, "<tr><td>{");
1369                string_join  (&matches, actions_to_html(csp, b->action));
1370                string_append(&matches, " }</b><br>\n<code>");
1371                string_join  (&matches, html_encode(b->url->spec));
1372                string_append(&matches, "</code></td></tr>\n");
1373
1374                if (merge_current_action(action, b->action))
1375                {
1376                   freez(matches);
1377                   free_http_request(url_to_query);
1378                   free_current_action(action);
1379                   free_map(exports);
1380                   return JB_ERR_MEMORY;
1381                }
1382                hits++;
1383             }
1384          }
1385
1386          if (!hits)
1387          {
1388             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1389          }
1390       }
1391       string_append(&matches, "</table>\n");
1392
1393       free_http_request(url_to_query);
1394
1395       if (matches == NULL)
1396       {
1397          free_current_action(action);
1398          free_map(exports);
1399          return JB_ERR_MEMORY;
1400       }
1401
1402       if (map(exports, "matches", 1, matches , 0))
1403       {
1404          free_current_action(action);
1405          free_map(exports);
1406          return JB_ERR_MEMORY;
1407       }
1408
1409       s = current_action_to_html(csp, action);
1410
1411       free_current_action(action);
1412
1413       if (map(exports, "final", 1, s, 0))
1414       {
1415          free_map(exports);
1416          return JB_ERR_MEMORY;
1417       }
1418    }
1419
1420    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1421 }
1422
1423
1424 /*********************************************************************
1425  *
1426  * Function    :  cgi_robots_txt
1427  *
1428  * Description :  CGI function to return "/robots.txt".
1429  *
1430  * Parameters  :
1431  *          1  :  csp = Current client state (buffers, headers, etc...)
1432  *          2  :  rsp = http_response data structure for output
1433  *          3  :  parameters = map of cgi parameters
1434  *
1435  * CGI Parameters : None
1436  *
1437  * Returns     :  JB_ERR_OK on success
1438  *                JB_ERR_MEMORY on out-of-memory error.  
1439  *
1440  *********************************************************************/
1441 jb_err cgi_robots_txt(struct client_state *csp,
1442                       struct http_response *rsp,
1443                       const struct map *parameters)
1444 {
1445    char buf[100];
1446    jb_err err;
1447
1448    rsp->body = strdup(
1449       "# This is the Privoxy control interface.\n"
1450       "# It isn't very useful to index it, and you're likely to break stuff.\n"
1451       "# So go away!\n"
1452       "\n"
1453       "User-agent: *\n"
1454       "Disallow: /\n"
1455       "\n");
1456    if (rsp->body == NULL)
1457    {
1458       return JB_ERR_MEMORY;
1459    }
1460
1461    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1462
1463    rsp->is_static = 1;
1464
1465    get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1466    if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1467
1468    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1469 }
1470
1471
1472 /*********************************************************************
1473  *
1474  * Function    :  show_defines
1475  *
1476  * Description :  Add to a map the state od all conditional #defines
1477  *                used when building
1478  *
1479  * Parameters  :
1480  *          1  :  exports = map to extend
1481  *
1482  * Returns     :  JB_ERR_OK on success
1483  *                JB_ERR_MEMORY on out-of-memory error.  
1484  *
1485  *********************************************************************/
1486 static jb_err show_defines(struct map *exports)
1487 {
1488    jb_err err = JB_ERR_OK;
1489
1490 #ifdef FEATURE_ACL
1491    if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1492 #else /* ifndef FEATURE_ACL */
1493    if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1494 #endif /* ndef FEATURE_ACL */
1495
1496 #ifdef FEATURE_CGI_EDIT_ACTIONS
1497    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1498 #else /* ifndef FEATURE_COOKIE_JAR */
1499    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1500 #endif /* ndef FEATURE_COOKIE_JAR */
1501
1502 #ifdef FEATURE_COOKIE_JAR
1503    if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1504 #else /* ifndef FEATURE_COOKIE_JAR */
1505    if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1506 #endif /* ndef FEATURE_COOKIE_JAR */
1507
1508 #ifdef FEATURE_FAST_REDIRECTS
1509    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1510 #else /* ifndef FEATURE_FAST_REDIRECTS */
1511    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1512 #endif /* ndef FEATURE_FAST_REDIRECTS */
1513
1514 #ifdef FEATURE_FORCE_LOAD
1515    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1516    if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1517 #else /* ifndef FEATURE_FORCE_LOAD */
1518    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1519    if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1520 #endif /* ndef FEATURE_FORCE_LOAD */
1521
1522 #ifdef FEATURE_GRACEFUL_TERMINATION
1523    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1524 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1525    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1526 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1527
1528 #ifdef FEATURE_IMAGE_BLOCKING
1529    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1530 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1531    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1532 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1533
1534 #ifdef FEATURE_IMAGE_DETECT_MSIE
1535    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1536 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1537    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1538 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1539
1540 #ifdef FEATURE_KILL_POPUPS
1541    if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1542 #else /* ifndef FEATURE_KILL_POPUPS */
1543    if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1544 #endif /* ndef FEATURE_KILL_POPUPS */
1545
1546 #ifdef FEATURE_NO_GIFS
1547    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1548 #else /* ifndef FEATURE_NO_GIFS */
1549    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1550 #endif /* ndef FEATURE_NO_GIFS */
1551
1552 #ifdef FEATURE_PTHREAD
1553    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1554 #else /* ifndef FEATURE_PTHREAD */
1555    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1556 #endif /* ndef FEATURE_PTHREAD */
1557
1558 #ifdef FEATURE_STATISTICS
1559    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1560 #else /* ifndef FEATURE_STATISTICS */
1561    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1562 #endif /* ndef FEATURE_STATISTICS */
1563
1564 #ifdef FEATURE_TOGGLE
1565    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1566 #else /* ifndef FEATURE_TOGGLE */
1567    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1568 #endif /* ndef FEATURE_TOGGLE */
1569
1570 #ifdef FEATURE_TRUST
1571    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1572 #else /* ifndef FEATURE_TRUST */
1573    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1574 #endif /* ndef FEATURE_TRUST */
1575
1576 #ifdef FEATURE_ZLIB
1577    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1578 #else /* ifndef FEATURE_ZLIB */
1579    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1580 #endif /* ndef FEATURE_ZLIB */
1581
1582 #ifdef STATIC_PCRE
1583    if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1584 #else /* ifndef STATIC_PCRE */
1585    if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1586 #endif /* ndef STATIC_PCRE */
1587
1588 #ifdef STATIC_PCRS
1589    if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1590 #else /* ifndef STATIC_PCRS */
1591    if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1592 #endif /* ndef STATIC_PCRS */
1593
1594    return err;
1595 }
1596
1597
1598 /*********************************************************************
1599  *
1600  * Function    :  show_rcs
1601  *
1602  * Description :  Create a string with the rcs info for all sourcefiles
1603  *
1604  * Parameters  :  None
1605  *
1606  * Returns     :  A string, or NULL on out-of-memory.
1607  *
1608  *********************************************************************/
1609 static char *show_rcs(void)
1610 {
1611    char *result = strdup("");
1612    char buf[BUFFER_SIZE];
1613
1614    /* Instead of including *all* dot h's in the project (thus creating a
1615     * tremendous amount of dependencies), I will concede to declaring them
1616     * as extern's.  This forces the developer to add to this list, but oh well.
1617     */
1618
1619 #define SHOW_RCS(__x)              \
1620    {                               \
1621       extern const char __x[];     \
1622       snprintf(buf, sizeof(buf), " %s\n", __x);   \
1623       string_append(&result, buf); \
1624    }
1625
1626    /* In alphabetical order */
1627    SHOW_RCS(actions_h_rcs)
1628    SHOW_RCS(actions_rcs)
1629 #ifdef AMIGA
1630    SHOW_RCS(amiga_h_rcs)
1631    SHOW_RCS(amiga_rcs)
1632 #endif /* def AMIGA */
1633    SHOW_RCS(cgi_h_rcs)
1634    SHOW_RCS(cgi_rcs)
1635 #ifdef FEATURE_CGI_EDIT_ACTIONS
1636    SHOW_RCS(cgiedit_h_rcs)
1637    SHOW_RCS(cgiedit_rcs)
1638 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1639    SHOW_RCS(cgisimple_h_rcs)
1640    SHOW_RCS(cgisimple_rcs)
1641 #ifdef __MINGW32__
1642    SHOW_RCS(cygwin_h_rcs)
1643 #endif
1644    SHOW_RCS(deanimate_h_rcs)
1645    SHOW_RCS(deanimate_rcs)
1646    SHOW_RCS(encode_h_rcs)
1647    SHOW_RCS(encode_rcs)
1648    SHOW_RCS(errlog_h_rcs)
1649    SHOW_RCS(errlog_rcs)
1650    SHOW_RCS(filters_h_rcs)
1651    SHOW_RCS(filters_rcs)
1652    SHOW_RCS(gateway_h_rcs)
1653    SHOW_RCS(gateway_rcs)
1654    SHOW_RCS(jbsockets_h_rcs)
1655    SHOW_RCS(jbsockets_rcs)
1656    SHOW_RCS(jcc_h_rcs)
1657    SHOW_RCS(jcc_rcs)
1658 #ifdef FEATURE_KILL_POPUPS
1659    SHOW_RCS(killpopup_h_rcs)
1660    SHOW_RCS(killpopup_rcs)
1661 #endif /* def FEATURE_KILL_POPUPS */
1662    SHOW_RCS(list_h_rcs)
1663    SHOW_RCS(list_rcs)
1664    SHOW_RCS(loadcfg_h_rcs)
1665    SHOW_RCS(loadcfg_rcs)
1666    SHOW_RCS(loaders_h_rcs)
1667    SHOW_RCS(loaders_rcs)
1668    SHOW_RCS(miscutil_h_rcs)
1669    SHOW_RCS(miscutil_rcs)
1670    SHOW_RCS(parsers_h_rcs)
1671    SHOW_RCS(parsers_rcs)
1672    SHOW_RCS(pcrs_rcs)
1673    SHOW_RCS(pcrs_h_rcs)
1674    SHOW_RCS(project_h_rcs)
1675    SHOW_RCS(ssplit_h_rcs)
1676    SHOW_RCS(ssplit_rcs)
1677    SHOW_RCS(urlmatch_h_rcs)
1678    SHOW_RCS(urlmatch_rcs)
1679 #ifdef _WIN32
1680 #ifndef _WIN_CONSOLE
1681    SHOW_RCS(w32log_h_rcs)
1682    SHOW_RCS(w32log_rcs)
1683    SHOW_RCS(w32res_h_rcs)
1684    SHOW_RCS(w32taskbar_h_rcs)
1685    SHOW_RCS(w32taskbar_rcs)
1686 #endif /* ndef _WIN_CONSOLE */
1687    SHOW_RCS(win32_h_rcs)
1688    SHOW_RCS(win32_rcs)
1689 #endif /* def _WIN32 */
1690
1691 #undef SHOW_RCS
1692
1693    return result;
1694
1695 }
1696
1697
1698 /*
1699   Local Variables:
1700   tab-width: 3
1701   end:
1702 */