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