1 const char parsers_rcs[] = "$Id: parsers.c,v 1.62 2006/08/14 08:58:42 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',
14 * and `server_set_cookie'.
16 * Copyright : Written by and Copyright (C) 2001 the SourceForge
17 * Privoxy team. http://www.privoxy.org/
19 * Based on the Internet Junkbuster originally written
20 * by and Copyright (C) 1997 Anonymous Coders and
21 * Junkbusters Corporation. http://www.junkbusters.com
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will
30 * be useful, but WITHOUT ANY WARRANTY; without even the
31 * implied warranty of MERCHANTABILITY or FITNESS FOR A
32 * PARTICULAR PURPOSE. See the GNU General Public
33 * License for more details.
35 * The GNU General Public License should be included with
36 * this file. If not, you can view it at
37 * http://www.gnu.org/copyleft/gpl.html
38 * or write to the Free Software Foundation, Inc., 59
39 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 * Revision 1.62 2006/08/14 08:58:42 fabiankeil
44 * Changed include from strptime.c to strptime.h
46 * Revision 1.61 2006/08/14 08:25:19 fabiankeil
47 * Split filter-headers{} into filter-client-headers{}
48 * and filter-server-headers{}.
49 * Added parse_header_time() to share some code.
50 * Replaced timegm() with mktime().
52 * Revision 1.60 2006/08/12 03:54:37 david__schmidt
53 * Windows service integration
55 * Revision 1.59 2006/08/03 02:46:41 david__schmidt
56 * Incorporate Fabian Keil's patch work:
\rhttp://www.fabiankeil.de/sourcecode/privoxy/
58 * Revision 1.58 2006/07/18 14:48:47 david__schmidt
59 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
60 * with what was really the latest development (the v_3_0_branch branch)
62 * Revision 1.56.2.10 2006/01/21 16:16:08 david__schmidt
63 * Thanks to Edward Carrel for his patch to modernize OSX's
\rpthreads support. See bug #1409623.
65 * Revision 1.56.2.9 2004/10/03 12:53:45 david__schmidt
66 * Add the ability to check jpeg images for invalid
67 * lengths of comment blocks. Defensive strategy
68 * against the exploit:
69 * Microsoft Security Bulletin MS04-028
70 * Buffer Overrun in JPEG Processing (GDI+) Could
71 * Allow Code Execution (833987)
72 * Enabled with +inspect-jpegs in actions files.
74 * Revision 1.56.2.8 2003/07/11 13:21:25 oes
75 * Excluded text/plain objects from filtering. This fixes a
76 * couple of client-crashing, download corruption and
77 * Privoxy performance issues, whose root cause lies in
78 * web servers labelling content of unknown type as text/plain.
80 * Revision 1.56.2.7 2003/05/06 12:07:26 oes
81 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
83 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
84 * Completing the previous change
86 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
87 * Added temporary workaround for bug in PHP < 4.2.3
89 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
90 * 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.
92 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
93 * Fix typo: supressed -> suppressed
95 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
96 * Improved cookie logging
98 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
99 * Added basic support for OPTIONS and TRACE HTTP methods:
100 * - New parser function client_max_forwards which decrements
101 * the Max-Forwards HTTP header field of OPTIONS and TRACE
102 * requests by one before forwarding
103 * - New parser function client_host which extracts the host
104 * and port information from the HTTP header field if the
105 * request URI was not absolute
106 * - Don't crumble and re-add the Host: header, but only generate
107 * and append if missing
109 * Revision 1.56 2002/05/12 15:34:22 jongfoster
110 * Fixing typo in a comment
112 * Revision 1.55 2002/05/08 16:01:07 oes
113 * Optimized add_to_iob:
114 * - Use realloc instead of malloc(), memcpy(), free()
115 * - Expand to powers of two if possible, to get
116 * O(log n) reallocs instead of O(n).
117 * - Moved check for buffer limit here from chat
118 * - Report failure via returncode
120 * Revision 1.54 2002/04/02 15:03:16 oes
121 * Tiny code cosmetics
123 * Revision 1.53 2002/03/26 22:29:55 swa
124 * we have a new homepage!
126 * Revision 1.52 2002/03/24 13:25:43 swa
127 * name change related issues
129 * Revision 1.51 2002/03/13 00:27:05 jongfoster
132 * Revision 1.50 2002/03/12 01:45:35 oes
133 * More verbose logging
135 * Revision 1.49 2002/03/09 20:03:52 jongfoster
136 * - Making various functions return int rather than size_t.
137 * (Undoing a recent change). Since size_t is unsigned on
138 * Windows, functions like read_socket that return -1 on
139 * error cannot return a size_t.
141 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
142 * crashes, and also frequently caused JB to jump to 100%
143 * CPU and stay there. (Because it thought it had just
144 * read ((unsigned)-1) == 4Gb of data...)
146 * - The signature of write_socket has changed, it now simply
147 * returns success=0/failure=nonzero.
149 * - Trying to get rid of a few warnings --with-debug on
150 * Windows, I've introduced a new type "jb_socket". This is
151 * used for the socket file descriptors. On Windows, this
152 * is SOCKET (a typedef for unsigned). Everywhere else, it's
153 * an int. The error value can't be -1 any more, so it's
154 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
155 * Windows it maps to the #define INVALID_SOCKET.)
157 * - The signature of bind_port has changed.
159 * Revision 1.48 2002/03/07 03:46:53 oes
160 * Fixed compiler warnings etc
162 * Revision 1.47 2002/02/20 23:15:13 jongfoster
163 * Parsing functions now handle out-of-memory gracefully by returning
166 * Revision 1.46 2002/01/17 21:03:47 jongfoster
167 * Moving all our URL and URL pattern parsing code to urlmatch.c.
169 * Revision 1.45 2002/01/09 14:33:03 oes
170 * Added support for localtime_r.
172 * Revision 1.44 2001/12/14 01:22:54 steudten
173 * Remove 'user:pass@' from 'proto://user:pass@host' for the
174 * new added header 'Host: ..'. (See Req ID 491818)
176 * Revision 1.43 2001/11/23 00:26:38 jongfoster
177 * Fixing two really stupid errors in my previous commit
179 * Revision 1.42 2001/11/22 21:59:30 jongfoster
180 * Adding code to handle +no-cookies-keep
182 * Revision 1.41 2001/11/05 23:43:05 steudten
183 * Add time+date to log files.
185 * Revision 1.40 2001/10/26 20:13:09 jongfoster
186 * ctype.h is needed in Windows, too.
188 * Revision 1.39 2001/10/26 17:40:04 oes
189 * Introduced get_header_value()
190 * Removed http->user_agent, csp->referrer and csp->accept_types
191 * Removed client_accept()
193 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
194 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
195 * threads to call select() simultaneously. So, it's time to do a real, live,
196 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
197 * (native). Both versions will work, but using __OS2__ offers multi-threading.
199 * Revision 1.37 2001/10/23 21:36:02 jongfoster
200 * Documenting sed()'s error behaviou (doc change only)
202 * Revision 1.36 2001/10/13 12:51:51 joergs
203 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
204 * force-load), instead crumble Host: and add it (again) in client_host_adder
205 * (in case we get a HTTP/1.0 request without Host: header and forward it to
206 * a HTTP/1.1 server/proxy).
208 * Revision 1.35 2001/10/09 22:39:21 jongfoster
209 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
212 * Revision 1.34 2001/10/07 18:50:55 oes
213 * Added server_content_encoding, renamed server_transfer_encoding
215 * Revision 1.33 2001/10/07 18:04:49 oes
216 * Changed server_http11 to server_http and its pattern to "HTTP".
217 * Additional functionality: it now saves the HTTP status into
218 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
220 * Revision 1.32 2001/10/07 15:43:28 oes
221 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
222 * client_te and client_accept_encoding_adder, triggered by the new
223 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
224 * changed to allow only identity and chunked, and the TE header is
225 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
227 * parse_http_request no longer does anything than parsing. The rewriting
228 * of http->cmd and version mangling are gone. It now also recognizes
229 * the put and delete methods and saves the url in http->url. Removed
232 * renamed content_type and content_length to have the server_ prefix
234 * server_content_type now only works if csp->content_type != CT_TABOO
236 * added server_transfer_encoding, which
237 * - Sets CT_TABOO to prohibit filtering if encoding compresses
238 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
239 * - Change from "chunked" to "identity" if body was chunked
240 * but has been de-chunked for filtering.
242 * added server_content_md5 which crunches any Content-MD5 headers
243 * if the body was modified.
245 * made server_http11 conditional on +downgrade action
247 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
249 * Revision 1.31 2001/10/05 14:25:02 oes
250 * Crumble Keep-Alive from Server
252 * Revision 1.30 2001/09/29 12:56:03 joergs
253 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
255 * Revision 1.29 2001/09/24 21:09:24 jongfoster
256 * Fixing 2 memory leaks that Guy spotted, where the paramater to
257 * enlist() was not being free()d.
259 * Revision 1.28 2001/09/22 16:32:28 jongfoster
260 * Removing unused #includes.
262 * Revision 1.27 2001/09/20 15:45:25 steudten
264 * add casting from size_t to int for printf()
265 * remove local variable shadow s2
267 * Revision 1.26 2001/09/16 17:05:14 jongfoster
268 * Removing unused #include showarg.h
270 * Revision 1.25 2001/09/16 13:21:27 jongfoster
271 * Changes to use new list functions.
273 * Revision 1.24 2001/09/13 23:05:50 jongfoster
274 * Changing the string paramater to the header parsers a "const".
276 * Revision 1.23 2001/09/12 18:08:19 steudten
278 * In parse_http_request() header rewriting miss the host value, so
279 * from http://www.mydomain.com the result was just " / " not
280 * http://www.mydomain.com/ in case we forward.
282 * Revision 1.22 2001/09/10 10:58:53 oes
283 * Silenced compiler warnings
285 * Revision 1.21 2001/07/31 14:46:00 oes
286 * - Persistant connections now suppressed
287 * - sed() no longer appends empty header to csp->headers
289 * Revision 1.20 2001/07/30 22:08:36 jongfoster
290 * Tidying up #defines:
291 * - All feature #defines are now of the form FEATURE_xxx
292 * - Permanently turned off WIN_GUI_EDIT
293 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
295 * Revision 1.19 2001/07/25 17:21:54 oes
296 * client_uagent now saves copy of User-Agent: header value
298 * Revision 1.18 2001/07/13 14:02:46 oes
299 * - Included fix to repair broken HTTP requests that
300 * don't contain a path, not even '/'.
301 * - Removed all #ifdef PCRS
302 * - content_type now always inspected and classified as
303 * text, gif or other.
304 * - formatting / comments
306 * Revision 1.17 2001/06/29 21:45:41 oes
307 * Indentation, CRLF->LF, Tab-> Space
309 * Revision 1.16 2001/06/29 13:32:42 oes
311 * - Adapted free_http_request
312 * - Removed logentry from cancelled commit
314 * Revision 1.15 2001/06/03 19:12:38 oes
315 * deleted const struct interceptors
317 * Revision 1.14 2001/06/01 18:49:17 jongfoster
318 * Replaced "list_share" with "list" - the tiny memory gain was not
319 * worth the extra complexity.
321 * Revision 1.13 2001/05/31 21:30:33 jongfoster
322 * Removed list code - it's now in list.[ch]
323 * Renamed "permission" to "action", and changed many features
324 * to use the actions file rather than the global config.
326 * Revision 1.12 2001/05/31 17:33:13 oes
330 * Revision 1.11 2001/05/29 20:11:19 joergs
331 * '/ * inside comment' warning removed.
333 * Revision 1.10 2001/05/29 09:50:24 jongfoster
334 * Unified blocklist/imagelist/permissionslist.
335 * File format is still under discussion, but the internal changes
338 * Also modified interceptor behaviour:
339 * - We now intercept all URLs beginning with one of the following
340 * prefixes (and *only* these prefixes):
342 * * http://ijbswa.sf.net/config/
343 * * http://ijbswa.sourceforge.net/config/
344 * - New interceptors "home page" - go to http://i.j.b/ to see it.
345 * - Internal changes so that intercepted and fast redirect pages
346 * are not replaced with an image.
347 * - Interceptors now have the option to send a binary page direct
348 * to the client. (i.e. ijb-send-banner uses this)
349 * - Implemented show-url-info interceptor. (Which is why I needed
350 * the above interceptors changes - a typical URL is
351 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
352 * The previous mechanism would not have intercepted that, and
353 * if it had been intercepted then it then it would have replaced
356 * Revision 1.9 2001/05/28 17:26:33 jongfoster
357 * Fixing segfault if last header was crunched.
358 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
359 * can use the cross-platform sprintf() instead.)
361 * Revision 1.8 2001/05/27 22:17:04 oes
363 * - re_process_buffer no longer writes the modified buffer
364 * to the client, which was very ugly. It now returns the
365 * buffer, which it is then written by chat.
367 * - content_length now adjusts the Content-Length: header
368 * for modified documents rather than crunch()ing it.
369 * (Length info in csp->content_length, which is 0 for
370 * unmodified documents)
372 * - For this to work, sed() is called twice when filtering.
374 * Revision 1.7 2001/05/27 13:19:06 oes
375 * Patched Joergs solution for the content-length in.
377 * Revision 1.6 2001/05/26 13:39:32 jongfoster
378 * Only crunches Content-Length header if applying RE filtering.
379 * Without this fix, Microsoft Windows Update wouldn't work.
381 * Revision 1.5 2001/05/26 00:28:36 jongfoster
382 * Automatic reloading of config file.
383 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
384 * Most of the global variables have been moved to a new
385 * struct configuration_spec, accessed through csp->config->globalname
386 * Most of the globals remaining are used by the Win32 GUI.
388 * Revision 1.4 2001/05/22 18:46:04 oes
390 * - Enabled filtering banners by size rather than URL
391 * by adding patterns that replace all standard banner
392 * sizes with the "Junkbuster" gif to the re_filterfile
394 * - Enabled filtering WebBugs by providing a pattern
395 * which kills all 1x1 images
397 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
398 * which is selected by the (nonstandard and therefore
399 * capital) letter 'U' in the option string.
400 * It causes the quantifiers to be ungreedy by default.
401 * Appending a ? turns back to greedy (!).
403 * - Added a new interceptor ijb-send-banner, which
404 * sends back the "Junkbuster" gif. Without imagelist or
405 * MSIE detection support, or if tinygif = 1, or the
406 * URL isn't recognized as an imageurl, a lame HTML
407 * explanation is sent instead.
409 * - Added new feature, which permits blocking remote
410 * script redirects and firing back a local redirect
412 * The feature is conditionally compiled, i.e. it
413 * can be disabled with --disable-fast-redirects,
414 * plus it must be activated by a "fast-redirects"
415 * line in the config file, has its own log level
416 * and of course wants to be displayed by show-proxy-args
417 * Note: Boy, all the #ifdefs in 1001 locations and
418 * all the fumbling with configure.in and acconfig.h
419 * were *way* more work than the feature itself :-(
421 * - Because a generic redirect template was needed for
422 * this, tinygif = 3 now uses the same.
424 * - Moved GIFs, and other static HTTP response templates
429 * - Removed some >400 CRs again (Jon, you really worked
432 * Revision 1.3 2001/05/20 01:21:20 jongfoster
433 * Version 2.9.4 checkin.
434 * - Merged popupfile and cookiefile, and added control over PCRS
435 * filtering, in new "permissionsfile".
436 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
437 * file error you now get a message box (in the Win32 GUI) rather
438 * than the program exiting with no explanation.
439 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
441 * - Removed tabs from "config"
442 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
443 * - Bumped up version number.
445 * Revision 1.2 2001/05/17 23:02:36 oes
446 * - Made referrer option accept 'L' as a substitute for '§'
448 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
449 * Initial import of version 2.9.3 source tree
452 *********************************************************************/
459 #include <sys/types.h>
468 #if !defined(_WIN32) && !defined(__OS2__)
477 /* jcc.h is for mutex semapores only */
478 #endif /* def OSX_DARWIN */
484 #include "jbsockets.h"
485 #include "miscutil.h"
488 #ifndef HAVE_STRPTIME
489 #include "strptime.h"
492 const char parsers_h_rcs[] = PARSERS_H_VERSION;
494 /* Fix a problem with Solaris. There should be no effect on other
496 * Solaris's isspace() is a macro which uses it's argument directly
497 * as an array index. Therefore we need to make sure that high-bit
498 * characters generate +ve values, and ideally we also want to make
499 * the argument match the declared parameter type of "int".
501 * Why did they write a character function that can't take a simple
502 * "char" argument? Doh!
504 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
505 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
508 const struct parsers client_patterns[] = {
509 { "referer:", 8, client_referrer },
510 { "user-agent:", 11, client_uagent },
511 { "ua-", 3, client_ua },
512 { "from:", 5, client_from },
513 { "cookie:", 7, client_send_cookie },
514 { "x-forwarded-for:", 16, client_x_forwarded },
515 { "Accept-Encoding:", 16, client_accept_encoding },
516 { "TE:", 3, client_te },
517 { "Host:", 5, client_host },
518 { "if-modified-since:", 18, client_if_modified_since },
519 { "Keep-Alive:", 11, crumble },
520 { "connection:", 11, crumble },
521 { "proxy-connection:", 17, crumble },
522 { "max-forwards:", 13, client_max_forwards },
523 { "Accept-Language:", 16, client_accept_language },
524 { "if-none-match:", 14, client_if_none_match },
525 { "X-Filter:", 9, client_x_filter },
526 { "*", 0, crunch_client_header },
527 { "*", 0, filter_client_header },
531 const struct parsers server_patterns[] = {
532 { "HTTP", 4, server_http },
533 { "set-cookie:", 11, server_set_cookie },
534 { "connection:", 11, crumble },
535 { "Content-Type:", 13, server_content_type },
536 { "Content-Length:", 15, server_content_length },
537 { "Content-MD5:", 12, server_content_md5 },
538 { "Content-Encoding:", 17, server_content_encoding },
539 { "Transfer-Encoding:", 18, server_transfer_coding },
540 { "Keep-Alive:", 11, crumble },
541 { "content-disposition:", 20, server_content_disposition },
542 { "Last-Modified:", 14, server_last_modified },
543 { "*", 0, crunch_server_header },
544 { "*", 0, filter_server_header },
548 const struct parsers server_patterns_light[] = {
549 { "Content-Length:", 15, server_content_length },
550 { "Transfer-Encoding:", 18, server_transfer_coding },
554 const add_header_func_ptr add_client_headers[] = {
557 client_x_forwarded_adder,
559 /* Temporarily disabled: client_accept_encoding_adder, */
560 connection_close_adder,
565 const add_header_func_ptr add_server_headers[] = {
566 connection_close_adder,
570 /*********************************************************************
572 * Function : flush_socket
574 * Description : Write any pending "buffered" content.
577 * 1 : fd = file descriptor of the socket to read
578 * 2 : csp = Current client state (buffers, headers, etc...)
580 * Returns : On success, the number of bytes written are returned (zero
581 * indicates nothing was written). On error, -1 is returned,
582 * and errno is set appropriately. If count is zero and the
583 * file descriptor refers to a regular file, 0 will be
584 * returned without causing any other effect. For a special
585 * file, the results are not portable.
587 *********************************************************************/
588 int flush_socket(jb_socket fd, struct client_state *csp)
590 struct iob *iob = csp->iob;
591 int len = iob->eod - iob->cur;
598 if (write_socket(fd, iob->cur, (size_t)len))
602 iob->eod = iob->cur = iob->buf;
608 /*********************************************************************
610 * Function : add_to_iob
612 * Description : Add content to the buffered page, expanding the
613 * buffer if necessary.
616 * 1 : csp = Current client state (buffers, headers, etc...)
617 * 2 : buf = holds the content to be added to the page
618 * 3 : n = number of bytes to be added
620 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
621 * or buffer limit reached.
623 *********************************************************************/
624 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
626 struct iob *iob = csp->iob;
627 size_t used, offset, need, want;
630 if (n <= 0) return JB_ERR_OK;
632 used = iob->eod - iob->buf;
633 offset = iob->cur - iob->buf;
637 * If the buffer can't hold the new data, extend it first.
638 * Use the next power of two if possible, else use the actual need.
640 if (need > csp->config->buffer_limit)
642 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
643 return JB_ERR_MEMORY;
646 if (need > iob->size)
648 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
650 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
654 else if (NULL != (p = (char *)realloc(iob->buf, need)))
660 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
661 return JB_ERR_MEMORY;
664 /* Update the iob pointers */
665 iob->cur = p + offset;
670 /* copy the new data into the iob buffer */
671 memcpy(iob->eod, buf, (size_t)n);
673 /* point to the end of the data */
676 /* null terminate == cheap insurance */
684 /*********************************************************************
686 * Function : get_header
688 * Description : This (odd) routine will parse the csp->iob
691 * 1 : csp = Current client state (buffers, headers, etc...)
693 * Returns : Any one of the following:
695 * 1) a pointer to a dynamically allocated string that contains a header line
696 * 2) NULL indicating that the end of the header was reached
697 * 3) "" indicating that the end of the iob was reached before finding
698 * a complete header line.
700 *********************************************************************/
701 char *get_header(struct client_state *csp)
707 if ((iob->cur == NULL)
708 || ((p = strchr(iob->cur, '\n')) == NULL))
710 return(""); /* couldn't find a complete header */
715 ret = strdup(iob->cur);
718 /* FIXME No way to handle error properly */
719 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
724 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
726 /* is this a blank line (i.e. the end of the header) ? */
738 /*********************************************************************
740 * Function : get_header_value
742 * Description : Get the value of a given header from a chained list
743 * of header lines or return NULL if no such header is
744 * present in the list.
747 * 1 : header_list = pointer to list
748 * 2 : header_name = string with name of header to look for.
749 * Trailing colon required, capitalization
752 * Returns : NULL if not found, else value of header
754 *********************************************************************/
755 char *get_header_value(const struct list *header_list, const char *header_name)
757 struct list_entry *cur_entry;
763 length = strlen(header_name);
765 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
769 if (!strncmpic(cur_entry->str, header_name, length))
772 * Found: return pointer to start of value
774 ret = (char *) (cur_entry->str + length);
775 while (*ret && ijb_isspace(*ret)) ret++;
788 /*********************************************************************
792 * Description : add, delete or modify lines in the HTTP header streams.
793 * On entry, it receives a linked list of headers space
794 * that was allocated dynamically (both the list nodes
795 * and the header contents).
797 * As a side effect it frees the space used by the original
801 * 1 : pats = list of patterns to match against headers
802 * 2 : more_headers = list of functions to add more
803 * headers (client or server)
804 * 3 : csp = Current client state (buffers, headers, etc...)
806 * Returns : Single pointer to a fully formed header, or NULL
807 * on out-of-memory error.
809 *********************************************************************/
810 char *sed(const struct parsers pats[],
811 const add_header_func_ptr more_headers[],
812 struct client_state *csp)
814 struct list_entry *p;
815 const struct parsers *v;
816 const add_header_func_ptr *f;
817 jb_err err = JB_ERR_OK;
821 * If filtering is enabled, sed is run twice,
822 * but most of the work needs to be done only once.
824 first_run = (more_headers != NULL ) ? 1 : 0;
826 if (first_run) /* Parse and print */
828 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
830 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
832 /* Header crunch()ed in previous run? -> ignore */
833 if (p->str == NULL) continue;
835 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
837 /* Does the current parser handle this header? */
838 if ((strncmpic(p->str, v->str, v->len) == 0) || (v->len == CHECK_EVERY_HEADER_REMAINING))
840 err = v->parser(csp, (char **)&(p->str));
844 /* place any additional headers on the csp->headers list */
845 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
850 else /* Parse only */
853 * The second run is only needed if the body was modified
854 * and the content-lenght has changed.
856 if (strncmpic(csp->http->cmd, "HEAD", 4))
858 /*XXX: Code duplication*/
859 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
861 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
863 /* Header crunch()ed in previous run? -> ignore */
864 if (p->str == NULL) continue;
866 /* Does the current parser handle this header? */
867 if (strncmpic(p->str, v->str, v->len) == 0)
869 err = v->parser(csp, (char **)&(p->str));
876 if (err != JB_ERR_OK)
881 return list_to_text(csp->headers);
885 /* here begins the family of parser functions that reformat header lines */
887 /*********************************************************************
889 * Function : filter_server_header
891 * Description : Checks if server header filtering is enabled.
892 * If it is, filter_header is called to do the work.
895 * 1 : csp = Current client state (buffers, headers, etc...)
896 * 2 : header = On input, pointer to header to modify.
897 * On output, pointer to the modified header, or NULL
898 * to remove the header. This function frees the
899 * original string if necessary.
901 * Returns : JB_ERR_OK on success and always succeeds
903 *********************************************************************/
904 jb_err filter_server_header(struct client_state *csp, char **header)
906 if (csp->action->flags & ACTION_FILTER_SERVER_HEADERS)
908 filter_header(csp, header);
913 /*********************************************************************
915 * Function : filter_client_header
917 * Description : Checks if client header filtering is enabled.
918 * If it is, filter_header is called to do the work.
921 * 1 : csp = Current client state (buffers, headers, etc...)
922 * 2 : header = On input, pointer to header to modify.
923 * On output, pointer to the modified header, or NULL
924 * to remove the header. This function frees the
925 * original string if necessary.
927 * Returns : JB_ERR_OK on success and always succeeds
929 *********************************************************************/
930 jb_err filter_client_header(struct client_state *csp, char **header)
932 if (csp->action->flags & ACTION_FILTER_CLIENT_HEADERS)
934 filter_header(csp, header);
939 /*********************************************************************
941 * Function : filter_header
943 * Description : Executes all text substitutions from all applying
944 * +filter actions on the header.
945 * Most of the code was copied from pcrs_filter_response,
946 * including the rather short variable names
949 * 1 : csp = Current client state (buffers, headers, etc...)
950 * 2 : header = On input, pointer to header to modify.
951 * On output, pointer to the modified header, or NULL
952 * to remove the header. This function frees the
953 * original string if necessary.
955 * Returns : JB_ERR_OK on success and always succeeds
957 *********************************************************************/
958 jb_err filter_header(struct client_state *csp, char **header)
962 size_t size = strlen(*header);
964 char *newheader = NULL;
967 struct file_list *fl;
968 struct re_filterfile_spec *b;
969 struct list_entry *filtername;
971 int i, found_filters = 0;
974 # define MAX_AF_FILES 1
975 # define INDEX_OR_NOT
977 # define INDEX_OR_NOT [i]
980 log_error(LOG_LEVEL_RE_FILTER, "Entered filter_headers");
982 * Need to check the set of re_filterfiles...
984 for (i = 0; i < MAX_AF_FILES; i++)
986 fl = csp->rlist INDEX_OR_NOT;
997 if (0 == found_filters)
999 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1003 for (i = 0; i < MAX_AF_FILES; i++)
1005 fl = csp->rlist INDEX_OR_NOT;
1006 if ((NULL == fl) || (NULL == fl->f))
1009 * For all applying +filter actions, look if a filter by that
1010 * name exists and if yes, execute it's pcrs_joblist on the
1013 for (b = fl->f; b; b = b->next)
1015 for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
1016 filtername ; filtername = filtername->next)
1018 if (strcmp(b->name, filtername->str) == 0)
1020 int current_hits = 0;
1022 if ( NULL == b->joblist )
1024 log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1028 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s (size %d) with filter %s...",
1029 *header, size, b->name);
1031 /* Apply all jobs from the joblist */
1032 for (job = b->joblist; NULL != job; job = job->next)
1034 matches = pcrs_execute(job, *header, size, &newheader, &size);
1037 current_hits += matches;
1038 log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
1040 *header = newheader;
1042 else if ( 0 == matches )
1044 /* Filter doesn't change header */
1050 log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
1051 *header, b->name, pcrs_strerror(matches));
1052 if( newheader != NULL)
1054 log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
1059 log_error(LOG_LEVEL_RE_FILTER, " ...produced %d hits (new size %d).", current_hits, size);
1060 hits += current_hits;
1068 log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
1071 log_error(LOG_LEVEL_RE_FILTER, "Leaving filter headers");
1077 /*********************************************************************
1079 * Function : crumble
1081 * Description : This is called if a header matches a pattern to "crunch"
1084 * 1 : csp = Current client state (buffers, headers, etc...)
1085 * 2 : header = On input, pointer to header to modify.
1086 * On output, pointer to the modified header, or NULL
1087 * to remove the header. This function frees the
1088 * original string if necessary.
1090 * Returns : JB_ERR_OK on success, or
1091 * JB_ERR_MEMORY on out-of-memory error.
1093 *********************************************************************/
1094 jb_err crumble(struct client_state *csp, char **header)
1096 log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
1101 /*********************************************************************
1103 * Function : crunch_server_header
1105 * Description : Crunch server header if it matches a string supplied by the
1106 * user. Called from `sed'.
1109 * 1 : csp = Current client state (buffers, headers, etc...)
1110 * 2 : header = On input, pointer to header to modify.
1111 * On output, pointer to the modified header, or NULL
1112 * to remove the header. This function frees the
1113 * original string if necessary.
1115 * Returns : JB_ERR_OK on success and always succeeds
1117 *********************************************************************/
1118 jb_err crunch_server_header(struct client_state *csp, char **header)
1120 const char *crunch_pattern;
1121 /*Is there a header to crunch*/
1123 if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
1125 crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
1127 /*Is the current header the lucky one?*/
1128 if (strstr(*header, crunch_pattern))
1130 log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
1137 /*********************************************************************
1139 * Function : server_content_type
1141 * Description : Set the content-type for filterable types (text/.*,
1142 * javascript and image/gif) unless filtering has been
1143 * forbidden (CT_TABOO) while parsing earlier headers.
1144 * NOTE: Since text/plain is commonly used by web servers
1145 * for files whose correct type is unknown, we don't
1146 * set CT_TEXT for it.
1149 * 1 : csp = Current client state (buffers, headers, etc...)
1150 * 2 : header = On input, pointer to header to modify.
1151 * On output, pointer to the modified header, or NULL
1152 * to remove the header. This function frees the
1153 * original string if necessary.
1155 * Returns : JB_ERR_OK on success, or
1156 * JB_ERR_MEMORY on out-of-memory error.
1158 *********************************************************************/
1159 jb_err server_content_type(struct client_state *csp, char **header)
1163 newval = csp->action->string[ACTION_STRING_CONTENT_TYPE];
1165 if (csp->content_type != CT_TABOO)
1167 if ((strstr(*header, " text/") && !strstr(*header, "plain"))
1168 || strstr(*header, "xml")
1169 || strstr(*header, "application/x-javascript"))
1170 csp->content_type = CT_TEXT;
1171 else if (strstr(*header, " image/gif"))
1172 csp->content_type = CT_GIF;
1173 else if (strstr(*header, " image/jpeg"))
1174 csp->content_type = CT_JPEG;
1176 csp->content_type = 0;
1179 * Are we enabling text mode by force?
1181 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
1184 * Do we really have to?
1186 if (csp->content_type == CT_TEXT)
1188 log_error(LOG_LEVEL_HEADER, "Text mode is already enabled.");
1192 csp->content_type = CT_TEXT;
1193 log_error(LOG_LEVEL_HEADER, "Text mode enabled by force. Take cover!");
1197 * Are we messing with the content type?
1199 if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
1202 * Make sure the user doesn't accidently
1203 * change the content type of binary documents.
1205 if (csp->content_type == CT_TEXT)
1208 *header = strdup("Content-Type: ");
1209 string_append(header, newval);
1213 log_error(LOG_LEVEL_HEADER, "Insufficient memory. Conten-Type crunched without replacement!");
1214 return JB_ERR_MEMORY;
1216 log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
1220 log_error(LOG_LEVEL_HEADER, "%s not replaced. It doesn't look like text. "
1221 "Enable force-text-mode if you know what you're doing.", *header);
1228 /*********************************************************************
1230 * Function : server_transfer_coding
1232 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
1233 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
1234 * - Change from "chunked" to "identity" if body was chunked
1235 * but has been de-chunked for filtering.
1238 * 1 : csp = Current client state (buffers, headers, etc...)
1239 * 2 : header = On input, pointer to header to modify.
1240 * On output, pointer to the modified header, or NULL
1241 * to remove the header. This function frees the
1242 * original string if necessary.
1244 * Returns : JB_ERR_OK on success, or
1245 * JB_ERR_MEMORY on out-of-memory error.
1247 *********************************************************************/
1248 jb_err server_transfer_coding(struct client_state *csp, char **header)
1251 * Turn off pcrs and gif filtering if body compressed
1253 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1255 csp->content_type = CT_TABOO;
1259 * Raise flag if body chunked
1261 if (strstr(*header, "chunked"))
1263 csp->flags |= CSP_FLAG_CHUNKED;
1266 * If the body was modified, it has been
1267 * de-chunked first, so adjust the header:
1269 if (csp->flags & CSP_FLAG_MODIFIED)
1272 *header = strdup("Transfer-Encoding: identity");
1273 log_error(LOG_LEVEL_HEADER, "Set: %s", *header);
1274 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1282 /*********************************************************************
1284 * Function : server_content_encoding
1286 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
1289 * 1 : csp = Current client state (buffers, headers, etc...)
1290 * 2 : header = On input, pointer to header to modify.
1291 * On output, pointer to the modified header, or NULL
1292 * to remove the header. This function frees the
1293 * original string if necessary.
1295 * Returns : JB_ERR_OK on success, or
1296 * JB_ERR_MEMORY on out-of-memory error.
1298 *********************************************************************/
1299 jb_err server_content_encoding(struct client_state *csp, char **header)
1302 * Turn off pcrs and gif filtering if body compressed
1304 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
1306 csp->content_type = CT_TABOO;
1314 /*********************************************************************
1316 * Function : server_content_length
1318 * Description : Adjust Content-Length header if we modified
1322 * 1 : csp = Current client state (buffers, headers, etc...)
1323 * 2 : header = On input, pointer to header to modify.
1324 * On output, pointer to the modified header, or NULL
1325 * to remove the header. This function frees the
1326 * original string if necessary.
1328 * Returns : JB_ERR_OK on success, or
1329 * JB_ERR_MEMORY on out-of-memory error.
1331 *********************************************************************/
1332 jb_err server_content_length(struct client_state *csp, char **header)
1334 if (csp->content_length != 0) /* Content length could have been modified */
1337 * XXX: Shouldn't we check if csp->content_length
1338 * is different than the original value?
1341 *header = (char *) zalloc(100);
1342 if (*header == NULL)
1344 return JB_ERR_MEMORY;
1347 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
1349 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1356 /*********************************************************************
1358 * Function : server_content_md5
1360 * Description : Crumble any Content-MD5 headers if the document was
1361 * modified. FIXME: Should we re-compute instead?
1364 * 1 : csp = Current client state (buffers, headers, etc...)
1365 * 2 : header = On input, pointer to header to modify.
1366 * On output, pointer to the modified header, or NULL
1367 * to remove the header. This function frees the
1368 * original string if necessary.
1370 * Returns : JB_ERR_OK on success, or
1371 * JB_ERR_MEMORY on out-of-memory error.
1373 *********************************************************************/
1374 jb_err server_content_md5(struct client_state *csp, char **header)
1376 if (csp->flags & CSP_FLAG_MODIFIED)
1378 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1385 /*********************************************************************
1387 * Function : server_content_disposition
1389 * Description : If enabled, blocks or modifies the "content-disposition" header.
1390 * Called from `sed'.
1393 * 1 : csp = Current client state (buffers, headers, etc...)
1394 * 2 : header = On input, pointer to header to modify.
1395 * On output, pointer to the modified header, or NULL
1396 * to remove the header. This function frees the
1397 * original string if necessary.
1399 * Returns : JB_ERR_OK on success, or
1400 * JB_ERR_MEMORY on out-of-memory error.
1402 *********************************************************************/
1403 jb_err server_content_disposition(struct client_state *csp, char **header)
1408 * Are we messing with the content-disposition header?
1410 if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
1416 newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
1418 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1421 * Blocking content-disposition header
1423 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1430 * Replacing content-disposition header
1433 *header = strdup("content-disposition: ");
1434 string_append(header, newval);
1436 if (*header == NULL)
1438 log_error(LOG_LEVEL_HEADER, "Insufficent memory. content-disposition header not fully replaced.");
1442 log_error(LOG_LEVEL_HEADER, "content-disposition header crunched and replaced with: %s", *header);
1445 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1448 /*********************************************************************
1450 * Function : server_last_modified
1452 * Description : Changes Last-Modified header to the actual date
1453 * to help hide-if-modified-since.
1454 * Called from `sed'.
1457 * 1 : csp = Current client state (buffers, headers, etc...)
1458 * 2 : header = On input, pointer to header to modify.
1459 * On output, pointer to the modified header, or NULL
1460 * to remove the header. This function frees the
1461 * original string if necessary.
1463 * Returns : JB_ERR_OK on success, or
1464 * JB_ERR_MEMORY on out-of-memory error.
1466 *********************************************************************/
1467 jb_err server_last_modified(struct client_state *csp, char **header)
1470 char buf[BUFFER_SIZE];
1473 struct tm *timeptr = NULL;
1474 time_t now, last_modified;
1476 long int days, hours, minutes, seconds;
1479 * Are we messing with the Last-Modified header?
1481 if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
1487 newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
1489 if (0 == strcmpic(newval, "block") )
1492 * Blocking Last-Modified header. Useless but why not.
1494 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
1498 else if (0 == strcmpic(newval, "reset-to-request-time"))
1501 * Setting Last-Modified Header to now.
1503 get_http_time(0, buf);
1505 *header = strdup("Last-Modified: ");
1506 string_append(header, buf);
1508 if (*header == NULL)
1510 log_error(LOG_LEVEL_HEADER, "Insufficent memory. Last-Modified header got lost, boohoo.");
1514 log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
1517 else if (0 == strcmpic(newval, "randomize"))
1519 log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
1521 timeptr = gmtime(&now);
1522 if ( (last_modified = parse_header_time(*header, timeptr)) < 0 )
1524 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
1529 rtime = difftime(now, last_modified);
1532 #if !defined(_WIN32) && !defined(__OS2__)
1533 rtime = random() % rtime + 1;
1535 rtime = rand() % (long int)(rtime + 1);
1536 #endif /* (ifndef _WIN32 || __OS2__) */
1537 last_modified += rtime;
1538 timeptr = localtime(&last_modified);
1539 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
1541 *header = strdup("Last-Modified: ");
1542 string_append(header, newheader);
1544 if (*header == NULL)
1546 log_error(LOG_LEVEL_ERROR, " Insufficent memory, header crunched without replacement.");
1547 return JB_ERR_MEMORY;
1550 if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
1552 days = rtime / (3600 * 24);
1553 hours = rtime / 3600 % 24;
1554 minutes = rtime / 60 % 60;
1555 seconds = rtime % 60;
1557 log_error(LOG_LEVEL_HEADER, "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
1558 *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
1559 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
1564 log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
1572 /*********************************************************************
1574 * Function : client_accept_encoding
1576 * Description : Rewrite the client's Accept-Encoding header so that
1577 * if doesn't allow compression, if the action applies.
1578 * Note: For HTTP/1.0 the absence of the header is enough.
1581 * 1 : csp = Current client state (buffers, headers, etc...)
1582 * 2 : header = On input, pointer to header to modify.
1583 * On output, pointer to the modified header, or NULL
1584 * to remove the header. This function frees the
1585 * original string if necessary.
1587 * Returns : JB_ERR_OK on success, or
1588 * JB_ERR_MEMORY on out-of-memory error.
1590 *********************************************************************/
1591 jb_err client_accept_encoding(struct client_state *csp, char **header)
1593 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1595 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1599 /* Temporarily disable the correct behaviour to
1600 * work around a PHP bug.
1602 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1604 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1605 * if (*header == NULL)
1607 * return JB_ERR_MEMORY;
1618 /*********************************************************************
1620 * Function : client_te
1622 * Description : Rewrite the client's TE header so that
1623 * if doesn't allow compression, if the action applies.
1626 * 1 : csp = Current client state (buffers, headers, etc...)
1627 * 2 : header = On input, pointer to header to modify.
1628 * On output, pointer to the modified header, or NULL
1629 * to remove the header. This function frees the
1630 * original string if necessary.
1632 * Returns : JB_ERR_OK on success, or
1633 * JB_ERR_MEMORY on out-of-memory error.
1635 *********************************************************************/
1636 jb_err client_te(struct client_state *csp, char **header)
1638 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1641 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1647 /*********************************************************************
1649 * Function : client_referrer
1651 * Description : Handle the "referer" config setting properly.
1652 * Called from `sed'.
1655 * 1 : csp = Current client state (buffers, headers, etc...)
1656 * 2 : header = On input, pointer to header to modify.
1657 * On output, pointer to the modified header, or NULL
1658 * to remove the header. This function frees the
1659 * original string if necessary.
1661 * Returns : JB_ERR_OK on success, or
1662 * JB_ERR_MEMORY on out-of-memory error.
1664 *********************************************************************/
1665 jb_err client_referrer(struct client_state *csp, char **header)
1672 #ifdef FEATURE_FORCE_LOAD
1673 /* Since the referrer can include the prefix even
1674 * if the request itself is non-forced, we must
1675 * clean it unconditionally
1677 strclean(*header, FORCE_PREFIX);
1678 #endif /* def FEATURE_FORCE_LOAD */
1681 * Are we sending referer?
1683 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1688 newval = csp->action->string[ACTION_STRING_REFERER];
1690 if ((0 != strcmpic(newval, "conditional-block")))
1694 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1699 log_error(LOG_LEVEL_HEADER, "Referer crunched!");
1702 else if (0 == strcmpic(newval, "conditional-block"))
1705 * Block referer if host has changed.
1708 if (NULL == (host = strdup(csp->http->hostport)))
1711 log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary host copy.");
1712 return JB_ERR_MEMORY;
1714 if (NULL == (referer = strdup(*header)))
1718 log_error(LOG_LEVEL_HEADER, "Referer crunched! Couldn't allocate memory for temporary referer copy.");
1719 return JB_ERR_MEMORY;
1721 hostlenght = strlen(host);
1722 if ( hostlenght < (strlen(referer)-17) ) /*referer begins with 'Referer: http[s]://'*/
1724 /*Shorten referer to make sure the referer is blocked
1725 *if www.example.org/www.example.com-shall-see-the-referer/
1726 *links to www.example.com/
1728 referer[hostlenght+17] = '\n';
1730 if ( 0 == strstr(referer, host)) /*Host has changed*/
1732 log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
1737 log_error(LOG_LEVEL_HEADER, "%s (not modified, still on %s)", *header, host);
1743 else if (0 != strcmpic(newval, "forge"))
1746 * We have a specific (fixed) referer we want to send.
1748 if ((0 != strncmpic(newval, "http://", 7)) && (0 != strncmpic(newval, "https://", 8)))
1750 log_error(LOG_LEVEL_HEADER, "Parameter: +referrer{%s} is a bad idea, but I don't care.", newval);
1752 *header = strdup("Referer: ");
1753 string_append(header, newval);
1754 log_error(LOG_LEVEL_HEADER, "Referer overwritten with: %s", *header);
1756 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1761 * Forge a referer as http://[hostname:port of REQUEST]/
1762 * to fool stupid checks for in-site links
1765 *header = strdup("Referer: http://");
1766 string_append(header, csp->http->hostport);
1767 string_append(header, "/");
1768 log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
1770 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1774 /*********************************************************************
1776 * Function : client_accept_language
1778 * Description : Handle the "Accept-Language" config setting properly.
1779 * Called from `sed'.
1782 * 1 : csp = Current client state (buffers, headers, etc...)
1783 * 2 : header = On input, pointer to header to modify.
1784 * On output, pointer to the modified header, or NULL
1785 * to remove the header. This function frees the
1786 * original string if necessary.
1788 * Returns : JB_ERR_OK on success, or
1789 * JB_ERR_MEMORY on out-of-memory error.
1791 *********************************************************************/
1792 jb_err client_accept_language(struct client_state *csp, char **header)
1797 * Are we messing with the Accept-Language?
1799 if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
1801 /*I don't think so*/
1805 newval = csp->action->string[ACTION_STRING_LANGUAGE];
1807 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1810 * Blocking Accept-Language header
1812 log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
1819 * Replacing Accept-Language header
1822 *header = strdup("Accept-Language: ");
1823 string_append(header, newval);
1825 if (*header == NULL)
1827 log_error(LOG_LEVEL_ERROR, " Insufficent memory. Accept-Language header crunched without replacement.");
1831 log_error(LOG_LEVEL_HEADER, "Accept-Language header crunched and replaced with: %s", *header);
1834 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1837 /*********************************************************************
1839 * Function : crunch_client_header
1841 * Description : Crunch client header if it matches a string supplied by the
1842 * user. Called from `sed'.
1845 * 1 : csp = Current client state (buffers, headers, etc...)
1846 * 2 : header = On input, pointer to header to modify.
1847 * On output, pointer to the modified header, or NULL
1848 * to remove the header. This function frees the
1849 * original string if necessary.
1851 * Returns : JB_ERR_OK on success and always succeeds
1853 *********************************************************************/
1854 jb_err crunch_client_header(struct client_state *csp, char **header)
1856 const char *crunch_pattern;
1857 /*Is there a header to crunch*/
1859 if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
1861 crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
1863 /*Is the current header the lucky one?*/
1864 if (strstr(*header, crunch_pattern))
1866 log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
1874 /*********************************************************************
1876 * Function : client_uagent
1878 * Description : Handle the "user-agent" config setting properly
1879 * and remember its original value to enable browser
1880 * bug workarounds. Called from `sed'.
1883 * 1 : csp = Current client state (buffers, headers, etc...)
1884 * 2 : header = On input, pointer to header to modify.
1885 * On output, pointer to the modified header, or NULL
1886 * to remove the header. This function frees the
1887 * original string if necessary.
1889 * Returns : JB_ERR_OK on success, or
1890 * JB_ERR_MEMORY on out-of-memory error.
1892 *********************************************************************/
1893 jb_err client_uagent(struct client_state *csp, char **header)
1897 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1902 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1909 *header = strdup("User-Agent: ");
1910 string_append(header, newval);
1912 log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
1914 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1917 /*********************************************************************
1919 * Function : client_ua
1921 * Description : Handle "ua-" headers properly. Called from `sed'.
1924 * 1 : csp = Current client state (buffers, headers, etc...)
1925 * 2 : header = On input, pointer to header to modify.
1926 * On output, pointer to the modified header, or NULL
1927 * to remove the header. This function frees the
1928 * original string if necessary.
1930 * Returns : JB_ERR_OK on success, or
1931 * JB_ERR_MEMORY on out-of-memory error.
1933 *********************************************************************/
1934 jb_err client_ua(struct client_state *csp, char **header)
1936 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1938 log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
1946 /*********************************************************************
1948 * Function : client_from
1950 * Description : Handle the "from" config setting properly.
1951 * Called from `sed'.
1954 * 1 : csp = Current client state (buffers, headers, etc...)
1955 * 2 : header = On input, pointer to header to modify.
1956 * On output, pointer to the modified header, or NULL
1957 * to remove the header. This function frees the
1958 * original string if necessary.
1960 * Returns : JB_ERR_OK on success, or
1961 * JB_ERR_MEMORY on out-of-memory error.
1963 *********************************************************************/
1964 jb_err client_from(struct client_state *csp, char **header)
1968 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1975 newval = csp->action->string[ACTION_STRING_FROM];
1978 * Are we blocking the e-mail address?
1980 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1982 log_error(LOG_LEVEL_HEADER, "crunched From!");
1986 log_error(LOG_LEVEL_HEADER, " modified");
1988 *header = strdup("From: ");
1989 string_append(header, newval);
1991 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1995 /*********************************************************************
1997 * Function : client_send_cookie
1999 * Description : Handle the "cookie" header properly. Called from `sed'.
2000 * If cookie is accepted, add it to the cookie_list,
2001 * else we crunch it. Mmmmmmmmmmm ... cookie ......
2004 * 1 : csp = Current client state (buffers, headers, etc...)
2005 * 2 : header = On input, pointer to header to modify.
2006 * On output, pointer to the modified header, or NULL
2007 * to remove the header. This function frees the
2008 * original string if necessary.
2010 * Returns : JB_ERR_OK on success, or
2011 * JB_ERR_MEMORY on out-of-memory error.
2013 *********************************************************************/
2014 jb_err client_send_cookie(struct client_state *csp, char **header)
2016 jb_err result = JB_ERR_OK;
2018 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
2020 /* strlen("cookie: ") == 8 */
2021 result = enlist(csp->cookie_list, *header + 8);
2025 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
2029 * Always remove the cookie here. The cookie header
2030 * will be sent at the end of the header.
2038 /*********************************************************************
2040 * Function : client_x_forwarded
2042 * Description : Handle the "x-forwarded-for" config setting properly,
2043 * also used in the add_client_headers list. Called from `sed'.
2046 * 1 : csp = Current client state (buffers, headers, etc...)
2047 * 2 : header = On input, pointer to header to modify.
2048 * On output, pointer to the modified header, or NULL
2049 * to remove the header. This function frees the
2050 * original string if necessary.
2052 * Returns : JB_ERR_OK on success, or
2053 * JB_ERR_MEMORY on out-of-memory error.
2055 *********************************************************************/
2056 jb_err client_x_forwarded(struct client_state *csp, char **header)
2058 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
2060 /* Save it so we can re-add it later */
2061 freez(csp->x_forwarded);
2062 csp->x_forwarded = *header;
2065 * Always set *header = NULL, since this information
2066 * will be sent at the end of the header.
2073 log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
2080 /*********************************************************************
2082 * Function : client_max_forwards
2084 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
2085 * from the value of the Max-Forwards header field.
2088 * 1 : csp = Current client state (buffers, headers, etc...)
2089 * 2 : header = On input, pointer to header to modify.
2090 * On output, pointer to the modified header, or NULL
2091 * to remove the header. This function frees the
2092 * original string if necessary.
2094 * Returns : JB_ERR_OK on success, or
2095 * JB_ERR_MEMORY on out-of-memory error.
2097 *********************************************************************/
2098 jb_err client_max_forwards(struct client_state *csp, char **header)
2100 unsigned int max_forwards;
2102 if ((0 == strcmpic(csp->http->gpc, "trace"))
2103 || (0 == strcmpic(csp->http->gpc, "options")))
2105 if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
2107 if (max_forwards-- >= 1)
2109 sprintf(*header, "Max-Forwards: %u", max_forwards);
2110 log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
2114 log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
2123 /*********************************************************************
2125 * Function : client_host
2127 * Description : If the request URI did not contain host and
2128 * port information, parse and evaluate the Host
2131 * Also, kill ill-formed HOST: headers as sent by
2132 * Apple's iTunes software when used with a proxy.
2135 * 1 : csp = Current client state (buffers, headers, etc...)
2136 * 2 : header = On input, pointer to header to modify.
2137 * On output, pointer to the modified header, or NULL
2138 * to remove the header. This function frees the
2139 * original string if necessary.
2141 * Returns : JB_ERR_OK on success, or
2142 * JB_ERR_MEMORY on out-of-memory error.
2144 *********************************************************************/
2145 jb_err client_host(struct client_state *csp, char **header)
2150 * If the header field name is all upper-case, chances are that it's
2151 * an ill-formed one from iTunes. BTW, killing innocent headers here is
2152 * not a problem -- they are regenerated later.
2154 if ((*header)[1] == 'O')
2156 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
2161 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
2162 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
2165 if (NULL == (p = strdup((*header)+6)))
2167 return JB_ERR_MEMORY;
2170 if (NULL == (q = strdup(p)))
2173 return JB_ERR_MEMORY;
2176 freez(csp->http->hostport);
2177 csp->http->hostport = p;
2178 freez(csp->http->host);
2179 csp->http->host = q;
2180 q = strchr(csp->http->host, ':');
2183 /* Terminate hostname and evaluate port string */
2185 csp->http->port = atoi(q);
2189 csp->http->port = csp->http->ssl ? 443 : 80;
2192 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
2193 csp->http->hostport, csp->http->host, csp->http->port);
2199 /*********************************************************************
2201 * Function : client_if_modified_since
2203 * Description : Remove or modify the If-Modified-Since header.
2206 * 1 : csp = Current client state (buffers, headers, etc...)
2207 * 2 : header = On input, pointer to header to modify.
2208 * On output, pointer to the modified header, or NULL
2209 * to remove the header. This function frees the
2210 * original string if necessary.
2212 * Returns : JB_ERR_OK on success, or
2213 * JB_ERR_MEMORY on out-of-memory error.
2215 *********************************************************************/
2216 jb_err client_if_modified_since(struct client_state *csp, char **header)
2219 struct tm *timeptr = NULL;
2223 time_t hours, minutes, seconds;
2227 if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
2230 * The client got an error message because of a temporary problem,
2231 * the problem is gone and the client now tries to revalidate our
2232 * error message on the real server. The revalidation would always
2233 * end with the transmission of the whole document and there is
2234 * no need to expose the bogus If-Modified-Since header.
2236 log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
2239 else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
2241 newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
2243 if ((0 == strcmpic(newval, "block")))
2245 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2248 else /* add random value */
2250 if ( (tm = parse_header_time(*header, timeptr)) < 0 )
2252 log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s (crunching!)", *header);
2257 rtime = (time_t) strtol(newval, &endptr, 0);
2260 log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d hou%s)",
2261 *header, rtime, (rtime == 1 || rtime == -1) ? "r": "rs");
2263 #if !defined(_WIN32) && !defined(__OS2__)
2264 rtime = random() % rtime + 1;
2266 rtime = rand() % (long int)(rtime + 1);
2267 #endif /* (_WIN32 || __OS2__) */
2268 if(newval[0] == '-')
2275 log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
2279 timeptr = localtime(&tm);
2280 strftime(newheader, sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr);
2283 *header = strdup("If-Modified-Since: ");
2284 string_append(header, newheader);
2286 if (*header == NULL)
2288 log_error(LOG_LEVEL_HEADER, " Insufficent memory, header crunched without replacement.");
2289 return JB_ERR_MEMORY;
2292 if(LOG_LEVEL_HEADER & debug) /* Save cycles if the user isn't interested. */
2299 hours = (int)rtime / 3600 % 24;
2300 minutes = (int)rtime / 60 % 60;
2301 seconds = (int)rtime % 60;
2303 log_error(LOG_LEVEL_HEADER, "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
2304 *header, (negative) ? "subtracted" : "added", hours, (hours == 1) ? "r" : "rs",
2305 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2314 /*********************************************************************
2316 * Function : client_if_none_match
2318 * Description : Remove the If-None-Match header.
2321 * 1 : csp = Current client state (buffers, headers, etc...)
2322 * 2 : header = On input, pointer to header to modify.
2323 * On output, pointer to the modified header, or NULL
2324 * to remove the header. This function frees the
2325 * original string if necessary.
2327 * Returns : JB_ERR_OK on success, or
2328 * JB_ERR_MEMORY on out-of-memory error.
2330 *********************************************************************/
2331 jb_err client_if_none_match(struct client_state *csp, char **header)
2333 if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
2335 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2342 /*********************************************************************
2344 * Function : client_x_filter
2346 * Description : Disables filtering if the client set "X-Filter: No".
2347 * Called from `sed'.
2350 * 1 : csp = Current client state (buffers, headers, etc...)
2351 * 2 : header = On input, pointer to header to modify.
2352 * On output, pointer to the modified header, or NULL
2353 * to remove the header. This function frees the
2354 * original string if necessary.
2356 * Returns : JB_ERR_OK on success
2358 *********************************************************************/
2359 jb_err client_x_filter(struct client_state *csp, char **header)
2361 if ( 0 == strcmpic(*header, "X-Filter: No"))
2363 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
2365 log_error(LOG_LEVEL_HEADER, "force-text-mode overruled the client's request to disable filtering!");
2369 csp->content_type = CT_TABOO;
2370 log_error(LOG_LEVEL_HEADER, "Disabled filter mode on behalf of the client.");
2372 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
2378 /* the following functions add headers directly to the header list */
2380 /*********************************************************************
2382 * Function : client_host_adder
2384 * Description : Adds the Host: header field if it is missing.
2385 * Called from `sed'.
2388 * 1 : csp = Current client state (buffers, headers, etc...)
2390 * Returns : JB_ERR_OK on success, or
2391 * JB_ERR_MEMORY on out-of-memory error.
2393 *********************************************************************/
2394 jb_err client_host_adder(struct client_state *csp)
2399 if ( !csp->http->hostport || !*(csp->http->hostport))
2405 * remove 'user:pass@' from 'proto://user:pass@host'
2407 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
2413 p = csp->http->hostport;
2416 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
2417 err = enlist_unique_header(csp->headers, "Host", p);
2423 /*********************************************************************
2425 * Function : client_cookie_adder
2427 * Description : Used in the add_client_headers list. Called from `sed'.
2430 * 1 : csp = Current client state (buffers, headers, etc...)
2432 * Returns : JB_ERR_OK on success, or
2433 * JB_ERR_MEMORY on out-of-memory error.
2435 *********************************************************************/
2436 jb_err client_cookie_adder(struct client_state *csp)
2438 struct list_entry *lst;
2440 struct list_entry *list1 = csp->cookie_list->first;
2441 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
2442 int first_cookie = 1;
2445 if ((list1 == NULL) && (list2 == NULL))
2451 tmp = strdup("Cookie: ");
2453 for (lst = list1; lst ; lst = lst->next)
2461 string_append(&tmp, "; ");
2463 string_append(&tmp, lst->str);
2466 for (lst = list2; lst ; lst = lst->next)
2474 string_append(&tmp, "; ");
2476 string_join(&tmp, cookie_encode(lst->str));
2481 return JB_ERR_MEMORY;
2484 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
2485 err = enlist(csp->headers, tmp);
2491 /*********************************************************************
2493 * Function : client_accept_encoding_adder
2495 * Description : Add an Accept-Encoding header to the client's request
2496 * that disables compression if the action applies, and
2497 * the header is not already there. Called from `sed'.
2498 * Note: For HTTP/1.0, the absence of the header is enough.
2501 * 1 : csp = Current client state (buffers, headers, etc...)
2503 * Returns : JB_ERR_OK on success, or
2504 * JB_ERR_MEMORY on out-of-memory error.
2506 *********************************************************************/
2507 jb_err client_accept_encoding_adder(struct client_state *csp)
2509 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2510 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
2512 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
2519 /*********************************************************************
2521 * Function : client_xtra_adder
2523 * Description : Used in the add_client_headers list. Called from `sed'.
2526 * 1 : csp = Current client state (buffers, headers, etc...)
2528 * Returns : JB_ERR_OK on success, or
2529 * JB_ERR_MEMORY on out-of-memory error.
2531 *********************************************************************/
2532 jb_err client_xtra_adder(struct client_state *csp)
2534 struct list_entry *lst;
2537 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
2538 lst ; lst = lst->next)
2540 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
2541 err = enlist(csp->headers, lst->str);
2553 /*********************************************************************
2555 * Function : client_x_forwarded_adder
2557 * Description : Used in the add_client_headers list. Called from `sed'.
2560 * 1 : csp = Current client state (buffers, headers, etc...)
2562 * Returns : JB_ERR_OK on success, or
2563 * JB_ERR_MEMORY on out-of-memory error.
2565 *********************************************************************/
2566 jb_err client_x_forwarded_adder(struct client_state *csp)
2571 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
2576 if (csp->x_forwarded)
2578 p = strdup(csp->x_forwarded);
2579 string_append(&p, ", ");
2583 p = strdup("X-Forwarded-For: ");
2585 string_append(&p, csp->ip_addr_str);
2589 return JB_ERR_MEMORY;
2592 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
2593 err = enlist(csp->headers, p);
2600 /*********************************************************************
2602 * Function : connection_close_adder
2604 * Description : Adds a "Connection: close" header to csp->headers
2605 * as a temporary fix for the needed but missing HTTP/1.1
2606 * support. Called from `sed'.
2607 * FIXME: This whole function shouldn't be neccessary!
2610 * 1 : csp = Current client state (buffers, headers, etc...)
2612 * Returns : JB_ERR_OK on success, or
2613 * JB_ERR_MEMORY on out-of-memory error.
2615 *********************************************************************/
2616 jb_err connection_close_adder(struct client_state *csp)
2618 log_error(LOG_LEVEL_HEADER, "Adding: Connection: close");
2619 return enlist(csp->headers, "Connection: close");
2623 /*********************************************************************
2625 * Function : server_http
2627 * Description : - Save the HTTP Status into csp->http->status
2628 * - Set CT_TABOO to prevent filtering if the answer
2629 * is a partial range (HTTP status 206)
2630 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
2634 * 1 : csp = Current client state (buffers, headers, etc...)
2635 * 2 : header = On input, pointer to header to modify.
2636 * On output, pointer to the modified header, or NULL
2637 * to remove the header. This function frees the
2638 * original string if necessary.
2640 * Returns : JB_ERR_OK on success, or
2641 * JB_ERR_MEMORY on out-of-memory error.
2643 *********************************************************************/
2644 jb_err server_http(struct client_state *csp, char **header)
2646 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
2647 if (csp->http->status == 206)
2649 csp->content_type = CT_TABOO;
2652 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
2655 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
2662 /*********************************************************************
2664 * Function : server_set_cookie
2666 * Description : Handle the server "cookie" header properly.
2667 * Log cookie to the jar file. Then "crunch" it,
2668 * or accept it. Called from `sed'.
2671 * 1 : csp = Current client state (buffers, headers, etc...)
2672 * 2 : header = On input, pointer to header to modify.
2673 * On output, pointer to the modified header, or NULL
2674 * to remove the header. This function frees the
2675 * original string if necessary.
2677 * Returns : JB_ERR_OK on success, or
2678 * JB_ERR_MEMORY on out-of-memory error.
2680 *********************************************************************/
2681 jb_err server_set_cookie(struct client_state *csp, char **header)
2683 #ifdef FEATURE_COOKIE_JAR
2684 if (csp->config->jar)
2687 * Write timestamp into outbuf.
2689 * Complex because not all OSs have tm_gmtoff or
2690 * the %z field in strftime()
2692 char tempbuf[ BUFFER_SIZE ];
2696 #ifdef HAVE_LOCALTIME_R
2697 tm_now = *localtime_r(&now, &tm_now);
2699 pthread_mutex_lock(&localtime_mutex);
2700 tm_now = *localtime (&now);
2701 pthread_mutex_unlock(&localtime_mutex);
2703 tm_now = *localtime (&now);
2705 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
2707 /* strlen("set-cookie: ") = 12 */
2708 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
2710 #endif /* def FEATURE_COOKIE_JAR */
2712 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
2714 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
2715 return crumble(csp, header);
2717 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
2719 /* Flag whether or not to log a message */
2722 /* A variable to store the tag we're working on */
2725 /* Skip "Set-Cookie:" (11 characters) in header */
2726 cur_tag = *header + 11;
2728 /* skip whitespace between "Set-Cookie:" and value */
2729 while (*cur_tag && ijb_isspace(*cur_tag))
2734 /* Loop through each tag in the cookie */
2738 char *next_tag = strchr(cur_tag, ';');
2739 if (next_tag != NULL)
2741 /* Skip the ';' character itself */
2744 /* skip whitespace ";" and start of tag */
2745 while (*next_tag && ijb_isspace(*next_tag))
2752 /* "Next tag" is the end of the string */
2753 next_tag = cur_tag + strlen(cur_tag);
2756 /* Is this the "Expires" tag? */
2757 if (strncmpic(cur_tag, "expires=", 8) == 0)
2759 /* Delete the tag by copying the rest of the string over it.
2760 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
2761 * since the behaviour of strcpy is undefined for overlapping
2764 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
2766 /* That changed the header, need to issue a log message */
2769 /* Note that the next tag has now been moved to *cur_tag,
2770 * so we do not need to update the cur_tag pointer.
2775 /* Move on to next cookie tag */
2782 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
2790 #ifdef FEATURE_FORCE_LOAD
2791 /*********************************************************************
2793 * Function : strclean
2795 * Description : In-Situ-Eliminate all occurances of substring in
2799 * 1 : string = string to clean
2800 * 2 : substring = substring to eliminate
2802 * Returns : Number of eliminations
2804 *********************************************************************/
2805 int strclean(const char *string, const char *substring)
2807 int hits = 0, len = strlen(substring);
2810 while((pos = strstr(string, substring)) != NULL)
2817 while (*p++ != '\0');
2824 #endif /* def FEATURE_FORCE_LOAD */
2826 /*********************************************************************
2828 * Function : parse_header_time
2830 * Description : Transforms time inside a HTTP header into
2831 * the usual time format.
2834 * 1 : header = header to parse
2835 * 2 : timeptr = storage for the resulting time structure
2837 * Returns : Time in seconds since Unix epoch or -1 for failure.
2839 *********************************************************************/
2840 time_t parse_header_time(char *header, struct tm *timeptr) {
2846 timeptr = localtime(&tm);
2847 /* Skipping header name */
2848 timestring = strstr(header, ": ");
2849 if (strptime(timestring, ": %a, %d %b %Y %H:%M:%S", timeptr) == NULL)
2853 tm = mktime(timeptr);