1 const char parsers_rcs[] = "$Id: parsers.c,v 1.56.2.2 2002/09/25 14:59:53 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.2 2002/09/25 14:59:53 oes
44 * Improved cookie logging
46 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
47 * Added basic support for OPTIONS and TRACE HTTP methods:
48 * - New parser function client_max_forwards which decrements
49 * the Max-Forwards HTTP header field of OPTIONS and TRACE
50 * requests by one before forwarding
51 * - New parser function client_host which extracts the host
52 * and port information from the HTTP header field if the
53 * request URI was not absolute
54 * - Don't crumble and re-add the Host: header, but only generate
55 * and append if missing
57 * Revision 1.56 2002/05/12 15:34:22 jongfoster
58 * Fixing typo in a comment
60 * Revision 1.55 2002/05/08 16:01:07 oes
61 * Optimized add_to_iob:
62 * - Use realloc instead of malloc(), memcpy(), free()
63 * - Expand to powers of two if possible, to get
64 * O(log n) reallocs instead of O(n).
65 * - Moved check for buffer limit here from chat
66 * - Report failure via returncode
68 * Revision 1.54 2002/04/02 15:03:16 oes
71 * Revision 1.53 2002/03/26 22:29:55 swa
72 * we have a new homepage!
74 * Revision 1.52 2002/03/24 13:25:43 swa
75 * name change related issues
77 * Revision 1.51 2002/03/13 00:27:05 jongfoster
80 * Revision 1.50 2002/03/12 01:45:35 oes
81 * More verbose logging
83 * Revision 1.49 2002/03/09 20:03:52 jongfoster
84 * - Making various functions return int rather than size_t.
85 * (Undoing a recent change). Since size_t is unsigned on
86 * Windows, functions like read_socket that return -1 on
87 * error cannot return a size_t.
89 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
90 * crashes, and also frequently caused JB to jump to 100%
91 * CPU and stay there. (Because it thought it had just
92 * read ((unsigned)-1) == 4Gb of data...)
94 * - The signature of write_socket has changed, it now simply
95 * returns success=0/failure=nonzero.
97 * - Trying to get rid of a few warnings --with-debug on
98 * Windows, I've introduced a new type "jb_socket". This is
99 * used for the socket file descriptors. On Windows, this
100 * is SOCKET (a typedef for unsigned). Everywhere else, it's
101 * an int. The error value can't be -1 any more, so it's
102 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
103 * Windows it maps to the #define INVALID_SOCKET.)
105 * - The signature of bind_port has changed.
107 * Revision 1.48 2002/03/07 03:46:53 oes
108 * Fixed compiler warnings etc
110 * Revision 1.47 2002/02/20 23:15:13 jongfoster
111 * Parsing functions now handle out-of-memory gracefully by returning
114 * Revision 1.46 2002/01/17 21:03:47 jongfoster
115 * Moving all our URL and URL pattern parsing code to urlmatch.c.
117 * Revision 1.45 2002/01/09 14:33:03 oes
118 * Added support for localtime_r.
120 * Revision 1.44 2001/12/14 01:22:54 steudten
121 * Remove 'user:pass@' from 'proto://user:pass@host' for the
122 * new added header 'Host: ..'. (See Req ID 491818)
124 * Revision 1.43 2001/11/23 00:26:38 jongfoster
125 * Fixing two really stupid errors in my previous commit
127 * Revision 1.42 2001/11/22 21:59:30 jongfoster
128 * Adding code to handle +no-cookies-keep
130 * Revision 1.41 2001/11/05 23:43:05 steudten
131 * Add time+date to log files.
133 * Revision 1.40 2001/10/26 20:13:09 jongfoster
134 * ctype.h is needed in Windows, too.
136 * Revision 1.39 2001/10/26 17:40:04 oes
137 * Introduced get_header_value()
138 * Removed http->user_agent, csp->referrer and csp->accept_types
139 * Removed client_accept()
141 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
142 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
143 * threads to call select() simultaneously. So, it's time to do a real, live,
144 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
145 * (native). Both versions will work, but using __OS2__ offers multi-threading.
147 * Revision 1.37 2001/10/23 21:36:02 jongfoster
148 * Documenting sed()'s error behaviou (doc change only)
150 * Revision 1.36 2001/10/13 12:51:51 joergs
151 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
152 * force-load), instead crumble Host: and add it (again) in client_host_adder
153 * (in case we get a HTTP/1.0 request without Host: header and forward it to
154 * a HTTP/1.1 server/proxy).
156 * Revision 1.35 2001/10/09 22:39:21 jongfoster
157 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
160 * Revision 1.34 2001/10/07 18:50:55 oes
161 * Added server_content_encoding, renamed server_transfer_encoding
163 * Revision 1.33 2001/10/07 18:04:49 oes
164 * Changed server_http11 to server_http and its pattern to "HTTP".
165 * Additional functionality: it now saves the HTTP status into
166 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
168 * Revision 1.32 2001/10/07 15:43:28 oes
169 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
170 * client_te and client_accept_encoding_adder, triggered by the new
171 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
172 * changed to allow only identity and chunked, and the TE header is
173 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
175 * parse_http_request no longer does anything than parsing. The rewriting
176 * of http->cmd and version mangling are gone. It now also recognizes
177 * the put and delete methods and saves the url in http->url. Removed
180 * renamed content_type and content_length to have the server_ prefix
182 * server_content_type now only works if csp->content_type != CT_TABOO
184 * added server_transfer_encoding, which
185 * - Sets CT_TABOO to prohibit filtering if encoding compresses
186 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
187 * - Change from "chunked" to "identity" if body was chunked
188 * but has been de-chunked for filtering.
190 * added server_content_md5 which crunches any Content-MD5 headers
191 * if the body was modified.
193 * made server_http11 conditional on +downgrade action
195 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
197 * Revision 1.31 2001/10/05 14:25:02 oes
198 * Crumble Keep-Alive from Server
200 * Revision 1.30 2001/09/29 12:56:03 joergs
201 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
203 * Revision 1.29 2001/09/24 21:09:24 jongfoster
204 * Fixing 2 memory leaks that Guy spotted, where the paramater to
205 * enlist() was not being free()d.
207 * Revision 1.28 2001/09/22 16:32:28 jongfoster
208 * Removing unused #includes.
210 * Revision 1.27 2001/09/20 15:45:25 steudten
212 * add casting from size_t to int for printf()
213 * remove local variable shadow s2
215 * Revision 1.26 2001/09/16 17:05:14 jongfoster
216 * Removing unused #include showarg.h
218 * Revision 1.25 2001/09/16 13:21:27 jongfoster
219 * Changes to use new list functions.
221 * Revision 1.24 2001/09/13 23:05:50 jongfoster
222 * Changing the string paramater to the header parsers a "const".
224 * Revision 1.23 2001/09/12 18:08:19 steudten
226 * In parse_http_request() header rewriting miss the host value, so
227 * from http://www.mydomain.com the result was just " / " not
228 * http://www.mydomain.com/ in case we forward.
230 * Revision 1.22 2001/09/10 10:58:53 oes
231 * Silenced compiler warnings
233 * Revision 1.21 2001/07/31 14:46:00 oes
234 * - Persistant connections now suppressed
235 * - sed() no longer appends empty header to csp->headers
237 * Revision 1.20 2001/07/30 22:08:36 jongfoster
238 * Tidying up #defines:
239 * - All feature #defines are now of the form FEATURE_xxx
240 * - Permanently turned off WIN_GUI_EDIT
241 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
243 * Revision 1.19 2001/07/25 17:21:54 oes
244 * client_uagent now saves copy of User-Agent: header value
246 * Revision 1.18 2001/07/13 14:02:46 oes
247 * - Included fix to repair broken HTTP requests that
248 * don't contain a path, not even '/'.
249 * - Removed all #ifdef PCRS
250 * - content_type now always inspected and classified as
251 * text, gif or other.
252 * - formatting / comments
254 * Revision 1.17 2001/06/29 21:45:41 oes
255 * Indentation, CRLF->LF, Tab-> Space
257 * Revision 1.16 2001/06/29 13:32:42 oes
259 * - Adapted free_http_request
260 * - Removed logentry from cancelled commit
262 * Revision 1.15 2001/06/03 19:12:38 oes
263 * deleted const struct interceptors
265 * Revision 1.14 2001/06/01 18:49:17 jongfoster
266 * Replaced "list_share" with "list" - the tiny memory gain was not
267 * worth the extra complexity.
269 * Revision 1.13 2001/05/31 21:30:33 jongfoster
270 * Removed list code - it's now in list.[ch]
271 * Renamed "permission" to "action", and changed many features
272 * to use the actions file rather than the global config.
274 * Revision 1.12 2001/05/31 17:33:13 oes
278 * Revision 1.11 2001/05/29 20:11:19 joergs
279 * '/ * inside comment' warning removed.
281 * Revision 1.10 2001/05/29 09:50:24 jongfoster
282 * Unified blocklist/imagelist/permissionslist.
283 * File format is still under discussion, but the internal changes
286 * Also modified interceptor behaviour:
287 * - We now intercept all URLs beginning with one of the following
288 * prefixes (and *only* these prefixes):
290 * * http://ijbswa.sf.net/config/
291 * * http://ijbswa.sourceforge.net/config/
292 * - New interceptors "home page" - go to http://i.j.b/ to see it.
293 * - Internal changes so that intercepted and fast redirect pages
294 * are not replaced with an image.
295 * - Interceptors now have the option to send a binary page direct
296 * to the client. (i.e. ijb-send-banner uses this)
297 * - Implemented show-url-info interceptor. (Which is why I needed
298 * the above interceptors changes - a typical URL is
299 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
300 * The previous mechanism would not have intercepted that, and
301 * if it had been intercepted then it then it would have replaced
304 * Revision 1.9 2001/05/28 17:26:33 jongfoster
305 * Fixing segfault if last header was crunched.
306 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
307 * can use the cross-platform sprintf() instead.)
309 * Revision 1.8 2001/05/27 22:17:04 oes
311 * - re_process_buffer no longer writes the modified buffer
312 * to the client, which was very ugly. It now returns the
313 * buffer, which it is then written by chat.
315 * - content_length now adjusts the Content-Length: header
316 * for modified documents rather than crunch()ing it.
317 * (Length info in csp->content_length, which is 0 for
318 * unmodified documents)
320 * - For this to work, sed() is called twice when filtering.
322 * Revision 1.7 2001/05/27 13:19:06 oes
323 * Patched Joergs solution for the content-length in.
325 * Revision 1.6 2001/05/26 13:39:32 jongfoster
326 * Only crunches Content-Length header if applying RE filtering.
327 * Without this fix, Microsoft Windows Update wouldn't work.
329 * Revision 1.5 2001/05/26 00:28:36 jongfoster
330 * Automatic reloading of config file.
331 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
332 * Most of the global variables have been moved to a new
333 * struct configuration_spec, accessed through csp->config->globalname
334 * Most of the globals remaining are used by the Win32 GUI.
336 * Revision 1.4 2001/05/22 18:46:04 oes
338 * - Enabled filtering banners by size rather than URL
339 * by adding patterns that replace all standard banner
340 * sizes with the "Junkbuster" gif to the re_filterfile
342 * - Enabled filtering WebBugs by providing a pattern
343 * which kills all 1x1 images
345 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
346 * which is selected by the (nonstandard and therefore
347 * capital) letter 'U' in the option string.
348 * It causes the quantifiers to be ungreedy by default.
349 * Appending a ? turns back to greedy (!).
351 * - Added a new interceptor ijb-send-banner, which
352 * sends back the "Junkbuster" gif. Without imagelist or
353 * MSIE detection support, or if tinygif = 1, or the
354 * URL isn't recognized as an imageurl, a lame HTML
355 * explanation is sent instead.
357 * - Added new feature, which permits blocking remote
358 * script redirects and firing back a local redirect
360 * The feature is conditionally compiled, i.e. it
361 * can be disabled with --disable-fast-redirects,
362 * plus it must be activated by a "fast-redirects"
363 * line in the config file, has its own log level
364 * and of course wants to be displayed by show-proxy-args
365 * Note: Boy, all the #ifdefs in 1001 locations and
366 * all the fumbling with configure.in and acconfig.h
367 * were *way* more work than the feature itself :-(
369 * - Because a generic redirect template was needed for
370 * this, tinygif = 3 now uses the same.
372 * - Moved GIFs, and other static HTTP response templates
377 * - Removed some >400 CRs again (Jon, you really worked
380 * Revision 1.3 2001/05/20 01:21:20 jongfoster
381 * Version 2.9.4 checkin.
382 * - Merged popupfile and cookiefile, and added control over PCRS
383 * filtering, in new "permissionsfile".
384 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
385 * file error you now get a message box (in the Win32 GUI) rather
386 * than the program exiting with no explanation.
387 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
389 * - Removed tabs from "config"
390 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
391 * - Bumped up version number.
393 * Revision 1.2 2001/05/17 23:02:36 oes
394 * - Made referrer option accept 'L' as a substitute for '§'
396 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
397 * Initial import of version 2.9.3 source tree
400 *********************************************************************/
407 #include <sys/types.h>
415 #if !defined(_WIN32) && !defined(__OS2__)
425 #include "jbsockets.h"
426 #include "miscutil.h"
429 const char parsers_h_rcs[] = PARSERS_H_VERSION;
431 /* Fix a problem with Solaris. There should be no effect on other
433 * Solaris's isspace() is a macro which uses it's argument directly
434 * as an array index. Therefore we need to make sure that high-bit
435 * characters generate +ve values, and ideally we also want to make
436 * the argument match the declared parameter type of "int".
438 * Why did they write a character function that can't take a simple
439 * "char" argument? Doh!
441 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
442 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
445 const struct parsers client_patterns[] = {
446 { "referer:", 8, client_referrer },
447 { "user-agent:", 11, client_uagent },
448 { "ua-", 3, client_ua },
449 { "from:", 5, client_from },
450 { "cookie:", 7, client_send_cookie },
451 { "x-forwarded-for:", 16, client_x_forwarded },
452 { "Accept-Encoding:", 16, client_accept_encoding },
453 { "TE:", 3, client_te },
454 { "Host:", 5, client_host },
455 /* { "if-modified-since:", 18, crumble }, */
456 { "Keep-Alive:", 11, crumble },
457 { "connection:", 11, crumble },
458 { "proxy-connection:", 17, crumble },
459 { "max-forwards:", 13, client_max_forwards },
464 const struct parsers server_patterns[] = {
465 { "HTTP", 4, server_http },
466 { "set-cookie:", 11, server_set_cookie },
467 { "connection:", 11, crumble },
468 { "Content-Type:", 13, server_content_type },
469 { "Content-Length:", 15, server_content_length },
470 { "Content-MD5:", 12, server_content_md5 },
471 { "Content-Encoding:", 17, server_content_encoding },
472 { "Transfer-Encoding:", 18, server_transfer_coding },
473 { "Keep-Alive:", 11, crumble },
478 const add_header_func_ptr add_client_headers[] = {
481 client_x_forwarded_adder,
483 client_accept_encoding_adder,
484 connection_close_adder,
489 const add_header_func_ptr add_server_headers[] = {
490 connection_close_adder,
495 /*********************************************************************
497 * Function : flush_socket
499 * Description : Write any pending "buffered" content.
502 * 1 : fd = file descriptor of the socket to read
503 * 2 : csp = Current client state (buffers, headers, etc...)
505 * Returns : On success, the number of bytes written are returned (zero
506 * indicates nothing was written). On error, -1 is returned,
507 * and errno is set appropriately. If count is zero and the
508 * file descriptor refers to a regular file, 0 will be
509 * returned without causing any other effect. For a special
510 * file, the results are not portable.
512 *********************************************************************/
513 int flush_socket(jb_socket fd, struct client_state *csp)
515 struct iob *iob = csp->iob;
516 int len = iob->eod - iob->cur;
523 if (write_socket(fd, iob->cur, (size_t)len))
527 iob->eod = iob->cur = iob->buf;
533 /*********************************************************************
535 * Function : add_to_iob
537 * Description : Add content to the buffered page, expanding the
538 * buffer if necessary.
541 * 1 : csp = Current client state (buffers, headers, etc...)
542 * 2 : buf = holds the content to be added to the page
543 * 3 : n = number of bytes to be added
545 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
546 * or buffer limit reached.
548 *********************************************************************/
549 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
551 struct iob *iob = csp->iob;
552 size_t used, offset, need, want;
555 if (n <= 0) return JB_ERR_OK;
557 used = iob->eod - iob->buf;
558 offset = iob->cur - iob->buf;
562 * If the buffer can't hold the new data, extend it first.
563 * Use the next power of two if possible, else use the actual need.
565 if (need > csp->config->buffer_limit)
567 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
568 return JB_ERR_MEMORY;
571 if (need > iob->size)
573 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
575 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
579 else if (NULL != (p = (char *)realloc(iob->buf, need)))
585 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
586 return JB_ERR_MEMORY;
589 /* Update the iob pointers */
590 iob->cur = p + offset;
595 /* copy the new data into the iob buffer */
596 memcpy(iob->eod, buf, (size_t)n);
598 /* point to the end of the data */
601 /* null terminate == cheap insurance */
609 /*********************************************************************
611 * Function : get_header
613 * Description : This (odd) routine will parse the csp->iob
616 * 1 : csp = Current client state (buffers, headers, etc...)
618 * Returns : Any one of the following:
620 * 1) a pointer to a dynamically allocated string that contains a header line
621 * 2) NULL indicating that the end of the header was reached
622 * 3) "" indicating that the end of the iob was reached before finding
623 * a complete header line.
625 *********************************************************************/
626 char *get_header(struct client_state *csp)
632 if ((iob->cur == NULL)
633 || ((p = strchr(iob->cur, '\n')) == NULL))
635 return(""); /* couldn't find a complete header */
640 ret = strdup(iob->cur);
643 /* FIXME No way to handle error properly */
644 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
649 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
651 /* is this a blank line (i.e. the end of the header) ? */
663 /*********************************************************************
665 * Function : get_header_value
667 * Description : Get the value of a given header from a chained list
668 * of header lines or return NULL if no such header is
669 * present in the list.
672 * 1 : header_list = pointer to list
673 * 2 : header_name = string with name of header to look for.
674 * Trailing colon required, capitalization
677 * Returns : NULL if not found, else value of header
679 *********************************************************************/
680 char *get_header_value(const struct list *header_list, const char *header_name)
682 struct list_entry *cur_entry;
688 length = strlen(header_name);
690 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
694 if (!strncmpic(cur_entry->str, header_name, length))
697 * Found: return pointer to start of value
699 ret = (char *) (cur_entry->str + length);
700 while (*ret && ijb_isspace(*ret)) ret++;
713 /*********************************************************************
717 * Description : add, delete or modify lines in the HTTP header streams.
718 * On entry, it receives a linked list of headers space
719 * that was allocated dynamically (both the list nodes
720 * and the header contents).
722 * As a side effect it frees the space used by the original
726 * 1 : pats = list of patterns to match against headers
727 * 2 : more_headers = list of functions to add more
728 * headers (client or server)
729 * 3 : csp = Current client state (buffers, headers, etc...)
731 * Returns : Single pointer to a fully formed header, or NULL
732 * on out-of-memory error.
734 *********************************************************************/
735 char *sed(const struct parsers pats[],
736 const add_header_func_ptr more_headers[],
737 struct client_state *csp)
739 struct list_entry *p;
740 const struct parsers *v;
741 const add_header_func_ptr *f;
742 jb_err err = JB_ERR_OK;
744 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
746 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
748 /* Header crunch()ed in previous run? -> ignore */
749 if (p->str == NULL) continue;
751 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
753 if (strncmpic(p->str, v->str, v->len) == 0)
755 err = v->parser(csp, (char **)&(p->str));
760 /* place any additional headers on the csp->headers list */
761 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
766 if (err != JB_ERR_OK)
771 return list_to_text(csp->headers);
775 /* here begins the family of parser functions that reformat header lines */
778 /*********************************************************************
782 * Description : This is called if a header matches a pattern to "crunch"
785 * 1 : csp = Current client state (buffers, headers, etc...)
786 * 2 : header = On input, pointer to header to modify.
787 * On output, pointer to the modified header, or NULL
788 * to remove the header. This function frees the
789 * original string if necessary.
791 * Returns : JB_ERR_OK on success, or
792 * JB_ERR_MEMORY on out-of-memory error.
794 *********************************************************************/
795 jb_err crumble(struct client_state *csp, char **header)
797 log_error(LOG_LEVEL_HEADER, "crunch!");
803 /*********************************************************************
805 * Function : server_content_type
807 * Description : Set the content-type for filterable types (text/.*,
808 * javascript and image/gif) unless filtering has been
809 * forbidden (CT_TABOO) while parsing earlier headers.
812 * 1 : csp = Current client state (buffers, headers, etc...)
813 * 2 : header = On input, pointer to header to modify.
814 * On output, pointer to the modified header, or NULL
815 * to remove the header. This function frees the
816 * original string if necessary.
818 * Returns : JB_ERR_OK on success, or
819 * JB_ERR_MEMORY on out-of-memory error.
821 *********************************************************************/
822 jb_err server_content_type(struct client_state *csp, char **header)
824 if (csp->content_type != CT_TABOO)
826 if (strstr(*header, " text/")
827 || strstr(*header, "application/x-javascript"))
828 csp->content_type = CT_TEXT;
829 else if (strstr(*header, " image/gif"))
830 csp->content_type = CT_GIF;
832 csp->content_type = 0;
839 /*********************************************************************
841 * Function : server_transfer_coding
843 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
844 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
845 * - Change from "chunked" to "identity" if body was chunked
846 * but has been de-chunked for filtering.
849 * 1 : csp = Current client state (buffers, headers, etc...)
850 * 2 : header = On input, pointer to header to modify.
851 * On output, pointer to the modified header, or NULL
852 * to remove the header. This function frees the
853 * original string if necessary.
855 * Returns : JB_ERR_OK on success, or
856 * JB_ERR_MEMORY on out-of-memory error.
858 *********************************************************************/
859 jb_err server_transfer_coding(struct client_state *csp, char **header)
862 * Turn off pcrs and gif filtering if body compressed
864 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
866 csp->content_type = CT_TABOO;
870 * Raise flag if body chunked
872 if (strstr(*header, "chunked"))
874 csp->flags |= CSP_FLAG_CHUNKED;
877 * If the body was modified, it has been
878 * de-chunked first, so adjust the header:
880 if (csp->flags & CSP_FLAG_MODIFIED)
883 *header = strdup("Transfer-Encoding: identity");
884 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
892 /*********************************************************************
894 * Function : server_content_encoding
896 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
899 * 1 : csp = Current client state (buffers, headers, etc...)
900 * 2 : header = On input, pointer to header to modify.
901 * On output, pointer to the modified header, or NULL
902 * to remove the header. This function frees the
903 * original string if necessary.
905 * Returns : JB_ERR_OK on success, or
906 * JB_ERR_MEMORY on out-of-memory error.
908 *********************************************************************/
909 jb_err server_content_encoding(struct client_state *csp, char **header)
912 * Turn off pcrs and gif filtering if body compressed
914 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
916 csp->content_type = CT_TABOO;
924 /*********************************************************************
926 * Function : server_content_length
928 * Description : Adjust Content-Length header if we modified
932 * 1 : csp = Current client state (buffers, headers, etc...)
933 * 2 : header = On input, pointer to header to modify.
934 * On output, pointer to the modified header, or NULL
935 * to remove the header. This function frees the
936 * original string if necessary.
938 * Returns : JB_ERR_OK on success, or
939 * JB_ERR_MEMORY on out-of-memory error.
941 *********************************************************************/
942 jb_err server_content_length(struct client_state *csp, char **header)
944 if (csp->content_length != 0) /* Content length has been modified */
947 *header = (char *) zalloc(100);
950 return JB_ERR_MEMORY;
953 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
955 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
962 /*********************************************************************
964 * Function : server_content_md5
966 * Description : Crumble any Content-MD5 headers if the document was
967 * modified. FIXME: Should we re-compute instead?
970 * 1 : csp = Current client state (buffers, headers, etc...)
971 * 2 : header = On input, pointer to header to modify.
972 * On output, pointer to the modified header, or NULL
973 * to remove the header. This function frees the
974 * original string if necessary.
976 * Returns : JB_ERR_OK on success, or
977 * JB_ERR_MEMORY on out-of-memory error.
979 *********************************************************************/
980 jb_err server_content_md5(struct client_state *csp, char **header)
982 if (csp->flags & CSP_FLAG_MODIFIED)
984 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
992 /*********************************************************************
994 * Function : client_accept_encoding
996 * Description : Rewrite the client's Accept-Encoding header so that
997 * if doesn't allow compression, if the action applies.
998 * Note: For HTTP/1.0 the absence of the header is enough.
1001 * 1 : csp = Current client state (buffers, headers, etc...)
1002 * 2 : header = On input, pointer to header to modify.
1003 * On output, pointer to the modified header, or NULL
1004 * to remove the header. This function frees the
1005 * original string if necessary.
1007 * Returns : JB_ERR_OK on success, or
1008 * JB_ERR_MEMORY on out-of-memory error.
1010 *********************************************************************/
1011 jb_err client_accept_encoding(struct client_state *csp, char **header)
1013 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1015 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1018 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1020 *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1021 if (*header == NULL)
1023 return JB_ERR_MEMORY;
1032 /*********************************************************************
1034 * Function : client_te
1036 * Description : Rewrite the client's TE header so that
1037 * if doesn't allow compression, if the action applies.
1040 * 1 : csp = Current client state (buffers, headers, etc...)
1041 * 2 : header = On input, pointer to header to modify.
1042 * On output, pointer to the modified header, or NULL
1043 * to remove the header. This function frees the
1044 * original string if necessary.
1046 * Returns : JB_ERR_OK on success, or
1047 * JB_ERR_MEMORY on out-of-memory error.
1049 *********************************************************************/
1050 jb_err client_te(struct client_state *csp, char **header)
1052 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1055 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1061 /*********************************************************************
1063 * Function : client_referrer
1065 * Description : Handle the "referer" config setting properly.
1066 * Called from `sed'.
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_referrer(struct client_state *csp, char **header)
1083 #ifdef FEATURE_FORCE_LOAD
1084 /* Since the referrer can include the prefix even
1085 * even if the request itself is non-forced, we must
1086 * clean it unconditionally
1088 strclean(*header, FORCE_PREFIX);
1089 #endif /* def FEATURE_FORCE_LOAD */
1092 * Are we sending referer?
1094 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1101 newval = csp->action->string[ACTION_STRING_REFERER];
1103 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1108 log_error(LOG_LEVEL_HEADER, "crunch!");
1111 else if (0 == strncmpic(newval, "http://", 7))
1114 * We have a specific (fixed) referer we want to send.
1116 log_error(LOG_LEVEL_HEADER, "modified");
1118 *header = strdup("Referer: ");
1119 string_append(header, newval);
1121 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1126 * Forge a referer as http://[hostname:port of REQUEST]/
1127 * to fool stupid checks for in-site links
1129 if (0 != strcmpic(newval, "forge"))
1132 * Invalid choice - but forge is probably the best default.
1134 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1137 *header = strdup("Referer: http://");
1138 string_append(header, csp->http->hostport);
1139 string_append(header, "/");
1140 log_error(LOG_LEVEL_HEADER, "crunch+forge to %s", *header);
1142 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1147 /*********************************************************************
1149 * Function : client_uagent
1151 * Description : Handle the "user-agent" config setting properly
1152 * and remember its original value to enable browser
1153 * bug workarounds. Called from `sed'.
1156 * 1 : csp = Current client state (buffers, headers, etc...)
1157 * 2 : header = On input, pointer to header to modify.
1158 * On output, pointer to the modified header, or NULL
1159 * to remove the header. This function frees the
1160 * original string if necessary.
1162 * Returns : JB_ERR_OK on success, or
1163 * JB_ERR_MEMORY on out-of-memory error.
1165 *********************************************************************/
1166 jb_err client_uagent(struct client_state *csp, char **header)
1170 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1175 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1181 log_error(LOG_LEVEL_HEADER, "modified");
1184 *header = strdup("User-Agent: ");
1185 string_append(header, newval);
1187 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1191 /*********************************************************************
1193 * Function : client_ua
1195 * Description : Handle "ua-" headers properly. Called from `sed'.
1198 * 1 : csp = Current client state (buffers, headers, etc...)
1199 * 2 : header = On input, pointer to header to modify.
1200 * On output, pointer to the modified header, or NULL
1201 * to remove the header. This function frees the
1202 * original string if necessary.
1204 * Returns : JB_ERR_OK on success, or
1205 * JB_ERR_MEMORY on out-of-memory error.
1207 *********************************************************************/
1208 jb_err client_ua(struct client_state *csp, char **header)
1210 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1212 log_error(LOG_LEVEL_HEADER, "crunch!");
1220 /*********************************************************************
1222 * Function : client_from
1224 * Description : Handle the "from" config setting properly.
1225 * Called from `sed'.
1228 * 1 : csp = Current client state (buffers, headers, etc...)
1229 * 2 : header = On input, pointer to header to modify.
1230 * On output, pointer to the modified header, or NULL
1231 * to remove the header. This function frees the
1232 * original string if necessary.
1234 * Returns : JB_ERR_OK on success, or
1235 * JB_ERR_MEMORY on out-of-memory error.
1237 *********************************************************************/
1238 jb_err client_from(struct client_state *csp, char **header)
1242 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1249 newval = csp->action->string[ACTION_STRING_FROM];
1252 * Are we blocking the e-mail address?
1254 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1256 log_error(LOG_LEVEL_HEADER, "crunch!");
1260 log_error(LOG_LEVEL_HEADER, " modified");
1262 *header = strdup("From: ");
1263 string_append(header, newval);
1265 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1269 /*********************************************************************
1271 * Function : client_send_cookie
1273 * Description : Handle the "cookie" header properly. Called from `sed'.
1274 * If cookie is accepted, add it to the cookie_list,
1275 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1278 * 1 : csp = Current client state (buffers, headers, etc...)
1279 * 2 : header = On input, pointer to header to modify.
1280 * On output, pointer to the modified header, or NULL
1281 * to remove the header. This function frees the
1282 * original string if necessary.
1284 * Returns : JB_ERR_OK on success, or
1285 * JB_ERR_MEMORY on out-of-memory error.
1287 *********************************************************************/
1288 jb_err client_send_cookie(struct client_state *csp, char **header)
1290 jb_err result = JB_ERR_OK;
1292 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1294 /* strlen("cookie: ") == 8 */
1295 result = enlist(csp->cookie_list, *header + 8);
1299 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
1303 * Always remove the cookie here. The cookie header
1304 * will be sent at the end of the header.
1312 /*********************************************************************
1314 * Function : client_x_forwarded
1316 * Description : Handle the "x-forwarded-for" config setting properly,
1317 * also used in the add_client_headers list. Called from `sed'.
1320 * 1 : csp = Current client state (buffers, headers, etc...)
1321 * 2 : header = On input, pointer to header to modify.
1322 * On output, pointer to the modified header, or NULL
1323 * to remove the header. This function frees the
1324 * original string if necessary.
1326 * Returns : JB_ERR_OK on success, or
1327 * JB_ERR_MEMORY on out-of-memory error.
1329 *********************************************************************/
1330 jb_err client_x_forwarded(struct client_state *csp, char **header)
1332 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1334 /* Save it so we can re-add it later */
1335 freez(csp->x_forwarded);
1336 csp->x_forwarded = *header;
1339 * Always set *header = NULL, since this information
1340 * will be sent at the end of the header.
1347 log_error(LOG_LEVEL_HEADER, " crunch!");
1354 /*********************************************************************
1356 * Function : client_max_forwards
1358 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
1359 * from the value of the Max-Forwards header field.
1362 * 1 : csp = Current client state (buffers, headers, etc...)
1363 * 2 : header = On input, pointer to header to modify.
1364 * On output, pointer to the modified header, or NULL
1365 * to remove the header. This function frees the
1366 * original string if necessary.
1368 * Returns : JB_ERR_OK on success, or
1369 * JB_ERR_MEMORY on out-of-memory error.
1371 *********************************************************************/
1372 jb_err client_max_forwards(struct client_state *csp, char **header)
1374 unsigned int max_forwards;
1376 if ((0 == strcmpic(csp->http->gpc, "trace"))
1377 || (0 == strcmpic(csp->http->gpc, "options")))
1379 if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
1381 if (max_forwards-- >= 1)
1383 sprintf(*header, "Max-Forwards: %u", max_forwards);
1384 log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
1388 log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
1397 /*********************************************************************
1399 * Function : client_host
1401 * Description : If the request URI did not contain host and
1402 * port information, parse and evaluate the Host
1406 * 1 : csp = Current client state (buffers, headers, etc...)
1407 * 2 : header = On input, pointer to header to modify.
1408 * On output, pointer to the modified header, or NULL
1409 * to remove the header. This function frees the
1410 * original string if necessary.
1412 * Returns : JB_ERR_OK on success, or
1413 * JB_ERR_MEMORY on out-of-memory error.
1415 *********************************************************************/
1416 jb_err client_host(struct client_state *csp, char **header)
1420 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
1421 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
1424 if (NULL == (p = strdup((*header)+6)))
1426 return JB_ERR_MEMORY;
1429 if (NULL == (q = strdup(p)))
1432 return JB_ERR_MEMORY;
1435 freez(csp->http->hostport);
1436 csp->http->hostport = p;
1437 freez(csp->http->host);
1438 csp->http->host = q;
1439 q = strchr(csp->http->host, ':');
1442 /* Terminate hostname and evaluate port string */
1444 csp->http->port = atoi(q);
1448 csp->http->port = csp->http->ssl ? 443 : 80;
1451 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
1452 csp->http->hostport, csp->http->host, csp->http->port);
1459 /* the following functions add headers directly to the header list */
1461 /*********************************************************************
1463 * Function : client_host_adder
1465 * Description : Adds the Host: header field if it is missing.
1466 * Called from `sed'.
1469 * 1 : csp = Current client state (buffers, headers, etc...)
1471 * Returns : JB_ERR_OK on success, or
1472 * JB_ERR_MEMORY on out-of-memory error.
1474 *********************************************************************/
1475 jb_err client_host_adder(struct client_state *csp)
1480 if ( !csp->http->hostport || !*(csp->http->hostport))
1486 * remove 'user:pass@' from 'proto://user:pass@host'
1488 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
1494 p = csp->http->hostport;
1497 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
1498 err = enlist_unique_header(csp->headers, "Host", p);
1504 /*********************************************************************
1506 * Function : client_cookie_adder
1508 * Description : Used in the add_client_headers list. Called from `sed'.
1511 * 1 : csp = Current client state (buffers, headers, etc...)
1513 * Returns : JB_ERR_OK on success, or
1514 * JB_ERR_MEMORY on out-of-memory error.
1516 *********************************************************************/
1517 jb_err client_cookie_adder(struct client_state *csp)
1519 struct list_entry *lst;
1521 struct list_entry *list1 = csp->cookie_list->first;
1522 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1523 int first_cookie = 1;
1526 if ((list1 == NULL) && (list2 == NULL))
1532 tmp = strdup("Cookie: ");
1534 for (lst = list1; lst ; lst = lst->next)
1542 string_append(&tmp, "; ");
1544 string_append(&tmp, lst->str);
1547 for (lst = list2; lst ; lst = lst->next)
1555 string_append(&tmp, "; ");
1557 string_join(&tmp, cookie_encode(lst->str));
1562 return JB_ERR_MEMORY;
1565 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1566 err = enlist(csp->headers, tmp);
1572 /*********************************************************************
1574 * Function : client_accept_encoding_adder
1576 * Description : Add an Accept-Encoding header to the client's request
1577 * that disables compression if the action applies, and
1578 * the header is not already there. Called from `sed'.
1579 * Note: For HTTP/1.0, the absence of the header is enough.
1582 * 1 : csp = Current client state (buffers, headers, etc...)
1584 * Returns : JB_ERR_OK on success, or
1585 * JB_ERR_MEMORY on out-of-memory error.
1587 *********************************************************************/
1588 jb_err client_accept_encoding_adder(struct client_state *csp)
1590 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1591 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1593 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1600 /*********************************************************************
1602 * Function : client_xtra_adder
1604 * Description : Used in the add_client_headers list. Called from `sed'.
1607 * 1 : csp = Current client state (buffers, headers, etc...)
1609 * Returns : JB_ERR_OK on success, or
1610 * JB_ERR_MEMORY on out-of-memory error.
1612 *********************************************************************/
1613 jb_err client_xtra_adder(struct client_state *csp)
1615 struct list_entry *lst;
1618 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1619 lst ; lst = lst->next)
1621 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1622 err = enlist(csp->headers, lst->str);
1634 /*********************************************************************
1636 * Function : client_x_forwarded_adder
1638 * Description : Used in the add_client_headers list. Called from `sed'.
1641 * 1 : csp = Current client state (buffers, headers, etc...)
1643 * Returns : JB_ERR_OK on success, or
1644 * JB_ERR_MEMORY on out-of-memory error.
1646 *********************************************************************/
1647 jb_err client_x_forwarded_adder(struct client_state *csp)
1652 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1657 if (csp->x_forwarded)
1659 p = strdup(csp->x_forwarded);
1660 string_append(&p, ", ");
1664 p = strdup("X-Forwarded-For: ");
1666 string_append(&p, csp->ip_addr_str);
1670 return JB_ERR_MEMORY;
1673 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1674 err = enlist(csp->headers, p);
1681 /*********************************************************************
1683 * Function : connection_close_adder
1685 * Description : Adds a "Connection: close" header to csp->headers
1686 * as a temporary fix for the needed but missing HTTP/1.1
1687 * support. Called from `sed'.
1688 * FIXME: This whole function shouldn't be neccessary!
1691 * 1 : csp = Current client state (buffers, headers, etc...)
1693 * Returns : JB_ERR_OK on success, or
1694 * JB_ERR_MEMORY on out-of-memory error.
1696 *********************************************************************/
1697 jb_err connection_close_adder(struct client_state *csp)
1699 return enlist(csp->headers, "Connection: close");
1703 /*********************************************************************
1705 * Function : server_http
1707 * Description : - Save the HTTP Status into csp->http->status
1708 * - Set CT_TABOO to prevent filtering if the answer
1709 * is a partial range (HTTP status 206)
1710 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1714 * 1 : csp = Current client state (buffers, headers, etc...)
1715 * 2 : header = On input, pointer to header to modify.
1716 * On output, pointer to the modified header, or NULL
1717 * to remove the header. This function frees the
1718 * original string if necessary.
1720 * Returns : JB_ERR_OK on success, or
1721 * JB_ERR_MEMORY on out-of-memory error.
1723 *********************************************************************/
1724 jb_err server_http(struct client_state *csp, char **header)
1726 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1727 if (csp->http->status == 206)
1729 csp->content_type = CT_TABOO;
1732 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1735 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1742 /*********************************************************************
1744 * Function : server_set_cookie
1746 * Description : Handle the server "cookie" header properly.
1747 * Log cookie to the jar file. Then "crunch" it,
1748 * or accept it. Called from `sed'.
1751 * 1 : csp = Current client state (buffers, headers, etc...)
1752 * 2 : header = On input, pointer to header to modify.
1753 * On output, pointer to the modified header, or NULL
1754 * to remove the header. This function frees the
1755 * original string if necessary.
1757 * Returns : JB_ERR_OK on success, or
1758 * JB_ERR_MEMORY on out-of-memory error.
1760 *********************************************************************/
1761 jb_err server_set_cookie(struct client_state *csp, char **header)
1763 #ifdef FEATURE_COOKIE_JAR
1764 if (csp->config->jar)
1767 * Write timestamp into outbuf.
1769 * Complex because not all OSs have tm_gmtoff or
1770 * the %z field in strftime()
1772 char tempbuf[ BUFFER_SIZE ];
1776 #ifdef HAVE_LOCALTIME_R
1777 tm_now = *localtime_r(&now, &tm_now);
1779 tm_now = *localtime (&now);
1781 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1783 /* strlen("set-cookie: ") = 12 */
1784 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1786 #endif /* def FEATURE_COOKIE_JAR */
1788 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1790 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
1791 return crumble(csp, header);
1793 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1795 /* Flag whether or not to log a message */
1798 /* A variable to store the tag we're working on */
1801 /* Skip "Set-Cookie:" (11 characters) in header */
1802 cur_tag = *header + 11;
1804 /* skip whitespace between "Set-Cookie:" and value */
1805 while (*cur_tag && ijb_isspace(*cur_tag))
1810 /* Loop through each tag in the cookie */
1814 char *next_tag = strchr(cur_tag, ';');
1815 if (next_tag != NULL)
1817 /* Skip the ';' character itself */
1820 /* skip whitespace ";" and start of tag */
1821 while (*next_tag && ijb_isspace(*next_tag))
1828 /* "Next tag" is the end of the string */
1829 next_tag = cur_tag + strlen(cur_tag);
1832 /* Is this the "Expires" tag? */
1833 if (strncmpic(cur_tag, "expires=", 8) == 0)
1835 /* Delete the tag by copying the rest of the string over it.
1836 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1837 * since the behaviour of strcpy is undefined for overlapping
1840 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1842 /* That changed the header, need to issue a log message */
1845 /* Note that the next tag has now been moved to *cur_tag,
1846 * so we do not need to update the cur_tag pointer.
1851 /* Move on to next cookie tag */
1858 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1866 #ifdef FEATURE_FORCE_LOAD
1867 /*********************************************************************
1869 * Function : strclean
1871 * Description : In-Situ-Eliminate all occurances of substring in
1875 * 1 : string = string to clean
1876 * 2 : substring = substring to eliminate
1878 * Returns : Number of eliminations
1880 *********************************************************************/
1881 int strclean(const char *string, const char *substring)
1883 int hits = 0, len = strlen(substring);
1886 while((pos = strstr(string, substring)) != NULL)
1893 while (*p++ != '\0');
1900 #endif /* def FEATURE_FORCE_LOAD */