1 const char parsers_rcs[] = "$Id: parsers.c,v 1.83 2007/01/12 15:03:02 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-2007 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.83 2007/01/12 15:03:02 fabiankeil
49 * Correct a cast, check inflateEnd() exit code
50 * to see if we have to, replace sprintf calls
53 * Revision 1.82 2007/01/01 19:36:37 fabiankeil
54 * Integrate a modified version of Wil Mahan's
55 * zlib patch (PR #895531).
57 * Revision 1.81 2006/12/31 22:21:33 fabiankeil
58 * Skip empty filter files in filter_header()
59 * but don't ignore the ones that come afterwards.
60 * Fixes BR 1619208, this time for real.
62 * Revision 1.80 2006/12/29 19:08:22 fabiankeil
63 * Reverted parts of my last commit
64 * to keep error handling working.
66 * Revision 1.79 2006/12/29 18:04:40 fabiankeil
67 * Fixed gcc43 conversion warnings.
69 * Revision 1.78 2006/12/26 17:19:20 fabiankeil
70 * Bringing back the "useless" localtime() call
71 * I removed in revision 1.67. On some platforms
72 * it's necessary to prevent time zone offsets.
74 * Revision 1.77 2006/12/07 18:44:26 fabiankeil
75 * Rebuild request URL in get_destination_from_headers()
76 * to make sure redirect{pcrs command} works as expected
77 * for intercepted requests.
79 * Revision 1.76 2006/12/06 19:52:25 fabiankeil
80 * Added get_destination_from_headers().
82 * Revision 1.75 2006/11/13 19:05:51 fabiankeil
83 * Make pthread mutex locking more generic. Instead of
84 * checking for OSX and OpenBSD, check for FEATURE_PTHREAD
85 * and use mutex locking unless there is an _r function
86 * available. Better safe than sorry.
88 * Fixes "./configure --disable-pthread" and should result
89 * in less threading-related problems on pthread-using platforms,
90 * but it still doesn't fix BR#1122404.
92 * Revision 1.74 2006/10/02 16:59:12 fabiankeil
93 * The special header "X-Filter: No" now disables
94 * header filtering as well.
96 * Revision 1.73 2006/09/23 13:26:38 roro
97 * Replace TABs by spaces in source code.
99 * Revision 1.72 2006/09/23 12:37:21 fabiankeil
100 * Don't print a log message every time filter_headers is
101 * entered or left. It only creates noise without any real
104 * Revision 1.71 2006/09/21 19:55:17 fabiankeil
105 * Fix +hide-if-modified-since{-n}.
107 * Revision 1.70 2006/09/08 12:06:34 fabiankeil
108 * Have hide-if-modified-since interpret the random
109 * range value as minutes instead of hours. Allows
110 * more fine-grained configuration.
112 * Revision 1.69 2006/09/06 16:25:51 fabiankeil
113 * Always have parse_header_time return a pointer
114 * that actual makes sense, even though we currently
115 * only need it to detect problems.
117 * Revision 1.68 2006/09/06 10:43:32 fabiankeil
118 * Added config option enable-remote-http-toggle
119 * to specify if Privoxy should recognize special
120 * headers (currently only X-Filter) to change its
121 * behaviour. Disabled by default.
123 * Revision 1.67 2006/09/04 11:01:26 fabiankeil
124 * After filtering de-chunked instances, remove
125 * "Transfer-Encoding" header entirely instead of changing
126 * it to "Transfer-Encoding: identity", which is invalid.
127 * Thanks Michael Shields <shields@msrl.com>. Fixes PR 1318658.
129 * Don't use localtime in parse_header_time. An empty time struct
130 * is good enough, it gets overwritten by strptime anyway.
132 * Revision 1.66 2006/09/03 19:38:28 fabiankeil
133 * Use gmtime_r if available, fallback to gmtime with mutex
134 * protection for MacOSX and use vanilla gmtime for the rest.
136 * Revision 1.65 2006/08/22 10:55:56 fabiankeil
137 * Changed client_referrer to use the right type (size_t) for
138 * hostlenght and to shorten the temporary referrer string with
139 * '\0' instead of adding a useless line break.
141 * Revision 1.64 2006/08/17 17:15:10 fabiankeil
142 * - Back to timegm() using GnuPG's replacement if necessary.
143 * Using mktime() and localtime() could add a on hour offset if
144 * the randomize factor was big enough to lead to a summer/wintertime
147 * - Removed now-useless Privoxy 3.0.3 compatibility glue.
149 * - Moved randomization code into pick_from_range().
151 * - Changed parse_header_time definition.
152 * time_t isn't guaranteed to be signed and
153 * if it isn't, -1 isn't available as error code.
154 * Changed some variable types in client_if_modified_since()
155 * because of the same reason.
157 * Revision 1.63 2006/08/14 13:18:08 david__schmidt
158 * OS/2 compilation compatibility fixups
160 * Revision 1.62 2006/08/14 08:58:42 fabiankeil
161 * Changed include from strptime.c to strptime.h
163 * Revision 1.61 2006/08/14 08:25:19 fabiankeil
164 * Split filter-headers{} into filter-client-headers{}
165 * and filter-server-headers{}.
166 * Added parse_header_time() to share some code.
167 * Replaced timegm() with mktime().
169 * Revision 1.60 2006/08/12 03:54:37 david__schmidt
170 * Windows service integration
172 * Revision 1.59 2006/08/03 02:46:41 david__schmidt
173 * Incorporate Fabian Keil's patch work:
\rhttp://www.fabiankeil.de/sourcecode/privoxy/
175 * Revision 1.58 2006/07/18 14:48:47 david__schmidt
176 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
177 * with what was really the latest development (the v_3_0_branch branch)
179 * Revision 1.56.2.10 2006/01/21 16:16:08 david__schmidt
180 * Thanks to Edward Carrel for his patch to modernize OSX's
\rpthreads support. See bug #1409623.
182 * Revision 1.56.2.9 2004/10/03 12:53:45 david__schmidt
183 * Add the ability to check jpeg images for invalid
184 * lengths of comment blocks. Defensive strategy
185 * against the exploit:
186 * Microsoft Security Bulletin MS04-028
187 * Buffer Overrun in JPEG Processing (GDI+) Could
188 * Allow Code Execution (833987)
189 * Enabled with +inspect-jpegs in actions files.
191 * Revision 1.56.2.8 2003/07/11 13:21:25 oes
192 * Excluded text/plain objects from filtering. This fixes a
193 * couple of client-crashing, download corruption and
194 * Privoxy performance issues, whose root cause lies in
195 * web servers labelling content of unknown type as text/plain.
197 * Revision 1.56.2.7 2003/05/06 12:07:26 oes
198 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
200 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
201 * Completing the previous change
203 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
204 * Added temporary workaround for bug in PHP < 4.2.3
206 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
207 * 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.
209 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
210 * Fix typo: supressed -> suppressed
212 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
213 * Improved cookie logging
215 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
216 * Added basic support for OPTIONS and TRACE HTTP methods:
217 * - New parser function client_max_forwards which decrements
218 * the Max-Forwards HTTP header field of OPTIONS and TRACE
219 * requests by one before forwarding
220 * - New parser function client_host which extracts the host
221 * and port information from the HTTP header field if the
222 * request URI was not absolute
223 * - Don't crumble and re-add the Host: header, but only generate
224 * and append if missing
226 * Revision 1.56 2002/05/12 15:34:22 jongfoster
227 * Fixing typo in a comment
229 * Revision 1.55 2002/05/08 16:01:07 oes
230 * Optimized add_to_iob:
231 * - Use realloc instead of malloc(), memcpy(), free()
232 * - Expand to powers of two if possible, to get
233 * O(log n) reallocs instead of O(n).
234 * - Moved check for buffer limit here from chat
235 * - Report failure via returncode
237 * Revision 1.54 2002/04/02 15:03:16 oes
238 * Tiny code cosmetics
240 * Revision 1.53 2002/03/26 22:29:55 swa
241 * we have a new homepage!
243 * Revision 1.52 2002/03/24 13:25:43 swa
244 * name change related issues
246 * Revision 1.51 2002/03/13 00:27:05 jongfoster
249 * Revision 1.50 2002/03/12 01:45:35 oes
250 * More verbose logging
252 * Revision 1.49 2002/03/09 20:03:52 jongfoster
253 * - Making various functions return int rather than size_t.
254 * (Undoing a recent change). Since size_t is unsigned on
255 * Windows, functions like read_socket that return -1 on
256 * error cannot return a size_t.
258 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
259 * crashes, and also frequently caused JB to jump to 100%
260 * CPU and stay there. (Because it thought it had just
261 * read ((unsigned)-1) == 4Gb of data...)
263 * - The signature of write_socket has changed, it now simply
264 * returns success=0/failure=nonzero.
266 * - Trying to get rid of a few warnings --with-debug on
267 * Windows, I've introduced a new type "jb_socket". This is
268 * used for the socket file descriptors. On Windows, this
269 * is SOCKET (a typedef for unsigned). Everywhere else, it's
270 * an int. The error value can't be -1 any more, so it's
271 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
272 * Windows it maps to the #define INVALID_SOCKET.)
274 * - The signature of bind_port has changed.
276 * Revision 1.48 2002/03/07 03:46:53 oes
277 * Fixed compiler warnings etc
279 * Revision 1.47 2002/02/20 23:15:13 jongfoster
280 * Parsing functions now handle out-of-memory gracefully by returning
283 * Revision 1.46 2002/01/17 21:03:47 jongfoster
284 * Moving all our URL and URL pattern parsing code to urlmatch.c.
286 * Revision 1.45 2002/01/09 14:33:03 oes
287 * Added support for localtime_r.
289 * Revision 1.44 2001/12/14 01:22:54 steudten
290 * Remove 'user:pass@' from 'proto://user:pass@host' for the
291 * new added header 'Host: ..'. (See Req ID 491818)
293 * Revision 1.43 2001/11/23 00:26:38 jongfoster
294 * Fixing two really stupid errors in my previous commit
296 * Revision 1.42 2001/11/22 21:59:30 jongfoster
297 * Adding code to handle +no-cookies-keep
299 * Revision 1.41 2001/11/05 23:43:05 steudten
300 * Add time+date to log files.
302 * Revision 1.40 2001/10/26 20:13:09 jongfoster
303 * ctype.h is needed in Windows, too.
305 * Revision 1.39 2001/10/26 17:40:04 oes
306 * Introduced get_header_value()
307 * Removed http->user_agent, csp->referrer and csp->accept_types
308 * Removed client_accept()
310 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
311 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
312 * threads to call select() simultaneously. So, it's time to do a real, live,
313 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
314 * (native). Both versions will work, but using __OS2__ offers multi-threading.
316 * Revision 1.37 2001/10/23 21:36:02 jongfoster
317 * Documenting sed()'s error behaviou (doc change only)
319 * Revision 1.36 2001/10/13 12:51:51 joergs
320 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
321 * force-load), instead crumble Host: and add it (again) in client_host_adder
322 * (in case we get a HTTP/1.0 request without Host: header and forward it to
323 * a HTTP/1.1 server/proxy).
325 * Revision 1.35 2001/10/09 22:39:21 jongfoster
326 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
329 * Revision 1.34 2001/10/07 18:50:55 oes
330 * Added server_content_encoding, renamed server_transfer_encoding
332 * Revision 1.33 2001/10/07 18:04:49 oes
333 * Changed server_http11 to server_http and its pattern to "HTTP".
334 * Additional functionality: it now saves the HTTP status into
335 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
337 * Revision 1.32 2001/10/07 15:43:28 oes
338 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
339 * client_te and client_accept_encoding_adder, triggered by the new
340 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
341 * changed to allow only identity and chunked, and the TE header is
342 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
344 * parse_http_request no longer does anything than parsing. The rewriting
345 * of http->cmd and version mangling are gone. It now also recognizes
346 * the put and delete methods and saves the url in http->url. Removed
349 * renamed content_type and content_length to have the server_ prefix
351 * server_content_type now only works if csp->content_type != CT_TABOO
353 * added server_transfer_encoding, which
354 * - Sets CT_TABOO to prohibit filtering if encoding compresses
355 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
356 * - Change from "chunked" to "identity" if body was chunked
357 * but has been de-chunked for filtering.
359 * added server_content_md5 which crunches any Content-MD5 headers
360 * if the body was modified.
362 * made server_http11 conditional on +downgrade action
364 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
366 * Revision 1.31 2001/10/05 14:25:02 oes
367 * Crumble Keep-Alive from Server
369 * Revision 1.30 2001/09/29 12:56:03 joergs
370 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
372 * Revision 1.29 2001/09/24 21:09:24 jongfoster
373 * Fixing 2 memory leaks that Guy spotted, where the paramater to
374 * enlist() was not being free()d.
376 * Revision 1.28 2001/09/22 16:32:28 jongfoster
377 * Removing unused #includes.
379 * Revision 1.27 2001/09/20 15:45:25 steudten
381 * add casting from size_t to int for printf()
382 * remove local variable shadow s2
384 * Revision 1.26 2001/09/16 17:05:14 jongfoster
385 * Removing unused #include showarg.h
387 * Revision 1.25 2001/09/16 13:21:27 jongfoster
388 * Changes to use new list functions.
390 * Revision 1.24 2001/09/13 23:05:50 jongfoster
391 * Changing the string paramater to the header parsers a "const".
393 * Revision 1.23 2001/09/12 18:08:19 steudten
395 * In parse_http_request() header rewriting miss the host value, so
396 * from http://www.mydomain.com the result was just " / " not
397 * http://www.mydomain.com/ in case we forward.
399 * Revision 1.22 2001/09/10 10:58:53 oes
400 * Silenced compiler warnings
402 * Revision 1.21 2001/07/31 14:46:00 oes
403 * - Persistant connections now suppressed
404 * - sed() no longer appends empty header to csp->headers
406 * Revision 1.20 2001/07/30 22:08:36 jongfoster
407 * Tidying up #defines:
408 * - All feature #defines are now of the form FEATURE_xxx
409 * - Permanently turned off WIN_GUI_EDIT
410 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
412 * Revision 1.19 2001/07/25 17:21:54 oes
413 * client_uagent now saves copy of User-Agent: header value
415 * Revision 1.18 2001/07/13 14:02:46 oes
416 * - Included fix to repair broken HTTP requests that
417 * don't contain a path, not even '/'.
418 * - Removed all #ifdef PCRS
419 * - content_type now always inspected and classified as
420 * text, gif or other.
421 * - formatting / comments
423 * Revision 1.17 2001/06/29 21:45:41 oes
424 * Indentation, CRLF->LF, Tab-> Space
426 * Revision 1.16 2001/06/29 13:32:42 oes
428 * - Adapted free_http_request
429 * - Removed logentry from cancelled commit
431 * Revision 1.15 2001/06/03 19:12:38 oes
432 * deleted const struct interceptors
434 * Revision 1.14 2001/06/01 18:49:17 jongfoster
435 * Replaced "list_share" with "list" - the tiny memory gain was not
436 * worth the extra complexity.
438 * Revision 1.13 2001/05/31 21:30:33 jongfoster
439 * Removed list code - it's now in list.[ch]
440 * Renamed "permission" to "action", and changed many features
441 * to use the actions file rather than the global config.
443 * Revision 1.12 2001/05/31 17:33:13 oes
447 * Revision 1.11 2001/05/29 20:11:19 joergs
448 * '/ * inside comment' warning removed.
450 * Revision 1.10 2001/05/29 09:50:24 jongfoster
451 * Unified blocklist/imagelist/permissionslist.
452 * File format is still under discussion, but the internal changes
455 * Also modified interceptor behaviour:
456 * - We now intercept all URLs beginning with one of the following
457 * prefixes (and *only* these prefixes):
459 * * http://ijbswa.sf.net/config/
460 * * http://ijbswa.sourceforge.net/config/
461 * - New interceptors "home page" - go to http://i.j.b/ to see it.
462 * - Internal changes so that intercepted and fast redirect pages
463 * are not replaced with an image.
464 * - Interceptors now have the option to send a binary page direct
465 * to the client. (i.e. ijb-send-banner uses this)
466 * - Implemented show-url-info interceptor. (Which is why I needed
467 * the above interceptors changes - a typical URL is
468 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
469 * The previous mechanism would not have intercepted that, and
470 * if it had been intercepted then it then it would have replaced
473 * Revision 1.9 2001/05/28 17:26:33 jongfoster
474 * Fixing segfault if last header was crunched.
475 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
476 * can use the cross-platform sprintf() instead.)
478 * Revision 1.8 2001/05/27 22:17:04 oes
480 * - re_process_buffer no longer writes the modified buffer
481 * to the client, which was very ugly. It now returns the
482 * buffer, which it is then written by chat.
484 * - content_length now adjusts the Content-Length: header
485 * for modified documents rather than crunch()ing it.
486 * (Length info in csp->content_length, which is 0 for
487 * unmodified documents)
489 * - For this to work, sed() is called twice when filtering.
491 * Revision 1.7 2001/05/27 13:19:06 oes
492 * Patched Joergs solution for the content-length in.
494 * Revision 1.6 2001/05/26 13:39:32 jongfoster
495 * Only crunches Content-Length header if applying RE filtering.
496 * Without this fix, Microsoft Windows Update wouldn't work.
498 * Revision 1.5 2001/05/26 00:28:36 jongfoster
499 * Automatic reloading of config file.
500 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
501 * Most of the global variables have been moved to a new
502 * struct configuration_spec, accessed through csp->config->globalname
503 * Most of the globals remaining are used by the Win32 GUI.
505 * Revision 1.4 2001/05/22 18:46:04 oes
507 * - Enabled filtering banners by size rather than URL
508 * by adding patterns that replace all standard banner
509 * sizes with the "Junkbuster" gif to the re_filterfile
511 * - Enabled filtering WebBugs by providing a pattern
512 * which kills all 1x1 images
514 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
515 * which is selected by the (nonstandard and therefore
516 * capital) letter 'U' in the option string.
517 * It causes the quantifiers to be ungreedy by default.
518 * Appending a ? turns back to greedy (!).
520 * - Added a new interceptor ijb-send-banner, which
521 * sends back the "Junkbuster" gif. Without imagelist or
522 * MSIE detection support, or if tinygif = 1, or the
523 * URL isn't recognized as an imageurl, a lame HTML
524 * explanation is sent instead.
526 * - Added new feature, which permits blocking remote
527 * script redirects and firing back a local redirect
529 * The feature is conditionally compiled, i.e. it
530 * can be disabled with --disable-fast-redirects,
531 * plus it must be activated by a "fast-redirects"
532 * line in the config file, has its own log level
533 * and of course wants to be displayed by show-proxy-args
534 * Note: Boy, all the #ifdefs in 1001 locations and
535 * all the fumbling with configure.in and acconfig.h
536 * were *way* more work than the feature itself :-(
538 * - Because a generic redirect template was needed for
539 * this, tinygif = 3 now uses the same.
541 * - Moved GIFs, and other static HTTP response templates
546 * - Removed some >400 CRs again (Jon, you really worked
549 * Revision 1.3 2001/05/20 01:21:20 jongfoster
550 * Version 2.9.4 checkin.
551 * - Merged popupfile and cookiefile, and added control over PCRS
552 * filtering, in new "permissionsfile".
553 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
554 * file error you now get a message box (in the Win32 GUI) rather
555 * than the program exiting with no explanation.
556 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
558 * - Removed tabs from "config"
559 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
560 * - Bumped up version number.
562 * Revision 1.2 2001/05/17 23:02:36 oes
563 * - Made referrer option accept 'L' as a substitute for '§'
565 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
566 * Initial import of version 2.9.3 source tree
569 *********************************************************************/
576 #include <sys/types.h>
589 #if !defined(_WIN32) && !defined(__OS2__)
595 #ifdef FEATURE_PTHREAD
597 /* jcc.h is for mutex semapores only */
598 #endif /* def FEATURE_PTHREAD */
604 #include "jbsockets.h"
605 #include "miscutil.h"
608 #ifndef HAVE_STRPTIME
609 #include "strptime.h"
612 const char parsers_h_rcs[] = PARSERS_H_VERSION;
614 /* Fix a problem with Solaris. There should be no effect on other
616 * Solaris's isspace() is a macro which uses it's argument directly
617 * as an array index. Therefore we need to make sure that high-bit
618 * characters generate +ve values, and ideally we also want to make
619 * the argument match the declared parameter type of "int".
621 * Why did they write a character function that can't take a simple
622 * "char" argument? Doh!
624 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
625 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
628 const struct parsers client_patterns[] = {
629 { "referer:", 8, client_referrer },
630 { "user-agent:", 11, client_uagent },
631 { "ua-", 3, client_ua },
632 { "from:", 5, client_from },
633 { "cookie:", 7, client_send_cookie },
634 { "x-forwarded-for:", 16, client_x_forwarded },
635 { "Accept-Encoding:", 16, client_accept_encoding },
636 { "TE:", 3, client_te },
637 { "Host:", 5, client_host },
638 { "if-modified-since:", 18, client_if_modified_since },
639 { "Keep-Alive:", 11, crumble },
640 { "connection:", 11, crumble },
641 { "proxy-connection:", 17, crumble },
642 { "max-forwards:", 13, client_max_forwards },
643 { "Accept-Language:", 16, client_accept_language },
644 { "if-none-match:", 14, client_if_none_match },
645 { "X-Filter:", 9, client_x_filter },
646 { "*", 0, crunch_client_header },
647 { "*", 0, filter_client_header },
651 const struct parsers server_patterns[] = {
652 { "HTTP", 4, server_http },
653 { "set-cookie:", 11, server_set_cookie },
654 { "connection:", 11, crumble },
655 { "Content-Type:", 13, server_content_type },
656 { "Content-Length:", 15, server_content_length },
657 { "Content-MD5:", 12, server_content_md5 },
658 { "Content-Encoding:", 17, server_content_encoding },
659 { "Transfer-Encoding:", 18, server_transfer_coding },
660 { "Keep-Alive:", 11, crumble },
661 { "content-disposition:", 20, server_content_disposition },
662 { "Last-Modified:", 14, server_last_modified },
663 { "*", 0, crunch_server_header },
664 { "*", 0, filter_server_header },
668 const struct parsers server_patterns_light[] = {
669 { "Content-Length:", 15, server_content_length },
670 { "Transfer-Encoding:", 18, server_transfer_coding },
672 { "Content-Encoding:", 17, server_content_encoding },
673 #endif /* def FEATURE_ZLIB */
677 const add_header_func_ptr add_client_headers[] = {
680 client_x_forwarded_adder,
682 /* Temporarily disabled: client_accept_encoding_adder, */
683 connection_close_adder,
688 const add_header_func_ptr add_server_headers[] = {
689 connection_close_adder,
693 /*********************************************************************
695 * Function : flush_socket
697 * Description : Write any pending "buffered" content.
700 * 1 : fd = file descriptor of the socket to read
701 * 2 : csp = Current client state (buffers, headers, etc...)
703 * Returns : On success, the number of bytes written are returned (zero
704 * indicates nothing was written). On error, -1 is returned,
705 * and errno is set appropriately. If count is zero and the
706 * file descriptor refers to a regular file, 0 will be
707 * returned without causing any other effect. For a special
708 * file, the results are not portable.
710 *********************************************************************/
711 int flush_socket(jb_socket fd, struct client_state *csp)
713 struct iob *iob = csp->iob;
714 int len = iob->eod - iob->cur;
721 if (write_socket(fd, iob->cur, (size_t)len))
725 iob->eod = iob->cur = iob->buf;
731 /*********************************************************************
733 * Function : add_to_iob
735 * Description : Add content to the buffered page, expanding the
736 * buffer if necessary.
739 * 1 : csp = Current client state (buffers, headers, etc...)
740 * 2 : buf = holds the content to be added to the page
741 * 3 : n = number of bytes to be added
743 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
744 * or buffer limit reached.
746 *********************************************************************/
747 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
749 struct iob *iob = csp->iob;
750 size_t used, offset, need, want;
753 if (n <= 0) return JB_ERR_OK;
755 used = (size_t)(iob->eod - iob->buf);
756 offset = (size_t)(iob->cur - iob->buf);
757 need = used + (size_t)n + 1;
760 * If the buffer can't hold the new data, extend it first.
761 * Use the next power of two if possible, else use the actual need.
763 if (need > csp->config->buffer_limit)
765 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
766 return JB_ERR_MEMORY;
769 if (need > iob->size)
771 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
773 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
777 else if (NULL != (p = (char *)realloc(iob->buf, need)))
783 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
784 return JB_ERR_MEMORY;
787 /* Update the iob pointers */
788 iob->cur = p + offset;
793 /* copy the new data into the iob buffer */
794 memcpy(iob->eod, buf, (size_t)n);
796 /* point to the end of the data */
799 /* null terminate == cheap insurance */
808 /*********************************************************************
810 * Function : decompress_iob
812 * Description : Decompress buffered page, expanding the
813 * buffer as necessary. csp->iob->cur
814 * should point to the the beginning of the
815 * compressed data block.
818 * 1 : csp = Current client state (buffers, headers, etc...)
820 * Returns : JB_ERR_OK on success,
821 * JB_ERR_MEMORY if out-of-memory limit reached, and
822 * JB_ERR_COMPRESS if error decompressing buffer.
824 *********************************************************************/
825 jb_err decompress_iob(struct client_state *csp)
827 char *buf; /* new, uncompressed buffer */
828 size_t bufsize; /* allocated size of the new buffer */
829 size_t skip_size; /* Number of bytes at the beginning of the iob
830 that we should NOT decompress. */
831 int status; /* return status of the inflate() call */
832 z_stream zstr; /* used by calls to zlib */
834 bufsize = csp->iob->size;
835 skip_size = (size_t)(csp->iob->cur - csp->iob->buf);
840 * This is to protect the parsing of gzipped data,
841 * but it should(?) be valid for deflated data also.
843 log_error (LOG_LEVEL_ERROR, "Buffer too small decompressing iob");
844 return JB_ERR_COMPRESS;
847 if (csp->content_type & CT_GZIP)
850 * Our task is slightly complicated by the facts that data
851 * compressed by gzip does not include a zlib header, and
852 * that there is no easily accessible interface in zlib to
853 * handle a gzip header. We strip off the gzip header by
854 * hand, and later inform zlib not to expect a header.
858 * Strip off the gzip header. Please see RFC 1952 for more
859 * explanation of the appropriate fields.
861 if ((*csp->iob->cur++ != (char)0x1f)
862 || (*csp->iob->cur++ != (char)0x8b)
863 || (*csp->iob->cur++ != Z_DEFLATED))
865 log_error (LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
866 return JB_ERR_COMPRESS;
870 int flags = *csp->iob->cur++;
872 * XXX: These magic numbers should be replaced
873 * with macros to give a better idea what they do.
877 /* The gzip header has reserved bits set; bail out. */
878 log_error (LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
879 return JB_ERR_COMPRESS;
883 /* Skip extra fields if necessary. */
887 * Skip a given number of bytes, specified
888 * as a 16-bit little-endian value.
890 csp->iob->cur += *csp->iob->cur++ + (*csp->iob->cur++ << 8);
893 /* Skip the filename if necessary. */
896 /* A null-terminated string follows. */
897 while (*csp->iob->cur++);
900 /* Skip the comment if necessary. */
903 while (*csp->iob->cur++);
906 /* Skip the CRC if necessary. */
913 else if (csp->content_type & CT_DEFLATE)
916 * XXX: The debug level should be lowered
917 * before the next stable release.
919 log_error (LOG_LEVEL_INFO, "Decompressing deflated iob: %d", *csp->iob->cur);
921 * In theory (that is, according to RFC 1950), deflate-compressed
922 * data should begin with a two-byte zlib header and have an
923 * adler32 checksum at the end. It seems that in practice only
924 * the raw compressed data is sent. Note that this means that
925 * we are not RFC 1950-compliant here, but the advantage is that
926 * this actually works. :)
928 * We add a dummy null byte to tell zlib where the data ends,
929 * and later inform it not to expect a header.
931 * Fortunately, add_to_iob() has thoughtfully null-terminated
932 * the buffer; we can just increment the end pointer to include
939 log_error (LOG_LEVEL_ERROR,
940 "Unable to determine compression format for decompression");
941 return JB_ERR_COMPRESS;
944 /* Set up the fields required by zlib. */
945 zstr.next_in = (Bytef *)csp->iob->cur;
946 zstr.avail_in = (unsigned int)(csp->iob->eod - csp->iob->cur);
947 zstr.zalloc = Z_NULL;
949 zstr.opaque = Z_NULL;
952 * Passing -MAX_WBITS to inflateInit2 tells the library
953 * that there is no zlib header.
955 if (inflateInit2 (&zstr, -MAX_WBITS) != Z_OK)
957 log_error (LOG_LEVEL_ERROR, "Error initializing decompression");
958 return JB_ERR_COMPRESS;
962 * Next, we allocate new storage for the inflated data.
963 * We don't modify the existing iob yet, so in case there
964 * is error in decompression we can recover gracefully.
966 buf = zalloc (bufsize);
969 log_error (LOG_LEVEL_ERROR, "Out of memory decompressing iob");
970 return JB_ERR_MEMORY;
973 assert(bufsize >= skip_size);
974 memcpy(buf, csp->iob->buf, skip_size);
975 zstr.avail_out = bufsize - skip_size;
976 zstr.next_out = (Bytef *)buf + skip_size;
978 /* Try to decompress the whole stream in one shot. */
979 while (Z_BUF_ERROR == (status = inflate(&zstr, Z_FINISH)))
981 /* We need to allocate more memory for the output buffer. */
983 char *tmpbuf; /* used for realloc'ing the buffer */
984 size_t oldbufsize = bufsize; /* keep track of the old bufsize */
987 * If zlib wants more data then there's a problem, because
988 * the complete compressed file should have been buffered.
990 if (0 == zstr.avail_in)
992 log_error(LOG_LEVEL_ERROR, "Unexpected end of compressed iob");
993 return JB_ERR_COMPRESS;
997 * If we tried the limit and still didn't have enough
998 * memory, just give up.
1000 if (bufsize == csp->config->buffer_limit)
1002 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while decompressing iob");
1003 return JB_ERR_MEMORY;
1006 /* Try doubling the buffer size each time. */
1009 /* Don't exceed the buffer limit. */
1010 if (bufsize > csp->config->buffer_limit)
1012 bufsize = csp->config->buffer_limit;
1015 /* Try to allocate the new buffer. */
1016 tmpbuf = realloc(buf, bufsize);
1019 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
1021 return JB_ERR_MEMORY;
1025 char *oldnext_out = (char *)zstr.next_out;
1028 * Update the fields for inflate() to use the new
1029 * buffer, which may be in a location different from
1032 zstr.avail_out += bufsize - oldbufsize;
1033 zstr.next_out = (Bytef *)tmpbuf + bufsize - zstr.avail_out;
1036 * Compare with an uglier method of calculating these values
1037 * that doesn't require the extra oldbufsize variable.
1039 assert(zstr.avail_out == tmpbuf + bufsize - (char *)zstr.next_out);
1040 assert((char *)zstr.next_out == tmpbuf + ((char *)oldnext_out - buf));
1041 assert(zstr.avail_out > 0);
1047 if (Z_STREAM_ERROR == inflateEnd(&zstr))
1049 log_error(LOG_LEVEL_ERROR,
1050 "Inconsistent stream state after decompression: %s", zstr.msg);
1052 * XXX: Intentionally no return.
1054 * According to zlib.h, Z_STREAM_ERROR is returned
1055 * "if the stream state was inconsistent".
1057 * I assume in this case inflate()'s status
1058 * would also be something different than Z_STREAM_END
1059 * so this check should be redundant, but lets see.
1063 if (status != Z_STREAM_END)
1065 /* We failed to decompress the stream. */
1066 log_error(LOG_LEVEL_ERROR,
1067 "Error in decompressing to the buffer (iob): %s", zstr.msg);
1068 return JB_ERR_COMPRESS;
1072 * Finally, we can actually update the iob, since the
1073 * decompression was successful. First, free the old
1076 freez(csp->iob->buf);
1078 /* Now, update the iob to use the new buffer. */
1079 csp->iob->buf = buf;
1080 csp->iob->cur = csp->iob->buf + skip_size;
1081 csp->iob->eod = (char *)zstr.next_out;
1082 csp->iob->size = bufsize;
1085 * Make sure the new uncompressed iob obeys some minimal
1086 * consistency conditions.
1088 if ((csp->iob->buf < csp->iob->cur)
1089 && (csp->iob->cur <= csp->iob->eod)
1090 && (csp->iob->eod <= csp->iob->buf + csp->iob->size))
1092 char t = csp->iob->cur[100];
1093 csp->iob->cur[100] = '\0';
1095 * XXX: The debug level should be lowered
1096 * before the next stable release.
1098 log_error(LOG_LEVEL_INFO, "Sucessfully decompressed: %s", csp->iob->cur);
1099 csp->iob->cur[100] = t;
1104 /* It seems that zlib did something weird. */
1105 log_error(LOG_LEVEL_ERROR,
1106 "Unexpected error decompressing the buffer (iob): %d==%d, %d>%d, %d<%d",
1107 csp->iob->cur, csp->iob->buf + skip_size, csp->iob->eod, csp->iob->buf,
1108 csp->iob->eod, csp->iob->buf + csp->iob->size);
1109 return JB_ERR_COMPRESS;
1113 #endif /* defined(FEATURE_ZLIB) */
1116 /*********************************************************************
1118 * Function : get_header
1120 * Description : This (odd) routine will parse the csp->iob
1123 * 1 : csp = Current client state (buffers, headers, etc...)
1125 * Returns : Any one of the following:
1127 * 1) a pointer to a dynamically allocated string that contains a header line
1128 * 2) NULL indicating that the end of the header was reached
1129 * 3) "" indicating that the end of the iob was reached before finding
1130 * a complete header line.
1132 *********************************************************************/
1133 char *get_header(struct client_state *csp)
1139 if ((iob->cur == NULL)
1140 || ((p = strchr(iob->cur, '\n')) == NULL))
1142 return(""); /* couldn't find a complete header */
1147 ret = strdup(iob->cur);
1150 /* FIXME No way to handle error properly */
1151 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
1156 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
1158 /* is this a blank line (i.e. the end of the header) ? */
1170 /*********************************************************************
1172 * Function : get_header_value
1174 * Description : Get the value of a given header from a chained list
1175 * of header lines or return NULL if no such header is
1176 * present in the list.
1179 * 1 : header_list = pointer to list
1180 * 2 : header_name = string with name of header to look for.
1181 * Trailing colon required, capitalization
1184 * Returns : NULL if not found, else value of header
1186 *********************************************************************/
1187 char *get_header_value(const struct list *header_list, const char *header_name)
1189 struct list_entry *cur_entry;
1193 assert(header_list);
1194 assert(header_name);
1195 length = strlen(header_name);
1197 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
1201 if (!strncmpic(cur_entry->str, header_name, length))
1204 * Found: return pointer to start of value
1206 ret = (char *) (cur_entry->str + length);
1207 while (*ret && ijb_isspace(*ret)) ret++;
1220 /*********************************************************************
1224 * Description : add, delete or modify lines in the HTTP header streams.
1225 * On entry, it receives a linked list of headers space
1226 * that was allocated dynamically (both the list nodes
1227 * and the header contents).
1229 * As a side effect it frees the space used by the original
1233 * 1 : pats = list of patterns to match against headers
1234 * 2 : more_headers = list of functions to add more
1235 * headers (client or server)
1236 * 3 : csp = Current client state (buffers, headers, etc...)
1238 * Returns : Single pointer to a fully formed header, or NULL
1239 * on out-of-memory error.
1241 *********************************************************************/
1242 char *sed(const struct parsers pats[],
1243 const add_header_func_ptr more_headers[],
1244 struct client_state *csp)
1246 struct list_entry *p;
1247 const struct parsers *v;
1248 const add_header_func_ptr *f;
1249 jb_err err = JB_ERR_OK;
1253 * If filtering is enabled, sed is run twice,
1254 * but most of the work needs to be done only once.
1256 first_run = (more_headers != NULL ) ? 1 : 0;
1258 if (first_run) /* Parse and print */
1260 log_error(LOG_LEVEL_HEADER, "scanning headers for: %s", csp->http->url);
1261 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
1263 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
1265 /* Header crunch()ed in previous run? -> ignore */
1266 if (p->str == NULL) continue;
1268 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
1270 /* Does the current parser handle this header? */
1271 if ((strncmpic(p->str, v->str, v->len) == 0) || (v->len == CHECK_EVERY_HEADER_REMAINING))
1273 err = v->parser(csp, (char **)&(p->str));
1277 /* place any additional headers on the csp->headers list */
1278 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
1283 else /* Parse only */
1286 * The second run is only needed if the body was modified
1287 * and the content-lenght has changed.
1289 if (strncmpic(csp->http->cmd, "HEAD", 4))
1291 /*XXX: Code duplication */
1292 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
1294 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
1296 /* Header crunch()ed in previous run? -> ignore */
1297 if (p->str == NULL) continue;
1299 /* Does the current parser handle this header? */
1300 if (strncmpic(p->str, v->str, v->len) == 0)
1302 err = v->parser(csp, (char **)&(p->str));
1309 if (err != JB_ERR_OK)
1314 return list_to_text(csp->headers);
1318 /* here begins the family of parser functions that reformat header lines */
1320 /*********************************************************************
1322 * Function : filter_server_header
1324 * Description : Checks if server header filtering is enabled.
1325 * If it is, filter_header is called to do the work.
1328 * 1 : csp = Current client state (buffers, headers, etc...)
1329 * 2 : header = On input, pointer to header to modify.
1330 * On output, pointer to the modified header, or NULL
1331 * to remove the header. This function frees the
1332 * original string if necessary.
1334 * Returns : JB_ERR_OK on success and always succeeds
1336 *********************************************************************/
1337 jb_err filter_server_header(struct client_state *csp, char **header)
1339 if (csp->action->flags & ACTION_FILTER_SERVER_HEADERS)
1341 filter_header(csp, header);
1346 /*********************************************************************
1348 * Function : filter_client_header
1350 * Description : Checks if client header filtering is enabled.
1351 * If it is, filter_header is called to do the work.
1354 * 1 : csp = Current client state (buffers, headers, etc...)
1355 * 2 : header = On input, pointer to header to modify.
1356 * On output, pointer to the modified header, or NULL
1357 * to remove the header. This function frees the
1358 * original string if necessary.
1360 * Returns : JB_ERR_OK on success and always succeeds
1362 *********************************************************************/
1363 jb_err filter_client_header(struct client_state *csp, char **header)
1365 if (csp->action->flags & ACTION_FILTER_CLIENT_HEADERS)
1367 filter_header(csp, header);
1372 /*********************************************************************
1374 * Function : filter_header
1376 * Description : Executes all text substitutions from all applying
1377 * +filter actions on the header.
1378 * Most of the code was copied from pcrs_filter_response,
1379 * including the rather short variable names
1382 * 1 : csp = Current client state (buffers, headers, etc...)
1383 * 2 : header = On input, pointer to header to modify.
1384 * On output, pointer to the modified header, or NULL
1385 * to remove the header. This function frees the
1386 * original string if necessary.
1388 * Returns : JB_ERR_OK on success and always succeeds
1390 *********************************************************************/
1391 jb_err filter_header(struct client_state *csp, char **header)
1395 size_t size = strlen(*header);
1397 char *newheader = NULL;
1400 struct file_list *fl;
1401 struct re_filterfile_spec *b;
1402 struct list_entry *filtername;
1404 int i, found_filters = 0;
1407 * Need to check the set of re_filterfiles...
1409 for (i = 0; i < MAX_AF_FILES; i++)
1422 if (0 == found_filters)
1424 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1428 for (i = 0; i < MAX_AF_FILES; i++)
1431 if ((NULL == fl) || (NULL == fl->f))
1434 * Either there are no filter files
1435 * left, or this filter file just
1436 * contains no valid filters.
1438 * Continue to be sure we don't miss
1439 * valid filter files that are chained
1440 * after empty or invalid ones.
1445 * For all applying +filter actions, look if a filter by that
1446 * name exists and if yes, execute its pcrs_joblist on the
1449 for (b = fl->f; b; b = b->next)
1451 for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
1452 filtername ; filtername = filtername->next)
1454 if (strcmp(b->name, filtername->str) == 0)
1456 int current_hits = 0;
1458 if ( NULL == b->joblist )
1460 log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1464 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s (size %d) with filter %s...",
1465 *header, size, b->name);
1467 /* Apply all jobs from the joblist */
1468 for (job = b->joblist; NULL != job; job = job->next)
1470 matches = pcrs_execute(job, *header, size, &newheader, &size);
1473 current_hits += matches;
1474 log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
1476 *header = newheader;
1478 else if ( 0 == matches )
1480 /* Filter doesn't change header */
1486 log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
1487 *header, b->name, pcrs_strerror(matches));
1488 if( newheader != NULL)
1490 log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
1495 log_error(LOG_LEVEL_RE_FILTER, " ...produced %d hits (new size %d).", current_hits, size);
1496 hits += current_hits;
1504 log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
1512 /*********************************************************************
1514 * Function : crumble
1516 * Description : This is called if a header matches a pattern to "crunch"
1519 * 1 : csp = Current client state (buffers, headers, etc...)
1520 * 2 : header = On input, pointer to header to modify.
1521 * On output, pointer to the modified header, or NULL
1522 * to remove the header. This function frees the
1523 * original string if necessary.
1525 * Returns : JB_ERR_OK on success, or
1526 * JB_ERR_MEMORY on out-of-memory error.
1528 *********************************************************************/
1529 jb_err crumble(struct client_state *csp, char **header)
1531 log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
1536 /*********************************************************************
1538 * Function : crunch_server_header
1540 * Description : Crunch server header if it matches a string supplied by the
1541 * user. Called from `sed'.
1544 * 1 : csp = Current client state (buffers, headers, etc...)
1545 * 2 : header = On input, pointer to header to modify.
1546 * On output, pointer to the modified header, or NULL
1547 * to remove the header. This function frees the
1548 * original string if necessary.
1550 * Returns : JB_ERR_OK on success and always succeeds
1552 *********************************************************************/
1553 jb_err crunch_server_header(struct client_state *csp, char **header)
1555 const char *crunch_pattern;
1556 /*Is there a header to crunch*/
1558 if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
1560 crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
1562 /*Is the current header the lucky one?*/
1563 if (strstr(*header, crunch_pattern))
1565 log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
1574 /*********************************************************************
1576 * Function : server_content_type
1578 * Description : Set the content-type for filterable types (text/.*,
1579 * .*xml.*, javascript and image/gif) unless filtering has been
1580 * forbidden (CT_TABOO) while parsing earlier headers.
1581 * NOTE: Since text/plain is commonly used by web servers
1582 * for files whose correct type is unknown, we don't
1583 * set CT_TEXT for it.
1586 * 1 : csp = Current client state (buffers, headers, etc...)
1587 * 2 : header = On input, pointer to header to modify.
1588 * On output, pointer to the modified header, or NULL
1589 * to remove the header. This function frees the
1590 * original string if necessary.
1592 * Returns : JB_ERR_OK on success, or
1593 * JB_ERR_MEMORY on out-of-memory error.
1595 *********************************************************************/
1596 jb_err server_content_type(struct client_state *csp, char **header)
1598 /* Remove header if it isn't the first Content-Type header */
1599 if(csp->content_type && (csp->content_type != CT_TABOO))
1602 * Another, slightly slower, way to see if
1603 * we already parsed another Content-Type header.
1605 assert(NULL != get_header_value(csp->headers, "Content-Type:"));
1607 log_error(LOG_LEVEL_ERROR,
1608 "Multiple Content-Type headers. Removing and ignoring: \'%s\'",
1615 if (!(csp->content_type & CT_TABOO))
1617 if ((strstr(*header, " text/") && !strstr(*header, "plain"))
1618 || strstr(*header, "xml")
1619 || strstr(*header, "application/x-javascript"))
1621 csp->content_type |= CT_TEXT;
1623 else if (strstr(*header, " image/gif"))
1625 csp->content_type |= CT_GIF;
1627 else if (strstr(*header, " image/jpeg"))
1629 csp->content_type |= CT_JPEG;
1633 csp->content_type = 0;
1637 * Are we enabling text mode by force?
1639 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
1642 * Do we really have to?
1644 if (csp->content_type & CT_TEXT)
1646 log_error(LOG_LEVEL_HEADER, "Text mode is already enabled.");
1650 csp->content_type |= CT_TEXT;
1651 log_error(LOG_LEVEL_HEADER, "Text mode enabled by force. Take cover!");
1655 * Are we messing with the content type?
1657 if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
1660 * Make sure the user doesn't accidently
1661 * change the content type of binary documents.
1663 if (csp->content_type & CT_TEXT)
1666 *header = strdup("Content-Type: ");
1667 string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
1671 log_error(LOG_LEVEL_HEADER, "Insufficient memory to replace Content-Type!");
1672 return JB_ERR_MEMORY;
1674 log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
1678 log_error(LOG_LEVEL_HEADER, "%s not replaced. It doesn't look like text. "
1679 "Enable force-text-mode if you know what you're doing.", *header);
1686 /*********************************************************************
1688 * Function : server_transfer_coding
1690 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
1691 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
1692 * - Remove header if body was chunked but has been
1693 * de-chunked for filtering.
1696 * 1 : csp = Current client state (buffers, headers, etc...)
1697 * 2 : header = On input, pointer to header to modify.
1698 * On output, pointer to the modified header, or NULL
1699 * to remove the header. This function frees the
1700 * original string if necessary.
1702 * Returns : JB_ERR_OK on success, or
1703 * JB_ERR_MEMORY on out-of-memory error.
1705 *********************************************************************/
1706 jb_err server_transfer_coding(struct client_state *csp, char **header)
1709 * Turn off pcrs and gif filtering if body compressed
1711 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1715 * XXX: Added to test if we could use CT_GZIP and CT_DEFLATE here.
1717 log_error(LOG_LEVEL_INFO, "Marking content type for %s as CT_TABOO because of %s.",
1718 csp->http->cmd, *header);
1719 #endif /* def FEATURE_ZLIB */
1720 csp->content_type = CT_TABOO;
1724 * Raise flag if body chunked
1726 if (strstr(*header, "chunked"))
1728 csp->flags |= CSP_FLAG_CHUNKED;
1731 * If the body was modified, it has been de-chunked first
1732 * and the header must be removed.
1734 * FIXME: If there is more than one transfer encoding,
1735 * only the "chunked" part should be removed here.
1737 if (csp->flags & CSP_FLAG_MODIFIED)
1739 log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
1748 /*********************************************************************
1750 * Function : server_content_encoding
1752 * Description : This function is run twice for each request,
1753 * unless FEATURE_ZLIB and filtering are disabled.
1755 * The first run is used to check if the content
1756 * is compressed, if FEATURE_ZLIB is disabled
1757 * filtering is then disabled as well, if FEATURE_ZLIB
1758 * is enabled the content is marked for decompression.
1760 * The second run is used to remove the Content-Encoding
1761 * header if the decompression was successful.
1764 * 1 : csp = Current client state (buffers, headers, etc...)
1765 * 2 : header = On input, pointer to header to modify.
1766 * On output, pointer to the modified header, or NULL
1767 * to remove the header. This function frees the
1768 * original string if necessary.
1770 * Returns : JB_ERR_OK on success, or
1771 * JB_ERR_MEMORY on out-of-memory error.
1773 *********************************************************************/
1774 jb_err server_content_encoding(struct client_state *csp, char **header)
1777 /* XXX: Why would we modify the content if it was taboo? */
1778 if ((csp->flags & CSP_FLAG_MODIFIED) && !(csp->content_type & CT_TABOO))
1781 * We successfully decompressed the content,
1782 * and have to clean the header now, so the
1783 * client no longer expects compressed data..
1785 * XXX: There is a difference between cleaning
1786 * and removing it completely.
1788 log_error(LOG_LEVEL_HEADER, "Crunching: %s", *header);
1791 else if (strstr(*header, "gzip"))
1793 /* Mark for gzip decompression */
1794 csp->content_type |= CT_GZIP;
1796 else if (strstr(*header, "deflate"))
1798 /* Mark for zlib decompression */
1799 csp->content_type |= CT_DEFLATE;
1801 else if (strstr(*header, "compress"))
1804 * We can't decompress this; therefore we can't filter
1807 csp->content_type |= CT_TABOO;
1809 #else /* !defined(FEATURE_ZLIB) */
1810 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1813 * Body is compressed, turn off pcrs and gif filtering.
1815 csp->content_type |= CT_TABOO;
1817 #endif /* !defined(FEATURE_ZLIB) */
1824 /*********************************************************************
1826 * Function : server_content_length
1828 * Description : Adjust Content-Length header if we modified
1832 * 1 : csp = Current client state (buffers, headers, etc...)
1833 * 2 : header = On input, pointer to header to modify.
1834 * On output, pointer to the modified header, or NULL
1835 * to remove the header. This function frees the
1836 * original string if necessary.
1838 * Returns : JB_ERR_OK on success, or
1839 * JB_ERR_MEMORY on out-of-memory error.
1841 *********************************************************************/
1842 jb_err server_content_length(struct client_state *csp, char **header)
1844 const size_t max_header_length = 80;
1845 if (csp->content_length != 0) /* Content length could have been modified */
1848 * XXX: Shouldn't we check if csp->content_length
1849 * is different than the original value?
1852 *header = (char *) zalloc(max_header_length);
1853 if (*header == NULL)
1855 return JB_ERR_MEMORY;
1858 snprintf(*header, max_header_length, "Content-Length: %d",
1859 (int)csp->content_length);
1860 log_error(LOG_LEVEL_HEADER, "Adjusted Content-Length to %d",
1861 (int)csp->content_length);
1868 /*********************************************************************
1870 * Function : server_content_md5
1872 * Description : Crumble any Content-MD5 headers if the document was
1873 * modified. FIXME: Should we re-compute instead?
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 server_content_md5(struct client_state *csp, char **header)
1888 if (csp->flags & CSP_FLAG_MODIFIED)
1890 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1897 /*********************************************************************
1899 * Function : server_content_disposition
1901 * Description : If enabled, blocks or modifies the "content-disposition" header.
1902 * Called from `sed'.
1905 * 1 : csp = Current client state (buffers, headers, etc...)
1906 * 2 : header = On input, pointer to header to modify.
1907 * On output, pointer to the modified header, or NULL
1908 * to remove the header. This function frees the
1909 * original string if necessary.
1911 * Returns : JB_ERR_OK on success, or
1912 * JB_ERR_MEMORY on out-of-memory error.
1914 *********************************************************************/
1915 jb_err server_content_disposition(struct client_state *csp, char **header)
1920 * Are we messing with the content-disposition header?
1922 if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
1928 newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
1930 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1933 * Blocking content-disposition header
1935 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1942 * Replacing content-disposition header
1945 *header = strdup("content-disposition: ");
1946 string_append(header, newval);
1948 if (*header == NULL)
1950 log_error(LOG_LEVEL_HEADER, "Insufficent memory. content-disposition header not fully replaced.");
1954 log_error(LOG_LEVEL_HEADER, "content-disposition header crunched and replaced with: %s", *header);
1957 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1960 /*********************************************************************
1962 * Function : server_last_modified
1964 * Description : Changes Last-Modified header to the actual date
1965 * to help hide-if-modified-since.
1966 * Called from `sed'.
1969 * 1 : csp = Current client state (buffers, headers, etc...)
1970 * 2 : header = On input, pointer to header to modify.
1971 * On output, pointer to the modified header, or NULL
1972 * to remove the header. This function frees the
1973 * original string if necessary.
1975 * Returns : JB_ERR_OK on success, or
1976 * JB_ERR_MEMORY on out-of-memory error.
1978 *********************************************************************/
1979 jb_err server_last_modified(struct client_state *csp, char **header)
1982 char buf[BUFFER_SIZE];
1985 #ifdef HAVE_GMTIME_R
1988 struct tm *timeptr = NULL;
1989 time_t now, last_modified;
1991 long int days, hours, minutes, seconds;
1994 * Are we messing with the Last-Modified header?
1996 if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
2002 newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
2004 if (0 == strcmpic(newval, "block") )
2007 * Blocking Last-Modified header. Useless but why not.
2009 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2013 else if (0 == strcmpic(newval, "reset-to-request-time"))
2016 * Setting Last-Modified Header to now.
2018 get_http_time(0, buf);
2020 *header = strdup("Last-Modified: ");
2021 string_append(header, buf);
2023 if (*header == NULL)
2025 log_error(LOG_LEVEL_HEADER, "Insufficent memory. Last-Modified header got lost, boohoo.");
2029 log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
2032 else if (0 == strcmpic(newval, "randomize"))
2034 log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
2036 #ifdef HAVE_GMTIME_R
2037 timeptr = gmtime_r(&now, &gmt);
2038 #elif FEATURE_PTHREAD
2039 pthread_mutex_lock(&gmtime_mutex);
2040 timeptr = gmtime(&now);
2041 pthread_mutex_unlock(&gmtime_mutex);
2043 timeptr = gmtime(&now);
2045 if ((timeptr = parse_header_time(*header, &last_modified)) == NULL)
2047 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
2052 rtime = (long int)difftime(now, last_modified);
2055 rtime = pick_from_range(rtime);
2056 last_modified += rtime;
2057 #ifdef HAVE_GMTIME_R
2058 timeptr = gmtime_r(&last_modified, &gmt);
2059 #elif FEATURE_PTHREAD
2060 pthread_mutex_lock(&gmtime_mutex);
2061 timeptr = gmtime(&last_modified);
2062 pthread_mutex_unlock(&gmtime_mutex);
2064 timeptr = gmtime(&last_modified);
2066 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2068 *header = strdup("Last-Modified: ");
2069 string_append(header, newheader);
2071 if (*header == NULL)
2073 log_error(LOG_LEVEL_ERROR, "Insufficent memory, header crunched without replacement.");
2074 return JB_ERR_MEMORY;
2077 if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2079 days = rtime / (3600 * 24);
2080 hours = rtime / 3600 % 24;
2081 minutes = rtime / 60 % 60;
2082 seconds = rtime % 60;
2084 log_error(LOG_LEVEL_HEADER, "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
2085 *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
2086 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2091 log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
2099 /*********************************************************************
2101 * Function : client_accept_encoding
2103 * Description : Rewrite the client's Accept-Encoding header so that
2104 * if doesn't allow compression, if the action applies.
2105 * Note: For HTTP/1.0 the absence of the header is enough.
2108 * 1 : csp = Current client state (buffers, headers, etc...)
2109 * 2 : header = On input, pointer to header to modify.
2110 * On output, pointer to the modified header, or NULL
2111 * to remove the header. This function frees the
2112 * original string if necessary.
2114 * Returns : JB_ERR_OK on success, or
2115 * JB_ERR_MEMORY on out-of-memory error.
2117 *********************************************************************/
2118 jb_err client_accept_encoding(struct client_state *csp, char **header)
2120 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2122 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
2126 /* Temporarily disable the correct behaviour to
2127 * work around a PHP bug.
2129 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
2131 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
2132 * if (*header == NULL)
2134 * return JB_ERR_MEMORY;
2145 /*********************************************************************
2147 * Function : client_te
2149 * Description : Rewrite the client's TE header so that
2150 * if doesn't allow compression, if the action applies.
2153 * 1 : csp = Current client state (buffers, headers, etc...)
2154 * 2 : header = On input, pointer to header to modify.
2155 * On output, pointer to the modified header, or NULL
2156 * to remove the header. This function frees the
2157 * original string if necessary.
2159 * Returns : JB_ERR_OK on success, or
2160 * JB_ERR_MEMORY on out-of-memory error.
2162 *********************************************************************/
2163 jb_err client_te(struct client_state *csp, char **header)
2165 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2168 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
2174 /*********************************************************************
2176 * Function : client_referrer
2178 * Description : Handle the "referer" config setting properly.
2179 * Called from `sed'.
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_referrer(struct client_state *csp, char **header)
2199 #ifdef FEATURE_FORCE_LOAD
2200 /* Since the referrer can include the prefix even
2201 * if the request itself is non-forced, we must
2202 * clean it unconditionally
2204 strclean(*header, FORCE_PREFIX);
2205 #endif /* def FEATURE_FORCE_LOAD */
2208 * Are we sending referer?
2210 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
2215 newval = csp->action->string[ACTION_STRING_REFERER];
2217 if ((0 != strcmpic(newval, "conditional-block")))
2221 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2226 log_error(LOG_LEVEL_HEADER, "Referer crunched!");
2229 else if (0 == strcmpic(newval, "conditional-block"))
2232 * Block referer if host has changed.
2235 if (NULL == (host = strdup(csp->http->hostport)))
2238 log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary host copy.");
2239 return JB_ERR_MEMORY;
2241 if (NULL == (referer = strdup(*header)))
2245 log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary referer copy.");
2246 return JB_ERR_MEMORY;
2248 hostlenght = strlen(host);
2249 if ( hostlenght < (strlen(referer)-17) ) /*referer begins with 'Referer: http[s]://'*/
2251 /*Shorten referer to make sure the referer is blocked
2252 *if www.example.org/www.example.com-shall-see-the-referer/
2253 *links to www.example.com/
2255 referer[hostlenght+17] = '\0';
2257 if ( 0 == strstr(referer, host)) /*Host has changed*/
2259 log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
2264 log_error(LOG_LEVEL_HEADER, "%s (not modified, still on %s)", *header, host);
2270 else if (0 != strcmpic(newval, "forge"))
2273 * We have a specific (fixed) referer we want to send.
2275 if ((0 != strncmpic(newval, "http://", 7)) && (0 != strncmpic(newval, "https://", 8)))
2277 log_error(LOG_LEVEL_HEADER, "Parameter: +referrer{%s} is a bad idea, but I don't care.", newval);
2279 *header = strdup("Referer: ");
2280 string_append(header, newval);
2281 log_error(LOG_LEVEL_HEADER, "Referer overwritten with: %s", *header);
2283 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2288 * Forge a referer as http://[hostname:port of REQUEST]/
2289 * to fool stupid checks for in-site links
2292 *header = strdup("Referer: http://");
2293 string_append(header, csp->http->hostport);
2294 string_append(header, "/");
2295 log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
2297 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2301 /*********************************************************************
2303 * Function : client_accept_language
2305 * Description : Handle the "Accept-Language" config setting properly.
2306 * Called from `sed'.
2309 * 1 : csp = Current client state (buffers, headers, etc...)
2310 * 2 : header = On input, pointer to header to modify.
2311 * On output, pointer to the modified header, or NULL
2312 * to remove the header. This function frees the
2313 * original string if necessary.
2315 * Returns : JB_ERR_OK on success, or
2316 * JB_ERR_MEMORY on out-of-memory error.
2318 *********************************************************************/
2319 jb_err client_accept_language(struct client_state *csp, char **header)
2324 * Are we messing with the Accept-Language?
2326 if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
2328 /*I don't think so*/
2332 newval = csp->action->string[ACTION_STRING_LANGUAGE];
2334 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2337 * Blocking Accept-Language header
2339 log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
2346 * Replacing Accept-Language header
2349 *header = strdup("Accept-Language: ");
2350 string_append(header, newval);
2352 if (*header == NULL)
2354 log_error(LOG_LEVEL_ERROR,
2355 "Insufficent memory. Accept-Language header crunched without replacement.");
2359 log_error(LOG_LEVEL_HEADER,
2360 "Accept-Language header crunched and replaced with: %s", *header);
2363 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2366 /*********************************************************************
2368 * Function : crunch_client_header
2370 * Description : Crunch client header if it matches a string supplied by the
2371 * user. Called from `sed'.
2374 * 1 : csp = Current client state (buffers, headers, etc...)
2375 * 2 : header = On input, pointer to header to modify.
2376 * On output, pointer to the modified header, or NULL
2377 * to remove the header. This function frees the
2378 * original string if necessary.
2380 * Returns : JB_ERR_OK on success and always succeeds
2382 *********************************************************************/
2383 jb_err crunch_client_header(struct client_state *csp, char **header)
2385 const char *crunch_pattern;
2386 /*Is there a header to crunch*/
2388 if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
2390 crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
2392 /*Is the current header the lucky one?*/
2393 if (strstr(*header, crunch_pattern))
2395 log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
2403 /*********************************************************************
2405 * Function : client_uagent
2407 * Description : Handle the "user-agent" config setting properly
2408 * and remember its original value to enable browser
2409 * bug workarounds. Called from `sed'.
2412 * 1 : csp = Current client state (buffers, headers, etc...)
2413 * 2 : header = On input, pointer to header to modify.
2414 * On output, pointer to the modified header, or NULL
2415 * to remove the header. This function frees the
2416 * original string if necessary.
2418 * Returns : JB_ERR_OK on success, or
2419 * JB_ERR_MEMORY on out-of-memory error.
2421 *********************************************************************/
2422 jb_err client_uagent(struct client_state *csp, char **header)
2426 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
2431 newval = csp->action->string[ACTION_STRING_USER_AGENT];
2438 *header = strdup("User-Agent: ");
2439 string_append(header, newval);
2441 log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
2443 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2446 /*********************************************************************
2448 * Function : client_ua
2450 * Description : Handle "ua-" headers properly. Called from `sed'.
2453 * 1 : csp = Current client state (buffers, headers, etc...)
2454 * 2 : header = On input, pointer to header to modify.
2455 * On output, pointer to the modified header, or NULL
2456 * to remove the header. This function frees the
2457 * original string if necessary.
2459 * Returns : JB_ERR_OK on success, or
2460 * JB_ERR_MEMORY on out-of-memory error.
2462 *********************************************************************/
2463 jb_err client_ua(struct client_state *csp, char **header)
2465 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
2467 log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
2475 /*********************************************************************
2477 * Function : client_from
2479 * Description : Handle the "from" config setting properly.
2480 * Called from `sed'.
2483 * 1 : csp = Current client state (buffers, headers, etc...)
2484 * 2 : header = On input, pointer to header to modify.
2485 * On output, pointer to the modified header, or NULL
2486 * to remove the header. This function frees the
2487 * original string if necessary.
2489 * Returns : JB_ERR_OK on success, or
2490 * JB_ERR_MEMORY on out-of-memory error.
2492 *********************************************************************/
2493 jb_err client_from(struct client_state *csp, char **header)
2497 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
2504 newval = csp->action->string[ACTION_STRING_FROM];
2507 * Are we blocking the e-mail address?
2509 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
2511 log_error(LOG_LEVEL_HEADER, "crunched From!");
2515 log_error(LOG_LEVEL_HEADER, " modified");
2517 *header = strdup("From: ");
2518 string_append(header, newval);
2520 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2524 /*********************************************************************
2526 * Function : client_send_cookie
2528 * Description : Handle the "cookie" header properly. Called from `sed'.
2529 * If cookie is accepted, add it to the cookie_list,
2530 * else we crunch it. Mmmmmmmmmmm ... cookie ......
2533 * 1 : csp = Current client state (buffers, headers, etc...)
2534 * 2 : header = On input, pointer to header to modify.
2535 * On output, pointer to the modified header, or NULL
2536 * to remove the header. This function frees the
2537 * original string if necessary.
2539 * Returns : JB_ERR_OK on success, or
2540 * JB_ERR_MEMORY on out-of-memory error.
2542 *********************************************************************/
2543 jb_err client_send_cookie(struct client_state *csp, char **header)
2545 jb_err result = JB_ERR_OK;
2547 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
2549 /* strlen("cookie: ") == 8 */
2550 result = enlist(csp->cookie_list, *header + 8);
2554 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
2558 * Always remove the cookie here. The cookie header
2559 * will be sent at the end of the header.
2567 /*********************************************************************
2569 * Function : client_x_forwarded
2571 * Description : Handle the "x-forwarded-for" config setting properly,
2572 * also used in the add_client_headers list. Called from `sed'.
2575 * 1 : csp = Current client state (buffers, headers, etc...)
2576 * 2 : header = On input, pointer to header to modify.
2577 * On output, pointer to the modified header, or NULL
2578 * to remove the header. This function frees the
2579 * original string if necessary.
2581 * Returns : JB_ERR_OK on success, or
2582 * JB_ERR_MEMORY on out-of-memory error.
2584 *********************************************************************/
2585 jb_err client_x_forwarded(struct client_state *csp, char **header)
2587 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
2589 /* Save it so we can re-add it later */
2590 freez(csp->x_forwarded);
2591 csp->x_forwarded = *header;
2594 * Always set *header = NULL, since this information
2595 * will be sent at the end of the header.
2602 log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
2609 /*********************************************************************
2611 * Function : client_max_forwards
2613 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
2614 * from the value of the Max-Forwards header field.
2617 * 1 : csp = Current client state (buffers, headers, etc...)
2618 * 2 : header = On input, pointer to header to modify.
2619 * On output, pointer to the modified header, or NULL
2620 * to remove the header. This function frees the
2621 * original string if necessary.
2623 * Returns : JB_ERR_OK on success, or
2624 * JB_ERR_MEMORY on out-of-memory error.
2626 *********************************************************************/
2627 jb_err client_max_forwards(struct client_state *csp, char **header)
2629 unsigned int max_forwards;
2631 if ((0 == strcmpic(csp->http->gpc, "trace")) ||
2632 (0 == strcmpic(csp->http->gpc, "options")))
2634 if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
2636 if (max_forwards-- > 0)
2638 snprintf(*header, strlen(*header)+1, "Max-Forwards: %u", max_forwards);
2639 log_error(LOG_LEVEL_HEADER, "Max-Forwards header for %s request replaced with: %s",
2640 csp->http->gpc, *header);
2644 /* FIXME: Follow spec and intercept the request. */
2645 log_error(LOG_LEVEL_ERROR,
2646 "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
2655 /*********************************************************************
2657 * Function : client_host
2659 * Description : If the request URI did not contain host and
2660 * port information, parse and evaluate the Host
2663 * Also, kill ill-formed HOST: headers as sent by
2664 * Apple's iTunes software when used with a proxy.
2667 * 1 : csp = Current client state (buffers, headers, etc...)
2668 * 2 : header = On input, pointer to header to modify.
2669 * On output, pointer to the modified header, or NULL
2670 * to remove the header. This function frees the
2671 * original string if necessary.
2673 * Returns : JB_ERR_OK on success, or
2674 * JB_ERR_MEMORY on out-of-memory error.
2676 *********************************************************************/
2677 jb_err client_host(struct client_state *csp, char **header)
2682 * If the header field name is all upper-case, chances are that it's
2683 * an ill-formed one from iTunes. BTW, killing innocent headers here is
2684 * not a problem -- they are regenerated later.
2686 if ((*header)[1] == 'O')
2688 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
2693 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
2694 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
2697 if (NULL == (p = strdup((*header)+6)))
2699 return JB_ERR_MEMORY;
2702 if (NULL == (q = strdup(p)))
2705 return JB_ERR_MEMORY;
2708 freez(csp->http->hostport);
2709 csp->http->hostport = p;
2710 freez(csp->http->host);
2711 csp->http->host = q;
2712 q = strchr(csp->http->host, ':');
2715 /* Terminate hostname and evaluate port string */
2717 csp->http->port = atoi(q);
2721 csp->http->port = csp->http->ssl ? 443 : 80;
2724 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
2725 csp->http->hostport, csp->http->host, csp->http->port);
2731 /*********************************************************************
2733 * Function : client_if_modified_since
2735 * Description : Remove or modify the If-Modified-Since header.
2738 * 1 : csp = Current client state (buffers, headers, etc...)
2739 * 2 : header = On input, pointer to header to modify.
2740 * On output, pointer to the modified header, or NULL
2741 * to remove the header. This function frees the
2742 * original string if necessary.
2744 * Returns : JB_ERR_OK on success, or
2745 * JB_ERR_MEMORY on out-of-memory error.
2747 *********************************************************************/
2748 jb_err client_if_modified_since(struct client_state *csp, char **header)
2751 #ifdef HAVE_GMTIME_R
2754 struct tm *timeptr = NULL;
2758 long int hours, minutes, seconds;
2762 if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
2765 * The client got an error message because of a temporary problem,
2766 * the problem is gone and the client now tries to revalidate our
2767 * error message on the real server. The revalidation would always
2768 * end with the transmission of the whole document and there is
2769 * no need to expose the bogus If-Modified-Since header.
2771 log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
2774 else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
2776 newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
2778 if ((0 == strcmpic(newval, "block")))
2780 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2783 else /* add random value */
2785 if ((timeptr = parse_header_time(*header, &tm)) == NULL)
2787 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
2792 rtime = strtol(newval, &endptr, 0);
2795 log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
2796 *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
2803 rtime = pick_from_range(rtime);
2807 log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
2810 tm += rtime * (negative ? -1 : 1);
2811 #ifdef HAVE_GMTIME_R
2812 timeptr = gmtime_r(&tm, &gmt);
2813 #elif FEATURE_PTHREAD
2814 pthread_mutex_lock(&gmtime_mutex);
2815 timeptr = gmtime(&tm);
2816 pthread_mutex_unlock(&gmtime_mutex);
2818 timeptr = gmtime(&tm);
2820 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2823 *header = strdup("If-Modified-Since: ");
2824 string_append(header, newheader);
2826 if (*header == NULL)
2828 log_error(LOG_LEVEL_HEADER, "Insufficent memory, header crunched without replacement.");
2829 return JB_ERR_MEMORY;
2832 if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2834 hours = rtime / 3600;
2835 minutes = rtime / 60 % 60;
2836 seconds = rtime % 60;
2838 log_error(LOG_LEVEL_HEADER, "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
2839 *header, (negative) ? "subtracted" : "added", hours, (hours == 1) ? "r" : "rs",
2840 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2849 /*********************************************************************
2851 * Function : client_if_none_match
2853 * Description : Remove the If-None-Match header.
2856 * 1 : csp = Current client state (buffers, headers, etc...)
2857 * 2 : header = On input, pointer to header to modify.
2858 * On output, pointer to the modified header, or NULL
2859 * to remove the header. This function frees the
2860 * original string if necessary.
2862 * Returns : JB_ERR_OK on success, or
2863 * JB_ERR_MEMORY on out-of-memory error.
2865 *********************************************************************/
2866 jb_err client_if_none_match(struct client_state *csp, char **header)
2868 if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
2870 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2877 /*********************************************************************
2879 * Function : client_x_filter
2881 * Description : Disables filtering if the client set "X-Filter: No".
2882 * Called from `sed'.
2885 * 1 : csp = Current client state (buffers, headers, etc...)
2886 * 2 : header = On input, pointer to header to modify.
2887 * On output, pointer to the modified header, or NULL
2888 * to remove the header. This function frees the
2889 * original string if necessary.
2891 * Returns : JB_ERR_OK on success
2893 *********************************************************************/
2894 jb_err client_x_filter(struct client_state *csp, char **header)
2896 if ( 0 == strcmpic(*header, "X-Filter: No"))
2898 if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
2900 log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
2904 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
2906 log_error(LOG_LEVEL_HEADER,
2907 "force-text-mode overruled the client's request to fetch without filtering!");
2911 csp->content_type = CT_TABOO;
2912 csp->action->flags &= ~ACTION_FILTER_SERVER_HEADERS;
2913 csp->action->flags &= ~ACTION_FILTER_CLIENT_HEADERS;
2914 log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
2916 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2923 /* the following functions add headers directly to the header list */
2925 /*********************************************************************
2927 * Function : client_host_adder
2929 * Description : Adds the Host: header field if it is missing.
2930 * Called from `sed'.
2933 * 1 : csp = Current client state (buffers, headers, etc...)
2935 * Returns : JB_ERR_OK on success, or
2936 * JB_ERR_MEMORY on out-of-memory error.
2938 *********************************************************************/
2939 jb_err client_host_adder(struct client_state *csp)
2944 if ( !csp->http->hostport || !*(csp->http->hostport))
2950 * remove 'user:pass@' from 'proto://user:pass@host'
2952 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
2958 p = csp->http->hostport;
2961 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
2962 err = enlist_unique_header(csp->headers, "Host", p);
2968 /*********************************************************************
2970 * Function : client_cookie_adder
2972 * Description : Used in the add_client_headers list. Called from `sed'.
2975 * 1 : csp = Current client state (buffers, headers, etc...)
2977 * Returns : JB_ERR_OK on success, or
2978 * JB_ERR_MEMORY on out-of-memory error.
2980 *********************************************************************/
2981 jb_err client_cookie_adder(struct client_state *csp)
2983 struct list_entry *lst;
2985 struct list_entry *list1 = csp->cookie_list->first;
2986 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
2987 int first_cookie = 1;
2990 if ((list1 == NULL) && (list2 == NULL))
2996 tmp = strdup("Cookie: ");
2998 for (lst = list1; lst ; lst = lst->next)
3006 string_append(&tmp, "; ");
3008 string_append(&tmp, lst->str);
3011 for (lst = list2; lst ; lst = lst->next)
3019 string_append(&tmp, "; ");
3021 string_join(&tmp, cookie_encode(lst->str));
3026 return JB_ERR_MEMORY;
3029 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
3030 err = enlist(csp->headers, tmp);
3036 /*********************************************************************
3038 * Function : client_accept_encoding_adder
3040 * Description : Add an Accept-Encoding header to the client's request
3041 * that disables compression if the action applies, and
3042 * the header is not already there. Called from `sed'.
3043 * Note: For HTTP/1.0, the absence of the header is enough.
3046 * 1 : csp = Current client state (buffers, headers, etc...)
3048 * Returns : JB_ERR_OK on success, or
3049 * JB_ERR_MEMORY on out-of-memory error.
3051 *********************************************************************/
3052 jb_err client_accept_encoding_adder(struct client_state *csp)
3054 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
3055 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
3057 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
3064 /*********************************************************************
3066 * Function : client_xtra_adder
3068 * Description : Used in the add_client_headers list. Called from `sed'.
3071 * 1 : csp = Current client state (buffers, headers, etc...)
3073 * Returns : JB_ERR_OK on success, or
3074 * JB_ERR_MEMORY on out-of-memory error.
3076 *********************************************************************/
3077 jb_err client_xtra_adder(struct client_state *csp)
3079 struct list_entry *lst;
3082 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
3083 lst ; lst = lst->next)
3085 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
3086 err = enlist(csp->headers, lst->str);
3098 /*********************************************************************
3100 * Function : client_x_forwarded_adder
3102 * Description : Used in the add_client_headers list. Called from `sed'.
3105 * 1 : csp = Current client state (buffers, headers, etc...)
3107 * Returns : JB_ERR_OK on success, or
3108 * JB_ERR_MEMORY on out-of-memory error.
3110 *********************************************************************/
3111 jb_err client_x_forwarded_adder(struct client_state *csp)
3116 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
3121 if (csp->x_forwarded)
3123 p = strdup(csp->x_forwarded);
3124 string_append(&p, ", ");
3128 p = strdup("X-Forwarded-For: ");
3130 string_append(&p, csp->ip_addr_str);
3134 return JB_ERR_MEMORY;
3137 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
3138 err = enlist(csp->headers, p);
3145 /*********************************************************************
3147 * Function : connection_close_adder
3149 * Description : Adds a "Connection: close" header to csp->headers
3150 * as a temporary fix for the needed but missing HTTP/1.1
3151 * support. Called from `sed'.
3152 * FIXME: This whole function shouldn't be neccessary!
3155 * 1 : csp = Current client state (buffers, headers, etc...)
3157 * Returns : JB_ERR_OK on success, or
3158 * JB_ERR_MEMORY on out-of-memory error.
3160 *********************************************************************/
3161 jb_err connection_close_adder(struct client_state *csp)
3163 log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
3164 return enlist(csp->headers, "Connection: close");
3168 /*********************************************************************
3170 * Function : server_http
3172 * Description : - Save the HTTP Status into csp->http->status
3173 * - Set CT_TABOO to prevent filtering if the answer
3174 * is a partial range (HTTP status 206)
3175 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
3179 * 1 : csp = Current client state (buffers, headers, etc...)
3180 * 2 : header = On input, pointer to header to modify.
3181 * On output, pointer to the modified header, or NULL
3182 * to remove the header. This function frees the
3183 * original string if necessary.
3185 * Returns : JB_ERR_OK on success, or
3186 * JB_ERR_MEMORY on out-of-memory error.
3188 *********************************************************************/
3189 jb_err server_http(struct client_state *csp, char **header)
3191 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
3192 if (csp->http->status == 206)
3194 csp->content_type = CT_TABOO;
3197 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
3200 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
3207 /*********************************************************************
3209 * Function : server_set_cookie
3211 * Description : Handle the server "cookie" header properly.
3212 * Log cookie to the jar file. Then "crunch" it,
3213 * or accept it. Called from `sed'.
3216 * 1 : csp = Current client state (buffers, headers, etc...)
3217 * 2 : header = On input, pointer to header to modify.
3218 * On output, pointer to the modified header, or NULL
3219 * to remove the header. This function frees the
3220 * original string if necessary.
3222 * Returns : JB_ERR_OK on success, or
3223 * JB_ERR_MEMORY on out-of-memory error.
3225 *********************************************************************/
3226 jb_err server_set_cookie(struct client_state *csp, char **header)
3228 #ifdef FEATURE_COOKIE_JAR
3229 if (csp->config->jar)
3232 * Write timestamp into outbuf.
3234 * Complex because not all OSs have tm_gmtoff or
3235 * the %z field in strftime()
3237 char tempbuf[ BUFFER_SIZE ];
3241 #ifdef HAVE_LOCALTIME_R
3242 tm_now = *localtime_r(&now, &tm_now);
3243 #elif FEATURE_PTHREAD
3244 pthread_mutex_lock(&localtime_mutex);
3245 tm_now = *localtime (&now);
3246 pthread_mutex_unlock(&localtime_mutex);
3248 tm_now = *localtime (&now);
3250 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
3252 /* strlen("set-cookie: ") = 12 */
3253 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
3255 #endif /* def FEATURE_COOKIE_JAR */
3257 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
3259 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
3260 return crumble(csp, header);
3262 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
3264 /* Flag whether or not to log a message */
3267 /* A variable to store the tag we're working on */
3270 /* Skip "Set-Cookie:" (11 characters) in header */
3271 cur_tag = *header + 11;
3273 /* skip whitespace between "Set-Cookie:" and value */
3274 while (*cur_tag && ijb_isspace(*cur_tag))
3279 /* Loop through each tag in the cookie */
3283 char *next_tag = strchr(cur_tag, ';');
3284 if (next_tag != NULL)
3286 /* Skip the ';' character itself */
3289 /* skip whitespace ";" and start of tag */
3290 while (*next_tag && ijb_isspace(*next_tag))
3297 /* "Next tag" is the end of the string */
3298 next_tag = cur_tag + strlen(cur_tag);
3301 /* Is this the "Expires" tag? */
3302 if (strncmpic(cur_tag, "expires=", 8) == 0)
3304 /* Delete the tag by copying the rest of the string over it.
3305 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
3306 * since the behaviour of strcpy is undefined for overlapping
3309 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
3311 /* That changed the header, need to issue a log message */
3314 /* Note that the next tag has now been moved to *cur_tag,
3315 * so we do not need to update the cur_tag pointer.
3320 /* Move on to next cookie tag */
3327 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
3335 #ifdef FEATURE_FORCE_LOAD
3336 /*********************************************************************
3338 * Function : strclean
3340 * Description : In-Situ-Eliminate all occurances of substring in
3344 * 1 : string = string to clean
3345 * 2 : substring = substring to eliminate
3347 * Returns : Number of eliminations
3349 *********************************************************************/
3350 int strclean(const char *string, const char *substring)
3356 len = strlen(substring);
3358 while((pos = strstr(string, substring)) != NULL)
3365 while (*p++ != '\0');
3372 #endif /* def FEATURE_FORCE_LOAD */
3374 /*********************************************************************
3376 * Function : parse_header_time
3378 * Description : Transforms time inside a HTTP header into
3379 * the usual time format.
3382 * 1 : header = header to parse
3383 * 2 : tm = storage for the resulting time in seconds
3385 * Returns : Time struct containing the header time, or
3386 * NULL in case of a parsing problems.
3388 *********************************************************************/
3389 struct tm *parse_header_time(char *header, time_t *tm) {
3393 struct tm * timeptr;
3396 * Initializing gmt to prevent time zone offsets.
3398 * While this is only necessary on some platforms
3399 * (mingw32 for example), I don't know how to
3400 * detect these automatically and doing it everywhere
3404 #ifdef HAVE_LOCALTIME_R
3405 gmt = *localtime_r(tm, &gmt);
3406 #elif FEATURE_PTHREAD
3407 pthread_mutex_lock(&localtime_mutex);
3408 gmt = *localtime(tm);
3409 pthread_mutex_unlock(&localtime_mutex);
3411 gmt = *localtime(tm);
3414 /* Skipping header name */
3415 timestring = strstr(header, ": ");
3416 if (strptime(timestring, ": %a, %d %b %Y %H:%M:%S", &gmt) == NULL)
3429 /*********************************************************************
3431 * Function : get_destination_from_headers
3433 * Description : Parse the "Host:" header to get the request's destination.
3434 * Only needed if the client's request was forcefully
3435 * redirected into Privoxy.
3437 * Code mainly copied from client_host() which is currently
3438 * run too late for this purpose.
3441 * 1 : headers = List of headers (one of them hopefully being
3442 * the "Host:" header)
3443 * 2 : http = storage for the result (host, port and hostport).
3445 * Returns : JB_ERR_MEMORY in case of memory problems,
3446 * JB_ERR_PARSE if the host header couldn't be found,
3447 * JB_ERR_OK otherwise.
3449 *********************************************************************/
3450 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
3456 host = get_header_value(headers, "Host:");
3460 log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
3461 return JB_ERR_PARSE;
3464 if (NULL == (p = strdup((host))))
3466 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
3467 return JB_ERR_MEMORY;
3470 if (NULL == (q = strdup(p)))
3473 log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
3474 return JB_ERR_MEMORY;
3477 freez(http->hostport);
3481 q = strchr(http->host, ':');
3484 /* Terminate hostname and evaluate port string */
3486 http->port = atoi(q);
3490 http->port = http->ssl ? 443 : 80;
3493 /* Rebuild request URL */
3495 http->url = strdup(http->ssl ? "https://" : "http://");
3496 string_append(&http->url, http->hostport);
3497 string_append(&http->url, http->path);
3498 if (http->url == NULL)
3500 return JB_ERR_MEMORY;
3503 log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header. New request URL: %s",