1 const char parsers_rcs[] = "$Id: parsers.c,v 1.56.2.7 2003/05/06 12:07:26 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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.56.2.7 2003/05/06 12:07:26 oes
44 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
46 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
47 * Completing the previous change
49 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
50 * Added temporary workaround for bug in PHP < 4.2.3
52 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
53 * 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.
55 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
56 * Fix typo: supressed -> suppressed
58 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
59 * Improved cookie logging
61 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
62 * Added basic support for OPTIONS and TRACE HTTP methods:
63 * - New parser function client_max_forwards which decrements
64 * the Max-Forwards HTTP header field of OPTIONS and TRACE
65 * requests by one before forwarding
66 * - New parser function client_host which extracts the host
67 * and port information from the HTTP header field if the
68 * request URI was not absolute
69 * - Don't crumble and re-add the Host: header, but only generate
70 * and append if missing
72 * Revision 1.56 2002/05/12 15:34:22 jongfoster
73 * Fixing typo in a comment
75 * Revision 1.55 2002/05/08 16:01:07 oes
76 * Optimized add_to_iob:
77 * - Use realloc instead of malloc(), memcpy(), free()
78 * - Expand to powers of two if possible, to get
79 * O(log n) reallocs instead of O(n).
80 * - Moved check for buffer limit here from chat
81 * - Report failure via returncode
83 * Revision 1.54 2002/04/02 15:03:16 oes
86 * Revision 1.53 2002/03/26 22:29:55 swa
87 * we have a new homepage!
89 * Revision 1.52 2002/03/24 13:25:43 swa
90 * name change related issues
92 * Revision 1.51 2002/03/13 00:27:05 jongfoster
95 * Revision 1.50 2002/03/12 01:45:35 oes
96 * More verbose logging
98 * Revision 1.49 2002/03/09 20:03:52 jongfoster
99 * - Making various functions return int rather than size_t.
100 * (Undoing a recent change). Since size_t is unsigned on
101 * Windows, functions like read_socket that return -1 on
102 * error cannot return a size_t.
104 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
105 * crashes, and also frequently caused JB to jump to 100%
106 * CPU and stay there. (Because it thought it had just
107 * read ((unsigned)-1) == 4Gb of data...)
109 * - The signature of write_socket has changed, it now simply
110 * returns success=0/failure=nonzero.
112 * - Trying to get rid of a few warnings --with-debug on
113 * Windows, I've introduced a new type "jb_socket". This is
114 * used for the socket file descriptors. On Windows, this
115 * is SOCKET (a typedef for unsigned). Everywhere else, it's
116 * an int. The error value can't be -1 any more, so it's
117 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
118 * Windows it maps to the #define INVALID_SOCKET.)
120 * - The signature of bind_port has changed.
122 * Revision 1.48 2002/03/07 03:46:53 oes
123 * Fixed compiler warnings etc
125 * Revision 1.47 2002/02/20 23:15:13 jongfoster
126 * Parsing functions now handle out-of-memory gracefully by returning
129 * Revision 1.46 2002/01/17 21:03:47 jongfoster
130 * Moving all our URL and URL pattern parsing code to urlmatch.c.
132 * Revision 1.45 2002/01/09 14:33:03 oes
133 * Added support for localtime_r.
135 * Revision 1.44 2001/12/14 01:22:54 steudten
136 * Remove 'user:pass@' from 'proto://user:pass@host' for the
137 * new added header 'Host: ..'. (See Req ID 491818)
139 * Revision 1.43 2001/11/23 00:26:38 jongfoster
140 * Fixing two really stupid errors in my previous commit
142 * Revision 1.42 2001/11/22 21:59:30 jongfoster
143 * Adding code to handle +no-cookies-keep
145 * Revision 1.41 2001/11/05 23:43:05 steudten
146 * Add time+date to log files.
148 * Revision 1.40 2001/10/26 20:13:09 jongfoster
149 * ctype.h is needed in Windows, too.
151 * Revision 1.39 2001/10/26 17:40:04 oes
152 * Introduced get_header_value()
153 * Removed http->user_agent, csp->referrer and csp->accept_types
154 * Removed client_accept()
156 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
157 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
158 * threads to call select() simultaneously. So, it's time to do a real, live,
159 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
160 * (native). Both versions will work, but using __OS2__ offers multi-threading.
162 * Revision 1.37 2001/10/23 21:36:02 jongfoster
163 * Documenting sed()'s error behaviou (doc change only)
165 * Revision 1.36 2001/10/13 12:51:51 joergs
166 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
167 * force-load), instead crumble Host: and add it (again) in client_host_adder
168 * (in case we get a HTTP/1.0 request without Host: header and forward it to
169 * a HTTP/1.1 server/proxy).
171 * Revision 1.35 2001/10/09 22:39:21 jongfoster
172 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
175 * Revision 1.34 2001/10/07 18:50:55 oes
176 * Added server_content_encoding, renamed server_transfer_encoding
178 * Revision 1.33 2001/10/07 18:04:49 oes
179 * Changed server_http11 to server_http and its pattern to "HTTP".
180 * Additional functionality: it now saves the HTTP status into
181 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
183 * Revision 1.32 2001/10/07 15:43:28 oes
184 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
185 * client_te and client_accept_encoding_adder, triggered by the new
186 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
187 * changed to allow only identity and chunked, and the TE header is
188 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
190 * parse_http_request no longer does anything than parsing. The rewriting
191 * of http->cmd and version mangling are gone. It now also recognizes
192 * the put and delete methods and saves the url in http->url. Removed
195 * renamed content_type and content_length to have the server_ prefix
197 * server_content_type now only works if csp->content_type != CT_TABOO
199 * added server_transfer_encoding, which
200 * - Sets CT_TABOO to prohibit filtering if encoding compresses
201 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
202 * - Change from "chunked" to "identity" if body was chunked
203 * but has been de-chunked for filtering.
205 * added server_content_md5 which crunches any Content-MD5 headers
206 * if the body was modified.
208 * made server_http11 conditional on +downgrade action
210 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
212 * Revision 1.31 2001/10/05 14:25:02 oes
213 * Crumble Keep-Alive from Server
215 * Revision 1.30 2001/09/29 12:56:03 joergs
216 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
218 * Revision 1.29 2001/09/24 21:09:24 jongfoster
219 * Fixing 2 memory leaks that Guy spotted, where the paramater to
220 * enlist() was not being free()d.
222 * Revision 1.28 2001/09/22 16:32:28 jongfoster
223 * Removing unused #includes.
225 * Revision 1.27 2001/09/20 15:45:25 steudten
227 * add casting from size_t to int for printf()
228 * remove local variable shadow s2
230 * Revision 1.26 2001/09/16 17:05:14 jongfoster
231 * Removing unused #include showarg.h
233 * Revision 1.25 2001/09/16 13:21:27 jongfoster
234 * Changes to use new list functions.
236 * Revision 1.24 2001/09/13 23:05:50 jongfoster
237 * Changing the string paramater to the header parsers a "const".
239 * Revision 1.23 2001/09/12 18:08:19 steudten
241 * In parse_http_request() header rewriting miss the host value, so
242 * from http://www.mydomain.com the result was just " / " not
243 * http://www.mydomain.com/ in case we forward.
245 * Revision 1.22 2001/09/10 10:58:53 oes
246 * Silenced compiler warnings
248 * Revision 1.21 2001/07/31 14:46:00 oes
249 * - Persistant connections now suppressed
250 * - sed() no longer appends empty header to csp->headers
252 * Revision 1.20 2001/07/30 22:08:36 jongfoster
253 * Tidying up #defines:
254 * - All feature #defines are now of the form FEATURE_xxx
255 * - Permanently turned off WIN_GUI_EDIT
256 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
258 * Revision 1.19 2001/07/25 17:21:54 oes
259 * client_uagent now saves copy of User-Agent: header value
261 * Revision 1.18 2001/07/13 14:02:46 oes
262 * - Included fix to repair broken HTTP requests that
263 * don't contain a path, not even '/'.
264 * - Removed all #ifdef PCRS
265 * - content_type now always inspected and classified as
266 * text, gif or other.
267 * - formatting / comments
269 * Revision 1.17 2001/06/29 21:45:41 oes
270 * Indentation, CRLF->LF, Tab-> Space
272 * Revision 1.16 2001/06/29 13:32:42 oes
274 * - Adapted free_http_request
275 * - Removed logentry from cancelled commit
277 * Revision 1.15 2001/06/03 19:12:38 oes
278 * deleted const struct interceptors
280 * Revision 1.14 2001/06/01 18:49:17 jongfoster
281 * Replaced "list_share" with "list" - the tiny memory gain was not
282 * worth the extra complexity.
284 * Revision 1.13 2001/05/31 21:30:33 jongfoster
285 * Removed list code - it's now in list.[ch]
286 * Renamed "permission" to "action", and changed many features
287 * to use the actions file rather than the global config.
289 * Revision 1.12 2001/05/31 17:33:13 oes
293 * Revision 1.11 2001/05/29 20:11:19 joergs
294 * '/ * inside comment' warning removed.
296 * Revision 1.10 2001/05/29 09:50:24 jongfoster
297 * Unified blocklist/imagelist/permissionslist.
298 * File format is still under discussion, but the internal changes
301 * Also modified interceptor behaviour:
302 * - We now intercept all URLs beginning with one of the following
303 * prefixes (and *only* these prefixes):
305 * * http://ijbswa.sf.net/config/
306 * * http://ijbswa.sourceforge.net/config/
307 * - New interceptors "home page" - go to http://i.j.b/ to see it.
308 * - Internal changes so that intercepted and fast redirect pages
309 * are not replaced with an image.
310 * - Interceptors now have the option to send a binary page direct
311 * to the client. (i.e. ijb-send-banner uses this)
312 * - Implemented show-url-info interceptor. (Which is why I needed
313 * the above interceptors changes - a typical URL is
314 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
315 * The previous mechanism would not have intercepted that, and
316 * if it had been intercepted then it then it would have replaced
319 * Revision 1.9 2001/05/28 17:26:33 jongfoster
320 * Fixing segfault if last header was crunched.
321 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
322 * can use the cross-platform sprintf() instead.)
324 * Revision 1.8 2001/05/27 22:17:04 oes
326 * - re_process_buffer no longer writes the modified buffer
327 * to the client, which was very ugly. It now returns the
328 * buffer, which it is then written by chat.
330 * - content_length now adjusts the Content-Length: header
331 * for modified documents rather than crunch()ing it.
332 * (Length info in csp->content_length, which is 0 for
333 * unmodified documents)
335 * - For this to work, sed() is called twice when filtering.
337 * Revision 1.7 2001/05/27 13:19:06 oes
338 * Patched Joergs solution for the content-length in.
340 * Revision 1.6 2001/05/26 13:39:32 jongfoster
341 * Only crunches Content-Length header if applying RE filtering.
342 * Without this fix, Microsoft Windows Update wouldn't work.
344 * Revision 1.5 2001/05/26 00:28:36 jongfoster
345 * Automatic reloading of config file.
346 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
347 * Most of the global variables have been moved to a new
348 * struct configuration_spec, accessed through csp->config->globalname
349 * Most of the globals remaining are used by the Win32 GUI.
351 * Revision 1.4 2001/05/22 18:46:04 oes
353 * - Enabled filtering banners by size rather than URL
354 * by adding patterns that replace all standard banner
355 * sizes with the "Junkbuster" gif to the re_filterfile
357 * - Enabled filtering WebBugs by providing a pattern
358 * which kills all 1x1 images
360 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
361 * which is selected by the (nonstandard and therefore
362 * capital) letter 'U' in the option string.
363 * It causes the quantifiers to be ungreedy by default.
364 * Appending a ? turns back to greedy (!).
366 * - Added a new interceptor ijb-send-banner, which
367 * sends back the "Junkbuster" gif. Without imagelist or
368 * MSIE detection support, or if tinygif = 1, or the
369 * URL isn't recognized as an imageurl, a lame HTML
370 * explanation is sent instead.
372 * - Added new feature, which permits blocking remote
373 * script redirects and firing back a local redirect
375 * The feature is conditionally compiled, i.e. it
376 * can be disabled with --disable-fast-redirects,
377 * plus it must be activated by a "fast-redirects"
378 * line in the config file, has its own log level
379 * and of course wants to be displayed by show-proxy-args
380 * Note: Boy, all the #ifdefs in 1001 locations and
381 * all the fumbling with configure.in and acconfig.h
382 * were *way* more work than the feature itself :-(
384 * - Because a generic redirect template was needed for
385 * this, tinygif = 3 now uses the same.
387 * - Moved GIFs, and other static HTTP response templates
392 * - Removed some >400 CRs again (Jon, you really worked
395 * Revision 1.3 2001/05/20 01:21:20 jongfoster
396 * Version 2.9.4 checkin.
397 * - Merged popupfile and cookiefile, and added control over PCRS
398 * filtering, in new "permissionsfile".
399 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
400 * file error you now get a message box (in the Win32 GUI) rather
401 * than the program exiting with no explanation.
402 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
404 * - Removed tabs from "config"
405 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
406 * - Bumped up version number.
408 * Revision 1.2 2001/05/17 23:02:36 oes
409 * - Made referrer option accept 'L' as a substitute for '§'
411 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
412 * Initial import of version 2.9.3 source tree
415 *********************************************************************/
422 #include <sys/types.h>
430 #if !defined(_WIN32) && !defined(__OS2__)
437 /* jcc.h is for mutex semapores only */
438 #endif /* def OSX_DARWIN */
445 #include "jbsockets.h"
446 #include "miscutil.h"
449 const char parsers_h_rcs[] = PARSERS_H_VERSION;
451 /* Fix a problem with Solaris. There should be no effect on other
453 * Solaris's isspace() is a macro which uses it's argument directly
454 * as an array index. Therefore we need to make sure that high-bit
455 * characters generate +ve values, and ideally we also want to make
456 * the argument match the declared parameter type of "int".
458 * Why did they write a character function that can't take a simple
459 * "char" argument? Doh!
461 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
462 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
465 const struct parsers client_patterns[] = {
466 { "referer:", 8, client_referrer },
467 { "user-agent:", 11, client_uagent },
468 { "ua-", 3, client_ua },
469 { "from:", 5, client_from },
470 { "cookie:", 7, client_send_cookie },
471 { "x-forwarded-for:", 16, client_x_forwarded },
472 { "Accept-Encoding:", 16, client_accept_encoding },
473 { "TE:", 3, client_te },
474 { "Host:", 5, client_host },
475 /* { "if-modified-since:", 18, crumble }, */
476 { "Keep-Alive:", 11, crumble },
477 { "connection:", 11, crumble },
478 { "proxy-connection:", 17, crumble },
479 { "max-forwards:", 13, client_max_forwards },
484 const struct parsers server_patterns[] = {
485 { "HTTP", 4, server_http },
486 { "set-cookie:", 11, server_set_cookie },
487 { "connection:", 11, crumble },
488 { "Content-Type:", 13, server_content_type },
489 { "Content-Length:", 15, server_content_length },
490 { "Content-MD5:", 12, server_content_md5 },
491 { "Content-Encoding:", 17, server_content_encoding },
492 { "Transfer-Encoding:", 18, server_transfer_coding },
493 { "Keep-Alive:", 11, crumble },
498 const add_header_func_ptr add_client_headers[] = {
501 client_x_forwarded_adder,
503 /* Temporarily disabled: client_accept_encoding_adder, */
504 connection_close_adder,
509 const add_header_func_ptr add_server_headers[] = {
510 connection_close_adder,
515 /*********************************************************************
517 * Function : flush_socket
519 * Description : Write any pending "buffered" content.
522 * 1 : fd = file descriptor of the socket to read
523 * 2 : csp = Current client state (buffers, headers, etc...)
525 * Returns : On success, the number of bytes written are returned (zero
526 * indicates nothing was written). On error, -1 is returned,
527 * and errno is set appropriately. If count is zero and the
528 * file descriptor refers to a regular file, 0 will be
529 * returned without causing any other effect. For a special
530 * file, the results are not portable.
532 *********************************************************************/
533 int flush_socket(jb_socket fd, struct client_state *csp)
535 struct iob *iob = csp->iob;
536 int len = iob->eod - iob->cur;
543 if (write_socket(fd, iob->cur, (size_t)len))
547 iob->eod = iob->cur = iob->buf;
553 /*********************************************************************
555 * Function : add_to_iob
557 * Description : Add content to the buffered page, expanding the
558 * buffer if necessary.
561 * 1 : csp = Current client state (buffers, headers, etc...)
562 * 2 : buf = holds the content to be added to the page
563 * 3 : n = number of bytes to be added
565 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
566 * or buffer limit reached.
568 *********************************************************************/
569 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
571 struct iob *iob = csp->iob;
572 size_t used, offset, need, want;
575 if (n <= 0) return JB_ERR_OK;
577 used = iob->eod - iob->buf;
578 offset = iob->cur - iob->buf;
582 * If the buffer can't hold the new data, extend it first.
583 * Use the next power of two if possible, else use the actual need.
585 if (need > csp->config->buffer_limit)
587 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
588 return JB_ERR_MEMORY;
591 if (need > iob->size)
593 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
595 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
599 else if (NULL != (p = (char *)realloc(iob->buf, need)))
605 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
606 return JB_ERR_MEMORY;
609 /* Update the iob pointers */
610 iob->cur = p + offset;
615 /* copy the new data into the iob buffer */
616 memcpy(iob->eod, buf, (size_t)n);
618 /* point to the end of the data */
621 /* null terminate == cheap insurance */
629 /*********************************************************************
631 * Function : get_header
633 * Description : This (odd) routine will parse the csp->iob
636 * 1 : csp = Current client state (buffers, headers, etc...)
638 * Returns : Any one of the following:
640 * 1) a pointer to a dynamically allocated string that contains a header line
641 * 2) NULL indicating that the end of the header was reached
642 * 3) "" indicating that the end of the iob was reached before finding
643 * a complete header line.
645 *********************************************************************/
646 char *get_header(struct client_state *csp)
652 if ((iob->cur == NULL)
653 || ((p = strchr(iob->cur, '\n')) == NULL))
655 return(""); /* couldn't find a complete header */
660 ret = strdup(iob->cur);
663 /* FIXME No way to handle error properly */
664 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
669 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
671 /* is this a blank line (i.e. the end of the header) ? */
683 /*********************************************************************
685 * Function : get_header_value
687 * Description : Get the value of a given header from a chained list
688 * of header lines or return NULL if no such header is
689 * present in the list.
692 * 1 : header_list = pointer to list
693 * 2 : header_name = string with name of header to look for.
694 * Trailing colon required, capitalization
697 * Returns : NULL if not found, else value of header
699 *********************************************************************/
700 char *get_header_value(const struct list *header_list, const char *header_name)
702 struct list_entry *cur_entry;
708 length = strlen(header_name);
710 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
714 if (!strncmpic(cur_entry->str, header_name, length))
717 * Found: return pointer to start of value
719 ret = (char *) (cur_entry->str + length);
720 while (*ret && ijb_isspace(*ret)) ret++;
733 /*********************************************************************
737 * Description : add, delete or modify lines in the HTTP header streams.
738 * On entry, it receives a linked list of headers space
739 * that was allocated dynamically (both the list nodes
740 * and the header contents).
742 * As a side effect it frees the space used by the original
746 * 1 : pats = list of patterns to match against headers
747 * 2 : more_headers = list of functions to add more
748 * headers (client or server)
749 * 3 : csp = Current client state (buffers, headers, etc...)
751 * Returns : Single pointer to a fully formed header, or NULL
752 * on out-of-memory error.
754 *********************************************************************/
755 char *sed(const struct parsers pats[],
756 const add_header_func_ptr more_headers[],
757 struct client_state *csp)
759 struct list_entry *p;
760 const struct parsers *v;
761 const add_header_func_ptr *f;
762 jb_err err = JB_ERR_OK;
764 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
766 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
768 /* Header crunch()ed in previous run? -> ignore */
769 if (p->str == NULL) continue;
771 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
773 if (strncmpic(p->str, v->str, v->len) == 0)
775 err = v->parser(csp, (char **)&(p->str));
780 /* place any additional headers on the csp->headers list */
781 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
786 if (err != JB_ERR_OK)
791 return list_to_text(csp->headers);
795 /* here begins the family of parser functions that reformat header lines */
798 /*********************************************************************
802 * Description : This is called if a header matches a pattern to "crunch"
805 * 1 : csp = Current client state (buffers, headers, etc...)
806 * 2 : header = On input, pointer to header to modify.
807 * On output, pointer to the modified header, or NULL
808 * to remove the header. This function frees the
809 * original string if necessary.
811 * Returns : JB_ERR_OK on success, or
812 * JB_ERR_MEMORY on out-of-memory error.
814 *********************************************************************/
815 jb_err crumble(struct client_state *csp, char **header)
817 log_error(LOG_LEVEL_HEADER, "crunch!");
823 /*********************************************************************
825 * Function : server_content_type
827 * Description : Set the content-type for filterable types (text/.*,
828 * javascript and image/gif) unless filtering has been
829 * forbidden (CT_TABOO) while parsing earlier headers.
830 * NOTE: Since text/plain is commonly used by web servers
831 * for files whose correct type is unknown, we don't
832 * set CT_TEXT for it.
835 * 1 : csp = Current client state (buffers, headers, etc...)
836 * 2 : header = On input, pointer to header to modify.
837 * On output, pointer to the modified header, or NULL
838 * to remove the header. This function frees the
839 * original string if necessary.
841 * Returns : JB_ERR_OK on success, or
842 * JB_ERR_MEMORY on out-of-memory error.
844 *********************************************************************/
845 jb_err server_content_type(struct client_state *csp, char **header)
847 if (csp->content_type != CT_TABOO)
849 if ((strstr(*header, " text/") && !strstr(*header, "plain"))
850 || strstr(*header, "application/x-javascript"))
851 csp->content_type = CT_TEXT;
852 else if (strstr(*header, " image/gif"))
853 csp->content_type = CT_GIF;
855 csp->content_type = 0;
862 /*********************************************************************
864 * Function : server_transfer_coding
866 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
867 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
868 * - Change from "chunked" to "identity" if body was chunked
869 * but has been de-chunked for filtering.
872 * 1 : csp = Current client state (buffers, headers, etc...)
873 * 2 : header = On input, pointer to header to modify.
874 * On output, pointer to the modified header, or NULL
875 * to remove the header. This function frees the
876 * original string if necessary.
878 * Returns : JB_ERR_OK on success, or
879 * JB_ERR_MEMORY on out-of-memory error.
881 *********************************************************************/
882 jb_err server_transfer_coding(struct client_state *csp, char **header)
885 * Turn off pcrs and gif filtering if body compressed
887 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
889 csp->content_type = CT_TABOO;
893 * Raise flag if body chunked
895 if (strstr(*header, "chunked"))
897 csp->flags |= CSP_FLAG_CHUNKED;
900 * If the body was modified, it has been
901 * de-chunked first, so adjust the header:
903 if (csp->flags & CSP_FLAG_MODIFIED)
906 *header = strdup("Transfer-Encoding: identity");
907 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
915 /*********************************************************************
917 * Function : server_content_encoding
919 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
922 * 1 : csp = Current client state (buffers, headers, etc...)
923 * 2 : header = On input, pointer to header to modify.
924 * On output, pointer to the modified header, or NULL
925 * to remove the header. This function frees the
926 * original string if necessary.
928 * Returns : JB_ERR_OK on success, or
929 * JB_ERR_MEMORY on out-of-memory error.
931 *********************************************************************/
932 jb_err server_content_encoding(struct client_state *csp, char **header)
935 * Turn off pcrs and gif filtering if body compressed
937 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
939 csp->content_type = CT_TABOO;
947 /*********************************************************************
949 * Function : server_content_length
951 * Description : Adjust Content-Length header if we modified
955 * 1 : csp = Current client state (buffers, headers, etc...)
956 * 2 : header = On input, pointer to header to modify.
957 * On output, pointer to the modified header, or NULL
958 * to remove the header. This function frees the
959 * original string if necessary.
961 * Returns : JB_ERR_OK on success, or
962 * JB_ERR_MEMORY on out-of-memory error.
964 *********************************************************************/
965 jb_err server_content_length(struct client_state *csp, char **header)
967 if (csp->content_length != 0) /* Content length has been modified */
970 *header = (char *) zalloc(100);
973 return JB_ERR_MEMORY;
976 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
978 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
985 /*********************************************************************
987 * Function : server_content_md5
989 * Description : Crumble any Content-MD5 headers if the document was
990 * modified. FIXME: Should we re-compute instead?
993 * 1 : csp = Current client state (buffers, headers, etc...)
994 * 2 : header = On input, pointer to header to modify.
995 * On output, pointer to the modified header, or NULL
996 * to remove the header. This function frees the
997 * original string if necessary.
999 * Returns : JB_ERR_OK on success, or
1000 * JB_ERR_MEMORY on out-of-memory error.
1002 *********************************************************************/
1003 jb_err server_content_md5(struct client_state *csp, char **header)
1005 if (csp->flags & CSP_FLAG_MODIFIED)
1007 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1015 /*********************************************************************
1017 * Function : client_accept_encoding
1019 * Description : Rewrite the client's Accept-Encoding header so that
1020 * if doesn't allow compression, if the action applies.
1021 * Note: For HTTP/1.0 the absence of the header is enough.
1024 * 1 : csp = Current client state (buffers, headers, etc...)
1025 * 2 : header = On input, pointer to header to modify.
1026 * On output, pointer to the modified header, or NULL
1027 * to remove the header. This function frees the
1028 * original string if necessary.
1030 * Returns : JB_ERR_OK on success, or
1031 * JB_ERR_MEMORY on out-of-memory error.
1033 *********************************************************************/
1034 jb_err client_accept_encoding(struct client_state *csp, char **header)
1036 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1038 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1042 /* Temporarily disable the correct behaviour to
1043 * work around a PHP bug.
1045 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1047 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1048 * if (*header == NULL)
1050 * return JB_ERR_MEMORY;
1061 /*********************************************************************
1063 * Function : client_te
1065 * Description : Rewrite the client's TE header so that
1066 * if doesn't allow compression, if the action applies.
1069 * 1 : csp = Current client state (buffers, headers, etc...)
1070 * 2 : header = On input, pointer to header to modify.
1071 * On output, pointer to the modified header, or NULL
1072 * to remove the header. This function frees the
1073 * original string if necessary.
1075 * Returns : JB_ERR_OK on success, or
1076 * JB_ERR_MEMORY on out-of-memory error.
1078 *********************************************************************/
1079 jb_err client_te(struct client_state *csp, char **header)
1081 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1084 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1090 /*********************************************************************
1092 * Function : client_referrer
1094 * Description : Handle the "referer" config setting properly.
1095 * Called from `sed'.
1098 * 1 : csp = Current client state (buffers, headers, etc...)
1099 * 2 : header = On input, pointer to header to modify.
1100 * On output, pointer to the modified header, or NULL
1101 * to remove the header. This function frees the
1102 * original string if necessary.
1104 * Returns : JB_ERR_OK on success, or
1105 * JB_ERR_MEMORY on out-of-memory error.
1107 *********************************************************************/
1108 jb_err client_referrer(struct client_state *csp, char **header)
1112 #ifdef FEATURE_FORCE_LOAD
1113 /* Since the referrer can include the prefix even
1114 * even if the request itself is non-forced, we must
1115 * clean it unconditionally
1117 strclean(*header, FORCE_PREFIX);
1118 #endif /* def FEATURE_FORCE_LOAD */
1121 * Are we sending referer?
1123 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1130 newval = csp->action->string[ACTION_STRING_REFERER];
1132 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1137 log_error(LOG_LEVEL_HEADER, "crunch!");
1140 else if (0 == strncmpic(newval, "http://", 7))
1143 * We have a specific (fixed) referer we want to send.
1145 log_error(LOG_LEVEL_HEADER, "modified");
1147 *header = strdup("Referer: ");
1148 string_append(header, newval);
1150 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1155 * Forge a referer as http://[hostname:port of REQUEST]/
1156 * to fool stupid checks for in-site links
1158 if (0 != strcmpic(newval, "forge"))
1161 * Invalid choice - but forge is probably the best default.
1163 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1166 *header = strdup("Referer: http://");
1167 string_append(header, csp->http->hostport);
1168 string_append(header, "/");
1169 log_error(LOG_LEVEL_HEADER, "crunch+forge to %s", *header);
1171 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1176 /*********************************************************************
1178 * Function : client_uagent
1180 * Description : Handle the "user-agent" config setting properly
1181 * and remember its original value to enable browser
1182 * bug workarounds. Called from `sed'.
1185 * 1 : csp = Current client state (buffers, headers, etc...)
1186 * 2 : header = On input, pointer to header to modify.
1187 * On output, pointer to the modified header, or NULL
1188 * to remove the header. This function frees the
1189 * original string if necessary.
1191 * Returns : JB_ERR_OK on success, or
1192 * JB_ERR_MEMORY on out-of-memory error.
1194 *********************************************************************/
1195 jb_err client_uagent(struct client_state *csp, char **header)
1199 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1204 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1210 log_error(LOG_LEVEL_HEADER, "modified");
1213 *header = strdup("User-Agent: ");
1214 string_append(header, newval);
1216 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1220 /*********************************************************************
1222 * Function : client_ua
1224 * Description : Handle "ua-" headers properly. Called from `sed'.
1227 * 1 : csp = Current client state (buffers, headers, etc...)
1228 * 2 : header = On input, pointer to header to modify.
1229 * On output, pointer to the modified header, or NULL
1230 * to remove the header. This function frees the
1231 * original string if necessary.
1233 * Returns : JB_ERR_OK on success, or
1234 * JB_ERR_MEMORY on out-of-memory error.
1236 *********************************************************************/
1237 jb_err client_ua(struct client_state *csp, char **header)
1239 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1241 log_error(LOG_LEVEL_HEADER, "crunch!");
1249 /*********************************************************************
1251 * Function : client_from
1253 * Description : Handle the "from" config setting properly.
1254 * Called from `sed'.
1257 * 1 : csp = Current client state (buffers, headers, etc...)
1258 * 2 : header = On input, pointer to header to modify.
1259 * On output, pointer to the modified header, or NULL
1260 * to remove the header. This function frees the
1261 * original string if necessary.
1263 * Returns : JB_ERR_OK on success, or
1264 * JB_ERR_MEMORY on out-of-memory error.
1266 *********************************************************************/
1267 jb_err client_from(struct client_state *csp, char **header)
1271 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1278 newval = csp->action->string[ACTION_STRING_FROM];
1281 * Are we blocking the e-mail address?
1283 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1285 log_error(LOG_LEVEL_HEADER, "crunch!");
1289 log_error(LOG_LEVEL_HEADER, " modified");
1291 *header = strdup("From: ");
1292 string_append(header, newval);
1294 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1298 /*********************************************************************
1300 * Function : client_send_cookie
1302 * Description : Handle the "cookie" header properly. Called from `sed'.
1303 * If cookie is accepted, add it to the cookie_list,
1304 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1307 * 1 : csp = Current client state (buffers, headers, etc...)
1308 * 2 : header = On input, pointer to header to modify.
1309 * On output, pointer to the modified header, or NULL
1310 * to remove the header. This function frees the
1311 * original string if necessary.
1313 * Returns : JB_ERR_OK on success, or
1314 * JB_ERR_MEMORY on out-of-memory error.
1316 *********************************************************************/
1317 jb_err client_send_cookie(struct client_state *csp, char **header)
1319 jb_err result = JB_ERR_OK;
1321 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1323 /* strlen("cookie: ") == 8 */
1324 result = enlist(csp->cookie_list, *header + 8);
1328 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
1332 * Always remove the cookie here. The cookie header
1333 * will be sent at the end of the header.
1341 /*********************************************************************
1343 * Function : client_x_forwarded
1345 * Description : Handle the "x-forwarded-for" config setting properly,
1346 * also used in the add_client_headers list. Called from `sed'.
1349 * 1 : csp = Current client state (buffers, headers, etc...)
1350 * 2 : header = On input, pointer to header to modify.
1351 * On output, pointer to the modified header, or NULL
1352 * to remove the header. This function frees the
1353 * original string if necessary.
1355 * Returns : JB_ERR_OK on success, or
1356 * JB_ERR_MEMORY on out-of-memory error.
1358 *********************************************************************/
1359 jb_err client_x_forwarded(struct client_state *csp, char **header)
1361 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1363 /* Save it so we can re-add it later */
1364 freez(csp->x_forwarded);
1365 csp->x_forwarded = *header;
1368 * Always set *header = NULL, since this information
1369 * will be sent at the end of the header.
1376 log_error(LOG_LEVEL_HEADER, " crunch!");
1383 /*********************************************************************
1385 * Function : client_max_forwards
1387 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
1388 * from the value of the Max-Forwards header field.
1391 * 1 : csp = Current client state (buffers, headers, etc...)
1392 * 2 : header = On input, pointer to header to modify.
1393 * On output, pointer to the modified header, or NULL
1394 * to remove the header. This function frees the
1395 * original string if necessary.
1397 * Returns : JB_ERR_OK on success, or
1398 * JB_ERR_MEMORY on out-of-memory error.
1400 *********************************************************************/
1401 jb_err client_max_forwards(struct client_state *csp, char **header)
1403 unsigned int max_forwards;
1405 if ((0 == strcmpic(csp->http->gpc, "trace"))
1406 || (0 == strcmpic(csp->http->gpc, "options")))
1408 if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
1410 if (max_forwards-- >= 1)
1412 sprintf(*header, "Max-Forwards: %u", max_forwards);
1413 log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
1417 log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
1426 /*********************************************************************
1428 * Function : client_host
1430 * Description : If the request URI did not contain host and
1431 * port information, parse and evaluate the Host
1434 * Also, kill ill-formed HOST: headers as sent by
1435 * Apple's iTunes software when used with a proxy.
1438 * 1 : csp = Current client state (buffers, headers, etc...)
1439 * 2 : header = On input, pointer to header to modify.
1440 * On output, pointer to the modified header, or NULL
1441 * to remove the header. This function frees the
1442 * original string if necessary.
1444 * Returns : JB_ERR_OK on success, or
1445 * JB_ERR_MEMORY on out-of-memory error.
1447 *********************************************************************/
1448 jb_err client_host(struct client_state *csp, char **header)
1453 * If the header field name is all upper-case, chances are that it's
1454 * an ill-formed one from iTunes. BTW, killing innocent headers here is
1455 * not a problem -- they are regenerated later.
1457 if ((*header)[1] == 'O')
1459 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
1464 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
1465 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
1468 if (NULL == (p = strdup((*header)+6)))
1470 return JB_ERR_MEMORY;
1473 if (NULL == (q = strdup(p)))
1476 return JB_ERR_MEMORY;
1479 freez(csp->http->hostport);
1480 csp->http->hostport = p;
1481 freez(csp->http->host);
1482 csp->http->host = q;
1483 q = strchr(csp->http->host, ':');
1486 /* Terminate hostname and evaluate port string */
1488 csp->http->port = atoi(q);
1492 csp->http->port = csp->http->ssl ? 443 : 80;
1495 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
1496 csp->http->hostport, csp->http->host, csp->http->port);
1503 /* the following functions add headers directly to the header list */
1505 /*********************************************************************
1507 * Function : client_host_adder
1509 * Description : Adds the Host: header field if it is missing.
1510 * Called from `sed'.
1513 * 1 : csp = Current client state (buffers, headers, etc...)
1515 * Returns : JB_ERR_OK on success, or
1516 * JB_ERR_MEMORY on out-of-memory error.
1518 *********************************************************************/
1519 jb_err client_host_adder(struct client_state *csp)
1524 if ( !csp->http->hostport || !*(csp->http->hostport))
1530 * remove 'user:pass@' from 'proto://user:pass@host'
1532 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
1538 p = csp->http->hostport;
1541 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
1542 err = enlist_unique_header(csp->headers, "Host", p);
1548 /*********************************************************************
1550 * Function : client_cookie_adder
1552 * Description : Used in the add_client_headers list. Called from `sed'.
1555 * 1 : csp = Current client state (buffers, headers, etc...)
1557 * Returns : JB_ERR_OK on success, or
1558 * JB_ERR_MEMORY on out-of-memory error.
1560 *********************************************************************/
1561 jb_err client_cookie_adder(struct client_state *csp)
1563 struct list_entry *lst;
1565 struct list_entry *list1 = csp->cookie_list->first;
1566 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1567 int first_cookie = 1;
1570 if ((list1 == NULL) && (list2 == NULL))
1576 tmp = strdup("Cookie: ");
1578 for (lst = list1; lst ; lst = lst->next)
1586 string_append(&tmp, "; ");
1588 string_append(&tmp, lst->str);
1591 for (lst = list2; lst ; lst = lst->next)
1599 string_append(&tmp, "; ");
1601 string_join(&tmp, cookie_encode(lst->str));
1606 return JB_ERR_MEMORY;
1609 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1610 err = enlist(csp->headers, tmp);
1616 /*********************************************************************
1618 * Function : client_accept_encoding_adder
1620 * Description : Add an Accept-Encoding header to the client's request
1621 * that disables compression if the action applies, and
1622 * the header is not already there. Called from `sed'.
1623 * Note: For HTTP/1.0, the absence of the header is enough.
1626 * 1 : csp = Current client state (buffers, headers, etc...)
1628 * Returns : JB_ERR_OK on success, or
1629 * JB_ERR_MEMORY on out-of-memory error.
1631 *********************************************************************/
1632 jb_err client_accept_encoding_adder(struct client_state *csp)
1634 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1635 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1637 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1644 /*********************************************************************
1646 * Function : client_xtra_adder
1648 * Description : Used in the add_client_headers list. Called from `sed'.
1651 * 1 : csp = Current client state (buffers, headers, etc...)
1653 * Returns : JB_ERR_OK on success, or
1654 * JB_ERR_MEMORY on out-of-memory error.
1656 *********************************************************************/
1657 jb_err client_xtra_adder(struct client_state *csp)
1659 struct list_entry *lst;
1662 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1663 lst ; lst = lst->next)
1665 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1666 err = enlist(csp->headers, lst->str);
1678 /*********************************************************************
1680 * Function : client_x_forwarded_adder
1682 * Description : Used in the add_client_headers list. Called from `sed'.
1685 * 1 : csp = Current client state (buffers, headers, etc...)
1687 * Returns : JB_ERR_OK on success, or
1688 * JB_ERR_MEMORY on out-of-memory error.
1690 *********************************************************************/
1691 jb_err client_x_forwarded_adder(struct client_state *csp)
1696 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1701 if (csp->x_forwarded)
1703 p = strdup(csp->x_forwarded);
1704 string_append(&p, ", ");
1708 p = strdup("X-Forwarded-For: ");
1710 string_append(&p, csp->ip_addr_str);
1714 return JB_ERR_MEMORY;
1717 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1718 err = enlist(csp->headers, p);
1725 /*********************************************************************
1727 * Function : connection_close_adder
1729 * Description : Adds a "Connection: close" header to csp->headers
1730 * as a temporary fix for the needed but missing HTTP/1.1
1731 * support. Called from `sed'.
1732 * FIXME: This whole function shouldn't be neccessary!
1735 * 1 : csp = Current client state (buffers, headers, etc...)
1737 * Returns : JB_ERR_OK on success, or
1738 * JB_ERR_MEMORY on out-of-memory error.
1740 *********************************************************************/
1741 jb_err connection_close_adder(struct client_state *csp)
1743 return enlist(csp->headers, "Connection: close");
1747 /*********************************************************************
1749 * Function : server_http
1751 * Description : - Save the HTTP Status into csp->http->status
1752 * - Set CT_TABOO to prevent filtering if the answer
1753 * is a partial range (HTTP status 206)
1754 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1758 * 1 : csp = Current client state (buffers, headers, etc...)
1759 * 2 : header = On input, pointer to header to modify.
1760 * On output, pointer to the modified header, or NULL
1761 * to remove the header. This function frees the
1762 * original string if necessary.
1764 * Returns : JB_ERR_OK on success, or
1765 * JB_ERR_MEMORY on out-of-memory error.
1767 *********************************************************************/
1768 jb_err server_http(struct client_state *csp, char **header)
1770 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1771 if (csp->http->status == 206)
1773 csp->content_type = CT_TABOO;
1776 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1779 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1786 /*********************************************************************
1788 * Function : server_set_cookie
1790 * Description : Handle the server "cookie" header properly.
1791 * Log cookie to the jar file. Then "crunch" it,
1792 * or accept it. Called from `sed'.
1795 * 1 : csp = Current client state (buffers, headers, etc...)
1796 * 2 : header = On input, pointer to header to modify.
1797 * On output, pointer to the modified header, or NULL
1798 * to remove the header. This function frees the
1799 * original string if necessary.
1801 * Returns : JB_ERR_OK on success, or
1802 * JB_ERR_MEMORY on out-of-memory error.
1804 *********************************************************************/
1805 jb_err server_set_cookie(struct client_state *csp, char **header)
1807 #ifdef FEATURE_COOKIE_JAR
1808 if (csp->config->jar)
1811 * Write timestamp into outbuf.
1813 * Complex because not all OSs have tm_gmtoff or
1814 * the %z field in strftime()
1816 char tempbuf[ BUFFER_SIZE ];
1820 #ifdef HAVE_LOCALTIME_R
1821 tm_now = *localtime_r(&now, &tm_now);
1823 pthread_mutex_lock(&localtime_mutex);
1824 tm_now = *localtime (&now);
1825 pthread_mutex_unlock(&localtime_mutex);
1827 tm_now = *localtime (&now);
1829 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1831 /* strlen("set-cookie: ") = 12 */
1832 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1834 #endif /* def FEATURE_COOKIE_JAR */
1836 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1838 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
1839 return crumble(csp, header);
1841 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1843 /* Flag whether or not to log a message */
1846 /* A variable to store the tag we're working on */
1849 /* Skip "Set-Cookie:" (11 characters) in header */
1850 cur_tag = *header + 11;
1852 /* skip whitespace between "Set-Cookie:" and value */
1853 while (*cur_tag && ijb_isspace(*cur_tag))
1858 /* Loop through each tag in the cookie */
1862 char *next_tag = strchr(cur_tag, ';');
1863 if (next_tag != NULL)
1865 /* Skip the ';' character itself */
1868 /* skip whitespace ";" and start of tag */
1869 while (*next_tag && ijb_isspace(*next_tag))
1876 /* "Next tag" is the end of the string */
1877 next_tag = cur_tag + strlen(cur_tag);
1880 /* Is this the "Expires" tag? */
1881 if (strncmpic(cur_tag, "expires=", 8) == 0)
1883 /* Delete the tag by copying the rest of the string over it.
1884 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1885 * since the behaviour of strcpy is undefined for overlapping
1888 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1890 /* That changed the header, need to issue a log message */
1893 /* Note that the next tag has now been moved to *cur_tag,
1894 * so we do not need to update the cur_tag pointer.
1899 /* Move on to next cookie tag */
1906 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1914 #ifdef FEATURE_FORCE_LOAD
1915 /*********************************************************************
1917 * Function : strclean
1919 * Description : In-Situ-Eliminate all occurances of substring in
1923 * 1 : string = string to clean
1924 * 2 : substring = substring to eliminate
1926 * Returns : Number of eliminations
1928 *********************************************************************/
1929 int strclean(const char *string, const char *substring)
1931 int hits = 0, len = strlen(substring);
1934 while((pos = strstr(string, substring)) != NULL)
1941 while (*p++ != '\0');
1948 #endif /* def FEATURE_FORCE_LOAD */