Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
[privoxy.git] / parsers.c
1 const char parsers_rcs[] = "$Id: parsers.c,v 1.37 2001/10/23 21:36:02 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
5  *
6  * Purpose     :  Declares functions to parse/crunch headers and pages.
7  *                Functions declared include:
8  *                   `add_to_iob', `client_cookie_adder', `client_from',
9  *                   `client_referrer', `client_send_cookie', `client_ua',
10  *                   `client_uagent', `client_x_forwarded',
11  *                   `client_x_forwarded_adder', `client_xtra_adder',
12  *                   `content_type', `crumble', `destroy_list', `enlist',
13  *                   `flush_socket', `free_http_request', `get_header',
14  *                   `list_to_text', `parse_http_request', `sed',
15  *                   and `server_set_cookie'.
16  *
17  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
18  *                IJBSWA team.  http://ijbswa.sourceforge.net
19  *
20  *                Based on the Internet Junkbuster originally written
21  *                by and Copyright (C) 1997 Anonymous Coders and
22  *                Junkbusters Corporation.  http://www.junkbusters.com
23  *
24  *                This program is free software; you can redistribute it
25  *                and/or modify it under the terms of the GNU General
26  *                Public License as published by the Free Software
27  *                Foundation; either version 2 of the License, or (at
28  *                your option) any later version.
29  *
30  *                This program is distributed in the hope that it will
31  *                be useful, but WITHOUT ANY WARRANTY; without even the
32  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
33  *                PARTICULAR PURPOSE.  See the GNU General Public
34  *                License for more details.
35  *
36  *                The GNU General Public License should be included with
37  *                this file.  If not, you can view it at
38  *                http://www.gnu.org/copyleft/gpl.html
39  *                or write to the Free Software Foundation, Inc., 59
40  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
41  *
42  * Revisions   :
43  *    $Log: parsers.c,v $
44  *    Revision 1.37  2001/10/23 21:36:02  jongfoster
45  *    Documenting sed()'s error behaviou (doc change only)
46  *
47  *    Revision 1.36  2001/10/13 12:51:51  joergs
48  *    Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
49  *    force-load), instead crumble Host: and add it (again) in client_host_adder
50  *    (in case we get a HTTP/1.0 request without Host: header and forward it to
51  *    a HTTP/1.1 server/proxy).
52  *
53  *    Revision 1.35  2001/10/09 22:39:21  jongfoster
54  *    assert.h is also required under Win32, so moving out of #ifndef _WIN32
55  *    block.
56  *
57  *    Revision 1.34  2001/10/07 18:50:55  oes
58  *    Added server_content_encoding, renamed server_transfer_encoding
59  *
60  *    Revision 1.33  2001/10/07 18:04:49  oes
61  *    Changed server_http11 to server_http and its pattern to "HTTP".
62  *      Additional functionality: it now saves the HTTP status into
63  *      csp->http->status and sets CT_TABOO for Status 206 (partial range)
64  *
65  *    Revision 1.32  2001/10/07 15:43:28  oes
66  *    Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
67  *       client_te and client_accept_encoding_adder, triggered by the new
68  *       +no-compression action. For HTTP/1.1 the Accept-Encoding header is
69  *       changed to allow only identity and chunked, and the TE header is
70  *       crunched. For HTTP/1.0, Accept-Encoding is crunched.
71  *
72  *    parse_http_request no longer does anything than parsing. The rewriting
73  *      of http->cmd and version mangling are gone. It now also recognizes
74  *      the put and delete methods and saves the url in http->url. Removed
75  *      unused variable.
76  *
77  *    renamed content_type and content_length to have the server_ prefix
78  *
79  *    server_content_type now only works if csp->content_type != CT_TABOO
80  *
81  *    added server_transfer_encoding, which
82  *      - Sets CT_TABOO to prohibit filtering if encoding compresses
83  *      - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
84  *      - Change from "chunked" to "identity" if body was chunked
85  *        but has been de-chunked for filtering.
86  *
87  *    added server_content_md5 which crunches any Content-MD5 headers
88  *      if the body was modified.
89  *
90  *    made server_http11 conditional on +downgrade action
91  *
92  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
93  *
94  *    Revision 1.31  2001/10/05 14:25:02  oes
95  *    Crumble Keep-Alive from Server
96  *
97  *    Revision 1.30  2001/09/29 12:56:03  joergs
98  *    IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
99  *
100  *    Revision 1.29  2001/09/24 21:09:24  jongfoster
101  *    Fixing 2 memory leaks that Guy spotted, where the paramater to
102  *    enlist() was not being free()d.
103  *
104  *    Revision 1.28  2001/09/22 16:32:28  jongfoster
105  *    Removing unused #includes.
106  *
107  *    Revision 1.27  2001/09/20 15:45:25  steudten
108  *
109  *    add casting from size_t to int for printf()
110  *    remove local variable shadow s2
111  *
112  *    Revision 1.26  2001/09/16 17:05:14  jongfoster
113  *    Removing unused #include showarg.h
114  *
115  *    Revision 1.25  2001/09/16 13:21:27  jongfoster
116  *    Changes to use new list functions.
117  *
118  *    Revision 1.24  2001/09/13 23:05:50  jongfoster
119  *    Changing the string paramater to the header parsers a "const".
120  *
121  *    Revision 1.23  2001/09/12 18:08:19  steudten
122  *
123  *    In parse_http_request() header rewriting miss the host value, so
124  *    from http://www.mydomain.com the result was just " / " not
125  *    http://www.mydomain.com/ in case we forward.
126  *
127  *    Revision 1.22  2001/09/10 10:58:53  oes
128  *    Silenced compiler warnings
129  *
130  *    Revision 1.21  2001/07/31 14:46:00  oes
131  *     - Persistant connections now suppressed
132  *     - sed() no longer appends empty header to csp->headers
133  *
134  *    Revision 1.20  2001/07/30 22:08:36  jongfoster
135  *    Tidying up #defines:
136  *    - All feature #defines are now of the form FEATURE_xxx
137  *    - Permanently turned off WIN_GUI_EDIT
138  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
139  *
140  *    Revision 1.19  2001/07/25 17:21:54  oes
141  *    client_uagent now saves copy of User-Agent: header value
142  *
143  *    Revision 1.18  2001/07/13 14:02:46  oes
144  *     - Included fix to repair broken HTTP requests that
145  *       don't contain a path, not even '/'.
146  *     - Removed all #ifdef PCRS
147  *     - content_type now always inspected and classified as
148  *       text, gif or other.
149  *     - formatting / comments
150  *
151  *    Revision 1.17  2001/06/29 21:45:41  oes
152  *    Indentation, CRLF->LF, Tab-> Space
153  *
154  *    Revision 1.16  2001/06/29 13:32:42  oes
155  *    - Fixed a comment
156  *    - Adapted free_http_request
157  *    - Removed logentry from cancelled commit
158  *
159  *    Revision 1.15  2001/06/03 19:12:38  oes
160  *    deleted const struct interceptors
161  *
162  *    Revision 1.14  2001/06/01 18:49:17  jongfoster
163  *    Replaced "list_share" with "list" - the tiny memory gain was not
164  *    worth the extra complexity.
165  *
166  *    Revision 1.13  2001/05/31 21:30:33  jongfoster
167  *    Removed list code - it's now in list.[ch]
168  *    Renamed "permission" to "action", and changed many features
169  *    to use the actions file rather than the global config.
170  *
171  *    Revision 1.12  2001/05/31 17:33:13  oes
172  *
173  *    CRLF -> LF
174  *
175  *    Revision 1.11  2001/05/29 20:11:19  joergs
176  *    '/ * inside comment' warning removed.
177  *
178  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
179  *    Unified blocklist/imagelist/permissionslist.
180  *    File format is still under discussion, but the internal changes
181  *    are (mostly) done.
182  *
183  *    Also modified interceptor behaviour:
184  *    - We now intercept all URLs beginning with one of the following
185  *      prefixes (and *only* these prefixes):
186  *        * http://i.j.b/
187  *        * http://ijbswa.sf.net/config/
188  *        * http://ijbswa.sourceforge.net/config/
189  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
190  *    - Internal changes so that intercepted and fast redirect pages
191  *      are not replaced with an image.
192  *    - Interceptors now have the option to send a binary page direct
193  *      to the client. (i.e. ijb-send-banner uses this)
194  *    - Implemented show-url-info interceptor.  (Which is why I needed
195  *      the above interceptors changes - a typical URL is
196  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
197  *      The previous mechanism would not have intercepted that, and
198  *      if it had been intercepted then it then it would have replaced
199  *      it with an image.)
200  *
201  *    Revision 1.9  2001/05/28 17:26:33  jongfoster
202  *    Fixing segfault if last header was crunched.
203  *    Fixing Windows build (snprintf() is _snprintf() under Win32, but we
204  *    can use the cross-platform sprintf() instead.)
205  *
206  *    Revision 1.8  2001/05/27 22:17:04  oes
207  *
208  *    - re_process_buffer no longer writes the modified buffer
209  *      to the client, which was very ugly. It now returns the
210  *      buffer, which it is then written by chat.
211  *
212  *    - content_length now adjusts the Content-Length: header
213  *      for modified documents rather than crunch()ing it.
214  *      (Length info in csp->content_length, which is 0 for
215  *      unmodified documents)
216  *
217  *    - For this to work, sed() is called twice when filtering.
218  *
219  *    Revision 1.7  2001/05/27 13:19:06  oes
220  *    Patched Joergs solution for the content-length in.
221  *
222  *    Revision 1.6  2001/05/26 13:39:32  jongfoster
223  *    Only crunches Content-Length header if applying RE filtering.
224  *    Without this fix, Microsoft Windows Update wouldn't work.
225  *
226  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
227  *    Automatic reloading of config file.
228  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
229  *    Most of the global variables have been moved to a new
230  *    struct configuration_spec, accessed through csp->config->globalname
231  *    Most of the globals remaining are used by the Win32 GUI.
232  *
233  *    Revision 1.4  2001/05/22 18:46:04  oes
234  *
235  *    - Enabled filtering banners by size rather than URL
236  *      by adding patterns that replace all standard banner
237  *      sizes with the "Junkbuster" gif to the re_filterfile
238  *
239  *    - Enabled filtering WebBugs by providing a pattern
240  *      which kills all 1x1 images
241  *
242  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
243  *      which is selected by the (nonstandard and therefore
244  *      capital) letter 'U' in the option string.
245  *      It causes the quantifiers to be ungreedy by default.
246  *      Appending a ? turns back to greedy (!).
247  *
248  *    - Added a new interceptor ijb-send-banner, which
249  *      sends back the "Junkbuster" gif. Without imagelist or
250  *      MSIE detection support, or if tinygif = 1, or the
251  *      URL isn't recognized as an imageurl, a lame HTML
252  *      explanation is sent instead.
253  *
254  *    - Added new feature, which permits blocking remote
255  *      script redirects and firing back a local redirect
256  *      to the browser.
257  *      The feature is conditionally compiled, i.e. it
258  *      can be disabled with --disable-fast-redirects,
259  *      plus it must be activated by a "fast-redirects"
260  *      line in the config file, has its own log level
261  *      and of course wants to be displayed by show-proxy-args
262  *      Note: Boy, all the #ifdefs in 1001 locations and
263  *      all the fumbling with configure.in and acconfig.h
264  *      were *way* more work than the feature itself :-(
265  *
266  *    - Because a generic redirect template was needed for
267  *      this, tinygif = 3 now uses the same.
268  *
269  *    - Moved GIFs, and other static HTTP response templates
270  *      to project.h
271  *
272  *    - Some minor fixes
273  *
274  *    - Removed some >400 CRs again (Jon, you really worked
275  *      a lot! ;-)
276  *
277  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
278  *    Version 2.9.4 checkin.
279  *    - Merged popupfile and cookiefile, and added control over PCRS
280  *      filtering, in new "permissionsfile".
281  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
282  *      file error you now get a message box (in the Win32 GUI) rather
283  *      than the program exiting with no explanation.
284  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
285  *      skipping.
286  *    - Removed tabs from "config"
287  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
288  *    - Bumped up version number.
289  *
290  *    Revision 1.2  2001/05/17 23:02:36  oes
291  *     - Made referrer option accept 'L' as a substitute for '§'
292  *
293  *    Revision 1.1.1.1  2001/05/15 13:59:01  oes
294  *    Initial import of version 2.9.3 source tree
295  *
296  *
297  *********************************************************************/
298 \f
299
300 #include "config.h"
301
302 #ifndef _WIN32
303 #include <stdio.h>
304 #include <sys/types.h>
305 #include <stdlib.h>
306 #include <ctype.h>
307 #endif
308
309 #include <assert.h>
310 #include <string.h>
311
312 #if !defined(_WIN32) && !defined(__OS2__)
313 #include <unistd.h>
314 #endif
315
316 #include "project.h"
317 #include "list.h"
318 #include "parsers.h"
319 #include "encode.h"
320 #include "ssplit.h"
321 #include "errlog.h"
322 #include "jbsockets.h"
323 #include "miscutil.h"
324
325 const char parsers_h_rcs[] = PARSERS_H_VERSION;
326
327 /* Fix a problem with Solaris.  There should be no effect on other
328  * platforms.
329  * Solaris's isspace() is a macro which uses it's argument directly
330  * as an array index.  Therefore we need to make sure that high-bit
331  * characters generate +ve values, and ideally we also want to make
332  * the argument match the declared parameter type of "int".
333  *
334  * Why did they write a character function that can't take a simple
335  * "char" argument?  Doh!
336  */
337 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
338 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
339
340
341 const struct parsers client_patterns[] = {
342    { "referer:",                 8,    client_referrer },
343    { "user-agent:",              11,   client_uagent },
344    { "ua-",                      3,    client_ua },
345    { "from:",                    5,    client_from },
346    { "cookie:",                  7,    client_send_cookie },
347    { "x-forwarded-for:",         16,   client_x_forwarded },
348    { "Accept-Encoding:",         16,   client_accept_encoding },
349    { "TE:",                      3,    client_te },
350 #if defined(FEATURE_IMAGE_DETECT_MSIE)
351    { "Accept:",                   7,   client_accept },
352 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
353    { "Host:",                     5,   crumble },
354 /* { "if-modified-since:",       18,   crumble }, */
355    { "Keep-Alive:",              11,   crumble },
356    { "connection:",              11,   crumble },
357    { "proxy-connection:",        17,   crumble },
358    { NULL,                       0,    NULL }
359 };
360
361
362 const struct parsers server_patterns[] = {
363    { "HTTP",                4, server_http },
364    { "set-cookie:",        11, server_set_cookie },
365    { "connection:",        11, crumble },
366    { "Content-Type:",      13, server_content_type },
367    { "Content-Length:",    15, server_content_length },
368    { "Content-MD5:",       12, server_content_md5 },
369    { "Content-Encoding:",  17, server_content_encoding },
370    { "Transfer-Encoding:", 18, server_transfer_coding },
371    { "Keep-Alive:",        11, crumble },
372    { NULL, 0, NULL }
373 };
374
375
376 void (* const add_client_headers[])(struct client_state *) = {
377    client_host_adder,
378    client_cookie_adder,
379    client_x_forwarded_adder,
380    client_xtra_adder,
381    client_accept_encoding_adder,
382    connection_close_adder,
383    NULL
384 };
385
386
387 void (* const add_server_headers[])(struct client_state *) = {
388    connection_close_adder,
389    NULL
390 };
391
392
393 /*********************************************************************
394  *
395  * Function    :  flush_socket
396  *
397  * Description :  Write any pending "buffered" content.
398  *
399  * Parameters  :
400  *          1  :  fd = file descriptor of the socket to read
401  *          2  :  csp = Current client state (buffers, headers, etc...)
402  *
403  * Returns     :  On success, the number of bytes written are returned (zero
404  *                indicates nothing was written).  On error, -1 is returned,
405  *                and errno is set appropriately.  If count is zero and the
406  *                file descriptor refers to a regular file, 0 will be
407  *                returned without causing any other effect.  For a special
408  *                file, the results are not portable.
409  *
410  *********************************************************************/
411 int flush_socket(int fd, struct client_state *csp)
412 {
413    struct iob *iob = csp->iob;
414    int n = iob->eod - iob->cur;
415
416    if (n <= 0)
417    {
418       return(0);
419    }
420
421    n = write_socket(fd, iob->cur, n);
422    iob->eod = iob->cur = iob->buf;
423    return(n);
424
425 }
426
427
428 /*********************************************************************
429  *
430  * Function    :  add_to_iob
431  *
432  * Description :  Add content to the buffered page.
433  *
434  * Parameters  :
435  *          1  :  csp = Current client state (buffers, headers, etc...)
436  *          2  :  buf = holds the content to be added to the page
437  *          3  :  n = number of bytes to be added
438  *
439  * Returns     :  Number of bytes in the content buffer.
440  *
441  *********************************************************************/
442 int add_to_iob(struct client_state *csp, char *buf, int n)
443 {
444    struct iob *iob = csp->iob;
445    int have, need;
446    char *p;
447
448    have = iob->eod - iob->cur;
449
450    if (n <= 0)
451    {
452       return(have);
453    }
454
455    need = have + n;
456
457    if ((p = (char *)malloc(need + 1)) == NULL)
458    {
459       log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
460       return(-1);
461    }
462
463    if (have)
464    {
465       /* there is something in the buffer - save it */
466       memcpy(p, iob->cur, have);
467
468       /* replace the buffer with the new space */
469       freez(iob->buf);
470       iob->buf = p;
471
472       /* point to the end of the data */
473       p += have;
474    }
475    else
476    {
477       /* the buffer is empty, free it and reinitialize */
478       freez(iob->buf);
479       iob->buf = p;
480    }
481
482    /* copy the new data into the iob buffer */
483    memcpy(p, buf, n);
484
485    /* point to the end of the data */
486    p += n;
487
488    /* null terminate == cheap insurance */
489    *p = '\0';
490
491    /* set the pointers to the new values */
492    iob->cur = iob->buf;
493    iob->eod = p;
494
495    return(need);
496
497 }
498
499
500 /*********************************************************************
501  *
502  * Function    :  get_header
503  *
504  * Description :  This (odd) routine will parse the csp->iob
505  *
506  * Parameters  :
507  *          1  :  csp = Current client state (buffers, headers, etc...)
508  *
509  * Returns     :  Any one of the following:
510  *
511  * 1) a pointer to a dynamically allocated string that contains a header line
512  * 2) NULL  indicating that the end of the header was reached
513  * 3) ""    indicating that the end of the iob was reached before finding
514  *          a complete header line.
515  *
516  *********************************************************************/
517 char *get_header(struct client_state *csp)
518 {
519    struct iob *iob;
520    char *p, *q, *ret;
521    iob = csp->iob;
522
523    if ((iob->cur == NULL)
524       || ((p = strchr(iob->cur, '\n')) == NULL))
525    {
526       return(""); /* couldn't find a complete header */
527    }
528
529    *p = '\0';
530
531    ret = strdup(iob->cur);
532
533    iob->cur = p+1;
534
535    if ((q = strchr(ret, '\r'))) *q = '\0';
536
537    /* is this a blank linke (i.e. the end of the header) ? */
538    if (*ret == '\0')
539    {
540       freez(ret);
541       return(NULL);
542    }
543
544    return(ret);
545
546 }
547
548
549 /*********************************************************************
550  *
551  * Function    :  sed
552  *
553  * Description :  add, delete or modify lines in the HTTP header streams.
554  *                On entry, it receives a linked list of headers space
555  *                that was allocated dynamically (both the list nodes
556  *                and the header contents).
557  *
558  *                As a side effect it frees the space used by the original
559  *                header lines.
560  *
561  * Parameters  :
562  *          1  :  pats = list of patterns to match against headers
563  *          2  :  more_headers = list of functions to add more
564  *                headers (client or server)
565  *          3  :  csp = Current client state (buffers, headers, etc...)
566  *
567  * Returns     :  Single pointer to a fully formed header, or NULL
568  *                on out-of-memory error.
569  *
570  *********************************************************************/
571 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
572 {
573    struct list_entry *p;
574    const struct parsers *v;
575    char *hdr;
576    void (* const *f)();
577
578    for (v = pats; v->str ; v++)
579    {
580       for (p = csp->headers->first; p ; p = p->next)
581       {
582          /* Header crunch()ed in previous run? -> ignore */
583          if (p->str == NULL) continue;
584
585          if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
586
587          if (strncmpic(p->str, v->str, v->len) == 0)
588          {
589             hdr = v->parser(v, p->str, csp);
590             freez(p->str); /* FIXME: Yuck! patching a list...*/
591             p->str = hdr;
592          }
593       }
594    }
595
596    /* place any additional headers on the csp->headers list */
597    for (f = more_headers; *f ; f++)
598    {
599       (*f)(csp);
600    }
601
602    hdr = list_to_text(csp->headers);
603
604    return hdr;
605
606 }
607
608
609 /*********************************************************************
610  *
611  * Function    :  free_http_request
612  *
613  * Description :  Freez a http_request structure
614  *
615  * Parameters  :
616  *          1  :  http = points to a http_request structure to free
617  *
618  * Returns     :  N/A
619  *
620  *********************************************************************/
621 void free_http_request(struct http_request *http)
622 {
623    assert(http);
624
625    freez(http->cmd);
626    freez(http->gpc);
627    freez(http->host);
628    freez(http->url);
629    freez(http->hostport);
630    freez(http->path);
631    freez(http->ver);
632    freez(http->host_ip_addr_str);
633    freez(http->user_agent);
634
635 }
636
637
638 /*********************************************************************
639  *
640  * Function    :  parse_http_request
641  *
642  * Description :  Parse out the host and port from the URL.  Find the
643  *                hostname & path, port (if ':'), and/or password (if '@')
644  *
645  * Parameters  :
646  *          1  :  req = URL (or is it URI?) to break down
647  *          2  :  http = pointer to the http structure to hold elements
648  *          3  :  csp = Current client state (buffers, headers, etc...)
649  *
650  * Returns     :  N/A
651  *
652  *********************************************************************/
653 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
654 {
655    char *buf, *v[10], *url, *p;
656    int n;
657
658    memset(http, '\0', sizeof(*http));
659    http->cmd = strdup(req);
660
661    buf = strdup(req);
662    n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
663
664    if (n == 3)
665    {
666       /* this could be a CONNECT request */
667       if (strcmpic(v[0], "connect") == 0)
668       {
669          http->ssl      = 1;
670          http->gpc      = strdup(v[0]);
671          http->hostport = strdup(v[1]);
672          http->ver      = strdup(v[2]);
673       }
674
675       /* or it could be any other basic HTTP request type */
676       if ((0 == strcmpic(v[0], "get"))
677        || (0 == strcmpic(v[0], "head"))
678        || (0 == strcmpic(v[0], "post"))
679        || (0 == strcmpic(v[0], "put"))
680        || (0 == strcmpic(v[0], "delete"))
681
682        /* or a webDAV extension (RFC2518) */
683        || (0 == strcmpic(v[0], "propfind"))
684        || (0 == strcmpic(v[0], "proppatch"))
685        || (0 == strcmpic(v[0], "move"))
686        || (0 == strcmpic(v[0], "copy"))
687        || (0 == strcmpic(v[0], "mkcol"))
688        || (0 == strcmpic(v[0], "lock"))
689        || (0 == strcmpic(v[0], "unlock"))
690        )
691       {
692          http->ssl    = 0;
693          http->gpc    = strdup(v[0]);
694          http->url    = strdup(v[1]);
695          http->ver    = strdup(v[2]);
696
697          url = v[1];
698          if (strncmpic(url, "http://",  7) == 0)
699          {
700             url += 7;
701          }
702          else if (strncmpic(url, "https://", 8) == 0)
703          {
704             url += 8;
705          }
706          else
707          {
708             url = NULL;
709          }
710
711          if (url)
712          {
713             if ((p = strchr(url, '/')))
714             {
715                http->path = strdup(p);
716                *p = '\0';
717                http->hostport = strdup(url);
718             }
719             /*
720              * Repair broken HTTP requests that don't contain a path
721              */
722             else
723             {
724                http->path = strdup("/");
725                http->hostport = strdup(url);
726             }
727          }
728       }
729    }
730
731    freez(buf);
732
733
734    if (http->hostport == NULL)
735    {
736       free_http_request(http);
737       return;
738    }
739
740    buf = strdup(http->hostport);
741
742
743    /* check if url contains password */
744    n = ssplit(buf, "@", v, SZ(v), 1, 1);
745    if (n == 2)
746    {
747       char * newbuf = NULL;
748       newbuf = strdup(v[1]);
749       freez(buf);
750       buf = newbuf;
751    }
752
753    n = ssplit(buf, ":", v, SZ(v), 1, 1);
754
755    if (n == 1)
756    {
757       http->host = strdup(v[0]);
758       http->port = 80;
759    }
760
761    if (n == 2)
762    {
763       http->host = strdup(v[0]);
764       http->port = atoi(v[1]);
765    }
766
767    freez(buf);
768
769    if (http->host == NULL)
770    {
771       free_http_request(http);
772    }
773
774    if (http->path == NULL)
775    {
776       http->path = strdup("/");
777    }
778
779 }
780
781
782 /* here begins the family of parser functions that reformat header lines */
783
784
785 /*********************************************************************
786  *
787  * Function    :  crumble
788  *
789  * Description :  This is called if a header matches a pattern to "crunch"
790  *
791  * Parameters  :
792  *          1  :  v = Pointer to parsers structure, which basically holds
793  *                headers (client or server) that we want to "crunch"
794  *          2  :  s = header (from sed) to "crunch"
795  *          3  :  csp = Current client state (buffers, headers, etc...)
796  *
797  * Returns     :  Always NULL.
798  *
799  *********************************************************************/
800 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
801 {
802    log_error(LOG_LEVEL_HEADER, "crunch!");
803    return(NULL);
804
805 }
806
807
808 /*********************************************************************
809  *
810  * Function    :  server_content_type
811  *
812  * Description :  Set the content-type for filterable types (text/.*,
813  *                javascript and image/gif) unless filtering has been
814  *                forbidden (CT_TABOO) while parsing earlier headers.
815  *
816  * Parameters  :
817  *          1  :  v = ignored
818  *          2  :  s = header string we are "considering"
819  *          3  :  csp = Current client state (buffers, headers, etc...)
820  *
821  * Returns     :  A duplicate string pointer to this header (ie. pass thru)
822  *
823  *********************************************************************/
824 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
825 {
826    if (csp->content_type != CT_TABOO)
827    {
828       if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
829          csp->content_type = CT_TEXT;
830       else if (strstr(s, " image/gif"))
831          csp->content_type = CT_GIF;
832       else
833          csp->content_type = 0;
834    }
835
836    return(strdup(s));
837
838 }
839
840
841 /*********************************************************************
842  *
843  * Function    :  server_transfer_coding
844  *
845  * Description :  - Prohibit filtering (CT_TABOO) if transfer coding compresses
846  *                - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
847  *                - Change from "chunked" to "identity" if body was chunked
848  *                  but has been de-chunked for filtering.
849  *
850  * Parameters  :
851  *          1  :  v = ignored
852  *          2  :  s = header string we are "considering"
853  *          3  :  csp = Current client state (buffers, headers, etc...)
854  *
855  * Returns     :  A duplicate string pointer to this header (ie. pass thru)
856  *
857  *********************************************************************/
858 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
859 {
860    /*
861     * Turn off pcrs and gif filtering if body compressed
862     */
863    if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
864    {
865       csp->content_type = CT_TABOO;
866    }
867
868    /*
869     * Raise flag if body chunked
870     */
871    if (strstr(s, "chunked"))
872    {
873       csp->flags |= CSP_FLAG_CHUNKED;
874
875       /*
876        * If the body was modified, it has been
877        * de-chunked first, so adjust the header:
878        */
879       if (csp->flags & CSP_FLAG_MODIFIED)
880       {
881          return(strdup("Transfer-Encoding: identity"));
882       }
883    }
884
885    return(strdup(s));
886
887 }
888
889
890 /*********************************************************************
891  *
892  * Function    :  server_content_encoding
893  *
894  * Description :  Prohibit filtering (CT_TABOO) if content encoding compresses
895  *
896  * Parameters  :
897  *          1  :  v = ignored
898  *          2  :  s = header string we are "considering"
899  *          3  :  csp = Current client state (buffers, headers, etc...)
900  *
901  * Returns     :  A duplicate string pointer to this header (ie. pass thru)
902  *
903  *********************************************************************/
904 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
905 {
906    /*
907     * Turn off pcrs and gif filtering if body compressed
908     */
909    if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
910    {
911       csp->content_type = CT_TABOO;
912    }
913
914    return(strdup(s));
915
916 }
917
918
919 /*********************************************************************
920  *
921  * Function    :  server_content_length
922  *
923  * Description :  Adjust Content-Length header if we modified
924  *                the body.
925  *
926  * Parameters  :
927  *          1  :  v = ignored
928  *          2  :  s = header string we are "considering"
929  *          3  :  csp = Current client state (buffers, headers, etc...)
930  *
931  * Returns     :  A duplicate string pointer to this header (ie. pass thru)
932  *
933  *********************************************************************/
934 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
935 {
936    if (csp->content_length != 0) /* Content length has been modified */
937    {
938       char * s2 = (char *) zalloc(100);
939       sprintf(s2, "Content-Length: %d", (int) csp->content_length);
940
941       log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
942       return(s2);
943    }
944    else
945    {
946       return(strdup(s));
947    }
948
949 }
950
951
952 /*********************************************************************
953  *
954  * Function    :  server_content_md5
955  *
956  * Description :  Crumble any Content-MD5 headers if the document was
957  *                modified. FIXME: Should we re-compute instead?
958  *
959  * Parameters  :
960  *          1  :  v = ignored
961  *          2  :  s = header string we are "considering"
962  *          3  :  csp = Current client state (buffers, headers, etc...)
963  *
964  * Returns     :  A duplicate string pointer to this header (ie. pass thru)
965  *
966  *********************************************************************/
967 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
968 {
969    if (csp->flags & CSP_FLAG_MODIFIED)
970    {
971       log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
972       return(NULL);
973    }
974    else
975    {
976       return(strdup(s));
977    }
978
979 }
980
981
982 /*********************************************************************
983  *
984  * Function    :  client_accept_encoding
985  *
986  * Description :  Rewrite the client's Accept-Encoding header so that
987  *                if doesn't allow compression, if the action applies.
988  *                Note: For HTTP/1.0 the absence of the header is enough.
989  *
990  * Parameters  :
991  *          1  :  v = ignored
992  *          2  :  s = header string we are "considering"
993  *          3  :  csp = Current client state (buffers, headers, etc...)
994  *
995  * Returns     :  A copy of the client's original or the modified header.
996  *
997  *********************************************************************/
998 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
999 {
1000    if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1001    {
1002       return(strdup(s));
1003    }
1004    else
1005    {
1006       log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1007
1008       if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1009       {
1010          return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1011       }
1012       else
1013       {
1014          return(NULL);
1015       }
1016    }
1017
1018 }
1019
1020
1021 /*********************************************************************
1022  *
1023  * Function    :  client_te
1024  *
1025  * Description :  Rewrite the client's TE header so that
1026  *                if doesn't allow compression, if the action applies.
1027  *
1028  * Parameters  :
1029  *          1  :  v = ignored
1030  *          2  :  s = header string we are "considering"
1031  *          3  :  csp = Current client state (buffers, headers, etc...)
1032  *
1033  * Returns     :  A copy of the client's original or the modified header.
1034  *
1035  *********************************************************************/
1036 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1037 {
1038    if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1039    {
1040       return(strdup(s));
1041    }
1042    else
1043    {
1044       log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1045       return(NULL);
1046    }
1047
1048 }
1049
1050 /*********************************************************************
1051  *
1052  * Function    :  client_referrer
1053  *
1054  * Description :  Handle the "referer" config setting properly.
1055  *                Called from `sed'.
1056  *
1057  * Parameters  :
1058  *          1  :  v = ignored
1059  *          2  :  s = header (from sed) to "crunch"
1060  *          3  :  csp = Current client state (buffers, headers, etc...)
1061  *
1062  * Returns     :  NULL if crunched, or a malloc'ed string with the original
1063  *                or modified header
1064  *
1065  *********************************************************************/
1066 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1067 {
1068    const char * newval;
1069    char * s2;
1070 #ifdef FEATURE_FORCE_LOAD
1071    /* Since the referrer can include the prefix even
1072     * even if the request itself is non-forced, we must
1073     * clean it unconditionally
1074     */
1075    strclean(s, FORCE_PREFIX);
1076 #endif /* def FEATURE_FORCE_LOAD */
1077
1078 #ifdef FEATURE_TRUST
1079    csp->referrer = strdup(s);
1080 #endif /* def FEATURE_TRUST */
1081
1082    /*
1083     * Are we sending referer?
1084     */
1085    if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1086    {
1087       return(strdup(s));
1088    }
1089
1090    newval = csp->action->string[ACTION_STRING_REFERER];
1091
1092    /*
1093     * Are we blocking referer?
1094     */
1095    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1096    {
1097       log_error(LOG_LEVEL_HEADER, "crunch!");
1098       return(NULL);
1099    }
1100
1101    /*
1102     * Are we forging referer?
1103     */
1104    if (0 == strcmpic(newval, "forge"))
1105    {
1106       /*
1107        * Forge a referer as http://[hostname:port of REQUEST]/
1108        * to fool stupid checks for in-site links
1109        */
1110       log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1111       s2 = strsav(NULL, "Referer: ");
1112       s2 = strsav(s2, "http://");
1113       s2 = strsav(s2, csp->http->hostport);
1114       s2 = strsav(s2, "/");
1115       return(s2);
1116    }
1117
1118    /*
1119     * Have we got a fixed referer?
1120     */
1121    if (0 == strncmpic(newval, "http://", 7))
1122    {
1123       /*
1124        * We have a specific (fixed) referer we want to send.
1125        */
1126       char * s3;
1127
1128       log_error(LOG_LEVEL_HEADER, "modified");
1129
1130       s3 = strsav( NULL, "Referer: " );
1131       s3 = strsav( s3, newval );
1132       return(s3);
1133    }
1134
1135    /* Should never get here! */
1136    log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1137
1138    /*
1139     * Forge is probably the best default.
1140     *
1141     * Forge a referer as http://[hostname:port of REQUEST]/
1142     * to fool stupid checks for in-site links
1143     */
1144    log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1145    s2 = strsav(NULL, "Referer: ");
1146    s2 = strsav(s2, "http://");
1147    s2 = strsav(s2, csp->http->hostport);
1148    s2 = strsav(s2, "/");
1149    return(s2);
1150 }
1151
1152
1153 /*********************************************************************
1154  *
1155  * Function    :  client_uagent
1156  *
1157  * Description :  Handle the "user-agent" config setting properly
1158  *                and remember its original value to enable browser
1159  *                bug workarounds. Called from `sed'.
1160  *
1161  * Parameters  :
1162  *          1  :  v = ignored
1163  *          2  :  s = header (from sed) to "crunch"
1164  *          3  :  csp = Current client state (buffers, headers, etc...)
1165  *
1166  * Returns     :  A malloc'ed pointer to the default agent, or
1167  *                a malloc'ed string pointer to this header (ie. pass thru).
1168  *
1169  *********************************************************************/
1170 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1171 {
1172    const char * newval;
1173    char * s2;
1174
1175    /* Save the client's User-Agent: value */
1176    if (strlen(s) >= 12)
1177    {
1178       csp->http->user_agent = strdup(s + 12);
1179    }
1180
1181 #ifdef FEATURE_IMAGE_DETECT_MSIE
1182    if (strstr (s, "MSIE "))
1183    {
1184       /* This is Microsoft Internet Explorer.
1185        * Enable auto-detect.
1186        */
1187       csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
1188    }
1189 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1190
1191    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1192    {
1193       return(strdup(s));
1194    }
1195
1196    newval = csp->action->string[ACTION_STRING_USER_AGENT];
1197    if (newval == NULL)
1198    {
1199       return(strdup(s));
1200    }
1201
1202    log_error(LOG_LEVEL_HEADER, "modified");
1203
1204    s2 = strsav( NULL, "User-Agent: " );
1205    s2 = strsav( s2, newval );
1206    return(s2);
1207
1208 }
1209
1210
1211 /*********************************************************************
1212  *
1213  * Function    :  client_ua
1214  *
1215  * Description :  Handle "ua-" headers properly.  Called from `sed'.
1216  *
1217  * Parameters  :
1218  *          1  :  v = ignored
1219  *          2  :  s = header (from sed) to "crunch"
1220  *          3  :  csp = Current client state (buffers, headers, etc...)
1221  *
1222  * Returns     :  NULL if crunched, or a malloc'ed string to original header
1223  *
1224  *********************************************************************/
1225 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1226 {
1227    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1228    {
1229       return(strdup(s));
1230    }
1231    else
1232    {
1233       log_error(LOG_LEVEL_HEADER, "crunch!");
1234       return(NULL);
1235    }
1236 }
1237
1238
1239 /*********************************************************************
1240  *
1241  * Function    :  client_from
1242  *
1243  * Description :  Handle the "from" config setting properly.
1244  *                Called from `sed'.
1245  *
1246  * Parameters  :
1247  *          1  :  v = ignored
1248  *          2  :  s = header (from sed) to "crunch"
1249  *          3  :  csp = Current client state (buffers, headers, etc...)
1250  *
1251  * Returns     :  NULL if crunched, or a malloc'ed string to
1252  *                modified/original header.
1253  *
1254  *********************************************************************/
1255 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1256 {
1257    const char * newval;
1258    char * s2;
1259
1260    if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1261    {
1262       return(strdup(s));
1263    }
1264
1265    newval = csp->action->string[ACTION_STRING_FROM];
1266
1267    /*
1268     * Are we blocking referer?
1269     */
1270    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1271    {
1272       log_error(LOG_LEVEL_HEADER, "crunch!");
1273       return(NULL);
1274    }
1275
1276    log_error(LOG_LEVEL_HEADER, " modified");
1277
1278    s2 = strsav( NULL, "From: " );
1279    s2 = strsav( s2, newval );
1280    return(s2);
1281
1282 }
1283
1284
1285 /*********************************************************************
1286  *
1287  * Function    :  client_send_cookie
1288  *
1289  * Description :  Handle the "cookie" header properly.  Called from `sed'.
1290  *                If cookie is accepted, add it to the cookie_list,
1291  *                else we crunch it.  Mmmmmmmmmmm ... cookie ......
1292  *
1293  * Parameters  :
1294  *          1  :  v = pattern of cookie `sed' found matching
1295  *          2  :  s = header (from sed) to "crunch"
1296  *          3  :  csp = Current client state (buffers, headers, etc...)
1297  *
1298  * Returns     :  Always NULL.
1299  *
1300  *********************************************************************/
1301 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1302 {
1303    if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1304    {
1305       enlist(csp->cookie_list, s + v->len + 1);
1306    }
1307    else
1308    {
1309       log_error(LOG_LEVEL_HEADER, " crunch!");
1310    }
1311
1312    /*
1313     * Always return NULL here.  The cookie header
1314     * will be sent at the end of the header.
1315     */
1316    return(NULL);
1317
1318 }
1319
1320
1321 /*********************************************************************
1322  *
1323  * Function    :  client_x_forwarded
1324  *
1325  * Description :  Handle the "x-forwarded-for" config setting properly,
1326  *                also used in the add_client_headers list.  Called from `sed'.
1327  *
1328  * Parameters  :
1329  *          1  :  v = ignored
1330  *          2  :  s = header (from sed) to "crunch"
1331  *          3  :  csp = Current client state (buffers, headers, etc...)
1332  *
1333  * Returns     :  Always NULL.
1334  *
1335  *********************************************************************/
1336 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1337 {
1338    if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1339    {
1340       /* Save it so we can re-add it later */
1341       csp->x_forwarded = strdup(s);
1342    }
1343
1344    /*
1345     * Always return NULL, since this information
1346     * will be sent at the end of the header.
1347     */
1348
1349    return(NULL);
1350
1351 }
1352
1353 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1354 /*********************************************************************
1355  *
1356  * Function    :  client_accept
1357  *
1358  * Description :  Detect whether the client wants HTML or an image.
1359  *                Clients do not always make this information available
1360  *                in a sane way.  Always passes the header through
1361  *                the proxy unchanged.
1362  *
1363  * Parameters  :
1364  *          1  :  v = Ignored.
1365  *          2  :  s = Header string.  Null terminated.
1366  *          3  :  csp = Current client state (buffers, headers, etc...)
1367  *
1368  * Returns     :  Duplicate of argument s.
1369  *
1370  *********************************************************************/
1371 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1372 {
1373 #ifdef FEATURE_IMAGE_DETECT_MSIE
1374    if (strstr (s, "image/gif"))
1375    {
1376       /* Client will accept HTML.  If this seems counterintuitive,
1377        * blame Microsoft.
1378        */
1379       csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1380    }
1381    else
1382    {
1383       csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1384    }
1385 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1386
1387    return(strdup(s));
1388
1389 }
1390 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1391
1392
1393
1394 /* the following functions add headers directly to the header list */
1395
1396
1397 /*********************************************************************
1398  *
1399  * Function    :  client_host_adder
1400  *
1401  * Description :  (re)adds the host header. Called from `sed'.
1402  *
1403  * Parameters  :
1404  *          1  :  csp = Current client state (buffers, headers, etc...)
1405  *
1406  * Returns     :  N/A
1407  *
1408  *********************************************************************/
1409 void client_host_adder(struct client_state *csp)
1410 {
1411    char *p = NULL;
1412
1413    p = strsav(p, "Host: ");
1414    p = strsav(p, csp->http->hostport);
1415
1416    log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1417    enlist(csp->headers, p);
1418
1419    freez(p);
1420 }
1421
1422
1423 /*********************************************************************
1424  *
1425  * Function    :  client_cookie_adder
1426  *
1427  * Description :  Used in the add_client_headers list.  Called from `sed'.
1428  *
1429  * Parameters  :
1430  *          1  :  csp = Current client state (buffers, headers, etc...)
1431  *
1432  * Returns     :  N/A
1433  *
1434  *********************************************************************/
1435 void client_cookie_adder(struct client_state *csp)
1436 {
1437    struct list_entry *lst;
1438    char *tmp = NULL;
1439    char *e;
1440
1441    for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1442    {
1443       if (tmp)
1444       {
1445          tmp = strsav(tmp, "; ");
1446       }
1447       tmp = strsav(tmp, lst->str);
1448    }
1449
1450    for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first;  lst ; lst = lst->next)
1451    {
1452       if (tmp)
1453       {
1454          tmp = strsav(tmp, "; ");
1455       }
1456
1457       if ((e = cookie_encode(lst->str)))
1458       {
1459          tmp = strsav(tmp, e);
1460          freez(e);
1461       }
1462    }
1463
1464    if (tmp)
1465    {
1466       char *ret;
1467
1468       ret = strdup("Cookie: ");
1469       ret = strsav(ret, tmp);
1470       log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1471       enlist(csp->headers, ret);
1472       freez(tmp);
1473       freez(ret);
1474    }
1475
1476 }
1477
1478
1479 /*********************************************************************
1480  *
1481  * Function    :  client_accept_encoding_adder
1482  *
1483  * Description :  Add an Accept-Encoding header to the client's request
1484  *                that disables compression if the action applies, and
1485  *                the header is not already there. Called from `sed'.
1486  *                Note: For HTTP/1.0, the absence of the header is enough.
1487  *
1488  * Parameters  :
1489  *          1  :  csp = Current client state (buffers, headers, etc...)
1490  *
1491  * Returns     :  N/A
1492  *
1493  *********************************************************************/
1494 void client_accept_encoding_adder(struct client_state *csp)
1495 {
1496    if (   ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1497        && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1498    {
1499       enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1500    }
1501
1502 }
1503
1504
1505 /*********************************************************************
1506  *
1507  * Function    :  client_xtra_adder
1508  *
1509  * Description :  Used in the add_client_headers list.  Called from `sed'.
1510  *
1511  * Parameters  :
1512  *          1  :  csp = Current client state (buffers, headers, etc...)
1513  *
1514  * Returns     :  N/A
1515  *
1516  *********************************************************************/
1517 void client_xtra_adder(struct client_state *csp)
1518 {
1519    struct list_entry *lst;
1520
1521    for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1522         lst ; lst = lst->next)
1523    {
1524       log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1525       enlist(csp->headers, lst->str);
1526    }
1527
1528 }
1529
1530
1531 /*********************************************************************
1532  *
1533  * Function    :  client_x_forwarded_adder
1534  *
1535  * Description :  Used in the add_client_headers list.  Called from `sed'.
1536  *
1537  * Parameters  :
1538  *          1  :  csp = Current client state (buffers, headers, etc...)
1539  *
1540  * Returns     :  N/A
1541  *
1542  *********************************************************************/
1543 void client_x_forwarded_adder(struct client_state *csp)
1544 {
1545    char *p = NULL;
1546
1547    if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1548    {
1549       return;
1550    }
1551
1552    if (csp->x_forwarded)
1553    {
1554       p = strsav(p, csp->x_forwarded);
1555       p = strsav(p, ", ");
1556       p = strsav(p, csp->ip_addr_str);
1557    }
1558    else
1559    {
1560       p = strsav(p, "X-Forwarded-For: ");
1561       p = strsav(p, csp->ip_addr_str);
1562    }
1563
1564    log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1565    enlist(csp->headers, p);
1566
1567    freez(p);
1568 }
1569
1570
1571 /*********************************************************************
1572  *
1573  * Function    :  connection_close_adder
1574  *
1575  * Description :  Adds a "Connection: close" header to csp->headers
1576  *                as a temporary fix for the needed but missing HTTP/1.1
1577  *                support. Called from `sed'.
1578  *                FIXME: This whole function shouldn't be neccessary!
1579  *
1580  * Parameters  :
1581  *          1  :  csp = Current client state (buffers, headers, etc...)
1582  *
1583  * Returns     :  N/A
1584  *
1585  *********************************************************************/
1586 void connection_close_adder(struct client_state *csp)
1587 {
1588    enlist(csp->headers, "Connection: close");
1589 }
1590
1591
1592 /*********************************************************************
1593  *
1594  * Function    :  server_http
1595  *
1596  * Description :  - Save the HTTP Status into csp->http->status
1597  *                - Set CT_TABOO to prevent filtering if the answer
1598  *                  is a partial range (HTTP status 206)
1599  *                - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1600  *                  action applies.
1601  *
1602  * Parameters  :
1603  *          1  :  v = parser pattern that matched this header
1604  *          2  :  s = header that matched this pattern
1605  *          3  :  csp = Current client state (buffers, headers, etc...)
1606  *
1607  * Returns     :  Copy of changed  or original answer.
1608  *
1609  *********************************************************************/
1610 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1611 {
1612    char *ret = strdup(s);
1613
1614    sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1615    if (csp->http->status == 206)
1616    {
1617       csp->content_type = CT_TABOO;
1618    }
1619
1620    if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1621    {
1622       ret[7] = '0';
1623       log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1624    }
1625    return(ret);
1626
1627 }
1628
1629
1630 /*********************************************************************
1631  *
1632  * Function    :  server_set_cookie
1633  *
1634  * Description :  Handle the server "cookie" header properly.
1635  *                Log cookie to the jar file.  Then "crunch" it,
1636  *                or accept it.  Called from `sed'.
1637  *
1638  * Parameters  :
1639  *          1  :  v = parser pattern that matched this header
1640  *          2  :  s = header that matched this pattern
1641  *          3  :  csp = Current client state (buffers, headers, etc...)
1642  *
1643  * Returns     :  `crumble' or a newly malloc'ed string.
1644  *
1645  *********************************************************************/
1646 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1647 {
1648 #ifdef FEATURE_COOKIE_JAR
1649    if (csp->config->jar)
1650    {
1651       fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1652    }
1653 #endif /* def FEATURE_COOKIE_JAR */
1654
1655    if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1656    {
1657       return(crumble(v, s, csp));
1658    }
1659
1660    return(strdup(s));
1661
1662 }
1663
1664
1665 #ifdef FEATURE_FORCE_LOAD
1666 /*********************************************************************
1667  *
1668  * Function    :  strclean
1669  *
1670  * Description :  In-Situ-Eliminate all occurances of substring in
1671  *                string
1672  *
1673  * Parameters  :
1674  *          1  :  string = string to clean
1675  *          2  :  substring = substring to eliminate
1676  *
1677  * Returns     :  Number of eliminations
1678  *
1679  *********************************************************************/
1680 int strclean(const char *string, const char *substring)
1681 {
1682    int hits = 0, len = strlen(substring);
1683    char *pos, *p;
1684
1685    while((pos = strstr(string, substring)))
1686    {
1687       p = pos + len;
1688       do
1689       {
1690          *(p - len) = *p;
1691       }
1692       while (*p++ != '\0');
1693
1694       hits++;
1695    }
1696
1697    return(hits);
1698 }
1699 #endif /* def FEATURE_FORCE_LOAD */
1700
1701
1702 /*
1703   Local Variables:
1704   tab-width: 3
1705   end:
1706 */