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