1 const char parsers_rcs[] = "$Id: parsers.c,v 1.77 2006/12/07 18:44:26 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_server_header'
14 * `filter_client_header', `filter_header', `crunch_server_header',
15 * `server_content_encoding', `server_content_disposition',
16 * `server_last_modified', `client_accept_language',
17 * `crunch_client_header', `client_if_modified_since',
18 * `client_if_none_match', `get_destination_from_headers',
19 * `parse_header_time' and `server_set_cookie'.
21 * Copyright : Written by and Copyright (C) 2001-2006 the SourceForge
22 * Privoxy team. http://www.privoxy.org/
24 * Based on the Internet Junkbuster originally written
25 * by and Copyright (C) 1997 Anonymous Coders and
26 * Junkbusters Corporation. http://www.junkbusters.com
28 * This program is free software; you can redistribute it
29 * and/or modify it under the terms of the GNU General
30 * Public License as published by the Free Software
31 * Foundation; either version 2 of the License, or (at
32 * your option) any later version.
34 * This program is distributed in the hope that it will
35 * be useful, but WITHOUT ANY WARRANTY; without even the
36 * implied warranty of MERCHANTABILITY or FITNESS FOR A
37 * PARTICULAR PURPOSE. See the GNU General Public
38 * License for more details.
40 * The GNU General Public License should be included with
41 * this file. If not, you can view it at
42 * http://www.gnu.org/copyleft/gpl.html
43 * or write to the Free Software Foundation, Inc., 59
44 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
48 * Revision 1.77 2006/12/07 18:44:26 fabiankeil
49 * Rebuild request URL in get_destination_from_headers()
50 * to make sure redirect{pcrs command} works as expected
51 * for intercepted requests.
53 * Revision 1.76 2006/12/06 19:52:25 fabiankeil
54 * Added get_destination_from_headers().
56 * Revision 1.75 2006/11/13 19:05:51 fabiankeil
57 * Make pthread mutex locking more generic. Instead of
58 * checking for OSX and OpenBSD, check for FEATURE_PTHREAD
59 * and use mutex locking unless there is an _r function
60 * available. Better safe than sorry.
62 * Fixes "./configure --disable-pthread" and should result
63 * in less threading-related problems on pthread-using platforms,
64 * but it still doesn't fix BR#1122404.
66 * Revision 1.74 2006/10/02 16:59:12 fabiankeil
67 * The special header "X-Filter: No" now disables
68 * header filtering as well.
70 * Revision 1.73 2006/09/23 13:26:38 roro
71 * Replace TABs by spaces in source code.
73 * Revision 1.72 2006/09/23 12:37:21 fabiankeil
74 * Don't print a log message every time filter_headers is
75 * entered or left. It only creates noise without any real
78 * Revision 1.71 2006/09/21 19:55:17 fabiankeil
79 * Fix +hide-if-modified-since{-n}.
81 * Revision 1.70 2006/09/08 12:06:34 fabiankeil
82 * Have hide-if-modified-since interpret the random
83 * range value as minutes instead of hours. Allows
84 * more fine-grained configuration.
86 * Revision 1.69 2006/09/06 16:25:51 fabiankeil
87 * Always have parse_header_time return a pointer
88 * that actual makes sense, even though we currently
89 * only need it to detect problems.
91 * Revision 1.68 2006/09/06 10:43:32 fabiankeil
92 * Added config option enable-remote-http-toggle
93 * to specify if Privoxy should recognize special
94 * headers (currently only X-Filter) to change its
95 * behaviour. Disabled by default.
97 * Revision 1.67 2006/09/04 11:01:26 fabiankeil
98 * After filtering de-chunked instances, remove
99 * "Transfer-Encoding" header entirely instead of changing
100 * it to "Transfer-Encoding: identity", which is invalid.
101 * Thanks Michael Shields <shields@msrl.com>. Fixes PR 1318658.
103 * Don't use localtime in parse_header_time. An empty time struct
104 * is good enough, it gets overwritten by strptime anyway.
106 * Revision 1.66 2006/09/03 19:38:28 fabiankeil
107 * Use gmtime_r if available, fallback to gmtime with mutex
108 * protection for MacOSX and use vanilla gmtime for the rest.
110 * Revision 1.65 2006/08/22 10:55:56 fabiankeil
111 * Changed client_referrer to use the right type (size_t) for
112 * hostlenght and to shorten the temporary referrer string with
113 * '\0' instead of adding a useless line break.
115 * Revision 1.64 2006/08/17 17:15:10 fabiankeil
116 * - Back to timegm() using GnuPG's replacement if necessary.
117 * Using mktime() and localtime() could add a on hour offset if
118 * the randomize factor was big enough to lead to a summer/wintertime
121 * - Removed now-useless Privoxy 3.0.3 compatibility glue.
123 * - Moved randomization code into pick_from_range().
125 * - Changed parse_header_time definition.
126 * time_t isn't guaranteed to be signed and
127 * if it isn't, -1 isn't available as error code.
128 * Changed some variable types in client_if_modified_since()
129 * because of the same reason.
131 * Revision 1.63 2006/08/14 13:18:08 david__schmidt
132 * OS/2 compilation compatibility fixups
134 * Revision 1.62 2006/08/14 08:58:42 fabiankeil
135 * Changed include from strptime.c to strptime.h
137 * Revision 1.61 2006/08/14 08:25:19 fabiankeil
138 * Split filter-headers{} into filter-client-headers{}
139 * and filter-server-headers{}.
140 * Added parse_header_time() to share some code.
141 * Replaced timegm() with mktime().
143 * Revision 1.60 2006/08/12 03:54:37 david__schmidt
144 * Windows service integration
146 * Revision 1.59 2006/08/03 02:46:41 david__schmidt
147 * Incorporate Fabian Keil's patch work:
\rhttp://www.fabiankeil.de/sourcecode/privoxy/
149 * Revision 1.58 2006/07/18 14:48:47 david__schmidt
150 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
151 * with what was really the latest development (the v_3_0_branch branch)
153 * Revision 1.56.2.10 2006/01/21 16:16:08 david__schmidt
154 * Thanks to Edward Carrel for his patch to modernize OSX's
\rpthreads support. See bug #1409623.
156 * Revision 1.56.2.9 2004/10/03 12:53:45 david__schmidt
157 * Add the ability to check jpeg images for invalid
158 * lengths of comment blocks. Defensive strategy
159 * against the exploit:
160 * Microsoft Security Bulletin MS04-028
161 * Buffer Overrun in JPEG Processing (GDI+) Could
162 * Allow Code Execution (833987)
163 * Enabled with +inspect-jpegs in actions files.
165 * Revision 1.56.2.8 2003/07/11 13:21:25 oes
166 * Excluded text/plain objects from filtering. This fixes a
167 * couple of client-crashing, download corruption and
168 * Privoxy performance issues, whose root cause lies in
169 * web servers labelling content of unknown type as text/plain.
171 * Revision 1.56.2.7 2003/05/06 12:07:26 oes
172 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
174 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
175 * Completing the previous change
177 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
178 * Added temporary workaround for bug in PHP < 4.2.3
180 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
181 * Wrapping all *_r functions (the non-_r versions of them) with mutex semaphores for OSX. Hopefully this will take care of all of those pesky crash reports.
183 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
184 * Fix typo: supressed -> suppressed
186 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
187 * Improved cookie logging
189 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
190 * Added basic support for OPTIONS and TRACE HTTP methods:
191 * - New parser function client_max_forwards which decrements
192 * the Max-Forwards HTTP header field of OPTIONS and TRACE
193 * requests by one before forwarding
194 * - New parser function client_host which extracts the host
195 * and port information from the HTTP header field if the
196 * request URI was not absolute
197 * - Don't crumble and re-add the Host: header, but only generate
198 * and append if missing
200 * Revision 1.56 2002/05/12 15:34:22 jongfoster
201 * Fixing typo in a comment
203 * Revision 1.55 2002/05/08 16:01:07 oes
204 * Optimized add_to_iob:
205 * - Use realloc instead of malloc(), memcpy(), free()
206 * - Expand to powers of two if possible, to get
207 * O(log n) reallocs instead of O(n).
208 * - Moved check for buffer limit here from chat
209 * - Report failure via returncode
211 * Revision 1.54 2002/04/02 15:03:16 oes
212 * Tiny code cosmetics
214 * Revision 1.53 2002/03/26 22:29:55 swa
215 * we have a new homepage!
217 * Revision 1.52 2002/03/24 13:25:43 swa
218 * name change related issues
220 * Revision 1.51 2002/03/13 00:27:05 jongfoster
223 * Revision 1.50 2002/03/12 01:45:35 oes
224 * More verbose logging
226 * Revision 1.49 2002/03/09 20:03:52 jongfoster
227 * - Making various functions return int rather than size_t.
228 * (Undoing a recent change). Since size_t is unsigned on
229 * Windows, functions like read_socket that return -1 on
230 * error cannot return a size_t.
232 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
233 * crashes, and also frequently caused JB to jump to 100%
234 * CPU and stay there. (Because it thought it had just
235 * read ((unsigned)-1) == 4Gb of data...)
237 * - The signature of write_socket has changed, it now simply
238 * returns success=0/failure=nonzero.
240 * - Trying to get rid of a few warnings --with-debug on
241 * Windows, I've introduced a new type "jb_socket". This is
242 * used for the socket file descriptors. On Windows, this
243 * is SOCKET (a typedef for unsigned). Everywhere else, it's
244 * an int. The error value can't be -1 any more, so it's
245 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
246 * Windows it maps to the #define INVALID_SOCKET.)
248 * - The signature of bind_port has changed.
250 * Revision 1.48 2002/03/07 03:46:53 oes
251 * Fixed compiler warnings etc
253 * Revision 1.47 2002/02/20 23:15:13 jongfoster
254 * Parsing functions now handle out-of-memory gracefully by returning
257 * Revision 1.46 2002/01/17 21:03:47 jongfoster
258 * Moving all our URL and URL pattern parsing code to urlmatch.c.
260 * Revision 1.45 2002/01/09 14:33:03 oes
261 * Added support for localtime_r.
263 * Revision 1.44 2001/12/14 01:22:54 steudten
264 * Remove 'user:pass@' from 'proto://user:pass@host' for the
265 * new added header 'Host: ..'. (See Req ID 491818)
267 * Revision 1.43 2001/11/23 00:26:38 jongfoster
268 * Fixing two really stupid errors in my previous commit
270 * Revision 1.42 2001/11/22 21:59:30 jongfoster
271 * Adding code to handle +no-cookies-keep
273 * Revision 1.41 2001/11/05 23:43:05 steudten
274 * Add time+date to log files.
276 * Revision 1.40 2001/10/26 20:13:09 jongfoster
277 * ctype.h is needed in Windows, too.
279 * Revision 1.39 2001/10/26 17:40:04 oes
280 * Introduced get_header_value()
281 * Removed http->user_agent, csp->referrer and csp->accept_types
282 * Removed client_accept()
284 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
285 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
286 * threads to call select() simultaneously. So, it's time to do a real, live,
287 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
288 * (native). Both versions will work, but using __OS2__ offers multi-threading.
290 * Revision 1.37 2001/10/23 21:36:02 jongfoster
291 * Documenting sed()'s error behaviou (doc change only)
293 * Revision 1.36 2001/10/13 12:51:51 joergs
294 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
295 * force-load), instead crumble Host: and add it (again) in client_host_adder
296 * (in case we get a HTTP/1.0 request without Host: header and forward it to
297 * a HTTP/1.1 server/proxy).
299 * Revision 1.35 2001/10/09 22:39:21 jongfoster
300 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
303 * Revision 1.34 2001/10/07 18:50:55 oes
304 * Added server_content_encoding, renamed server_transfer_encoding
306 * Revision 1.33 2001/10/07 18:04:49 oes
307 * Changed server_http11 to server_http and its pattern to "HTTP".
308 * Additional functionality: it now saves the HTTP status into
309 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
311 * Revision 1.32 2001/10/07 15:43:28 oes
312 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
313 * client_te and client_accept_encoding_adder, triggered by the new
314 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
315 * changed to allow only identity and chunked, and the TE header is
316 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
318 * parse_http_request no longer does anything than parsing. The rewriting
319 * of http->cmd and version mangling are gone. It now also recognizes
320 * the put and delete methods and saves the url in http->url. Removed
323 * renamed content_type and content_length to have the server_ prefix
325 * server_content_type now only works if csp->content_type != CT_TABOO
327 * added server_transfer_encoding, which
328 * - Sets CT_TABOO to prohibit filtering if encoding compresses
329 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
330 * - Change from "chunked" to "identity" if body was chunked
331 * but has been de-chunked for filtering.
333 * added server_content_md5 which crunches any Content-MD5 headers
334 * if the body was modified.
336 * made server_http11 conditional on +downgrade action
338 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
340 * Revision 1.31 2001/10/05 14:25:02 oes
341 * Crumble Keep-Alive from Server
343 * Revision 1.30 2001/09/29 12:56:03 joergs
344 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
346 * Revision 1.29 2001/09/24 21:09:24 jongfoster
347 * Fixing 2 memory leaks that Guy spotted, where the paramater to
348 * enlist() was not being free()d.
350 * Revision 1.28 2001/09/22 16:32:28 jongfoster
351 * Removing unused #includes.
353 * Revision 1.27 2001/09/20 15:45:25 steudten
355 * add casting from size_t to int for printf()
356 * remove local variable shadow s2
358 * Revision 1.26 2001/09/16 17:05:14 jongfoster
359 * Removing unused #include showarg.h
361 * Revision 1.25 2001/09/16 13:21:27 jongfoster
362 * Changes to use new list functions.
364 * Revision 1.24 2001/09/13 23:05:50 jongfoster
365 * Changing the string paramater to the header parsers a "const".
367 * Revision 1.23 2001/09/12 18:08:19 steudten
369 * In parse_http_request() header rewriting miss the host value, so
370 * from http://www.mydomain.com the result was just " / " not
371 * http://www.mydomain.com/ in case we forward.
373 * Revision 1.22 2001/09/10 10:58:53 oes
374 * Silenced compiler warnings
376 * Revision 1.21 2001/07/31 14:46:00 oes
377 * - Persistant connections now suppressed
378 * - sed() no longer appends empty header to csp->headers
380 * Revision 1.20 2001/07/30 22:08:36 jongfoster
381 * Tidying up #defines:
382 * - All feature #defines are now of the form FEATURE_xxx
383 * - Permanently turned off WIN_GUI_EDIT
384 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
386 * Revision 1.19 2001/07/25 17:21:54 oes
387 * client_uagent now saves copy of User-Agent: header value
389 * Revision 1.18 2001/07/13 14:02:46 oes
390 * - Included fix to repair broken HTTP requests that
391 * don't contain a path, not even '/'.
392 * - Removed all #ifdef PCRS
393 * - content_type now always inspected and classified as
394 * text, gif or other.
395 * - formatting / comments
397 * Revision 1.17 2001/06/29 21:45:41 oes
398 * Indentation, CRLF->LF, Tab-> Space
400 * Revision 1.16 2001/06/29 13:32:42 oes
402 * - Adapted free_http_request
403 * - Removed logentry from cancelled commit
405 * Revision 1.15 2001/06/03 19:12:38 oes
406 * deleted const struct interceptors
408 * Revision 1.14 2001/06/01 18:49:17 jongfoster
409 * Replaced "list_share" with "list" - the tiny memory gain was not
410 * worth the extra complexity.
412 * Revision 1.13 2001/05/31 21:30:33 jongfoster
413 * Removed list code - it's now in list.[ch]
414 * Renamed "permission" to "action", and changed many features
415 * to use the actions file rather than the global config.
417 * Revision 1.12 2001/05/31 17:33:13 oes
421 * Revision 1.11 2001/05/29 20:11:19 joergs
422 * '/ * inside comment' warning removed.
424 * Revision 1.10 2001/05/29 09:50:24 jongfoster
425 * Unified blocklist/imagelist/permissionslist.
426 * File format is still under discussion, but the internal changes
429 * Also modified interceptor behaviour:
430 * - We now intercept all URLs beginning with one of the following
431 * prefixes (and *only* these prefixes):
433 * * http://ijbswa.sf.net/config/
434 * * http://ijbswa.sourceforge.net/config/
435 * - New interceptors "home page" - go to http://i.j.b/ to see it.
436 * - Internal changes so that intercepted and fast redirect pages
437 * are not replaced with an image.
438 * - Interceptors now have the option to send a binary page direct
439 * to the client. (i.e. ijb-send-banner uses this)
440 * - Implemented show-url-info interceptor. (Which is why I needed
441 * the above interceptors changes - a typical URL is
442 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
443 * The previous mechanism would not have intercepted that, and
444 * if it had been intercepted then it then it would have replaced
447 * Revision 1.9 2001/05/28 17:26:33 jongfoster
448 * Fixing segfault if last header was crunched.
449 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
450 * can use the cross-platform sprintf() instead.)
452 * Revision 1.8 2001/05/27 22:17:04 oes
454 * - re_process_buffer no longer writes the modified buffer
455 * to the client, which was very ugly. It now returns the
456 * buffer, which it is then written by chat.
458 * - content_length now adjusts the Content-Length: header
459 * for modified documents rather than crunch()ing it.
460 * (Length info in csp->content_length, which is 0 for
461 * unmodified documents)
463 * - For this to work, sed() is called twice when filtering.
465 * Revision 1.7 2001/05/27 13:19:06 oes
466 * Patched Joergs solution for the content-length in.
468 * Revision 1.6 2001/05/26 13:39:32 jongfoster
469 * Only crunches Content-Length header if applying RE filtering.
470 * Without this fix, Microsoft Windows Update wouldn't work.
472 * Revision 1.5 2001/05/26 00:28:36 jongfoster
473 * Automatic reloading of config file.
474 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
475 * Most of the global variables have been moved to a new
476 * struct configuration_spec, accessed through csp->config->globalname
477 * Most of the globals remaining are used by the Win32 GUI.
479 * Revision 1.4 2001/05/22 18:46:04 oes
481 * - Enabled filtering banners by size rather than URL
482 * by adding patterns that replace all standard banner
483 * sizes with the "Junkbuster" gif to the re_filterfile
485 * - Enabled filtering WebBugs by providing a pattern
486 * which kills all 1x1 images
488 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
489 * which is selected by the (nonstandard and therefore
490 * capital) letter 'U' in the option string.
491 * It causes the quantifiers to be ungreedy by default.
492 * Appending a ? turns back to greedy (!).
494 * - Added a new interceptor ijb-send-banner, which
495 * sends back the "Junkbuster" gif. Without imagelist or
496 * MSIE detection support, or if tinygif = 1, or the
497 * URL isn't recognized as an imageurl, a lame HTML
498 * explanation is sent instead.
500 * - Added new feature, which permits blocking remote
501 * script redirects and firing back a local redirect
503 * The feature is conditionally compiled, i.e. it
504 * can be disabled with --disable-fast-redirects,
505 * plus it must be activated by a "fast-redirects"
506 * line in the config file, has its own log level
507 * and of course wants to be displayed by show-proxy-args
508 * Note: Boy, all the #ifdefs in 1001 locations and
509 * all the fumbling with configure.in and acconfig.h
510 * were *way* more work than the feature itself :-(
512 * - Because a generic redirect template was needed for
513 * this, tinygif = 3 now uses the same.
515 * - Moved GIFs, and other static HTTP response templates
520 * - Removed some >400 CRs again (Jon, you really worked
523 * Revision 1.3 2001/05/20 01:21:20 jongfoster
524 * Version 2.9.4 checkin.
525 * - Merged popupfile and cookiefile, and added control over PCRS
526 * filtering, in new "permissionsfile".
527 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
528 * file error you now get a message box (in the Win32 GUI) rather
529 * than the program exiting with no explanation.
530 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
532 * - Removed tabs from "config"
533 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
534 * - Bumped up version number.
536 * Revision 1.2 2001/05/17 23:02:36 oes
537 * - Made referrer option accept 'L' as a substitute for '§'
539 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
540 * Initial import of version 2.9.3 source tree
543 *********************************************************************/
550 #include <sys/types.h>
559 #if !defined(_WIN32) && !defined(__OS2__)
565 #ifdef FEATURE_PTHREAD
567 /* jcc.h is for mutex semapores only */
568 #endif /* def FEATURE_PTHREAD */
574 #include "jbsockets.h"
575 #include "miscutil.h"
578 #ifndef HAVE_STRPTIME
579 #include "strptime.h"
582 const char parsers_h_rcs[] = PARSERS_H_VERSION;
584 /* Fix a problem with Solaris. There should be no effect on other
586 * Solaris's isspace() is a macro which uses it's argument directly
587 * as an array index. Therefore we need to make sure that high-bit
588 * characters generate +ve values, and ideally we also want to make
589 * the argument match the declared parameter type of "int".
591 * Why did they write a character function that can't take a simple
592 * "char" argument? Doh!
594 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
595 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
598 const struct parsers client_patterns[] = {
599 { "referer:", 8, client_referrer },
600 { "user-agent:", 11, client_uagent },
601 { "ua-", 3, client_ua },
602 { "from:", 5, client_from },
603 { "cookie:", 7, client_send_cookie },
604 { "x-forwarded-for:", 16, client_x_forwarded },
605 { "Accept-Encoding:", 16, client_accept_encoding },
606 { "TE:", 3, client_te },
607 { "Host:", 5, client_host },
608 { "if-modified-since:", 18, client_if_modified_since },
609 { "Keep-Alive:", 11, crumble },
610 { "connection:", 11, crumble },
611 { "proxy-connection:", 17, crumble },
612 { "max-forwards:", 13, client_max_forwards },
613 { "Accept-Language:", 16, client_accept_language },
614 { "if-none-match:", 14, client_if_none_match },
615 { "X-Filter:", 9, client_x_filter },
616 { "*", 0, crunch_client_header },
617 { "*", 0, filter_client_header },
621 const struct parsers server_patterns[] = {
622 { "HTTP", 4, server_http },
623 { "set-cookie:", 11, server_set_cookie },
624 { "connection:", 11, crumble },
625 { "Content-Type:", 13, server_content_type },
626 { "Content-Length:", 15, server_content_length },
627 { "Content-MD5:", 12, server_content_md5 },
628 { "Content-Encoding:", 17, server_content_encoding },
629 { "Transfer-Encoding:", 18, server_transfer_coding },
630 { "Keep-Alive:", 11, crumble },
631 { "content-disposition:", 20, server_content_disposition },
632 { "Last-Modified:", 14, server_last_modified },
633 { "*", 0, crunch_server_header },
634 { "*", 0, filter_server_header },
638 const struct parsers server_patterns_light[] = {
639 { "Content-Length:", 15, server_content_length },
640 { "Transfer-Encoding:", 18, server_transfer_coding },
644 const add_header_func_ptr add_client_headers[] = {
647 client_x_forwarded_adder,
649 /* Temporarily disabled: client_accept_encoding_adder, */
650 connection_close_adder,
655 const add_header_func_ptr add_server_headers[] = {
656 connection_close_adder,
660 /*********************************************************************
662 * Function : flush_socket
664 * Description : Write any pending "buffered" content.
667 * 1 : fd = file descriptor of the socket to read
668 * 2 : csp = Current client state (buffers, headers, etc...)
670 * Returns : On success, the number of bytes written are returned (zero
671 * indicates nothing was written). On error, -1 is returned,
672 * and errno is set appropriately. If count is zero and the
673 * file descriptor refers to a regular file, 0 will be
674 * returned without causing any other effect. For a special
675 * file, the results are not portable.
677 *********************************************************************/
678 int flush_socket(jb_socket fd, struct client_state *csp)
680 struct iob *iob = csp->iob;
681 int len = iob->eod - iob->cur;
688 if (write_socket(fd, iob->cur, (size_t)len))
692 iob->eod = iob->cur = iob->buf;
698 /*********************************************************************
700 * Function : add_to_iob
702 * Description : Add content to the buffered page, expanding the
703 * buffer if necessary.
706 * 1 : csp = Current client state (buffers, headers, etc...)
707 * 2 : buf = holds the content to be added to the page
708 * 3 : n = number of bytes to be added
710 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
711 * or buffer limit reached.
713 *********************************************************************/
714 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
716 struct iob *iob = csp->iob;
717 size_t used, offset, need, want;
720 if (n <= 0) return JB_ERR_OK;
722 used = iob->eod - iob->buf;
723 offset = iob->cur - iob->buf;
727 * If the buffer can't hold the new data, extend it first.
728 * Use the next power of two if possible, else use the actual need.
730 if (need > csp->config->buffer_limit)
732 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
733 return JB_ERR_MEMORY;
736 if (need > iob->size)
738 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
740 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
744 else if (NULL != (p = (char *)realloc(iob->buf, need)))
750 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
751 return JB_ERR_MEMORY;
754 /* Update the iob pointers */
755 iob->cur = p + offset;
760 /* copy the new data into the iob buffer */
761 memcpy(iob->eod, buf, (size_t)n);
763 /* point to the end of the data */
766 /* null terminate == cheap insurance */
774 /*********************************************************************
776 * Function : get_header
778 * Description : This (odd) routine will parse the csp->iob
781 * 1 : csp = Current client state (buffers, headers, etc...)
783 * Returns : Any one of the following:
785 * 1) a pointer to a dynamically allocated string that contains a header line
786 * 2) NULL indicating that the end of the header was reached
787 * 3) "" indicating that the end of the iob was reached before finding
788 * a complete header line.
790 *********************************************************************/
791 char *get_header(struct client_state *csp)
797 if ((iob->cur == NULL)
798 || ((p = strchr(iob->cur, '\n')) == NULL))
800 return(""); /* couldn't find a complete header */
805 ret = strdup(iob->cur);
808 /* FIXME No way to handle error properly */
809 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
814 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
816 /* is this a blank line (i.e. the end of the header) ? */
828 /*********************************************************************
830 * Function : get_header_value
832 * Description : Get the value of a given header from a chained list
833 * of header lines or return NULL if no such header is
834 * present in the list.
837 * 1 : header_list = pointer to list
838 * 2 : header_name = string with name of header to look for.
839 * Trailing colon required, capitalization
842 * Returns : NULL if not found, else value of header
844 *********************************************************************/
845 char *get_header_value(const struct list *header_list, const char *header_name)
847 struct list_entry *cur_entry;
853 length = strlen(header_name);
855 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
859 if (!strncmpic(cur_entry->str, header_name, length))
862 * Found: return pointer to start of value
864 ret = (char *) (cur_entry->str + length);
865 while (*ret && ijb_isspace(*ret)) ret++;
878 /*********************************************************************
882 * Description : add, delete or modify lines in the HTTP header streams.
883 * On entry, it receives a linked list of headers space
884 * that was allocated dynamically (both the list nodes
885 * and the header contents).
887 * As a side effect it frees the space used by the original
891 * 1 : pats = list of patterns to match against headers
892 * 2 : more_headers = list of functions to add more
893 * headers (client or server)
894 * 3 : csp = Current client state (buffers, headers, etc...)
896 * Returns : Single pointer to a fully formed header, or NULL
897 * on out-of-memory error.
899 *********************************************************************/
900 char *sed(const struct parsers pats[],
901 const add_header_func_ptr more_headers[],
902 struct client_state *csp)
904 struct list_entry *p;
905 const struct parsers *v;
906 const add_header_func_ptr *f;
907 jb_err err = JB_ERR_OK;
911 * If filtering is enabled, sed is run twice,
912 * but most of the work needs to be done only once.
914 first_run = (more_headers != NULL ) ? 1 : 0;
916 if (first_run) /* Parse and print */
918 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
920 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
922 /* Header crunch()ed in previous run? -> ignore */
923 if (p->str == NULL) continue;
925 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
927 /* Does the current parser handle this header? */
928 if ((strncmpic(p->str, v->str, v->len) == 0) || (v->len == CHECK_EVERY_HEADER_REMAINING))
930 err = v->parser(csp, (char **)&(p->str));
934 /* place any additional headers on the csp->headers list */
935 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
940 else /* Parse only */
943 * The second run is only needed if the body was modified
944 * and the content-lenght has changed.
946 if (strncmpic(csp->http->cmd, "HEAD", 4))
948 /*XXX: Code duplication*/
949 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
951 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
953 /* Header crunch()ed in previous run? -> ignore */
954 if (p->str == NULL) continue;
956 /* Does the current parser handle this header? */
957 if (strncmpic(p->str, v->str, v->len) == 0)
959 err = v->parser(csp, (char **)&(p->str));
966 if (err != JB_ERR_OK)
971 return list_to_text(csp->headers);
975 /* here begins the family of parser functions that reformat header lines */
977 /*********************************************************************
979 * Function : filter_server_header
981 * Description : Checks if server header filtering is enabled.
982 * If it is, filter_header is called to do the work.
985 * 1 : csp = Current client state (buffers, headers, etc...)
986 * 2 : header = On input, pointer to header to modify.
987 * On output, pointer to the modified header, or NULL
988 * to remove the header. This function frees the
989 * original string if necessary.
991 * Returns : JB_ERR_OK on success and always succeeds
993 *********************************************************************/
994 jb_err filter_server_header(struct client_state *csp, char **header)
996 if (csp->action->flags & ACTION_FILTER_SERVER_HEADERS)
998 filter_header(csp, header);
1003 /*********************************************************************
1005 * Function : filter_client_header
1007 * Description : Checks if client header filtering is enabled.
1008 * If it is, filter_header is called to do the work.
1011 * 1 : csp = Current client state (buffers, headers, etc...)
1012 * 2 : header = On input, pointer to header to modify.
1013 * On output, pointer to the modified header, or NULL
1014 * to remove the header. This function frees the
1015 * original string if necessary.
1017 * Returns : JB_ERR_OK on success and always succeeds
1019 *********************************************************************/
1020 jb_err filter_client_header(struct client_state *csp, char **header)
1022 if (csp->action->flags & ACTION_FILTER_CLIENT_HEADERS)
1024 filter_header(csp, header);
1029 /*********************************************************************
1031 * Function : filter_header
1033 * Description : Executes all text substitutions from all applying
1034 * +filter actions on the header.
1035 * Most of the code was copied from pcrs_filter_response,
1036 * including the rather short variable names
1039 * 1 : csp = Current client state (buffers, headers, etc...)
1040 * 2 : header = On input, pointer to header to modify.
1041 * On output, pointer to the modified header, or NULL
1042 * to remove the header. This function frees the
1043 * original string if necessary.
1045 * Returns : JB_ERR_OK on success and always succeeds
1047 *********************************************************************/
1048 jb_err filter_header(struct client_state *csp, char **header)
1052 size_t size = strlen(*header);
1054 char *newheader = NULL;
1057 struct file_list *fl;
1058 struct re_filterfile_spec *b;
1059 struct list_entry *filtername;
1061 int i, found_filters = 0;
1064 * Need to check the set of re_filterfiles...
1066 for (i = 0; i < MAX_AF_FILES; i++)
1079 if (0 == found_filters)
1081 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1085 for (i = 0; i < MAX_AF_FILES; i++)
1088 if ((NULL == fl) || (NULL == fl->f))
1091 * For all applying +filter actions, look if a filter by that
1092 * name exists and if yes, execute its pcrs_joblist on the
1095 for (b = fl->f; b; b = b->next)
1097 for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
1098 filtername ; filtername = filtername->next)
1100 if (strcmp(b->name, filtername->str) == 0)
1102 int current_hits = 0;
1104 if ( NULL == b->joblist )
1106 log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1110 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s (size %d) with filter %s...",
1111 *header, size, b->name);
1113 /* Apply all jobs from the joblist */
1114 for (job = b->joblist; NULL != job; job = job->next)
1116 matches = pcrs_execute(job, *header, size, &newheader, &size);
1119 current_hits += matches;
1120 log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
1122 *header = newheader;
1124 else if ( 0 == matches )
1126 /* Filter doesn't change header */
1132 log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
1133 *header, b->name, pcrs_strerror(matches));
1134 if( newheader != NULL)
1136 log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
1141 log_error(LOG_LEVEL_RE_FILTER, " ...produced %d hits (new size %d).", current_hits, size);
1142 hits += current_hits;
1150 log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
1158 /*********************************************************************
1160 * Function : crumble
1162 * Description : This is called if a header matches a pattern to "crunch"
1165 * 1 : csp = Current client state (buffers, headers, etc...)
1166 * 2 : header = On input, pointer to header to modify.
1167 * On output, pointer to the modified header, or NULL
1168 * to remove the header. This function frees the
1169 * original string if necessary.
1171 * Returns : JB_ERR_OK on success, or
1172 * JB_ERR_MEMORY on out-of-memory error.
1174 *********************************************************************/
1175 jb_err crumble(struct client_state *csp, char **header)
1177 log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
1182 /*********************************************************************
1184 * Function : crunch_server_header
1186 * Description : Crunch server header if it matches a string supplied by the
1187 * user. Called from `sed'.
1190 * 1 : csp = Current client state (buffers, headers, etc...)
1191 * 2 : header = On input, pointer to header to modify.
1192 * On output, pointer to the modified header, or NULL
1193 * to remove the header. This function frees the
1194 * original string if necessary.
1196 * Returns : JB_ERR_OK on success and always succeeds
1198 *********************************************************************/
1199 jb_err crunch_server_header(struct client_state *csp, char **header)
1201 const char *crunch_pattern;
1202 /*Is there a header to crunch*/
1204 if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
1206 crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
1208 /*Is the current header the lucky one?*/
1209 if (strstr(*header, crunch_pattern))
1211 log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
1218 /*********************************************************************
1220 * Function : server_content_type
1222 * Description : Set the content-type for filterable types (text/.*,
1223 * javascript and image/gif) unless filtering has been
1224 * forbidden (CT_TABOO) while parsing earlier headers.
1225 * NOTE: Since text/plain is commonly used by web servers
1226 * for files whose correct type is unknown, we don't
1227 * set CT_TEXT for it.
1230 * 1 : csp = Current client state (buffers, headers, etc...)
1231 * 2 : header = On input, pointer to header to modify.
1232 * On output, pointer to the modified header, or NULL
1233 * to remove the header. This function frees the
1234 * original string if necessary.
1236 * Returns : JB_ERR_OK on success, or
1237 * JB_ERR_MEMORY on out-of-memory error.
1239 *********************************************************************/
1240 jb_err server_content_type(struct client_state *csp, char **header)
1244 newval = csp->action->string[ACTION_STRING_CONTENT_TYPE];
1246 if (csp->content_type != CT_TABOO)
1248 if ((strstr(*header, " text/") && !strstr(*header, "plain"))
1249 || strstr(*header, "xml")
1250 || strstr(*header, "application/x-javascript"))
1251 csp->content_type = CT_TEXT;
1252 else if (strstr(*header, " image/gif"))
1253 csp->content_type = CT_GIF;
1254 else if (strstr(*header, " image/jpeg"))
1255 csp->content_type = CT_JPEG;
1257 csp->content_type = 0;
1260 * Are we enabling text mode by force?
1262 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
1265 * Do we really have to?
1267 if (csp->content_type == CT_TEXT)
1269 log_error(LOG_LEVEL_HEADER, "Text mode is already enabled.");
1273 csp->content_type = CT_TEXT;
1274 log_error(LOG_LEVEL_HEADER, "Text mode enabled by force. Take cover!");
1278 * Are we messing with the content type?
1280 if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
1283 * Make sure the user doesn't accidently
1284 * change the content type of binary documents.
1286 if (csp->content_type == CT_TEXT)
1289 *header = strdup("Content-Type: ");
1290 string_append(header, newval);
1294 log_error(LOG_LEVEL_HEADER, "Insufficient memory. Conten-Type crunched without replacement!");
1295 return JB_ERR_MEMORY;
1297 log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
1301 log_error(LOG_LEVEL_HEADER, "%s not replaced. It doesn't look like text. "
1302 "Enable force-text-mode if you know what you're doing.", *header);
1309 /*********************************************************************
1311 * Function : server_transfer_coding
1313 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
1314 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
1315 * - Remove header if body was chunked but has been
1316 * de-chunked for filtering.
1319 * 1 : csp = Current client state (buffers, headers, etc...)
1320 * 2 : header = On input, pointer to header to modify.
1321 * On output, pointer to the modified header, or NULL
1322 * to remove the header. This function frees the
1323 * original string if necessary.
1325 * Returns : JB_ERR_OK on success, or
1326 * JB_ERR_MEMORY on out-of-memory error.
1328 *********************************************************************/
1329 jb_err server_transfer_coding(struct client_state *csp, char **header)
1332 * Turn off pcrs and gif filtering if body compressed
1334 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1336 csp->content_type = CT_TABOO;
1340 * Raise flag if body chunked
1342 if (strstr(*header, "chunked"))
1344 csp->flags |= CSP_FLAG_CHUNKED;
1347 * If the body was modified, it has been de-chunked first
1348 * and the header must be removed.
1350 if (csp->flags & CSP_FLAG_MODIFIED)
1352 log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
1361 /*********************************************************************
1363 * Function : server_content_encoding
1365 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
1368 * 1 : csp = Current client state (buffers, headers, etc...)
1369 * 2 : header = On input, pointer to header to modify.
1370 * On output, pointer to the modified header, or NULL
1371 * to remove the header. This function frees the
1372 * original string if necessary.
1374 * Returns : JB_ERR_OK on success, or
1375 * JB_ERR_MEMORY on out-of-memory error.
1377 *********************************************************************/
1378 jb_err server_content_encoding(struct client_state *csp, char **header)
1381 * Turn off pcrs and gif filtering if body compressed
1383 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1385 csp->content_type = CT_TABOO;
1393 /*********************************************************************
1395 * Function : server_content_length
1397 * Description : Adjust Content-Length header if we modified
1401 * 1 : csp = Current client state (buffers, headers, etc...)
1402 * 2 : header = On input, pointer to header to modify.
1403 * On output, pointer to the modified header, or NULL
1404 * to remove the header. This function frees the
1405 * original string if necessary.
1407 * Returns : JB_ERR_OK on success, or
1408 * JB_ERR_MEMORY on out-of-memory error.
1410 *********************************************************************/
1411 jb_err server_content_length(struct client_state *csp, char **header)
1413 if (csp->content_length != 0) /* Content length could have been modified */
1416 * XXX: Shouldn't we check if csp->content_length
1417 * is different than the original value?
1420 *header = (char *) zalloc(100);
1421 if (*header == NULL)
1423 return JB_ERR_MEMORY;
1426 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
1428 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1435 /*********************************************************************
1437 * Function : server_content_md5
1439 * Description : Crumble any Content-MD5 headers if the document was
1440 * modified. FIXME: Should we re-compute instead?
1443 * 1 : csp = Current client state (buffers, headers, etc...)
1444 * 2 : header = On input, pointer to header to modify.
1445 * On output, pointer to the modified header, or NULL
1446 * to remove the header. This function frees the
1447 * original string if necessary.
1449 * Returns : JB_ERR_OK on success, or
1450 * JB_ERR_MEMORY on out-of-memory error.
1452 *********************************************************************/
1453 jb_err server_content_md5(struct client_state *csp, char **header)
1455 if (csp->flags & CSP_FLAG_MODIFIED)
1457 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1464 /*********************************************************************
1466 * Function : server_content_disposition
1468 * Description : If enabled, blocks or modifies the "content-disposition" header.
1469 * Called from `sed'.
1472 * 1 : csp = Current client state (buffers, headers, etc...)
1473 * 2 : header = On input, pointer to header to modify.
1474 * On output, pointer to the modified header, or NULL
1475 * to remove the header. This function frees the
1476 * original string if necessary.
1478 * Returns : JB_ERR_OK on success, or
1479 * JB_ERR_MEMORY on out-of-memory error.
1481 *********************************************************************/
1482 jb_err server_content_disposition(struct client_state *csp, char **header)
1487 * Are we messing with the content-disposition header?
1489 if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
1495 newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
1497 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1500 * Blocking content-disposition header
1502 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1509 * Replacing content-disposition header
1512 *header = strdup("content-disposition: ");
1513 string_append(header, newval);
1515 if (*header == NULL)
1517 log_error(LOG_LEVEL_HEADER, "Insufficent memory. content-disposition header not fully replaced.");
1521 log_error(LOG_LEVEL_HEADER, "content-disposition header crunched and replaced with: %s", *header);
1524 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1527 /*********************************************************************
1529 * Function : server_last_modified
1531 * Description : Changes Last-Modified header to the actual date
1532 * to help hide-if-modified-since.
1533 * Called from `sed'.
1536 * 1 : csp = Current client state (buffers, headers, etc...)
1537 * 2 : header = On input, pointer to header to modify.
1538 * On output, pointer to the modified header, or NULL
1539 * to remove the header. This function frees the
1540 * original string if necessary.
1542 * Returns : JB_ERR_OK on success, or
1543 * JB_ERR_MEMORY on out-of-memory error.
1545 *********************************************************************/
1546 jb_err server_last_modified(struct client_state *csp, char **header)
1549 char buf[BUFFER_SIZE];
1552 #ifdef HAVE_GMTIME_R
1555 struct tm *timeptr = NULL;
1556 time_t now, last_modified;
1558 long int days, hours, minutes, seconds;
1561 * Are we messing with the Last-Modified header?
1563 if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
1569 newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
1571 if (0 == strcmpic(newval, "block") )
1574 * Blocking Last-Modified header. Useless but why not.
1576 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1580 else if (0 == strcmpic(newval, "reset-to-request-time"))
1583 * Setting Last-Modified Header to now.
1585 get_http_time(0, buf);
1587 *header = strdup("Last-Modified: ");
1588 string_append(header, buf);
1590 if (*header == NULL)
1592 log_error(LOG_LEVEL_HEADER, "Insufficent memory. Last-Modified header got lost, boohoo.");
1596 log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
1599 else if (0 == strcmpic(newval, "randomize"))
1601 log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
1603 #ifdef HAVE_GMTIME_R
1604 timeptr = gmtime_r(&now, &gmt);
1605 #elif FEATURE_PTHREAD
1606 pthread_mutex_lock(&gmtime_mutex);
1607 timeptr = gmtime(&now);
1608 pthread_mutex_unlock(&gmtime_mutex);
1610 timeptr = gmtime(&now);
1612 if ((timeptr = parse_header_time(*header, &last_modified)) == NULL)
1614 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
1619 rtime = difftime(now, last_modified);
1622 rtime = pick_from_range(rtime);
1623 last_modified += rtime;
1624 #ifdef HAVE_GMTIME_R
1625 timeptr = gmtime_r(&last_modified, &gmt);
1626 #elif FEATURE_PTHREAD
1627 pthread_mutex_lock(&gmtime_mutex);
1628 timeptr = gmtime(&last_modified);
1629 pthread_mutex_unlock(&gmtime_mutex);
1631 timeptr = gmtime(&last_modified);
1633 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
1635 *header = strdup("Last-Modified: ");
1636 string_append(header, newheader);
1638 if (*header == NULL)
1640 log_error(LOG_LEVEL_ERROR, "Insufficent memory, header crunched without replacement.");
1641 return JB_ERR_MEMORY;
1644 if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
1646 days = rtime / (3600 * 24);
1647 hours = rtime / 3600 % 24;
1648 minutes = rtime / 60 % 60;
1649 seconds = rtime % 60;
1651 log_error(LOG_LEVEL_HEADER, "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
1652 *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
1653 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
1658 log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
1666 /*********************************************************************
1668 * Function : client_accept_encoding
1670 * Description : Rewrite the client's Accept-Encoding header so that
1671 * if doesn't allow compression, if the action applies.
1672 * Note: For HTTP/1.0 the absence of the header is enough.
1675 * 1 : csp = Current client state (buffers, headers, etc...)
1676 * 2 : header = On input, pointer to header to modify.
1677 * On output, pointer to the modified header, or NULL
1678 * to remove the header. This function frees the
1679 * original string if necessary.
1681 * Returns : JB_ERR_OK on success, or
1682 * JB_ERR_MEMORY on out-of-memory error.
1684 *********************************************************************/
1685 jb_err client_accept_encoding(struct client_state *csp, char **header)
1687 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1689 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1693 /* Temporarily disable the correct behaviour to
1694 * work around a PHP bug.
1696 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1698 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1699 * if (*header == NULL)
1701 * return JB_ERR_MEMORY;
1712 /*********************************************************************
1714 * Function : client_te
1716 * Description : Rewrite the client's TE header so that
1717 * if doesn't allow compression, if the action applies.
1720 * 1 : csp = Current client state (buffers, headers, etc...)
1721 * 2 : header = On input, pointer to header to modify.
1722 * On output, pointer to the modified header, or NULL
1723 * to remove the header. This function frees the
1724 * original string if necessary.
1726 * Returns : JB_ERR_OK on success, or
1727 * JB_ERR_MEMORY on out-of-memory error.
1729 *********************************************************************/
1730 jb_err client_te(struct client_state *csp, char **header)
1732 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1735 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1741 /*********************************************************************
1743 * Function : client_referrer
1745 * Description : Handle the "referer" config setting properly.
1746 * Called from `sed'.
1749 * 1 : csp = Current client state (buffers, headers, etc...)
1750 * 2 : header = On input, pointer to header to modify.
1751 * On output, pointer to the modified header, or NULL
1752 * to remove the header. This function frees the
1753 * original string if necessary.
1755 * Returns : JB_ERR_OK on success, or
1756 * JB_ERR_MEMORY on out-of-memory error.
1758 *********************************************************************/
1759 jb_err client_referrer(struct client_state *csp, char **header)
1766 #ifdef FEATURE_FORCE_LOAD
1767 /* Since the referrer can include the prefix even
1768 * if the request itself is non-forced, we must
1769 * clean it unconditionally
1771 strclean(*header, FORCE_PREFIX);
1772 #endif /* def FEATURE_FORCE_LOAD */
1775 * Are we sending referer?
1777 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1782 newval = csp->action->string[ACTION_STRING_REFERER];
1784 if ((0 != strcmpic(newval, "conditional-block")))
1788 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1793 log_error(LOG_LEVEL_HEADER, "Referer crunched!");
1796 else if (0 == strcmpic(newval, "conditional-block"))
1799 * Block referer if host has changed.
1802 if (NULL == (host = strdup(csp->http->hostport)))
1805 log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary host copy.");
1806 return JB_ERR_MEMORY;
1808 if (NULL == (referer = strdup(*header)))
1812 log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary referer copy.");
1813 return JB_ERR_MEMORY;
1815 hostlenght = strlen(host);
1816 if ( hostlenght < (strlen(referer)-17) ) /*referer begins with 'Referer: http[s]://'*/
1818 /*Shorten referer to make sure the referer is blocked
1819 *if www.example.org/www.example.com-shall-see-the-referer/
1820 *links to www.example.com/
1822 referer[hostlenght+17] = '\0';
1824 if ( 0 == strstr(referer, host)) /*Host has changed*/
1826 log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
1831 log_error(LOG_LEVEL_HEADER, "%s (not modified, still on %s)", *header, host);
1837 else if (0 != strcmpic(newval, "forge"))
1840 * We have a specific (fixed) referer we want to send.
1842 if ((0 != strncmpic(newval, "http://", 7)) && (0 != strncmpic(newval, "https://", 8)))
1844 log_error(LOG_LEVEL_HEADER, "Parameter: +referrer{%s} is a bad idea, but I don't care.", newval);
1846 *header = strdup("Referer: ");
1847 string_append(header, newval);
1848 log_error(LOG_LEVEL_HEADER, "Referer overwritten with: %s", *header);
1850 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1855 * Forge a referer as http://[hostname:port of REQUEST]/
1856 * to fool stupid checks for in-site links
1859 *header = strdup("Referer: http://");
1860 string_append(header, csp->http->hostport);
1861 string_append(header, "/");
1862 log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
1864 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1868 /*********************************************************************
1870 * Function : client_accept_language
1872 * Description : Handle the "Accept-Language" config setting properly.
1873 * Called from `sed'.
1876 * 1 : csp = Current client state (buffers, headers, etc...)
1877 * 2 : header = On input, pointer to header to modify.
1878 * On output, pointer to the modified header, or NULL
1879 * to remove the header. This function frees the
1880 * original string if necessary.
1882 * Returns : JB_ERR_OK on success, or
1883 * JB_ERR_MEMORY on out-of-memory error.
1885 *********************************************************************/
1886 jb_err client_accept_language(struct client_state *csp, char **header)
1891 * Are we messing with the Accept-Language?
1893 if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
1895 /*I don't think so*/
1899 newval = csp->action->string[ACTION_STRING_LANGUAGE];
1901 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1904 * Blocking Accept-Language header
1906 log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
1913 * Replacing Accept-Language header
1916 *header = strdup("Accept-Language: ");
1917 string_append(header, newval);
1919 if (*header == NULL)
1921 log_error(LOG_LEVEL_ERROR, " Insufficent memory. Accept-Language header crunched without replacement.");
1925 log_error(LOG_LEVEL_HEADER, "Accept-Language header crunched and replaced with: %s", *header);
1928 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1931 /*********************************************************************
1933 * Function : crunch_client_header
1935 * Description : Crunch client header if it matches a string supplied by the
1936 * user. Called from `sed'.
1939 * 1 : csp = Current client state (buffers, headers, etc...)
1940 * 2 : header = On input, pointer to header to modify.
1941 * On output, pointer to the modified header, or NULL
1942 * to remove the header. This function frees the
1943 * original string if necessary.
1945 * Returns : JB_ERR_OK on success and always succeeds
1947 *********************************************************************/
1948 jb_err crunch_client_header(struct client_state *csp, char **header)
1950 const char *crunch_pattern;
1951 /*Is there a header to crunch*/
1953 if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
1955 crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
1957 /*Is the current header the lucky one?*/
1958 if (strstr(*header, crunch_pattern))
1960 log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
1968 /*********************************************************************
1970 * Function : client_uagent
1972 * Description : Handle the "user-agent" config setting properly
1973 * and remember its original value to enable browser
1974 * bug workarounds. Called from `sed'.
1977 * 1 : csp = Current client state (buffers, headers, etc...)
1978 * 2 : header = On input, pointer to header to modify.
1979 * On output, pointer to the modified header, or NULL
1980 * to remove the header. This function frees the
1981 * original string if necessary.
1983 * Returns : JB_ERR_OK on success, or
1984 * JB_ERR_MEMORY on out-of-memory error.
1986 *********************************************************************/
1987 jb_err client_uagent(struct client_state *csp, char **header)
1991 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1996 newval = csp->action->string[ACTION_STRING_USER_AGENT];
2003 *header = strdup("User-Agent: ");
2004 string_append(header, newval);
2006 log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
2008 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2011 /*********************************************************************
2013 * Function : client_ua
2015 * Description : Handle "ua-" headers properly. Called from `sed'.
2018 * 1 : csp = Current client state (buffers, headers, etc...)
2019 * 2 : header = On input, pointer to header to modify.
2020 * On output, pointer to the modified header, or NULL
2021 * to remove the header. This function frees the
2022 * original string if necessary.
2024 * Returns : JB_ERR_OK on success, or
2025 * JB_ERR_MEMORY on out-of-memory error.
2027 *********************************************************************/
2028 jb_err client_ua(struct client_state *csp, char **header)
2030 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
2032 log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
2040 /*********************************************************************
2042 * Function : client_from
2044 * Description : Handle the "from" config setting properly.
2045 * Called from `sed'.
2048 * 1 : csp = Current client state (buffers, headers, etc...)
2049 * 2 : header = On input, pointer to header to modify.
2050 * On output, pointer to the modified header, or NULL
2051 * to remove the header. This function frees the
2052 * original string if necessary.
2054 * Returns : JB_ERR_OK on success, or
2055 * JB_ERR_MEMORY on out-of-memory error.
2057 *********************************************************************/
2058 jb_err client_from(struct client_state *csp, char **header)
2062 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
2069 newval = csp->action->string[ACTION_STRING_FROM];
2072 * Are we blocking the e-mail address?
2074 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2076 log_error(LOG_LEVEL_HEADER, "crunched From!");
2080 log_error(LOG_LEVEL_HEADER, " modified");
2082 *header = strdup("From: ");
2083 string_append(header, newval);
2085 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2089 /*********************************************************************
2091 * Function : client_send_cookie
2093 * Description : Handle the "cookie" header properly. Called from `sed'.
2094 * If cookie is accepted, add it to the cookie_list,
2095 * else we crunch it. Mmmmmmmmmmm ... cookie ......
2098 * 1 : csp = Current client state (buffers, headers, etc...)
2099 * 2 : header = On input, pointer to header to modify.
2100 * On output, pointer to the modified header, or NULL
2101 * to remove the header. This function frees the
2102 * original string if necessary.
2104 * Returns : JB_ERR_OK on success, or
2105 * JB_ERR_MEMORY on out-of-memory error.
2107 *********************************************************************/
2108 jb_err client_send_cookie(struct client_state *csp, char **header)
2110 jb_err result = JB_ERR_OK;
2112 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
2114 /* strlen("cookie: ") == 8 */
2115 result = enlist(csp->cookie_list, *header + 8);
2119 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
2123 * Always remove the cookie here. The cookie header
2124 * will be sent at the end of the header.
2132 /*********************************************************************
2134 * Function : client_x_forwarded
2136 * Description : Handle the "x-forwarded-for" config setting properly,
2137 * also used in the add_client_headers list. Called from `sed'.
2140 * 1 : csp = Current client state (buffers, headers, etc...)
2141 * 2 : header = On input, pointer to header to modify.
2142 * On output, pointer to the modified header, or NULL
2143 * to remove the header. This function frees the
2144 * original string if necessary.
2146 * Returns : JB_ERR_OK on success, or
2147 * JB_ERR_MEMORY on out-of-memory error.
2149 *********************************************************************/
2150 jb_err client_x_forwarded(struct client_state *csp, char **header)
2152 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
2154 /* Save it so we can re-add it later */
2155 freez(csp->x_forwarded);
2156 csp->x_forwarded = *header;
2159 * Always set *header = NULL, since this information
2160 * will be sent at the end of the header.
2167 log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
2174 /*********************************************************************
2176 * Function : client_max_forwards
2178 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
2179 * from the value of the Max-Forwards header field.
2182 * 1 : csp = Current client state (buffers, headers, etc...)
2183 * 2 : header = On input, pointer to header to modify.
2184 * On output, pointer to the modified header, or NULL
2185 * to remove the header. This function frees the
2186 * original string if necessary.
2188 * Returns : JB_ERR_OK on success, or
2189 * JB_ERR_MEMORY on out-of-memory error.
2191 *********************************************************************/
2192 jb_err client_max_forwards(struct client_state *csp, char **header)
2194 unsigned int max_forwards;
2196 if ((0 == strcmpic(csp->http->gpc, "trace"))
2197 || (0 == strcmpic(csp->http->gpc, "options")))
2199 if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
2201 if (max_forwards-- >= 1)
2203 sprintf(*header, "Max-Forwards: %u", max_forwards);
2204 log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
2208 log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
2217 /*********************************************************************
2219 * Function : client_host
2221 * Description : If the request URI did not contain host and
2222 * port information, parse and evaluate the Host
2225 * Also, kill ill-formed HOST: headers as sent by
2226 * Apple's iTunes software when used with a proxy.
2229 * 1 : csp = Current client state (buffers, headers, etc...)
2230 * 2 : header = On input, pointer to header to modify.
2231 * On output, pointer to the modified header, or NULL
2232 * to remove the header. This function frees the
2233 * original string if necessary.
2235 * Returns : JB_ERR_OK on success, or
2236 * JB_ERR_MEMORY on out-of-memory error.
2238 *********************************************************************/
2239 jb_err client_host(struct client_state *csp, char **header)
2244 * If the header field name is all upper-case, chances are that it's
2245 * an ill-formed one from iTunes. BTW, killing innocent headers here is
2246 * not a problem -- they are regenerated later.
2248 if ((*header)[1] == 'O')
2250 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
2255 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
2256 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
2259 if (NULL == (p = strdup((*header)+6)))
2261 return JB_ERR_MEMORY;
2264 if (NULL == (q = strdup(p)))
2267 return JB_ERR_MEMORY;
2270 freez(csp->http->hostport);
2271 csp->http->hostport = p;
2272 freez(csp->http->host);
2273 csp->http->host = q;
2274 q = strchr(csp->http->host, ':');
2277 /* Terminate hostname and evaluate port string */
2279 csp->http->port = atoi(q);
2283 csp->http->port = csp->http->ssl ? 443 : 80;
2286 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
2287 csp->http->hostport, csp->http->host, csp->http->port);
2293 /*********************************************************************
2295 * Function : client_if_modified_since
2297 * Description : Remove or modify the If-Modified-Since header.
2300 * 1 : csp = Current client state (buffers, headers, etc...)
2301 * 2 : header = On input, pointer to header to modify.
2302 * On output, pointer to the modified header, or NULL
2303 * to remove the header. This function frees the
2304 * original string if necessary.
2306 * Returns : JB_ERR_OK on success, or
2307 * JB_ERR_MEMORY on out-of-memory error.
2309 *********************************************************************/
2310 jb_err client_if_modified_since(struct client_state *csp, char **header)
2313 #ifdef HAVE_GMTIME_R
2316 struct tm *timeptr = NULL;
2320 long int hours, minutes, seconds;
2324 if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
2327 * The client got an error message because of a temporary problem,
2328 * the problem is gone and the client now tries to revalidate our
2329 * error message on the real server. The revalidation would always
2330 * end with the transmission of the whole document and there is
2331 * no need to expose the bogus If-Modified-Since header.
2333 log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
2336 else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
2338 newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
2340 if ((0 == strcmpic(newval, "block")))
2342 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2345 else /* add random value */
2347 if ((timeptr = parse_header_time(*header, &tm)) == NULL)
2349 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
2354 rtime = strtol(newval, &endptr, 0);
2357 log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
2358 *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
2365 rtime = pick_from_range(rtime);
2369 log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
2372 tm += rtime * (negative ? -1 : 1);
2373 #ifdef HAVE_GMTIME_R
2374 timeptr = gmtime_r(&tm, &gmt);
2375 #elif FEATURE_PTHREAD
2376 pthread_mutex_lock(&gmtime_mutex);
2377 timeptr = gmtime(&tm);
2378 pthread_mutex_unlock(&gmtime_mutex);
2380 timeptr = gmtime(&tm);
2382 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2385 *header = strdup("If-Modified-Since: ");
2386 string_append(header, newheader);
2388 if (*header == NULL)
2390 log_error(LOG_LEVEL_HEADER, "Insufficent memory, header crunched without replacement.");
2391 return JB_ERR_MEMORY;
2394 if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2396 hours = rtime / 3600;
2397 minutes = rtime / 60 % 60;
2398 seconds = rtime % 60;
2400 log_error(LOG_LEVEL_HEADER, "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
2401 *header, (negative) ? "subtracted" : "added", hours, (hours == 1) ? "r" : "rs",
2402 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2411 /*********************************************************************
2413 * Function : client_if_none_match
2415 * Description : Remove the If-None-Match header.
2418 * 1 : csp = Current client state (buffers, headers, etc...)
2419 * 2 : header = On input, pointer to header to modify.
2420 * On output, pointer to the modified header, or NULL
2421 * to remove the header. This function frees the
2422 * original string if necessary.
2424 * Returns : JB_ERR_OK on success, or
2425 * JB_ERR_MEMORY on out-of-memory error.
2427 *********************************************************************/
2428 jb_err client_if_none_match(struct client_state *csp, char **header)
2430 if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
2432 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2439 /*********************************************************************
2441 * Function : client_x_filter
2443 * Description : Disables filtering if the client set "X-Filter: No".
2444 * Called from `sed'.
2447 * 1 : csp = Current client state (buffers, headers, etc...)
2448 * 2 : header = On input, pointer to header to modify.
2449 * On output, pointer to the modified header, or NULL
2450 * to remove the header. This function frees the
2451 * original string if necessary.
2453 * Returns : JB_ERR_OK on success
2455 *********************************************************************/
2456 jb_err client_x_filter(struct client_state *csp, char **header)
2458 if ( 0 == strcmpic(*header, "X-Filter: No"))
2460 if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
2462 log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
2466 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
2468 log_error(LOG_LEVEL_HEADER, "force-text-mode overruled the client's request to fetch without filtering!");
2472 csp->content_type = CT_TABOO;
2473 csp->action->flags &= ~ACTION_FILTER_SERVER_HEADERS;
2474 csp->action->flags &= ~ACTION_FILTER_CLIENT_HEADERS;
2475 log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
2477 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2484 /* the following functions add headers directly to the header list */
2486 /*********************************************************************
2488 * Function : client_host_adder
2490 * Description : Adds the Host: header field if it is missing.
2491 * Called from `sed'.
2494 * 1 : csp = Current client state (buffers, headers, etc...)
2496 * Returns : JB_ERR_OK on success, or
2497 * JB_ERR_MEMORY on out-of-memory error.
2499 *********************************************************************/
2500 jb_err client_host_adder(struct client_state *csp)
2505 if ( !csp->http->hostport || !*(csp->http->hostport))
2511 * remove 'user:pass@' from 'proto://user:pass@host'
2513 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
2519 p = csp->http->hostport;
2522 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
2523 err = enlist_unique_header(csp->headers, "Host", p);
2529 /*********************************************************************
2531 * Function : client_cookie_adder
2533 * Description : Used in the add_client_headers list. Called from `sed'.
2536 * 1 : csp = Current client state (buffers, headers, etc...)
2538 * Returns : JB_ERR_OK on success, or
2539 * JB_ERR_MEMORY on out-of-memory error.
2541 *********************************************************************/
2542 jb_err client_cookie_adder(struct client_state *csp)
2544 struct list_entry *lst;
2546 struct list_entry *list1 = csp->cookie_list->first;
2547 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
2548 int first_cookie = 1;
2551 if ((list1 == NULL) && (list2 == NULL))
2557 tmp = strdup("Cookie: ");
2559 for (lst = list1; lst ; lst = lst->next)
2567 string_append(&tmp, "; ");
2569 string_append(&tmp, lst->str);
2572 for (lst = list2; lst ; lst = lst->next)
2580 string_append(&tmp, "; ");
2582 string_join(&tmp, cookie_encode(lst->str));
2587 return JB_ERR_MEMORY;
2590 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
2591 err = enlist(csp->headers, tmp);
2597 /*********************************************************************
2599 * Function : client_accept_encoding_adder
2601 * Description : Add an Accept-Encoding header to the client's request
2602 * that disables compression if the action applies, and
2603 * the header is not already there. Called from `sed'.
2604 * Note: For HTTP/1.0, the absence of the header is enough.
2607 * 1 : csp = Current client state (buffers, headers, etc...)
2609 * Returns : JB_ERR_OK on success, or
2610 * JB_ERR_MEMORY on out-of-memory error.
2612 *********************************************************************/
2613 jb_err client_accept_encoding_adder(struct client_state *csp)
2615 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2616 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
2618 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
2625 /*********************************************************************
2627 * Function : client_xtra_adder
2629 * Description : Used in the add_client_headers list. Called from `sed'.
2632 * 1 : csp = Current client state (buffers, headers, etc...)
2634 * Returns : JB_ERR_OK on success, or
2635 * JB_ERR_MEMORY on out-of-memory error.
2637 *********************************************************************/
2638 jb_err client_xtra_adder(struct client_state *csp)
2640 struct list_entry *lst;
2643 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
2644 lst ; lst = lst->next)
2646 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
2647 err = enlist(csp->headers, lst->str);
2659 /*********************************************************************
2661 * Function : client_x_forwarded_adder
2663 * Description : Used in the add_client_headers list. Called from `sed'.
2666 * 1 : csp = Current client state (buffers, headers, etc...)
2668 * Returns : JB_ERR_OK on success, or
2669 * JB_ERR_MEMORY on out-of-memory error.
2671 *********************************************************************/
2672 jb_err client_x_forwarded_adder(struct client_state *csp)
2677 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
2682 if (csp->x_forwarded)
2684 p = strdup(csp->x_forwarded);
2685 string_append(&p, ", ");
2689 p = strdup("X-Forwarded-For: ");
2691 string_append(&p, csp->ip_addr_str);
2695 return JB_ERR_MEMORY;
2698 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
2699 err = enlist(csp->headers, p);
2706 /*********************************************************************
2708 * Function : connection_close_adder
2710 * Description : Adds a "Connection: close" header to csp->headers
2711 * as a temporary fix for the needed but missing HTTP/1.1
2712 * support. Called from `sed'.
2713 * FIXME: This whole function shouldn't be neccessary!
2716 * 1 : csp = Current client state (buffers, headers, etc...)
2718 * Returns : JB_ERR_OK on success, or
2719 * JB_ERR_MEMORY on out-of-memory error.
2721 *********************************************************************/
2722 jb_err connection_close_adder(struct client_state *csp)
2724 log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
2725 return enlist(csp->headers, "Connection: close");
2729 /*********************************************************************
2731 * Function : server_http
2733 * Description : - Save the HTTP Status into csp->http->status
2734 * - Set CT_TABOO to prevent filtering if the answer
2735 * is a partial range (HTTP status 206)
2736 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
2740 * 1 : csp = Current client state (buffers, headers, etc...)
2741 * 2 : header = On input, pointer to header to modify.
2742 * On output, pointer to the modified header, or NULL
2743 * to remove the header. This function frees the
2744 * original string if necessary.
2746 * Returns : JB_ERR_OK on success, or
2747 * JB_ERR_MEMORY on out-of-memory error.
2749 *********************************************************************/
2750 jb_err server_http(struct client_state *csp, char **header)
2752 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
2753 if (csp->http->status == 206)
2755 csp->content_type = CT_TABOO;
2758 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
2761 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
2768 /*********************************************************************
2770 * Function : server_set_cookie
2772 * Description : Handle the server "cookie" header properly.
2773 * Log cookie to the jar file. Then "crunch" it,
2774 * or accept it. Called from `sed'.
2777 * 1 : csp = Current client state (buffers, headers, etc...)
2778 * 2 : header = On input, pointer to header to modify.
2779 * On output, pointer to the modified header, or NULL
2780 * to remove the header. This function frees the
2781 * original string if necessary.
2783 * Returns : JB_ERR_OK on success, or
2784 * JB_ERR_MEMORY on out-of-memory error.
2786 *********************************************************************/
2787 jb_err server_set_cookie(struct client_state *csp, char **header)
2789 #ifdef FEATURE_COOKIE_JAR
2790 if (csp->config->jar)
2793 * Write timestamp into outbuf.
2795 * Complex because not all OSs have tm_gmtoff or
2796 * the %z field in strftime()
2798 char tempbuf[ BUFFER_SIZE ];
2802 #ifdef HAVE_LOCALTIME_R
2803 tm_now = *localtime_r(&now, &tm_now);
2804 #elif FEATURE_PTHREAD
2805 pthread_mutex_lock(&localtime_mutex);
2806 tm_now = *localtime (&now);
2807 pthread_mutex_unlock(&localtime_mutex);
2809 tm_now = *localtime (&now);
2811 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
2813 /* strlen("set-cookie: ") = 12 */
2814 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
2816 #endif /* def FEATURE_COOKIE_JAR */
2818 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
2820 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
2821 return crumble(csp, header);
2823 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
2825 /* Flag whether or not to log a message */
2828 /* A variable to store the tag we're working on */
2831 /* Skip "Set-Cookie:" (11 characters) in header */
2832 cur_tag = *header + 11;
2834 /* skip whitespace between "Set-Cookie:" and value */
2835 while (*cur_tag && ijb_isspace(*cur_tag))
2840 /* Loop through each tag in the cookie */
2844 char *next_tag = strchr(cur_tag, ';');
2845 if (next_tag != NULL)
2847 /* Skip the ';' character itself */
2850 /* skip whitespace ";" and start of tag */
2851 while (*next_tag && ijb_isspace(*next_tag))
2858 /* "Next tag" is the end of the string */
2859 next_tag = cur_tag + strlen(cur_tag);
2862 /* Is this the "Expires" tag? */
2863 if (strncmpic(cur_tag, "expires=", 8) == 0)
2865 /* Delete the tag by copying the rest of the string over it.
2866 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
2867 * since the behaviour of strcpy is undefined for overlapping
2870 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
2872 /* That changed the header, need to issue a log message */
2875 /* Note that the next tag has now been moved to *cur_tag,
2876 * so we do not need to update the cur_tag pointer.
2881 /* Move on to next cookie tag */
2888 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
2896 #ifdef FEATURE_FORCE_LOAD
2897 /*********************************************************************
2899 * Function : strclean
2901 * Description : In-Situ-Eliminate all occurances of substring in
2905 * 1 : string = string to clean
2906 * 2 : substring = substring to eliminate
2908 * Returns : Number of eliminations
2910 *********************************************************************/
2911 int strclean(const char *string, const char *substring)
2913 int hits = 0, len = strlen(substring);
2916 while((pos = strstr(string, substring)) != NULL)
2923 while (*p++ != '\0');
2930 #endif /* def FEATURE_FORCE_LOAD */
2932 /*********************************************************************
2934 * Function : parse_header_time
2936 * Description : Transforms time inside a HTTP header into
2937 * the usual time format.
2940 * 1 : header = header to parse
2941 * 2 : tm = storage for the resulting time in seconds
2943 * Returns : Time struct containing the header time, or
2944 * NULL in case of a parsing problems.
2946 *********************************************************************/
2947 struct tm *parse_header_time(char *header, time_t *tm) {
2951 struct tm * timeptr;
2954 * Initializing gmt to prevent time zone offsets.
2956 * While this is only necessary on some platforms
2957 * (mingw32 for example), I don't know how to
2958 * detect these automatically and doing it everywhere
2962 #ifdef HAVE_LOCALTIME_R
2963 gmt = *localtime_r(tm, &gmt);
2964 #elif FEATURE_PTHREAD
2965 pthread_mutex_lock(&localtime_mutex);
2966 gmt = *localtime(tm);
2967 pthread_mutex_unlock(&localtime_mutex);
2969 gmt = *localtime(tm);
2972 /* Skipping header name */
2973 timestring = strstr(header, ": ");
2974 if (strptime(timestring, ": %a, %d %b %Y %H:%M:%S", &gmt) == NULL)
2987 /*********************************************************************
2989 * Function : get_destination_from_headers
2991 * Description : Parse the "Host:" header to get the request's destination.
2992 * Only needed if the client's request was forcefully
2993 * redirected into Privoxy.
2995 * Code mainly copied from client_host() which is currently
2996 * run too late for this purpose.
2999 * 1 : headers = List of headers (one of them hopefully being
3000 * the "Host:" header)
3001 * 2 : http = storage for the result (host, port and hostport).
3003 * Returns : JB_ERR_MEMORY in case of memory problems,
3004 * JB_ERR_PARSE if the host header couldn't be found,
3005 * JB_ERR_OK otherwise.
3007 *********************************************************************/
3008 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
3014 host = get_header_value(headers, "Host:");
3018 log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
3019 return JB_ERR_PARSE;
3022 if (NULL == (p = strdup((host))))
3024 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
3025 return JB_ERR_MEMORY;
3028 if (NULL == (q = strdup(p)))
3031 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
3032 return JB_ERR_MEMORY;
3035 freez(http->hostport);
3039 q = strchr(http->host, ':');
3042 /* Terminate hostname and evaluate port string */
3044 http->port = atoi(q);
3048 http->port = http->ssl ? 443 : 80;
3051 /* Rebuild request URL */
3053 http->url = strdup(http->ssl ? "https://" : "http://");
3054 string_append(&http->url, http->hostport);
3055 string_append(&http->url, http->path);
3056 if (http->url == NULL)
3058 return JB_ERR_MEMORY;
3061 log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header. New request URL: %s",