1 const char parsers_rcs[] = "$Id: parsers.c,v 2.5 2003/09/25 01:44:33 david__schmidt Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/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 2.5 2003/09/25 01:44:33 david__schmidt
44 * Resyncing HEAD with v_3_0_branch for two OSX fixes:
45 * Making thread IDs look sane in the logfile for Mach kernels,
46 * and fixing multithreading crashes due to thread-unsafe
50 * Revision 2.4 2003/01/26 20:24:26 david__schmidt
51 * Updated activity console instrumentation locations
53 * Revision 2.3 2002/12/28 03:58:19 david__schmidt
54 * Initial drop of dashboard instrumentation - enabled with
55 * --enable-activity-console
57 * Revision 2.2 2002/11/10 04:20:38 hal9
58 * Fix typo: supressed -> suppressed
60 * Revision 2.1 2002/09/11 11:23:59 oes
61 * Fixed logging of cookies: Killed incoming cookies now logged; incoming/outgoing cookies now distinguished in log
63 * Revision 2.0 2002/06/04 14:34:21 jongfoster
64 * Moving source files to src/
66 * Revision 1.56 2002/05/12 15:34:22 jongfoster
67 * Fixing typo in a comment
69 * Revision 1.55 2002/05/08 16:01:07 oes
70 * Optimized add_to_iob:
71 * - Use realloc instead of malloc(), memcpy(), free()
72 * - Expand to powers of two if possible, to get
73 * O(log n) reallocs instead of O(n).
74 * - Moved check for buffer limit here from chat
75 * - Report failure via returncode
77 * Revision 1.54 2002/04/02 15:03:16 oes
80 * Revision 1.53 2002/03/26 22:29:55 swa
81 * we have a new homepage!
83 * Revision 1.52 2002/03/24 13:25:43 swa
84 * name change related issues
86 * Revision 1.51 2002/03/13 00:27:05 jongfoster
89 * Revision 1.50 2002/03/12 01:45:35 oes
90 * More verbose logging
92 * Revision 1.49 2002/03/09 20:03:52 jongfoster
93 * - Making various functions return int rather than size_t.
94 * (Undoing a recent change). Since size_t is unsigned on
95 * Windows, functions like read_socket that return -1 on
96 * error cannot return a size_t.
98 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
99 * crashes, and also frequently caused JB to jump to 100%
100 * CPU and stay there. (Because it thought it had just
101 * read ((unsigned)-1) == 4Gb of data...)
103 * - The signature of write_socket has changed, it now simply
104 * returns success=0/failure=nonzero.
106 * - Trying to get rid of a few warnings --with-debug on
107 * Windows, I've introduced a new type "jb_socket". This is
108 * used for the socket file descriptors. On Windows, this
109 * is SOCKET (a typedef for unsigned). Everywhere else, it's
110 * an int. The error value can't be -1 any more, so it's
111 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
112 * Windows it maps to the #define INVALID_SOCKET.)
114 * - The signature of bind_port has changed.
116 * Revision 1.48 2002/03/07 03:46:53 oes
117 * Fixed compiler warnings etc
119 * Revision 1.47 2002/02/20 23:15:13 jongfoster
120 * Parsing functions now handle out-of-memory gracefully by returning
123 * Revision 1.46 2002/01/17 21:03:47 jongfoster
124 * Moving all our URL and URL pattern parsing code to urlmatch.c.
126 * Revision 1.45 2002/01/09 14:33:03 oes
127 * Added support for localtime_r.
129 * Revision 1.44 2001/12/14 01:22:54 steudten
130 * Remove 'user:pass@' from 'proto://user:pass@host' for the
131 * new added header 'Host: ..'. (See Req ID 491818)
133 * Revision 1.43 2001/11/23 00:26:38 jongfoster
134 * Fixing two really stupid errors in my previous commit
136 * Revision 1.42 2001/11/22 21:59:30 jongfoster
137 * Adding code to handle +no-cookies-keep
139 * Revision 1.41 2001/11/05 23:43:05 steudten
140 * Add time+date to log files.
142 * Revision 1.40 2001/10/26 20:13:09 jongfoster
143 * ctype.h is needed in Windows, too.
145 * Revision 1.39 2001/10/26 17:40:04 oes
146 * Introduced get_header_value()
147 * Removed http->user_agent, csp->referrer and csp->accept_types
148 * Removed client_accept()
150 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
151 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
152 * threads to call select() simultaneously. So, it's time to do a real, live,
153 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
154 * (native). Both versions will work, but using __OS2__ offers multi-threading.
156 * Revision 1.37 2001/10/23 21:36:02 jongfoster
157 * Documenting sed()'s error behaviou (doc change only)
159 * Revision 1.36 2001/10/13 12:51:51 joergs
160 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
161 * force-load), instead crumble Host: and add it (again) in client_host_adder
162 * (in case we get a HTTP/1.0 request without Host: header and forward it to
163 * a HTTP/1.1 server/proxy).
165 * Revision 1.35 2001/10/09 22:39:21 jongfoster
166 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
169 * Revision 1.34 2001/10/07 18:50:55 oes
170 * Added server_content_encoding, renamed server_transfer_encoding
172 * Revision 1.33 2001/10/07 18:04:49 oes
173 * Changed server_http11 to server_http and its pattern to "HTTP".
174 * Additional functionality: it now saves the HTTP status into
175 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
177 * Revision 1.32 2001/10/07 15:43:28 oes
178 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
179 * client_te and client_accept_encoding_adder, triggered by the new
180 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
181 * changed to allow only identity and chunked, and the TE header is
182 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
184 * parse_http_request no longer does anything than parsing. The rewriting
185 * of http->cmd and version mangling are gone. It now also recognizes
186 * the put and delete methods and saves the url in http->url. Removed
189 * renamed content_type and content_length to have the server_ prefix
191 * server_content_type now only works if csp->content_type != CT_TABOO
193 * added server_transfer_encoding, which
194 * - Sets CT_TABOO to prohibit filtering if encoding compresses
195 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
196 * - Change from "chunked" to "identity" if body was chunked
197 * but has been de-chunked for filtering.
199 * added server_content_md5 which crunches any Content-MD5 headers
200 * if the body was modified.
202 * made server_http11 conditional on +downgrade action
204 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
206 * Revision 1.31 2001/10/05 14:25:02 oes
207 * Crumble Keep-Alive from Server
209 * Revision 1.30 2001/09/29 12:56:03 joergs
210 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
212 * Revision 1.29 2001/09/24 21:09:24 jongfoster
213 * Fixing 2 memory leaks that Guy spotted, where the paramater to
214 * enlist() was not being free()d.
216 * Revision 1.28 2001/09/22 16:32:28 jongfoster
217 * Removing unused #includes.
219 * Revision 1.27 2001/09/20 15:45:25 steudten
221 * add casting from size_t to int for printf()
222 * remove local variable shadow s2
224 * Revision 1.26 2001/09/16 17:05:14 jongfoster
225 * Removing unused #include showarg.h
227 * Revision 1.25 2001/09/16 13:21:27 jongfoster
228 * Changes to use new list functions.
230 * Revision 1.24 2001/09/13 23:05:50 jongfoster
231 * Changing the string paramater to the header parsers a "const".
233 * Revision 1.23 2001/09/12 18:08:19 steudten
235 * In parse_http_request() header rewriting miss the host value, so
236 * from http://www.mydomain.com the result was just " / " not
237 * http://www.mydomain.com/ in case we forward.
239 * Revision 1.22 2001/09/10 10:58:53 oes
240 * Silenced compiler warnings
242 * Revision 1.21 2001/07/31 14:46:00 oes
243 * - Persistant connections now suppressed
244 * - sed() no longer appends empty header to csp->headers
246 * Revision 1.20 2001/07/30 22:08:36 jongfoster
247 * Tidying up #defines:
248 * - All feature #defines are now of the form FEATURE_xxx
249 * - Permanently turned off WIN_GUI_EDIT
250 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
252 * Revision 1.19 2001/07/25 17:21:54 oes
253 * client_uagent now saves copy of User-Agent: header value
255 * Revision 1.18 2001/07/13 14:02:46 oes
256 * - Included fix to repair broken HTTP requests that
257 * don't contain a path, not even '/'.
258 * - Removed all #ifdef PCRS
259 * - content_type now always inspected and classified as
260 * text, gif or other.
261 * - formatting / comments
263 * Revision 1.17 2001/06/29 21:45:41 oes
264 * Indentation, CRLF->LF, Tab-> Space
266 * Revision 1.16 2001/06/29 13:32:42 oes
268 * - Adapted free_http_request
269 * - Removed logentry from cancelled commit
271 * Revision 1.15 2001/06/03 19:12:38 oes
272 * deleted const struct interceptors
274 * Revision 1.14 2001/06/01 18:49:17 jongfoster
275 * Replaced "list_share" with "list" - the tiny memory gain was not
276 * worth the extra complexity.
278 * Revision 1.13 2001/05/31 21:30:33 jongfoster
279 * Removed list code - it's now in list.[ch]
280 * Renamed "permission" to "action", and changed many features
281 * to use the actions file rather than the global config.
283 * Revision 1.12 2001/05/31 17:33:13 oes
287 * Revision 1.11 2001/05/29 20:11:19 joergs
288 * '/ * inside comment' warning removed.
290 * Revision 1.10 2001/05/29 09:50:24 jongfoster
291 * Unified blocklist/imagelist/permissionslist.
292 * File format is still under discussion, but the internal changes
295 * Also modified interceptor behaviour:
296 * - We now intercept all URLs beginning with one of the following
297 * prefixes (and *only* these prefixes):
299 * * http://ijbswa.sf.net/config/
300 * * http://ijbswa.sourceforge.net/config/
301 * - New interceptors "home page" - go to http://i.j.b/ to see it.
302 * - Internal changes so that intercepted and fast redirect pages
303 * are not replaced with an image.
304 * - Interceptors now have the option to send a binary page direct
305 * to the client. (i.e. ijb-send-banner uses this)
306 * - Implemented show-url-info interceptor. (Which is why I needed
307 * the above interceptors changes - a typical URL is
308 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
309 * The previous mechanism would not have intercepted that, and
310 * if it had been intercepted then it then it would have replaced
313 * Revision 1.9 2001/05/28 17:26:33 jongfoster
314 * Fixing segfault if last header was crunched.
315 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
316 * can use the cross-platform sprintf() instead.)
318 * Revision 1.8 2001/05/27 22:17:04 oes
320 * - re_process_buffer no longer writes the modified buffer
321 * to the client, which was very ugly. It now returns the
322 * buffer, which it is then written by chat.
324 * - content_length now adjusts the Content-Length: header
325 * for modified documents rather than crunch()ing it.
326 * (Length info in csp->content_length, which is 0 for
327 * unmodified documents)
329 * - For this to work, sed() is called twice when filtering.
331 * Revision 1.7 2001/05/27 13:19:06 oes
332 * Patched Joergs solution for the content-length in.
334 * Revision 1.6 2001/05/26 13:39:32 jongfoster
335 * Only crunches Content-Length header if applying RE filtering.
336 * Without this fix, Microsoft Windows Update wouldn't work.
338 * Revision 1.5 2001/05/26 00:28:36 jongfoster
339 * Automatic reloading of config file.
340 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
341 * Most of the global variables have been moved to a new
342 * struct configuration_spec, accessed through csp->config->globalname
343 * Most of the globals remaining are used by the Win32 GUI.
345 * Revision 1.4 2001/05/22 18:46:04 oes
347 * - Enabled filtering banners by size rather than URL
348 * by adding patterns that replace all standard banner
349 * sizes with the "Junkbuster" gif to the re_filterfile
351 * - Enabled filtering WebBugs by providing a pattern
352 * which kills all 1x1 images
354 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
355 * which is selected by the (nonstandard and therefore
356 * capital) letter 'U' in the option string.
357 * It causes the quantifiers to be ungreedy by default.
358 * Appending a ? turns back to greedy (!).
360 * - Added a new interceptor ijb-send-banner, which
361 * sends back the "Junkbuster" gif. Without imagelist or
362 * MSIE detection support, or if tinygif = 1, or the
363 * URL isn't recognized as an imageurl, a lame HTML
364 * explanation is sent instead.
366 * - Added new feature, which permits blocking remote
367 * script redirects and firing back a local redirect
369 * The feature is conditionally compiled, i.e. it
370 * can be disabled with --disable-fast-redirects,
371 * plus it must be activated by a "fast-redirects"
372 * line in the config file, has its own log level
373 * and of course wants to be displayed by show-proxy-args
374 * Note: Boy, all the #ifdefs in 1001 locations and
375 * all the fumbling with configure.in and acconfig.h
376 * were *way* more work than the feature itself :-(
378 * - Because a generic redirect template was needed for
379 * this, tinygif = 3 now uses the same.
381 * - Moved GIFs, and other static HTTP response templates
386 * - Removed some >400 CRs again (Jon, you really worked
389 * Revision 1.3 2001/05/20 01:21:20 jongfoster
390 * Version 2.9.4 checkin.
391 * - Merged popupfile and cookiefile, and added control over PCRS
392 * filtering, in new "permissionsfile".
393 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
394 * file error you now get a message box (in the Win32 GUI) rather
395 * than the program exiting with no explanation.
396 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
398 * - Removed tabs from "config"
399 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
400 * - Bumped up version number.
402 * Revision 1.2 2001/05/17 23:02:36 oes
403 * - Made referrer option accept 'L' as a substitute for '§'
405 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
406 * Initial import of version 2.9.3 source tree
409 *********************************************************************/
416 #include <sys/types.h>
424 #if !defined(_WIN32) && !defined(__OS2__)
431 /* jcc.h is for mutex semapores only */
432 #endif /* def OSX_DARWIN */
439 #include "jbsockets.h"
440 #include "miscutil.h"
442 #ifdef FEATURE_ACTIVITY_CONSOLE
444 #endif /* def FEATURE_ACTIVITY_CONSOLE */
446 const char parsers_h_rcs[] = PARSERS_H_VERSION;
448 /* Fix a problem with Solaris. There should be no effect on other
450 * Solaris's isspace() is a macro which uses it's argument directly
451 * as an array index. Therefore we need to make sure that high-bit
452 * characters generate +ve values, and ideally we also want to make
453 * the argument match the declared parameter type of "int".
455 * Why did they write a character function that can't take a simple
456 * "char" argument? Doh!
458 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
459 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
462 const struct parsers client_patterns[] = {
463 { "referer:", 8, client_referrer },
464 { "user-agent:", 11, client_uagent },
465 { "ua-", 3, client_ua },
466 { "from:", 5, client_from },
467 { "cookie:", 7, client_send_cookie },
468 { "x-forwarded-for:", 16, client_x_forwarded },
469 { "Accept-Encoding:", 16, client_accept_encoding },
470 { "TE:", 3, client_te },
471 { "Host:", 5, crumble },
472 /* { "if-modified-since:", 18, crumble }, */
473 { "Keep-Alive:", 11, crumble },
474 { "connection:", 11, crumble },
475 { "proxy-connection:", 17, crumble },
480 const struct parsers server_patterns[] = {
481 { "HTTP", 4, server_http },
482 { "set-cookie:", 11, server_set_cookie },
483 { "connection:", 11, crumble },
484 { "Content-Type:", 13, server_content_type },
485 { "Content-Length:", 15, server_content_length },
486 { "Content-MD5:", 12, server_content_md5 },
487 { "Content-Encoding:", 17, server_content_encoding },
488 { "Transfer-Encoding:", 18, server_transfer_coding },
489 { "Keep-Alive:", 11, crumble },
494 const add_header_func_ptr add_client_headers[] = {
497 client_x_forwarded_adder,
499 client_accept_encoding_adder,
500 connection_close_adder,
505 const add_header_func_ptr add_server_headers[] = {
506 connection_close_adder,
511 /*********************************************************************
513 * Function : flush_socket
515 * Description : Write any pending "buffered" content.
518 * 1 : fd = file descriptor of the socket to read
519 * 2 : csp = Current client state (buffers, headers, etc...)
521 * Returns : On success, the number of bytes written are returned (zero
522 * indicates nothing was written). On error, -1 is returned,
523 * and errno is set appropriately. If count is zero and the
524 * file descriptor refers to a regular file, 0 will be
525 * returned without causing any other effect. For a special
526 * file, the results are not portable.
528 *********************************************************************/
529 int flush_socket(jb_socket fd, struct client_state *csp)
531 struct iob *iob = csp->iob;
532 int len = iob->eod - iob->cur;
539 if (write_socket(fd, iob->cur, (size_t)len))
543 iob->eod = iob->cur = iob->buf;
549 /*********************************************************************
551 * Function : add_to_iob
553 * Description : Add content to the buffered page, expanding the
554 * buffer if necessary.
557 * 1 : csp = Current client state (buffers, headers, etc...)
558 * 2 : buf = holds the content to be added to the page
559 * 3 : n = number of bytes to be added
561 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
562 * or buffer limit reached.
564 *********************************************************************/
565 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
567 struct iob *iob = csp->iob;
568 size_t used, offset, need, want;
571 if (n <= 0) return JB_ERR_OK;
573 used = iob->eod - iob->buf;
574 offset = iob->cur - iob->buf;
578 * If the buffer can't hold the new data, extend it first.
579 * Use the next power of two if possible, else use the actual need.
581 if (need > csp->config->buffer_limit)
583 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
584 return JB_ERR_MEMORY;
587 if (need > iob->size)
589 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
591 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
595 else if (NULL != (p = (char *)realloc(iob->buf, need)))
601 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
602 return JB_ERR_MEMORY;
605 /* Update the iob pointers */
606 iob->cur = p + offset;
611 /* copy the new data into the iob buffer */
612 memcpy(iob->eod, buf, (size_t)n);
614 /* point to the end of the data */
617 /* null terminate == cheap insurance */
625 /*********************************************************************
627 * Function : get_header
629 * Description : This (odd) routine will parse the csp->iob
632 * 1 : csp = Current client state (buffers, headers, etc...)
634 * Returns : Any one of the following:
636 * 1) a pointer to a dynamically allocated string that contains a header line
637 * 2) NULL indicating that the end of the header was reached
638 * 3) "" indicating that the end of the iob was reached before finding
639 * a complete header line.
641 *********************************************************************/
642 char *get_header(struct client_state *csp)
648 if ((iob->cur == NULL)
649 || ((p = strchr(iob->cur, '\n')) == NULL))
651 return(""); /* couldn't find a complete header */
656 ret = strdup(iob->cur);
659 /* FIXME No way to handle error properly */
660 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
665 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
667 /* is this a blank line (i.e. the end of the header) ? */
679 /*********************************************************************
681 * Function : get_header_value
683 * Description : Get the value of a given header from a chained list
684 * of header lines or return NULL if no such header is
685 * present in the list.
688 * 1 : header_list = pointer to list
689 * 2 : header_name = string with name of header to look for.
690 * Trailing colon required, capitalization
693 * Returns : NULL if not found, else value of header
695 *********************************************************************/
696 char *get_header_value(const struct list *header_list, const char *header_name)
698 struct list_entry *cur_entry;
704 length = strlen(header_name);
706 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
710 if (!strncmpic(cur_entry->str, header_name, length))
713 * Found: return pointer to start of value
715 ret = (char *) (cur_entry->str + length);
716 while (*ret && ijb_isspace(*ret)) ret++;
729 /*********************************************************************
733 * Description : add, delete or modify lines in the HTTP header streams.
734 * On entry, it receives a linked list of headers space
735 * that was allocated dynamically (both the list nodes
736 * and the header contents).
738 * As a side effect it frees the space used by the original
742 * 1 : pats = list of patterns to match against headers
743 * 2 : more_headers = list of functions to add more
744 * headers (client or server)
745 * 3 : csp = Current client state (buffers, headers, etc...)
747 * Returns : Single pointer to a fully formed header, or NULL
748 * on out-of-memory error.
750 *********************************************************************/
751 char *sed(const struct parsers pats[],
752 const add_header_func_ptr more_headers[],
753 struct client_state *csp)
755 struct list_entry *p;
756 const struct parsers *v;
757 const add_header_func_ptr *f;
758 jb_err err = JB_ERR_OK;
760 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
762 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
764 /* Header crunch()ed in previous run? -> ignore */
765 if (p->str == NULL) continue;
767 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
769 if (strncmpic(p->str, v->str, v->len) == 0)
771 err = v->parser(csp, (char **)&(p->str));
776 /* place any additional headers on the csp->headers list */
777 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
782 if (err != JB_ERR_OK)
787 return list_to_text(csp->headers);
791 /* here begins the family of parser functions that reformat header lines */
794 /*********************************************************************
798 * Description : This is called if a header matches a pattern to "crunch"
801 * 1 : csp = Current client state (buffers, headers, etc...)
802 * 2 : header = On input, pointer to header to modify.
803 * On output, pointer to the modified header, or NULL
804 * to remove the header. This function frees the
805 * original string if necessary.
807 * Returns : JB_ERR_OK on success, or
808 * JB_ERR_MEMORY on out-of-memory error.
810 *********************************************************************/
811 jb_err crumble(struct client_state *csp, char **header)
813 log_error(LOG_LEVEL_HEADER, "crunch: %s",*header);
819 /*********************************************************************
821 * Function : server_content_type
823 * Description : Set the content-type for filterable types (text/.*,
824 * javascript and image/gif) unless filtering has been
825 * forbidden (CT_TABOO) while parsing earlier headers.
828 * 1 : csp = Current client state (buffers, headers, etc...)
829 * 2 : header = On input, pointer to header to modify.
830 * On output, pointer to the modified header, or NULL
831 * to remove the header. This function frees the
832 * original string if necessary.
834 * Returns : JB_ERR_OK on success, or
835 * JB_ERR_MEMORY on out-of-memory error.
837 *********************************************************************/
838 jb_err server_content_type(struct client_state *csp, char **header)
840 if (csp->content_type != CT_TABOO)
842 if (strstr(*header, " text/")
843 || strstr(*header, "application/x-javascript"))
844 csp->content_type = CT_TEXT;
845 else if (strstr(*header, " image/gif"))
846 csp->content_type = CT_GIF;
848 csp->content_type = 0;
855 /*********************************************************************
857 * Function : server_transfer_coding
859 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
860 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
861 * - Change from "chunked" to "identity" if body was chunked
862 * but has been de-chunked for filtering.
865 * 1 : csp = Current client state (buffers, headers, etc...)
866 * 2 : header = On input, pointer to header to modify.
867 * On output, pointer to the modified header, or NULL
868 * to remove the header. This function frees the
869 * original string if necessary.
871 * Returns : JB_ERR_OK on success, or
872 * JB_ERR_MEMORY on out-of-memory error.
874 *********************************************************************/
875 jb_err server_transfer_coding(struct client_state *csp, char **header)
878 * Turn off pcrs and gif filtering if body compressed
880 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
882 csp->content_type = CT_TABOO;
886 * Raise flag if body chunked
888 if (strstr(*header, "chunked"))
890 csp->flags |= CSP_FLAG_CHUNKED;
893 * If the body was modified, it has been
894 * de-chunked first, so adjust the header:
896 if (csp->flags & CSP_FLAG_MODIFIED)
899 *header = strdup("Transfer-Encoding: identity");
900 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
908 /*********************************************************************
910 * Function : server_content_encoding
912 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
915 * 1 : csp = Current client state (buffers, headers, etc...)
916 * 2 : header = On input, pointer to header to modify.
917 * On output, pointer to the modified header, or NULL
918 * to remove the header. This function frees the
919 * original string if necessary.
921 * Returns : JB_ERR_OK on success, or
922 * JB_ERR_MEMORY on out-of-memory error.
924 *********************************************************************/
925 jb_err server_content_encoding(struct client_state *csp, char **header)
928 * Turn off pcrs and gif filtering if body compressed
930 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
932 csp->content_type = CT_TABOO;
940 /*********************************************************************
942 * Function : server_content_length
944 * Description : Adjust Content-Length header if we modified
948 * 1 : csp = Current client state (buffers, headers, etc...)
949 * 2 : header = On input, pointer to header to modify.
950 * On output, pointer to the modified header, or NULL
951 * to remove the header. This function frees the
952 * original string if necessary.
954 * Returns : JB_ERR_OK on success, or
955 * JB_ERR_MEMORY on out-of-memory error.
957 *********************************************************************/
958 jb_err server_content_length(struct client_state *csp, char **header)
960 if (csp->content_length != 0) /* Content length has been modified */
963 *header = (char *) zalloc(100);
966 return JB_ERR_MEMORY;
969 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
971 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
978 /*********************************************************************
980 * Function : server_content_md5
982 * Description : Crumble any Content-MD5 headers if the document was
983 * modified. FIXME: Should we re-compute instead?
986 * 1 : csp = Current client state (buffers, headers, etc...)
987 * 2 : header = On input, pointer to header to modify.
988 * On output, pointer to the modified header, or NULL
989 * to remove the header. This function frees the
990 * original string if necessary.
992 * Returns : JB_ERR_OK on success, or
993 * JB_ERR_MEMORY on out-of-memory error.
995 *********************************************************************/
996 jb_err server_content_md5(struct client_state *csp, char **header)
998 if (csp->flags & CSP_FLAG_MODIFIED)
1000 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5: %s", *header);
1008 /*********************************************************************
1010 * Function : client_accept_encoding
1012 * Description : Rewrite the client's Accept-Encoding header so that
1013 * if doesn't allow compression, if the action applies.
1014 * Note: For HTTP/1.0 the absence of the header is enough.
1017 * 1 : csp = Current client state (buffers, headers, etc...)
1018 * 2 : header = On input, pointer to header to modify.
1019 * On output, pointer to the modified header, or NULL
1020 * to remove the header. This function frees the
1021 * original string if necessary.
1023 * Returns : JB_ERR_OK on success, or
1024 * JB_ERR_MEMORY on out-of-memory error.
1026 *********************************************************************/
1027 jb_err client_accept_encoding(struct client_state *csp, char **header)
1029 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1031 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1034 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1036 *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1037 if (*header == NULL)
1039 return JB_ERR_MEMORY;
1048 /*********************************************************************
1050 * Function : client_te
1052 * Description : Rewrite the client's TE header so that
1053 * if doesn't allow compression, if the action applies.
1056 * 1 : csp = Current client state (buffers, headers, etc...)
1057 * 2 : header = On input, pointer to header to modify.
1058 * On output, pointer to the modified header, or NULL
1059 * to remove the header. This function frees the
1060 * original string if necessary.
1062 * Returns : JB_ERR_OK on success, or
1063 * JB_ERR_MEMORY on out-of-memory error.
1065 *********************************************************************/
1066 jb_err client_te(struct client_state *csp, char **header)
1068 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1071 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1077 /*********************************************************************
1079 * Function : client_referrer
1081 * Description : Handle the "referer" config setting properly.
1082 * Called from `sed'.
1085 * 1 : csp = Current client state (buffers, headers, etc...)
1086 * 2 : header = On input, pointer to header to modify.
1087 * On output, pointer to the modified header, or NULL
1088 * to remove the header. This function frees the
1089 * original string if necessary.
1091 * Returns : JB_ERR_OK on success, or
1092 * JB_ERR_MEMORY on out-of-memory error.
1094 *********************************************************************/
1095 jb_err client_referrer(struct client_state *csp, char **header)
1098 jb_err err = JB_ERR_OK;
1100 #ifdef FEATURE_FORCE_LOAD
1101 /* Since the referrer can include the prefix even
1102 * even if the request itself is non-forced, we must
1103 * clean it unconditionally
1105 strclean(*header, FORCE_PREFIX);
1106 #endif /* def FEATURE_FORCE_LOAD */
1109 * As long as we're not blocking the referer...
1111 if ((csp->action->flags & ACTION_HIDE_REFERER) != 0)
1114 newval = csp->action->string[ACTION_STRING_REFERER];
1116 #ifdef FEATURE_ACTIVITY_CONSOLE
1117 /* We are doing something with the referer. */
1118 accumulate_stats(STATS_REFERER, 1);
1119 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1122 * Note that each clause needs to freez the header pointer;
1123 * but everybody needs to use it for logging purposes before
1124 * it gets freed, so always free it just after logging it.
1126 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1131 log_error(LOG_LEVEL_HEADER, "crunch: %s", *header);
1135 else if (0 == strncmpic(newval, "http://", 7))
1138 * We have a specific (fixed) referer we want to send.
1140 log_error(LOG_LEVEL_HEADER, "modified: %s", *header);
1142 *header = strdup("Referer: ");
1143 if (NULL == *header)
1144 err = JB_ERR_MEMORY;
1146 err = string_append(header, newval);
1151 * Forge a referer as http://[hostname:port of REQUEST]/
1152 * to fool stupid checks for in-site links
1154 if (0 != strcmpic(newval, "forge"))
1157 * Invalid choice - but forge is probably the best default.
1159 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1162 log_error(LOG_LEVEL_HEADER, "crunch+forge: %s", *header);
1164 *header = strdup("Referer: http://");
1165 if (NULL == *header)
1166 err = JB_ERR_MEMORY;
1169 err = string_append(header, csp->http->hostport);
1170 err = string_append(header, "/");
1171 if (JB_ERR_OK == err)
1172 log_error(LOG_LEVEL_HEADER, "crunch+forged to: %s", *header);
1180 /*********************************************************************
1182 * Function : client_uagent
1184 * Description : Handle the "user-agent" config setting properly
1185 * and remember its original value to enable browser
1186 * bug workarounds. Called from `sed'.
1189 * 1 : csp = Current client state (buffers, headers, etc...)
1190 * 2 : header = On input, pointer to header to modify.
1191 * On output, pointer to the modified header, or NULL
1192 * to remove the header. This function frees the
1193 * original string if necessary.
1195 * Returns : JB_ERR_OK on success, or
1196 * JB_ERR_MEMORY on out-of-memory error.
1198 *********************************************************************/
1199 jb_err client_uagent(struct client_state *csp, char **header)
1202 jb_err err = JB_ERR_OK;
1205 * As long as we don't want to hide user-agent...
1207 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1209 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1212 log_error(LOG_LEVEL_HEADER, "modified: %s", *header);
1215 *header = strdup("User-Agent: ");
1216 err = string_append(header, newval);
1224 /*********************************************************************
1226 * Function : client_ua
1228 * Description : Handle "ua-" headers properly. Called from `sed'.
1231 * 1 : csp = Current client state (buffers, headers, etc...)
1232 * 2 : header = On input, pointer to header to modify.
1233 * On output, pointer to the modified header, or NULL
1234 * to remove the header. This function frees the
1235 * original string if necessary.
1237 * Returns : JB_ERR_OK on success, or
1238 * JB_ERR_MEMORY on out-of-memory error.
1240 *********************************************************************/
1241 jb_err client_ua(struct client_state *csp, char **header)
1243 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1245 #ifdef FEATURE_ACTIVITY_CONSOLE
1246 accumulate_stats(STATS_CLIENT_UA, 1);
1247 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1248 log_error(LOG_LEVEL_HEADER, "crunch: %s", *header);
1256 /*********************************************************************
1258 * Function : client_from
1260 * Description : Handle the "from" config setting properly.
1261 * Called from `sed'.
1264 * 1 : csp = Current client state (buffers, headers, etc...)
1265 * 2 : header = On input, pointer to header to modify.
1266 * On output, pointer to the modified header, or NULL
1267 * to remove the header. This function frees the
1268 * original string if necessary.
1270 * Returns : JB_ERR_OK on success, or
1271 * JB_ERR_MEMORY on out-of-memory error.
1273 *********************************************************************/
1274 jb_err client_from(struct client_state *csp, char **header)
1277 jb_err err = JB_ERR_OK;
1280 * As long as we should handle the "from" setting...
1282 if ((csp->action->flags & ACTION_HIDE_FROM) != 0)
1284 #ifdef FEATURE_ACTIVITY_CONSOLE
1285 /* We're doing something with it. */
1286 accumulate_stats(STATS_CLIENT_FROM, 1);
1287 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1289 newval = csp->action->string[ACTION_STRING_FROM];
1292 * Are we blocking the e-mail address?
1294 if ((NULL == newval) || (0 == strcmpic(newval, "block")) )
1296 log_error(LOG_LEVEL_HEADER, "crunch: %s", *header);
1301 log_error(LOG_LEVEL_HEADER, "modified: %s", *header);
1303 *header = strdup("From: ");
1304 err = string_append(header, newval);
1311 /*********************************************************************
1313 * Function : client_send_cookie
1315 * Description : Handle the "cookie" header properly. Called from `sed'.
1316 * If cookie is accepted, add it to the cookie_list,
1317 * else we crunch it. Mmmmmmmmmmm ... cookie ......
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_send_cookie(struct client_state *csp, char **header)
1332 jb_err err = JB_ERR_OK;
1334 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1336 /* strlen("cookie: ") == 8 */
1337 err = enlist(csp->cookie_list, *header + 8);
1341 log_error(LOG_LEVEL_HEADER, "Crunched cookie: %s", *header);
1345 * Always remove the cookie here. The cookie header
1346 * will be sent at the end of the header.
1354 /*********************************************************************
1356 * Function : client_x_forwarded
1358 * Description : Handle the "x-forwarded-for" config setting properly,
1359 * also used in the add_client_headers list. Called from `sed'.
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_x_forwarded(struct client_state *csp, char **header)
1374 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1376 /* Save it so we can re-add it later */
1377 freez(csp->x_forwarded);
1378 csp->x_forwarded = *header;
1381 * Always set *header = NULL, since this information
1382 * will be sent at the end of the header.
1388 #ifdef FEATURE_ACTIVITY_CONSOLE
1389 accumulate_stats(STATS_CLIENT_X_FORWARDED, 1);
1390 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1391 log_error(LOG_LEVEL_HEADER, "crunch: %s", *header);
1398 /* the following functions add headers directly to the header list */
1400 /*********************************************************************
1402 * Function : client_host_adder
1404 * Description : (re)adds the host header. Called from `sed'.
1407 * 1 : csp = Current client state (buffers, headers, etc...)
1409 * Returns : JB_ERR_OK on success, or
1410 * JB_ERR_MEMORY on out-of-memory error.
1412 *********************************************************************/
1413 jb_err client_host_adder(struct client_state *csp)
1417 jb_err err = JB_ERR_OK;
1419 if ( csp->http->hostport && *(csp->http->hostport))
1421 p = strdup("Host: ");
1425 * remove 'user:pass@' from 'proto://user:pass@host'
1427 if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
1429 string_append(&p, pos+1);
1433 string_append(&p, csp->http->hostport);
1436 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1438 err = enlist(csp->headers, p);
1444 err = JB_ERR_MEMORY;
1451 /*********************************************************************
1453 * Function : client_cookie_adder
1455 * Description : Used in the add_client_headers list. Called from `sed'.
1458 * 1 : csp = Current client state (buffers, headers, etc...)
1460 * Returns : JB_ERR_OK on success, or
1461 * JB_ERR_MEMORY on out-of-memory error.
1463 *********************************************************************/
1464 jb_err client_cookie_adder(struct client_state *csp)
1466 struct list_entry *lst;
1468 struct list_entry *list1 = csp->cookie_list->first;
1469 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1470 int first_cookie = 1;
1471 jb_err err = JB_ERR_OK;
1473 if ((list1 != NULL) || (list2 != NULL))
1475 tmp = strdup("Cookie: ");
1478 for (lst = list1; lst ; lst = lst->next)
1486 string_append(&tmp, "; ");
1488 string_append(&tmp, lst->str);
1491 for (lst = list2; lst ; lst = lst->next)
1499 string_append(&tmp, "; ");
1501 string_join(&tmp, cookie_encode(lst->str));
1503 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1504 err = enlist(csp->headers, tmp);
1509 err = JB_ERR_MEMORY;
1516 /*********************************************************************
1518 * Function : client_accept_encoding_adder
1520 * Description : Add an Accept-Encoding header to the client's request
1521 * that disables compression if the action applies, and
1522 * the header is not already there. Called from `sed'.
1523 * Note: For HTTP/1.0, the absence of the header is enough.
1526 * 1 : csp = Current client state (buffers, headers, etc...)
1528 * Returns : JB_ERR_OK on success, or
1529 * JB_ERR_MEMORY on out-of-memory error.
1531 *********************************************************************/
1532 jb_err client_accept_encoding_adder(struct client_state *csp)
1534 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1535 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1537 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1544 /*********************************************************************
1546 * Function : client_xtra_adder
1548 * Description : Used in the add_client_headers list. Called from `sed'.
1551 * 1 : csp = Current client state (buffers, headers, etc...)
1553 * Returns : JB_ERR_OK on success, or
1554 * JB_ERR_MEMORY on out-of-memory error.
1556 *********************************************************************/
1557 jb_err client_xtra_adder(struct client_state *csp)
1559 struct list_entry *lst;
1562 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1563 lst ; lst = lst->next)
1565 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1566 err = enlist(csp->headers, lst->str);
1578 /*********************************************************************
1580 * Function : client_x_forwarded_adder
1582 * Description : Used in the add_client_headers list. Called from `sed'.
1585 * 1 : csp = Current client state (buffers, headers, etc...)
1587 * Returns : JB_ERR_OK on success, or
1588 * JB_ERR_MEMORY on out-of-memory error.
1590 *********************************************************************/
1591 jb_err client_x_forwarded_adder(struct client_state *csp)
1594 jb_err err = JB_ERR_OK;
1596 if (0 == (csp->action->flags & ACTION_HIDE_FORWARDED))
1599 if (csp->x_forwarded)
1601 p = strdup(csp->x_forwarded);
1602 err = string_append(&p, ", ");
1606 p = strdup("X-Forwarded-For: ");
1608 err = string_append(&p, csp->ip_addr_str);
1612 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1613 err = enlist(csp->headers, p);
1621 /*********************************************************************
1623 * Function : connection_close_adder
1625 * Description : Adds a "Connection: close" header to csp->headers
1626 * as a temporary fix for the needed but missing HTTP/1.1
1627 * support. Called from `sed'.
1628 * FIXME: This whole function shouldn't be neccessary!
1631 * 1 : csp = Current client state (buffers, headers, etc...)
1633 * Returns : JB_ERR_OK on success, or
1634 * JB_ERR_MEMORY on out-of-memory error.
1636 *********************************************************************/
1637 jb_err connection_close_adder(struct client_state *csp)
1639 return enlist(csp->headers, "Connection: close");
1643 /*********************************************************************
1645 * Function : server_http
1647 * Description : - Save the HTTP Status into csp->http->status
1648 * - Set CT_TABOO to prevent filtering if the answer
1649 * is a partial range (HTTP status 206)
1650 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1654 * 1 : csp = Current client state (buffers, headers, etc...)
1655 * 2 : header = On input, pointer to header to modify.
1656 * On output, pointer to the modified header, or NULL
1657 * to remove the header. This function frees the
1658 * original string if necessary.
1660 * Returns : JB_ERR_OK on success, or
1661 * JB_ERR_MEMORY on out-of-memory error.
1663 *********************************************************************/
1664 jb_err server_http(struct client_state *csp, char **header)
1666 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1667 if (csp->http->status == 206)
1669 csp->content_type = CT_TABOO;
1672 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1675 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1682 /*********************************************************************
1684 * Function : server_set_cookie
1686 * Description : Handle the server "cookie" header properly.
1687 * Log cookie to the jar file. Then "crunch" it,
1688 * or accept it. Called from `sed'.
1691 * 1 : csp = Current client state (buffers, headers, etc...)
1692 * 2 : header = On input, pointer to header to modify.
1693 * On output, pointer to the modified header, or NULL
1694 * to remove the header. This function frees the
1695 * original string if necessary.
1697 * Returns : JB_ERR_OK on success, or
1698 * JB_ERR_MEMORY on out-of-memory error.
1700 *********************************************************************/
1701 jb_err server_set_cookie(struct client_state *csp, char **header)
1703 jb_err err = JB_ERR_OK;
1705 #ifdef FEATURE_COOKIE_JAR
1706 if (csp->config->jar)
1709 * Write timestamp into outbuf.
1711 * Complex because not all OSs have tm_gmtoff or
1712 * the %z field in strftime()
1714 char tempbuf[ BUFFER_SIZE ];
1718 #ifdef HAVE_LOCALTIME_R
1719 tm_now = *localtime_r(&now, &tm_now);
1721 pthread_mutex_lock(&localtime_mutex);
1722 tm_now = *localtime (&now);
1723 pthread_mutex_unlock(&localtime_mutex);
1725 tm_now = *localtime (&now);
1727 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1729 /* strlen("set-cookie: ") = 12 */
1730 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1732 #endif /* def FEATURE_COOKIE_JAR */
1734 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1736 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
1737 #ifdef FEATURE_ACTIVITY_CONSOLE
1738 accumulate_stats(STATS_COOKIE, 1);
1739 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1740 err = crumble(csp, header);
1742 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1744 /* Flag whether or not to log a message */
1747 /* A variable to store the tag we're working on */
1750 /* Skip "Set-Cookie:" (11 characters) in header */
1751 cur_tag = *header + 11;
1753 /* skip whitespace between "Set-Cookie:" and value */
1754 while (*cur_tag && ijb_isspace(*cur_tag))
1759 /* Loop through each tag in the cookie */
1763 char *next_tag = strchr(cur_tag, ';');
1764 if (next_tag != NULL)
1766 /* Skip the ';' character itself */
1769 /* skip whitespace ";" and start of tag */
1770 while (*next_tag && ijb_isspace(*next_tag))
1777 /* "Next tag" is the end of the string */
1778 next_tag = cur_tag + strlen(cur_tag);
1781 /* Is this the "Expires" tag? */
1782 if (strncmpic(cur_tag, "expires=", 8) == 0)
1784 /* Delete the tag by copying the rest of the string over it.
1785 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1786 * since the behaviour of strcpy is undefined for overlapping
1789 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1791 /* That changed the header, need to issue a log message */
1794 /* Note that the next tag has now been moved to *cur_tag,
1795 * so we do not need to update the cur_tag pointer.
1800 /* Move on to next cookie tag */
1807 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1815 #ifdef FEATURE_FORCE_LOAD
1816 /*********************************************************************
1818 * Function : strclean
1820 * Description : In-Situ-Eliminate all occurances of substring in
1824 * 1 : string = string to clean
1825 * 2 : substring = substring to eliminate
1827 * Returns : Number of eliminations
1829 *********************************************************************/
1830 int strclean(const char *string, const char *substring)
1832 int hits = 0, len = strlen(substring);
1835 while((pos = strstr(string, substring)) != NULL)
1842 while (*p++ != '\0');
1849 #endif /* def FEATURE_FORCE_LOAD */