1 const char parsers_rcs[] = "$Id: parsers.c,v 1.56.2.10 2006/01/21 16:16:08 david__schmidt 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.10 2006/01/21 16:16:08 david__schmidt
44 * Thanks to Edward Carrel for his patch to modernize OSX's
\rpthreads support. See bug #1409623.
46 * Revision 1.56.2.9 2004/10/03 12:53:45 david__schmidt
47 * Add the ability to check jpeg images for invalid
48 * lengths of comment blocks. Defensive strategy
49 * against the exploit:
50 * Microsoft Security Bulletin MS04-028
51 * Buffer Overrun in JPEG Processing (GDI+) Could
52 * Allow Code Execution (833987)
53 * Enabled with +inspect-jpegs in actions files.
55 * Revision 1.56.2.8 2003/07/11 13:21:25 oes
56 * Excluded text/plain objects from filtering. This fixes a
57 * couple of client-crashing, download corruption and
58 * Privoxy performance issues, whose root cause lies in
59 * web servers labelling content of unknown type as text/plain.
61 * Revision 1.56.2.7 2003/05/06 12:07:26 oes
62 * Fixed bug #729900: Suspicious HOST: headers are now killed and regenerated if necessary
64 * Revision 1.56.2.6 2003/04/14 21:28:30 oes
65 * Completing the previous change
67 * Revision 1.56.2.5 2003/04/14 12:08:16 oes
68 * Added temporary workaround for bug in PHP < 4.2.3
70 * Revision 1.56.2.4 2003/03/07 03:41:05 david__schmidt
71 * Wrapping all *_r functions (the non-_r versions of them) with mutex semaphores for OSX. Hopefully this will take care of all of those pesky crash reports.
73 * Revision 1.56.2.3 2002/11/10 04:20:02 hal9
74 * Fix typo: supressed -> suppressed
76 * Revision 1.56.2.2 2002/09/25 14:59:53 oes
77 * Improved cookie logging
79 * Revision 1.56.2.1 2002/09/25 14:52:45 oes
80 * Added basic support for OPTIONS and TRACE HTTP methods:
81 * - New parser function client_max_forwards which decrements
82 * the Max-Forwards HTTP header field of OPTIONS and TRACE
83 * requests by one before forwarding
84 * - New parser function client_host which extracts the host
85 * and port information from the HTTP header field if the
86 * request URI was not absolute
87 * - Don't crumble and re-add the Host: header, but only generate
88 * and append if missing
90 * Revision 1.56 2002/05/12 15:34:22 jongfoster
91 * Fixing typo in a comment
93 * Revision 1.55 2002/05/08 16:01:07 oes
94 * Optimized add_to_iob:
95 * - Use realloc instead of malloc(), memcpy(), free()
96 * - Expand to powers of two if possible, to get
97 * O(log n) reallocs instead of O(n).
98 * - Moved check for buffer limit here from chat
99 * - Report failure via returncode
101 * Revision 1.54 2002/04/02 15:03:16 oes
102 * Tiny code cosmetics
104 * Revision 1.53 2002/03/26 22:29:55 swa
105 * we have a new homepage!
107 * Revision 1.52 2002/03/24 13:25:43 swa
108 * name change related issues
110 * Revision 1.51 2002/03/13 00:27:05 jongfoster
113 * Revision 1.50 2002/03/12 01:45:35 oes
114 * More verbose logging
116 * Revision 1.49 2002/03/09 20:03:52 jongfoster
117 * - Making various functions return int rather than size_t.
118 * (Undoing a recent change). Since size_t is unsigned on
119 * Windows, functions like read_socket that return -1 on
120 * error cannot return a size_t.
122 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
123 * crashes, and also frequently caused JB to jump to 100%
124 * CPU and stay there. (Because it thought it had just
125 * read ((unsigned)-1) == 4Gb of data...)
127 * - The signature of write_socket has changed, it now simply
128 * returns success=0/failure=nonzero.
130 * - Trying to get rid of a few warnings --with-debug on
131 * Windows, I've introduced a new type "jb_socket". This is
132 * used for the socket file descriptors. On Windows, this
133 * is SOCKET (a typedef for unsigned). Everywhere else, it's
134 * an int. The error value can't be -1 any more, so it's
135 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
136 * Windows it maps to the #define INVALID_SOCKET.)
138 * - The signature of bind_port has changed.
140 * Revision 1.48 2002/03/07 03:46:53 oes
141 * Fixed compiler warnings etc
143 * Revision 1.47 2002/02/20 23:15:13 jongfoster
144 * Parsing functions now handle out-of-memory gracefully by returning
147 * Revision 1.46 2002/01/17 21:03:47 jongfoster
148 * Moving all our URL and URL pattern parsing code to urlmatch.c.
150 * Revision 1.45 2002/01/09 14:33:03 oes
151 * Added support for localtime_r.
153 * Revision 1.44 2001/12/14 01:22:54 steudten
154 * Remove 'user:pass@' from 'proto://user:pass@host' for the
155 * new added header 'Host: ..'. (See Req ID 491818)
157 * Revision 1.43 2001/11/23 00:26:38 jongfoster
158 * Fixing two really stupid errors in my previous commit
160 * Revision 1.42 2001/11/22 21:59:30 jongfoster
161 * Adding code to handle +no-cookies-keep
163 * Revision 1.41 2001/11/05 23:43:05 steudten
164 * Add time+date to log files.
166 * Revision 1.40 2001/10/26 20:13:09 jongfoster
167 * ctype.h is needed in Windows, too.
169 * Revision 1.39 2001/10/26 17:40:04 oes
170 * Introduced get_header_value()
171 * Removed http->user_agent, csp->referrer and csp->accept_types
172 * Removed client_accept()
174 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
175 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
176 * threads to call select() simultaneously. So, it's time to do a real, live,
177 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
178 * (native). Both versions will work, but using __OS2__ offers multi-threading.
180 * Revision 1.37 2001/10/23 21:36:02 jongfoster
181 * Documenting sed()'s error behaviou (doc change only)
183 * Revision 1.36 2001/10/13 12:51:51 joergs
184 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
185 * force-load), instead crumble Host: and add it (again) in client_host_adder
186 * (in case we get a HTTP/1.0 request without Host: header and forward it to
187 * a HTTP/1.1 server/proxy).
189 * Revision 1.35 2001/10/09 22:39:21 jongfoster
190 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
193 * Revision 1.34 2001/10/07 18:50:55 oes
194 * Added server_content_encoding, renamed server_transfer_encoding
196 * Revision 1.33 2001/10/07 18:04:49 oes
197 * Changed server_http11 to server_http and its pattern to "HTTP".
198 * Additional functionality: it now saves the HTTP status into
199 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
201 * Revision 1.32 2001/10/07 15:43:28 oes
202 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
203 * client_te and client_accept_encoding_adder, triggered by the new
204 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
205 * changed to allow only identity and chunked, and the TE header is
206 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
208 * parse_http_request no longer does anything than parsing. The rewriting
209 * of http->cmd and version mangling are gone. It now also recognizes
210 * the put and delete methods and saves the url in http->url. Removed
213 * renamed content_type and content_length to have the server_ prefix
215 * server_content_type now only works if csp->content_type != CT_TABOO
217 * added server_transfer_encoding, which
218 * - Sets CT_TABOO to prohibit filtering if encoding compresses
219 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
220 * - Change from "chunked" to "identity" if body was chunked
221 * but has been de-chunked for filtering.
223 * added server_content_md5 which crunches any Content-MD5 headers
224 * if the body was modified.
226 * made server_http11 conditional on +downgrade action
228 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
230 * Revision 1.31 2001/10/05 14:25:02 oes
231 * Crumble Keep-Alive from Server
233 * Revision 1.30 2001/09/29 12:56:03 joergs
234 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
236 * Revision 1.29 2001/09/24 21:09:24 jongfoster
237 * Fixing 2 memory leaks that Guy spotted, where the paramater to
238 * enlist() was not being free()d.
240 * Revision 1.28 2001/09/22 16:32:28 jongfoster
241 * Removing unused #includes.
243 * Revision 1.27 2001/09/20 15:45:25 steudten
245 * add casting from size_t to int for printf()
246 * remove local variable shadow s2
248 * Revision 1.26 2001/09/16 17:05:14 jongfoster
249 * Removing unused #include showarg.h
251 * Revision 1.25 2001/09/16 13:21:27 jongfoster
252 * Changes to use new list functions.
254 * Revision 1.24 2001/09/13 23:05:50 jongfoster
255 * Changing the string paramater to the header parsers a "const".
257 * Revision 1.23 2001/09/12 18:08:19 steudten
259 * In parse_http_request() header rewriting miss the host value, so
260 * from http://www.mydomain.com the result was just " / " not
261 * http://www.mydomain.com/ in case we forward.
263 * Revision 1.22 2001/09/10 10:58:53 oes
264 * Silenced compiler warnings
266 * Revision 1.21 2001/07/31 14:46:00 oes
267 * - Persistant connections now suppressed
268 * - sed() no longer appends empty header to csp->headers
270 * Revision 1.20 2001/07/30 22:08:36 jongfoster
271 * Tidying up #defines:
272 * - All feature #defines are now of the form FEATURE_xxx
273 * - Permanently turned off WIN_GUI_EDIT
274 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
276 * Revision 1.19 2001/07/25 17:21:54 oes
277 * client_uagent now saves copy of User-Agent: header value
279 * Revision 1.18 2001/07/13 14:02:46 oes
280 * - Included fix to repair broken HTTP requests that
281 * don't contain a path, not even '/'.
282 * - Removed all #ifdef PCRS
283 * - content_type now always inspected and classified as
284 * text, gif or other.
285 * - formatting / comments
287 * Revision 1.17 2001/06/29 21:45:41 oes
288 * Indentation, CRLF->LF, Tab-> Space
290 * Revision 1.16 2001/06/29 13:32:42 oes
292 * - Adapted free_http_request
293 * - Removed logentry from cancelled commit
295 * Revision 1.15 2001/06/03 19:12:38 oes
296 * deleted const struct interceptors
298 * Revision 1.14 2001/06/01 18:49:17 jongfoster
299 * Replaced "list_share" with "list" - the tiny memory gain was not
300 * worth the extra complexity.
302 * Revision 1.13 2001/05/31 21:30:33 jongfoster
303 * Removed list code - it's now in list.[ch]
304 * Renamed "permission" to "action", and changed many features
305 * to use the actions file rather than the global config.
307 * Revision 1.12 2001/05/31 17:33:13 oes
311 * Revision 1.11 2001/05/29 20:11:19 joergs
312 * '/ * inside comment' warning removed.
314 * Revision 1.10 2001/05/29 09:50:24 jongfoster
315 * Unified blocklist/imagelist/permissionslist.
316 * File format is still under discussion, but the internal changes
319 * Also modified interceptor behaviour:
320 * - We now intercept all URLs beginning with one of the following
321 * prefixes (and *only* these prefixes):
323 * * http://ijbswa.sf.net/config/
324 * * http://ijbswa.sourceforge.net/config/
325 * - New interceptors "home page" - go to http://i.j.b/ to see it.
326 * - Internal changes so that intercepted and fast redirect pages
327 * are not replaced with an image.
328 * - Interceptors now have the option to send a binary page direct
329 * to the client. (i.e. ijb-send-banner uses this)
330 * - Implemented show-url-info interceptor. (Which is why I needed
331 * the above interceptors changes - a typical URL is
332 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
333 * The previous mechanism would not have intercepted that, and
334 * if it had been intercepted then it then it would have replaced
337 * Revision 1.9 2001/05/28 17:26:33 jongfoster
338 * Fixing segfault if last header was crunched.
339 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
340 * can use the cross-platform sprintf() instead.)
342 * Revision 1.8 2001/05/27 22:17:04 oes
344 * - re_process_buffer no longer writes the modified buffer
345 * to the client, which was very ugly. It now returns the
346 * buffer, which it is then written by chat.
348 * - content_length now adjusts the Content-Length: header
349 * for modified documents rather than crunch()ing it.
350 * (Length info in csp->content_length, which is 0 for
351 * unmodified documents)
353 * - For this to work, sed() is called twice when filtering.
355 * Revision 1.7 2001/05/27 13:19:06 oes
356 * Patched Joergs solution for the content-length in.
358 * Revision 1.6 2001/05/26 13:39:32 jongfoster
359 * Only crunches Content-Length header if applying RE filtering.
360 * Without this fix, Microsoft Windows Update wouldn't work.
362 * Revision 1.5 2001/05/26 00:28:36 jongfoster
363 * Automatic reloading of config file.
364 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
365 * Most of the global variables have been moved to a new
366 * struct configuration_spec, accessed through csp->config->globalname
367 * Most of the globals remaining are used by the Win32 GUI.
369 * Revision 1.4 2001/05/22 18:46:04 oes
371 * - Enabled filtering banners by size rather than URL
372 * by adding patterns that replace all standard banner
373 * sizes with the "Junkbuster" gif to the re_filterfile
375 * - Enabled filtering WebBugs by providing a pattern
376 * which kills all 1x1 images
378 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
379 * which is selected by the (nonstandard and therefore
380 * capital) letter 'U' in the option string.
381 * It causes the quantifiers to be ungreedy by default.
382 * Appending a ? turns back to greedy (!).
384 * - Added a new interceptor ijb-send-banner, which
385 * sends back the "Junkbuster" gif. Without imagelist or
386 * MSIE detection support, or if tinygif = 1, or the
387 * URL isn't recognized as an imageurl, a lame HTML
388 * explanation is sent instead.
390 * - Added new feature, which permits blocking remote
391 * script redirects and firing back a local redirect
393 * The feature is conditionally compiled, i.e. it
394 * can be disabled with --disable-fast-redirects,
395 * plus it must be activated by a "fast-redirects"
396 * line in the config file, has its own log level
397 * and of course wants to be displayed by show-proxy-args
398 * Note: Boy, all the #ifdefs in 1001 locations and
399 * all the fumbling with configure.in and acconfig.h
400 * were *way* more work than the feature itself :-(
402 * - Because a generic redirect template was needed for
403 * this, tinygif = 3 now uses the same.
405 * - Moved GIFs, and other static HTTP response templates
410 * - Removed some >400 CRs again (Jon, you really worked
413 * Revision 1.3 2001/05/20 01:21:20 jongfoster
414 * Version 2.9.4 checkin.
415 * - Merged popupfile and cookiefile, and added control over PCRS
416 * filtering, in new "permissionsfile".
417 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
418 * file error you now get a message box (in the Win32 GUI) rather
419 * than the program exiting with no explanation.
420 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
422 * - Removed tabs from "config"
423 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
424 * - Bumped up version number.
426 * Revision 1.2 2001/05/17 23:02:36 oes
427 * - Made referrer option accept 'L' as a substitute for '§'
429 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
430 * Initial import of version 2.9.3 source tree
433 *********************************************************************/
440 #include <sys/types.h>
448 #if !defined(_WIN32) && !defined(__OS2__)
457 /* jcc.h is for mutex semapores only */
458 #endif /* def OSX_DARWIN */
464 #include "jbsockets.h"
465 #include "miscutil.h"
468 const char parsers_h_rcs[] = PARSERS_H_VERSION;
470 /* Fix a problem with Solaris. There should be no effect on other
472 * Solaris's isspace() is a macro which uses it's argument directly
473 * as an array index. Therefore we need to make sure that high-bit
474 * characters generate +ve values, and ideally we also want to make
475 * the argument match the declared parameter type of "int".
477 * Why did they write a character function that can't take a simple
478 * "char" argument? Doh!
480 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
481 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
484 const struct parsers client_patterns[] = {
485 { "referer:", 8, client_referrer },
486 { "user-agent:", 11, client_uagent },
487 { "ua-", 3, client_ua },
488 { "from:", 5, client_from },
489 { "cookie:", 7, client_send_cookie },
490 { "x-forwarded-for:", 16, client_x_forwarded },
491 { "Accept-Encoding:", 16, client_accept_encoding },
492 { "TE:", 3, client_te },
493 { "Host:", 5, client_host },
494 /* { "if-modified-since:", 18, crumble }, */
495 { "Keep-Alive:", 11, crumble },
496 { "connection:", 11, crumble },
497 { "proxy-connection:", 17, crumble },
498 { "max-forwards:", 13, client_max_forwards },
503 const struct parsers server_patterns[] = {
504 { "HTTP", 4, server_http },
505 { "set-cookie:", 11, server_set_cookie },
506 { "connection:", 11, crumble },
507 { "Content-Type:", 13, server_content_type },
508 { "Content-Length:", 15, server_content_length },
509 { "Content-MD5:", 12, server_content_md5 },
510 { "Content-Encoding:", 17, server_content_encoding },
511 { "Transfer-Encoding:", 18, server_transfer_coding },
512 { "Keep-Alive:", 11, crumble },
517 const add_header_func_ptr add_client_headers[] = {
520 client_x_forwarded_adder,
522 /* Temporarily disabled: client_accept_encoding_adder, */
523 connection_close_adder,
528 const add_header_func_ptr add_server_headers[] = {
529 connection_close_adder,
534 /*********************************************************************
536 * Function : flush_socket
538 * Description : Write any pending "buffered" content.
541 * 1 : fd = file descriptor of the socket to read
542 * 2 : csp = Current client state (buffers, headers, etc...)
544 * Returns : On success, the number of bytes written are returned (zero
545 * indicates nothing was written). On error, -1 is returned,
546 * and errno is set appropriately. If count is zero and the
547 * file descriptor refers to a regular file, 0 will be
548 * returned without causing any other effect. For a special
549 * file, the results are not portable.
551 *********************************************************************/
552 int flush_socket(jb_socket fd, struct client_state *csp)
554 struct iob *iob = csp->iob;
555 int len = iob->eod - iob->cur;
562 if (write_socket(fd, iob->cur, (size_t)len))
566 iob->eod = iob->cur = iob->buf;
572 /*********************************************************************
574 * Function : add_to_iob
576 * Description : Add content to the buffered page, expanding the
577 * buffer if necessary.
580 * 1 : csp = Current client state (buffers, headers, etc...)
581 * 2 : buf = holds the content to be added to the page
582 * 3 : n = number of bytes to be added
584 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
585 * or buffer limit reached.
587 *********************************************************************/
588 jb_err add_to_iob(struct client_state *csp, char *buf, int n)
590 struct iob *iob = csp->iob;
591 size_t used, offset, need, want;
594 if (n <= 0) return JB_ERR_OK;
596 used = iob->eod - iob->buf;
597 offset = iob->cur - iob->buf;
601 * If the buffer can't hold the new data, extend it first.
602 * Use the next power of two if possible, else use the actual need.
604 if (need > csp->config->buffer_limit)
606 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
607 return JB_ERR_MEMORY;
610 if (need > iob->size)
612 for (want = csp->iob->size ? csp->iob->size : 512; want <= need;) want *= 2;
614 if (want <= csp->config->buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
618 else if (NULL != (p = (char *)realloc(iob->buf, need)))
624 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
625 return JB_ERR_MEMORY;
628 /* Update the iob pointers */
629 iob->cur = p + offset;
634 /* copy the new data into the iob buffer */
635 memcpy(iob->eod, buf, (size_t)n);
637 /* point to the end of the data */
640 /* null terminate == cheap insurance */
648 /*********************************************************************
650 * Function : get_header
652 * Description : This (odd) routine will parse the csp->iob
655 * 1 : csp = Current client state (buffers, headers, etc...)
657 * Returns : Any one of the following:
659 * 1) a pointer to a dynamically allocated string that contains a header line
660 * 2) NULL indicating that the end of the header was reached
661 * 3) "" indicating that the end of the iob was reached before finding
662 * a complete header line.
664 *********************************************************************/
665 char *get_header(struct client_state *csp)
671 if ((iob->cur == NULL)
672 || ((p = strchr(iob->cur, '\n')) == NULL))
674 return(""); /* couldn't find a complete header */
679 ret = strdup(iob->cur);
682 /* FIXME No way to handle error properly */
683 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
688 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
690 /* is this a blank line (i.e. the end of the header) ? */
702 /*********************************************************************
704 * Function : get_header_value
706 * Description : Get the value of a given header from a chained list
707 * of header lines or return NULL if no such header is
708 * present in the list.
711 * 1 : header_list = pointer to list
712 * 2 : header_name = string with name of header to look for.
713 * Trailing colon required, capitalization
716 * Returns : NULL if not found, else value of header
718 *********************************************************************/
719 char *get_header_value(const struct list *header_list, const char *header_name)
721 struct list_entry *cur_entry;
727 length = strlen(header_name);
729 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
733 if (!strncmpic(cur_entry->str, header_name, length))
736 * Found: return pointer to start of value
738 ret = (char *) (cur_entry->str + length);
739 while (*ret && ijb_isspace(*ret)) ret++;
752 /*********************************************************************
756 * Description : add, delete or modify lines in the HTTP header streams.
757 * On entry, it receives a linked list of headers space
758 * that was allocated dynamically (both the list nodes
759 * and the header contents).
761 * As a side effect it frees the space used by the original
765 * 1 : pats = list of patterns to match against headers
766 * 2 : more_headers = list of functions to add more
767 * headers (client or server)
768 * 3 : csp = Current client state (buffers, headers, etc...)
770 * Returns : Single pointer to a fully formed header, or NULL
771 * on out-of-memory error.
773 *********************************************************************/
774 char *sed(const struct parsers pats[],
775 const add_header_func_ptr more_headers[],
776 struct client_state *csp)
778 struct list_entry *p;
779 const struct parsers *v;
780 const add_header_func_ptr *f;
781 jb_err err = JB_ERR_OK;
783 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
785 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
787 /* Header crunch()ed in previous run? -> ignore */
788 if (p->str == NULL) continue;
790 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
792 if (strncmpic(p->str, v->str, v->len) == 0)
794 err = v->parser(csp, (char **)&(p->str));
799 /* place any additional headers on the csp->headers list */
800 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
805 if (err != JB_ERR_OK)
810 return list_to_text(csp->headers);
814 /* here begins the family of parser functions that reformat header lines */
817 /*********************************************************************
821 * Description : This is called if a header matches a pattern to "crunch"
824 * 1 : csp = Current client state (buffers, headers, etc...)
825 * 2 : header = On input, pointer to header to modify.
826 * On output, pointer to the modified header, or NULL
827 * to remove the header. This function frees the
828 * original string if necessary.
830 * Returns : JB_ERR_OK on success, or
831 * JB_ERR_MEMORY on out-of-memory error.
833 *********************************************************************/
834 jb_err crumble(struct client_state *csp, char **header)
836 log_error(LOG_LEVEL_HEADER, "crunch!");
842 /*********************************************************************
844 * Function : server_content_type
846 * Description : Set the content-type for filterable types (text/.*,
847 * javascript and image/gif) unless filtering has been
848 * forbidden (CT_TABOO) while parsing earlier headers.
849 * NOTE: Since text/plain is commonly used by web servers
850 * for files whose correct type is unknown, we don't
851 * set CT_TEXT for it.
854 * 1 : csp = Current client state (buffers, headers, etc...)
855 * 2 : header = On input, pointer to header to modify.
856 * On output, pointer to the modified header, or NULL
857 * to remove the header. This function frees the
858 * original string if necessary.
860 * Returns : JB_ERR_OK on success, or
861 * JB_ERR_MEMORY on out-of-memory error.
863 *********************************************************************/
864 jb_err server_content_type(struct client_state *csp, char **header)
866 if (csp->content_type != CT_TABOO)
868 if ((strstr(*header, " text/") && !strstr(*header, "plain"))
869 || strstr(*header, "application/x-javascript"))
870 csp->content_type = CT_TEXT;
871 else if (strstr(*header, " image/gif"))
872 csp->content_type = CT_GIF;
873 else if (strstr(*header, " image/jpeg"))
874 csp->content_type = CT_JPEG;
876 csp->content_type = 0;
883 /*********************************************************************
885 * Function : server_transfer_coding
887 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
888 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
889 * - Change from "chunked" to "identity" if body was chunked
890 * but has been de-chunked for filtering.
893 * 1 : csp = Current client state (buffers, headers, etc...)
894 * 2 : header = On input, pointer to header to modify.
895 * On output, pointer to the modified header, or NULL
896 * to remove the header. This function frees the
897 * original string if necessary.
899 * Returns : JB_ERR_OK on success, or
900 * JB_ERR_MEMORY on out-of-memory error.
902 *********************************************************************/
903 jb_err server_transfer_coding(struct client_state *csp, char **header)
906 * Turn off pcrs and gif filtering if body compressed
908 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
910 csp->content_type = CT_TABOO;
914 * Raise flag if body chunked
916 if (strstr(*header, "chunked"))
918 csp->flags |= CSP_FLAG_CHUNKED;
921 * If the body was modified, it has been
922 * de-chunked first, so adjust the header:
924 if (csp->flags & CSP_FLAG_MODIFIED)
927 *header = strdup("Transfer-Encoding: identity");
928 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
936 /*********************************************************************
938 * Function : server_content_encoding
940 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
943 * 1 : csp = Current client state (buffers, headers, etc...)
944 * 2 : header = On input, pointer to header to modify.
945 * On output, pointer to the modified header, or NULL
946 * to remove the header. This function frees the
947 * original string if necessary.
949 * Returns : JB_ERR_OK on success, or
950 * JB_ERR_MEMORY on out-of-memory error.
952 *********************************************************************/
953 jb_err server_content_encoding(struct client_state *csp, char **header)
956 * Turn off pcrs and gif filtering if body compressed
958 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
960 csp->content_type = CT_TABOO;
968 /*********************************************************************
970 * Function : server_content_length
972 * Description : Adjust Content-Length header if we modified
976 * 1 : csp = Current client state (buffers, headers, etc...)
977 * 2 : header = On input, pointer to header to modify.
978 * On output, pointer to the modified header, or NULL
979 * to remove the header. This function frees the
980 * original string if necessary.
982 * Returns : JB_ERR_OK on success, or
983 * JB_ERR_MEMORY on out-of-memory error.
985 *********************************************************************/
986 jb_err server_content_length(struct client_state *csp, char **header)
988 if (csp->content_length != 0) /* Content length has been modified */
991 *header = (char *) zalloc(100);
994 return JB_ERR_MEMORY;
997 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
999 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1006 /*********************************************************************
1008 * Function : server_content_md5
1010 * Description : Crumble any Content-MD5 headers if the document was
1011 * modified. FIXME: Should we re-compute instead?
1014 * 1 : csp = Current client state (buffers, headers, etc...)
1015 * 2 : header = On input, pointer to header to modify.
1016 * On output, pointer to the modified header, or NULL
1017 * to remove the header. This function frees the
1018 * original string if necessary.
1020 * Returns : JB_ERR_OK on success, or
1021 * JB_ERR_MEMORY on out-of-memory error.
1023 *********************************************************************/
1024 jb_err server_content_md5(struct client_state *csp, char **header)
1026 if (csp->flags & CSP_FLAG_MODIFIED)
1028 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1036 /*********************************************************************
1038 * Function : client_accept_encoding
1040 * Description : Rewrite the client's Accept-Encoding header so that
1041 * if doesn't allow compression, if the action applies.
1042 * Note: For HTTP/1.0 the absence of the header is enough.
1045 * 1 : csp = Current client state (buffers, headers, etc...)
1046 * 2 : header = On input, pointer to header to modify.
1047 * On output, pointer to the modified header, or NULL
1048 * to remove the header. This function frees the
1049 * original string if necessary.
1051 * Returns : JB_ERR_OK on success, or
1052 * JB_ERR_MEMORY on out-of-memory error.
1054 *********************************************************************/
1055 jb_err client_accept_encoding(struct client_state *csp, char **header)
1057 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1059 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
1063 /* Temporarily disable the correct behaviour to
1064 * work around a PHP bug.
1066 * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1068 * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
1069 * if (*header == NULL)
1071 * return JB_ERR_MEMORY;
1082 /*********************************************************************
1084 * Function : client_te
1086 * Description : Rewrite the client's TE header so that
1087 * if doesn't allow compression, if the action applies.
1090 * 1 : csp = Current client state (buffers, headers, etc...)
1091 * 2 : header = On input, pointer to header to modify.
1092 * On output, pointer to the modified header, or NULL
1093 * to remove the header. This function frees the
1094 * original string if necessary.
1096 * Returns : JB_ERR_OK on success, or
1097 * JB_ERR_MEMORY on out-of-memory error.
1099 *********************************************************************/
1100 jb_err client_te(struct client_state *csp, char **header)
1102 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1105 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
1111 /*********************************************************************
1113 * Function : client_referrer
1115 * Description : Handle the "referer" config setting properly.
1116 * Called from `sed'.
1119 * 1 : csp = Current client state (buffers, headers, etc...)
1120 * 2 : header = On input, pointer to header to modify.
1121 * On output, pointer to the modified header, or NULL
1122 * to remove the header. This function frees the
1123 * original string if necessary.
1125 * Returns : JB_ERR_OK on success, or
1126 * JB_ERR_MEMORY on out-of-memory error.
1128 *********************************************************************/
1129 jb_err client_referrer(struct client_state *csp, char **header)
1133 #ifdef FEATURE_FORCE_LOAD
1134 /* Since the referrer can include the prefix even
1135 * even if the request itself is non-forced, we must
1136 * clean it unconditionally
1138 strclean(*header, FORCE_PREFIX);
1139 #endif /* def FEATURE_FORCE_LOAD */
1142 * Are we sending referer?
1144 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1151 newval = csp->action->string[ACTION_STRING_REFERER];
1153 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1158 log_error(LOG_LEVEL_HEADER, "crunch!");
1161 else if (0 == strncmpic(newval, "http://", 7))
1164 * We have a specific (fixed) referer we want to send.
1166 log_error(LOG_LEVEL_HEADER, "modified");
1168 *header = strdup("Referer: ");
1169 string_append(header, newval);
1171 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1176 * Forge a referer as http://[hostname:port of REQUEST]/
1177 * to fool stupid checks for in-site links
1179 if (0 != strcmpic(newval, "forge"))
1182 * Invalid choice - but forge is probably the best default.
1184 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1187 *header = strdup("Referer: http://");
1188 string_append(header, csp->http->hostport);
1189 string_append(header, "/");
1190 log_error(LOG_LEVEL_HEADER, "crunch+forge to %s", *header);
1192 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1197 /*********************************************************************
1199 * Function : client_uagent
1201 * Description : Handle the "user-agent" config setting properly
1202 * and remember its original value to enable browser
1203 * bug workarounds. Called from `sed'.
1206 * 1 : csp = Current client state (buffers, headers, etc...)
1207 * 2 : header = On input, pointer to header to modify.
1208 * On output, pointer to the modified header, or NULL
1209 * to remove the header. This function frees the
1210 * original string if necessary.
1212 * Returns : JB_ERR_OK on success, or
1213 * JB_ERR_MEMORY on out-of-memory error.
1215 *********************************************************************/
1216 jb_err client_uagent(struct client_state *csp, char **header)
1220 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1225 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1231 log_error(LOG_LEVEL_HEADER, "modified");
1234 *header = strdup("User-Agent: ");
1235 string_append(header, newval);
1237 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1241 /*********************************************************************
1243 * Function : client_ua
1245 * Description : Handle "ua-" headers properly. Called from `sed'.
1248 * 1 : csp = Current client state (buffers, headers, etc...)
1249 * 2 : header = On input, pointer to header to modify.
1250 * On output, pointer to the modified header, or NULL
1251 * to remove the header. This function frees the
1252 * original string if necessary.
1254 * Returns : JB_ERR_OK on success, or
1255 * JB_ERR_MEMORY on out-of-memory error.
1257 *********************************************************************/
1258 jb_err client_ua(struct client_state *csp, char **header)
1260 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1262 log_error(LOG_LEVEL_HEADER, "crunch!");
1270 /*********************************************************************
1272 * Function : client_from
1274 * Description : Handle the "from" config setting properly.
1275 * Called from `sed'.
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_from(struct client_state *csp, char **header)
1292 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1299 newval = csp->action->string[ACTION_STRING_FROM];
1302 * Are we blocking the e-mail address?
1304 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1306 log_error(LOG_LEVEL_HEADER, "crunch!");
1310 log_error(LOG_LEVEL_HEADER, " modified");
1312 *header = strdup("From: ");
1313 string_append(header, newval);
1315 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1319 /*********************************************************************
1321 * Function : client_send_cookie
1323 * Description : Handle the "cookie" header properly. Called from `sed'.
1324 * If cookie is accepted, add it to the cookie_list,
1325 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1328 * 1 : csp = Current client state (buffers, headers, etc...)
1329 * 2 : header = On input, pointer to header to modify.
1330 * On output, pointer to the modified header, or NULL
1331 * to remove the header. This function frees the
1332 * original string if necessary.
1334 * Returns : JB_ERR_OK on success, or
1335 * JB_ERR_MEMORY on out-of-memory error.
1337 *********************************************************************/
1338 jb_err client_send_cookie(struct client_state *csp, char **header)
1340 jb_err result = JB_ERR_OK;
1342 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1344 /* strlen("cookie: ") == 8 */
1345 result = enlist(csp->cookie_list, *header + 8);
1349 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
1353 * Always remove the cookie here. The cookie header
1354 * will be sent at the end of the header.
1362 /*********************************************************************
1364 * Function : client_x_forwarded
1366 * Description : Handle the "x-forwarded-for" config setting properly,
1367 * also used in the add_client_headers list. Called from `sed'.
1370 * 1 : csp = Current client state (buffers, headers, etc...)
1371 * 2 : header = On input, pointer to header to modify.
1372 * On output, pointer to the modified header, or NULL
1373 * to remove the header. This function frees the
1374 * original string if necessary.
1376 * Returns : JB_ERR_OK on success, or
1377 * JB_ERR_MEMORY on out-of-memory error.
1379 *********************************************************************/
1380 jb_err client_x_forwarded(struct client_state *csp, char **header)
1382 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1384 /* Save it so we can re-add it later */
1385 freez(csp->x_forwarded);
1386 csp->x_forwarded = *header;
1389 * Always set *header = NULL, since this information
1390 * will be sent at the end of the header.
1397 log_error(LOG_LEVEL_HEADER, " crunch!");
1404 /*********************************************************************
1406 * Function : client_max_forwards
1408 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
1409 * from the value of the Max-Forwards header field.
1412 * 1 : csp = Current client state (buffers, headers, etc...)
1413 * 2 : header = On input, pointer to header to modify.
1414 * On output, pointer to the modified header, or NULL
1415 * to remove the header. This function frees the
1416 * original string if necessary.
1418 * Returns : JB_ERR_OK on success, or
1419 * JB_ERR_MEMORY on out-of-memory error.
1421 *********************************************************************/
1422 jb_err client_max_forwards(struct client_state *csp, char **header)
1424 unsigned int max_forwards;
1426 if ((0 == strcmpic(csp->http->gpc, "trace"))
1427 || (0 == strcmpic(csp->http->gpc, "options")))
1429 if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
1431 if (max_forwards-- >= 1)
1433 sprintf(*header, "Max-Forwards: %u", max_forwards);
1434 log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
1438 log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
1447 /*********************************************************************
1449 * Function : client_host
1451 * Description : If the request URI did not contain host and
1452 * port information, parse and evaluate the Host
1455 * Also, kill ill-formed HOST: headers as sent by
1456 * Apple's iTunes software when used with a proxy.
1459 * 1 : csp = Current client state (buffers, headers, etc...)
1460 * 2 : header = On input, pointer to header to modify.
1461 * On output, pointer to the modified header, or NULL
1462 * to remove the header. This function frees the
1463 * original string if necessary.
1465 * Returns : JB_ERR_OK on success, or
1466 * JB_ERR_MEMORY on out-of-memory error.
1468 *********************************************************************/
1469 jb_err client_host(struct client_state *csp, char **header)
1474 * If the header field name is all upper-case, chances are that it's
1475 * an ill-formed one from iTunes. BTW, killing innocent headers here is
1476 * not a problem -- they are regenerated later.
1478 if ((*header)[1] == 'O')
1480 log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
1485 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
1486 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
1489 if (NULL == (p = strdup((*header)+6)))
1491 return JB_ERR_MEMORY;
1494 if (NULL == (q = strdup(p)))
1497 return JB_ERR_MEMORY;
1500 freez(csp->http->hostport);
1501 csp->http->hostport = p;
1502 freez(csp->http->host);
1503 csp->http->host = q;
1504 q = strchr(csp->http->host, ':');
1507 /* Terminate hostname and evaluate port string */
1509 csp->http->port = atoi(q);
1513 csp->http->port = csp->http->ssl ? 443 : 80;
1516 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
1517 csp->http->hostport, csp->http->host, csp->http->port);
1524 /* the following functions add headers directly to the header list */
1526 /*********************************************************************
1528 * Function : client_host_adder
1530 * Description : Adds the Host: header field if it is missing.
1531 * Called from `sed'.
1534 * 1 : csp = Current client state (buffers, headers, etc...)
1536 * Returns : JB_ERR_OK on success, or
1537 * JB_ERR_MEMORY on out-of-memory error.
1539 *********************************************************************/
1540 jb_err client_host_adder(struct client_state *csp)
1545 if ( !csp->http->hostport || !*(csp->http->hostport))
1551 * remove 'user:pass@' from 'proto://user:pass@host'
1553 if ( (p = strchr( csp->http->hostport, '@')) != NULL )
1559 p = csp->http->hostport;
1562 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
1563 err = enlist_unique_header(csp->headers, "Host", p);
1569 /*********************************************************************
1571 * Function : client_cookie_adder
1573 * Description : Used in the add_client_headers list. Called from `sed'.
1576 * 1 : csp = Current client state (buffers, headers, etc...)
1578 * Returns : JB_ERR_OK on success, or
1579 * JB_ERR_MEMORY on out-of-memory error.
1581 *********************************************************************/
1582 jb_err client_cookie_adder(struct client_state *csp)
1584 struct list_entry *lst;
1586 struct list_entry *list1 = csp->cookie_list->first;
1587 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1588 int first_cookie = 1;
1591 if ((list1 == NULL) && (list2 == NULL))
1597 tmp = strdup("Cookie: ");
1599 for (lst = list1; lst ; lst = lst->next)
1607 string_append(&tmp, "; ");
1609 string_append(&tmp, lst->str);
1612 for (lst = list2; lst ; lst = lst->next)
1620 string_append(&tmp, "; ");
1622 string_join(&tmp, cookie_encode(lst->str));
1627 return JB_ERR_MEMORY;
1630 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1631 err = enlist(csp->headers, tmp);
1637 /*********************************************************************
1639 * Function : client_accept_encoding_adder
1641 * Description : Add an Accept-Encoding header to the client's request
1642 * that disables compression if the action applies, and
1643 * the header is not already there. Called from `sed'.
1644 * Note: For HTTP/1.0, the absence of the header is enough.
1647 * 1 : csp = Current client state (buffers, headers, etc...)
1649 * Returns : JB_ERR_OK on success, or
1650 * JB_ERR_MEMORY on out-of-memory error.
1652 *********************************************************************/
1653 jb_err client_accept_encoding_adder(struct client_state *csp)
1655 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1656 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1658 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1665 /*********************************************************************
1667 * Function : client_xtra_adder
1669 * Description : Used in the add_client_headers list. Called from `sed'.
1672 * 1 : csp = Current client state (buffers, headers, etc...)
1674 * Returns : JB_ERR_OK on success, or
1675 * JB_ERR_MEMORY on out-of-memory error.
1677 *********************************************************************/
1678 jb_err client_xtra_adder(struct client_state *csp)
1680 struct list_entry *lst;
1683 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1684 lst ; lst = lst->next)
1686 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1687 err = enlist(csp->headers, lst->str);
1699 /*********************************************************************
1701 * Function : client_x_forwarded_adder
1703 * Description : Used in the add_client_headers list. Called from `sed'.
1706 * 1 : csp = Current client state (buffers, headers, etc...)
1708 * Returns : JB_ERR_OK on success, or
1709 * JB_ERR_MEMORY on out-of-memory error.
1711 *********************************************************************/
1712 jb_err client_x_forwarded_adder(struct client_state *csp)
1717 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1722 if (csp->x_forwarded)
1724 p = strdup(csp->x_forwarded);
1725 string_append(&p, ", ");
1729 p = strdup("X-Forwarded-For: ");
1731 string_append(&p, csp->ip_addr_str);
1735 return JB_ERR_MEMORY;
1738 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1739 err = enlist(csp->headers, p);
1746 /*********************************************************************
1748 * Function : connection_close_adder
1750 * Description : Adds a "Connection: close" header to csp->headers
1751 * as a temporary fix for the needed but missing HTTP/1.1
1752 * support. Called from `sed'.
1753 * FIXME: This whole function shouldn't be neccessary!
1756 * 1 : csp = Current client state (buffers, headers, etc...)
1758 * Returns : JB_ERR_OK on success, or
1759 * JB_ERR_MEMORY on out-of-memory error.
1761 *********************************************************************/
1762 jb_err connection_close_adder(struct client_state *csp)
1764 return enlist(csp->headers, "Connection: close");
1768 /*********************************************************************
1770 * Function : server_http
1772 * Description : - Save the HTTP Status into csp->http->status
1773 * - Set CT_TABOO to prevent filtering if the answer
1774 * is a partial range (HTTP status 206)
1775 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1779 * 1 : csp = Current client state (buffers, headers, etc...)
1780 * 2 : header = On input, pointer to header to modify.
1781 * On output, pointer to the modified header, or NULL
1782 * to remove the header. This function frees the
1783 * original string if necessary.
1785 * Returns : JB_ERR_OK on success, or
1786 * JB_ERR_MEMORY on out-of-memory error.
1788 *********************************************************************/
1789 jb_err server_http(struct client_state *csp, char **header)
1791 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1792 if (csp->http->status == 206)
1794 csp->content_type = CT_TABOO;
1797 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1800 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1807 /*********************************************************************
1809 * Function : server_set_cookie
1811 * Description : Handle the server "cookie" header properly.
1812 * Log cookie to the jar file. Then "crunch" it,
1813 * or accept it. Called from `sed'.
1816 * 1 : csp = Current client state (buffers, headers, etc...)
1817 * 2 : header = On input, pointer to header to modify.
1818 * On output, pointer to the modified header, or NULL
1819 * to remove the header. This function frees the
1820 * original string if necessary.
1822 * Returns : JB_ERR_OK on success, or
1823 * JB_ERR_MEMORY on out-of-memory error.
1825 *********************************************************************/
1826 jb_err server_set_cookie(struct client_state *csp, char **header)
1828 #ifdef FEATURE_COOKIE_JAR
1829 if (csp->config->jar)
1832 * Write timestamp into outbuf.
1834 * Complex because not all OSs have tm_gmtoff or
1835 * the %z field in strftime()
1837 char tempbuf[ BUFFER_SIZE ];
1841 #ifdef HAVE_LOCALTIME_R
1842 tm_now = *localtime_r(&now, &tm_now);
1844 pthread_mutex_lock(&localtime_mutex);
1845 tm_now = *localtime (&now);
1846 pthread_mutex_unlock(&localtime_mutex);
1848 tm_now = *localtime (&now);
1850 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1852 /* strlen("set-cookie: ") = 12 */
1853 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1855 #endif /* def FEATURE_COOKIE_JAR */
1857 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1859 log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
1860 return crumble(csp, header);
1862 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1864 /* Flag whether or not to log a message */
1867 /* A variable to store the tag we're working on */
1870 /* Skip "Set-Cookie:" (11 characters) in header */
1871 cur_tag = *header + 11;
1873 /* skip whitespace between "Set-Cookie:" and value */
1874 while (*cur_tag && ijb_isspace(*cur_tag))
1879 /* Loop through each tag in the cookie */
1883 char *next_tag = strchr(cur_tag, ';');
1884 if (next_tag != NULL)
1886 /* Skip the ';' character itself */
1889 /* skip whitespace ";" and start of tag */
1890 while (*next_tag && ijb_isspace(*next_tag))
1897 /* "Next tag" is the end of the string */
1898 next_tag = cur_tag + strlen(cur_tag);
1901 /* Is this the "Expires" tag? */
1902 if (strncmpic(cur_tag, "expires=", 8) == 0)
1904 /* Delete the tag by copying the rest of the string over it.
1905 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1906 * since the behaviour of strcpy is undefined for overlapping
1909 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1911 /* That changed the header, need to issue a log message */
1914 /* Note that the next tag has now been moved to *cur_tag,
1915 * so we do not need to update the cur_tag pointer.
1920 /* Move on to next cookie tag */
1927 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1935 #ifdef FEATURE_FORCE_LOAD
1936 /*********************************************************************
1938 * Function : strclean
1940 * Description : In-Situ-Eliminate all occurances of substring in
1944 * 1 : string = string to clean
1945 * 2 : substring = substring to eliminate
1947 * Returns : Number of eliminations
1949 *********************************************************************/
1950 int strclean(const char *string, const char *substring)
1952 int hits = 0, len = strlen(substring);
1955 while((pos = strstr(string, substring)) != NULL)
1962 while (*p++ != '\0');
1969 #endif /* def FEATURE_FORCE_LOAD */