Bringing back the "useless" localtime() call
[privoxy.git] / parsers.c
1 const char parsers_rcs[] = "$Id: parsers.c,v 1.77 2006/12/07 18:44:26 fabiankeil 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', ``get_header', `sed', `filter_server_header'
14  *                   `filter_client_header', `filter_header', `crunch_server_header',
15  *                   `server_content_encoding', `server_content_disposition',
16  *                   `server_last_modified', `client_accept_language',
17  *                   `crunch_client_header', `client_if_modified_since',
18  *                   `client_if_none_match', `get_destination_from_headers',
19  *                   `parse_header_time' and `server_set_cookie'.
20  *
21  * Copyright   :  Written by and Copyright (C) 2001-2006 the SourceForge
22  *                Privoxy team. http://www.privoxy.org/
23  *
24  *                Based on the Internet Junkbuster originally written
25  *                by and Copyright (C) 1997 Anonymous Coders and
26  *                Junkbusters Corporation.  http://www.junkbusters.com
27  *
28  *                This program is free software; you can redistribute it
29  *                and/or modify it under the terms of the GNU General
30  *                Public License as published by the Free Software
31  *                Foundation; either version 2 of the License, or (at
32  *                your option) any later version.
33  *
34  *                This program is distributed in the hope that it will
35  *                be useful, but WITHOUT ANY WARRANTY; without even the
36  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
37  *                PARTICULAR PURPOSE.  See the GNU General Public
38  *                License for more details.
39  *
40  *                The GNU General Public License should be included with
41  *                this file.  If not, you can view it at
42  *                http://www.gnu.org/copyleft/gpl.html
43  *                or write to the Free Software Foundation, Inc., 59
44  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
45  *
46  * Revisions   :
47  *    $Log: parsers.c,v $
48  *    Revision 1.77  2006/12/07 18:44:26  fabiankeil
49  *    Rebuild request URL in get_destination_from_headers()
50  *    to make sure redirect{pcrs command} works as expected
51  *    for intercepted requests.
52  *
53  *    Revision 1.76  2006/12/06 19:52:25  fabiankeil
54  *    Added get_destination_from_headers().
55  *
56  *    Revision 1.75  2006/11/13 19:05:51  fabiankeil
57  *    Make pthread mutex locking more generic. Instead of
58  *    checking for OSX and OpenBSD, check for FEATURE_PTHREAD
59  *    and use mutex locking unless there is an _r function
60  *    available. Better safe than sorry.
61  *
62  *    Fixes "./configure --disable-pthread" and should result
63  *    in less threading-related problems on pthread-using platforms,
64  *    but it still doesn't fix BR#1122404.
65  *
66  *    Revision 1.74  2006/10/02 16:59:12  fabiankeil
67  *    The special header "X-Filter: No" now disables
68  *    header filtering as well.
69  *
70  *    Revision 1.73  2006/09/23 13:26:38  roro
71  *    Replace TABs by spaces in source code.
72  *
73  *    Revision 1.72  2006/09/23 12:37:21  fabiankeil
74  *    Don't print a log message every time filter_headers is
75  *    entered or left. It only creates noise without any real
76  *    information.
77  *
78  *    Revision 1.71  2006/09/21 19:55:17  fabiankeil
79  *    Fix +hide-if-modified-since{-n}.
80  *
81  *    Revision 1.70  2006/09/08 12:06:34  fabiankeil
82  *    Have hide-if-modified-since interpret the random
83  *    range value as minutes instead of hours. Allows
84  *    more fine-grained configuration.
85  *
86  *    Revision 1.69  2006/09/06 16:25:51  fabiankeil
87  *    Always have parse_header_time return a pointer
88  *    that actual makes sense, even though we currently
89  *    only need it to detect problems.
90  *
91  *    Revision 1.68  2006/09/06 10:43:32  fabiankeil
92  *    Added config option enable-remote-http-toggle
93  *    to specify if Privoxy should recognize special
94  *    headers (currently only X-Filter) to change its
95  *    behaviour. Disabled by default.
96  *
97  *    Revision 1.67  2006/09/04 11:01:26  fabiankeil
98  *    After filtering de-chunked instances, remove
99  *    "Transfer-Encoding" header entirely instead of changing
100  *    it to "Transfer-Encoding: identity", which is invalid.
101  *    Thanks Michael Shields <shields@msrl.com>. Fixes PR 1318658.
102  *
103  *    Don't use localtime in parse_header_time. An empty time struct
104  *    is good enough, it gets overwritten by strptime anyway.
105  *
106  *    Revision 1.66  2006/09/03 19:38:28  fabiankeil
107  *    Use gmtime_r if available, fallback to gmtime with mutex
108  *    protection for MacOSX and use vanilla gmtime for the rest.
109  *
110  *    Revision 1.65  2006/08/22 10:55:56  fabiankeil
111  *    Changed client_referrer to use the right type (size_t) for
112  *    hostlenght and to shorten the temporary referrer string with
113  *    '\0' instead of adding a useless line break.
114  *
115  *    Revision 1.64  2006/08/17 17:15:10  fabiankeil
116  *    - Back to timegm() using GnuPG's replacement if necessary.
117  *      Using mktime() and localtime() could add a on hour offset if
118  *      the randomize factor was big enough to lead to a summer/wintertime
119  *      switch.
120  *
121  *    - Removed now-useless Privoxy 3.0.3 compatibility glue.
122  *
123  *    - Moved randomization code into pick_from_range().
124  *
125  *    - Changed parse_header_time definition.
126  *      time_t isn't guaranteed to be signed and
127  *      if it isn't, -1 isn't available as error code.
128  *      Changed some variable types in client_if_modified_since()
129  *      because of the same reason.
130  *
131  *    Revision 1.63  2006/08/14 13:18:08  david__schmidt
132  *    OS/2 compilation compatibility fixups
133  *
134  *    Revision 1.62  2006/08/14 08:58:42  fabiankeil
135  *    Changed include from strptime.c to strptime.h
136  *
137  *    Revision 1.61  2006/08/14 08:25:19  fabiankeil
138  *    Split filter-headers{} into filter-client-headers{}
139  *    and filter-server-headers{}.
140  *    Added parse_header_time() to share some code.
141  *    Replaced timegm() with mktime().
142  *
143  *    Revision 1.60  2006/08/12 03:54:37  david__schmidt
144  *    Windows service integration
145  *
146  *    Revision 1.59  2006/08/03 02:46:41  david__schmidt
147  *    Incorporate Fabian Keil's patch work:\rhttp://www.fabiankeil.de/sourcecode/privoxy/
148  *
149  *    Revision 1.58  2006/07/18 14:48:47  david__schmidt
150  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
151  *    with what was really the latest development (the v_3_0_branch branch)
152  *
153  *    Revision 1.56.2.10  2006/01/21 16:16:08  david__schmidt
154  *    Thanks to  Edward Carrel for his patch to modernize OSX's\rpthreads support.  See bug #1409623.
155  *
156  *    Revision 1.56.2.9  2004/10/03 12:53:45  david__schmidt
157  *    Add the ability to check jpeg images for invalid
158  *    lengths of comment blocks.  Defensive strategy
159  *    against the exploit:
160  *       Microsoft Security Bulletin MS04-028
161  *       Buffer Overrun in JPEG Processing (GDI+) Could
162  *       Allow Code Execution (833987)
163  *    Enabled with +inspect-jpegs in actions files.
164  *
165  *    Revision 1.56.2.8  2003/07/11 13:21:25  oes
166  *    Excluded text/plain objects from filtering. This fixes a
167  *    couple of client-crashing, download corruption and
168  *    Privoxy performance issues, whose root cause lies in
169  *    web servers labelling content of unknown type as text/plain.
170  *
171  *    Revision 1.56.2.7  2003/05/06 12:07:26  oes
172  *    Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
173  *
174  *    Revision 1.56.2.6  2003/04/14 21:28:30  oes
175  *    Completing the previous change
176  *
177  *    Revision 1.56.2.5  2003/04/14 12:08:16  oes
178  *    Added temporary workaround for bug in PHP < 4.2.3
179  *
180  *    Revision 1.56.2.4  2003/03/07 03:41:05  david__schmidt
181  *    Wrapping all *_r functions (the non-_r versions of them) with mutex semaphores for OSX.  Hopefully this will take care of all of those pesky crash reports.
182  *
183  *    Revision 1.56.2.3  2002/11/10 04:20:02  hal9
184  *    Fix typo: supressed -> suppressed
185  *
186  *    Revision 1.56.2.2  2002/09/25 14:59:53  oes
187  *    Improved cookie logging
188  *
189  *    Revision 1.56.2.1  2002/09/25 14:52:45  oes
190  *    Added basic support for OPTIONS and TRACE HTTP methods:
191  *     - New parser function client_max_forwards which decrements
192  *       the Max-Forwards HTTP header field of OPTIONS and TRACE
193  *       requests by one before forwarding
194  *     - New parser function client_host which extracts the host
195  *       and port information from the HTTP header field if the
196  *       request URI was not absolute
197  *     - Don't crumble and re-add the Host: header, but only generate
198  *       and append if missing
199  *
200  *    Revision 1.56  2002/05/12 15:34:22  jongfoster
201  *    Fixing typo in a comment
202  *
203  *    Revision 1.55  2002/05/08 16:01:07  oes
204  *    Optimized add_to_iob:
205  *     - Use realloc instead of malloc(), memcpy(), free()
206  *     - Expand to powers of two if possible, to get
207  *       O(log n) reallocs instead of O(n).
208  *     - Moved check for buffer limit here from chat
209  *     - Report failure via returncode
210  *
211  *    Revision 1.54  2002/04/02 15:03:16  oes
212  *    Tiny code cosmetics
213  *
214  *    Revision 1.53  2002/03/26 22:29:55  swa
215  *    we have a new homepage!
216  *
217  *    Revision 1.52  2002/03/24 13:25:43  swa
218  *    name change related issues
219  *
220  *    Revision 1.51  2002/03/13 00:27:05  jongfoster
221  *    Killing warnings
222  *
223  *    Revision 1.50  2002/03/12 01:45:35  oes
224  *    More verbose logging
225  *
226  *    Revision 1.49  2002/03/09 20:03:52  jongfoster
227  *    - Making various functions return int rather than size_t.
228  *      (Undoing a recent change).  Since size_t is unsigned on
229  *      Windows, functions like read_socket that return -1 on
230  *      error cannot return a size_t.
231  *
232  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
233  *      crashes, and also frequently caused JB to jump to 100%
234  *      CPU and stay there.  (Because it thought it had just
235  *      read ((unsigned)-1) == 4Gb of data...)
236  *
237  *    - The signature of write_socket has changed, it now simply
238  *      returns success=0/failure=nonzero.
239  *
240  *    - Trying to get rid of a few warnings --with-debug on
241  *      Windows, I've introduced a new type "jb_socket".  This is
242  *      used for the socket file descriptors.  On Windows, this
243  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
244  *      an int.  The error value can't be -1 any more, so it's
245  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
246  *      Windows it maps to the #define INVALID_SOCKET.)
247  *
248  *    - The signature of bind_port has changed.
249  *
250  *    Revision 1.48  2002/03/07 03:46:53  oes
251  *    Fixed compiler warnings etc
252  *
253  *    Revision 1.47  2002/02/20 23:15:13  jongfoster
254  *    Parsing functions now handle out-of-memory gracefully by returning
255  *    an error code.
256  *
257  *    Revision 1.46  2002/01/17 21:03:47  jongfoster
258  *    Moving all our URL and URL pattern parsing code to urlmatch.c.
259  *
260  *    Revision 1.45  2002/01/09 14:33:03  oes
261  *    Added support for localtime_r.
262  *
263  *    Revision 1.44  2001/12/14 01:22:54  steudten
264  *    Remove 'user:pass@' from 'proto://user:pass@host' for the
265  *    new added header 'Host: ..'. (See Req ID 491818)
266  *
267  *    Revision 1.43  2001/11/23 00:26:38  jongfoster
268  *    Fixing two really stupid errors in my previous commit
269  *
270  *    Revision 1.42  2001/11/22 21:59:30  jongfoster
271  *    Adding code to handle +no-cookies-keep
272  *
273  *    Revision 1.41  2001/11/05 23:43:05  steudten
274  *    Add time+date to log files.
275  *
276  *    Revision 1.40  2001/10/26 20:13:09  jongfoster
277  *    ctype.h is needed in Windows, too.
278  *
279  *    Revision 1.39  2001/10/26 17:40:04  oes
280  *    Introduced get_header_value()
281  *    Removed http->user_agent, csp->referrer and csp->accept_types
282  *    Removed client_accept()
283  *
284  *    Revision 1.38  2001/10/25 03:40:48  david__schmidt
285  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
286  *    threads to call select() simultaneously.  So, it's time to do a real, live,
287  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
288  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
289  *
290  *    Revision 1.37  2001/10/23 21:36:02  jongfoster
291  *    Documenting sed()'s error behaviou (doc change only)
292  *
293  *    Revision 1.36  2001/10/13 12:51:51  joergs
294  *    Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
295  *    force-load), instead crumble Host: and add it (again) in client_host_adder
296  *    (in case we get a HTTP/1.0 request without Host: header and forward it to
297  *    a HTTP/1.1 server/proxy).
298  *
299  *    Revision 1.35  2001/10/09 22:39:21  jongfoster
300  *    assert.h is also required under Win32, so moving out of #ifndef _WIN32
301  *    block.
302  *
303  *    Revision 1.34  2001/10/07 18:50:55  oes
304  *    Added server_content_encoding, renamed server_transfer_encoding
305  *
306  *    Revision 1.33  2001/10/07 18:04:49  oes
307  *    Changed server_http11 to server_http and its pattern to "HTTP".
308  *      Additional functionality: it now saves the HTTP status into
309  *      csp->http->status and sets CT_TABOO for Status 206 (partial range)
310  *
311  *    Revision 1.32  2001/10/07 15:43:28  oes
312  *    Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
313  *       client_te and client_accept_encoding_adder, triggered by the new
314  *       +no-compression action. For HTTP/1.1 the Accept-Encoding header is
315  *       changed to allow only identity and chunked, and the TE header is
316  *       crunched. For HTTP/1.0, Accept-Encoding is crunched.
317  *
318  *    parse_http_request no longer does anything than parsing. The rewriting
319  *      of http->cmd and version mangling are gone. It now also recognizes
320  *      the put and delete methods and saves the url in http->url. Removed
321  *      unused variable.
322  *
323  *    renamed content_type and content_length to have the server_ prefix
324  *
325  *    server_content_type now only works if csp->content_type != CT_TABOO
326  *
327  *    added server_transfer_encoding, which
328  *      - Sets CT_TABOO to prohibit filtering if encoding compresses
329  *      - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
330  *      - Change from "chunked" to "identity" if body was chunked
331  *        but has been de-chunked for filtering.
332  *
333  *    added server_content_md5 which crunches any Content-MD5 headers
334  *      if the body was modified.
335  *
336  *    made server_http11 conditional on +downgrade action
337  *
338  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
339  *
340  *    Revision 1.31  2001/10/05 14:25:02  oes
341  *    Crumble Keep-Alive from Server
342  *
343  *    Revision 1.30  2001/09/29 12:56:03  joergs
344  *    IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
345  *
346  *    Revision 1.29  2001/09/24 21:09:24  jongfoster
347  *    Fixing 2 memory leaks that Guy spotted, where the paramater to
348  *    enlist() was not being free()d.
349  *
350  *    Revision 1.28  2001/09/22 16:32:28  jongfoster
351  *    Removing unused #includes.
352  *
353  *    Revision 1.27  2001/09/20 15:45:25  steudten
354  *
355  *    add casting from size_t to int for printf()
356  *    remove local variable shadow s2
357  *
358  *    Revision 1.26  2001/09/16 17:05:14  jongfoster
359  *    Removing unused #include showarg.h
360  *
361  *    Revision 1.25  2001/09/16 13:21:27  jongfoster
362  *    Changes to use new list functions.
363  *
364  *    Revision 1.24  2001/09/13 23:05:50  jongfoster
365  *    Changing the string paramater to the header parsers a "const".
366  *
367  *    Revision 1.23  2001/09/12 18:08:19  steudten
368  *
369  *    In parse_http_request() header rewriting miss the host value, so
370  *    from http://www.mydomain.com the result was just " / " not
371  *    http://www.mydomain.com/ in case we forward.
372  *
373  *    Revision 1.22  2001/09/10 10:58:53  oes
374  *    Silenced compiler warnings
375  *
376  *    Revision 1.21  2001/07/31 14:46:00  oes
377  *     - Persistant connections now suppressed
378  *     - sed() no longer appends empty header to csp->headers
379  *
380  *    Revision 1.20  2001/07/30 22:08:36  jongfoster
381  *    Tidying up #defines:
382  *    - All feature #defines are now of the form FEATURE_xxx
383  *    - Permanently turned off WIN_GUI_EDIT
384  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
385  *
386  *    Revision 1.19  2001/07/25 17:21:54  oes
387  *    client_uagent now saves copy of User-Agent: header value
388  *
389  *    Revision 1.18  2001/07/13 14:02:46  oes
390  *     - Included fix to repair broken HTTP requests that
391  *       don't contain a path, not even '/'.
392  *     - Removed all #ifdef PCRS
393  *     - content_type now always inspected and classified as
394  *       text, gif or other.
395  *     - formatting / comments
396  *
397  *    Revision 1.17  2001/06/29 21:45:41  oes
398  *    Indentation, CRLF->LF, Tab-> Space
399  *
400  *    Revision 1.16  2001/06/29 13:32:42  oes
401  *    - Fixed a comment
402  *    - Adapted free_http_request
403  *    - Removed logentry from cancelled commit
404  *
405  *    Revision 1.15  2001/06/03 19:12:38  oes
406  *    deleted const struct interceptors
407  *
408  *    Revision 1.14  2001/06/01 18:49:17  jongfoster
409  *    Replaced "list_share" with "list" - the tiny memory gain was not
410  *    worth the extra complexity.
411  *
412  *    Revision 1.13  2001/05/31 21:30:33  jongfoster
413  *    Removed list code - it's now in list.[ch]
414  *    Renamed "permission" to "action", and changed many features
415  *    to use the actions file rather than the global config.
416  *
417  *    Revision 1.12  2001/05/31 17:33:13  oes
418  *
419  *    CRLF -> LF
420  *
421  *    Revision 1.11  2001/05/29 20:11:19  joergs
422  *    '/ * inside comment' warning removed.
423  *
424  *    Revision 1.10  2001/05/29 09:50:24  jongfoster
425  *    Unified blocklist/imagelist/permissionslist.
426  *    File format is still under discussion, but the internal changes
427  *    are (mostly) done.
428  *
429  *    Also modified interceptor behaviour:
430  *    - We now intercept all URLs beginning with one of the following
431  *      prefixes (and *only* these prefixes):
432  *        * http://i.j.b/
433  *        * http://ijbswa.sf.net/config/
434  *        * http://ijbswa.sourceforge.net/config/
435  *    - New interceptors "home page" - go to http://i.j.b/ to see it.
436  *    - Internal changes so that intercepted and fast redirect pages
437  *      are not replaced with an image.
438  *    - Interceptors now have the option to send a binary page direct
439  *      to the client. (i.e. ijb-send-banner uses this)
440  *    - Implemented show-url-info interceptor.  (Which is why I needed
441  *      the above interceptors changes - a typical URL is
442  *      "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
443  *      The previous mechanism would not have intercepted that, and
444  *      if it had been intercepted then it then it would have replaced
445  *      it with an image.)
446  *
447  *    Revision 1.9  2001/05/28 17:26:33  jongfoster
448  *    Fixing segfault if last header was crunched.
449  *    Fixing Windows build (snprintf() is _snprintf() under Win32, but we
450  *    can use the cross-platform sprintf() instead.)
451  *
452  *    Revision 1.8  2001/05/27 22:17:04  oes
453  *
454  *    - re_process_buffer no longer writes the modified buffer
455  *      to the client, which was very ugly. It now returns the
456  *      buffer, which it is then written by chat.
457  *
458  *    - content_length now adjusts the Content-Length: header
459  *      for modified documents rather than crunch()ing it.
460  *      (Length info in csp->content_length, which is 0 for
461  *      unmodified documents)
462  *
463  *    - For this to work, sed() is called twice when filtering.
464  *
465  *    Revision 1.7  2001/05/27 13:19:06  oes
466  *    Patched Joergs solution for the content-length in.
467  *
468  *    Revision 1.6  2001/05/26 13:39:32  jongfoster
469  *    Only crunches Content-Length header if applying RE filtering.
470  *    Without this fix, Microsoft Windows Update wouldn't work.
471  *
472  *    Revision 1.5  2001/05/26 00:28:36  jongfoster
473  *    Automatic reloading of config file.
474  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
475  *    Most of the global variables have been moved to a new
476  *    struct configuration_spec, accessed through csp->config->globalname
477  *    Most of the globals remaining are used by the Win32 GUI.
478  *
479  *    Revision 1.4  2001/05/22 18:46:04  oes
480  *
481  *    - Enabled filtering banners by size rather than URL
482  *      by adding patterns that replace all standard banner
483  *      sizes with the "Junkbuster" gif to the re_filterfile
484  *
485  *    - Enabled filtering WebBugs by providing a pattern
486  *      which kills all 1x1 images
487  *
488  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
489  *      which is selected by the (nonstandard and therefore
490  *      capital) letter 'U' in the option string.
491  *      It causes the quantifiers to be ungreedy by default.
492  *      Appending a ? turns back to greedy (!).
493  *
494  *    - Added a new interceptor ijb-send-banner, which
495  *      sends back the "Junkbuster" gif. Without imagelist or
496  *      MSIE detection support, or if tinygif = 1, or the
497  *      URL isn't recognized as an imageurl, a lame HTML
498  *      explanation is sent instead.
499  *
500  *    - Added new feature, which permits blocking remote
501  *      script redirects and firing back a local redirect
502  *      to the browser.
503  *      The feature is conditionally compiled, i.e. it
504  *      can be disabled with --disable-fast-redirects,
505  *      plus it must be activated by a "fast-redirects"
506  *      line in the config file, has its own log level
507  *      and of course wants to be displayed by show-proxy-args
508  *      Note: Boy, all the #ifdefs in 1001 locations and
509  *      all the fumbling with configure.in and acconfig.h
510  *      were *way* more work than the feature itself :-(
511  *
512  *    - Because a generic redirect template was needed for
513  *      this, tinygif = 3 now uses the same.
514  *
515  *    - Moved GIFs, and other static HTTP response templates
516  *      to project.h
517  *
518  *    - Some minor fixes
519  *
520  *    - Removed some >400 CRs again (Jon, you really worked
521  *      a lot! ;-)
522  *
523  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
524  *    Version 2.9.4 checkin.
525  *    - Merged popupfile and cookiefile, and added control over PCRS
526  *      filtering, in new "permissionsfile".
527  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
528  *      file error you now get a message box (in the Win32 GUI) rather
529  *      than the program exiting with no explanation.
530  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
531  *      skipping.
532  *    - Removed tabs from "config"
533  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
534  *    - Bumped up version number.
535  *
536  *    Revision 1.2  2001/05/17 23:02:36  oes
537  *     - Made referrer option accept 'L' as a substitute for '§'
538  *
539  *    Revision 1.1.1.1  2001/05/15 13:59:01  oes
540  *    Initial import of version 2.9.3 source tree
541  *
542  *
543  *********************************************************************/
544 \f
545
546 #include "config.h"
547
548 #ifndef _WIN32
549 #include <stdio.h>
550 #include <sys/types.h>
551 #endif
552
553 #include <stdlib.h>
554 #include <ctype.h>
555 #include <assert.h>
556 #include <string.h>
557 #include <time.h>
558
559 #if !defined(_WIN32) && !defined(__OS2__)
560 #include <unistd.h>
561 #endif
562
563 #include "project.h"
564
565 #ifdef FEATURE_PTHREAD
566 #include "jcc.h"
567 /* jcc.h is for mutex semapores only */
568 #endif /* def FEATURE_PTHREAD */
569 #include "list.h"
570 #include "parsers.h"
571 #include "encode.h"
572 #include "ssplit.h"
573 #include "errlog.h"
574 #include "jbsockets.h"
575 #include "miscutil.h"
576 #include "list.h"
577
578 #ifndef HAVE_STRPTIME
579 #include "strptime.h"
580 #endif
581
582 const char parsers_h_rcs[] = PARSERS_H_VERSION;
583
584 /* Fix a problem with Solaris.  There should be no effect on other
585  * platforms.
586  * Solaris's isspace() is a macro which uses it's argument directly
587  * as an array index.  Therefore we need to make sure that high-bit
588  * characters generate +ve values, and ideally we also want to make
589  * the argument match the declared parameter type of "int".
590  *
591  * Why did they write a character function that can't take a simple
592  * "char" argument?  Doh!
593  */
594 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
595 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
596
597
598 const struct parsers client_patterns[] = {
599    { "referer:",                  8,   client_referrer },
600    { "user-agent:",              11,   client_uagent },
601    { "ua-",                       3,   client_ua },
602    { "from:",                     5,   client_from },
603    { "cookie:",                   7,   client_send_cookie },
604    { "x-forwarded-for:",         16,   client_x_forwarded },
605    { "Accept-Encoding:",         16,   client_accept_encoding },
606    { "TE:",                       3,   client_te },
607    { "Host:",                     5,   client_host },
608    { "if-modified-since:",       18,   client_if_modified_since },
609    { "Keep-Alive:",              11,   crumble },
610    { "connection:",              11,   crumble },
611    { "proxy-connection:",        17,   crumble },
612    { "max-forwards:",            13,   client_max_forwards },
613    { "Accept-Language:",         16,   client_accept_language },
614    { "if-none-match:",           14,   client_if_none_match },
615    { "X-Filter:",                 9,   client_x_filter },
616    { "*",                         0,   crunch_client_header },
617    { "*",                         0,   filter_client_header },
618    { NULL,                        0,   NULL }
619 };
620
621 const struct parsers server_patterns[] = {
622    { "HTTP",                      4, server_http },
623    { "set-cookie:",              11, server_set_cookie },
624    { "connection:",              11, crumble },
625    { "Content-Type:",            13, server_content_type },
626    { "Content-Length:",          15, server_content_length },
627    { "Content-MD5:",             12, server_content_md5 },
628    { "Content-Encoding:",        17, server_content_encoding },
629    { "Transfer-Encoding:",       18, server_transfer_coding },
630    { "Keep-Alive:",              11, crumble },
631    { "content-disposition:",     20, server_content_disposition },
632    { "Last-Modified:",           14, server_last_modified },
633    { "*",                         0, crunch_server_header },
634    { "*",                         0, filter_server_header },
635    { NULL, 0, NULL }
636 };
637
638 const struct parsers server_patterns_light[] = {
639    { "Content-Length:",          15, server_content_length },
640    { "Transfer-Encoding:",       18, server_transfer_coding },
641    { NULL, 0, NULL }
642 };
643
644 const add_header_func_ptr add_client_headers[] = {
645    client_host_adder,
646    client_cookie_adder,
647    client_x_forwarded_adder,
648    client_xtra_adder,
649    /* Temporarily disabled:    client_accept_encoding_adder, */
650    connection_close_adder,
651    NULL
652 };
653
654
655 const add_header_func_ptr add_server_headers[] = {
656    connection_close_adder,
657    NULL
658 };
659
660 /*********************************************************************
661  *
662  * Function    :  flush_socket
663  *
664  * Description :  Write any pending "buffered" content.
665  *
666  * Parameters  :
667  *          1  :  fd = file descriptor of the socket to read
668  *          2  :  csp = Current client state (buffers, headers, etc...)
669  *
670  * Returns     :  On success, the number of bytes written are returned (zero
671  *                indicates nothing was written).  On error, -1 is returned,
672  *                and errno is set appropriately.  If count is zero and the
673  *                file descriptor refers to a regular file, 0 will be
674  *                returned without causing any other effect.  For a special
675  *                file, the results are not portable.
676  *
677  *********************************************************************/
678 int flush_socket(jb_socket fd, struct client_state *csp)
679 {
680    struct iob *iob = csp->iob;
681    int len = iob->eod - iob->cur;
682
683    if (len <= 0)
684    {
685       return(0);
686    }
687
688    if (write_socket(fd, iob->cur, (size_t)len))
689    {
690       return(-1);
691    }
692    iob->eod = iob->cur = iob->buf;
693    return(len);
694
695 }
696
697
698 /*********************************************************************
699  *
700  * Function    :  add_to_iob
701  *
702  * Description :  Add content to the buffered page, expanding the
703  *                buffer if necessary.
704  *
705  * Parameters  :
706  *          1  :  csp = Current client state (buffers, headers, etc...)
707  *          2  :  buf = holds the content to be added to the page
708  *          3  :  n = number of bytes to be added
709  *
710  * Returns     :  JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
711  *                or buffer limit reached.
712  *
713  *********************************************************************/
714 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
715 {
716    struct iob *iob = csp->iob;
717    size_t used, offset, need, want;
718    char *p;
719
720    if (n <= 0) return JB_ERR_OK;
721
722    used   = iob->eod - iob->buf;
723    offset = iob->cur - iob->buf;
724    need   = used + n + 1;
725
726    /*
727     * If the buffer can't hold the new data, extend it first.
728     * Use the next power of two if possible, else use the actual need.
729     */
730    if (need > csp->config->buffer_limit)
731    {
732       log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
733       return JB_ERR_MEMORY;
734    }
735
736    if (need > iob->size)
737    {
738       for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
739       
740       if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
741       {
742          iob->size = want;
743       }
744       else if (NULL != (p = (char *)realloc(iob->buf, need)))
745       {
746          iob->size = need;
747       }
748       else
749       {
750          log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
751          return JB_ERR_MEMORY;
752       }
753
754       /* Update the iob pointers */
755       iob->cur = p + offset;
756       iob->eod = p + used;
757       iob->buf = p;
758    }
759
760    /* copy the new data into the iob buffer */
761    memcpy(iob->eod, buf, (size_t)n);
762
763    /* point to the end of the data */
764    iob->eod += n;
765
766    /* null terminate == cheap insurance */
767    *iob->eod = '\0';
768
769    return JB_ERR_OK;
770
771 }
772
773
774 /*********************************************************************
775  *
776  * Function    :  get_header
777  *
778  * Description :  This (odd) routine will parse the csp->iob
779  *
780  * Parameters  :
781  *          1  :  csp = Current client state (buffers, headers, etc...)
782  *
783  * Returns     :  Any one of the following:
784  *
785  * 1) a pointer to a dynamically allocated string that contains a header line
786  * 2) NULL  indicating that the end of the header was reached
787  * 3) ""    indicating that the end of the iob was reached before finding
788  *          a complete header line.
789  *
790  *********************************************************************/
791 char *get_header(struct client_state *csp)
792 {
793    struct iob *iob;
794    char *p, *q, *ret;
795    iob = csp->iob;
796
797    if ((iob->cur == NULL)
798       || ((p = strchr(iob->cur, '\n')) == NULL))
799    {
800       return(""); /* couldn't find a complete header */
801    }
802
803    *p = '\0';
804
805    ret = strdup(iob->cur);
806    if (ret == NULL)
807    {
808       /* FIXME No way to handle error properly */
809       log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
810    }
811
812    iob->cur = p+1;
813
814    if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
815
816    /* is this a blank line (i.e. the end of the header) ? */
817    if (*ret == '\0')
818    {
819       freez(ret);
820       return(NULL);
821    }
822
823    return(ret);
824
825 }
826
827
828 /*********************************************************************
829  *
830  * Function    :  get_header_value
831  *
832  * Description :  Get the value of a given header from a chained list
833  *                of header lines or return NULL if no such header is
834  *                present in the list.
835  *
836  * Parameters  :
837  *          1  :  header_list = pointer to list
838  *          2  :  header_name = string with name of header to look for.
839  *                              Trailing colon required, capitalization
840  *                              doesn't matter.
841  *
842  * Returns     :  NULL if not found, else value of header
843  *
844  *********************************************************************/
845 char *get_header_value(const struct list *header_list, const char *header_name)
846 {
847    struct list_entry *cur_entry;
848    char *ret = NULL;
849    size_t length = 0;
850
851    assert(header_list);
852    assert(header_name);
853    length = strlen(header_name);
854
855    for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
856    {
857       if (cur_entry->str)
858       {
859          if (!strncmpic(cur_entry->str, header_name, length))
860          {
861             /*
862              * Found: return pointer to start of value
863              */
864             ret = (char *) (cur_entry->str + length);
865             while (*ret && ijb_isspace(*ret)) ret++;
866             return(ret);
867          }
868       }
869    }
870
871    /* 
872     * Not found
873     */
874    return NULL;
875
876 }
877
878 /*********************************************************************
879  *
880  * Function    :  sed
881  *
882  * Description :  add, delete or modify lines in the HTTP header streams.
883  *                On entry, it receives a linked list of headers space
884  *                that was allocated dynamically (both the list nodes
885  *                and the header contents).
886  *
887  *                As a side effect it frees the space used by the original
888  *                header lines.
889  *
890  * Parameters  :
891  *          1  :  pats = list of patterns to match against headers
892  *          2  :  more_headers = list of functions to add more
893  *                headers (client or server)
894  *          3  :  csp = Current client state (buffers, headers, etc...)
895  *
896  * Returns     :  Single pointer to a fully formed header, or NULL
897  *                on out-of-memory error.
898  *
899  *********************************************************************/
900 char *sed(const struct parsers pats[],
901           const add_header_func_ptr more_headers[],
902           struct client_state *csp)
903 {
904    struct list_entry *p;
905    const struct parsers *v;
906    const add_header_func_ptr *f;
907    jb_err err = JB_ERR_OK;
908    int first_run;
909
910    /*
911     * If filtering is enabled, sed is run twice,
912     * but most of the work needs to be done only once.
913     */
914    first_run = (more_headers != NULL ) ? 1 : 0;
915
916    if (first_run) /* Parse and print */
917    {
918       for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
919       {
920          for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
921          {
922             /* Header crunch()ed in previous run? -> ignore */
923             if (p->str == NULL) continue;
924
925             if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
926
927             /* Does the current parser handle this header? */
928             if ((strncmpic(p->str, v->str, v->len) == 0) || (v->len == CHECK_EVERY_HEADER_REMAINING))
929             {
930                err = v->parser(csp, (char **)&(p->str));
931             }
932          }
933       }
934       /* place any additional headers on the csp->headers list */
935       for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
936       {
937          err = (*f)(csp);
938       }
939    }
940    else /* Parse only */
941    {
942       /*
943        * The second run is only needed if the body was modified
944        * and the content-lenght has changed.
945        */
946       if (strncmpic(csp->http->cmd, "HEAD", 4))
947       {
948          /*XXX: Code duplication*/
949          for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
950          {
951             for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
952             {
953                /* Header crunch()ed in previous run? -> ignore */
954                if (p->str == NULL) continue;
955
956                /* Does the current parser handle this header? */
957                if (strncmpic(p->str, v->str, v->len) == 0)
958                {
959                   err = v->parser(csp, (char **)&(p->str));
960                }
961             }
962          }
963       }
964    }
965
966    if (err != JB_ERR_OK)
967    {
968       return NULL;
969    }
970
971    return list_to_text(csp->headers);
972 }
973
974
975 /* here begins the family of parser functions that reformat header lines */
976
977 /*********************************************************************
978  *
979  * Function    :  filter_server_header
980  *
981  * Description :  Checks if server header filtering is enabled.
982  *                If it is, filter_header is called to do the work. 
983  *
984  * Parameters  :
985  *          1  :  csp = Current client state (buffers, headers, etc...)
986  *          2  :  header = On input, pointer to header to modify.
987  *                On output, pointer to the modified header, or NULL
988  *                to remove the header.  This function frees the
989  *                original string if necessary.
990  *
991  * Returns     :  JB_ERR_OK on success and always succeeds
992  *
993  *********************************************************************/
994 jb_err filter_server_header(struct client_state *csp, char **header)
995 {
996    if (csp->action->flags & ACTION_FILTER_SERVER_HEADERS)
997    {
998       filter_header(csp, header);
999    }
1000    return(JB_ERR_OK);
1001 }
1002
1003 /*********************************************************************
1004  *
1005  * Function    :  filter_client_header
1006  *
1007  * Description :  Checks if client header filtering is enabled.
1008  *                If it is, filter_header is called to do the work. 
1009  *
1010  * Parameters  :
1011  *          1  :  csp = Current client state (buffers, headers, etc...)
1012  *          2  :  header = On input, pointer to header to modify.
1013  *                On output, pointer to the modified header, or NULL
1014  *                to remove the header.  This function frees the
1015  *                original string if necessary.
1016  *
1017  * Returns     :  JB_ERR_OK on success and always succeeds
1018  *
1019  *********************************************************************/
1020 jb_err filter_client_header(struct client_state *csp, char **header)
1021 {
1022    if (csp->action->flags & ACTION_FILTER_CLIENT_HEADERS)
1023    {
1024       filter_header(csp, header);
1025    }
1026    return(JB_ERR_OK);
1027 }
1028
1029 /*********************************************************************
1030  *
1031  * Function    :  filter_header
1032  *
1033  * Description :  Executes all text substitutions from all applying
1034  *                +filter actions on the header.
1035  *                Most of the code was copied from pcrs_filter_response,
1036  *                including the rather short variable names
1037  *
1038  * Parameters  :
1039  *          1  :  csp = Current client state (buffers, headers, etc...)
1040  *          2  :  header = On input, pointer to header to modify.
1041  *                On output, pointer to the modified header, or NULL
1042  *                to remove the header.  This function frees the
1043  *                original string if necessary.
1044  *
1045  * Returns     :  JB_ERR_OK on success and always succeeds
1046  *
1047  *********************************************************************/
1048 jb_err filter_header(struct client_state *csp, char **header)
1049 {
1050    int hits=0;
1051    int matches;
1052    size_t size = strlen(*header);
1053
1054    char *newheader = NULL;
1055    pcrs_job *job;
1056
1057    struct file_list *fl;
1058    struct re_filterfile_spec *b;
1059    struct list_entry *filtername;
1060
1061    int i, found_filters = 0;
1062
1063    /*
1064     * Need to check the set of re_filterfiles...
1065     */
1066    for (i = 0; i < MAX_AF_FILES; i++)
1067    {
1068       fl = csp->rlist[i];
1069       if (NULL != fl)
1070       {
1071          if (NULL != fl->f)
1072          {
1073            found_filters = 1;
1074            break;
1075          }
1076       }
1077    }
1078
1079    if (0 == found_filters)
1080    {
1081       log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1082          return(JB_ERR_OK);
1083    }
1084
1085    for (i = 0; i < MAX_AF_FILES; i++)
1086    {
1087       fl = csp->rlist[i];
1088       if ((NULL == fl) || (NULL == fl->f))
1089          break;
1090       /*
1091        * For all applying +filter actions, look if a filter by that
1092        * name exists and if yes, execute its pcrs_joblist on the
1093        * buffer.
1094        */
1095       for (b = fl->f; b; b = b->next)
1096       {
1097          for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
1098               filtername ; filtername = filtername->next)
1099          {
1100             if (strcmp(b->name, filtername->str) == 0)
1101             {
1102                int current_hits = 0;
1103
1104                if ( NULL == b->joblist )
1105                {
1106                   log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1107                   continue;
1108                }
1109
1110                log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s (size %d) with filter %s...",
1111                          *header, size, b->name);
1112
1113                /* Apply all jobs from the joblist */
1114                for (job = b->joblist; NULL != job; job = job->next)
1115                {
1116                   matches = pcrs_execute(job, *header, size, &newheader, &size);
1117                   if ( 0 < matches )
1118                   {
1119                      current_hits += matches; 
1120                      log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
1121                      freez(*header);
1122                      *header = newheader;
1123                   }
1124                   else if ( 0 == matches )
1125                   {
1126                      /* Filter doesn't change header */
1127                      freez(newheader);
1128                   }
1129                   else
1130                   {
1131                      /* RegEx failure */
1132                      log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
1133                         *header, b->name, pcrs_strerror(matches));
1134                      if( newheader != NULL)
1135                      {
1136                         log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
1137                         freez(newheader);
1138                      }
1139                   }
1140                }
1141                log_error(LOG_LEVEL_RE_FILTER, " ...produced %d hits (new size %d).", current_hits, size);
1142                hits += current_hits;
1143             }
1144          }
1145       }
1146    }
1147
1148    if ( 0 == size )
1149    {
1150       log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
1151       freez(*header);
1152    }
1153
1154    return(JB_ERR_OK);
1155 }
1156
1157
1158 /*********************************************************************
1159  *
1160  * Function    :  crumble
1161  *
1162  * Description :  This is called if a header matches a pattern to "crunch"
1163  *
1164  * Parameters  :
1165  *          1  :  csp = Current client state (buffers, headers, etc...)
1166  *          2  :  header = On input, pointer to header to modify.
1167  *                On output, pointer to the modified header, or NULL
1168  *                to remove the header.  This function frees the
1169  *                original string if necessary.
1170  *
1171  * Returns     :  JB_ERR_OK on success, or
1172  *                JB_ERR_MEMORY on out-of-memory error.
1173  *
1174  *********************************************************************/
1175 jb_err crumble(struct client_state *csp, char **header)
1176 {
1177    log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
1178    freez(*header);
1179    return JB_ERR_OK;
1180 }
1181
1182 /*********************************************************************
1183  *
1184  * Function    :  crunch_server_header
1185  *
1186  * Description :  Crunch server header if it matches a string supplied by the
1187  *                user. Called from `sed'.
1188  *
1189  * Parameters  :
1190  *          1  :  csp = Current client state (buffers, headers, etc...)
1191  *          2  :  header = On input, pointer to header to modify.
1192  *                On output, pointer to the modified header, or NULL
1193  *                to remove the header.  This function frees the
1194  *                original string if necessary.
1195  *
1196  * Returns     :  JB_ERR_OK on success and always succeeds
1197  *
1198  *********************************************************************/
1199 jb_err crunch_server_header(struct client_state *csp, char **header)
1200 {
1201    const char *crunch_pattern;
1202    /*Is there a header to crunch*/
1203
1204    if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
1205    {
1206       crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
1207
1208       /*Is the current header the lucky one?*/
1209       if (strstr(*header, crunch_pattern))
1210       {
1211          log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);  
1212          freez(*header);
1213       }
1214    }
1215
1216    return JB_ERR_OK;
1217 }
1218 /*********************************************************************
1219  *
1220  * Function    :  server_content_type
1221  *
1222  * Description :  Set the content-type for filterable types (text/.*,
1223  *                javascript and image/gif) unless filtering has been
1224  *                forbidden (CT_TABOO) while parsing earlier headers.
1225  *                NOTE: Since text/plain is commonly used by web servers
1226  *                      for files whose correct type is unknown, we don't
1227  *                      set CT_TEXT for it.
1228  *
1229  * Parameters  :
1230  *          1  :  csp = Current client state (buffers, headers, etc...)
1231  *          2  :  header = On input, pointer to header to modify.
1232  *                On output, pointer to the modified header, or NULL
1233  *                to remove the header.  This function frees the
1234  *                original string if necessary.
1235  *
1236  * Returns     :  JB_ERR_OK on success, or
1237  *                JB_ERR_MEMORY on out-of-memory error.
1238  *
1239  *********************************************************************/
1240 jb_err server_content_type(struct client_state *csp, char **header)
1241 {
1242    const char *newval;
1243    
1244    newval = csp->action->string[ACTION_STRING_CONTENT_TYPE]; 
1245
1246    if (csp->content_type != CT_TABOO)
1247    {
1248       if ((strstr(*header, " text/") && !strstr(*header, "plain"))
1249        || strstr(*header, "xml")
1250        || strstr(*header, "application/x-javascript"))
1251          csp->content_type = CT_TEXT;
1252       else if (strstr(*header, " image/gif"))
1253          csp->content_type = CT_GIF;
1254       else if (strstr(*header, " image/jpeg"))
1255          csp->content_type = CT_JPEG;
1256       else
1257          csp->content_type = 0;
1258    }
1259    /*
1260     * Are we enabling text mode by force?
1261     */
1262    if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
1263    {
1264      /*
1265       * Do we really have to?
1266       */
1267       if (csp->content_type == CT_TEXT)
1268       {
1269          log_error(LOG_LEVEL_HEADER, "Text mode is already enabled.");   
1270       }
1271       else
1272       {
1273          csp->content_type = CT_TEXT;
1274          log_error(LOG_LEVEL_HEADER, "Text mode enabled by force. Take cover!");   
1275       }
1276    }
1277    /*
1278     * Are we messing with the content type?
1279     */ 
1280    if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
1281    { 
1282      /*
1283       * Make sure the user doesn't accidently
1284       * change the content type of binary documents. 
1285       */ 
1286      if (csp->content_type == CT_TEXT)
1287      { 
1288         freez(*header);
1289         *header = strdup("Content-Type: ");
1290         string_append(header, newval);
1291         
1292         if (header == NULL)
1293         { 
1294            log_error(LOG_LEVEL_HEADER, "Insufficient memory. Conten-Type crunched without replacement!");
1295            return JB_ERR_MEMORY;
1296         }
1297         log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
1298      }
1299      else
1300      {
1301         log_error(LOG_LEVEL_HEADER, "%s not replaced. It doesn't look like text. "
1302                  "Enable force-text-mode if you know what you're doing.", *header);   
1303      }
1304    }  
1305    return JB_ERR_OK;
1306 }
1307
1308
1309 /*********************************************************************
1310  *
1311  * Function    :  server_transfer_coding
1312  *
1313  * Description :  - Prohibit filtering (CT_TABOO) if transfer coding compresses
1314  *                - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
1315  *                - Remove header if body was chunked but has been
1316  *                  de-chunked for filtering.
1317  *
1318  * Parameters  :
1319  *          1  :  csp = Current client state (buffers, headers, etc...)
1320  *          2  :  header = On input, pointer to header to modify.
1321  *                On output, pointer to the modified header, or NULL
1322  *                to remove the header.  This function frees the
1323  *                original string if necessary.
1324  *
1325  * Returns     :  JB_ERR_OK on success, or
1326  *                JB_ERR_MEMORY on out-of-memory error.
1327  *
1328  *********************************************************************/
1329 jb_err server_transfer_coding(struct client_state *csp, char **header)
1330 {
1331    /*
1332     * Turn off pcrs and gif filtering if body compressed
1333     */
1334    if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1335    {
1336       csp->content_type = CT_TABOO;
1337    }
1338
1339    /*
1340     * Raise flag if body chunked
1341     */
1342    if (strstr(*header, "chunked"))
1343    {
1344       csp->flags |= CSP_FLAG_CHUNKED;
1345
1346       /*
1347        * If the body was modified, it has been de-chunked first
1348        * and the header must be removed. 
1349        */
1350       if (csp->flags & CSP_FLAG_MODIFIED)
1351       {
1352          log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
1353          freez(*header);
1354       }
1355    }
1356
1357    return JB_ERR_OK;
1358 }
1359
1360
1361 /*********************************************************************
1362  *
1363  * Function    :  server_content_encoding
1364  *
1365  * Description :  Prohibit filtering (CT_TABOO) if content encoding compresses
1366  *
1367  * Parameters  :
1368  *          1  :  csp = Current client state (buffers, headers, etc...)
1369  *          2  :  header = On input, pointer to header to modify.
1370  *                On output, pointer to the modified header, or NULL
1371  *                to remove the header.  This function frees the
1372  *                original string if necessary.
1373  *
1374  * Returns     :  JB_ERR_OK on success, or
1375  *                JB_ERR_MEMORY on out-of-memory error.
1376  *
1377  *********************************************************************/
1378 jb_err server_content_encoding(struct client_state *csp, char **header)
1379 {
1380    /*
1381     * Turn off pcrs and gif filtering if body compressed
1382     */
1383    if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1384    {
1385       csp->content_type = CT_TABOO;
1386    }
1387
1388    return JB_ERR_OK;
1389
1390 }
1391
1392
1393 /*********************************************************************
1394  *
1395  * Function    :  server_content_length
1396  *
1397  * Description :  Adjust Content-Length header if we modified
1398  *                the body.
1399  *
1400  * Parameters  :
1401  *          1  :  csp = Current client state (buffers, headers, etc...)
1402  *          2  :  header = On input, pointer to header to modify.
1403  *                On output, pointer to the modified header, or NULL
1404  *                to remove the header.  This function frees the
1405  *                original string if necessary.
1406  *
1407  * Returns     :  JB_ERR_OK on success, or
1408  *                JB_ERR_MEMORY on out-of-memory error.
1409  *
1410  *********************************************************************/
1411 jb_err server_content_length(struct client_state *csp, char **header)
1412 {
1413    if (csp->content_length != 0) /* Content length could have been modified */
1414    {
1415       /*
1416        * XXX: Shouldn't we check if csp->content_length
1417        * is different than the original value?
1418        */
1419       freez(*header);
1420       *header = (char *) zalloc(100);
1421       if (*header == NULL)
1422       {
1423          return JB_ERR_MEMORY;
1424       }
1425
1426       sprintf(*header, "Content-Length: %d", (int) csp->content_length);
1427
1428       log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1429    }
1430
1431    return JB_ERR_OK;
1432 }
1433
1434
1435 /*********************************************************************
1436  *
1437  * Function    :  server_content_md5
1438  *
1439  * Description :  Crumble any Content-MD5 headers if the document was
1440  *                modified. FIXME: Should we re-compute instead?
1441  *
1442  * Parameters  :
1443  *          1  :  csp = Current client state (buffers, headers, etc...)
1444  *          2  :  header = On input, pointer to header to modify.
1445  *                On output, pointer to the modified header, or NULL
1446  *                to remove the header.  This function frees the
1447  *                original string if necessary.
1448  *
1449  * Returns     :  JB_ERR_OK on success, or
1450  *                JB_ERR_MEMORY on out-of-memory error.
1451  *
1452  *********************************************************************/
1453 jb_err server_content_md5(struct client_state *csp, char **header)
1454 {
1455    if (csp->flags & CSP_FLAG_MODIFIED)
1456    {
1457       log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1458       freez(*header);
1459    }
1460
1461    return JB_ERR_OK;
1462 }
1463
1464 /*********************************************************************
1465  *
1466  * Function    :  server_content_disposition
1467  *
1468  * Description :  If enabled, blocks or modifies the "content-disposition" header.
1469  *                Called from `sed'.
1470  *
1471  * Parameters  :
1472  *          1  :  csp = Current client state (buffers, headers, etc...)
1473  *          2  :  header = On input, pointer to header to modify.
1474  *                On output, pointer to the modified header, or NULL
1475  *                to remove the header.  This function frees the
1476  *                original string if necessary.
1477  *
1478  * Returns     :  JB_ERR_OK on success, or
1479  *                JB_ERR_MEMORY on out-of-memory error.
1480  *
1481  *********************************************************************/
1482 jb_err server_content_disposition(struct client_state *csp, char **header)
1483 {
1484    const char *newval;
1485
1486    /*
1487     * Are we messing with the content-disposition header?
1488     */
1489    if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
1490    {
1491       /*Me tinks not*/
1492       return JB_ERR_OK;
1493    }
1494
1495    newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
1496
1497    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1498    {
1499       /*
1500        * Blocking content-disposition header
1501        */
1502       log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1503       freez(*header);
1504       return JB_ERR_OK;
1505    }
1506    else
1507    {  
1508       /*
1509        * Replacing content-disposition header
1510        */
1511       freez(*header);
1512       *header = strdup("content-disposition: ");
1513       string_append(header, newval);   
1514
1515       if (*header == NULL)
1516       {
1517          log_error(LOG_LEVEL_HEADER, "Insufficent memory. content-disposition header not fully replaced.");  
1518       }
1519       else
1520       {
1521          log_error(LOG_LEVEL_HEADER, "content-disposition header crunched and replaced with: %s", *header);
1522       }
1523    }
1524    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1525 }
1526
1527 /*********************************************************************
1528  *
1529  * Function    :  server_last_modified
1530  *
1531  * Description :  Changes Last-Modified header to the actual date
1532  *                to help hide-if-modified-since.
1533  *                Called from `sed'.
1534  *
1535  * Parameters  :
1536  *          1  :  csp = Current client state (buffers, headers, etc...)
1537  *          2  :  header = On input, pointer to header to modify.
1538  *                On output, pointer to the modified header, or NULL
1539  *                to remove the header.  This function frees the
1540  *                original string if necessary.
1541  *
1542  * Returns     :  JB_ERR_OK on success, or
1543  *                JB_ERR_MEMORY on out-of-memory error.
1544  *
1545  *********************************************************************/
1546 jb_err server_last_modified(struct client_state *csp, char **header)
1547 {
1548    const char *newval;
1549    char buf[BUFFER_SIZE];
1550
1551    char newheader[50];
1552 #ifdef HAVE_GMTIME_R
1553    struct tm gmt;
1554 #endif
1555    struct tm *timeptr = NULL;
1556    time_t now, last_modified;                  
1557    long int rtime;
1558    long int days, hours, minutes, seconds;
1559    
1560    /*
1561     * Are we messing with the Last-Modified header?
1562     */
1563    if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
1564    {
1565       /*Nope*/
1566       return JB_ERR_OK;
1567    }
1568
1569    newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
1570
1571    if (0 == strcmpic(newval, "block") )
1572    {
1573       /*
1574        * Blocking Last-Modified header. Useless but why not.
1575        */
1576       log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1577       freez(*header);
1578       return JB_ERR_OK;
1579    }
1580    else if (0 == strcmpic(newval, "reset-to-request-time"))
1581    {  
1582       /*
1583        * Setting Last-Modified Header to now.
1584        */
1585       get_http_time(0, buf);
1586       freez(*header);
1587       *header = strdup("Last-Modified: ");
1588       string_append(header, buf);   
1589
1590       if (*header == NULL)
1591       {
1592          log_error(LOG_LEVEL_HEADER, "Insufficent memory. Last-Modified header got lost, boohoo.");  
1593       }
1594       else
1595       {
1596          log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
1597       }
1598    }
1599    else if (0 == strcmpic(newval, "randomize"))
1600    {
1601       log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
1602       now = time(NULL);
1603 #ifdef HAVE_GMTIME_R
1604       timeptr = gmtime_r(&now, &gmt);
1605 #elif FEATURE_PTHREAD
1606       pthread_mutex_lock(&gmtime_mutex);
1607       timeptr = gmtime(&now);
1608       pthread_mutex_unlock(&gmtime_mutex);
1609 #else
1610       timeptr = gmtime(&now);
1611 #endif
1612       if ((timeptr = parse_header_time(*header, &last_modified)) == NULL)
1613       {
1614          log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
1615          freez(*header);
1616       }
1617       else
1618       {
1619          rtime = difftime(now, last_modified);
1620          if (rtime)
1621          {
1622             rtime = pick_from_range(rtime);
1623             last_modified += rtime;
1624 #ifdef HAVE_GMTIME_R
1625             timeptr = gmtime_r(&last_modified, &gmt);
1626 #elif FEATURE_PTHREAD
1627             pthread_mutex_lock(&gmtime_mutex);
1628             timeptr = gmtime(&last_modified);
1629             pthread_mutex_unlock(&gmtime_mutex);
1630 #else
1631             timeptr = gmtime(&last_modified);
1632 #endif
1633             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
1634             freez(*header);
1635             *header = strdup("Last-Modified: ");
1636             string_append(header, newheader);
1637
1638             if (*header == NULL)
1639             {
1640                log_error(LOG_LEVEL_ERROR, "Insufficent memory, header crunched without replacement.");
1641                return JB_ERR_MEMORY;  
1642             }
1643
1644             if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
1645             {
1646                days    = rtime / (3600 * 24);
1647                hours   = rtime / 3600 % 24;
1648                minutes = rtime / 60 % 60;
1649                seconds = rtime % 60;            
1650
1651                log_error(LOG_LEVEL_HEADER, "Randomized:  %s (added %d da%s %d hou%s %d minut%s %d second%s",
1652                   *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
1653                   minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
1654             }
1655          }
1656          else
1657          {
1658             log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
1659          }
1660       }
1661    }
1662
1663    return JB_ERR_OK;
1664 }
1665
1666 /*********************************************************************
1667  *
1668  * Function    :  client_accept_encoding
1669  *
1670  * Description :  Rewrite the client's Accept-Encoding header so that
1671  *                if doesn't allow compression, if the action applies.
1672  *                Note: For HTTP/1.0 the absence of the header is enough.
1673  *
1674  * Parameters  :
1675  *          1  :  csp = Current client state (buffers, headers, etc...)
1676  *          2  :  header = On input, pointer to header to modify.
1677  *                On output, pointer to the modified header, or NULL
1678  *                to remove the header.  This function frees the
1679  *                original string if necessary.
1680  *
1681  * Returns     :  JB_ERR_OK on success, or
1682  *                JB_ERR_MEMORY on out-of-memory error.
1683  *
1684  *********************************************************************/
1685 jb_err client_accept_encoding(struct client_state *csp, char **header)
1686 {
1687    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1688    {
1689       log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1690
1691       freez(*header);
1692
1693       /* Temporarily disable the correct behaviour to
1694        * work around a PHP bug. 
1695        *
1696        * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1697        * {
1698        *    *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1699        *    if (*header == NULL)
1700        *    {
1701        *       return JB_ERR_MEMORY;
1702        *    }
1703        * }
1704        * 
1705        */
1706    }
1707
1708    return JB_ERR_OK;
1709 }
1710
1711
1712 /*********************************************************************
1713  *
1714  * Function    :  client_te
1715  *
1716  * Description :  Rewrite the client's TE header so that
1717  *                if doesn't allow compression, if the action applies.
1718  *
1719  * Parameters  :
1720  *          1  :  csp = Current client state (buffers, headers, etc...)
1721  *          2  :  header = On input, pointer to header to modify.
1722  *                On output, pointer to the modified header, or NULL
1723  *                to remove the header.  This function frees the
1724  *                original string if necessary.
1725  *
1726  * Returns     :  JB_ERR_OK on success, or
1727  *                JB_ERR_MEMORY on out-of-memory error.
1728  *
1729  *********************************************************************/
1730 jb_err client_te(struct client_state *csp, char **header)
1731 {
1732    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1733    {
1734       freez(*header);
1735       log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1736    }
1737
1738    return JB_ERR_OK;
1739 }
1740
1741 /*********************************************************************
1742  *
1743  * Function    :  client_referrer
1744  *
1745  * Description :  Handle the "referer" config setting properly.
1746  *                Called from `sed'.
1747  *
1748  * Parameters  :
1749  *          1  :  csp = Current client state (buffers, headers, etc...)
1750  *          2  :  header = On input, pointer to header to modify.
1751  *                On output, pointer to the modified header, or NULL
1752  *                to remove the header.  This function frees the
1753  *                original string if necessary.
1754  *
1755  * Returns     :  JB_ERR_OK on success, or
1756  *                JB_ERR_MEMORY on out-of-memory error.
1757  *
1758  *********************************************************************/
1759 jb_err client_referrer(struct client_state *csp, char **header)
1760 {
1761    const char *newval;
1762    const char *host;
1763    char *referer;
1764    size_t hostlenght;
1765  
1766 #ifdef FEATURE_FORCE_LOAD
1767    /* Since the referrer can include the prefix even
1768     * if the request itself is non-forced, we must
1769     * clean it unconditionally
1770     */
1771    strclean(*header, FORCE_PREFIX);
1772 #endif /* def FEATURE_FORCE_LOAD */
1773
1774    /*
1775     * Are we sending referer?
1776     */
1777    if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1778    {
1779       return JB_ERR_OK;
1780    }
1781
1782    newval = csp->action->string[ACTION_STRING_REFERER];
1783
1784    if ((0 != strcmpic(newval, "conditional-block")))
1785    {  
1786       freez(*header);
1787    }
1788    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1789    {
1790       /*
1791        * Blocking referer
1792        */
1793       log_error(LOG_LEVEL_HEADER, "Referer crunched!");
1794       return JB_ERR_OK;
1795    }
1796    else if (0 == strcmpic(newval, "conditional-block"))
1797    {
1798       /*
1799        * Block referer if host has changed.
1800        */
1801
1802       if (NULL == (host = strdup(csp->http->hostport)))
1803       {
1804          freez(*header);
1805          log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary host copy.");
1806          return JB_ERR_MEMORY;
1807       }
1808       if (NULL == (referer = strdup(*header)))
1809       {
1810          freez(*header);
1811          freez(host);
1812          log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary referer copy.");
1813          return JB_ERR_MEMORY;
1814       }
1815       hostlenght = strlen(host);
1816       if ( hostlenght < (strlen(referer)-17) ) /*referer begins with 'Referer: http[s]://'*/
1817       {
1818          /*Shorten referer to make sure the referer is blocked
1819           *if www.example.org/www.example.com-shall-see-the-referer/
1820           *links to www.example.com/
1821           */
1822          referer[hostlenght+17] = '\0';
1823       }
1824       if ( 0 == strstr(referer, host)) /*Host has changed*/
1825       {
1826          log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
1827          freez(*header);
1828       }
1829       else
1830       {
1831          log_error(LOG_LEVEL_HEADER, "%s (not modified, still on %s)", *header, host);
1832       }
1833       freez(referer);
1834       freez(host);
1835       return JB_ERR_OK;    
1836    }
1837    else if (0 != strcmpic(newval, "forge"))
1838    {
1839       /*
1840        * We have a specific (fixed) referer we want to send.
1841        */
1842       if ((0 != strncmpic(newval, "http://", 7)) && (0 != strncmpic(newval, "https://", 8)))
1843       {
1844          log_error(LOG_LEVEL_HEADER, "Parameter: +referrer{%s} is a bad idea, but I don't care.", newval);
1845       }
1846       *header = strdup("Referer: ");
1847       string_append(header, newval);
1848       log_error(LOG_LEVEL_HEADER, "Referer overwritten with: %s", *header);
1849
1850       return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1851    }
1852    else
1853    {
1854       /*
1855        * Forge a referer as http://[hostname:port of REQUEST]/
1856        * to fool stupid checks for in-site links
1857        */
1858
1859       *header = strdup("Referer: http://");
1860       string_append(header, csp->http->hostport);
1861       string_append(header, "/");
1862       log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
1863       
1864       return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1865    }
1866 }
1867
1868 /*********************************************************************
1869  *
1870  * Function    :  client_accept_language
1871  *
1872  * Description :  Handle the "Accept-Language" config setting properly.
1873  *                Called from `sed'.
1874  *
1875  * Parameters  :
1876  *          1  :  csp = Current client state (buffers, headers, etc...)
1877  *          2  :  header = On input, pointer to header to modify.
1878  *                On output, pointer to the modified header, or NULL
1879  *                to remove the header.  This function frees the
1880  *                original string if necessary.
1881  *
1882  * Returns     :  JB_ERR_OK on success, or
1883  *                JB_ERR_MEMORY on out-of-memory error.
1884  *
1885  *********************************************************************/
1886 jb_err client_accept_language(struct client_state *csp, char **header)
1887 {
1888    const char *newval;
1889
1890    /*
1891     * Are we messing with the Accept-Language?
1892     */
1893    if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
1894    {
1895       /*I don't think so*/
1896       return JB_ERR_OK;
1897    }
1898
1899    newval = csp->action->string[ACTION_STRING_LANGUAGE];
1900
1901    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1902    {
1903       /*
1904        * Blocking Accept-Language header
1905        */
1906       log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
1907       freez(*header);
1908       return JB_ERR_OK;
1909    }
1910    else
1911    {  
1912       /*
1913        * Replacing Accept-Language header
1914        */
1915       freez(*header);
1916       *header = strdup("Accept-Language: ");
1917       string_append(header, newval);   
1918
1919       if (*header == NULL)
1920       {
1921          log_error(LOG_LEVEL_ERROR, " Insufficent memory. Accept-Language header crunched without replacement.");  
1922       }
1923       else
1924       {
1925          log_error(LOG_LEVEL_HEADER, "Accept-Language header crunched and replaced with: %s", *header);
1926       }
1927    }
1928    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1929 }
1930
1931 /*********************************************************************
1932  *
1933  * Function    :  crunch_client_header
1934  *
1935  * Description :  Crunch client header if it matches a string supplied by the
1936  *                user. Called from `sed'.
1937  *
1938  * Parameters  :
1939  *          1  :  csp = Current client state (buffers, headers, etc...)
1940  *          2  :  header = On input, pointer to header to modify.
1941  *                On output, pointer to the modified header, or NULL
1942  *                to remove the header.  This function frees the
1943  *                original string if necessary.
1944  *
1945  * Returns     :  JB_ERR_OK on success and always succeeds
1946  *
1947  *********************************************************************/
1948 jb_err crunch_client_header(struct client_state *csp, char **header)
1949 {
1950    const char *crunch_pattern;
1951    /*Is there a header to crunch*/
1952    
1953    if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
1954    {
1955       crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
1956
1957       /*Is the current header the lucky one?*/
1958       if (strstr(*header, crunch_pattern))
1959       {
1960          log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);  
1961          freez(*header);
1962       }
1963    }
1964    return JB_ERR_OK;
1965 }
1966
1967
1968 /*********************************************************************
1969  *
1970  * Function    :  client_uagent
1971  *
1972  * Description :  Handle the "user-agent" config setting properly
1973  *                and remember its original value to enable browser
1974  *                bug workarounds. Called from `sed'.
1975  *
1976  * Parameters  :
1977  *          1  :  csp = Current client state (buffers, headers, etc...)
1978  *          2  :  header = On input, pointer to header to modify.
1979  *                On output, pointer to the modified header, or NULL
1980  *                to remove the header.  This function frees the
1981  *                original string if necessary.
1982  *
1983  * Returns     :  JB_ERR_OK on success, or
1984  *                JB_ERR_MEMORY on out-of-memory error.
1985  *
1986  *********************************************************************/
1987 jb_err client_uagent(struct client_state *csp, char **header)
1988 {
1989    const char *newval;
1990
1991    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1992    {
1993       return JB_ERR_OK;
1994    }
1995
1996    newval = csp->action->string[ACTION_STRING_USER_AGENT];
1997    if (newval == NULL)
1998    {
1999       return JB_ERR_OK;
2000    }
2001
2002    freez(*header);
2003    *header = strdup("User-Agent: ");
2004    string_append(header, newval);
2005
2006    log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
2007
2008    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2009 }
2010
2011 /*********************************************************************
2012  *
2013  * Function    :  client_ua
2014  *
2015  * Description :  Handle "ua-" headers properly.  Called from `sed'.
2016  *
2017  * Parameters  :
2018  *          1  :  csp = Current client state (buffers, headers, etc...)
2019  *          2  :  header = On input, pointer to header to modify.
2020  *                On output, pointer to the modified header, or NULL
2021  *                to remove the header.  This function frees the
2022  *                original string if necessary.
2023  *
2024  * Returns     :  JB_ERR_OK on success, or
2025  *                JB_ERR_MEMORY on out-of-memory error.
2026  *
2027  *********************************************************************/
2028 jb_err client_ua(struct client_state *csp, char **header)
2029 {
2030    if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
2031    {
2032       log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
2033       freez(*header);
2034    }
2035
2036    return JB_ERR_OK;
2037 }
2038
2039
2040 /*********************************************************************
2041  *
2042  * Function    :  client_from
2043  *
2044  * Description :  Handle the "from" config setting properly.
2045  *                Called from `sed'.
2046  *
2047  * Parameters  :
2048  *          1  :  csp = Current client state (buffers, headers, etc...)
2049  *          2  :  header = On input, pointer to header to modify.
2050  *                On output, pointer to the modified header, or NULL
2051  *                to remove the header.  This function frees the
2052  *                original string if necessary.
2053  *
2054  * Returns     :  JB_ERR_OK on success, or
2055  *                JB_ERR_MEMORY on out-of-memory error.
2056  *
2057  *********************************************************************/
2058 jb_err client_from(struct client_state *csp, char **header)
2059 {
2060    const char *newval;
2061
2062    if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
2063    {
2064       return JB_ERR_OK;
2065    }
2066
2067    freez(*header);
2068
2069    newval = csp->action->string[ACTION_STRING_FROM];
2070
2071    /*
2072     * Are we blocking the e-mail address?
2073     */
2074    if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2075    {
2076       log_error(LOG_LEVEL_HEADER, "crunched From!");
2077       return JB_ERR_OK;
2078    }
2079
2080    log_error(LOG_LEVEL_HEADER, " modified");
2081
2082    *header = strdup("From: ");
2083    string_append(header, newval);
2084
2085    return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2086 }
2087
2088
2089 /*********************************************************************
2090  *
2091  * Function    :  client_send_cookie
2092  *
2093  * Description :  Handle the "cookie" header properly.  Called from `sed'.
2094  *                If cookie is accepted, add it to the cookie_list,
2095  *                else we crunch it.  Mmmmmmmmmmm ... cookie ......
2096  *
2097  * Parameters  :
2098  *          1  :  csp = Current client state (buffers, headers, etc...)
2099  *          2  :  header = On input, pointer to header to modify.
2100  *                On output, pointer to the modified header, or NULL
2101  *                to remove the header.  This function frees the
2102  *                original string if necessary.
2103  *
2104  * Returns     :  JB_ERR_OK on success, or
2105  *                JB_ERR_MEMORY on out-of-memory error.
2106  *
2107  *********************************************************************/
2108 jb_err client_send_cookie(struct client_state *csp, char **header)
2109 {
2110    jb_err result = JB_ERR_OK;
2111
2112    if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
2113    {
2114       /* strlen("cookie: ") == 8 */
2115       result = enlist(csp->cookie_list, *header + 8);
2116    }
2117    else
2118    {
2119       log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
2120    }
2121
2122    /*
2123     * Always remove the cookie here.  The cookie header
2124     * will be sent at the end of the header.
2125     */
2126    freez(*header);
2127
2128    return result;
2129 }
2130
2131
2132 /*********************************************************************
2133  *
2134  * Function    :  client_x_forwarded
2135  *
2136  * Description :  Handle the "x-forwarded-for" config setting properly,
2137  *                also used in the add_client_headers list.  Called from `sed'.
2138  *
2139  * Parameters  :
2140  *          1  :  csp = Current client state (buffers, headers, etc...)
2141  *          2  :  header = On input, pointer to header to modify.
2142  *                On output, pointer to the modified header, or NULL
2143  *                to remove the header.  This function frees the
2144  *                original string if necessary.
2145  *
2146  * Returns     :  JB_ERR_OK on success, or
2147  *                JB_ERR_MEMORY on out-of-memory error.
2148  *
2149  *********************************************************************/
2150 jb_err client_x_forwarded(struct client_state *csp, char **header)
2151 {
2152    if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
2153    {
2154       /* Save it so we can re-add it later */
2155       freez(csp->x_forwarded);
2156       csp->x_forwarded = *header;
2157
2158       /*
2159        * Always set *header = NULL, since this information
2160        * will be sent at the end of the header.
2161        */
2162       *header = NULL;
2163    }
2164    else
2165    {
2166       freez(*header);
2167       log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
2168    }
2169
2170    return JB_ERR_OK;
2171 }
2172
2173
2174 /*********************************************************************
2175  *
2176  * Function    :  client_max_forwards
2177  *
2178  * Description :  If the HTTP method is OPTIONS or TRACE, subtract one
2179  *                from the value of the Max-Forwards header field.
2180  *
2181  * Parameters  :
2182  *          1  :  csp = Current client state (buffers, headers, etc...)
2183  *          2  :  header = On input, pointer to header to modify.
2184  *                On output, pointer to the modified header, or NULL
2185  *                to remove the header.  This function frees the
2186  *                original string if necessary.
2187  *
2188  * Returns     :  JB_ERR_OK on success, or
2189  *                JB_ERR_MEMORY on out-of-memory error.
2190  *
2191  *********************************************************************/
2192 jb_err client_max_forwards(struct client_state *csp, char **header)
2193 {
2194    unsigned int max_forwards;
2195
2196    if ((0 == strcmpic(csp->http->gpc, "trace"))
2197       || (0 == strcmpic(csp->http->gpc, "options")))
2198    {
2199       if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
2200       {
2201          if (max_forwards-- >= 1)
2202          {
2203             sprintf(*header, "Max-Forwards: %u", max_forwards);
2204             log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
2205          }
2206          else
2207          {
2208             log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
2209          }
2210       }
2211    }
2212
2213    return JB_ERR_OK;
2214 }
2215
2216
2217 /*********************************************************************
2218  *
2219  * Function    :  client_host
2220  *
2221  * Description :  If the request URI did not contain host and
2222  *                port information, parse and evaluate the Host
2223  *                header field.
2224  *
2225  *                Also, kill ill-formed HOST: headers as sent by
2226  *                Apple's iTunes software when used with a proxy.
2227  *
2228  * Parameters  :
2229  *          1  :  csp = Current client state (buffers, headers, etc...)
2230  *          2  :  header = On input, pointer to header to modify.
2231  *                On output, pointer to the modified header, or NULL
2232  *                to remove the header.  This function frees the
2233  *                original string if necessary.
2234  *
2235  * Returns     :  JB_ERR_OK on success, or
2236  *                JB_ERR_MEMORY on out-of-memory error.
2237  *
2238  *********************************************************************/
2239 jb_err client_host(struct client_state *csp, char **header)
2240 {
2241    char *p, *q;
2242
2243    /*
2244     * If the header field name is all upper-case, chances are that it's
2245     * an ill-formed one from iTunes. BTW, killing innocent headers here is
2246     * not a problem -- they are regenerated later.
2247     */
2248    if ((*header)[1] == 'O')
2249    {
2250       log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
2251       freez(*header);
2252       return JB_ERR_OK;
2253    }
2254
2255    if (!csp->http->hostport || (*csp->http->hostport == '*') ||  
2256        *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
2257    {
2258       
2259       if (NULL == (p = strdup((*header)+6)))
2260       {
2261          return JB_ERR_MEMORY;
2262       }
2263       chomp(p);
2264       if (NULL == (q = strdup(p)))
2265       {
2266          freez(p);
2267          return JB_ERR_MEMORY;
2268       }
2269
2270       freez(csp->http->hostport);
2271       csp->http->hostport = p;
2272       freez(csp->http->host);
2273       csp->http->host = q;
2274       q = strchr(csp->http->host, ':');
2275       if (q != NULL)
2276       {
2277          /* Terminate hostname and evaluate port string */
2278          *q++ = '\0';
2279          csp->http->port = atoi(q);
2280       }
2281       else
2282       {
2283          csp->http->port = csp->http->ssl ? 443 : 80;
2284       }
2285
2286       log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
2287                 csp->http->hostport, csp->http->host, csp->http->port);
2288    }
2289
2290    return JB_ERR_OK;
2291 }
2292
2293 /*********************************************************************
2294  *
2295  * Function    :  client_if_modified_since
2296  *
2297  * Description :  Remove or modify the If-Modified-Since header.
2298  *
2299  * Parameters  :
2300  *          1  :  csp = Current client state (buffers, headers, etc...)
2301  *          2  :  header = On input, pointer to header to modify.
2302  *                On output, pointer to the modified header, or NULL
2303  *                to remove the header.  This function frees the
2304  *                original string if necessary.
2305  *
2306  * Returns     :  JB_ERR_OK on success, or
2307  *                JB_ERR_MEMORY on out-of-memory error.
2308  *
2309  *********************************************************************/
2310 jb_err client_if_modified_since(struct client_state *csp, char **header)
2311 {
2312    char newheader[50];
2313 #ifdef HAVE_GMTIME_R
2314    struct tm gmt;
2315 #endif
2316    struct tm *timeptr = NULL;
2317    time_t tm = 0;                  
2318    const char *newval;
2319    long int rtime;
2320    long int hours, minutes, seconds;
2321    int negative = 0;
2322    char * endptr;
2323    
2324    if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
2325    {
2326       /* 
2327        * The client got an error message because of a temporary problem,
2328        * the problem is gone and the client now tries to revalidate our
2329        * error message on the real server. The revalidation would always
2330        * end with the transmission of the whole document and there is
2331        * no need to expose the bogus If-Modified-Since header.
2332        */
2333       log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
2334       freez(*header);
2335    }
2336    else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
2337    {
2338       newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
2339
2340       if ((0 == strcmpic(newval, "block")))
2341       {
2342          log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2343          freez(*header);
2344       }
2345       else /* add random value */
2346       {
2347          if ((timeptr = parse_header_time(*header, &tm)) == NULL)
2348          {
2349             log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
2350             freez(*header);
2351          }
2352          else
2353          {
2354             rtime = strtol(newval, &endptr, 0);
2355             if(rtime)
2356             {
2357                log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
2358                   *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
2359                if(rtime < 0)
2360                {
2361                   rtime *= -1; 
2362                   negative = 1;
2363                }
2364                rtime *= 60;
2365                rtime = pick_from_range(rtime);
2366             }
2367             else
2368             {
2369                log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
2370                   *header);
2371             }
2372             tm += rtime * (negative ? -1 : 1);
2373 #ifdef HAVE_GMTIME_R
2374             timeptr = gmtime_r(&tm, &gmt);
2375 #elif FEATURE_PTHREAD
2376             pthread_mutex_lock(&gmtime_mutex);
2377             timeptr = gmtime(&tm);
2378             pthread_mutex_unlock(&gmtime_mutex);
2379 #else
2380             timeptr = gmtime(&tm);
2381 #endif
2382             strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2383
2384             freez(*header);
2385             *header = strdup("If-Modified-Since: ");
2386             string_append(header, newheader);
2387
2388             if (*header == NULL)
2389             {
2390                log_error(LOG_LEVEL_HEADER, "Insufficent memory, header crunched without replacement.");
2391                return JB_ERR_MEMORY;  
2392             }
2393
2394             if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2395             {
2396                hours   = rtime / 3600;
2397                minutes = rtime / 60 % 60;
2398                seconds = rtime % 60;            
2399
2400                log_error(LOG_LEVEL_HEADER, "Randomized:  %s (%s %d hou%s %d minut%s %d second%s",
2401                   *header, (negative) ? "subtracted" : "added", hours, (hours == 1) ? "r" : "rs",
2402                   minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2403             }
2404          }
2405       }
2406    }
2407
2408    return JB_ERR_OK;
2409 }
2410
2411 /*********************************************************************
2412  *
2413  * Function    :  client_if_none_match
2414  *
2415  * Description :  Remove the If-None-Match header.
2416  *
2417  * Parameters  :
2418  *          1  :  csp = Current client state (buffers, headers, etc...)
2419  *          2  :  header = On input, pointer to header to modify.
2420  *                On output, pointer to the modified header, or NULL
2421  *                to remove the header.  This function frees the
2422  *                original string if necessary.
2423  *
2424  * Returns     :  JB_ERR_OK on success, or
2425  *                JB_ERR_MEMORY on out-of-memory error.
2426  *
2427  *********************************************************************/
2428 jb_err client_if_none_match(struct client_state *csp, char **header)
2429 {
2430    if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
2431    {  
2432       log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2433       freez(*header);
2434    }
2435
2436    return JB_ERR_OK;
2437 }
2438
2439 /*********************************************************************
2440  *
2441  * Function    :  client_x_filter
2442  *
2443  * Description :  Disables filtering if the client set "X-Filter: No".
2444  *                Called from `sed'.
2445  *
2446  * Parameters  :
2447  *          1  :  csp = Current client state (buffers, headers, etc...)
2448  *          2  :  header = On input, pointer to header to modify.
2449  *                On output, pointer to the modified header, or NULL
2450  *                to remove the header.  This function frees the
2451  *                original string if necessary.
2452  *
2453  * Returns     :  JB_ERR_OK on success
2454  *
2455  *********************************************************************/
2456 jb_err client_x_filter(struct client_state *csp, char **header)
2457 {
2458    if ( 0 == strcmpic(*header, "X-Filter: No"))
2459    {
2460       if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
2461       {
2462          log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
2463       }
2464       else
2465       {
2466          if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
2467          {
2468             log_error(LOG_LEVEL_HEADER, "force-text-mode overruled the client's request to fetch without filtering!");
2469          }
2470          else
2471          {  
2472             csp->content_type = CT_TABOO;
2473             csp->action->flags &= ~ACTION_FILTER_SERVER_HEADERS;
2474             csp->action->flags &= ~ACTION_FILTER_CLIENT_HEADERS;
2475             log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
2476          }
2477          log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2478          freez(*header);
2479       }
2480    }
2481    return JB_ERR_OK; 
2482 }
2483
2484 /* the following functions add headers directly to the header list */
2485
2486 /*********************************************************************
2487  *
2488  * Function    :  client_host_adder
2489  *
2490  * Description :  Adds the Host: header field if it is missing.
2491  *                Called from `sed'.
2492  *
2493  * Parameters  :
2494  *          1  :  csp = Current client state (buffers, headers, etc...)
2495  *
2496  * Returns     :  JB_ERR_OK on success, or
2497  *                JB_ERR_MEMORY on out-of-memory error.
2498  *
2499  *********************************************************************/
2500 jb_err client_host_adder(struct client_state *csp)
2501 {
2502    char *p;
2503    jb_err err;
2504
2505    if ( !csp->http->hostport || !*(csp->http->hostport))
2506    {
2507       return JB_ERR_OK;
2508    }
2509
2510    /*
2511     * remove 'user:pass@' from 'proto://user:pass@host'
2512     */
2513    if ( (p = strchr( csp->http->hostport, '@')) != NULL )
2514    {
2515       p++;
2516    }
2517    else
2518    {
2519       p = csp->http->hostport;
2520    }
2521
2522    log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
2523    err = enlist_unique_header(csp->headers, "Host", p);
2524    return err;
2525
2526 }
2527
2528
2529 /*********************************************************************
2530  *
2531  * Function    :  client_cookie_adder
2532  *
2533  * Description :  Used in the add_client_headers list.  Called from `sed'.
2534  *
2535  * Parameters  :
2536  *          1  :  csp = Current client state (buffers, headers, etc...)
2537  *
2538  * Returns     :  JB_ERR_OK on success, or
2539  *                JB_ERR_MEMORY on out-of-memory error.
2540  *
2541  *********************************************************************/
2542 jb_err client_cookie_adder(struct client_state *csp)
2543 {
2544    struct list_entry *lst;
2545    char *tmp;
2546    struct list_entry *list1 = csp->cookie_list->first;
2547    struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
2548    int first_cookie = 1;
2549    jb_err err;
2550
2551    if ((list1 == NULL) && (list2 == NULL))
2552    {
2553       /* Nothing to do */
2554       return JB_ERR_OK;
2555    }
2556
2557    tmp = strdup("Cookie: ");
2558
2559    for (lst = list1; lst ; lst = lst->next)
2560    {
2561       if (first_cookie)
2562       {
2563          first_cookie = 0;
2564       }
2565       else
2566       {
2567          string_append(&tmp, "; ");
2568       }
2569       string_append(&tmp, lst->str);
2570    }
2571
2572    for (lst = list2;  lst ; lst = lst->next)
2573    {
2574       if (first_cookie)
2575       {
2576          first_cookie = 0;
2577       }
2578       else
2579       {
2580          string_append(&tmp, "; ");
2581       }
2582       string_join(&tmp, cookie_encode(lst->str));
2583    }
2584
2585    if (tmp == NULL)
2586    {
2587       return JB_ERR_MEMORY;
2588    }
2589
2590    log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
2591    err = enlist(csp->headers, tmp);
2592    free(tmp);
2593    return err;
2594 }
2595
2596
2597 /*********************************************************************
2598  *
2599  * Function    :  client_accept_encoding_adder
2600  *
2601  * Description :  Add an Accept-Encoding header to the client's request
2602  *                that disables compression if the action applies, and
2603  *                the header is not already there. Called from `sed'.
2604  *                Note: For HTTP/1.0, the absence of the header is enough.
2605  *
2606  * Parameters  :
2607  *          1  :  csp = Current client state (buffers, headers, etc...)
2608  *
2609  * Returns     :  JB_ERR_OK on success, or
2610  *                JB_ERR_MEMORY on out-of-memory error.
2611  *
2612  *********************************************************************/
2613 jb_err client_accept_encoding_adder(struct client_state *csp)
2614 {
2615    if (   ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2616        && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
2617    {
2618       return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
2619    }
2620
2621    return JB_ERR_OK;
2622 }
2623
2624
2625 /*********************************************************************
2626  *
2627  * Function    :  client_xtra_adder
2628  *
2629  * Description :  Used in the add_client_headers list.  Called from `sed'.
2630  *
2631  * Parameters  :
2632  *          1  :  csp = Current client state (buffers, headers, etc...)
2633  *
2634  * Returns     :  JB_ERR_OK on success, or
2635  *                JB_ERR_MEMORY on out-of-memory error.
2636  *
2637  *********************************************************************/
2638 jb_err client_xtra_adder(struct client_state *csp)
2639 {
2640    struct list_entry *lst;
2641    jb_err err;
2642
2643    for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
2644         lst ; lst = lst->next)
2645    {
2646       log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
2647       err = enlist(csp->headers, lst->str);
2648       if (err)
2649       {
2650          return err;
2651       }
2652
2653    }
2654
2655    return JB_ERR_OK;
2656 }
2657
2658
2659 /*********************************************************************
2660  *
2661  * Function    :  client_x_forwarded_adder
2662  *
2663  * Description :  Used in the add_client_headers list.  Called from `sed'.
2664  *
2665  * Parameters  :
2666  *          1  :  csp = Current client state (buffers, headers, etc...)
2667  *
2668  * Returns     :  JB_ERR_OK on success, or
2669  *                JB_ERR_MEMORY on out-of-memory error.
2670  *
2671  *********************************************************************/
2672 jb_err client_x_forwarded_adder(struct client_state *csp)
2673 {
2674    char *p = NULL;
2675    jb_err err;
2676
2677    if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
2678    {
2679       return JB_ERR_OK;
2680    }
2681
2682    if (csp->x_forwarded)
2683    {
2684       p = strdup(csp->x_forwarded);
2685       string_append(&p, ", ");
2686    }
2687    else
2688    {
2689       p = strdup("X-Forwarded-For: ");
2690    }
2691    string_append(&p, csp->ip_addr_str);
2692
2693    if (p == NULL)
2694    {
2695       return JB_ERR_MEMORY;
2696    }
2697
2698    log_error(LOG_LEVEL_HEADER, "addh: %s", p);
2699    err = enlist(csp->headers, p);
2700    free(p);
2701
2702    return err;
2703 }
2704
2705
2706 /*********************************************************************
2707  *
2708  * Function    :  connection_close_adder
2709  *
2710  * Description :  Adds a "Connection: close" header to csp->headers
2711  *                as a temporary fix for the needed but missing HTTP/1.1
2712  *                support. Called from `sed'.
2713  *                FIXME: This whole function shouldn't be neccessary!
2714  *
2715  * Parameters  :
2716  *          1  :  csp = Current client state (buffers, headers, etc...)
2717  *
2718  * Returns     :  JB_ERR_OK on success, or
2719  *                JB_ERR_MEMORY on out-of-memory error.
2720  *
2721  *********************************************************************/
2722 jb_err connection_close_adder(struct client_state *csp)
2723 {
2724    log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
2725    return enlist(csp->headers, "Connection: close");
2726 }
2727
2728
2729 /*********************************************************************
2730  *
2731  * Function    :  server_http
2732  *
2733  * Description :  - Save the HTTP Status into csp->http->status
2734  *                - Set CT_TABOO to prevent filtering if the answer
2735  *                  is a partial range (HTTP status 206)
2736  *                - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
2737  *                  action applies.
2738  *
2739  * Parameters  :
2740  *          1  :  csp = Current client state (buffers, headers, etc...)
2741  *          2  :  header = On input, pointer to header to modify.
2742  *                On output, pointer to the modified header, or NULL
2743  *                to remove the header.  This function frees the
2744  *                original string if necessary.
2745  *
2746  * Returns     :  JB_ERR_OK on success, or
2747  *                JB_ERR_MEMORY on out-of-memory error.
2748  *
2749  *********************************************************************/
2750 jb_err server_http(struct client_state *csp, char **header)
2751 {
2752    sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
2753    if (csp->http->status == 206)
2754    {
2755       csp->content_type = CT_TABOO;
2756    }
2757
2758    if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
2759    {
2760       (*header)[7] = '0';
2761       log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
2762    }
2763
2764    return JB_ERR_OK;
2765 }
2766
2767
2768 /*********************************************************************
2769  *
2770  * Function    :  server_set_cookie
2771  *
2772  * Description :  Handle the server "cookie" header properly.
2773  *                Log cookie to the jar file.  Then "crunch" it,
2774  *                or accept it.  Called from `sed'.
2775  *
2776  * Parameters  :
2777  *          1  :  csp = Current client state (buffers, headers, etc...)
2778  *          2  :  header = On input, pointer to header to modify.
2779  *                On output, pointer to the modified header, or NULL
2780  *                to remove the header.  This function frees the
2781  *                original string if necessary.
2782  *
2783  * Returns     :  JB_ERR_OK on success, or
2784  *                JB_ERR_MEMORY on out-of-memory error.
2785  *
2786  *********************************************************************/
2787 jb_err server_set_cookie(struct client_state *csp, char **header)
2788 {
2789 #ifdef FEATURE_COOKIE_JAR
2790    if (csp->config->jar)
2791    {
2792       /*
2793        * Write timestamp into outbuf.
2794        *
2795        * Complex because not all OSs have tm_gmtoff or
2796        * the %z field in strftime()
2797        */
2798       char tempbuf[ BUFFER_SIZE ];
2799       time_t now; 
2800       struct tm tm_now; 
2801       time (&now); 
2802 #ifdef HAVE_LOCALTIME_R
2803       tm_now = *localtime_r(&now, &tm_now);
2804 #elif FEATURE_PTHREAD
2805       pthread_mutex_lock(&localtime_mutex);
2806       tm_now = *localtime (&now); 
2807       pthread_mutex_unlock(&localtime_mutex);
2808 #else
2809       tm_now = *localtime (&now); 
2810 #endif
2811       strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now); 
2812
2813       /* strlen("set-cookie: ") = 12 */
2814       fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
2815    }
2816 #endif /* def FEATURE_COOKIE_JAR */
2817
2818    if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
2819    {
2820       log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
2821       return crumble(csp, header);
2822    }
2823    else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
2824    {
2825       /* Flag whether or not to log a message */
2826       int changed = 0;
2827
2828       /* A variable to store the tag we're working on */
2829       char *cur_tag;
2830
2831       /* Skip "Set-Cookie:" (11 characters) in header */
2832       cur_tag = *header + 11;
2833
2834       /* skip whitespace between "Set-Cookie:" and value */
2835       while (*cur_tag && ijb_isspace(*cur_tag))
2836       {
2837          cur_tag++;
2838       }
2839
2840       /* Loop through each tag in the cookie */
2841       while (*cur_tag)
2842       {
2843          /* Find next tag */
2844          char *next_tag = strchr(cur_tag, ';');
2845          if (next_tag != NULL)
2846          {
2847             /* Skip the ';' character itself */
2848             next_tag++;
2849
2850             /* skip whitespace ";" and start of tag */
2851             while (*next_tag && ijb_isspace(*next_tag))
2852             {
2853                next_tag++;
2854             }
2855          }
2856          else
2857          {
2858             /* "Next tag" is the end of the string */
2859             next_tag = cur_tag + strlen(cur_tag);
2860          }
2861
2862          /* Is this the "Expires" tag? */
2863          if (strncmpic(cur_tag, "expires=", 8) == 0)
2864          {
2865             /* Delete the tag by copying the rest of the string over it.
2866              * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
2867              * since the behaviour of strcpy is undefined for overlapping
2868              * strings.)
2869              */
2870             memmove(cur_tag, next_tag, strlen(next_tag) + 1);
2871
2872             /* That changed the header, need to issue a log message */
2873             changed = 1;
2874
2875             /* Note that the next tag has now been moved to *cur_tag,
2876              * so we do not need to update the cur_tag pointer.
2877              */
2878          }
2879          else
2880          {
2881             /* Move on to next cookie tag */
2882             cur_tag = next_tag;
2883          }
2884       }
2885
2886       if (changed)
2887       {
2888          log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
2889       }
2890    }
2891
2892    return JB_ERR_OK;
2893 }
2894
2895
2896 #ifdef FEATURE_FORCE_LOAD
2897 /*********************************************************************
2898  *
2899  * Function    :  strclean
2900  *
2901  * Description :  In-Situ-Eliminate all occurances of substring in
2902  *                string
2903  *
2904  * Parameters  :
2905  *          1  :  string = string to clean
2906  *          2  :  substring = substring to eliminate
2907  *
2908  * Returns     :  Number of eliminations
2909  *
2910  *********************************************************************/
2911 int strclean(const char *string, const char *substring)
2912 {
2913    int hits = 0, len = strlen(substring);
2914    char *pos, *p;
2915
2916    while((pos = strstr(string, substring)) != NULL)
2917    {
2918       p = pos + len;
2919       do
2920       {
2921          *(p - len) = *p;
2922       }
2923       while (*p++ != '\0');
2924
2925       hits++;
2926    }
2927
2928    return(hits);
2929 }
2930 #endif /* def FEATURE_FORCE_LOAD */
2931
2932 /*********************************************************************
2933  *
2934  * Function    :  parse_header_time
2935  *
2936  * Description :  Transforms time inside a HTTP header into
2937  *                the usual time format.
2938  *
2939  * Parameters  :
2940  *          1  :  header = header to parse
2941  *          2  :  tm = storage for the resulting time in seconds 
2942  *
2943  * Returns     :  Time struct containing the header time, or
2944  *                NULL in case of a parsing problems.
2945  *
2946  *********************************************************************/
2947 struct tm *parse_header_time(char *header, time_t *tm) {
2948
2949    char * timestring;
2950    struct tm gmt;
2951    struct tm * timeptr;
2952
2953    /*
2954     * Initializing gmt to prevent time zone offsets.
2955     *
2956     * While this is only necessary on some platforms
2957     * (mingw32 for example), I don't know how to
2958     * detect these automatically and doing it everywhere
2959     * shouldn't hurt.
2960     */
2961    time(tm); 
2962 #ifdef HAVE_LOCALTIME_R
2963    gmt = *localtime_r(tm, &gmt);
2964 #elif FEATURE_PTHREAD
2965    pthread_mutex_lock(&localtime_mutex);
2966    gmt = *localtime(tm); 
2967    pthread_mutex_unlock(&localtime_mutex);
2968 #else
2969    gmt = *localtime(tm); 
2970 #endif
2971
2972    /* Skipping header name */
2973    timestring = strstr(header, ": ");
2974    if (strptime(timestring, ": %a, %d %b %Y %H:%M:%S", &gmt) == NULL)
2975    {
2976       timeptr = NULL;
2977    }
2978    else
2979    {
2980       *tm = timegm(&gmt);
2981       timeptr = &gmt;
2982    }
2983    return(timeptr);
2984
2985 }
2986
2987 /*********************************************************************
2988  *
2989  * Function    :  get_destination_from_headers
2990  *
2991  * Description :  Parse the "Host:" header to get the request's destination.
2992  *                Only needed if the client's request was forcefully
2993  *                redirected into Privoxy.
2994  *
2995  *                Code mainly copied from client_host() which is currently
2996  *                run too late for this purpose.
2997  *
2998  * Parameters  :
2999  *          1  :  headers = List of headers (one of them hopefully being
3000  *                the "Host:" header)
3001  *          2  :  http = storage for the result (host, port and hostport). 
3002  *
3003  * Returns     :  JB_ERR_MEMORY in case of memory problems,
3004  *                JB_ERR_PARSE if the host header couldn't be found,
3005  *                JB_ERR_OK otherwise.
3006  *
3007  *********************************************************************/
3008 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
3009 {
3010    char *q;
3011    char *p;
3012    char *host;
3013
3014    host = get_header_value(headers, "Host:");
3015
3016    if (NULL == host)
3017    {
3018       log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
3019       return JB_ERR_PARSE;
3020    }
3021
3022    if (NULL == (p = strdup((host))))
3023    {
3024       log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
3025       return JB_ERR_MEMORY;
3026    }
3027    chomp(p);
3028    if (NULL == (q = strdup(p)))
3029    {
3030       freez(p);
3031       log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
3032       return JB_ERR_MEMORY;
3033    }
3034
3035    freez(http->hostport);
3036    http->hostport = p;
3037    freez(http->host);
3038    http->host = q;
3039    q = strchr(http->host, ':');
3040    if (q != NULL)
3041    {
3042       /* Terminate hostname and evaluate port string */
3043       *q++ = '\0';
3044       http->port = atoi(q);
3045    }
3046    else
3047    {
3048       http->port = http->ssl ? 443 : 80;
3049    }
3050
3051    /* Rebuild request URL */
3052    freez(http->url);
3053    http->url = strdup(http->ssl ? "https://" : "http://");
3054    string_append(&http->url, http->hostport);
3055    string_append(&http->url, http->path);
3056    if (http->url == NULL)
3057    {
3058       return JB_ERR_MEMORY;
3059    }
3060
3061    log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header. New request URL: %s",
3062       http->url);
3063
3064    return JB_ERR_OK;
3065
3066 }
3067
3068
3069 /*
3070   Local Variables:
3071   tab-width: 3
3072   end:
3073 */