1 const char parsers_rcs[] = "$Id: parsers.c,v 1.115 2007/11/02 16:52:50 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
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_header'
14 * `server_content_encoding', `server_content_disposition',
15 * `server_last_modified', `client_accept_language',
16 * `crunch_client_header', `client_if_modified_since',
17 * `client_if_none_match', `get_destination_from_headers',
18 * `parse_header_time', `decompress_iob' and `server_set_cookie'.
20 * Copyright : Written by and Copyright (C) 2001-2007 the SourceForge
21 * Privoxy team. http://www.privoxy.org/
23 * Based on the Internet Junkbuster originally written
24 * by and Copyright (C) 1997 Anonymous Coders and
25 * Junkbusters Corporation. http://www.junkbusters.com
27 * This program is free software; you can redistribute it
28 * and/or modify it under the terms of the GNU General
29 * Public License as published by the Free Software
30 * Foundation; either version 2 of the License, or (at
31 * your option) any later version.
33 * This program is distributed in the hope that it will
34 * be useful, but WITHOUT ANY WARRANTY; without even the
35 * implied warranty of MERCHANTABILITY or FITNESS FOR A
36 * PARTICULAR PURPOSE. See the GNU General Public
37 * License for more details.
39 * The GNU General Public License should be included with
40 * this file. If not, you can view it at
41 * http://www.gnu.org/copyleft/gpl.html
42 * or write to the Free Software Foundation, Inc., 59
43 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 * Revision 1.115 2007/11/02 16:52:50 fabiankeil
48 * Remove a "can't happen" error block which, over
49 * time, mutated into a "guaranteed to happen" block.
51 * Revision 1.114 2007/10/19 16:56:26 fabiankeil
52 * - Downgrade "Buffer limit reached" message to LOG_LEVEL_INFO.
53 * - Use shiny new content_filters_enabled() in client_range().
55 * Revision 1.113 2007/10/10 17:29:57 fabiankeil
56 * I forgot about Poland.
58 * Revision 1.112 2007/10/09 16:38:40 fabiankeil
59 * Remove Range and If-Range headers if content filtering is enabled.
61 * Revision 1.111 2007/10/04 18:07:00 fabiankeil
62 * Move ACTION_VANILLA_WAFER handling from jcc's chat() into
63 * client_cookie_adder() to make sure send-vanilla-wafer can be
64 * controlled through tags (and thus regression-tested).
66 * Revision 1.110 2007/09/29 10:42:37 fabiankeil
67 * - Remove "scanning headers for" log message again.
68 * - Some more whitespace fixes.
70 * Revision 1.109 2007/09/08 14:25:48 fabiankeil
71 * Refactor client_referrer() and add conditional-forge parameter.
73 * Revision 1.108 2007/08/28 18:21:03 fabiankeil
74 * A bunch of whitespace fixes, pointy hat to me.
76 * Revision 1.107 2007/08/28 18:16:32 fabiankeil
77 * Fix possible memory corruption in server_http, make sure it's not
78 * executed for ordinary server headers and mark some problems for later.
80 * Revision 1.106 2007/08/18 14:30:32 fabiankeil
81 * Let content-type-overwrite{} honour force-text-mode again.
83 * Revision 1.105 2007/08/11 14:49:49 fabiankeil
84 * - Add prototpyes for the header parsers and make them static.
85 * - Comment out client_accept_encoding_adder() which isn't used right now.
87 * Revision 1.104 2007/07/14 07:38:19 fabiankeil
88 * Move the ACTION_FORCE_TEXT_MODE check out of
89 * server_content_type(). Signal other functions
90 * whether or not a content type has been declared.
91 * Part of the fix for BR#1750917.
93 * Revision 1.103 2007/06/01 16:31:54 fabiankeil
94 * Change sed() to return a jb_err in preparation for forward-override{}.
96 * Revision 1.102 2007/05/27 12:39:32 fabiankeil
97 * Adjust "X-Filter: No" to disable dedicated header filters.
99 * Revision 1.101 2007/05/14 10:16:41 fabiankeil
100 * Streamline client_cookie_adder().
102 * Revision 1.100 2007/04/30 15:53:11 fabiankeil
103 * Make sure filters with dynamic jobs actually use them.
105 * Revision 1.99 2007/04/30 15:06:26 fabiankeil
106 * - Introduce dynamic pcrs jobs that can resolve variables.
107 * - Remove unnecessary update_action_bits_for_all_tags() call.
109 * Revision 1.98 2007/04/17 18:32:10 fabiankeil
110 * - Make tagging based on tags set by earlier taggers
111 * of the same kind possible.
112 * - Log whether or not new tags cause action bits updates
113 * (in which case a matching tag-pattern section exists).
114 * - Log if the user tries to set a tag that is already set.
116 * Revision 1.97 2007/04/15 16:39:21 fabiankeil
117 * Introduce tags as alternative way to specify which
118 * actions apply to a request. At the moment tags can be
119 * created based on client and server headers.
121 * Revision 1.96 2007/04/12 12:53:58 fabiankeil
122 * Log a warning if the content is compressed, filtering is
123 * enabled and Privoxy was compiled without zlib support.
126 * Revision 1.95 2007/03/25 14:26:40 fabiankeil
127 * - Fix warnings when compiled with glibc.
128 * - Don't use crumble() for cookie crunching.
129 * - Move cookie time parsing into parse_header_time().
130 * - Let parse_header_time() return a jb_err code
131 * instead of a pointer that can only be used to
132 * check for NULL anyway.
134 * Revision 1.94 2007/03/21 12:23:53 fabiankeil
135 * - Add better protection against malicious gzip headers.
136 * - Stop logging the first hundred bytes of decompressed content.
137 * It looks like it's working and there is always debug 16.
138 * - Log the content size after decompression in decompress_iob()
139 * instead of pcrs_filter_response().
141 * Revision 1.93 2007/03/20 15:21:44 fabiankeil
142 * - Use dedicated header filter actions instead of abusing "filter".
143 * Replace "filter-client-headers" and "filter-client-headers"
144 * with "server-header-filter" and "client-header-filter".
145 * - Remove filter_client_header() and filter_client_header(),
146 * filter_header() now checks the shiny new
147 * CSP_FLAG_CLIENT_HEADER_PARSING_DONE flag instead.
149 * Revision 1.92 2007/03/05 13:25:32 fabiankeil
150 * - Cosmetical changes for LOG_LEVEL_RE_FILTER messages.
151 * - Handle "Cookie:" and "Connection:" headers a bit smarter
152 * (don't crunch them just to recreate them later on).
153 * - Add another non-standard time format for the cookie
154 * expiration date detection.
155 * - Fix a valgrind warning.
157 * Revision 1.91 2007/02/24 12:27:32 fabiankeil
158 * Improve cookie expiration date detection.
160 * Revision 1.90 2007/02/08 19:12:35 fabiankeil
161 * Don't run server_content_length() the first time
162 * sed() parses server headers; only adjust the
163 * Content-Length header if the page was modified.
165 * Revision 1.89 2007/02/07 16:52:11 fabiankeil
166 * Fix log messages regarding the cookie time format
167 * (cookie and request URL were mixed up).
169 * Revision 1.88 2007/02/07 11:27:12 fabiankeil
170 * - Let decompress_iob()
171 * - not corrupt the content if decompression fails
172 * early. (the first byte(s) were lost).
173 * - use pointer arithmetics with defined outcome for
175 * - Use a different kludge to remember a failed decompression.
177 * Revision 1.87 2007/01/31 16:21:38 fabiankeil
178 * Search for Max-Forwards headers case-insensitive,
179 * don't generate the "501 unsupported" message for invalid
180 * Max-Forwards values and don't increase negative ones.
182 * Revision 1.86 2007/01/30 13:05:26 fabiankeil
183 * - Let server_set_cookie() check the expiration date
184 * of cookies and don't touch the ones that are already
185 * expired. Fixes problems with low quality web applications
186 * as described in BR 932612.
188 * - Adjust comment in client_max_forwards to reality;
189 * remove invalid Max-Forwards headers.
191 * Revision 1.85 2007/01/26 15:33:46 fabiankeil
192 * Stop filter_header() from unintentionally removing
193 * empty header lines that were enlisted by the continue
196 * Revision 1.84 2007/01/24 12:56:52 fabiankeil
197 * - Repeat the request URL before logging any headers.
198 * Makes reading the log easier in case of simultaneous requests.
199 * - If there are more than one Content-Type headers in one request,
200 * use the first one and remove the others.
201 * - Remove "newval" variable in server_content_type().
202 * It's only used once.
204 * Revision 1.83 2007/01/12 15:03:02 fabiankeil
205 * Correct a cast, check inflateEnd() exit code
206 * to see if we have to, replace sprintf calls
209 * Revision 1.82 2007/01/01 19:36:37 fabiankeil
210 * Integrate a modified version of Wil Mahan's
211 * zlib patch (PR #895531).
213 * Revision 1.81 2006/12/31 22:21:33 fabiankeil
214 * Skip empty filter files in filter_header()
215 * but don't ignore the ones that come afterwards.
216 * Fixes BR 1619208, this time for real.
218 * Revision 1.80 2006/12/29 19:08:22 fabiankeil
219 * Reverted parts of my last commit
220 * to keep error handling working.
222 * Revision 1.79 2006/12/29 18:04:40 fabiankeil
223 * Fixed gcc43 conversion warnings.
225 * Revision 1.78 2006/12/26 17:19:20 fabiankeil
226 * Bringing back the "useless" localtime() call
227 * I removed in revision 1.67. On some platforms
228 * it's necessary to prevent time zone offsets.
230 * Revision 1.77 2006/12/07 18:44:26 fabiankeil
231 * Rebuild request URL in get_destination_from_headers()
232 * to make sure redirect{pcrs command} works as expected
233 * for intercepted requests.
235 * Revision 1.76 2006/12/06 19:52:25 fabiankeil
236 * Added get_destination_from_headers().
238 * Revision 1.75 2006/11/13 19:05:51 fabiankeil
239 * Make pthread mutex locking more generic. Instead of
240 * checking for OSX and OpenBSD, check for FEATURE_PTHREAD
241 * and use mutex locking unless there is an _r function
242 * available. Better safe than sorry.
244 * Fixes "./configure --disable-pthread" and should result
245 * in less threading-related problems on pthread-using platforms,
246 * but it still doesn't fix BR#1122404.
248 * Revision 1.74 2006/10/02 16:59:12 fabiankeil
249 * The special header "X-Filter: No" now disables
250 * header filtering as well.
252 * Revision 1.73 2006/09/23 13:26:38 roro
253 * Replace TABs by spaces in source code.
255 * Revision 1.72 2006/09/23 12:37:21 fabiankeil
256 * Don't print a log message every time filter_headers is
257 * entered or left. It only creates noise without any real
260 * Revision 1.71 2006/09/21 19:55:17 fabiankeil
261 * Fix +hide-if-modified-since{-n}.
263 * Revision 1.70 2006/09/08 12:06:34 fabiankeil
264 * Have hide-if-modified-since interpret the random
265 * range value as minutes instead of hours. Allows
266 * more fine-grained configuration.
268 * Revision 1.69 2006/09/06 16:25:51 fabiankeil
269 * Always have parse_header_time return a pointer
270 * that actual makes sense, even though we currently
271 * only need it to detect problems.
273 * Revision 1.68 2006/09/06 10:43:32 fabiankeil
274 * Added config option enable-remote-http-toggle
275 * to specify if Privoxy should recognize special
276 * headers (currently only X-Filter) to change its
277 * behaviour. Disabled by default.
279 * Revision 1.67 2006/09/04 11:01:26 fabiankeil
280 * After filtering de-chunked instances, remove
281 * "Transfer-Encoding" header entirely instead of changing
282 * it to "Transfer-Encoding: identity", which is invalid.
283 * Thanks Michael Shields <shields@msrl.com>. Fixes PR 1318658.
285 * Don't use localtime in parse_header_time. An empty time struct
286 * is good enough, it gets overwritten by strptime anyway.
288 * Revision 1.66 2006/09/03 19:38:28 fabiankeil
289 * Use gmtime_r if available, fallback to gmtime with mutex
290 * protection for MacOSX and use vanilla gmtime for the rest.
292 * Revision 1.65 2006/08/22 10:55:56 fabiankeil
293 * Changed client_referrer to use the right type (size_t) for
294 * hostlenght and to shorten the temporary referrer string with
295 * '\0' instead of adding a useless line break.
297 * Revision 1.64 2006/08/17 17:15:10 fabiankeil
298 * - Back to timegm() using GnuPG's replacement if necessary.
299 * Using mktime() and localtime() could add a on hour offset if
300 * the randomize factor was big enough to lead to a summer/wintertime
303 * - Removed now-useless Privoxy 3.0.3 compatibility glue.
305 * - Moved randomization code into pick_from_range().
307 * - Changed parse_header_time definition.
308 * time_t isn't guaranteed to be signed and
309 * if it isn't, -1 isn't available as error code.
310 * Changed some variable types in client_if_modified_since()
311 * because of the same reason.
313 * Revision 1.63 2006/08/14 13:18:08 david__schmidt
314 * OS/2 compilation compatibility fixups
316 * Revision 1.62 2006/08/14 08:58:42 fabiankeil
317 * Changed include from strptime.c to strptime.h
319 * Revision 1.61 2006/08/14 08:25:19 fabiankeil
320 * Split filter-headers{} into filter-client-headers{}
321 * and filter-server-headers{}.
322 * Added parse_header_time() to share some code.
323 * Replaced timegm() with mktime().
325 * Revision 1.60 2006/08/12 03:54:37 david__schmidt
326 * Windows service integration
328 * Revision 1.59 2006/08/03 02:46:41 david__schmidt
329 * Incorporate Fabian Keil's patch work:
\rhttp://www.fabiankeil.de/sourcecode/privoxy/
331 * Revision 1.58 2006/07/18 14:48:47 david__schmidt
332 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
333 * with what was really the latest development (the v_3_0_branch branch)
335 * Revision 1.56.2.10 2006/01/21 16:16:08 david__schmidt
336 * Thanks to Edward Carrel for his patch to modernize OSX's
\rpthreads support. See bug #1409623.
338 * Revision 1.56.2.9 2004/10/03 12:53:45 david__schmidt
339 * Add the ability to check jpeg images for invalid
340 * lengths of comment blocks. Defensive strategy
341 * against the exploit:
342 * Microsoft Security Bulletin MS04-028
343 * Buffer Overrun in JPEG Processing (GDI+) Could
344 * Allow Code Execution (833987)
345 * Enabled with +inspect-jpegs in actions files.
347 * Revision 1.56.2.8 2003/07/11 13:21:25 oes
348 * Excluded text/plain objects from filtering. This fixes a
349 * couple of client-crashing, download corruption and
350 * Privoxy performance issues, whose root cause lies in
351 * web servers labelling content of unknown type as text/plain.
353 * Revision 1.56.2.7 2003/05/06 12:07:26 oes
354 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
356 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
357 * Completing the previous change
359 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
360 * Added temporary workaround for bug in PHP < 4.2.3
362 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
363 * 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.
365 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
366 * Fix typo: supressed -> suppressed
368 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
369 * Improved cookie logging
371 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
372 * Added basic support for OPTIONS and TRACE HTTP methods:
373 * - New parser function client_max_forwards which decrements
374 * the Max-Forwards HTTP header field of OPTIONS and TRACE
375 * requests by one before forwarding
376 * - New parser function client_host which extracts the host
377 * and port information from the HTTP header field if the
378 * request URI was not absolute
379 * - Don't crumble and re-add the Host: header, but only generate
380 * and append if missing
382 * Revision 1.56 2002/05/12 15:34:22 jongfoster
383 * Fixing typo in a comment
385 * Revision 1.55 2002/05/08 16:01:07 oes
386 * Optimized add_to_iob:
387 * - Use realloc instead of malloc(), memcpy(), free()
388 * - Expand to powers of two if possible, to get
389 * O(log n) reallocs instead of O(n).
390 * - Moved check for buffer limit here from chat
391 * - Report failure via returncode
393 * Revision 1.54 2002/04/02 15:03:16 oes
394 * Tiny code cosmetics
396 * Revision 1.53 2002/03/26 22:29:55 swa
397 * we have a new homepage!
399 * Revision 1.52 2002/03/24 13:25:43 swa
400 * name change related issues
402 * Revision 1.51 2002/03/13 00:27:05 jongfoster
405 * Revision 1.50 2002/03/12 01:45:35 oes
406 * More verbose logging
408 * Revision 1.49 2002/03/09 20:03:52 jongfoster
409 * - Making various functions return int rather than size_t.
410 * (Undoing a recent change). Since size_t is unsigned on
411 * Windows, functions like read_socket that return -1 on
412 * error cannot return a size_t.
414 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
415 * crashes, and also frequently caused JB to jump to 100%
416 * CPU and stay there. (Because it thought it had just
417 * read ((unsigned)-1) == 4Gb of data...)
419 * - The signature of write_socket has changed, it now simply
420 * returns success=0/failure=nonzero.
422 * - Trying to get rid of a few warnings --with-debug on
423 * Windows, I've introduced a new type "jb_socket". This is
424 * used for the socket file descriptors. On Windows, this
425 * is SOCKET (a typedef for unsigned). Everywhere else, it's
426 * an int. The error value can't be -1 any more, so it's
427 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
428 * Windows it maps to the #define INVALID_SOCKET.)
430 * - The signature of bind_port has changed.
432 * Revision 1.48 2002/03/07 03:46:53 oes
433 * Fixed compiler warnings etc
435 * Revision 1.47 2002/02/20 23:15:13 jongfoster
436 * Parsing functions now handle out-of-memory gracefully by returning
439 * Revision 1.46 2002/01/17 21:03:47 jongfoster
440 * Moving all our URL and URL pattern parsing code to urlmatch.c.
442 * Revision 1.45 2002/01/09 14:33:03 oes
443 * Added support for localtime_r.
445 * Revision 1.44 2001/12/14 01:22:54 steudten
446 * Remove 'user:pass@' from 'proto://user:pass@host' for the
447 * new added header 'Host: ..'. (See Req ID 491818)
449 * Revision 1.43 2001/11/23 00:26:38 jongfoster
450 * Fixing two really stupid errors in my previous commit
452 * Revision 1.42 2001/11/22 21:59:30 jongfoster
453 * Adding code to handle +no-cookies-keep
455 * Revision 1.41 2001/11/05 23:43:05 steudten
456 * Add time+date to log files.
458 * Revision 1.40 2001/10/26 20:13:09 jongfoster
459 * ctype.h is needed in Windows, too.
461 * Revision 1.39 2001/10/26 17:40:04 oes
462 * Introduced get_header_value()
463 * Removed http->user_agent, csp->referrer and csp->accept_types
464 * Removed client_accept()
466 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
467 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
468 * threads to call select() simultaneously. So, it's time to do a real, live,
469 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
470 * (native). Both versions will work, but using __OS2__ offers multi-threading.
472 * Revision 1.37 2001/10/23 21:36:02 jongfoster
473 * Documenting sed()'s error behaviou (doc change only)
475 * Revision 1.36 2001/10/13 12:51:51 joergs
476 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
477 * force-load), instead crumble Host: and add it (again) in client_host_adder
478 * (in case we get a HTTP/1.0 request without Host: header and forward it to
479 * a HTTP/1.1 server/proxy).
481 * Revision 1.35 2001/10/09 22:39:21 jongfoster
482 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
485 * Revision 1.34 2001/10/07 18:50:55 oes
486 * Added server_content_encoding, renamed server_transfer_encoding
488 * Revision 1.33 2001/10/07 18:04:49 oes
489 * Changed server_http11 to server_http and its pattern to "HTTP".
490 * Additional functionality: it now saves the HTTP status into
491 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
493 * Revision 1.32 2001/10/07 15:43:28 oes
494 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
495 * client_te and client_accept_encoding_adder, triggered by the new
496 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
497 * changed to allow only identity and chunked, and the TE header is
498 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
500 * parse_http_request no longer does anything than parsing. The rewriting
501 * of http->cmd and version mangling are gone. It now also recognizes
502 * the put and delete methods and saves the url in http->url. Removed
505 * renamed content_type and content_length to have the server_ prefix
507 * server_content_type now only works if csp->content_type != CT_TABOO
509 * added server_transfer_encoding, which
510 * - Sets CT_TABOO to prohibit filtering if encoding compresses
511 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
512 * - Change from "chunked" to "identity" if body was chunked
513 * but has been de-chunked for filtering.
515 * added server_content_md5 which crunches any Content-MD5 headers
516 * if the body was modified.
518 * made server_http11 conditional on +downgrade action
520 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
522 * Revision 1.31 2001/10/05 14:25:02 oes
523 * Crumble Keep-Alive from Server
525 * Revision 1.30 2001/09/29 12:56:03 joergs
526 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
528 * Revision 1.29 2001/09/24 21:09:24 jongfoster
529 * Fixing 2 memory leaks that Guy spotted, where the paramater to
530 * enlist() was not being free()d.
532 * Revision 1.28 2001/09/22 16:32:28 jongfoster
533 * Removing unused #includes.
535 * Revision 1.27 2001/09/20 15:45:25 steudten
537 * add casting from size_t to int for printf()
538 * remove local variable shadow s2
540 * Revision 1.26 2001/09/16 17:05:14 jongfoster
541 * Removing unused #include showarg.h
543 * Revision 1.25 2001/09/16 13:21:27 jongfoster
544 * Changes to use new list functions.
546 * Revision 1.24 2001/09/13 23:05:50 jongfoster
547 * Changing the string paramater to the header parsers a "const".
549 * Revision 1.23 2001/09/12 18:08:19 steudten
551 * In parse_http_request() header rewriting miss the host value, so
552 * from http://www.mydomain.com the result was just " / " not
553 * http://www.mydomain.com/ in case we forward.
555 * Revision 1.22 2001/09/10 10:58:53 oes
556 * Silenced compiler warnings
558 * Revision 1.21 2001/07/31 14:46:00 oes
559 * - Persistant connections now suppressed
560 * - sed() no longer appends empty header to csp->headers
562 * Revision 1.20 2001/07/30 22:08:36 jongfoster
563 * Tidying up #defines:
564 * - All feature #defines are now of the form FEATURE_xxx
565 * - Permanently turned off WIN_GUI_EDIT
566 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
568 * Revision 1.19 2001/07/25 17:21:54 oes
569 * client_uagent now saves copy of User-Agent: header value
571 * Revision 1.18 2001/07/13 14:02:46 oes
572 * - Included fix to repair broken HTTP requests that
573 * don't contain a path, not even '/'.
574 * - Removed all #ifdef PCRS
575 * - content_type now always inspected and classified as
576 * text, gif or other.
577 * - formatting / comments
579 * Revision 1.17 2001/06/29 21:45:41 oes
580 * Indentation, CRLF->LF, Tab-> Space
582 * Revision 1.16 2001/06/29 13:32:42 oes
584 * - Adapted free_http_request
585 * - Removed logentry from cancelled commit
587 * Revision 1.15 2001/06/03 19:12:38 oes
588 * deleted const struct interceptors
590 * Revision 1.14 2001/06/01 18:49:17 jongfoster
591 * Replaced "list_share" with "list" - the tiny memory gain was not
592 * worth the extra complexity.
594 * Revision 1.13 2001/05/31 21:30:33 jongfoster
595 * Removed list code - it's now in list.[ch]
596 * Renamed "permission" to "action", and changed many features
597 * to use the actions file rather than the global config.
599 * Revision 1.12 2001/05/31 17:33:13 oes
603 * Revision 1.11 2001/05/29 20:11:19 joergs
604 * '/ * inside comment' warning removed.
606 * Revision 1.10 2001/05/29 09:50:24 jongfoster
607 * Unified blocklist/imagelist/permissionslist.
608 * File format is still under discussion, but the internal changes
611 * Also modified interceptor behaviour:
612 * - We now intercept all URLs beginning with one of the following
613 * prefixes (and *only* these prefixes):
615 * * http://ijbswa.sf.net/config/
616 * * http://ijbswa.sourceforge.net/config/
617 * - New interceptors "home page" - go to http://i.j.b/ to see it.
618 * - Internal changes so that intercepted and fast redirect pages
619 * are not replaced with an image.
620 * - Interceptors now have the option to send a binary page direct
621 * to the client. (i.e. ijb-send-banner uses this)
622 * - Implemented show-url-info interceptor. (Which is why I needed
623 * the above interceptors changes - a typical URL is
624 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
625 * The previous mechanism would not have intercepted that, and
626 * if it had been intercepted then it then it would have replaced
629 * Revision 1.9 2001/05/28 17:26:33 jongfoster
630 * Fixing segfault if last header was crunched.
631 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
632 * can use the cross-platform sprintf() instead.)
634 * Revision 1.8 2001/05/27 22:17:04 oes
636 * - re_process_buffer no longer writes the modified buffer
637 * to the client, which was very ugly. It now returns the
638 * buffer, which it is then written by chat.
640 * - content_length now adjusts the Content-Length: header
641 * for modified documents rather than crunch()ing it.
642 * (Length info in csp->content_length, which is 0 for
643 * unmodified documents)
645 * - For this to work, sed() is called twice when filtering.
647 * Revision 1.7 2001/05/27 13:19:06 oes
648 * Patched Joergs solution for the content-length in.
650 * Revision 1.6 2001/05/26 13:39:32 jongfoster
651 * Only crunches Content-Length header if applying RE filtering.
652 * Without this fix, Microsoft Windows Update wouldn't work.
654 * Revision 1.5 2001/05/26 00:28:36 jongfoster
655 * Automatic reloading of config file.
656 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
657 * Most of the global variables have been moved to a new
658 * struct configuration_spec, accessed through csp->config->globalname
659 * Most of the globals remaining are used by the Win32 GUI.
661 * Revision 1.4 2001/05/22 18:46:04 oes
663 * - Enabled filtering banners by size rather than URL
664 * by adding patterns that replace all standard banner
665 * sizes with the "Junkbuster" gif to the re_filterfile
667 * - Enabled filtering WebBugs by providing a pattern
668 * which kills all 1x1 images
670 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
671 * which is selected by the (nonstandard and therefore
672 * capital) letter 'U' in the option string.
673 * It causes the quantifiers to be ungreedy by default.
674 * Appending a ? turns back to greedy (!).
676 * - Added a new interceptor ijb-send-banner, which
677 * sends back the "Junkbuster" gif. Without imagelist or
678 * MSIE detection support, or if tinygif = 1, or the
679 * URL isn't recognized as an imageurl, a lame HTML
680 * explanation is sent instead.
682 * - Added new feature, which permits blocking remote
683 * script redirects and firing back a local redirect
685 * The feature is conditionally compiled, i.e. it
686 * can be disabled with --disable-fast-redirects,
687 * plus it must be activated by a "fast-redirects"
688 * line in the config file, has its own log level
689 * and of course wants to be displayed by show-proxy-args
690 * Note: Boy, all the #ifdefs in 1001 locations and
691 * all the fumbling with configure.in and acconfig.h
692 * were *way* more work than the feature itself :-(
694 * - Because a generic redirect template was needed for
695 * this, tinygif = 3 now uses the same.
697 * - Moved GIFs, and other static HTTP response templates
702 * - Removed some >400 CRs again (Jon, you really worked
705 * Revision 1.3 2001/05/20 01:21:20 jongfoster
706 * Version 2.9.4 checkin.
707 * - Merged popupfile and cookiefile, and added control over PCRS
708 * filtering, in new "permissionsfile".
709 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
710 * file error you now get a message box (in the Win32 GUI) rather
711 * than the program exiting with no explanation.
712 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
714 * - Removed tabs from "config"
715 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
716 * - Bumped up version number.
718 * Revision 1.2 2001/05/17 23:02:36 oes
719 * - Made referrer option accept 'L' as a substitute for '§'
721 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
722 * Initial import of version 2.9.3 source tree
725 *********************************************************************/
732 #include <sys/types.h>
742 * Convince GNU's libc to provide a strptime prototype.
745 #endif /*__GLIBC__ */
752 #if !defined(_WIN32) && !defined(__OS2__)
758 #ifdef FEATURE_PTHREAD
760 /* jcc.h is for mutex semapores only */
761 #endif /* def FEATURE_PTHREAD */
767 #include "jbsockets.h"
768 #include "miscutil.h"
773 #ifndef HAVE_STRPTIME
774 #include "strptime.h"
777 const char parsers_h_rcs[] = PARSERS_H_VERSION;
779 /* Fix a problem with Solaris. There should be no effect on other
781 * Solaris's isspace() is a macro which uses its argument directly
782 * as an array index. Therefore we need to make sure that high-bit
783 * characters generate +ve values, and ideally we also want to make
784 * the argument match the declared parameter type of "int".
786 * Why did they write a character function that can't take a simple
787 * "char" argument? Doh!
789 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
790 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
792 static jb_err scan_headers(struct client_state *csp);
793 static jb_err header_tagger(struct client_state *csp, char *header);
794 static jb_err parse_header_time(const char *header_time, time_t *result);
796 static jb_err crumble (struct client_state *csp, char **header);
797 static jb_err connection (struct client_state *csp, char **header);
798 static jb_err filter_header (struct client_state *csp, char **header);
799 static jb_err client_referrer (struct client_state *csp, char **header);
800 static jb_err client_uagent (struct client_state *csp, char **header);
801 static jb_err client_ua (struct client_state *csp, char **header);
802 static jb_err client_from (struct client_state *csp, char **header);
803 static jb_err client_send_cookie (struct client_state *csp, char **header);
804 static jb_err client_x_forwarded (struct client_state *csp, char **header);
805 static jb_err client_accept_encoding (struct client_state *csp, char **header);
806 static jb_err client_te (struct client_state *csp, char **header);
807 static jb_err client_max_forwards (struct client_state *csp, char **header);
808 static jb_err client_host (struct client_state *csp, char **header);
809 static jb_err client_if_modified_since (struct client_state *csp, char **header);
810 static jb_err client_accept_language (struct client_state *csp, char **header);
811 static jb_err client_if_none_match (struct client_state *csp, char **header);
812 static jb_err crunch_client_header (struct client_state *csp, char **header);
813 static jb_err client_x_filter (struct client_state *csp, char **header);
814 static jb_err client_range (struct client_state *csp, char **header);
815 static jb_err server_set_cookie (struct client_state *csp, char **header);
816 static jb_err server_content_type (struct client_state *csp, char **header);
817 static jb_err server_content_length (struct client_state *csp, char **header);
818 static jb_err server_content_md5 (struct client_state *csp, char **header);
819 static jb_err server_content_encoding (struct client_state *csp, char **header);
820 static jb_err server_transfer_coding (struct client_state *csp, char **header);
821 static jb_err server_http (struct client_state *csp, char **header);
822 static jb_err crunch_server_header (struct client_state *csp, char **header);
823 static jb_err server_last_modified (struct client_state *csp, char **header);
824 static jb_err server_content_disposition(struct client_state *csp, char **header);
826 static jb_err client_host_adder (struct client_state *csp);
827 static jb_err client_cookie_adder (struct client_state *csp);
828 static jb_err client_xtra_adder (struct client_state *csp);
829 static jb_err client_x_forwarded_adder(struct client_state *csp);
830 static jb_err connection_close_adder (struct client_state *csp);
832 static jb_err create_forged_referrer(char **header, const char *hostport);
833 static jb_err create_fake_referrer(char **header, const char *fake_referrer);
834 static jb_err handle_conditional_hide_referrer_parameter(char **header,
835 const char *host, const int parameter_conditional_block);
837 const struct parsers client_patterns[] = {
838 { "referer:", 8, client_referrer },
839 { "user-agent:", 11, client_uagent },
840 { "ua-", 3, client_ua },
841 { "from:", 5, client_from },
842 { "cookie:", 7, client_send_cookie },
843 { "x-forwarded-for:", 16, client_x_forwarded },
844 { "Accept-Encoding:", 16, client_accept_encoding },
845 { "TE:", 3, client_te },
846 { "Host:", 5, client_host },
847 { "if-modified-since:", 18, client_if_modified_since },
848 { "Keep-Alive:", 11, crumble },
849 { "connection:", 11, connection },
850 { "proxy-connection:", 17, crumble },
851 { "max-forwards:", 13, client_max_forwards },
852 { "Accept-Language:", 16, client_accept_language },
853 { "if-none-match:", 14, client_if_none_match },
854 { "Range:", 6, client_range },
855 { "If-Range:", 9, client_range },
856 { "X-Filter:", 9, client_x_filter },
857 { "*", 0, crunch_client_header },
858 { "*", 0, filter_header },
862 const struct parsers server_patterns[] = {
863 { "HTTP/", 5, server_http },
864 { "set-cookie:", 11, server_set_cookie },
865 { "connection:", 11, connection },
866 { "Content-Type:", 13, server_content_type },
867 { "Content-MD5:", 12, server_content_md5 },
868 { "Content-Encoding:", 17, server_content_encoding },
869 { "Transfer-Encoding:", 18, server_transfer_coding },
870 { "Keep-Alive:", 11, crumble },
871 { "content-disposition:", 20, server_content_disposition },
872 { "Last-Modified:", 14, server_last_modified },
873 { "*", 0, crunch_server_header },
874 { "*", 0, filter_header },
878 const struct parsers server_patterns_light[] = {
879 { "Content-Length:", 15, server_content_length },
880 { "Transfer-Encoding:", 18, server_transfer_coding },
882 { "Content-Encoding:", 17, server_content_encoding },
883 #endif /* def FEATURE_ZLIB */
887 const add_header_func_ptr add_client_headers[] = {
890 client_x_forwarded_adder,
892 /* Temporarily disabled: client_accept_encoding_adder, */
893 connection_close_adder,
897 const add_header_func_ptr add_server_headers[] = {
898 connection_close_adder,
902 /* The vanilla wafer. */
903 static const char VANILLA_WAFER[] =
904 "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
905 "Do_not_send_me_any_copyrighted_information_other_than_the_"
906 "document_that_I_am_requesting_or_any_of_its_necessary_components._"
907 "In_particular_do_not_send_me_any_cookies_that_"
908 "are_subject_to_a_claim_of_copyright_by_anybody._"
909 "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
910 "(copyright_or_otherwise)_applying_to_any_cookie._";
912 /*********************************************************************
914 * Function : flush_socket
916 * Description : Write any pending "buffered" content.
919 * 1 : fd = file descriptor of the socket to read
920 * 2 : csp = Current client state (buffers, headers, etc...)
922 * Returns : On success, the number of bytes written are returned (zero
923 * indicates nothing was written). On error, -1 is returned,
924 * and errno is set appropriately. If count is zero and the
925 * file descriptor refers to a regular file, 0 will be
926 * returned without causing any other effect. For a special
927 * file, the results are not portable.
929 *********************************************************************/
930 int flush_socket(jb_socket fd, struct client_state *csp)
932 struct iob *iob = csp->iob;
933 int len = iob->eod - iob->cur;
940 if (write_socket(fd, iob->cur, (size_t)len))
944 iob->eod = iob->cur = iob->buf;
950 /*********************************************************************
952 * Function : add_to_iob
954 * Description : Add content to the buffered page, expanding the
955 * buffer if necessary.
958 * 1 : csp = Current client state (buffers, headers, etc...)
959 * 2 : buf = holds the content to be added to the page
960 * 3 : n = number of bytes to be added
962 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
963 * or buffer limit reached.
965 *********************************************************************/
966 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
968 struct iob *iob = csp->iob;
969 size_t used, offset, need, want;
972 if (n <= 0) return JB_ERR_OK;
974 used = (size_t)(iob->eod - iob->buf);
975 offset = (size_t)(iob->cur - iob->buf);
976 need = used + (size_t)n + 1;
979 * If the buffer can't hold the new data, extend it first.
980 * Use the next power of two if possible, else use the actual need.
982 if (need > csp->config->buffer_limit)
984 log_error(LOG_LEVEL_INFO, "Buffer limit reached while extending the buffer (iob)");
985 return JB_ERR_MEMORY;
988 if (need > iob->size)
990 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
992 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
996 else if (NULL != (p = (char *)realloc(iob->buf, need)))
1002 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
1003 return JB_ERR_MEMORY;
1006 /* Update the iob pointers */
1007 iob->cur = p + offset;
1008 iob->eod = p + used;
1012 /* copy the new data into the iob buffer */
1013 memcpy(iob->eod, buf, (size_t)n);
1015 /* point to the end of the data */
1018 /* null terminate == cheap insurance */
1027 /*********************************************************************
1029 * Function : decompress_iob
1031 * Description : Decompress buffered page, expanding the
1032 * buffer as necessary. csp->iob->cur
1033 * should point to the the beginning of the
1034 * compressed data block.
1037 * 1 : csp = Current client state (buffers, headers, etc...)
1039 * Returns : JB_ERR_OK on success,
1040 * JB_ERR_MEMORY if out-of-memory limit reached, and
1041 * JB_ERR_COMPRESS if error decompressing buffer.
1043 *********************************************************************/
1044 jb_err decompress_iob(struct client_state *csp)
1046 char *buf; /* new, uncompressed buffer */
1047 char *cur; /* Current iob position (to keep the original
1048 * iob->cur unmodified if we return early) */
1049 size_t bufsize; /* allocated size of the new buffer */
1050 size_t old_size; /* Content size before decompression */
1051 size_t skip_size; /* Number of bytes at the beginning of the iob
1052 that we should NOT decompress. */
1053 int status; /* return status of the inflate() call */
1054 z_stream zstr; /* used by calls to zlib */
1056 assert(csp->iob->cur - csp->iob->buf > 0);
1057 assert(csp->iob->eod - csp->iob->cur > 0);
1059 bufsize = csp->iob->size;
1060 skip_size = (size_t)(csp->iob->cur - csp->iob->buf);
1061 old_size = (size_t)(csp->iob->eod - csp->iob->cur);
1063 cur = csp->iob->cur;
1068 * This is to protect the parsing of gzipped data,
1069 * but it should(?) be valid for deflated data also.
1071 log_error(LOG_LEVEL_ERROR, "Buffer too small decompressing iob");
1072 return JB_ERR_COMPRESS;
1075 if (csp->content_type & CT_GZIP)
1078 * Our task is slightly complicated by the facts that data
1079 * compressed by gzip does not include a zlib header, and
1080 * that there is no easily accessible interface in zlib to
1081 * handle a gzip header. We strip off the gzip header by
1082 * hand, and later inform zlib not to expect a header.
1086 * Strip off the gzip header. Please see RFC 1952 for more
1087 * explanation of the appropriate fields.
1089 if ((*cur++ != (char)0x1f)
1090 || (*cur++ != (char)0x8b)
1091 || (*cur++ != Z_DEFLATED))
1093 log_error(LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
1094 return JB_ERR_COMPRESS;
1100 * XXX: These magic numbers should be replaced
1101 * with macros to give a better idea what they do.
1105 /* The gzip header has reserved bits set; bail out. */
1106 log_error(LOG_LEVEL_ERROR, "Invalid gzip header flags when decompressing");
1107 return JB_ERR_COMPRESS;
1111 /* Skip extra fields if necessary. */
1115 * Skip a given number of bytes, specified
1116 * as a 16-bit little-endian value.
1119 * XXX: This code used to be:
1121 * csp->iob->cur += *csp->iob->cur++ + (*csp->iob->cur++ << 8);
1123 * which I had to change into:
1125 * cur += *cur++ + (*cur++ << 8);
1127 * at which point gcc43 finally noticed that the value
1128 * of cur is undefined (it depends on which of the
1129 * summands is evaluated first).
1131 * I haven't come across a site where this
1132 * code is actually executed yet, but I hope
1136 skip_bytes = *cur++;
1137 skip_bytes = *cur++ << 8;
1139 assert(skip_bytes == *csp->iob->cur - 2 + ((*csp->iob->cur - 1) << 8));
1142 * The number of bytes to skip should be positive
1143 * and we'd like to stay in the buffer.
1145 if ((skip_bytes < 0) || (skip_bytes >= (csp->iob->eod - cur)))
1147 log_error(LOG_LEVEL_ERROR,
1148 "Unreasonable amount of bytes to skip (%d). Stopping decompression",
1150 return JB_ERR_COMPRESS;
1152 log_error(LOG_LEVEL_INFO,
1153 "Skipping %d bytes for gzip compression. Does this sound right?",
1158 /* Skip the filename if necessary. */
1161 /* A null-terminated string is supposed to follow. */
1162 while (*cur++ && (cur < csp->iob->eod));
1166 /* Skip the comment if necessary. */
1169 /* A null-terminated string is supposed to follow. */
1170 while (*cur++ && (cur < csp->iob->eod));
1173 /* Skip the CRC if necessary. */
1179 if (cur >= csp->iob->eod)
1182 * If the current position pointer reached or passed
1183 * the buffer end, we were obviously tricked to skip
1186 log_error(LOG_LEVEL_ERROR,
1187 "Malformed gzip header detected. Aborting decompression.");
1188 return JB_ERR_COMPRESS;
1192 else if (csp->content_type & CT_DEFLATE)
1195 * XXX: The debug level should be lowered
1196 * before the next stable release.
1198 log_error(LOG_LEVEL_INFO, "Decompressing deflated iob: %d", *cur);
1200 * In theory (that is, according to RFC 1950), deflate-compressed
1201 * data should begin with a two-byte zlib header and have an
1202 * adler32 checksum at the end. It seems that in practice only
1203 * the raw compressed data is sent. Note that this means that
1204 * we are not RFC 1950-compliant here, but the advantage is that
1205 * this actually works. :)
1207 * We add a dummy null byte to tell zlib where the data ends,
1208 * and later inform it not to expect a header.
1210 * Fortunately, add_to_iob() has thoughtfully null-terminated
1211 * the buffer; we can just increment the end pointer to include
1218 log_error(LOG_LEVEL_ERROR,
1219 "Unable to determine compression format for decompression");
1220 return JB_ERR_COMPRESS;
1223 /* Set up the fields required by zlib. */
1224 zstr.next_in = (Bytef *)cur;
1225 zstr.avail_in = (unsigned int)(csp->iob->eod - cur);
1226 zstr.zalloc = Z_NULL;
1227 zstr.zfree = Z_NULL;
1228 zstr.opaque = Z_NULL;
1231 * Passing -MAX_WBITS to inflateInit2 tells the library
1232 * that there is no zlib header.
1234 if (inflateInit2 (&zstr, -MAX_WBITS) != Z_OK)
1236 log_error(LOG_LEVEL_ERROR, "Error initializing decompression");
1237 return JB_ERR_COMPRESS;
1241 * Next, we allocate new storage for the inflated data.
1242 * We don't modify the existing iob yet, so in case there
1243 * is error in decompression we can recover gracefully.
1245 buf = zalloc(bufsize);
1248 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1249 return JB_ERR_MEMORY;
1252 assert(bufsize >= skip_size);
1253 memcpy(buf, csp->iob->buf, skip_size);
1254 zstr.avail_out = bufsize - skip_size;
1255 zstr.next_out = (Bytef *)buf + skip_size;
1257 /* Try to decompress the whole stream in one shot. */
1258 while (Z_BUF_ERROR == (status = inflate(&zstr, Z_FINISH)))
1260 /* We need to allocate more memory for the output buffer. */
1262 char *tmpbuf; /* used for realloc'ing the buffer */
1263 size_t oldbufsize = bufsize; /* keep track of the old bufsize */
1266 * If zlib wants more data then there's a problem, because
1267 * the complete compressed file should have been buffered.
1269 if (0 == zstr.avail_in)
1271 log_error(LOG_LEVEL_ERROR, "Unexpected end of compressed iob");
1272 return JB_ERR_COMPRESS;
1276 * If we tried the limit and still didn't have enough
1277 * memory, just give up.
1279 if (bufsize == csp->config->buffer_limit)
1281 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while decompressing iob");
1282 return JB_ERR_MEMORY;
1285 /* Try doubling the buffer size each time. */
1288 /* Don't exceed the buffer limit. */
1289 if (bufsize > csp->config->buffer_limit)
1291 bufsize = csp->config->buffer_limit;
1294 /* Try to allocate the new buffer. */
1295 tmpbuf = realloc(buf, bufsize);
1298 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1300 return JB_ERR_MEMORY;
1304 char *oldnext_out = (char *)zstr.next_out;
1307 * Update the fields for inflate() to use the new
1308 * buffer, which may be in a location different from
1311 zstr.avail_out += bufsize - oldbufsize;
1312 zstr.next_out = (Bytef *)tmpbuf + bufsize - zstr.avail_out;
1315 * Compare with an uglier method of calculating these values
1316 * that doesn't require the extra oldbufsize variable.
1318 assert(zstr.avail_out == tmpbuf + bufsize - (char *)zstr.next_out);
1319 assert((char *)zstr.next_out == tmpbuf + ((char *)oldnext_out - buf));
1320 assert(zstr.avail_out > 0);
1326 if (Z_STREAM_ERROR == inflateEnd(&zstr))
1328 log_error(LOG_LEVEL_ERROR,
1329 "Inconsistent stream state after decompression: %s", zstr.msg);
1331 * XXX: Intentionally no return.
1333 * According to zlib.h, Z_STREAM_ERROR is returned
1334 * "if the stream state was inconsistent".
1336 * I assume in this case inflate()'s status
1337 * would also be something different than Z_STREAM_END
1338 * so this check should be redundant, but lets see.
1342 if (status != Z_STREAM_END)
1344 /* We failed to decompress the stream. */
1345 log_error(LOG_LEVEL_ERROR,
1346 "Error in decompressing to the buffer (iob): %s", zstr.msg);
1347 return JB_ERR_COMPRESS;
1351 * Finally, we can actually update the iob, since the
1352 * decompression was successful. First, free the old
1355 freez(csp->iob->buf);
1357 /* Now, update the iob to use the new buffer. */
1358 csp->iob->buf = buf;
1359 csp->iob->cur = csp->iob->buf + skip_size;
1360 csp->iob->eod = (char *)zstr.next_out;
1361 csp->iob->size = bufsize;
1364 * Make sure the new uncompressed iob obeys some minimal
1365 * consistency conditions.
1367 if ((csp->iob->buf < csp->iob->cur)
1368 && (csp->iob->cur <= csp->iob->eod)
1369 && (csp->iob->eod <= csp->iob->buf + csp->iob->size))
1371 const size_t new_size = (size_t)(csp->iob->eod - csp->iob->cur);
1374 log_error(LOG_LEVEL_RE_FILTER,
1375 "Decompression successful. Old size: %d, new size: %d.",
1376 old_size, new_size);
1380 /* zlib thinks this is OK, so lets do the same. */
1381 log_error(LOG_LEVEL_INFO, "Decompression didn't result in any content.");
1386 /* It seems that zlib did something weird. */
1387 log_error(LOG_LEVEL_ERROR,
1388 "Unexpected error decompressing the buffer (iob): %d==%d, %d>%d, %d<%d",
1389 csp->iob->cur, csp->iob->buf + skip_size, csp->iob->eod, csp->iob->buf,
1390 csp->iob->eod, csp->iob->buf + csp->iob->size);
1391 return JB_ERR_COMPRESS;
1397 #endif /* defined(FEATURE_ZLIB) */
1400 /*********************************************************************
1402 * Function : get_header
1404 * Description : This (odd) routine will parse the csp->iob
1407 * 1 : csp = Current client state (buffers, headers, etc...)
1409 * Returns : Any one of the following:
1411 * 1) a pointer to a dynamically allocated string that contains a header line
1412 * 2) NULL indicating that the end of the header was reached
1413 * 3) "" indicating that the end of the iob was reached before finding
1414 * a complete header line.
1416 *********************************************************************/
1417 char *get_header(struct client_state *csp)
1423 if ((iob->cur == NULL)
1424 || ((p = strchr(iob->cur, '\n')) == NULL))
1426 return(""); /* couldn't find a complete header */
1431 ret = strdup(iob->cur);
1434 /* FIXME No way to handle error properly */
1435 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
1440 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
1442 /* is this a blank line (i.e. the end of the header) ? */
1454 /*********************************************************************
1456 * Function : get_header_value
1458 * Description : Get the value of a given header from a chained list
1459 * of header lines or return NULL if no such header is
1460 * present in the list.
1463 * 1 : header_list = pointer to list
1464 * 2 : header_name = string with name of header to look for.
1465 * Trailing colon required, capitalization
1468 * Returns : NULL if not found, else value of header
1470 *********************************************************************/
1471 char *get_header_value(const struct list *header_list, const char *header_name)
1473 struct list_entry *cur_entry;
1477 assert(header_list);
1478 assert(header_name);
1479 length = strlen(header_name);
1481 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
1485 if (!strncmpic(cur_entry->str, header_name, length))
1488 * Found: return pointer to start of value
1490 ret = (char *) (cur_entry->str + length);
1491 while (*ret && ijb_isspace(*ret)) ret++;
1505 /*********************************************************************
1507 * Function : scan_headers
1509 * Description : Scans headers, applies tags and updates action bits.
1512 * 1 : csp = Current client state (buffers, headers, etc...)
1514 * Returns : JB_ERR_OK
1516 *********************************************************************/
1517 static jb_err scan_headers(struct client_state *csp)
1519 struct list_entry *h; /* Header */
1520 jb_err err = JB_ERR_OK;
1522 for (h = csp->headers->first; (err == JB_ERR_OK) && (h != NULL) ; h = h->next)
1524 /* Header crunch()ed in previous run? -> ignore */
1525 if (h->str == NULL) continue;
1526 log_error(LOG_LEVEL_HEADER, "scan: %s", h->str);
1527 err = header_tagger(csp, h->str);
1534 /*********************************************************************
1538 * Description : add, delete or modify lines in the HTTP header streams.
1539 * On entry, it receives a linked list of headers space
1540 * that was allocated dynamically (both the list nodes
1541 * and the header contents).
1543 * As a side effect it frees the space used by the original
1546 * XXX: should be split to remove the first_run hack.
1549 * 1 : pats = list of patterns to match against headers
1550 * 2 : more_headers = list of functions to add more
1551 * headers (client or server)
1552 * 3 : csp = Current client state (buffers, headers, etc...)
1554 * Returns : JB_ERR_OK in case off success, or
1555 * JB_ERR_MEMORY on out-of-memory error.
1557 *********************************************************************/
1558 jb_err sed(const struct parsers pats[],
1559 const add_header_func_ptr more_headers[],
1560 struct client_state *csp)
1562 struct list_entry *p;
1563 const struct parsers *v;
1564 const add_header_func_ptr *f;
1565 jb_err err = JB_ERR_OK;
1569 * If filtering is enabled, sed is run twice,
1570 * but most of the work needs to be done only once.
1572 first_run = (more_headers != NULL ) ? 1 : 0;
1574 if (first_run) /* Parse and print */
1578 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
1580 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
1582 /* Header crunch()ed in previous run? -> ignore */
1583 if (p->str == NULL) continue;
1585 /* Does the current parser handle this header? */
1586 if ((strncmpic(p->str, v->str, v->len) == 0) || (v->len == CHECK_EVERY_HEADER_REMAINING))
1588 err = v->parser(csp, (char **)&(p->str));
1592 /* place any additional headers on the csp->headers list */
1593 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
1598 else /* Parse only */
1601 * The second run is only needed if the body was modified
1602 * and the content-lenght has changed.
1604 if (strncmpic(csp->http->cmd, "HEAD", 4))
1606 /*XXX: Code duplication */
1607 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
1609 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
1611 /* Header crunch()ed in previous run? -> ignore */
1612 if (p->str == NULL) continue;
1614 /* Does the current parser handle this header? */
1615 if (strncmpic(p->str, v->str, v->len) == 0)
1617 err = v->parser(csp, (char **)&(p->str));
1629 /*********************************************************************
1631 * Function : header_tagger
1633 * Description : Executes all text substitutions from applying
1634 * tag actions and saves the result as tag.
1636 * XXX: Shares enough code with filter_header() and
1637 * pcrs_filter_response() to warrant some helper functions.
1640 * 1 : csp = Current client state (buffers, headers, etc...)
1641 * 2 : header = Header that is used as tagger input
1643 * Returns : JB_ERR_OK on success and always succeeds
1645 *********************************************************************/
1646 static jb_err header_tagger(struct client_state *csp, char *header)
1648 int wanted_filter_type;
1649 int multi_action_index;
1653 struct file_list *fl;
1654 struct re_filterfile_spec *b;
1655 struct list_entry *tag_name;
1657 int found_filters = 0;
1658 const size_t header_length = strlen(header);
1660 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
1662 wanted_filter_type = FT_SERVER_HEADER_TAGGER;
1663 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
1667 wanted_filter_type = FT_CLIENT_HEADER_TAGGER;
1668 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
1671 /* Check if there are any filters */
1672 for (i = 0; i < MAX_AF_FILES; i++)
1685 if (0 == found_filters)
1687 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regex tagging.");
1691 for (i = 0; i < MAX_AF_FILES; i++)
1694 if ((NULL == fl) || (NULL == fl->f))
1697 * Either there are no filter files
1698 * left, or this filter file just
1699 * contains no valid filters.
1701 * Continue to be sure we don't miss
1702 * valid filter files that are chained
1703 * after empty or invalid ones.
1708 /* For all filters, */
1709 for (b = fl->f; b; b = b->next)
1711 if (b->type != wanted_filter_type)
1713 /* skip the ones we don't care about, */
1716 /* leaving only taggers that could apply, of which we use the ones, */
1717 for (tag_name = csp->action->multi[multi_action_index]->first;
1718 NULL != tag_name; tag_name = tag_name->next)
1720 /* that do apply, and */
1721 if (strcmp(b->name, tag_name->str) == 0)
1723 char *modified_tag = NULL;
1725 size_t size = header_length;
1726 pcrs_job *joblist = b->joblist;
1728 if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
1730 if (NULL == joblist)
1732 log_error(LOG_LEVEL_RE_FILTER,
1733 "Tagger %s has empty joblist. Nothing to do.", b->name);
1737 /* execute their pcrs_joblist on the header. */
1738 for (job = joblist; NULL != job; job = job->next)
1740 const int hits = pcrs_execute(job, tag, size, &modified_tag, &size);
1744 /* Success, continue with the modified version. */
1753 /* Tagger doesn't match */
1756 /* Regex failure, log it but continue anyway. */
1757 log_error(LOG_LEVEL_ERROR,
1758 "Problems with tagger \'%s\' and header \'%s\': %s",
1759 b->name, *header, pcrs_strerror(hits));
1761 freez(modified_tag);
1765 if (b->dynamic) pcrs_free_joblist(joblist);
1767 /* If this tagger matched */
1773 * There is to technical limitation which makes
1774 * it impossible to use empty tags, but I assume
1775 * no one would do it intentionally.
1778 log_error(LOG_LEVEL_INFO,
1779 "Tagger \'%s\' created an empty tag. Ignored.",
1784 if (!list_contains_item(csp->tags, tag))
1786 if (JB_ERR_OK != enlist(csp->tags, tag))
1788 log_error(LOG_LEVEL_ERROR,
1789 "Insufficient memory to add tag \'%s\', "
1790 "based on tagger \'%s\' and header \'%s\'",
1791 tag, b->name, *header);
1795 char *action_message;
1797 * update the action bits right away, to make
1798 * tagging based on tags set by earlier taggers
1799 * of the same kind possible.
1801 if (update_action_bits_for_tag(csp, tag))
1803 action_message = "Action bits updated accordingly.";
1807 action_message = "No action bits update necessary.";
1810 log_error(LOG_LEVEL_HEADER,
1811 "Tagger \'%s\' added tag \'%s\'. %s",
1812 b->name, tag, action_message);
1817 /* XXX: Is this log-worthy? */
1818 log_error(LOG_LEVEL_HEADER,
1819 "Tagger \'%s\' didn't add tag \'%s\'. "
1820 "Tag already present", b->name, tag);
1823 } /* if the tagger matched */
1824 } /* if the tagger applies */
1825 } /* for every tagger that could apply */
1826 } /* for all filters */
1827 } /* for all filter files */
1832 /* here begins the family of parser functions that reformat header lines */
1834 /*********************************************************************
1836 * Function : filter_header
1838 * Description : Executes all text substitutions from all applying
1839 * +(server|client)-header-filter actions on the header.
1840 * Most of the code was copied from pcrs_filter_response,
1841 * including the rather short variable names
1844 * 1 : csp = Current client state (buffers, headers, etc...)
1845 * 2 : header = On input, pointer to header to modify.
1846 * On output, pointer to the modified header, or NULL
1847 * to remove the header. This function frees the
1848 * original string if necessary.
1850 * Returns : JB_ERR_OK on success and always succeeds
1852 *********************************************************************/
1853 static jb_err filter_header(struct client_state *csp, char **header)
1857 size_t size = strlen(*header);
1859 char *newheader = NULL;
1862 struct file_list *fl;
1863 struct re_filterfile_spec *b;
1864 struct list_entry *filtername;
1866 int i, found_filters = 0;
1867 int wanted_filter_type;
1868 int multi_action_index;
1870 if (csp->flags & CSP_FLAG_NO_FILTERING)
1875 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
1877 wanted_filter_type = FT_SERVER_HEADER_FILTER;
1878 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
1882 wanted_filter_type = FT_CLIENT_HEADER_FILTER;
1883 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
1887 * Need to check the set of re_filterfiles...
1889 for (i = 0; i < MAX_AF_FILES; i++)
1902 if (0 == found_filters)
1904 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1908 for (i = 0; i < MAX_AF_FILES; i++)
1911 if ((NULL == fl) || (NULL == fl->f))
1914 * Either there are no filter files
1915 * left, or this filter file just
1916 * contains no valid filters.
1918 * Continue to be sure we don't miss
1919 * valid filter files that are chained
1920 * after empty or invalid ones.
1925 * For all applying +filter actions, look if a filter by that
1926 * name exists and if yes, execute its pcrs_joblist on the
1929 for (b = fl->f; b; b = b->next)
1931 if (b->type != wanted_filter_type)
1933 /* Skip other filter types */
1937 for (filtername = csp->action->multi[multi_action_index]->first;
1938 filtername ; filtername = filtername->next)
1940 if (strcmp(b->name, filtername->str) == 0)
1942 int current_hits = 0;
1943 pcrs_job *joblist = b->joblist;
1945 if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
1947 if (NULL == joblist)
1949 log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1953 log_error(LOG_LEVEL_RE_FILTER, "filtering \'%s\' (size %d) with \'%s\' ...",
1954 *header, size, b->name);
1956 /* Apply all jobs from the joblist */
1957 for (job = joblist; NULL != job; job = job->next)
1959 matches = pcrs_execute(job, *header, size, &newheader, &size);
1962 current_hits += matches;
1963 log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
1965 *header = newheader;
1967 else if ( 0 == matches )
1969 /* Filter doesn't change header */
1975 log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
1976 *header, b->name, pcrs_strerror(matches));
1977 if (newheader != NULL)
1979 log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
1985 if (b->dynamic) pcrs_free_joblist(joblist);
1987 log_error(LOG_LEVEL_RE_FILTER, "... produced %d hits (new size %d).", current_hits, size);
1988 hits += current_hits;
1995 * Additionally checking for hits is important because if
1996 * the continue hack is triggered, server headers can
1997 * arrive empty to separate multiple heads from each other.
1999 if ((0 == size) && hits)
2001 log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
2009 /*********************************************************************
2011 * Function : connection
2013 * Description : Makes sure that the value of the Connection: header
2014 * is "close" and signals connection_close_adder
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.
2024 * Returns : JB_ERR_OK on success, or
2025 * JB_ERR_MEMORY on out-of-memory error.
2027 *********************************************************************/
2028 static jb_err connection(struct client_state *csp, char **header)
2030 char *old_header = *header;
2032 /* Do we have a 'Connection: close' header? */
2033 if (strcmpic(*header, "Connection: close"))
2035 /* No, create one */
2036 *header = strdup("Connection: close");
2039 return JB_ERR_MEMORY;
2041 log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
2045 /* Signal connection_close_adder() to return early. */
2046 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
2048 csp->flags |= CSP_FLAG_SERVER_CONNECTION_CLOSE_SET;
2052 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET;
2059 /*********************************************************************
2061 * Function : crumble
2063 * Description : This is called if a header matches a pattern to "crunch"
2066 * 1 : csp = Current client state (buffers, headers, etc...)
2067 * 2 : header = On input, pointer to header to modify.
2068 * On output, pointer to the modified header, or NULL
2069 * to remove the header. This function frees the
2070 * original string if necessary.
2072 * Returns : JB_ERR_OK on success, or
2073 * JB_ERR_MEMORY on out-of-memory error.
2075 *********************************************************************/
2076 static jb_err crumble(struct client_state *csp, char **header)
2078 log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
2084 /*********************************************************************
2086 * Function : crunch_server_header
2088 * Description : Crunch server header if it matches a string supplied by the
2089 * user. Called from `sed'.
2092 * 1 : csp = Current client state (buffers, headers, etc...)
2093 * 2 : header = On input, pointer to header to modify.
2094 * On output, pointer to the modified header, or NULL
2095 * to remove the header. This function frees the
2096 * original string if necessary.
2098 * Returns : JB_ERR_OK on success and always succeeds
2100 *********************************************************************/
2101 static jb_err crunch_server_header(struct client_state *csp, char **header)
2103 const char *crunch_pattern;
2105 /* Do we feel like crunching? */
2106 if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
2108 crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
2110 /* Is the current header the lucky one? */
2111 if (strstr(*header, crunch_pattern))
2113 log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
2122 /*********************************************************************
2124 * Function : server_content_type
2126 * Description : Set the content-type for filterable types (text/.*,
2127 * .*xml.*, javascript and image/gif) unless filtering has been
2128 * forbidden (CT_TABOO) while parsing earlier headers.
2129 * NOTE: Since text/plain is commonly used by web servers
2130 * for files whose correct type is unknown, we don't
2131 * set CT_TEXT for it.
2134 * 1 : csp = Current client state (buffers, headers, etc...)
2135 * 2 : header = On input, pointer to header to modify.
2136 * On output, pointer to the modified header, or NULL
2137 * to remove the header. This function frees the
2138 * original string if necessary.
2140 * Returns : JB_ERR_OK on success, or
2141 * JB_ERR_MEMORY on out-of-memory error.
2143 *********************************************************************/
2144 static jb_err server_content_type(struct client_state *csp, char **header)
2146 /* Remove header if it isn't the first Content-Type header */
2147 if ((csp->content_type & CT_DECLARED))
2150 * Another, slightly slower, way to see if
2151 * we already parsed another Content-Type header.
2153 assert(NULL != get_header_value(csp->headers, "Content-Type:"));
2155 log_error(LOG_LEVEL_ERROR,
2156 "Multiple Content-Type headers. Removing and ignoring: \'%s\'",
2164 * Signal that the Content-Type has been set.
2166 csp->content_type |= CT_DECLARED;
2168 if (!(csp->content_type & CT_TABOO))
2170 if ((strstr(*header, " text/") && !strstr(*header, "plain"))
2171 || strstr(*header, "xml")
2172 || strstr(*header, "application/x-javascript"))
2174 csp->content_type |= CT_TEXT;
2176 else if (strstr(*header, " image/gif"))
2178 csp->content_type |= CT_GIF;
2180 else if (strstr(*header, " image/jpeg"))
2182 csp->content_type |= CT_JPEG;
2187 * Are we messing with the content type?
2189 if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
2192 * Make sure the user doesn't accidently
2193 * change the content type of binary documents.
2195 if ((csp->content_type & CT_TEXT) || (csp->action->flags & ACTION_FORCE_TEXT_MODE))
2198 *header = strdup("Content-Type: ");
2199 string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
2203 log_error(LOG_LEVEL_HEADER, "Insufficient memory to replace Content-Type!");
2204 return JB_ERR_MEMORY;
2206 log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
2210 log_error(LOG_LEVEL_HEADER, "%s not replaced. It doesn't look like text. "
2211 "Enable force-text-mode if you know what you're doing.", *header);
2219 /*********************************************************************
2221 * Function : server_transfer_coding
2223 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
2224 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
2225 * - Remove header if body was chunked but has been
2226 * de-chunked for filtering.
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.
2235 * Returns : JB_ERR_OK on success, or
2236 * JB_ERR_MEMORY on out-of-memory error.
2238 *********************************************************************/
2239 static jb_err server_transfer_coding(struct client_state *csp, char **header)
2242 * Turn off pcrs and gif filtering if body compressed
2244 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2248 * XXX: Added to test if we could use CT_GZIP and CT_DEFLATE here.
2250 log_error(LOG_LEVEL_INFO, "Marking content type for %s as CT_TABOO because of %s.",
2251 csp->http->cmd, *header);
2252 #endif /* def FEATURE_ZLIB */
2253 csp->content_type = CT_TABOO;
2257 * Raise flag if body chunked
2259 if (strstr(*header, "chunked"))
2261 csp->flags |= CSP_FLAG_CHUNKED;
2264 * If the body was modified, it has been de-chunked first
2265 * and the header must be removed.
2267 * FIXME: If there is more than one transfer encoding,
2268 * only the "chunked" part should be removed here.
2270 if (csp->flags & CSP_FLAG_MODIFIED)
2272 log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
2281 /*********************************************************************
2283 * Function : server_content_encoding
2285 * Description : This function is run twice for each request,
2286 * unless FEATURE_ZLIB and filtering are disabled.
2288 * The first run is used to check if the content
2289 * is compressed, if FEATURE_ZLIB is disabled
2290 * filtering is then disabled as well, if FEATURE_ZLIB
2291 * is enabled the content is marked for decompression.
2293 * The second run is used to remove the Content-Encoding
2294 * header if the decompression was successful.
2297 * 1 : csp = Current client state (buffers, headers, etc...)
2298 * 2 : header = On input, pointer to header to modify.
2299 * On output, pointer to the modified header, or NULL
2300 * to remove the header. This function frees the
2301 * original string if necessary.
2303 * Returns : JB_ERR_OK on success, or
2304 * JB_ERR_MEMORY on out-of-memory error.
2306 *********************************************************************/
2307 static jb_err server_content_encoding(struct client_state *csp, char **header)
2310 if ((csp->flags & CSP_FLAG_MODIFIED)
2311 && (csp->content_type & (CT_GZIP | CT_DEFLATE)))
2314 * We successfully decompressed the content,
2315 * and have to clean the header now, so the
2316 * client no longer expects compressed data..
2318 * XXX: There is a difference between cleaning
2319 * and removing it completely.
2321 log_error(LOG_LEVEL_HEADER, "Crunching: %s", *header);
2324 else if (strstr(*header, "gzip"))
2326 /* Mark for gzip decompression */
2327 csp->content_type |= CT_GZIP;
2329 else if (strstr(*header, "deflate"))
2331 /* Mark for zlib decompression */
2332 csp->content_type |= CT_DEFLATE;
2334 else if (strstr(*header, "compress"))
2337 * We can't decompress this; therefore we can't filter
2340 csp->content_type |= CT_TABOO;
2342 #else /* !defined(FEATURE_ZLIB) */
2343 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2346 * Body is compressed, turn off pcrs and gif filtering.
2348 csp->content_type |= CT_TABOO;
2351 * Log a warning if the user expects the content to be filtered.
2353 if ((csp->rlist != NULL) &&
2354 (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
2356 log_error(LOG_LEVEL_INFO,
2357 "Compressed content detected, content filtering disabled. "
2358 "Consider recompiling Privoxy with zlib support or "
2359 "enable the prevent-compression action.");
2362 #endif /* defined(FEATURE_ZLIB) */
2369 /*********************************************************************
2371 * Function : server_content_length
2373 * Description : Adjust Content-Length header if we modified
2377 * 1 : csp = Current client state (buffers, headers, etc...)
2378 * 2 : header = On input, pointer to header to modify.
2379 * On output, pointer to the modified header, or NULL
2380 * to remove the header. This function frees the
2381 * original string if necessary.
2383 * Returns : JB_ERR_OK on success, or
2384 * JB_ERR_MEMORY on out-of-memory error.
2386 *********************************************************************/
2387 static jb_err server_content_length(struct client_state *csp, char **header)
2389 const size_t max_header_length = 80;
2391 /* Regenerate header if the content was modified. */
2392 if (csp->flags & CSP_FLAG_MODIFIED)
2395 *header = (char *) zalloc(max_header_length);
2396 if (*header == NULL)
2398 return JB_ERR_MEMORY;
2401 snprintf(*header, max_header_length, "Content-Length: %d",
2402 (int)csp->content_length);
2403 log_error(LOG_LEVEL_HEADER, "Adjusted Content-Length to %d",
2404 (int)csp->content_length);
2411 /*********************************************************************
2413 * Function : server_content_md5
2415 * Description : Crumble any Content-MD5 headers if the document was
2416 * modified. FIXME: Should we re-compute instead?
2419 * 1 : csp = Current client state (buffers, headers, etc...)
2420 * 2 : header = On input, pointer to header to modify.
2421 * On output, pointer to the modified header, or NULL
2422 * to remove the header. This function frees the
2423 * original string if necessary.
2425 * Returns : JB_ERR_OK on success, or
2426 * JB_ERR_MEMORY on out-of-memory error.
2428 *********************************************************************/
2429 static jb_err server_content_md5(struct client_state *csp, char **header)
2431 if (csp->flags & CSP_FLAG_MODIFIED)
2433 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
2441 /*********************************************************************
2443 * Function : server_content_disposition
2445 * Description : If enabled, blocks or modifies the "content-disposition" header.
2446 * Called from `sed'.
2449 * 1 : csp = Current client state (buffers, headers, etc...)
2450 * 2 : header = On input, pointer to header to modify.
2451 * On output, pointer to the modified header, or NULL
2452 * to remove the header. This function frees the
2453 * original string if necessary.
2455 * Returns : JB_ERR_OK on success, or
2456 * JB_ERR_MEMORY on out-of-memory error.
2458 *********************************************************************/
2459 static jb_err server_content_disposition(struct client_state *csp, char **header)
2464 * Are we messing with the content-disposition header?
2466 if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
2472 newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
2474 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2477 * Blocking content-disposition header
2479 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2486 * Replacing content-disposition header
2489 *header = strdup("content-disposition: ");
2490 string_append(header, newval);
2492 if (*header == NULL)
2494 log_error(LOG_LEVEL_HEADER, "Insufficent memory. content-disposition header not fully replaced.");
2498 log_error(LOG_LEVEL_HEADER, "content-disposition header crunched and replaced with: %s", *header);
2501 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2505 /*********************************************************************
2507 * Function : server_last_modified
2509 * Description : Changes Last-Modified header to the actual date
2510 * to help hide-if-modified-since.
2511 * Called from `sed'.
2514 * 1 : csp = Current client state (buffers, headers, etc...)
2515 * 2 : header = On input, pointer to header to modify.
2516 * On output, pointer to the modified header, or NULL
2517 * to remove the header. This function frees the
2518 * original string if necessary.
2520 * Returns : JB_ERR_OK on success, or
2521 * JB_ERR_MEMORY on out-of-memory error.
2523 *********************************************************************/
2524 static jb_err server_last_modified(struct client_state *csp, char **header)
2527 char buf[BUFFER_SIZE];
2530 #ifdef HAVE_GMTIME_R
2533 struct tm *timeptr = NULL;
2534 time_t now, last_modified;
2536 long int days, hours, minutes, seconds;
2539 * Are we messing with the Last-Modified header?
2541 if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
2547 newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
2549 if (0 == strcmpic(newval, "block") )
2552 * Blocking Last-Modified header. Useless but why not.
2554 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2558 else if (0 == strcmpic(newval, "reset-to-request-time"))
2561 * Setting Last-Modified Header to now.
2563 get_http_time(0, buf);
2565 *header = strdup("Last-Modified: ");
2566 string_append(header, buf);
2568 if (*header == NULL)
2570 log_error(LOG_LEVEL_HEADER, "Insufficent memory. Last-Modified header got lost, boohoo.");
2574 log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
2577 else if (0 == strcmpic(newval, "randomize"))
2579 const char *header_time = *header + sizeof("Last-Modified:");
2581 log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
2583 #ifdef HAVE_GMTIME_R
2584 timeptr = gmtime_r(&now, &gmt);
2585 #elif FEATURE_PTHREAD
2586 pthread_mutex_lock(&gmtime_mutex);
2587 timeptr = gmtime(&now);
2588 pthread_mutex_unlock(&gmtime_mutex);
2590 timeptr = gmtime(&now);
2592 if (JB_ERR_OK != parse_header_time(header_time, &last_modified))
2594 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
2599 rtime = (long int)difftime(now, last_modified);
2608 log_error(LOG_LEVEL_HEADER, "Server time in the future.");
2610 rtime = pick_from_range(rtime);
2611 if (negative) rtime *= -1;
2612 last_modified += rtime;
2613 #ifdef HAVE_GMTIME_R
2614 timeptr = gmtime_r(&last_modified, &gmt);
2615 #elif FEATURE_PTHREAD
2616 pthread_mutex_lock(&gmtime_mutex);
2617 timeptr = gmtime(&last_modified);
2618 pthread_mutex_unlock(&gmtime_mutex);
2620 timeptr = gmtime(&last_modified);
2622 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2624 *header = strdup("Last-Modified: ");
2625 string_append(header, newheader);
2627 if (*header == NULL)
2629 log_error(LOG_LEVEL_ERROR, "Insufficent memory, header crunched without replacement.");
2630 return JB_ERR_MEMORY;
2633 if (LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2635 days = rtime / (3600 * 24);
2636 hours = rtime / 3600 % 24;
2637 minutes = rtime / 60 % 60;
2638 seconds = rtime % 60;
2640 log_error(LOG_LEVEL_HEADER, "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
2641 *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
2642 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2647 log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
2656 /*********************************************************************
2658 * Function : client_accept_encoding
2660 * Description : Rewrite the client's Accept-Encoding header so that
2661 * if doesn't allow compression, if the action applies.
2662 * Note: For HTTP/1.0 the absence of the header is enough.
2665 * 1 : csp = Current client state (buffers, headers, etc...)
2666 * 2 : header = On input, pointer to header to modify.
2667 * On output, pointer to the modified header, or NULL
2668 * to remove the header. This function frees the
2669 * original string if necessary.
2671 * Returns : JB_ERR_OK on success, or
2672 * JB_ERR_MEMORY on out-of-memory error.
2674 *********************************************************************/
2675 static jb_err client_accept_encoding(struct client_state *csp, char **header)
2677 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2679 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
2683 /* Temporarily disable the correct behaviour to
2684 * work around a PHP bug.
2686 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
2688 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
2689 * if (*header == NULL)
2691 * return JB_ERR_MEMORY;
2702 /*********************************************************************
2704 * Function : client_te
2706 * Description : Rewrite the client's TE header so that
2707 * if doesn't allow compression, if the action applies.
2710 * 1 : csp = Current client state (buffers, headers, etc...)
2711 * 2 : header = On input, pointer to header to modify.
2712 * On output, pointer to the modified header, or NULL
2713 * to remove the header. This function frees the
2714 * original string if necessary.
2716 * Returns : JB_ERR_OK on success, or
2717 * JB_ERR_MEMORY on out-of-memory error.
2719 *********************************************************************/
2720 static jb_err client_te(struct client_state *csp, char **header)
2722 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2725 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
2732 /*********************************************************************
2734 * Function : client_referrer
2736 * Description : Handle the "referer" config setting properly.
2737 * Called from `sed'.
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.
2746 * Returns : JB_ERR_OK on success, or
2747 * JB_ERR_MEMORY on out-of-memory error.
2749 *********************************************************************/
2750 static jb_err client_referrer(struct client_state *csp, char **header)
2752 const char *parameter;
2753 /* booleans for parameters we have to check multiple times */
2754 int parameter_conditional_block;
2755 int parameter_conditional_forge;
2757 #ifdef FEATURE_FORCE_LOAD
2759 * Since the referrer can include the prefix even
2760 * if the request itself is non-forced, we must
2761 * clean it unconditionally.
2763 * XXX: strclean is too broad
2765 strclean(*header, FORCE_PREFIX);
2766 #endif /* def FEATURE_FORCE_LOAD */
2768 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
2770 /* Nothing left to do */
2774 parameter = csp->action->string[ACTION_STRING_REFERER];
2775 assert(parameter != NULL);
2776 parameter_conditional_block = (0 == strcmpic(parameter, "conditional-block"));
2777 parameter_conditional_forge = (0 == strcmpic(parameter, "conditional-forge"));
2779 if (!parameter_conditional_block && !parameter_conditional_forge)
2782 * As conditional-block and conditional-forge are the only
2783 * parameters that rely on the original referrer, we can
2784 * remove it now for all the others.
2789 if (0 == strcmpic(parameter, "block"))
2791 log_error(LOG_LEVEL_HEADER, "Referer crunched!");
2794 else if (parameter_conditional_block || parameter_conditional_forge)
2796 return handle_conditional_hide_referrer_parameter(header,
2797 csp->http->hostport, parameter_conditional_block);
2799 else if (0 == strcmpic(parameter, "forge"))
2801 return create_forged_referrer(header, csp->http->hostport);
2805 /* interpret parameter as user-supplied referer to fake */
2806 return create_fake_referrer(header, parameter);
2811 /*********************************************************************
2813 * Function : client_accept_language
2815 * Description : Handle the "Accept-Language" config setting properly.
2816 * Called from `sed'.
2819 * 1 : csp = Current client state (buffers, headers, etc...)
2820 * 2 : header = On input, pointer to header to modify.
2821 * On output, pointer to the modified header, or NULL
2822 * to remove the header. This function frees the
2823 * original string if necessary.
2825 * Returns : JB_ERR_OK on success, or
2826 * JB_ERR_MEMORY on out-of-memory error.
2828 *********************************************************************/
2829 static jb_err client_accept_language(struct client_state *csp, char **header)
2834 * Are we messing with the Accept-Language?
2836 if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
2838 /*I don't think so*/
2842 newval = csp->action->string[ACTION_STRING_LANGUAGE];
2844 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2847 * Blocking Accept-Language header
2849 log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
2856 * Replacing Accept-Language header
2859 *header = strdup("Accept-Language: ");
2860 string_append(header, newval);
2862 if (*header == NULL)
2864 log_error(LOG_LEVEL_ERROR,
2865 "Insufficent memory. Accept-Language header crunched without replacement.");
2869 log_error(LOG_LEVEL_HEADER,
2870 "Accept-Language header crunched and replaced with: %s", *header);
2873 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2877 /*********************************************************************
2879 * Function : crunch_client_header
2881 * Description : Crunch client header if it matches a string supplied by the
2882 * user. Called from `sed'.
2885 * 1 : csp = Current client state (buffers, headers, etc...)
2886 * 2 : header = On input, pointer to header to modify.
2887 * On output, pointer to the modified header, or NULL
2888 * to remove the header. This function frees the
2889 * original string if necessary.
2891 * Returns : JB_ERR_OK on success and always succeeds
2893 *********************************************************************/
2894 static jb_err crunch_client_header(struct client_state *csp, char **header)
2896 const char *crunch_pattern;
2898 /* Do we feel like crunching? */
2899 if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
2901 crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
2903 /* Is the current header the lucky one? */
2904 if (strstr(*header, crunch_pattern))
2906 log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
2914 /*********************************************************************
2916 * Function : client_uagent
2918 * Description : Handle the "user-agent" config setting properly
2919 * and remember its original value to enable browser
2920 * bug workarounds. Called from `sed'.
2923 * 1 : csp = Current client state (buffers, headers, etc...)
2924 * 2 : header = On input, pointer to header to modify.
2925 * On output, pointer to the modified header, or NULL
2926 * to remove the header. This function frees the
2927 * original string if necessary.
2929 * Returns : JB_ERR_OK on success, or
2930 * JB_ERR_MEMORY on out-of-memory error.
2932 *********************************************************************/
2933 static jb_err client_uagent(struct client_state *csp, char **header)
2937 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
2942 newval = csp->action->string[ACTION_STRING_USER_AGENT];
2949 *header = strdup("User-Agent: ");
2950 string_append(header, newval);
2952 log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
2954 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2958 /*********************************************************************
2960 * Function : client_ua
2962 * Description : Handle "ua-" headers properly. Called from `sed'.
2965 * 1 : csp = Current client state (buffers, headers, etc...)
2966 * 2 : header = On input, pointer to header to modify.
2967 * On output, pointer to the modified header, or NULL
2968 * to remove the header. This function frees the
2969 * original string if necessary.
2971 * Returns : JB_ERR_OK on success, or
2972 * JB_ERR_MEMORY on out-of-memory error.
2974 *********************************************************************/
2975 static jb_err client_ua(struct client_state *csp, char **header)
2977 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
2979 log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
2987 /*********************************************************************
2989 * Function : client_from
2991 * Description : Handle the "from" config setting properly.
2992 * Called from `sed'.
2995 * 1 : csp = Current client state (buffers, headers, etc...)
2996 * 2 : header = On input, pointer to header to modify.
2997 * On output, pointer to the modified header, or NULL
2998 * to remove the header. This function frees the
2999 * original string if necessary.
3001 * Returns : JB_ERR_OK on success, or
3002 * JB_ERR_MEMORY on out-of-memory error.
3004 *********************************************************************/
3005 static jb_err client_from(struct client_state *csp, char **header)
3009 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
3016 newval = csp->action->string[ACTION_STRING_FROM];
3019 * Are we blocking the e-mail address?
3021 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
3023 log_error(LOG_LEVEL_HEADER, "crunched From!");
3027 log_error(LOG_LEVEL_HEADER, " modified");
3029 *header = strdup("From: ");
3030 string_append(header, newval);
3032 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3036 /*********************************************************************
3038 * Function : client_send_cookie
3040 * Description : Crunches the "cookie" header if necessary.
3041 * Called from `sed'.
3043 * XXX: Stupid name, doesn't send squat.
3046 * 1 : csp = Current client state (buffers, headers, etc...)
3047 * 2 : header = On input, pointer to header to modify.
3048 * On output, pointer to the modified header, or NULL
3049 * to remove the header. This function frees the
3050 * original string if necessary.
3052 * Returns : JB_ERR_OK on success, or
3053 * JB_ERR_MEMORY on out-of-memory error.
3055 *********************************************************************/
3056 static jb_err client_send_cookie(struct client_state *csp, char **header)
3058 if (csp->action->flags & ACTION_NO_COOKIE_READ)
3060 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie: %s", *header);
3068 /*********************************************************************
3070 * Function : client_x_forwarded
3072 * Description : Handle the "x-forwarded-for" config setting properly,
3073 * also used in the add_client_headers list. Called from `sed'.
3076 * 1 : csp = Current client state (buffers, headers, etc...)
3077 * 2 : header = On input, pointer to header to modify.
3078 * On output, pointer to the modified header, or NULL
3079 * to remove the header. This function frees the
3080 * original string if necessary.
3082 * Returns : JB_ERR_OK on success, or
3083 * JB_ERR_MEMORY on out-of-memory error.
3085 *********************************************************************/
3086 jb_err client_x_forwarded(struct client_state *csp, char **header)
3088 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
3090 /* Save it so we can re-add it later */
3091 freez(csp->x_forwarded);
3092 csp->x_forwarded = *header;
3095 * Always set *header = NULL, since this information
3096 * will be sent at the end of the header.
3103 log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
3110 /*********************************************************************
3112 * Function : client_max_forwards
3114 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
3115 * from the value of the Max-Forwards header field.
3118 * 1 : csp = Current client state (buffers, headers, etc...)
3119 * 2 : header = On input, pointer to header to modify.
3120 * On output, pointer to the modified header, or NULL
3121 * to remove the header. This function frees the
3122 * original string if necessary.
3124 * Returns : JB_ERR_OK on success, or
3125 * JB_ERR_MEMORY on out-of-memory error.
3127 *********************************************************************/
3128 static jb_err client_max_forwards(struct client_state *csp, char **header)
3132 if ((0 == strcmpic(csp->http->gpc, "trace")) ||
3133 (0 == strcmpic(csp->http->gpc, "options")))
3135 assert(*(*header+12) == ':');
3136 if (1 == sscanf(*header+12, ": %u", &max_forwards))
3138 if (max_forwards > 0)
3140 snprintf(*header, strlen(*header)+1, "Max-Forwards: %u", --max_forwards);
3141 log_error(LOG_LEVEL_HEADER, "Max-Forwards value for %s request reduced to %u.",
3142 csp->http->gpc, max_forwards);
3144 else if (max_forwards < 0)
3146 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3152 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3161 /*********************************************************************
3163 * Function : client_host
3165 * Description : If the request URI did not contain host and
3166 * port information, parse and evaluate the Host
3169 * Also, kill ill-formed HOST: headers as sent by
3170 * Apple's iTunes software when used with a proxy.
3173 * 1 : csp = Current client state (buffers, headers, etc...)
3174 * 2 : header = On input, pointer to header to modify.
3175 * On output, pointer to the modified header, or NULL
3176 * to remove the header. This function frees the
3177 * original string if necessary.
3179 * Returns : JB_ERR_OK on success, or
3180 * JB_ERR_MEMORY on out-of-memory error.
3182 *********************************************************************/
3183 static jb_err client_host(struct client_state *csp, char **header)
3188 * If the header field name is all upper-case, chances are that it's
3189 * an ill-formed one from iTunes. BTW, killing innocent headers here is
3190 * not a problem -- they are regenerated later.
3192 if ((*header)[1] == 'O')
3194 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
3199 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
3200 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
3203 if (NULL == (p = strdup((*header)+6)))
3205 return JB_ERR_MEMORY;
3208 if (NULL == (q = strdup(p)))
3211 return JB_ERR_MEMORY;
3214 freez(csp->http->hostport);
3215 csp->http->hostport = p;
3216 freez(csp->http->host);
3217 csp->http->host = q;
3218 q = strchr(csp->http->host, ':');
3221 /* Terminate hostname and evaluate port string */
3223 csp->http->port = atoi(q);
3227 csp->http->port = csp->http->ssl ? 443 : 80;
3230 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
3231 csp->http->hostport, csp->http->host, csp->http->port);
3234 /* Signal client_host_adder() to return right away */
3235 csp->flags |= CSP_FLAG_HOST_HEADER_IS_SET;
3241 /*********************************************************************
3243 * Function : client_if_modified_since
3245 * Description : Remove or modify the If-Modified-Since header.
3248 * 1 : csp = Current client state (buffers, headers, etc...)
3249 * 2 : header = On input, pointer to header to modify.
3250 * On output, pointer to the modified header, or NULL
3251 * to remove the header. This function frees the
3252 * original string if necessary.
3254 * Returns : JB_ERR_OK on success, or
3255 * JB_ERR_MEMORY on out-of-memory error.
3257 *********************************************************************/
3258 static jb_err client_if_modified_since(struct client_state *csp, char **header)
3261 #ifdef HAVE_GMTIME_R
3264 struct tm *timeptr = NULL;
3268 long int hours, minutes, seconds;
3272 if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
3275 * The client got an error message because of a temporary problem,
3276 * the problem is gone and the client now tries to revalidate our
3277 * error message on the real server. The revalidation would always
3278 * end with the transmission of the whole document and there is
3279 * no need to expose the bogus If-Modified-Since header.
3281 log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
3284 else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
3286 newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
3288 if ((0 == strcmpic(newval, "block")))
3290 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3293 else /* add random value */
3295 const char *header_time = *header + sizeof("If-Modified-Since:");
3297 if (JB_ERR_OK != parse_header_time(header_time, &tm))
3299 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
3304 rtime = strtol(newval, &endptr, 0);
3307 log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
3308 *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
3315 rtime = pick_from_range(rtime);
3319 log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
3322 tm += rtime * (negative ? -1 : 1);
3323 #ifdef HAVE_GMTIME_R
3324 timeptr = gmtime_r(&tm, &gmt);
3325 #elif FEATURE_PTHREAD
3326 pthread_mutex_lock(&gmtime_mutex);
3327 timeptr = gmtime(&tm);
3328 pthread_mutex_unlock(&gmtime_mutex);
3330 timeptr = gmtime(&tm);
3332 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
3335 *header = strdup("If-Modified-Since: ");
3336 string_append(header, newheader);
3338 if (*header == NULL)
3340 log_error(LOG_LEVEL_HEADER, "Insufficent memory, header crunched without replacement.");
3341 return JB_ERR_MEMORY;
3344 if (LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
3346 hours = rtime / 3600;
3347 minutes = rtime / 60 % 60;
3348 seconds = rtime % 60;
3350 log_error(LOG_LEVEL_HEADER, "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
3351 *header, (negative) ? "subtracted" : "added", hours, (hours == 1) ? "r" : "rs",
3352 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
3362 /*********************************************************************
3364 * Function : client_if_none_match
3366 * Description : Remove the If-None-Match header.
3369 * 1 : csp = Current client state (buffers, headers, etc...)
3370 * 2 : header = On input, pointer to header to modify.
3371 * On output, pointer to the modified header, or NULL
3372 * to remove the header. This function frees the
3373 * original string if necessary.
3375 * Returns : JB_ERR_OK on success, or
3376 * JB_ERR_MEMORY on out-of-memory error.
3378 *********************************************************************/
3379 static jb_err client_if_none_match(struct client_state *csp, char **header)
3381 if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
3383 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3391 /*********************************************************************
3393 * Function : client_x_filter
3395 * Description : Disables filtering if the client set "X-Filter: No".
3396 * Called from `sed'.
3399 * 1 : csp = Current client state (buffers, headers, etc...)
3400 * 2 : header = On input, pointer to header to modify.
3401 * On output, pointer to the modified header, or NULL
3402 * to remove the header. This function frees the
3403 * original string if necessary.
3405 * Returns : JB_ERR_OK on success
3407 *********************************************************************/
3408 jb_err client_x_filter(struct client_state *csp, char **header)
3410 if ( 0 == strcmpic(*header, "X-Filter: No"))
3412 if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
3414 log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
3418 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
3420 log_error(LOG_LEVEL_HEADER,
3421 "force-text-mode overruled the client's request to fetch without filtering!");
3425 csp->content_type = CT_TABOO; /* XXX: This hack shouldn't be necessary */
3426 csp->flags |= CSP_FLAG_NO_FILTERING;
3427 log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
3429 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3437 /*********************************************************************
3439 * Function : client_range
3441 * Description : Removes Range and If-Range headers if content
3442 * filtering is enabled. If the client's version of
3443 * the document has been altered by Privoxy, the server
3444 * could interpret the range differently than the client
3445 * intended in which case the user could end up with
3446 * corrupted content.
3449 * 1 : csp = Current client state (buffers, headers, etc...)
3450 * 2 : header = On input, pointer to header to modify.
3451 * On output, pointer to the modified header, or NULL
3452 * to remove the header. This function frees the
3453 * original string if necessary.
3455 * Returns : JB_ERR_OK
3457 *********************************************************************/
3458 static jb_err client_range(struct client_state *csp, char **header)
3460 if (content_filters_enabled(csp))
3462 log_error(LOG_LEVEL_HEADER, "Content filtering is enabled."
3463 " Crunching: \'%s\' to prevent range-mismatch problems.", *header);
3470 /* the following functions add headers directly to the header list */
3472 /*********************************************************************
3474 * Function : client_host_adder
3476 * Description : Adds the Host: header field if it is missing.
3477 * Called from `sed'.
3480 * 1 : csp = Current client state (buffers, headers, etc...)
3482 * Returns : JB_ERR_OK on success, or
3483 * JB_ERR_MEMORY on out-of-memory error.
3485 *********************************************************************/
3486 static jb_err client_host_adder(struct client_state *csp)
3491 if (csp->flags & CSP_FLAG_HOST_HEADER_IS_SET)
3493 /* Header already set by the client, nothing to do. */
3497 if ( !csp->http->hostport || !*(csp->http->hostport))
3499 /* XXX: When does this happen and why is it OK? */
3500 log_error(LOG_LEVEL_INFO, "Weirdness in client_host_adder detected and ignored.");
3505 * remove 'user:pass@' from 'proto://user:pass@host'
3507 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
3513 p = csp->http->hostport;
3516 /* XXX: Just add it, we already made sure that it will be unique */
3517 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
3518 err = enlist_unique_header(csp->headers, "Host", p);
3524 /*********************************************************************
3526 * Function : client_cookie_adder
3528 * Description : Used in the add_client_headers list to add "wafers".
3529 * Called from `sed'.
3532 * 1 : csp = Current client state (buffers, headers, etc...)
3534 * Returns : JB_ERR_OK on success, or
3535 * JB_ERR_MEMORY on out-of-memory error.
3537 *********************************************************************/
3538 jb_err client_cookie_adder(struct client_state *csp)
3541 struct list_entry *wafer;
3542 struct list_entry *wafer_list;
3546 * If the user has not supplied any wafers, and the user has not
3547 * told us to suppress the vanilla wafer, then send the vanilla wafer.
3549 if ((0 != (csp->action->flags & ACTION_VANILLA_WAFER))
3550 && list_is_empty(csp->action->multi[ACTION_MULTI_WAFER]))
3552 enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
3555 wafer_list = csp->action->multi[ACTION_MULTI_WAFER]->first;
3557 if (NULL == wafer_list)
3563 tmp = strdup("Cookie: ");
3565 for (wafer = wafer_list; (NULL != tmp) && (NULL != wafer); wafer = wafer->next)
3567 if (wafer != wafer_list)
3569 /* As this isn't the first wafer, we need a delimiter. */
3570 string_append(&tmp, "; ");
3572 string_join(&tmp, cookie_encode(wafer->str));
3577 return JB_ERR_MEMORY;
3580 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
3581 err = enlist(csp->headers, tmp);
3588 /*********************************************************************
3590 * Function : client_accept_encoding_adder
3592 * Description : Add an Accept-Encoding header to the client's request
3593 * that disables compression if the action applies, and
3594 * the header is not already there. Called from `sed'.
3595 * Note: For HTTP/1.0, the absence of the header is enough.
3598 * 1 : csp = Current client state (buffers, headers, etc...)
3600 * Returns : JB_ERR_OK on success, or
3601 * JB_ERR_MEMORY on out-of-memory error.
3603 *********************************************************************/
3604 static jb_err client_accept_encoding_adder(struct client_state *csp)
3606 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
3607 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
3609 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
3617 /*********************************************************************
3619 * Function : client_xtra_adder
3621 * Description : Used in the add_client_headers list. Called from `sed'.
3624 * 1 : csp = Current client state (buffers, headers, etc...)
3626 * Returns : JB_ERR_OK on success, or
3627 * JB_ERR_MEMORY on out-of-memory error.
3629 *********************************************************************/
3630 static jb_err client_xtra_adder(struct client_state *csp)
3632 struct list_entry *lst;
3635 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
3636 lst ; lst = lst->next)
3638 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
3639 err = enlist(csp->headers, lst->str);
3651 /*********************************************************************
3653 * Function : client_x_forwarded_adder
3655 * Description : Used in the add_client_headers list. Called from `sed'.
3658 * 1 : csp = Current client state (buffers, headers, etc...)
3660 * Returns : JB_ERR_OK on success, or
3661 * JB_ERR_MEMORY on out-of-memory error.
3663 *********************************************************************/
3664 static jb_err client_x_forwarded_adder(struct client_state *csp)
3669 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
3674 if (csp->x_forwarded)
3676 p = strdup(csp->x_forwarded);
3677 string_append(&p, ", ");
3681 p = strdup("X-Forwarded-For: ");
3683 string_append(&p, csp->ip_addr_str);
3687 return JB_ERR_MEMORY;
3690 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
3691 err = enlist(csp->headers, p);
3698 /*********************************************************************
3700 * Function : connection_close_adder
3702 * Description : "Temporary" fix for the needed but missing HTTP/1.1
3703 * support. Adds a "Connection: close" header to csp->headers
3704 * unless the header was already present. Called from `sed'.
3706 * FIXME: This whole function shouldn't be neccessary!
3709 * 1 : csp = Current client state (buffers, headers, etc...)
3711 * Returns : JB_ERR_OK on success, or
3712 * JB_ERR_MEMORY on out-of-memory error.
3714 *********************************************************************/
3715 static jb_err connection_close_adder(struct client_state *csp)
3717 const unsigned int flags = csp->flags;
3720 * Return right away if
3722 * - we're parsing server headers and the server header
3723 * "Connection: close" is already set, or if
3725 * - we're parsing client headers and the client header
3726 * "Connection: close" is already set.
3728 if ((flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE
3729 && flags & CSP_FLAG_SERVER_CONNECTION_CLOSE_SET)
3730 ||(!(flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
3731 && flags & CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET))
3736 log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
3738 return enlist(csp->headers, "Connection: close");
3742 /*********************************************************************
3744 * Function : server_http
3746 * Description : - Save the HTTP Status into csp->http->status
3747 * - Set CT_TABOO to prevent filtering if the answer
3748 * is a partial range (HTTP status 206)
3749 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
3753 * 1 : csp = Current client state (buffers, headers, etc...)
3754 * 2 : header = On input, pointer to header to modify.
3755 * On output, pointer to the modified header, or NULL
3756 * to remove the header. This function frees the
3757 * original string if necessary.
3759 * Returns : JB_ERR_OK on success, or
3760 * JB_ERR_MEMORY on out-of-memory error.
3762 *********************************************************************/
3763 static jb_err server_http(struct client_state *csp, char **header)
3765 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
3766 if (csp->http->status == 206)
3768 csp->content_type = CT_TABOO;
3771 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
3773 /* XXX: Should we do a real validity check here? */
3774 if (strlen(*header) > 8)
3777 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
3782 * XXX: Should we block the request or
3783 * enlist a valid status code line here?
3785 log_error(LOG_LEVEL_INFO, "Malformed server response detected. "
3786 "Downgrading to HTTP/1.0 impossible.");
3794 /*********************************************************************
3796 * Function : server_set_cookie
3798 * Description : Handle the server "cookie" header properly.
3799 * Log cookie to the jar file. Then "crunch",
3800 * accept or rewrite it to a session cookie.
3801 * Called from `sed'.
3803 * TODO: Allow the user to specify a new expiration
3804 * time to cause the cookie to expire even before the
3805 * browser is closed.
3808 * 1 : csp = Current client state (buffers, headers, etc...)
3809 * 2 : header = On input, pointer to header to modify.
3810 * On output, pointer to the modified header, or NULL
3811 * to remove the header. This function frees the
3812 * original string if necessary.
3814 * Returns : JB_ERR_OK on success, or
3815 * JB_ERR_MEMORY on out-of-memory error.
3817 *********************************************************************/
3818 static jb_err server_set_cookie(struct client_state *csp, char **header)
3825 #ifdef FEATURE_COOKIE_JAR
3826 if (csp->config->jar)
3829 * Write timestamp into outbuf.
3831 * Complex because not all OSs have tm_gmtoff or
3832 * the %z field in strftime()
3834 char tempbuf[ BUFFER_SIZE ];
3836 #ifdef HAVE_LOCALTIME_R
3837 tm_now = *localtime_r(&now, &tm_now);
3838 #elif FEATURE_PTHREAD
3839 pthread_mutex_lock(&localtime_mutex);
3840 tm_now = *localtime (&now);
3841 pthread_mutex_unlock(&localtime_mutex);
3843 tm_now = *localtime (&now);
3845 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
3847 /* strlen("set-cookie: ") = 12 */
3848 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
3850 #endif /* def FEATURE_COOKIE_JAR */
3852 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
3854 log_error(LOG_LEVEL_HEADER, "Crunching incoming cookie: %s", *header);
3857 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
3859 /* Flag whether or not to log a message */
3862 /* A variable to store the tag we're working on */
3865 /* Skip "Set-Cookie:" (11 characters) in header */
3866 cur_tag = *header + 11;
3868 /* skip whitespace between "Set-Cookie:" and value */
3869 while (*cur_tag && ijb_isspace(*cur_tag))
3874 /* Loop through each tag in the cookie */
3878 char *next_tag = strchr(cur_tag, ';');
3879 if (next_tag != NULL)
3881 /* Skip the ';' character itself */
3884 /* skip whitespace ";" and start of tag */
3885 while (*next_tag && ijb_isspace(*next_tag))
3892 /* "Next tag" is the end of the string */
3893 next_tag = cur_tag + strlen(cur_tag);
3897 * Check the expiration date to see
3898 * if the cookie is still valid, if yes,
3899 * rewrite it to a session cookie.
3901 if ((strncmpic(cur_tag, "expires=", 8) == 0) && *(cur_tag + 8))
3903 char *expiration_date = cur_tag + 8; /* Skip "[Ee]xpires=" */
3905 /* Did we detect the date properly? */
3906 if (JB_ERR_OK != parse_header_time(expiration_date, &cookie_time))
3909 * Nope, treat it as if it was still valid.
3911 * XXX: Should we remove the whole cookie instead?
3913 log_error(LOG_LEVEL_ERROR,
3914 "Can't parse \'%s\', send by %s. Unsupported time format?", cur_tag, csp->http->url);
3915 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
3921 * Yes. Check if the cookie is still valid.
3923 * If the cookie is already expired it's probably
3924 * a delete cookie and even if it isn't, the browser
3925 * will discard it anyway.
3929 * XXX: timegm() isn't available on some AmigaOS
3930 * versions and our replacement doesn't work.
3932 * Our options are to either:
3934 * - disable session-cookies-only completely if timegm
3937 * - to simply remove all expired tags, like it has
3938 * been done until Privoxy 3.0.6 and to live with
3939 * the consequence that it can cause login/logout
3940 * problems on servers that don't validate their
3941 * input properly, or
3943 * - to replace it with mktime in which
3944 * case there is a slight chance of valid cookies
3945 * passing as already expired.
3947 * This is the way it's currently done and it's not
3948 * as bad as it sounds. If the missing GMT offset is
3949 * enough to change the result of the expiration check
3950 * the cookie will be only valid for a few hours
3951 * anyway, which in many cases will be shorter
3952 * than a browser session.
3954 if (cookie_time - now < 0)
3956 log_error(LOG_LEVEL_HEADER,
3957 "Cookie \'%s\' is already expired and can pass unmodified.", *header);
3958 /* Just in case some clown sets more then one expiration date */
3964 * Still valid, delete expiration date by copying
3965 * the rest of the string over it.
3967 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
3968 * since the behaviour of strcpy is undefined for overlapping
3971 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
3973 /* That changed the header, need to issue a log message */
3977 * Note that the next tag has now been moved to *cur_tag,
3978 * so we do not need to update the cur_tag pointer.
3986 /* Move on to next cookie tag */
3993 assert(NULL != *header);
3994 log_error(LOG_LEVEL_HEADER, "Cookie rewritten to a temporary one: %s",
4003 #ifdef FEATURE_FORCE_LOAD
4004 /*********************************************************************
4006 * Function : strclean
4008 * Description : In-Situ-Eliminate all occurances of substring in
4012 * 1 : string = string to clean
4013 * 2 : substring = substring to eliminate
4015 * Returns : Number of eliminations
4017 *********************************************************************/
4018 int strclean(const char *string, const char *substring)
4024 len = strlen(substring);
4026 while((pos = strstr(string, substring)) != NULL)
4033 while (*p++ != '\0');
4040 #endif /* def FEATURE_FORCE_LOAD */
4043 /*********************************************************************
4045 * Function : parse_header_time
4047 * Description : Parses time formats used in HTTP header strings
4048 * to get the numerical respresentation.
4051 * 1 : header_time = HTTP header time as string.
4052 * 2 : result = storage for header_time in seconds
4054 * Returns : JB_ERR_OK if the time format was recognized, or
4055 * JB_ERR_PARSE otherwise.
4057 *********************************************************************/
4058 static jb_err parse_header_time(const char *header_time, time_t *result)
4063 * Zero out gmt to prevent time zone offsets.
4065 * While this is only necessary on some platforms
4066 * (mingw32 for example), I don't know how to
4067 * detect these automatically and doing it everywhere
4070 memset(&gmt, 0, sizeof(gmt));
4072 /* Tue, 02 Jun 2037 20:00:00 */
4073 if ((NULL == strptime(header_time, "%a, %d %b %Y %H:%M:%S", &gmt))
4074 /* Tue, 02-Jun-2037 20:00:00 */
4075 && (NULL == strptime(header_time, "%a, %d-%b-%Y %H:%M:%S", &gmt))
4076 /* Tue, 02-Jun-37 20:00:00 */
4077 && (NULL == strptime(header_time, "%a, %d-%b-%y %H:%M:%S", &gmt))
4078 /* Tuesday, 02-Jun-2037 20:00:00 */
4079 && (NULL == strptime(header_time, "%A, %d-%b-%Y %H:%M:%S", &gmt))
4080 /* Tuesday Jun 02 20:00:00 2037 */
4081 && (NULL == strptime(header_time, "%A %b %d %H:%M:%S %Y", &gmt)))
4083 return JB_ERR_PARSE;
4086 *result = timegm(&gmt);
4093 /*********************************************************************
4095 * Function : get_destination_from_headers
4097 * Description : Parse the "Host:" header to get the request's destination.
4098 * Only needed if the client's request was forcefully
4099 * redirected into Privoxy.
4101 * Code mainly copied from client_host() which is currently
4102 * run too late for this purpose.
4105 * 1 : headers = List of headers (one of them hopefully being
4106 * the "Host:" header)
4107 * 2 : http = storage for the result (host, port and hostport).
4109 * Returns : JB_ERR_MEMORY in case of memory problems,
4110 * JB_ERR_PARSE if the host header couldn't be found,
4111 * JB_ERR_OK otherwise.
4113 *********************************************************************/
4114 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
4120 host = get_header_value(headers, "Host:");
4124 log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
4125 return JB_ERR_PARSE;
4128 if (NULL == (p = strdup((host))))
4130 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
4131 return JB_ERR_MEMORY;
4134 if (NULL == (q = strdup(p)))
4137 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
4138 return JB_ERR_MEMORY;
4141 freez(http->hostport);
4145 q = strchr(http->host, ':');
4148 /* Terminate hostname and evaluate port string */
4150 http->port = atoi(q);
4154 http->port = http->ssl ? 443 : 80;
4157 /* Rebuild request URL */
4159 http->url = strdup(http->ssl ? "https://" : "http://");
4160 string_append(&http->url, http->hostport);
4161 string_append(&http->url, http->path);
4162 if (http->url == NULL)
4164 return JB_ERR_MEMORY;
4167 log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header. New request URL: %s",
4175 /*********************************************************************
4177 * Function : create_forged_referrer
4179 * Description : Helper for client_referrer to forge a referer as
4180 * 'http://[hostname:port/' to fool stupid
4181 * checks for in-site links
4184 * 1 : header = Pointer to header pointer
4185 * 2 : hostport = Host and optionally port as string
4187 * Returns : JB_ERR_OK in case of success, or
4188 * JB_ERR_MEMORY in case of memory problems.
4190 *********************************************************************/
4191 static jb_err create_forged_referrer(char **header, const char *hostport)
4193 assert(NULL == *header);
4195 *header = strdup("Referer: http://");
4196 string_append(header, hostport);
4197 string_append(header, "/");
4199 if (NULL == *header)
4201 return JB_ERR_MEMORY;
4204 log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
4211 /*********************************************************************
4213 * Function : create_fake_referrer
4215 * Description : Helper for client_referrer to create a fake referrer
4216 * based on a string supplied by the user.
4219 * 1 : header = Pointer to header pointer
4220 * 2 : hosthost = Referrer to fake
4222 * Returns : JB_ERR_OK in case of success, or
4223 * JB_ERR_MEMORY in case of memory problems.
4225 *********************************************************************/
4226 static jb_err create_fake_referrer(char **header, const char *fake_referrer)
4228 assert(NULL == *header);
4230 if ((0 != strncmpic(fake_referrer, "http://", 7)) && (0 != strncmpic(fake_referrer, "https://", 8)))
4232 log_error(LOG_LEVEL_HEADER,
4233 "Parameter: +hide-referrer{%s} is a bad idea, but I don't care.", fake_referrer);
4235 *header = strdup("Referer: ");
4236 string_append(header, fake_referrer);
4238 if (NULL == *header)
4240 return JB_ERR_MEMORY;
4243 log_error(LOG_LEVEL_HEADER, "Referer replaced with: %s", *header);
4250 /*********************************************************************
4252 * Function : handle_conditional_hide_referrer_parameter
4254 * Description : Helper for client_referrer to crunch or forge
4255 * the referrer header if the host has changed.
4258 * 1 : header = Pointer to header pointer
4259 * 2 : host = The target host (may include the port)
4260 * 3 : parameter_conditional_block = Boolean to signal
4261 * if we're in conditional-block mode. If not set,
4262 * we're in conditional-forge mode.
4264 * Returns : JB_ERR_OK in case of success, or
4265 * JB_ERR_MEMORY in case of memory problems.
4267 *********************************************************************/
4268 static jb_err handle_conditional_hide_referrer_parameter(char **header,
4269 const char *host, const int parameter_conditional_block)
4271 char *referer = strdup(*header);
4272 const size_t hostlenght = strlen(host);
4274 if (NULL == referer)
4277 return JB_ERR_MEMORY;
4280 /* referer begins with 'Referer: http[s]://' */
4281 if (hostlenght < (strlen(referer)-17))
4284 * Shorten referer to make sure the referer is blocked
4285 * if www.example.org/www.example.com-shall-see-the-referer/
4286 * links to www.example.com/
4288 referer[hostlenght+17] = '\0';
4290 if (NULL == strstr(referer, host))
4292 /* Host has changed */
4293 if (parameter_conditional_block)
4295 log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
4302 return create_forged_referrer(header, host);