1 const char parsers_rcs[] = "$Id: parsers.c,v 1.152 2009/03/01 18:43:48 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-2009 the
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.152 2009/03/01 18:43:48 fabiankeil
48 * Help clang understand that we aren't dereferencing
51 * Revision 1.151 2009/02/15 14:46:35 fabiankeil
52 * Don't let hide-referrer{conditional-*}} pass
53 * Referer headers without http URLs.
55 * Revision 1.150 2008/12/04 18:12:19 fabiankeil
56 * Fix some cparser warnings.
58 * Revision 1.149 2008/11/21 18:39:53 fabiankeil
59 * In case of CONNECT requests there's no point
60 * in trying to keep the connection alive.
62 * Revision 1.148 2008/11/16 12:43:49 fabiankeil
63 * Turn keep-alive support into a runtime feature
64 * that is disabled by setting keep-alive-timeout
65 * to a negative value.
67 * Revision 1.147 2008/11/04 17:20:31 fabiankeil
68 * HTTP/1.1 responses without Connection
69 * header imply keep-alive. Act accordingly.
71 * Revision 1.146 2008/10/12 16:46:35 fabiankeil
72 * Remove obsolete warning about delayed delivery with chunked
73 * transfer encoding and FEATURE_CONNECTION_KEEP_ALIVE enabled.
75 * Revision 1.145 2008/10/09 18:21:41 fabiankeil
76 * Flush work-in-progress changes to keep outgoing connections
77 * alive where possible. Incomplete and mostly #ifdef'd out.
79 * Revision 1.144 2008/09/21 13:59:33 fabiankeil
80 * Treat unknown change-x-forwarded-for parameters as fatal errors.
82 * Revision 1.143 2008/09/21 13:36:52 fabiankeil
83 * If change-x-forwarded-for{add} is used and the client
84 * sends multiple X-Forwarded-For headers, append the client's
85 * IP address to each one of them. "Traditionally" we would
86 * lose all but the last one.
88 * Revision 1.142 2008/09/20 10:04:33 fabiankeil
89 * Remove hide-forwarded-for-headers action which has
90 * been obsoleted by change-x-forwarded-for{block}.
92 * Revision 1.141 2008/09/19 15:26:28 fabiankeil
93 * Add change-x-forwarded-for{} action to block or add
94 * X-Forwarded-For headers. Mostly based on code removed
97 * Revision 1.140 2008/09/12 17:51:43 fabiankeil
98 * - A few style fixes.
99 * - Remove a pointless cast.
101 * Revision 1.139 2008/09/04 08:13:58 fabiankeil
102 * Prepare for critical sections on Windows by adding a
103 * layer of indirection before the pthread mutex functions.
105 * Revision 1.138 2008/08/30 12:03:07 fabiankeil
106 * Remove FEATURE_COOKIE_JAR.
108 * Revision 1.137 2008/05/30 15:50:08 fabiankeil
109 * Remove questionable micro-optimizations
110 * whose usefulness has never been measured.
112 * Revision 1.136 2008/05/26 16:02:24 fabiankeil
113 * s@Insufficent@Insufficient@
115 * Revision 1.135 2008/05/21 20:12:10 fabiankeil
116 * The whole point of strclean() is to modify the
117 * first parameter, so don't mark it immutable,
118 * even though the compiler lets us get away with it.
120 * Revision 1.134 2008/05/21 19:27:25 fabiankeil
121 * As the wafer actions are gone, we can stop including encode.h.
123 * Revision 1.133 2008/05/21 15:50:47 fabiankeil
124 * Ditch cast from (char **) to (char **).
126 * Revision 1.132 2008/05/21 15:47:14 fabiankeil
127 * Streamline sed()'s prototype and declare
128 * the header parse and add structures static.
130 * Revision 1.131 2008/05/20 20:13:30 fabiankeil
131 * Factor update_server_headers() out of sed(), ditch the
132 * first_run hack and make server_patterns_light static.
134 * Revision 1.130 2008/05/19 17:18:04 fabiankeil
135 * Wrap memmove() calls in string_move()
136 * to document the purpose in one place.
138 * Revision 1.129 2008/05/17 14:02:07 fabiankeil
139 * Normalize linear header white space.
141 * Revision 1.128 2008/05/16 16:39:03 fabiankeil
142 * If a header is split across multiple lines,
143 * merge them to a single line before parsing them.
145 * Revision 1.127 2008/05/10 13:23:38 fabiankeil
146 * Don't provide get_header() with the whole client state
147 * structure when it only needs access to csp->iob.
149 * Revision 1.126 2008/05/03 16:40:45 fabiankeil
150 * Change content_filters_enabled()'s parameter from
151 * csp->action to action so it can be also used in the
152 * CGI code. Don't bother checking if there are filters
153 * loaded, as that's somewhat besides the point.
155 * Revision 1.125 2008/04/17 14:40:49 fabiankeil
156 * Provide get_http_time() with the buffer size so it doesn't
157 * have to blindly assume that the buffer is big enough.
159 * Revision 1.124 2008/04/16 16:38:21 fabiankeil
160 * Don't pass the whole csp structure to flush_socket()
161 * when it only needs a file descriptor and a buffer.
163 * Revision 1.123 2008/03/29 12:13:46 fabiankeil
164 * Remove send-wafer and send-vanilla-wafer actions.
166 * Revision 1.122 2008/03/28 15:13:39 fabiankeil
167 * Remove inspect-jpegs action.
169 * Revision 1.121 2008/01/05 21:37:03 fabiankeil
170 * Let client_range() also handle Request-Range headers
171 * which apparently are still supported by many servers.
173 * Revision 1.120 2008/01/04 17:43:45 fabiankeil
174 * Improve the warning messages that get logged if the action files
175 * "enable" filters but no filters of that type have been loaded.
177 * Revision 1.119 2007/12/28 18:32:51 fabiankeil
178 * In server_content_type():
179 * - Don't require leading white space when detecting image content types.
180 * - Change '... not replaced ...' message to sound less crazy if the text
181 * type actually is 'text/plain'.
182 * - Mark the 'text/plain == binary data' assumption for removal.
183 * - Remove a bunch of trailing white space.
185 * Revision 1.118 2007/12/28 16:56:35 fabiankeil
186 * Minor server_content_disposition() changes:
187 * - Don't regenerate the header name all lower-case.
188 * - Some white space fixes.
189 * - Remove useless log message in case of ENOMEM.
191 * Revision 1.117 2007/12/06 18:11:50 fabiankeil
192 * Garbage-collect the code to add a X-Forwarded-For
193 * header as it seems to be mostly used by accident.
195 * Revision 1.116 2007/12/01 13:04:22 fabiankeil
196 * Fix a crash on mingw32 with some Last Modified times in the future.
198 * Revision 1.115 2007/11/02 16:52:50 fabiankeil
199 * Remove a "can't happen" error block which, over
200 * time, mutated into a "guaranteed to happen" block.
202 * Revision 1.114 2007/10/19 16:56:26 fabiankeil
203 * - Downgrade "Buffer limit reached" message to LOG_LEVEL_INFO.
204 * - Use shiny new content_filters_enabled() in client_range().
206 * Revision 1.113 2007/10/10 17:29:57 fabiankeil
207 * I forgot about Poland.
209 * Revision 1.112 2007/10/09 16:38:40 fabiankeil
210 * Remove Range and If-Range headers if content filtering is enabled.
212 * Revision 1.111 2007/10/04 18:07:00 fabiankeil
213 * Move ACTION_VANILLA_WAFER handling from jcc's chat() into
214 * client_cookie_adder() to make sure send-vanilla-wafer can be
215 * controlled through tags (and thus regression-tested).
217 * Revision 1.110 2007/09/29 10:42:37 fabiankeil
218 * - Remove "scanning headers for" log message again.
219 * - Some more whitespace fixes.
221 * Revision 1.109 2007/09/08 14:25:48 fabiankeil
222 * Refactor client_referrer() and add conditional-forge parameter.
224 * Revision 1.108 2007/08/28 18:21:03 fabiankeil
225 * A bunch of whitespace fixes, pointy hat to me.
227 * Revision 1.107 2007/08/28 18:16:32 fabiankeil
228 * Fix possible memory corruption in server_http, make sure it's not
229 * executed for ordinary server headers and mark some problems for later.
231 * Revision 1.106 2007/08/18 14:30:32 fabiankeil
232 * Let content-type-overwrite{} honour force-text-mode again.
234 * Revision 1.105 2007/08/11 14:49:49 fabiankeil
235 * - Add prototpyes for the header parsers and make them static.
236 * - Comment out client_accept_encoding_adder() which isn't used right now.
238 * Revision 1.104 2007/07/14 07:38:19 fabiankeil
239 * Move the ACTION_FORCE_TEXT_MODE check out of
240 * server_content_type(). Signal other functions
241 * whether or not a content type has been declared.
242 * Part of the fix for BR#1750917.
244 * Revision 1.103 2007/06/01 16:31:54 fabiankeil
245 * Change sed() to return a jb_err in preparation for forward-override{}.
247 * Revision 1.102 2007/05/27 12:39:32 fabiankeil
248 * Adjust "X-Filter: No" to disable dedicated header filters.
250 * Revision 1.101 2007/05/14 10:16:41 fabiankeil
251 * Streamline client_cookie_adder().
253 * Revision 1.100 2007/04/30 15:53:11 fabiankeil
254 * Make sure filters with dynamic jobs actually use them.
256 * Revision 1.99 2007/04/30 15:06:26 fabiankeil
257 * - Introduce dynamic pcrs jobs that can resolve variables.
258 * - Remove unnecessary update_action_bits_for_all_tags() call.
260 * Revision 1.98 2007/04/17 18:32:10 fabiankeil
261 * - Make tagging based on tags set by earlier taggers
262 * of the same kind possible.
263 * - Log whether or not new tags cause action bits updates
264 * (in which case a matching tag-pattern section exists).
265 * - Log if the user tries to set a tag that is already set.
267 * Revision 1.97 2007/04/15 16:39:21 fabiankeil
268 * Introduce tags as alternative way to specify which
269 * actions apply to a request. At the moment tags can be
270 * created based on client and server headers.
272 * Revision 1.96 2007/04/12 12:53:58 fabiankeil
273 * Log a warning if the content is compressed, filtering is
274 * enabled and Privoxy was compiled without zlib support.
277 * Revision 1.95 2007/03/25 14:26:40 fabiankeil
278 * - Fix warnings when compiled with glibc.
279 * - Don't use crumble() for cookie crunching.
280 * - Move cookie time parsing into parse_header_time().
281 * - Let parse_header_time() return a jb_err code
282 * instead of a pointer that can only be used to
283 * check for NULL anyway.
285 * Revision 1.94 2007/03/21 12:23:53 fabiankeil
286 * - Add better protection against malicious gzip headers.
287 * - Stop logging the first hundred bytes of decompressed content.
288 * It looks like it's working and there is always debug 16.
289 * - Log the content size after decompression in decompress_iob()
290 * instead of pcrs_filter_response().
292 * Revision 1.93 2007/03/20 15:21:44 fabiankeil
293 * - Use dedicated header filter actions instead of abusing "filter".
294 * Replace "filter-client-headers" and "filter-client-headers"
295 * with "server-header-filter" and "client-header-filter".
296 * - Remove filter_client_header() and filter_client_header(),
297 * filter_header() now checks the shiny new
298 * CSP_FLAG_CLIENT_HEADER_PARSING_DONE flag instead.
300 * Revision 1.92 2007/03/05 13:25:32 fabiankeil
301 * - Cosmetical changes for LOG_LEVEL_RE_FILTER messages.
302 * - Handle "Cookie:" and "Connection:" headers a bit smarter
303 * (don't crunch them just to recreate them later on).
304 * - Add another non-standard time format for the cookie
305 * expiration date detection.
306 * - Fix a valgrind warning.
308 * Revision 1.91 2007/02/24 12:27:32 fabiankeil
309 * Improve cookie expiration date detection.
311 * Revision 1.90 2007/02/08 19:12:35 fabiankeil
312 * Don't run server_content_length() the first time
313 * sed() parses server headers; only adjust the
314 * Content-Length header if the page was modified.
316 * Revision 1.89 2007/02/07 16:52:11 fabiankeil
317 * Fix log messages regarding the cookie time format
318 * (cookie and request URL were mixed up).
320 * Revision 1.88 2007/02/07 11:27:12 fabiankeil
321 * - Let decompress_iob()
322 * - not corrupt the content if decompression fails
323 * early. (the first byte(s) were lost).
324 * - use pointer arithmetics with defined outcome for
326 * - Use a different kludge to remember a failed decompression.
328 * Revision 1.87 2007/01/31 16:21:38 fabiankeil
329 * Search for Max-Forwards headers case-insensitive,
330 * don't generate the "501 unsupported" message for invalid
331 * Max-Forwards values and don't increase negative ones.
333 * Revision 1.86 2007/01/30 13:05:26 fabiankeil
334 * - Let server_set_cookie() check the expiration date
335 * of cookies and don't touch the ones that are already
336 * expired. Fixes problems with low quality web applications
337 * as described in BR 932612.
339 * - Adjust comment in client_max_forwards to reality;
340 * remove invalid Max-Forwards headers.
342 * Revision 1.85 2007/01/26 15:33:46 fabiankeil
343 * Stop filter_header() from unintentionally removing
344 * empty header lines that were enlisted by the continue
347 * Revision 1.84 2007/01/24 12:56:52 fabiankeil
348 * - Repeat the request URL before logging any headers.
349 * Makes reading the log easier in case of simultaneous requests.
350 * - If there are more than one Content-Type headers in one request,
351 * use the first one and remove the others.
352 * - Remove "newval" variable in server_content_type().
353 * It's only used once.
355 * Revision 1.83 2007/01/12 15:03:02 fabiankeil
356 * Correct a cast, check inflateEnd() exit code
357 * to see if we have to, replace sprintf calls
360 * Revision 1.82 2007/01/01 19:36:37 fabiankeil
361 * Integrate a modified version of Wil Mahan's
362 * zlib patch (PR #895531).
364 * Revision 1.81 2006/12/31 22:21:33 fabiankeil
365 * Skip empty filter files in filter_header()
366 * but don't ignore the ones that come afterwards.
367 * Fixes BR 1619208, this time for real.
369 * Revision 1.80 2006/12/29 19:08:22 fabiankeil
370 * Reverted parts of my last commit
371 * to keep error handling working.
373 * Revision 1.79 2006/12/29 18:04:40 fabiankeil
374 * Fixed gcc43 conversion warnings.
376 * Revision 1.78 2006/12/26 17:19:20 fabiankeil
377 * Bringing back the "useless" localtime() call
378 * I removed in revision 1.67. On some platforms
379 * it's necessary to prevent time zone offsets.
381 * Revision 1.77 2006/12/07 18:44:26 fabiankeil
382 * Rebuild request URL in get_destination_from_headers()
383 * to make sure redirect{pcrs command} works as expected
384 * for intercepted requests.
386 * Revision 1.76 2006/12/06 19:52:25 fabiankeil
387 * Added get_destination_from_headers().
389 * Revision 1.75 2006/11/13 19:05:51 fabiankeil
390 * Make pthread mutex locking more generic. Instead of
391 * checking for OSX and OpenBSD, check for FEATURE_PTHREAD
392 * and use mutex locking unless there is an _r function
393 * available. Better safe than sorry.
395 * Fixes "./configure --disable-pthread" and should result
396 * in less threading-related problems on pthread-using platforms,
397 * but it still doesn't fix BR#1122404.
399 * Revision 1.74 2006/10/02 16:59:12 fabiankeil
400 * The special header "X-Filter: No" now disables
401 * header filtering as well.
403 * Revision 1.73 2006/09/23 13:26:38 roro
404 * Replace TABs by spaces in source code.
406 * Revision 1.72 2006/09/23 12:37:21 fabiankeil
407 * Don't print a log message every time filter_headers is
408 * entered or left. It only creates noise without any real
411 * Revision 1.71 2006/09/21 19:55:17 fabiankeil
412 * Fix +hide-if-modified-since{-n}.
414 * Revision 1.70 2006/09/08 12:06:34 fabiankeil
415 * Have hide-if-modified-since interpret the random
416 * range value as minutes instead of hours. Allows
417 * more fine-grained configuration.
419 * Revision 1.69 2006/09/06 16:25:51 fabiankeil
420 * Always have parse_header_time return a pointer
421 * that actual makes sense, even though we currently
422 * only need it to detect problems.
424 * Revision 1.68 2006/09/06 10:43:32 fabiankeil
425 * Added config option enable-remote-http-toggle
426 * to specify if Privoxy should recognize special
427 * headers (currently only X-Filter) to change its
428 * behaviour. Disabled by default.
430 * Revision 1.67 2006/09/04 11:01:26 fabiankeil
431 * After filtering de-chunked instances, remove
432 * "Transfer-Encoding" header entirely instead of changing
433 * it to "Transfer-Encoding: identity", which is invalid.
434 * Thanks Michael Shields <shields@msrl.com>. Fixes PR 1318658.
436 * Don't use localtime in parse_header_time. An empty time struct
437 * is good enough, it gets overwritten by strptime anyway.
439 * Revision 1.66 2006/09/03 19:38:28 fabiankeil
440 * Use gmtime_r if available, fallback to gmtime with mutex
441 * protection for MacOSX and use vanilla gmtime for the rest.
443 * Revision 1.65 2006/08/22 10:55:56 fabiankeil
444 * Changed client_referrer to use the right type (size_t) for
445 * hostlenght and to shorten the temporary referrer string with
446 * '\0' instead of adding a useless line break.
448 * Revision 1.64 2006/08/17 17:15:10 fabiankeil
449 * - Back to timegm() using GnuPG's replacement if necessary.
450 * Using mktime() and localtime() could add a on hour offset if
451 * the randomize factor was big enough to lead to a summer/wintertime
454 * - Removed now-useless Privoxy 3.0.3 compatibility glue.
456 * - Moved randomization code into pick_from_range().
458 * - Changed parse_header_time definition.
459 * time_t isn't guaranteed to be signed and
460 * if it isn't, -1 isn't available as error code.
461 * Changed some variable types in client_if_modified_since()
462 * because of the same reason.
464 * Revision 1.63 2006/08/14 13:18:08 david__schmidt
465 * OS/2 compilation compatibility fixups
467 * Revision 1.62 2006/08/14 08:58:42 fabiankeil
468 * Changed include from strptime.c to strptime.h
470 * Revision 1.61 2006/08/14 08:25:19 fabiankeil
471 * Split filter-headers{} into filter-client-headers{}
472 * and filter-server-headers{}.
473 * Added parse_header_time() to share some code.
474 * Replaced timegm() with mktime().
476 * Revision 1.60 2006/08/12 03:54:37 david__schmidt
477 * Windows service integration
479 * Revision 1.59 2006/08/03 02:46:41 david__schmidt
480 * Incorporate Fabian Keil's patch work:
\rhttp://www.fabiankeil.de/sourcecode/privoxy/
482 * Revision 1.58 2006/07/18 14:48:47 david__schmidt
483 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
484 * with what was really the latest development (the v_3_0_branch branch)
486 * Revision 1.56.2.10 2006/01/21 16:16:08 david__schmidt
487 * Thanks to Edward Carrel for his patch to modernize OSX's
\rpthreads support. See bug #1409623.
489 * Revision 1.56.2.9 2004/10/03 12:53:45 david__schmidt
490 * Add the ability to check jpeg images for invalid
491 * lengths of comment blocks. Defensive strategy
492 * against the exploit:
493 * Microsoft Security Bulletin MS04-028
494 * Buffer Overrun in JPEG Processing (GDI+) Could
495 * Allow Code Execution (833987)
496 * Enabled with +inspect-jpegs in actions files.
498 * Revision 1.56.2.8 2003/07/11 13:21:25 oes
499 * Excluded text/plain objects from filtering. This fixes a
500 * couple of client-crashing, download corruption and
501 * Privoxy performance issues, whose root cause lies in
502 * web servers labelling content of unknown type as text/plain.
504 * Revision 1.56.2.7 2003/05/06 12:07:26 oes
505 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
507 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
508 * Completing the previous change
510 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
511 * Added temporary workaround for bug in PHP < 4.2.3
513 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
514 * 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.
516 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
517 * Fix typo: supressed -> suppressed
519 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
520 * Improved cookie logging
522 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
523 * Added basic support for OPTIONS and TRACE HTTP methods:
524 * - New parser function client_max_forwards which decrements
525 * the Max-Forwards HTTP header field of OPTIONS and TRACE
526 * requests by one before forwarding
527 * - New parser function client_host which extracts the host
528 * and port information from the HTTP header field if the
529 * request URI was not absolute
530 * - Don't crumble and re-add the Host: header, but only generate
531 * and append if missing
533 * Revision 1.56 2002/05/12 15:34:22 jongfoster
534 * Fixing typo in a comment
536 * Revision 1.55 2002/05/08 16:01:07 oes
537 * Optimized add_to_iob:
538 * - Use realloc instead of malloc(), memcpy(), free()
539 * - Expand to powers of two if possible, to get
540 * O(log n) reallocs instead of O(n).
541 * - Moved check for buffer limit here from chat
542 * - Report failure via returncode
544 * Revision 1.54 2002/04/02 15:03:16 oes
545 * Tiny code cosmetics
547 * Revision 1.53 2002/03/26 22:29:55 swa
548 * we have a new homepage!
550 * Revision 1.52 2002/03/24 13:25:43 swa
551 * name change related issues
553 * Revision 1.51 2002/03/13 00:27:05 jongfoster
556 * Revision 1.50 2002/03/12 01:45:35 oes
557 * More verbose logging
559 * Revision 1.49 2002/03/09 20:03:52 jongfoster
560 * - Making various functions return int rather than size_t.
561 * (Undoing a recent change). Since size_t is unsigned on
562 * Windows, functions like read_socket that return -1 on
563 * error cannot return a size_t.
565 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
566 * crashes, and also frequently caused JB to jump to 100%
567 * CPU and stay there. (Because it thought it had just
568 * read ((unsigned)-1) == 4Gb of data...)
570 * - The signature of write_socket has changed, it now simply
571 * returns success=0/failure=nonzero.
573 * - Trying to get rid of a few warnings --with-debug on
574 * Windows, I've introduced a new type "jb_socket". This is
575 * used for the socket file descriptors. On Windows, this
576 * is SOCKET (a typedef for unsigned). Everywhere else, it's
577 * an int. The error value can't be -1 any more, so it's
578 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
579 * Windows it maps to the #define INVALID_SOCKET.)
581 * - The signature of bind_port has changed.
583 * Revision 1.48 2002/03/07 03:46:53 oes
584 * Fixed compiler warnings etc
586 * Revision 1.47 2002/02/20 23:15:13 jongfoster
587 * Parsing functions now handle out-of-memory gracefully by returning
590 * Revision 1.46 2002/01/17 21:03:47 jongfoster
591 * Moving all our URL and URL pattern parsing code to urlmatch.c.
593 * Revision 1.45 2002/01/09 14:33:03 oes
594 * Added support for localtime_r.
596 * Revision 1.44 2001/12/14 01:22:54 steudten
597 * Remove 'user:pass@' from 'proto://user:pass@host' for the
598 * new added header 'Host: ..'. (See Req ID 491818)
600 * Revision 1.43 2001/11/23 00:26:38 jongfoster
601 * Fixing two really stupid errors in my previous commit
603 * Revision 1.42 2001/11/22 21:59:30 jongfoster
604 * Adding code to handle +no-cookies-keep
606 * Revision 1.41 2001/11/05 23:43:05 steudten
607 * Add time+date to log files.
609 * Revision 1.40 2001/10/26 20:13:09 jongfoster
610 * ctype.h is needed in Windows, too.
612 * Revision 1.39 2001/10/26 17:40:04 oes
613 * Introduced get_header_value()
614 * Removed http->user_agent, csp->referrer and csp->accept_types
615 * Removed client_accept()
617 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
618 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
619 * threads to call select() simultaneously. So, it's time to do a real, live,
620 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
621 * (native). Both versions will work, but using __OS2__ offers multi-threading.
623 * Revision 1.37 2001/10/23 21:36:02 jongfoster
624 * Documenting sed()'s error behaviou (doc change only)
626 * Revision 1.36 2001/10/13 12:51:51 joergs
627 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
628 * force-load), instead crumble Host: and add it (again) in client_host_adder
629 * (in case we get a HTTP/1.0 request without Host: header and forward it to
630 * a HTTP/1.1 server/proxy).
632 * Revision 1.35 2001/10/09 22:39:21 jongfoster
633 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
636 * Revision 1.34 2001/10/07 18:50:55 oes
637 * Added server_content_encoding, renamed server_transfer_encoding
639 * Revision 1.33 2001/10/07 18:04:49 oes
640 * Changed server_http11 to server_http and its pattern to "HTTP".
641 * Additional functionality: it now saves the HTTP status into
642 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
644 * Revision 1.32 2001/10/07 15:43:28 oes
645 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
646 * client_te and client_accept_encoding_adder, triggered by the new
647 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
648 * changed to allow only identity and chunked, and the TE header is
649 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
651 * parse_http_request no longer does anything than parsing. The rewriting
652 * of http->cmd and version mangling are gone. It now also recognizes
653 * the put and delete methods and saves the url in http->url. Removed
656 * renamed content_type and content_length to have the server_ prefix
658 * server_content_type now only works if csp->content_type != CT_TABOO
660 * added server_transfer_encoding, which
661 * - Sets CT_TABOO to prohibit filtering if encoding compresses
662 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
663 * - Change from "chunked" to "identity" if body was chunked
664 * but has been de-chunked for filtering.
666 * added server_content_md5 which crunches any Content-MD5 headers
667 * if the body was modified.
669 * made server_http11 conditional on +downgrade action
671 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
673 * Revision 1.31 2001/10/05 14:25:02 oes
674 * Crumble Keep-Alive from Server
676 * Revision 1.30 2001/09/29 12:56:03 joergs
677 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
679 * Revision 1.29 2001/09/24 21:09:24 jongfoster
680 * Fixing 2 memory leaks that Guy spotted, where the paramater to
681 * enlist() was not being free()d.
683 * Revision 1.28 2001/09/22 16:32:28 jongfoster
684 * Removing unused #includes.
686 * Revision 1.27 2001/09/20 15:45:25 steudten
688 * add casting from size_t to int for printf()
689 * remove local variable shadow s2
691 * Revision 1.26 2001/09/16 17:05:14 jongfoster
692 * Removing unused #include showarg.h
694 * Revision 1.25 2001/09/16 13:21:27 jongfoster
695 * Changes to use new list functions.
697 * Revision 1.24 2001/09/13 23:05:50 jongfoster
698 * Changing the string paramater to the header parsers a "const".
700 * Revision 1.23 2001/09/12 18:08:19 steudten
702 * In parse_http_request() header rewriting miss the host value, so
703 * from http://www.mydomain.com the result was just " / " not
704 * http://www.mydomain.com/ in case we forward.
706 * Revision 1.22 2001/09/10 10:58:53 oes
707 * Silenced compiler warnings
709 * Revision 1.21 2001/07/31 14:46:00 oes
710 * - Persistant connections now suppressed
711 * - sed() no longer appends empty header to csp->headers
713 * Revision 1.20 2001/07/30 22:08:36 jongfoster
714 * Tidying up #defines:
715 * - All feature #defines are now of the form FEATURE_xxx
716 * - Permanently turned off WIN_GUI_EDIT
717 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
719 * Revision 1.19 2001/07/25 17:21:54 oes
720 * client_uagent now saves copy of User-Agent: header value
722 * Revision 1.18 2001/07/13 14:02:46 oes
723 * - Included fix to repair broken HTTP requests that
724 * don't contain a path, not even '/'.
725 * - Removed all #ifdef PCRS
726 * - content_type now always inspected and classified as
727 * text, gif or other.
728 * - formatting / comments
730 * Revision 1.17 2001/06/29 21:45:41 oes
731 * Indentation, CRLF->LF, Tab-> Space
733 * Revision 1.16 2001/06/29 13:32:42 oes
735 * - Adapted free_http_request
736 * - Removed logentry from cancelled commit
738 * Revision 1.15 2001/06/03 19:12:38 oes
739 * deleted const struct interceptors
741 * Revision 1.14 2001/06/01 18:49:17 jongfoster
742 * Replaced "list_share" with "list" - the tiny memory gain was not
743 * worth the extra complexity.
745 * Revision 1.13 2001/05/31 21:30:33 jongfoster
746 * Removed list code - it's now in list.[ch]
747 * Renamed "permission" to "action", and changed many features
748 * to use the actions file rather than the global config.
750 * Revision 1.12 2001/05/31 17:33:13 oes
754 * Revision 1.11 2001/05/29 20:11:19 joergs
755 * '/ * inside comment' warning removed.
757 * Revision 1.10 2001/05/29 09:50:24 jongfoster
758 * Unified blocklist/imagelist/permissionslist.
759 * File format is still under discussion, but the internal changes
762 * Also modified interceptor behaviour:
763 * - We now intercept all URLs beginning with one of the following
764 * prefixes (and *only* these prefixes):
766 * * http://ijbswa.sf.net/config/
767 * * http://ijbswa.sourceforge.net/config/
768 * - New interceptors "home page" - go to http://i.j.b/ to see it.
769 * - Internal changes so that intercepted and fast redirect pages
770 * are not replaced with an image.
771 * - Interceptors now have the option to send a binary page direct
772 * to the client. (i.e. ijb-send-banner uses this)
773 * - Implemented show-url-info interceptor. (Which is why I needed
774 * the above interceptors changes - a typical URL is
775 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
776 * The previous mechanism would not have intercepted that, and
777 * if it had been intercepted then it then it would have replaced
780 * Revision 1.9 2001/05/28 17:26:33 jongfoster
781 * Fixing segfault if last header was crunched.
782 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
783 * can use the cross-platform sprintf() instead.)
785 * Revision 1.8 2001/05/27 22:17:04 oes
787 * - re_process_buffer no longer writes the modified buffer
788 * to the client, which was very ugly. It now returns the
789 * buffer, which it is then written by chat.
791 * - content_length now adjusts the Content-Length: header
792 * for modified documents rather than crunch()ing it.
793 * (Length info in csp->content_length, which is 0 for
794 * unmodified documents)
796 * - For this to work, sed() is called twice when filtering.
798 * Revision 1.7 2001/05/27 13:19:06 oes
799 * Patched Joergs solution for the content-length in.
801 * Revision 1.6 2001/05/26 13:39:32 jongfoster
802 * Only crunches Content-Length header if applying RE filtering.
803 * Without this fix, Microsoft Windows Update wouldn't work.
805 * Revision 1.5 2001/05/26 00:28:36 jongfoster
806 * Automatic reloading of config file.
807 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
808 * Most of the global variables have been moved to a new
809 * struct configuration_spec, accessed through csp->config->globalname
810 * Most of the globals remaining are used by the Win32 GUI.
812 * Revision 1.4 2001/05/22 18:46:04 oes
814 * - Enabled filtering banners by size rather than URL
815 * by adding patterns that replace all standard banner
816 * sizes with the "Junkbuster" gif to the re_filterfile
818 * - Enabled filtering WebBugs by providing a pattern
819 * which kills all 1x1 images
821 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
822 * which is selected by the (nonstandard and therefore
823 * capital) letter 'U' in the option string.
824 * It causes the quantifiers to be ungreedy by default.
825 * Appending a ? turns back to greedy (!).
827 * - Added a new interceptor ijb-send-banner, which
828 * sends back the "Junkbuster" gif. Without imagelist or
829 * MSIE detection support, or if tinygif = 1, or the
830 * URL isn't recognized as an imageurl, a lame HTML
831 * explanation is sent instead.
833 * - Added new feature, which permits blocking remote
834 * script redirects and firing back a local redirect
836 * The feature is conditionally compiled, i.e. it
837 * can be disabled with --disable-fast-redirects,
838 * plus it must be activated by a "fast-redirects"
839 * line in the config file, has its own log level
840 * and of course wants to be displayed by show-proxy-args
841 * Note: Boy, all the #ifdefs in 1001 locations and
842 * all the fumbling with configure.in and acconfig.h
843 * were *way* more work than the feature itself :-(
845 * - Because a generic redirect template was needed for
846 * this, tinygif = 3 now uses the same.
848 * - Moved GIFs, and other static HTTP response templates
853 * - Removed some >400 CRs again (Jon, you really worked
856 * Revision 1.3 2001/05/20 01:21:20 jongfoster
857 * Version 2.9.4 checkin.
858 * - Merged popupfile and cookiefile, and added control over PCRS
859 * filtering, in new "permissionsfile".
860 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
861 * file error you now get a message box (in the Win32 GUI) rather
862 * than the program exiting with no explanation.
863 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
865 * - Removed tabs from "config"
866 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
867 * - Bumped up version number.
869 * Revision 1.2 2001/05/17 23:02:36 oes
870 * - Made referrer option accept 'L' as a substitute for '§'
872 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
873 * Initial import of version 2.9.3 source tree
876 *********************************************************************/
883 #include <sys/types.h>
893 * Convince GNU's libc to provide a strptime prototype.
896 #endif /*__GLIBC__ */
903 #if !defined(_WIN32) && !defined(__OS2__)
909 #ifdef FEATURE_PTHREAD
911 /* jcc.h is for mutex semapores only */
912 #endif /* def FEATURE_PTHREAD */
917 #include "jbsockets.h"
918 #include "miscutil.h"
923 #ifndef HAVE_STRPTIME
924 #include "strptime.h"
927 const char parsers_h_rcs[] = PARSERS_H_VERSION;
929 /* Fix a problem with Solaris. There should be no effect on other
931 * Solaris's isspace() is a macro which uses its argument directly
932 * as an array index. Therefore we need to make sure that high-bit
933 * characters generate +ve values, and ideally we also want to make
934 * the argument match the declared parameter type of "int".
936 * Why did they write a character function that can't take a simple
937 * "char" argument? Doh!
939 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
940 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
942 static char *get_header_line(struct iob *iob);
943 static jb_err scan_headers(struct client_state *csp);
944 static jb_err header_tagger(struct client_state *csp, char *header);
945 static jb_err parse_header_time(const char *header_time, time_t *result);
947 static jb_err crumble (struct client_state *csp, char **header);
948 static jb_err filter_header (struct client_state *csp, char **header);
949 static jb_err client_connection (struct client_state *csp, char **header);
950 static jb_err client_referrer (struct client_state *csp, char **header);
951 static jb_err client_uagent (struct client_state *csp, char **header);
952 static jb_err client_ua (struct client_state *csp, char **header);
953 static jb_err client_from (struct client_state *csp, char **header);
954 static jb_err client_send_cookie (struct client_state *csp, char **header);
955 static jb_err client_x_forwarded (struct client_state *csp, char **header);
956 static jb_err client_accept_encoding (struct client_state *csp, char **header);
957 static jb_err client_te (struct client_state *csp, char **header);
958 static jb_err client_max_forwards (struct client_state *csp, char **header);
959 static jb_err client_host (struct client_state *csp, char **header);
960 static jb_err client_if_modified_since (struct client_state *csp, char **header);
961 static jb_err client_accept_language (struct client_state *csp, char **header);
962 static jb_err client_if_none_match (struct client_state *csp, char **header);
963 static jb_err crunch_client_header (struct client_state *csp, char **header);
964 static jb_err client_x_filter (struct client_state *csp, char **header);
965 static jb_err client_range (struct client_state *csp, char **header);
966 static jb_err server_set_cookie (struct client_state *csp, char **header);
967 static jb_err server_connection (struct client_state *csp, char **header);
968 static jb_err server_content_type (struct client_state *csp, char **header);
969 static jb_err server_adjust_content_length(struct client_state *csp, char **header);
970 static jb_err server_content_md5 (struct client_state *csp, char **header);
971 static jb_err server_content_encoding (struct client_state *csp, char **header);
972 static jb_err server_transfer_coding (struct client_state *csp, char **header);
973 static jb_err server_http (struct client_state *csp, char **header);
974 static jb_err crunch_server_header (struct client_state *csp, char **header);
975 static jb_err server_last_modified (struct client_state *csp, char **header);
976 static jb_err server_content_disposition(struct client_state *csp, char **header);
978 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
979 static jb_err server_save_content_length(struct client_state *csp, char **header);
980 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
982 static jb_err client_host_adder (struct client_state *csp);
983 static jb_err client_xtra_adder (struct client_state *csp);
984 static jb_err client_x_forwarded_for_adder(struct client_state *csp);
985 static jb_err client_connection_header_adder(struct client_state *csp);
986 static jb_err server_connection_close_adder(struct client_state *csp);
988 static jb_err create_forged_referrer(char **header, const char *hostport);
989 static jb_err create_fake_referrer(char **header, const char *fake_referrer);
990 static jb_err handle_conditional_hide_referrer_parameter(char **header,
991 const char *host, const int parameter_conditional_block);
992 static const char *get_appropiate_connection_header(const struct client_state *csp);
995 * List of functions to run on a list of headers.
999 /** The header prefix to match */
1002 /** The length of the prefix to match */
1005 /** The function to apply to this line */
1006 const parser_func_ptr parser;
1009 static const struct parsers client_patterns[] = {
1010 { "referer:", 8, client_referrer },
1011 { "user-agent:", 11, client_uagent },
1012 { "ua-", 3, client_ua },
1013 { "from:", 5, client_from },
1014 { "cookie:", 7, client_send_cookie },
1015 { "x-forwarded-for:", 16, client_x_forwarded },
1016 { "Accept-Encoding:", 16, client_accept_encoding },
1017 { "TE:", 3, client_te },
1018 { "Host:", 5, client_host },
1019 { "if-modified-since:", 18, client_if_modified_since },
1020 { "Keep-Alive:", 11, crumble },
1021 { "connection:", 11, client_connection },
1022 { "proxy-connection:", 17, crumble },
1023 { "max-forwards:", 13, client_max_forwards },
1024 { "Accept-Language:", 16, client_accept_language },
1025 { "if-none-match:", 14, client_if_none_match },
1026 { "Range:", 6, client_range },
1027 { "Request-Range:", 14, client_range },
1028 { "If-Range:", 9, client_range },
1029 { "X-Filter:", 9, client_x_filter },
1030 { "*", 0, crunch_client_header },
1031 { "*", 0, filter_header },
1035 static const struct parsers server_patterns[] = {
1036 { "HTTP/", 5, server_http },
1037 { "set-cookie:", 11, server_set_cookie },
1038 { "connection:", 11, server_connection },
1039 { "Content-Type:", 13, server_content_type },
1040 { "Content-MD5:", 12, server_content_md5 },
1041 { "Content-Encoding:", 17, server_content_encoding },
1042 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1043 { "Content-Length:", 15, server_save_content_length },
1044 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1045 { "Transfer-Encoding:", 18, server_transfer_coding },
1046 { "Keep-Alive:", 11, crumble },
1047 { "content-disposition:", 20, server_content_disposition },
1048 { "Last-Modified:", 14, server_last_modified },
1049 { "*", 0, crunch_server_header },
1050 { "*", 0, filter_header },
1054 static const add_header_func_ptr add_client_headers[] = {
1056 client_x_forwarded_for_adder,
1058 /* Temporarily disabled: client_accept_encoding_adder, */
1059 client_connection_header_adder,
1063 static const add_header_func_ptr add_server_headers[] = {
1064 server_connection_close_adder,
1068 /*********************************************************************
1070 * Function : flush_socket
1072 * Description : Write any pending "buffered" content.
1075 * 1 : fd = file descriptor of the socket to read
1076 * 2 : iob = The I/O buffer to flush, usually csp->iob.
1078 * Returns : On success, the number of bytes written are returned (zero
1079 * indicates nothing was written). On error, -1 is returned,
1080 * and errno is set appropriately. If count is zero and the
1081 * file descriptor refers to a regular file, 0 will be
1082 * returned without causing any other effect. For a special
1083 * file, the results are not portable.
1085 *********************************************************************/
1086 int flush_socket(jb_socket fd, struct iob *iob)
1088 int len = iob->eod - iob->cur;
1095 if (write_socket(fd, iob->cur, (size_t)len))
1099 iob->eod = iob->cur = iob->buf;
1105 /*********************************************************************
1107 * Function : add_to_iob
1109 * Description : Add content to the buffered page, expanding the
1110 * buffer if necessary.
1113 * 1 : csp = Current client state (buffers, headers, etc...)
1114 * 2 : buf = holds the content to be added to the page
1115 * 3 : n = number of bytes to be added
1117 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
1118 * or buffer limit reached.
1120 *********************************************************************/
1121 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
1123 struct iob *iob = csp->iob;
1124 size_t used, offset, need, want;
1127 if (n <= 0) return JB_ERR_OK;
1129 used = (size_t)(iob->eod - iob->buf);
1130 offset = (size_t)(iob->cur - iob->buf);
1131 need = used + (size_t)n + 1;
1134 * If the buffer can't hold the new data, extend it first.
1135 * Use the next power of two if possible, else use the actual need.
1137 if (need > csp->config->buffer_limit)
1139 log_error(LOG_LEVEL_INFO,
1140 "Buffer limit reached while extending the buffer (iob). Needed: %d. Limit: %d",
1141 need, csp->config->buffer_limit);
1142 return JB_ERR_MEMORY;
1145 if (need > iob->size)
1147 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
1149 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
1153 else if (NULL != (p = (char *)realloc(iob->buf, need)))
1159 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
1160 return JB_ERR_MEMORY;
1163 /* Update the iob pointers */
1164 iob->cur = p + offset;
1165 iob->eod = p + used;
1169 /* copy the new data into the iob buffer */
1170 memcpy(iob->eod, buf, (size_t)n);
1172 /* point to the end of the data */
1175 /* null terminate == cheap insurance */
1184 /*********************************************************************
1186 * Function : decompress_iob
1188 * Description : Decompress buffered page, expanding the
1189 * buffer as necessary. csp->iob->cur
1190 * should point to the the beginning of the
1191 * compressed data block.
1194 * 1 : csp = Current client state (buffers, headers, etc...)
1196 * Returns : JB_ERR_OK on success,
1197 * JB_ERR_MEMORY if out-of-memory limit reached, and
1198 * JB_ERR_COMPRESS if error decompressing buffer.
1200 *********************************************************************/
1201 jb_err decompress_iob(struct client_state *csp)
1203 char *buf; /* new, uncompressed buffer */
1204 char *cur; /* Current iob position (to keep the original
1205 * iob->cur unmodified if we return early) */
1206 size_t bufsize; /* allocated size of the new buffer */
1207 size_t old_size; /* Content size before decompression */
1208 size_t skip_size; /* Number of bytes at the beginning of the iob
1209 that we should NOT decompress. */
1210 int status; /* return status of the inflate() call */
1211 z_stream zstr; /* used by calls to zlib */
1213 assert(csp->iob->cur - csp->iob->buf > 0);
1214 assert(csp->iob->eod - csp->iob->cur > 0);
1216 bufsize = csp->iob->size;
1217 skip_size = (size_t)(csp->iob->cur - csp->iob->buf);
1218 old_size = (size_t)(csp->iob->eod - csp->iob->cur);
1220 cur = csp->iob->cur;
1222 if (bufsize < (size_t)10)
1225 * This is to protect the parsing of gzipped data,
1226 * but it should(?) be valid for deflated data also.
1228 log_error(LOG_LEVEL_ERROR, "Buffer too small decompressing iob");
1229 return JB_ERR_COMPRESS;
1232 if (csp->content_type & CT_GZIP)
1235 * Our task is slightly complicated by the facts that data
1236 * compressed by gzip does not include a zlib header, and
1237 * that there is no easily accessible interface in zlib to
1238 * handle a gzip header. We strip off the gzip header by
1239 * hand, and later inform zlib not to expect a header.
1243 * Strip off the gzip header. Please see RFC 1952 for more
1244 * explanation of the appropriate fields.
1246 if ((*cur++ != (char)0x1f)
1247 || (*cur++ != (char)0x8b)
1248 || (*cur++ != Z_DEFLATED))
1250 log_error(LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
1251 return JB_ERR_COMPRESS;
1257 * XXX: These magic numbers should be replaced
1258 * with macros to give a better idea what they do.
1262 /* The gzip header has reserved bits set; bail out. */
1263 log_error(LOG_LEVEL_ERROR, "Invalid gzip header flags when decompressing");
1264 return JB_ERR_COMPRESS;
1268 /* Skip extra fields if necessary. */
1272 * Skip a given number of bytes, specified
1273 * as a 16-bit little-endian value.
1276 * XXX: This code used to be:
1278 * csp->iob->cur += *csp->iob->cur++ + (*csp->iob->cur++ << 8);
1280 * which I had to change into:
1282 * cur += *cur++ + (*cur++ << 8);
1284 * at which point gcc43 finally noticed that the value
1285 * of cur is undefined (it depends on which of the
1286 * summands is evaluated first).
1288 * I haven't come across a site where this
1289 * code is actually executed yet, but I hope
1293 skip_bytes = *cur++;
1294 skip_bytes = *cur++ << 8;
1296 assert(skip_bytes == *csp->iob->cur - 2 + ((*csp->iob->cur - 1) << 8));
1299 * The number of bytes to skip should be positive
1300 * and we'd like to stay in the buffer.
1302 if ((skip_bytes < 0) || (skip_bytes >= (csp->iob->eod - cur)))
1304 log_error(LOG_LEVEL_ERROR,
1305 "Unreasonable amount of bytes to skip (%d). Stopping decompression",
1307 return JB_ERR_COMPRESS;
1309 log_error(LOG_LEVEL_INFO,
1310 "Skipping %d bytes for gzip compression. Does this sound right?",
1315 /* Skip the filename if necessary. */
1318 /* A null-terminated string is supposed to follow. */
1319 while (*cur++ && (cur < csp->iob->eod));
1323 /* Skip the comment if necessary. */
1326 /* A null-terminated string is supposed to follow. */
1327 while (*cur++ && (cur < csp->iob->eod));
1330 /* Skip the CRC if necessary. */
1336 if (cur >= csp->iob->eod)
1339 * If the current position pointer reached or passed
1340 * the buffer end, we were obviously tricked to skip
1343 log_error(LOG_LEVEL_ERROR,
1344 "Malformed gzip header detected. Aborting decompression.");
1345 return JB_ERR_COMPRESS;
1349 else if (csp->content_type & CT_DEFLATE)
1352 * XXX: The debug level should be lowered
1353 * before the next stable release.
1355 log_error(LOG_LEVEL_INFO, "Decompressing deflated iob: %d", *cur);
1357 * In theory (that is, according to RFC 1950), deflate-compressed
1358 * data should begin with a two-byte zlib header and have an
1359 * adler32 checksum at the end. It seems that in practice only
1360 * the raw compressed data is sent. Note that this means that
1361 * we are not RFC 1950-compliant here, but the advantage is that
1362 * this actually works. :)
1364 * We add a dummy null byte to tell zlib where the data ends,
1365 * and later inform it not to expect a header.
1367 * Fortunately, add_to_iob() has thoughtfully null-terminated
1368 * the buffer; we can just increment the end pointer to include
1375 log_error(LOG_LEVEL_ERROR,
1376 "Unable to determine compression format for decompression");
1377 return JB_ERR_COMPRESS;
1380 /* Set up the fields required by zlib. */
1381 zstr.next_in = (Bytef *)cur;
1382 zstr.avail_in = (unsigned int)(csp->iob->eod - cur);
1383 zstr.zalloc = Z_NULL;
1384 zstr.zfree = Z_NULL;
1385 zstr.opaque = Z_NULL;
1388 * Passing -MAX_WBITS to inflateInit2 tells the library
1389 * that there is no zlib header.
1391 if (inflateInit2 (&zstr, -MAX_WBITS) != Z_OK)
1393 log_error(LOG_LEVEL_ERROR, "Error initializing decompression");
1394 return JB_ERR_COMPRESS;
1398 * Next, we allocate new storage for the inflated data.
1399 * We don't modify the existing iob yet, so in case there
1400 * is error in decompression we can recover gracefully.
1402 buf = zalloc(bufsize);
1405 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1406 return JB_ERR_MEMORY;
1409 assert(bufsize >= skip_size);
1410 memcpy(buf, csp->iob->buf, skip_size);
1411 zstr.avail_out = bufsize - skip_size;
1412 zstr.next_out = (Bytef *)buf + skip_size;
1414 /* Try to decompress the whole stream in one shot. */
1415 while (Z_BUF_ERROR == (status = inflate(&zstr, Z_FINISH)))
1417 /* We need to allocate more memory for the output buffer. */
1419 char *tmpbuf; /* used for realloc'ing the buffer */
1420 size_t oldbufsize = bufsize; /* keep track of the old bufsize */
1423 * If zlib wants more data then there's a problem, because
1424 * the complete compressed file should have been buffered.
1426 if (0 == zstr.avail_in)
1428 log_error(LOG_LEVEL_ERROR, "Unexpected end of compressed iob");
1429 return JB_ERR_COMPRESS;
1433 * If we tried the limit and still didn't have enough
1434 * memory, just give up.
1436 if (bufsize == csp->config->buffer_limit)
1438 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while decompressing iob");
1439 return JB_ERR_MEMORY;
1442 /* Try doubling the buffer size each time. */
1445 /* Don't exceed the buffer limit. */
1446 if (bufsize > csp->config->buffer_limit)
1448 bufsize = csp->config->buffer_limit;
1451 /* Try to allocate the new buffer. */
1452 tmpbuf = realloc(buf, bufsize);
1455 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1457 return JB_ERR_MEMORY;
1461 char *oldnext_out = (char *)zstr.next_out;
1464 * Update the fields for inflate() to use the new
1465 * buffer, which may be in a location different from
1468 zstr.avail_out += bufsize - oldbufsize;
1469 zstr.next_out = (Bytef *)tmpbuf + bufsize - zstr.avail_out;
1472 * Compare with an uglier method of calculating these values
1473 * that doesn't require the extra oldbufsize variable.
1475 assert(zstr.avail_out == tmpbuf + bufsize - (char *)zstr.next_out);
1476 assert((char *)zstr.next_out == tmpbuf + ((char *)oldnext_out - buf));
1477 assert(zstr.avail_out > 0U);
1483 if (Z_STREAM_ERROR == inflateEnd(&zstr))
1485 log_error(LOG_LEVEL_ERROR,
1486 "Inconsistent stream state after decompression: %s", zstr.msg);
1488 * XXX: Intentionally no return.
1490 * According to zlib.h, Z_STREAM_ERROR is returned
1491 * "if the stream state was inconsistent".
1493 * I assume in this case inflate()'s status
1494 * would also be something different than Z_STREAM_END
1495 * so this check should be redundant, but lets see.
1499 if (status != Z_STREAM_END)
1501 /* We failed to decompress the stream. */
1502 log_error(LOG_LEVEL_ERROR,
1503 "Error in decompressing to the buffer (iob): %s", zstr.msg);
1504 return JB_ERR_COMPRESS;
1508 * Finally, we can actually update the iob, since the
1509 * decompression was successful. First, free the old
1512 freez(csp->iob->buf);
1514 /* Now, update the iob to use the new buffer. */
1515 csp->iob->buf = buf;
1516 csp->iob->cur = csp->iob->buf + skip_size;
1517 csp->iob->eod = (char *)zstr.next_out;
1518 csp->iob->size = bufsize;
1521 * Make sure the new uncompressed iob obeys some minimal
1522 * consistency conditions.
1524 if ((csp->iob->buf < csp->iob->cur)
1525 && (csp->iob->cur <= csp->iob->eod)
1526 && (csp->iob->eod <= csp->iob->buf + csp->iob->size))
1528 const size_t new_size = (size_t)(csp->iob->eod - csp->iob->cur);
1529 if (new_size > (size_t)0)
1531 log_error(LOG_LEVEL_RE_FILTER,
1532 "Decompression successful. Old size: %d, new size: %d.",
1533 old_size, new_size);
1537 /* zlib thinks this is OK, so lets do the same. */
1538 log_error(LOG_LEVEL_INFO, "Decompression didn't result in any content.");
1543 /* It seems that zlib did something weird. */
1544 log_error(LOG_LEVEL_ERROR,
1545 "Unexpected error decompressing the buffer (iob): %d==%d, %d>%d, %d<%d",
1546 csp->iob->cur, csp->iob->buf + skip_size, csp->iob->eod, csp->iob->buf,
1547 csp->iob->eod, csp->iob->buf + csp->iob->size);
1548 return JB_ERR_COMPRESS;
1554 #endif /* defined(FEATURE_ZLIB) */
1557 /*********************************************************************
1559 * Function : string_move
1561 * Description : memmove wrapper to move the last part of a string
1562 * towards the beginning, overwriting the part in
1563 * the middle. strlcpy() can't be used here as the
1567 * 1 : dst = Destination to overwrite
1568 * 2 : src = Source to move.
1572 *********************************************************************/
1573 static void string_move(char *dst, char *src)
1577 /* +1 to copy the terminating nul as well. */
1578 memmove(dst, src, strlen(src)+1);
1582 /*********************************************************************
1584 * Function : normalize_lws
1586 * Description : Reduces unquoted linear white space in headers
1587 * to a single space in accordance with RFC 2616 2.2.
1588 * This simplifies parsing and filtering later on.
1590 * XXX: Remove log messages before
1591 * the next stable release?
1594 * 1 : header = A header with linear white space to reduce.
1598 *********************************************************************/
1599 static void normalize_lws(char *header)
1605 if (ijb_isspace(*p) && ijb_isspace(*(p+1)))
1609 while (ijb_isspace(*q))
1613 log_error(LOG_LEVEL_HEADER, "Reducing white space in '%s'", header);
1614 string_move(p+1, q);
1619 log_error(LOG_LEVEL_HEADER,
1620 "Converting tab to space in '%s'", header);
1625 char *end_of_token = strstr(p+1, "\"");
1627 if (NULL != end_of_token)
1629 /* Don't mess with quoted text. */
1634 log_error(LOG_LEVEL_HEADER,
1635 "Ignoring single quote in '%s'", header);
1641 p = strchr(header, ':');
1642 if ((p != NULL) && (p != header) && ijb_isspace(*(p-1)))
1645 * There's still space before the colon.
1648 string_move(p-1, p);
1653 /*********************************************************************
1655 * Function : get_header
1657 * Description : This (odd) routine will parse the csp->iob
1658 * to get the next complete header.
1661 * 1 : iob = The I/O buffer to parse, usually csp->iob.
1663 * Returns : Any one of the following:
1665 * 1) a pointer to a dynamically allocated string that contains a header line
1666 * 2) NULL indicating that the end of the header was reached
1667 * 3) "" indicating that the end of the iob was reached before finding
1668 * a complete header line.
1670 *********************************************************************/
1671 char *get_header(struct iob *iob)
1675 header = get_header_line(iob);
1677 if ((header == NULL) || (*header == '\0'))
1680 * No complete header read yet, tell the client.
1685 while ((iob->cur[0] == ' ') || (iob->cur[0] == '\t'))
1688 * Header spans multiple lines, append the next one.
1690 char *continued_header;
1692 continued_header = get_header_line(iob);
1693 if ((continued_header == NULL) || (*continued_header == '\0'))
1696 * No complete header read yet, return what we got.
1697 * XXX: Should "unread" header instead.
1699 log_error(LOG_LEVEL_INFO,
1700 "Failed to read a multi-line header properly: '%s'",
1705 if (JB_ERR_OK != string_join(&header, continued_header))
1707 log_error(LOG_LEVEL_FATAL,
1708 "Out of memory while appending multiple headers.");
1712 /* XXX: remove before next stable release. */
1713 log_error(LOG_LEVEL_HEADER,
1714 "Merged multiple header lines to: '%s'",
1719 normalize_lws(header);
1726 /*********************************************************************
1728 * Function : get_header_line
1730 * Description : This (odd) routine will parse the csp->iob
1731 * to get the next header line.
1734 * 1 : iob = The I/O buffer to parse, usually csp->iob.
1736 * Returns : Any one of the following:
1738 * 1) a pointer to a dynamically allocated string that contains a header line
1739 * 2) NULL indicating that the end of the header was reached
1740 * 3) "" indicating that the end of the iob was reached before finding
1741 * a complete header line.
1743 *********************************************************************/
1744 static char *get_header_line(struct iob *iob)
1748 if ((iob->cur == NULL)
1749 || ((p = strchr(iob->cur, '\n')) == NULL))
1751 return(""); /* couldn't find a complete header */
1756 ret = strdup(iob->cur);
1759 /* FIXME No way to handle error properly */
1760 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header_line()");
1762 assert(ret != NULL);
1766 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
1768 /* is this a blank line (i.e. the end of the header) ? */
1780 /*********************************************************************
1782 * Function : get_header_value
1784 * Description : Get the value of a given header from a chained list
1785 * of header lines or return NULL if no such header is
1786 * present in the list.
1789 * 1 : header_list = pointer to list
1790 * 2 : header_name = string with name of header to look for.
1791 * Trailing colon required, capitalization
1794 * Returns : NULL if not found, else value of header
1796 *********************************************************************/
1797 char *get_header_value(const struct list *header_list, const char *header_name)
1799 struct list_entry *cur_entry;
1803 assert(header_list);
1804 assert(header_name);
1805 length = strlen(header_name);
1807 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
1811 if (!strncmpic(cur_entry->str, header_name, length))
1814 * Found: return pointer to start of value
1816 ret = cur_entry->str + length;
1817 while (*ret && ijb_isspace(*ret)) ret++;
1831 /*********************************************************************
1833 * Function : scan_headers
1835 * Description : Scans headers, applies tags and updates action bits.
1838 * 1 : csp = Current client state (buffers, headers, etc...)
1840 * Returns : JB_ERR_OK
1842 *********************************************************************/
1843 static jb_err scan_headers(struct client_state *csp)
1845 struct list_entry *h; /* Header */
1846 jb_err err = JB_ERR_OK;
1848 for (h = csp->headers->first; (err == JB_ERR_OK) && (h != NULL) ; h = h->next)
1850 /* Header crunch()ed in previous run? -> ignore */
1851 if (h->str == NULL) continue;
1852 log_error(LOG_LEVEL_HEADER, "scan: %s", h->str);
1853 err = header_tagger(csp, h->str);
1860 /*********************************************************************
1864 * Description : add, delete or modify lines in the HTTP header streams.
1865 * On entry, it receives a linked list of headers space
1866 * that was allocated dynamically (both the list nodes
1867 * and the header contents).
1869 * As a side effect it frees the space used by the original
1873 * 1 : csp = Current client state (buffers, headers, etc...)
1874 * 2 : filter_server_headers = Boolean to switch between
1875 * server and header filtering.
1877 * Returns : JB_ERR_OK in case off success, or
1878 * JB_ERR_MEMORY on out-of-memory error.
1880 *********************************************************************/
1881 jb_err sed(struct client_state *csp, int filter_server_headers)
1883 /* XXX: use more descriptive names. */
1884 struct list_entry *p;
1885 const struct parsers *v;
1886 const add_header_func_ptr *f;
1887 jb_err err = JB_ERR_OK;
1889 if (filter_server_headers)
1891 v = server_patterns;
1892 f = add_server_headers;
1896 v = client_patterns;
1897 f = add_client_headers;
1902 while ((err == JB_ERR_OK) && (v->str != NULL))
1904 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL); p = p->next)
1906 /* Header crunch()ed in previous run? -> ignore */
1907 if (p->str == NULL) continue;
1909 /* Does the current parser handle this header? */
1910 if ((strncmpic(p->str, v->str, v->len) == 0) ||
1911 (v->len == CHECK_EVERY_HEADER_REMAINING))
1913 err = v->parser(csp, &(p->str));
1919 /* place additional headers on the csp->headers list */
1920 while ((err == JB_ERR_OK) && (*f))
1930 /*********************************************************************
1932 * Function : update_server_headers
1934 * Description : Updates server headers after the body has been modified.
1937 * 1 : csp = Current client state (buffers, headers, etc...)
1939 * Returns : JB_ERR_OK in case off success, or
1940 * JB_ERR_MEMORY on out-of-memory error.
1942 *********************************************************************/
1943 jb_err update_server_headers(struct client_state *csp)
1945 jb_err err = JB_ERR_OK;
1947 static const struct parsers server_patterns_light[] = {
1948 { "Content-Length:", 15, server_adjust_content_length },
1949 { "Transfer-Encoding:", 18, server_transfer_coding },
1951 { "Content-Encoding:", 17, server_content_encoding },
1952 #endif /* def FEATURE_ZLIB */
1956 if (strncmpic(csp->http->cmd, "HEAD", 4))
1958 const struct parsers *v;
1959 struct list_entry *p;
1961 for (v = server_patterns_light; (err == JB_ERR_OK) && (v->str != NULL); v++)
1963 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL); p = p->next)
1965 /* Header crunch()ed in previous run? -> ignore */
1966 if (p->str == NULL) continue;
1968 /* Does the current parser handle this header? */
1969 if (strncmpic(p->str, v->str, v->len) == 0)
1971 err = v->parser(csp, (char **)&(p->str));
1981 /*********************************************************************
1983 * Function : header_tagger
1985 * Description : Executes all text substitutions from applying
1986 * tag actions and saves the result as tag.
1988 * XXX: Shares enough code with filter_header() and
1989 * pcrs_filter_response() to warrant some helper functions.
1992 * 1 : csp = Current client state (buffers, headers, etc...)
1993 * 2 : header = Header that is used as tagger input
1995 * Returns : JB_ERR_OK on success and always succeeds
1997 *********************************************************************/
1998 static jb_err header_tagger(struct client_state *csp, char *header)
2000 int wanted_filter_type;
2001 int multi_action_index;
2005 struct file_list *fl;
2006 struct re_filterfile_spec *b;
2007 struct list_entry *tag_name;
2009 int found_filters = 0;
2010 const size_t header_length = strlen(header);
2012 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
2014 wanted_filter_type = FT_SERVER_HEADER_TAGGER;
2015 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
2019 wanted_filter_type = FT_CLIENT_HEADER_TAGGER;
2020 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
2023 /* Check if there are any filters */
2024 for (i = 0; i < MAX_AF_FILES; i++)
2037 if (0 == found_filters)
2039 log_error(LOG_LEVEL_ERROR, "Inconsistent configuration: "
2040 "tagging enabled, but no taggers available.");
2044 for (i = 0; i < MAX_AF_FILES; i++)
2047 if ((NULL == fl) || (NULL == fl->f))
2050 * Either there are no filter files
2051 * left, or this filter file just
2052 * contains no valid filters.
2054 * Continue to be sure we don't miss
2055 * valid filter files that are chained
2056 * after empty or invalid ones.
2061 /* For all filters, */
2062 for (b = fl->f; b; b = b->next)
2064 if (b->type != wanted_filter_type)
2066 /* skip the ones we don't care about, */
2069 /* leaving only taggers that could apply, of which we use the ones, */
2070 for (tag_name = csp->action->multi[multi_action_index]->first;
2071 NULL != tag_name; tag_name = tag_name->next)
2073 /* that do apply, and */
2074 if (strcmp(b->name, tag_name->str) == 0)
2076 char *modified_tag = NULL;
2078 size_t size = header_length;
2079 pcrs_job *joblist = b->joblist;
2081 if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
2083 if (NULL == joblist)
2085 log_error(LOG_LEVEL_RE_FILTER,
2086 "Tagger %s has empty joblist. Nothing to do.", b->name);
2090 /* execute their pcrs_joblist on the header. */
2091 for (job = joblist; NULL != job; job = job->next)
2093 const int hits = pcrs_execute(job, tag, size, &modified_tag, &size);
2097 /* Success, continue with the modified version. */
2106 /* Tagger doesn't match */
2109 /* Regex failure, log it but continue anyway. */
2110 assert(NULL != header);
2111 log_error(LOG_LEVEL_ERROR,
2112 "Problems with tagger \'%s\' and header \'%s\': %s",
2113 b->name, *header, pcrs_strerror(hits));
2115 freez(modified_tag);
2119 if (b->dynamic) pcrs_free_joblist(joblist);
2121 /* If this tagger matched */
2127 * There is to technical limitation which makes
2128 * it impossible to use empty tags, but I assume
2129 * no one would do it intentionally.
2132 log_error(LOG_LEVEL_INFO,
2133 "Tagger \'%s\' created an empty tag. Ignored.",
2138 if (!list_contains_item(csp->tags, tag))
2140 if (JB_ERR_OK != enlist(csp->tags, tag))
2142 log_error(LOG_LEVEL_ERROR,
2143 "Insufficient memory to add tag \'%s\', "
2144 "based on tagger \'%s\' and header \'%s\'",
2145 tag, b->name, *header);
2149 char *action_message;
2151 * update the action bits right away, to make
2152 * tagging based on tags set by earlier taggers
2153 * of the same kind possible.
2155 if (update_action_bits_for_tag(csp, tag))
2157 action_message = "Action bits updated accordingly.";
2161 action_message = "No action bits update necessary.";
2164 log_error(LOG_LEVEL_HEADER,
2165 "Tagger \'%s\' added tag \'%s\'. %s",
2166 b->name, tag, action_message);
2171 /* XXX: Is this log-worthy? */
2172 log_error(LOG_LEVEL_HEADER,
2173 "Tagger \'%s\' didn't add tag \'%s\'. "
2174 "Tag already present", b->name, tag);
2177 } /* if the tagger matched */
2178 } /* if the tagger applies */
2179 } /* for every tagger that could apply */
2180 } /* for all filters */
2181 } /* for all filter files */
2186 /* here begins the family of parser functions that reformat header lines */
2188 /*********************************************************************
2190 * Function : filter_header
2192 * Description : Executes all text substitutions from all applying
2193 * +(server|client)-header-filter actions on the header.
2194 * Most of the code was copied from pcrs_filter_response,
2195 * including the rather short variable names
2198 * 1 : csp = Current client state (buffers, headers, etc...)
2199 * 2 : header = On input, pointer to header to modify.
2200 * On output, pointer to the modified header, or NULL
2201 * to remove the header. This function frees the
2202 * original string if necessary.
2204 * Returns : JB_ERR_OK on success and always succeeds
2206 *********************************************************************/
2207 static jb_err filter_header(struct client_state *csp, char **header)
2211 size_t size = strlen(*header);
2213 char *newheader = NULL;
2216 struct file_list *fl;
2217 struct re_filterfile_spec *b;
2218 struct list_entry *filtername;
2220 int i, found_filters = 0;
2221 int wanted_filter_type;
2222 int multi_action_index;
2224 if (csp->flags & CSP_FLAG_NO_FILTERING)
2229 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
2231 wanted_filter_type = FT_SERVER_HEADER_FILTER;
2232 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
2236 wanted_filter_type = FT_CLIENT_HEADER_FILTER;
2237 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
2241 * Need to check the set of re_filterfiles...
2243 for (i = 0; i < MAX_AF_FILES; i++)
2256 if (0 == found_filters)
2258 log_error(LOG_LEVEL_ERROR, "Inconsistent configuration: "
2259 "header filtering enabled, but no matching filters available.");
2263 for (i = 0; i < MAX_AF_FILES; i++)
2266 if ((NULL == fl) || (NULL == fl->f))
2269 * Either there are no filter files
2270 * left, or this filter file just
2271 * contains no valid filters.
2273 * Continue to be sure we don't miss
2274 * valid filter files that are chained
2275 * after empty or invalid ones.
2280 * For all applying +filter actions, look if a filter by that
2281 * name exists and if yes, execute its pcrs_joblist on the
2284 for (b = fl->f; b; b = b->next)
2286 if (b->type != wanted_filter_type)
2288 /* Skip other filter types */
2292 for (filtername = csp->action->multi[multi_action_index]->first;
2293 filtername ; filtername = filtername->next)
2295 if (strcmp(b->name, filtername->str) == 0)
2297 int current_hits = 0;
2298 pcrs_job *joblist = b->joblist;
2300 if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
2302 if (NULL == joblist)
2304 log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
2308 log_error(LOG_LEVEL_RE_FILTER, "filtering \'%s\' (size %d) with \'%s\' ...",
2309 *header, size, b->name);
2311 /* Apply all jobs from the joblist */
2312 for (job = joblist; NULL != job; job = job->next)
2314 matches = pcrs_execute(job, *header, size, &newheader, &size);
2317 current_hits += matches;
2318 log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
2320 *header = newheader;
2322 else if ( 0 == matches )
2324 /* Filter doesn't change header */
2330 log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
2331 *header, b->name, pcrs_strerror(matches));
2332 if (newheader != NULL)
2334 log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
2340 if (b->dynamic) pcrs_free_joblist(joblist);
2342 log_error(LOG_LEVEL_RE_FILTER, "... produced %d hits (new size %d).", current_hits, size);
2343 hits += current_hits;
2350 * Additionally checking for hits is important because if
2351 * the continue hack is triggered, server headers can
2352 * arrive empty to separate multiple heads from each other.
2354 if ((0 == size) && hits)
2356 log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
2364 /*********************************************************************
2366 * Function : server_connection
2368 * Description : Makes sure that the value of the Connection: header
2369 * is "close" and signals server_connection_close_adder
2373 * 1 : csp = Current client state (buffers, headers, etc...)
2374 * 2 : header = On input, pointer to header to modify.
2375 * On output, pointer to the modified header, or NULL
2376 * to remove the header. This function frees the
2377 * original string if necessary.
2379 * Returns : JB_ERR_OK on success, or
2380 * JB_ERR_MEMORY on out-of-memory error.
2382 *********************************************************************/
2383 static jb_err server_connection(struct client_state *csp, char **header)
2385 char *old_header = *header;
2387 /* Do we have a 'Connection: close' header? */
2388 if (strcmpic(*header, "Connection: close"))
2390 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2391 if ((csp->config->feature_flags &
2392 RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
2393 && !strcmpic(*header, "Connection: keep-alive"))
2395 /* Remember to keep the connection alive. */
2396 csp->flags |= CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE;
2398 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
2400 *header = strdup("Connection: close");
2403 return JB_ERR_MEMORY;
2405 log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
2409 /* Signal server_connection_close_adder() to return early. */
2410 csp->flags |= CSP_FLAG_SERVER_CONNECTION_CLOSE_SET;
2415 /*********************************************************************
2417 * Function : client_connection
2419 * Description : Makes sure a proper "Connection:" header is
2420 * set and signals connection_header_adder
2424 * 1 : csp = Current client state (buffers, headers, etc...)
2425 * 2 : header = On input, pointer to header to modify.
2426 * On output, pointer to the modified header, or NULL
2427 * to remove the header. This function frees the
2428 * original string if necessary.
2430 * Returns : JB_ERR_OK on success, or
2431 * JB_ERR_MEMORY on out-of-memory error.
2433 *********************************************************************/
2434 static jb_err client_connection(struct client_state *csp, char **header)
2436 char *old_header = *header;
2437 const char *wanted_header = get_appropiate_connection_header(csp);
2439 if (strcmpic(*header, wanted_header))
2441 *header = strdup(wanted_header);
2444 return JB_ERR_MEMORY;
2446 log_error(LOG_LEVEL_HEADER,
2447 "Replaced: \'%s\' with \'%s\'", old_header, *header);
2451 /* Signal client_connection_close_adder() to return early. */
2452 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_HEADER_SET;
2458 /*********************************************************************
2460 * Function : crumble
2462 * Description : This is called if a header matches a pattern to "crunch"
2465 * 1 : csp = Current client state (buffers, headers, etc...)
2466 * 2 : header = On input, pointer to header to modify.
2467 * On output, pointer to the modified header, or NULL
2468 * to remove the header. This function frees the
2469 * original string if necessary.
2471 * Returns : JB_ERR_OK on success, or
2472 * JB_ERR_MEMORY on out-of-memory error.
2474 *********************************************************************/
2475 static jb_err crumble(struct client_state *csp, char **header)
2478 log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
2484 /*********************************************************************
2486 * Function : crunch_server_header
2488 * Description : Crunch server header if it matches a string supplied by the
2489 * user. Called from `sed'.
2492 * 1 : csp = Current client state (buffers, headers, etc...)
2493 * 2 : header = On input, pointer to header to modify.
2494 * On output, pointer to the modified header, or NULL
2495 * to remove the header. This function frees the
2496 * original string if necessary.
2498 * Returns : JB_ERR_OK on success and always succeeds
2500 *********************************************************************/
2501 static jb_err crunch_server_header(struct client_state *csp, char **header)
2503 const char *crunch_pattern;
2505 /* Do we feel like crunching? */
2506 if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
2508 crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
2510 /* Is the current header the lucky one? */
2511 if (strstr(*header, crunch_pattern))
2513 log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
2522 /*********************************************************************
2524 * Function : server_content_type
2526 * Description : Set the content-type for filterable types (text/.*,
2527 * .*xml.*, javascript and image/gif) unless filtering has been
2528 * forbidden (CT_TABOO) while parsing earlier headers.
2529 * NOTE: Since text/plain is commonly used by web servers
2530 * for files whose correct type is unknown, we don't
2531 * set CT_TEXT for it.
2534 * 1 : csp = Current client state (buffers, headers, etc...)
2535 * 2 : header = On input, pointer to header to modify.
2536 * On output, pointer to the modified header, or NULL
2537 * to remove the header. This function frees the
2538 * original string if necessary.
2540 * Returns : JB_ERR_OK on success, or
2541 * JB_ERR_MEMORY on out-of-memory error.
2543 *********************************************************************/
2544 static jb_err server_content_type(struct client_state *csp, char **header)
2546 /* Remove header if it isn't the first Content-Type header */
2547 if ((csp->content_type & CT_DECLARED))
2550 * Another, slightly slower, way to see if
2551 * we already parsed another Content-Type header.
2553 assert(NULL != get_header_value(csp->headers, "Content-Type:"));
2555 log_error(LOG_LEVEL_ERROR,
2556 "Multiple Content-Type headers. Removing and ignoring: \'%s\'",
2564 * Signal that the Content-Type has been set.
2566 csp->content_type |= CT_DECLARED;
2568 if (!(csp->content_type & CT_TABOO))
2571 * XXX: The assumption that text/plain is a sign of
2572 * binary data seems to be somewhat unreasonable nowadays
2573 * and should be dropped after 3.0.8 is out.
2575 if ((strstr(*header, "text/") && !strstr(*header, "plain"))
2576 || strstr(*header, "xml")
2577 || strstr(*header, "application/x-javascript"))
2579 csp->content_type |= CT_TEXT;
2581 else if (strstr(*header, "image/gif"))
2583 csp->content_type |= CT_GIF;
2588 * Are we messing with the content type?
2590 if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
2593 * Make sure the user doesn't accidently
2594 * change the content type of binary documents.
2596 if ((csp->content_type & CT_TEXT) || (csp->action->flags & ACTION_FORCE_TEXT_MODE))
2599 *header = strdup("Content-Type: ");
2600 string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
2604 log_error(LOG_LEVEL_HEADER, "Insufficient memory to replace Content-Type!");
2605 return JB_ERR_MEMORY;
2607 log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
2611 log_error(LOG_LEVEL_HEADER, "%s not replaced. "
2612 "It doesn't look like a content type that should be filtered. "
2613 "Enable force-text-mode if you know what you're doing.", *header);
2621 /*********************************************************************
2623 * Function : server_transfer_coding
2625 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
2626 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
2627 * - Remove header if body was chunked but has been
2628 * de-chunked for filtering.
2631 * 1 : csp = Current client state (buffers, headers, etc...)
2632 * 2 : header = On input, pointer to header to modify.
2633 * On output, pointer to the modified header, or NULL
2634 * to remove the header. This function frees the
2635 * original string if necessary.
2637 * Returns : JB_ERR_OK on success, or
2638 * JB_ERR_MEMORY on out-of-memory error.
2640 *********************************************************************/
2641 static jb_err server_transfer_coding(struct client_state *csp, char **header)
2644 * Turn off pcrs and gif filtering if body compressed
2646 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2650 * XXX: Added to test if we could use CT_GZIP and CT_DEFLATE here.
2652 log_error(LOG_LEVEL_INFO, "Marking content type for %s as CT_TABOO because of %s.",
2653 csp->http->cmd, *header);
2654 #endif /* def FEATURE_ZLIB */
2655 csp->content_type = CT_TABOO;
2659 * Raise flag if body chunked
2661 if (strstr(*header, "chunked"))
2663 csp->flags |= CSP_FLAG_CHUNKED;
2666 * If the body was modified, it has been de-chunked first
2667 * and the header must be removed.
2669 * FIXME: If there is more than one transfer encoding,
2670 * only the "chunked" part should be removed here.
2672 if (csp->flags & CSP_FLAG_MODIFIED)
2674 log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
2683 /*********************************************************************
2685 * Function : server_content_encoding
2687 * Description : This function is run twice for each request,
2688 * unless FEATURE_ZLIB and filtering are disabled.
2690 * The first run is used to check if the content
2691 * is compressed, if FEATURE_ZLIB is disabled
2692 * filtering is then disabled as well, if FEATURE_ZLIB
2693 * is enabled the content is marked for decompression.
2695 * The second run is used to remove the Content-Encoding
2696 * header if the decompression was successful.
2699 * 1 : csp = Current client state (buffers, headers, etc...)
2700 * 2 : header = On input, pointer to header to modify.
2701 * On output, pointer to the modified header, or NULL
2702 * to remove the header. This function frees the
2703 * original string if necessary.
2705 * Returns : JB_ERR_OK on success, or
2706 * JB_ERR_MEMORY on out-of-memory error.
2708 *********************************************************************/
2709 static jb_err server_content_encoding(struct client_state *csp, char **header)
2712 if ((csp->flags & CSP_FLAG_MODIFIED)
2713 && (csp->content_type & (CT_GZIP | CT_DEFLATE)))
2716 * We successfully decompressed the content,
2717 * and have to clean the header now, so the
2718 * client no longer expects compressed data..
2720 * XXX: There is a difference between cleaning
2721 * and removing it completely.
2723 log_error(LOG_LEVEL_HEADER, "Crunching: %s", *header);
2726 else if (strstr(*header, "gzip"))
2728 /* Mark for gzip decompression */
2729 csp->content_type |= CT_GZIP;
2731 else if (strstr(*header, "deflate"))
2733 /* Mark for zlib decompression */
2734 csp->content_type |= CT_DEFLATE;
2736 else if (strstr(*header, "compress"))
2739 * We can't decompress this; therefore we can't filter
2742 csp->content_type |= CT_TABOO;
2744 #else /* !defined(FEATURE_ZLIB) */
2745 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2748 * Body is compressed, turn off pcrs and gif filtering.
2750 csp->content_type |= CT_TABOO;
2753 * Log a warning if the user expects the content to be filtered.
2755 if ((csp->rlist != NULL) &&
2756 (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
2758 log_error(LOG_LEVEL_INFO,
2759 "Compressed content detected, content filtering disabled. "
2760 "Consider recompiling Privoxy with zlib support or "
2761 "enable the prevent-compression action.");
2764 #endif /* defined(FEATURE_ZLIB) */
2771 /*********************************************************************
2773 * Function : server_adjust_content_length
2775 * Description : Adjust Content-Length header if we modified
2779 * 1 : csp = Current client state (buffers, headers, etc...)
2780 * 2 : header = On input, pointer to header to modify.
2781 * On output, pointer to the modified header, or NULL
2782 * to remove the header. This function frees the
2783 * original string if necessary.
2785 * Returns : JB_ERR_OK on success, or
2786 * JB_ERR_MEMORY on out-of-memory error.
2788 *********************************************************************/
2789 static jb_err server_adjust_content_length(struct client_state *csp, char **header)
2791 const size_t max_header_length = 80;
2793 /* Regenerate header if the content was modified. */
2794 if (csp->flags & CSP_FLAG_MODIFIED)
2797 *header = (char *) zalloc(max_header_length);
2798 if (*header == NULL)
2800 return JB_ERR_MEMORY;
2803 snprintf(*header, max_header_length, "Content-Length: %d",
2804 (int)csp->content_length);
2805 log_error(LOG_LEVEL_HEADER, "Adjusted Content-Length to %d",
2806 (int)csp->content_length);
2813 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2814 /*********************************************************************
2816 * Function : server_save_content_length
2818 * Description : Save the Content-Length sent by the server.
2821 * 1 : csp = Current client state (buffers, headers, etc...)
2822 * 2 : header = On input, pointer to header to modify.
2823 * On output, pointer to the modified header, or NULL
2824 * to remove the header. This function frees the
2825 * original string if necessary.
2827 * Returns : JB_ERR_OK on success, or
2828 * JB_ERR_MEMORY on out-of-memory error.
2830 *********************************************************************/
2831 static jb_err server_save_content_length(struct client_state *csp, char **header)
2833 unsigned long long content_length = 0;
2835 assert(*(*header+14) == ':');
2837 if (1 != sscanf(*header+14, ": %llu", &content_length))
2839 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
2844 csp->expected_content_length = content_length;
2845 csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
2850 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2853 /*********************************************************************
2855 * Function : server_content_md5
2857 * Description : Crumble any Content-MD5 headers if the document was
2858 * modified. FIXME: Should we re-compute instead?
2861 * 1 : csp = Current client state (buffers, headers, etc...)
2862 * 2 : header = On input, pointer to header to modify.
2863 * On output, pointer to the modified header, or NULL
2864 * to remove the header. This function frees the
2865 * original string if necessary.
2867 * Returns : JB_ERR_OK on success, or
2868 * JB_ERR_MEMORY on out-of-memory error.
2870 *********************************************************************/
2871 static jb_err server_content_md5(struct client_state *csp, char **header)
2873 if (csp->flags & CSP_FLAG_MODIFIED)
2875 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
2883 /*********************************************************************
2885 * Function : server_content_disposition
2887 * Description : If enabled, blocks or modifies the "Content-Disposition" header.
2888 * Called from `sed'.
2891 * 1 : csp = Current client state (buffers, headers, etc...)
2892 * 2 : header = On input, pointer to header to modify.
2893 * On output, pointer to the modified header, or NULL
2894 * to remove the header. This function frees the
2895 * original string if necessary.
2897 * Returns : JB_ERR_OK on success, or
2898 * JB_ERR_MEMORY on out-of-memory error.
2900 *********************************************************************/
2901 static jb_err server_content_disposition(struct client_state *csp, char **header)
2906 * Are we messing with the Content-Disposition header?
2908 if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
2914 newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
2916 if ((newval == NULL) || (0 == strcmpic(newval, "block")))
2919 * Blocking content-disposition header
2921 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2928 * Replacing Content-Disposition header
2931 *header = strdup("Content-Disposition: ");
2932 string_append(header, newval);
2934 if (*header != NULL)
2936 log_error(LOG_LEVEL_HEADER,
2937 "Content-Disposition header crunched and replaced with: %s", *header);
2940 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2944 /*********************************************************************
2946 * Function : server_last_modified
2948 * Description : Changes Last-Modified header to the actual date
2949 * to help hide-if-modified-since.
2950 * Called from `sed'.
2953 * 1 : csp = Current client state (buffers, headers, etc...)
2954 * 2 : header = On input, pointer to header to modify.
2955 * On output, pointer to the modified header, or NULL
2956 * to remove the header. This function frees the
2957 * original string if necessary.
2959 * Returns : JB_ERR_OK on success, or
2960 * JB_ERR_MEMORY on out-of-memory error.
2962 *********************************************************************/
2963 static jb_err server_last_modified(struct client_state *csp, char **header)
2966 char buf[BUFFER_SIZE];
2969 #ifdef HAVE_GMTIME_R
2972 struct tm *timeptr = NULL;
2973 time_t now, last_modified;
2975 long int days, hours, minutes, seconds;
2978 * Are we messing with the Last-Modified header?
2980 if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
2986 newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
2988 if (0 == strcmpic(newval, "block") )
2991 * Blocking Last-Modified header. Useless but why not.
2993 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2997 else if (0 == strcmpic(newval, "reset-to-request-time"))
3000 * Setting Last-Modified Header to now.
3002 get_http_time(0, buf, sizeof(buf));
3004 *header = strdup("Last-Modified: ");
3005 string_append(header, buf);
3007 if (*header == NULL)
3009 log_error(LOG_LEVEL_HEADER, "Insufficient memory. Last-Modified header got lost, boohoo.");
3013 log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
3016 else if (0 == strcmpic(newval, "randomize"))
3018 const char *header_time = *header + sizeof("Last-Modified:");
3020 log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
3022 #ifdef HAVE_GMTIME_R
3023 timeptr = gmtime_r(&now, &gmt);
3024 #elif FEATURE_PTHREAD
3025 privoxy_mutex_lock(&gmtime_mutex);
3026 timeptr = gmtime(&now);
3027 privoxy_mutex_unlock(&gmtime_mutex);
3029 timeptr = gmtime(&now);
3031 if (JB_ERR_OK != parse_header_time(header_time, &last_modified))
3033 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
3038 rtime = (long int)difftime(now, last_modified);
3047 log_error(LOG_LEVEL_HEADER, "Server time in the future.");
3049 rtime = pick_from_range(rtime);
3050 if (negative) rtime *= -1;
3051 last_modified += rtime;
3052 #ifdef HAVE_GMTIME_R
3053 timeptr = gmtime_r(&last_modified, &gmt);
3054 #elif FEATURE_PTHREAD
3055 privoxy_mutex_lock(&gmtime_mutex);
3056 timeptr = gmtime(&last_modified);
3057 privoxy_mutex_unlock(&gmtime_mutex);
3059 timeptr = gmtime(&last_modified);
3061 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
3063 *header = strdup("Last-Modified: ");
3064 string_append(header, newheader);
3066 if (*header == NULL)
3068 log_error(LOG_LEVEL_ERROR, "Insufficient memory, header crunched without replacement.");
3069 return JB_ERR_MEMORY;
3072 days = rtime / (3600 * 24);
3073 hours = rtime / 3600 % 24;
3074 minutes = rtime / 60 % 60;
3075 seconds = rtime % 60;
3077 log_error(LOG_LEVEL_HEADER,
3078 "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
3079 *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
3080 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
3084 log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
3093 /*********************************************************************
3095 * Function : client_accept_encoding
3097 * Description : Rewrite the client's Accept-Encoding header so that
3098 * if doesn't allow compression, if the action applies.
3099 * Note: For HTTP/1.0 the absence of the header is enough.
3102 * 1 : csp = Current client state (buffers, headers, etc...)
3103 * 2 : header = On input, pointer to header to modify.
3104 * On output, pointer to the modified header, or NULL
3105 * to remove the header. This function frees the
3106 * original string if necessary.
3108 * Returns : JB_ERR_OK on success, or
3109 * JB_ERR_MEMORY on out-of-memory error.
3111 *********************************************************************/
3112 static jb_err client_accept_encoding(struct client_state *csp, char **header)
3114 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
3116 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
3120 /* Temporarily disable the correct behaviour to
3121 * work around a PHP bug.
3123 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
3125 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
3126 * if (*header == NULL)
3128 * return JB_ERR_MEMORY;
3139 /*********************************************************************
3141 * Function : client_te
3143 * Description : Rewrite the client's TE header so that
3144 * if doesn't allow compression, if the action applies.
3147 * 1 : csp = Current client state (buffers, headers, etc...)
3148 * 2 : header = On input, pointer to header to modify.
3149 * On output, pointer to the modified header, or NULL
3150 * to remove the header. This function frees the
3151 * original string if necessary.
3153 * Returns : JB_ERR_OK on success, or
3154 * JB_ERR_MEMORY on out-of-memory error.
3156 *********************************************************************/
3157 static jb_err client_te(struct client_state *csp, char **header)
3159 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
3162 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
3169 /*********************************************************************
3171 * Function : client_referrer
3173 * Description : Handle the "referer" config setting properly.
3174 * Called from `sed'.
3177 * 1 : csp = Current client state (buffers, headers, etc...)
3178 * 2 : header = On input, pointer to header to modify.
3179 * On output, pointer to the modified header, or NULL
3180 * to remove the header. This function frees the
3181 * original string if necessary.
3183 * Returns : JB_ERR_OK on success, or
3184 * JB_ERR_MEMORY on out-of-memory error.
3186 *********************************************************************/
3187 static jb_err client_referrer(struct client_state *csp, char **header)
3189 const char *parameter;
3190 /* booleans for parameters we have to check multiple times */
3191 int parameter_conditional_block;
3192 int parameter_conditional_forge;
3194 #ifdef FEATURE_FORCE_LOAD
3196 * Since the referrer can include the prefix even
3197 * if the request itself is non-forced, we must
3198 * clean it unconditionally.
3200 * XXX: strclean is too broad
3202 strclean(*header, FORCE_PREFIX);
3203 #endif /* def FEATURE_FORCE_LOAD */
3205 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
3207 /* Nothing left to do */
3211 parameter = csp->action->string[ACTION_STRING_REFERER];
3212 assert(parameter != NULL);
3213 parameter_conditional_block = (0 == strcmpic(parameter, "conditional-block"));
3214 parameter_conditional_forge = (0 == strcmpic(parameter, "conditional-forge"));
3216 if (!parameter_conditional_block && !parameter_conditional_forge)
3219 * As conditional-block and conditional-forge are the only
3220 * parameters that rely on the original referrer, we can
3221 * remove it now for all the others.
3226 if (0 == strcmpic(parameter, "block"))
3228 log_error(LOG_LEVEL_HEADER, "Referer crunched!");
3231 else if (parameter_conditional_block || parameter_conditional_forge)
3233 return handle_conditional_hide_referrer_parameter(header,
3234 csp->http->hostport, parameter_conditional_block);
3236 else if (0 == strcmpic(parameter, "forge"))
3238 return create_forged_referrer(header, csp->http->hostport);
3242 /* interpret parameter as user-supplied referer to fake */
3243 return create_fake_referrer(header, parameter);
3248 /*********************************************************************
3250 * Function : client_accept_language
3252 * Description : Handle the "Accept-Language" config setting properly.
3253 * Called from `sed'.
3256 * 1 : csp = Current client state (buffers, headers, etc...)
3257 * 2 : header = On input, pointer to header to modify.
3258 * On output, pointer to the modified header, or NULL
3259 * to remove the header. This function frees the
3260 * original string if necessary.
3262 * Returns : JB_ERR_OK on success, or
3263 * JB_ERR_MEMORY on out-of-memory error.
3265 *********************************************************************/
3266 static jb_err client_accept_language(struct client_state *csp, char **header)
3271 * Are we messing with the Accept-Language?
3273 if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
3275 /*I don't think so*/
3279 newval = csp->action->string[ACTION_STRING_LANGUAGE];
3281 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
3284 * Blocking Accept-Language header
3286 log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
3293 * Replacing Accept-Language header
3296 *header = strdup("Accept-Language: ");
3297 string_append(header, newval);
3299 if (*header == NULL)
3301 log_error(LOG_LEVEL_ERROR,
3302 "Insufficient memory. Accept-Language header crunched without replacement.");
3306 log_error(LOG_LEVEL_HEADER,
3307 "Accept-Language header crunched and replaced with: %s", *header);
3310 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3314 /*********************************************************************
3316 * Function : crunch_client_header
3318 * Description : Crunch client header if it matches a string supplied by the
3319 * user. Called from `sed'.
3322 * 1 : csp = Current client state (buffers, headers, etc...)
3323 * 2 : header = On input, pointer to header to modify.
3324 * On output, pointer to the modified header, or NULL
3325 * to remove the header. This function frees the
3326 * original string if necessary.
3328 * Returns : JB_ERR_OK on success and always succeeds
3330 *********************************************************************/
3331 static jb_err crunch_client_header(struct client_state *csp, char **header)
3333 const char *crunch_pattern;
3335 /* Do we feel like crunching? */
3336 if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
3338 crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
3340 /* Is the current header the lucky one? */
3341 if (strstr(*header, crunch_pattern))
3343 log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
3351 /*********************************************************************
3353 * Function : client_uagent
3355 * Description : Handle the "user-agent" config setting properly
3356 * and remember its original value to enable browser
3357 * bug workarounds. Called from `sed'.
3360 * 1 : csp = Current client state (buffers, headers, etc...)
3361 * 2 : header = On input, pointer to header to modify.
3362 * On output, pointer to the modified header, or NULL
3363 * to remove the header. This function frees the
3364 * original string if necessary.
3366 * Returns : JB_ERR_OK on success, or
3367 * JB_ERR_MEMORY on out-of-memory error.
3369 *********************************************************************/
3370 static jb_err client_uagent(struct client_state *csp, char **header)
3374 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
3379 newval = csp->action->string[ACTION_STRING_USER_AGENT];
3386 *header = strdup("User-Agent: ");
3387 string_append(header, newval);
3389 log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
3391 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3395 /*********************************************************************
3397 * Function : client_ua
3399 * Description : Handle "ua-" headers properly. Called from `sed'.
3402 * 1 : csp = Current client state (buffers, headers, etc...)
3403 * 2 : header = On input, pointer to header to modify.
3404 * On output, pointer to the modified header, or NULL
3405 * to remove the header. This function frees the
3406 * original string if necessary.
3408 * Returns : JB_ERR_OK on success, or
3409 * JB_ERR_MEMORY on out-of-memory error.
3411 *********************************************************************/
3412 static jb_err client_ua(struct client_state *csp, char **header)
3414 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
3416 log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
3424 /*********************************************************************
3426 * Function : client_from
3428 * Description : Handle the "from" config setting properly.
3429 * Called from `sed'.
3432 * 1 : csp = Current client state (buffers, headers, etc...)
3433 * 2 : header = On input, pointer to header to modify.
3434 * On output, pointer to the modified header, or NULL
3435 * to remove the header. This function frees the
3436 * original string if necessary.
3438 * Returns : JB_ERR_OK on success, or
3439 * JB_ERR_MEMORY on out-of-memory error.
3441 *********************************************************************/
3442 static jb_err client_from(struct client_state *csp, char **header)
3446 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
3453 newval = csp->action->string[ACTION_STRING_FROM];
3456 * Are we blocking the e-mail address?
3458 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
3460 log_error(LOG_LEVEL_HEADER, "crunched From!");
3464 log_error(LOG_LEVEL_HEADER, " modified");
3466 *header = strdup("From: ");
3467 string_append(header, newval);
3469 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3473 /*********************************************************************
3475 * Function : client_send_cookie
3477 * Description : Crunches the "cookie" header if necessary.
3478 * Called from `sed'.
3480 * XXX: Stupid name, doesn't send squat.
3483 * 1 : csp = Current client state (buffers, headers, etc...)
3484 * 2 : header = On input, pointer to header to modify.
3485 * On output, pointer to the modified header, or NULL
3486 * to remove the header. This function frees the
3487 * original string if necessary.
3489 * Returns : JB_ERR_OK on success, or
3490 * JB_ERR_MEMORY on out-of-memory error.
3492 *********************************************************************/
3493 static jb_err client_send_cookie(struct client_state *csp, char **header)
3495 if (csp->action->flags & ACTION_NO_COOKIE_READ)
3497 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie: %s", *header);
3505 /*********************************************************************
3507 * Function : client_x_forwarded
3509 * Description : Handle the "x-forwarded-for" config setting properly,
3510 * also used in the add_client_headers list. Called from `sed'.
3513 * 1 : csp = Current client state (buffers, headers, etc...)
3514 * 2 : header = On input, pointer to header to modify.
3515 * On output, pointer to the modified header, or NULL
3516 * to remove the header. This function frees the
3517 * original string if necessary.
3519 * Returns : JB_ERR_OK on success, or
3520 * JB_ERR_MEMORY on out-of-memory error.
3522 *********************************************************************/
3523 jb_err client_x_forwarded(struct client_state *csp, char **header)
3525 if (0 != (csp->action->flags & ACTION_CHANGE_X_FORWARDED_FOR))
3527 const char *parameter = csp->action->string[ACTION_STRING_CHANGE_X_FORWARDED_FOR];
3529 if (0 == strcmpic(parameter, "block"))
3532 log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
3534 else if (0 == strcmpic(parameter, "add"))
3536 string_append(header, ", ");
3537 string_append(header, csp->ip_addr_str);
3539 if (*header == NULL)
3541 return JB_ERR_MEMORY;
3543 log_error(LOG_LEVEL_HEADER,
3544 "Appended client IP address to %s", *header);
3545 csp->flags |= CSP_FLAG_X_FORWARDED_FOR_APPENDED;
3549 log_error(LOG_LEVEL_FATAL,
3550 "Invalid change-x-forwarded-for parameter: '%s'", parameter);
3558 /*********************************************************************
3560 * Function : client_max_forwards
3562 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
3563 * from the value of the Max-Forwards header field.
3566 * 1 : csp = Current client state (buffers, headers, etc...)
3567 * 2 : header = On input, pointer to header to modify.
3568 * On output, pointer to the modified header, or NULL
3569 * to remove the header. This function frees the
3570 * original string if necessary.
3572 * Returns : JB_ERR_OK on success, or
3573 * JB_ERR_MEMORY on out-of-memory error.
3575 *********************************************************************/
3576 static jb_err client_max_forwards(struct client_state *csp, char **header)
3580 if ((0 == strcmpic(csp->http->gpc, "trace")) ||
3581 (0 == strcmpic(csp->http->gpc, "options")))
3583 assert(*(*header+12) == ':');
3584 if (1 == sscanf(*header+12, ": %d", &max_forwards))
3586 if (max_forwards > 0)
3588 snprintf(*header, strlen(*header)+1, "Max-Forwards: %d", --max_forwards);
3589 log_error(LOG_LEVEL_HEADER,
3590 "Max-Forwards value for %s request reduced to %d.",
3591 csp->http->gpc, max_forwards);
3593 else if (max_forwards < 0)
3595 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3601 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3610 /*********************************************************************
3612 * Function : client_host
3614 * Description : If the request URI did not contain host and
3615 * port information, parse and evaluate the Host
3618 * Also, kill ill-formed HOST: headers as sent by
3619 * Apple's iTunes software when used with a proxy.
3622 * 1 : csp = Current client state (buffers, headers, etc...)
3623 * 2 : header = On input, pointer to header to modify.
3624 * On output, pointer to the modified header, or NULL
3625 * to remove the header. This function frees the
3626 * original string if necessary.
3628 * Returns : JB_ERR_OK on success, or
3629 * JB_ERR_MEMORY on out-of-memory error.
3631 *********************************************************************/
3632 static jb_err client_host(struct client_state *csp, char **header)
3637 * If the header field name is all upper-case, chances are that it's
3638 * an ill-formed one from iTunes. BTW, killing innocent headers here is
3639 * not a problem -- they are regenerated later.
3641 if ((*header)[1] == 'O')
3643 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
3648 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
3649 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
3652 if (NULL == (p = strdup((*header)+6)))
3654 return JB_ERR_MEMORY;
3657 if (NULL == (q = strdup(p)))
3660 return JB_ERR_MEMORY;
3663 freez(csp->http->hostport);
3664 csp->http->hostport = p;
3665 freez(csp->http->host);
3666 csp->http->host = q;
3667 q = strchr(csp->http->host, ':');
3670 /* Terminate hostname and evaluate port string */
3672 csp->http->port = atoi(q);
3676 csp->http->port = csp->http->ssl ? 443 : 80;
3679 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
3680 csp->http->hostport, csp->http->host, csp->http->port);
3683 /* Signal client_host_adder() to return right away */
3684 csp->flags |= CSP_FLAG_HOST_HEADER_IS_SET;
3690 /*********************************************************************
3692 * Function : client_if_modified_since
3694 * Description : Remove or modify the If-Modified-Since header.
3697 * 1 : csp = Current client state (buffers, headers, etc...)
3698 * 2 : header = On input, pointer to header to modify.
3699 * On output, pointer to the modified header, or NULL
3700 * to remove the header. This function frees the
3701 * original string if necessary.
3703 * Returns : JB_ERR_OK on success, or
3704 * JB_ERR_MEMORY on out-of-memory error.
3706 *********************************************************************/
3707 static jb_err client_if_modified_since(struct client_state *csp, char **header)
3710 #ifdef HAVE_GMTIME_R
3713 struct tm *timeptr = NULL;
3717 long int hours, minutes, seconds;
3721 if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
3724 * The client got an error message because of a temporary problem,
3725 * the problem is gone and the client now tries to revalidate our
3726 * error message on the real server. The revalidation would always
3727 * end with the transmission of the whole document and there is
3728 * no need to expose the bogus If-Modified-Since header.
3730 log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
3733 else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
3735 newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
3737 if ((0 == strcmpic(newval, "block")))
3739 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3742 else /* add random value */
3744 const char *header_time = *header + sizeof("If-Modified-Since:");
3746 if (JB_ERR_OK != parse_header_time(header_time, &tm))
3748 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
3753 rtime = strtol(newval, &endptr, 0);
3756 log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
3757 *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
3764 rtime = pick_from_range(rtime);
3768 log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
3771 tm += rtime * (negative ? -1 : 1);
3772 #ifdef HAVE_GMTIME_R
3773 timeptr = gmtime_r(&tm, &gmt);
3774 #elif FEATURE_PTHREAD
3775 privoxy_mutex_lock(&gmtime_mutex);
3776 timeptr = gmtime(&tm);
3777 privoxy_mutex_unlock(&gmtime_mutex);
3779 timeptr = gmtime(&tm);
3781 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
3784 *header = strdup("If-Modified-Since: ");
3785 string_append(header, newheader);
3787 if (*header == NULL)
3789 log_error(LOG_LEVEL_HEADER, "Insufficient memory, header crunched without replacement.");
3790 return JB_ERR_MEMORY;
3793 hours = rtime / 3600;
3794 minutes = rtime / 60 % 60;
3795 seconds = rtime % 60;
3797 log_error(LOG_LEVEL_HEADER,
3798 "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
3799 *header, (negative) ? "subtracted" : "added", hours,
3800 (hours == 1) ? "r" : "rs", minutes, (minutes == 1) ? "e" : "es",
3801 seconds, (seconds == 1) ? ")" : "s)");
3810 /*********************************************************************
3812 * Function : client_if_none_match
3814 * Description : Remove the If-None-Match header.
3817 * 1 : csp = Current client state (buffers, headers, etc...)
3818 * 2 : header = On input, pointer to header to modify.
3819 * On output, pointer to the modified header, or NULL
3820 * to remove the header. This function frees the
3821 * original string if necessary.
3823 * Returns : JB_ERR_OK on success, or
3824 * JB_ERR_MEMORY on out-of-memory error.
3826 *********************************************************************/
3827 static jb_err client_if_none_match(struct client_state *csp, char **header)
3829 if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
3831 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3839 /*********************************************************************
3841 * Function : client_x_filter
3843 * Description : Disables filtering if the client set "X-Filter: No".
3844 * Called from `sed'.
3847 * 1 : csp = Current client state (buffers, headers, etc...)
3848 * 2 : header = On input, pointer to header to modify.
3849 * On output, pointer to the modified header, or NULL
3850 * to remove the header. This function frees the
3851 * original string if necessary.
3853 * Returns : JB_ERR_OK on success
3855 *********************************************************************/
3856 jb_err client_x_filter(struct client_state *csp, char **header)
3858 if ( 0 == strcmpic(*header, "X-Filter: No"))
3860 if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
3862 log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
3866 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
3868 log_error(LOG_LEVEL_HEADER,
3869 "force-text-mode overruled the client's request to fetch without filtering!");
3873 csp->content_type = CT_TABOO; /* XXX: This hack shouldn't be necessary */
3874 csp->flags |= CSP_FLAG_NO_FILTERING;
3875 log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
3877 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3885 /*********************************************************************
3887 * Function : client_range
3889 * Description : Removes Range, Request-Range and If-Range headers if
3890 * content filtering is enabled. If the client's version
3891 * of the document has been altered by Privoxy, the server
3892 * could interpret the range differently than the client
3893 * intended in which case the user could end up with
3894 * corrupted content.
3897 * 1 : csp = Current client state (buffers, headers, etc...)
3898 * 2 : header = On input, pointer to header to modify.
3899 * On output, pointer to the modified header, or NULL
3900 * to remove the header. This function frees the
3901 * original string if necessary.
3903 * Returns : JB_ERR_OK
3905 *********************************************************************/
3906 static jb_err client_range(struct client_state *csp, char **header)
3908 if (content_filters_enabled(csp->action))
3910 log_error(LOG_LEVEL_HEADER, "Content filtering is enabled."
3911 " Crunching: \'%s\' to prevent range-mismatch problems.", *header);
3918 /* the following functions add headers directly to the header list */
3920 /*********************************************************************
3922 * Function : client_host_adder
3924 * Description : Adds the Host: header field if it is missing.
3925 * Called from `sed'.
3928 * 1 : csp = Current client state (buffers, headers, etc...)
3930 * Returns : JB_ERR_OK on success, or
3931 * JB_ERR_MEMORY on out-of-memory error.
3933 *********************************************************************/
3934 static jb_err client_host_adder(struct client_state *csp)
3939 if (csp->flags & CSP_FLAG_HOST_HEADER_IS_SET)
3941 /* Header already set by the client, nothing to do. */
3945 if ( !csp->http->hostport || !*(csp->http->hostport))
3947 /* XXX: When does this happen and why is it OK? */
3948 log_error(LOG_LEVEL_INFO, "Weirdness in client_host_adder detected and ignored.");
3953 * remove 'user:pass@' from 'proto://user:pass@host'
3955 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
3961 p = csp->http->hostport;
3964 /* XXX: Just add it, we already made sure that it will be unique */
3965 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
3966 err = enlist_unique_header(csp->headers, "Host", p);
3973 /*********************************************************************
3975 * Function : client_accept_encoding_adder
3977 * Description : Add an Accept-Encoding header to the client's request
3978 * that disables compression if the action applies, and
3979 * the header is not already there. Called from `sed'.
3980 * Note: For HTTP/1.0, the absence of the header is enough.
3983 * 1 : csp = Current client state (buffers, headers, etc...)
3985 * Returns : JB_ERR_OK on success, or
3986 * JB_ERR_MEMORY on out-of-memory error.
3988 *********************************************************************/
3989 static jb_err client_accept_encoding_adder(struct client_state *csp)
3991 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
3992 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
3994 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
4002 /*********************************************************************
4004 * Function : client_xtra_adder
4006 * Description : Used in the add_client_headers list. Called from `sed'.
4009 * 1 : csp = Current client state (buffers, headers, etc...)
4011 * Returns : JB_ERR_OK on success, or
4012 * JB_ERR_MEMORY on out-of-memory error.
4014 *********************************************************************/
4015 static jb_err client_xtra_adder(struct client_state *csp)
4017 struct list_entry *lst;
4020 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
4021 lst ; lst = lst->next)
4023 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
4024 err = enlist(csp->headers, lst->str);
4036 /*********************************************************************
4038 * Function : client_x_forwarded_for_adder
4040 * Description : Used in the add_client_headers list. Called from `sed'.
4043 * 1 : csp = Current client state (buffers, headers, etc...)
4045 * Returns : JB_ERR_OK on success, or
4046 * JB_ERR_MEMORY on out-of-memory error.
4048 *********************************************************************/
4049 static jb_err client_x_forwarded_for_adder(struct client_state *csp)
4051 char *header = NULL;
4054 if (!((csp->action->flags & ACTION_CHANGE_X_FORWARDED_FOR)
4055 && (0 == strcmpic(csp->action->string[ACTION_STRING_CHANGE_X_FORWARDED_FOR], "add")))
4056 || (csp->flags & CSP_FLAG_X_FORWARDED_FOR_APPENDED))
4059 * If we aren't adding X-Forwarded-For headers,
4060 * or we already appended an existing X-Forwarded-For
4061 * header, there's nothing left to do here.
4066 header = strdup("X-Forwarded-For: ");
4067 string_append(&header, csp->ip_addr_str);
4071 return JB_ERR_MEMORY;
4074 log_error(LOG_LEVEL_HEADER, "addh: %s", header);
4075 err = enlist(csp->headers, header);
4082 /*********************************************************************
4084 * Function : server_connection_close_adder
4086 * Description : "Temporary" fix for the needed but missing HTTP/1.1
4087 * support. Adds a "Connection: close" header to csp->headers
4088 * unless the header was already present. Called from `sed'.
4090 * FIXME: This whole function shouldn't be neccessary!
4093 * 1 : csp = Current client state (buffers, headers, etc...)
4095 * Returns : JB_ERR_OK on success, or
4096 * JB_ERR_MEMORY on out-of-memory error.
4098 *********************************************************************/
4099 static jb_err server_connection_close_adder(struct client_state *csp)
4101 const unsigned int flags = csp->flags;
4102 const char *response_status_line = csp->headers->first->str;
4104 if ((flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
4105 && (flags & CSP_FLAG_SERVER_CONNECTION_CLOSE_SET))
4111 * XXX: if we downgraded the response, this check will fail.
4113 if ((csp->config->feature_flags &
4114 RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
4115 && (NULL != response_status_line)
4116 && !strncmpic(response_status_line, "HTTP/1.1", 8))
4118 log_error(LOG_LEVEL_HEADER, "A HTTP/1.1 response "
4119 "without Connection header implies keep-alive.");
4120 csp->flags |= CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE;
4123 log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
4125 return enlist(csp->headers, "Connection: close");
4129 /*********************************************************************
4131 * Function : client_connection_header_adder
4133 * Description : Adds a proper "Connection:" header to csp->headers
4134 * unless the header was already present. Called from `sed'.
4137 * 1 : csp = Current client state (buffers, headers, etc...)
4139 * Returns : JB_ERR_OK on success, or
4140 * JB_ERR_MEMORY on out-of-memory error.
4142 *********************************************************************/
4143 static jb_err client_connection_header_adder(struct client_state *csp)
4145 const unsigned int flags = csp->flags;
4146 const char *wanted_header = get_appropiate_connection_header(csp);
4148 if (!(flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
4149 && (flags & CSP_FLAG_CLIENT_CONNECTION_HEADER_SET))
4154 log_error(LOG_LEVEL_HEADER, "Adding: %s", wanted_header);
4156 return enlist(csp->headers, wanted_header);
4160 /*********************************************************************
4162 * Function : server_http
4164 * Description : - Save the HTTP Status into csp->http->status
4165 * - Set CT_TABOO to prevent filtering if the answer
4166 * is a partial range (HTTP status 206)
4167 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
4171 * 1 : csp = Current client state (buffers, headers, etc...)
4172 * 2 : header = On input, pointer to header to modify.
4173 * On output, pointer to the modified header, or NULL
4174 * to remove the header. This function frees the
4175 * original string if necessary.
4177 * Returns : JB_ERR_OK on success, or
4178 * JB_ERR_MEMORY on out-of-memory error.
4180 *********************************************************************/
4181 static jb_err server_http(struct client_state *csp, char **header)
4183 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
4184 if (csp->http->status == 206)
4186 csp->content_type = CT_TABOO;
4189 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
4191 /* XXX: Should we do a real validity check here? */
4192 if (strlen(*header) > 8)
4195 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
4200 * XXX: Should we block the request or
4201 * enlist a valid status code line here?
4203 log_error(LOG_LEVEL_INFO, "Malformed server response detected. "
4204 "Downgrading to HTTP/1.0 impossible.");
4212 /*********************************************************************
4214 * Function : server_set_cookie
4216 * Description : Handle the server "cookie" header properly.
4217 * Log cookie to the jar file. Then "crunch",
4218 * accept or rewrite it to a session cookie.
4219 * Called from `sed'.
4221 * TODO: Allow the user to specify a new expiration
4222 * time to cause the cookie to expire even before the
4223 * browser is closed.
4226 * 1 : csp = Current client state (buffers, headers, etc...)
4227 * 2 : header = On input, pointer to header to modify.
4228 * On output, pointer to the modified header, or NULL
4229 * to remove the header. This function frees the
4230 * original string if necessary.
4232 * Returns : JB_ERR_OK on success, or
4233 * JB_ERR_MEMORY on out-of-memory error.
4235 *********************************************************************/
4236 static jb_err server_set_cookie(struct client_state *csp, char **header)
4243 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
4245 log_error(LOG_LEVEL_HEADER, "Crunching incoming cookie: %s", *header);
4248 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
4250 /* Flag whether or not to log a message */
4253 /* A variable to store the tag we're working on */
4256 /* Skip "Set-Cookie:" (11 characters) in header */
4257 cur_tag = *header + 11;
4259 /* skip whitespace between "Set-Cookie:" and value */
4260 while (*cur_tag && ijb_isspace(*cur_tag))
4265 /* Loop through each tag in the cookie */
4269 char *next_tag = strchr(cur_tag, ';');
4270 if (next_tag != NULL)
4272 /* Skip the ';' character itself */
4275 /* skip whitespace ";" and start of tag */
4276 while (*next_tag && ijb_isspace(*next_tag))
4283 /* "Next tag" is the end of the string */
4284 next_tag = cur_tag + strlen(cur_tag);
4288 * Check the expiration date to see
4289 * if the cookie is still valid, if yes,
4290 * rewrite it to a session cookie.
4292 if ((strncmpic(cur_tag, "expires=", 8) == 0) && *(cur_tag + 8))
4294 char *expiration_date = cur_tag + 8; /* Skip "[Ee]xpires=" */
4296 /* Did we detect the date properly? */
4297 if (JB_ERR_OK != parse_header_time(expiration_date, &cookie_time))
4300 * Nope, treat it as if it was still valid.
4302 * XXX: Should we remove the whole cookie instead?
4304 log_error(LOG_LEVEL_ERROR,
4305 "Can't parse \'%s\', send by %s. Unsupported time format?", cur_tag, csp->http->url);
4306 string_move(cur_tag, next_tag);
4312 * Yes. Check if the cookie is still valid.
4314 * If the cookie is already expired it's probably
4315 * a delete cookie and even if it isn't, the browser
4316 * will discard it anyway.
4320 * XXX: timegm() isn't available on some AmigaOS
4321 * versions and our replacement doesn't work.
4323 * Our options are to either:
4325 * - disable session-cookies-only completely if timegm
4328 * - to simply remove all expired tags, like it has
4329 * been done until Privoxy 3.0.6 and to live with
4330 * the consequence that it can cause login/logout
4331 * problems on servers that don't validate their
4332 * input properly, or
4334 * - to replace it with mktime in which
4335 * case there is a slight chance of valid cookies
4336 * passing as already expired.
4338 * This is the way it's currently done and it's not
4339 * as bad as it sounds. If the missing GMT offset is
4340 * enough to change the result of the expiration check
4341 * the cookie will be only valid for a few hours
4342 * anyway, which in many cases will be shorter
4343 * than a browser session.
4345 if (cookie_time - now < 0)
4347 log_error(LOG_LEVEL_HEADER,
4348 "Cookie \'%s\' is already expired and can pass unmodified.", *header);
4349 /* Just in case some clown sets more then one expiration date */
4355 * Still valid, delete expiration date by copying
4356 * the rest of the string over it.
4358 string_move(cur_tag, next_tag);
4360 /* That changed the header, need to issue a log message */
4364 * Note that the next tag has now been moved to *cur_tag,
4365 * so we do not need to update the cur_tag pointer.
4373 /* Move on to next cookie tag */
4380 assert(NULL != *header);
4381 log_error(LOG_LEVEL_HEADER, "Cookie rewritten to a temporary one: %s",
4390 #ifdef FEATURE_FORCE_LOAD
4391 /*********************************************************************
4393 * Function : strclean
4395 * Description : In-Situ-Eliminate all occurances of substring in
4399 * 1 : string = string to clean
4400 * 2 : substring = substring to eliminate
4402 * Returns : Number of eliminations
4404 *********************************************************************/
4405 int strclean(char *string, const char *substring)
4411 len = strlen(substring);
4413 while((pos = strstr(string, substring)) != NULL)
4420 while (*p++ != '\0');
4427 #endif /* def FEATURE_FORCE_LOAD */
4430 /*********************************************************************
4432 * Function : parse_header_time
4434 * Description : Parses time formats used in HTTP header strings
4435 * to get the numerical respresentation.
4438 * 1 : header_time = HTTP header time as string.
4439 * 2 : result = storage for header_time in seconds
4441 * Returns : JB_ERR_OK if the time format was recognized, or
4442 * JB_ERR_PARSE otherwise.
4444 *********************************************************************/
4445 static jb_err parse_header_time(const char *header_time, time_t *result)
4450 * Zero out gmt to prevent time zone offsets.
4452 * While this is only necessary on some platforms
4453 * (mingw32 for example), I don't know how to
4454 * detect these automatically and doing it everywhere
4457 memset(&gmt, 0, sizeof(gmt));
4459 /* Tue, 02 Jun 2037 20:00:00 */
4460 if ((NULL == strptime(header_time, "%a, %d %b %Y %H:%M:%S", &gmt))
4461 /* Tue, 02-Jun-2037 20:00:00 */
4462 && (NULL == strptime(header_time, "%a, %d-%b-%Y %H:%M:%S", &gmt))
4463 /* Tue, 02-Jun-37 20:00:00 */
4464 && (NULL == strptime(header_time, "%a, %d-%b-%y %H:%M:%S", &gmt))
4465 /* Tuesday, 02-Jun-2037 20:00:00 */
4466 && (NULL == strptime(header_time, "%A, %d-%b-%Y %H:%M:%S", &gmt))
4467 /* Tuesday Jun 02 20:00:00 2037 */
4468 && (NULL == strptime(header_time, "%A %b %d %H:%M:%S %Y", &gmt)))
4470 return JB_ERR_PARSE;
4473 *result = timegm(&gmt);
4480 /*********************************************************************
4482 * Function : get_destination_from_headers
4484 * Description : Parse the "Host:" header to get the request's destination.
4485 * Only needed if the client's request was forcefully
4486 * redirected into Privoxy.
4488 * Code mainly copied from client_host() which is currently
4489 * run too late for this purpose.
4492 * 1 : headers = List of headers (one of them hopefully being
4493 * the "Host:" header)
4494 * 2 : http = storage for the result (host, port and hostport).
4496 * Returns : JB_ERR_MEMORY in case of memory problems,
4497 * JB_ERR_PARSE if the host header couldn't be found,
4498 * JB_ERR_OK otherwise.
4500 *********************************************************************/
4501 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
4507 host = get_header_value(headers, "Host:");
4511 log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
4512 return JB_ERR_PARSE;
4515 if (NULL == (p = strdup((host))))
4517 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
4518 return JB_ERR_MEMORY;
4521 if (NULL == (q = strdup(p)))
4524 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
4525 return JB_ERR_MEMORY;
4528 freez(http->hostport);
4532 q = strchr(http->host, ':');
4535 /* Terminate hostname and evaluate port string */
4537 http->port = atoi(q);
4541 http->port = http->ssl ? 443 : 80;
4544 /* Rebuild request URL */
4546 http->url = strdup(http->ssl ? "https://" : "http://");
4547 string_append(&http->url, http->hostport);
4548 string_append(&http->url, http->path);
4549 if (http->url == NULL)
4551 return JB_ERR_MEMORY;
4554 log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header. New request URL: %s",
4562 /*********************************************************************
4564 * Function : create_forged_referrer
4566 * Description : Helper for client_referrer to forge a referer as
4567 * 'http://[hostname:port/' to fool stupid
4568 * checks for in-site links
4571 * 1 : header = Pointer to header pointer
4572 * 2 : hostport = Host and optionally port as string
4574 * Returns : JB_ERR_OK in case of success, or
4575 * JB_ERR_MEMORY in case of memory problems.
4577 *********************************************************************/
4578 static jb_err create_forged_referrer(char **header, const char *hostport)
4580 assert(NULL == *header);
4582 *header = strdup("Referer: http://");
4583 string_append(header, hostport);
4584 string_append(header, "/");
4586 if (NULL == *header)
4588 return JB_ERR_MEMORY;
4591 log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
4598 /*********************************************************************
4600 * Function : create_fake_referrer
4602 * Description : Helper for client_referrer to create a fake referrer
4603 * based on a string supplied by the user.
4606 * 1 : header = Pointer to header pointer
4607 * 2 : hosthost = Referrer to fake
4609 * Returns : JB_ERR_OK in case of success, or
4610 * JB_ERR_MEMORY in case of memory problems.
4612 *********************************************************************/
4613 static jb_err create_fake_referrer(char **header, const char *fake_referrer)
4615 assert(NULL == *header);
4617 if ((0 != strncmpic(fake_referrer, "http://", 7)) && (0 != strncmpic(fake_referrer, "https://", 8)))
4619 log_error(LOG_LEVEL_HEADER,
4620 "Parameter: +hide-referrer{%s} is a bad idea, but I don't care.", fake_referrer);
4622 *header = strdup("Referer: ");
4623 string_append(header, fake_referrer);
4625 if (NULL == *header)
4627 return JB_ERR_MEMORY;
4630 log_error(LOG_LEVEL_HEADER, "Referer replaced with: %s", *header);
4637 /*********************************************************************
4639 * Function : handle_conditional_hide_referrer_parameter
4641 * Description : Helper for client_referrer to crunch or forge
4642 * the referrer header if the host has changed.
4645 * 1 : header = Pointer to header pointer
4646 * 2 : host = The target host (may include the port)
4647 * 3 : parameter_conditional_block = Boolean to signal
4648 * if we're in conditional-block mode. If not set,
4649 * we're in conditional-forge mode.
4651 * Returns : JB_ERR_OK in case of success, or
4652 * JB_ERR_MEMORY in case of memory problems.
4654 *********************************************************************/
4655 static jb_err handle_conditional_hide_referrer_parameter(char **header,
4656 const char *host, const int parameter_conditional_block)
4658 char *referer = strdup(*header);
4659 const size_t hostlenght = strlen(host);
4660 const char *referer_url = NULL;
4662 if (NULL == referer)
4665 return JB_ERR_MEMORY;
4668 /* referer begins with 'Referer: http[s]://' */
4669 if ((hostlenght+17) < strlen(referer))
4672 * Shorten referer to make sure the referer is blocked
4673 * if www.example.org/www.example.com-shall-see-the-referer/
4674 * links to www.example.com/
4676 referer[hostlenght+17] = '\0';
4678 referer_url = strstr(referer, "http://");
4679 if ((NULL == referer_url) || (NULL == strstr(referer_url, host)))
4681 /* Host has changed, Referer is invalid or a https URL. */
4682 if (parameter_conditional_block)
4684 log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
4691 return create_forged_referrer(header, host);
4701 /*********************************************************************
4703 * Function : get_appropiate_connection_header
4705 * Description : Returns an appropiate Connection header
4706 * depending on whether or not we try to keep
4707 * the connection to the server alive.
4710 * 1 : csp = Current client state (buffers, headers, etc...)
4712 * Returns : Pointer to statically allocated header buffer.
4714 *********************************************************************/
4715 static const char *get_appropiate_connection_header(const struct client_state *csp)
4717 static const char connection_keep_alive[] = "Connection: keep-alive";
4718 static const char connection_close[] = "Connection: close";
4720 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
4721 && (csp->http->ssl == 0))
4723 return connection_keep_alive;
4725 return connection_close;