1 const char parsers_rcs[] = "$Id: parsers.c,v 1.53 2002/03/26 22:29:55 swa Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `add_to_iob', `client_cookie_adder', `client_from',
9 * `client_referrer', `client_send_cookie', `client_ua',
10 * `client_uagent', `client_x_forwarded',
11 * `client_x_forwarded_adder', `client_xtra_adder',
12 * `content_type', `crumble', `destroy_list', `enlist',
13 * `flush_socket', ``get_header', `sed',
14 * and `server_set_cookie'.
16 * Copyright : Written by and Copyright (C) 2001 the SourceForge
17 * Privoxy team. http://www.privoxy.org/
19 * Based on the Internet Junkbuster originally written
20 * by and Copyright (C) 1997 Anonymous Coders and
21 * Junkbusters Corporation. http://www.junkbusters.com
23 * This program is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General
25 * Public License as published by the Free Software
26 * Foundation; either version 2 of the License, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will
30 * be useful, but WITHOUT ANY WARRANTY; without even the
31 * implied warranty of MERCHANTABILITY or FITNESS FOR A
32 * PARTICULAR PURPOSE. See the GNU General Public
33 * License for more details.
35 * The GNU General Public License should be included with
36 * this file. If not, you can view it at
37 * http://www.gnu.org/copyleft/gpl.html
38 * or write to the Free Software Foundation, Inc., 59
39 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 * Revision 1.53 2002/03/26 22:29:55 swa
44 * we have a new homepage!
46 * Revision 1.52 2002/03/24 13:25:43 swa
47 * name change related issues
49 * Revision 1.51 2002/03/13 00:27:05 jongfoster
52 * Revision 1.50 2002/03/12 01:45:35 oes
53 * More verbose logging
55 * Revision 1.49 2002/03/09 20:03:52 jongfoster
56 * - Making various functions return int rather than size_t.
57 * (Undoing a recent change). Since size_t is unsigned on
58 * Windows, functions like read_socket that return -1 on
59 * error cannot return a size_t.
61 * THIS WAS A MAJOR BUG - it caused frequent, unpredictable
62 * crashes, and also frequently caused JB to jump to 100%
63 * CPU and stay there. (Because it thought it had just
64 * read ((unsigned)-1) == 4Gb of data...)
66 * - The signature of write_socket has changed, it now simply
67 * returns success=0/failure=nonzero.
69 * - Trying to get rid of a few warnings --with-debug on
70 * Windows, I've introduced a new type "jb_socket". This is
71 * used for the socket file descriptors. On Windows, this
72 * is SOCKET (a typedef for unsigned). Everywhere else, it's
73 * an int. The error value can't be -1 any more, so it's
74 * now JB_INVALID_SOCKET (which is -1 on UNIX, and in
75 * Windows it maps to the #define INVALID_SOCKET.)
77 * - The signature of bind_port has changed.
79 * Revision 1.48 2002/03/07 03:46:53 oes
80 * Fixed compiler warnings etc
82 * Revision 1.47 2002/02/20 23:15:13 jongfoster
83 * Parsing functions now handle out-of-memory gracefully by returning
86 * Revision 1.46 2002/01/17 21:03:47 jongfoster
87 * Moving all our URL and URL pattern parsing code to urlmatch.c.
89 * Revision 1.45 2002/01/09 14:33:03 oes
90 * Added support for localtime_r.
92 * Revision 1.44 2001/12/14 01:22:54 steudten
93 * Remove 'user:pass@' from 'proto://user:pass@host' for the
94 * new added header 'Host: ..'. (See Req ID 491818)
96 * Revision 1.43 2001/11/23 00:26:38 jongfoster
97 * Fixing two really stupid errors in my previous commit
99 * Revision 1.42 2001/11/22 21:59:30 jongfoster
100 * Adding code to handle +no-cookies-keep
102 * Revision 1.41 2001/11/05 23:43:05 steudten
103 * Add time+date to log files.
105 * Revision 1.40 2001/10/26 20:13:09 jongfoster
106 * ctype.h is needed in Windows, too.
108 * Revision 1.39 2001/10/26 17:40:04 oes
109 * Introduced get_header_value()
110 * Removed http->user_agent, csp->referrer and csp->accept_types
111 * Removed client_accept()
113 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
114 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
115 * threads to call select() simultaneously. So, it's time to do a real, live,
116 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
117 * (native). Both versions will work, but using __OS2__ offers multi-threading.
119 * Revision 1.37 2001/10/23 21:36:02 jongfoster
120 * Documenting sed()'s error behaviou (doc change only)
122 * Revision 1.36 2001/10/13 12:51:51 joergs
123 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
124 * force-load), instead crumble Host: and add it (again) in client_host_adder
125 * (in case we get a HTTP/1.0 request without Host: header and forward it to
126 * a HTTP/1.1 server/proxy).
128 * Revision 1.35 2001/10/09 22:39:21 jongfoster
129 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
132 * Revision 1.34 2001/10/07 18:50:55 oes
133 * Added server_content_encoding, renamed server_transfer_encoding
135 * Revision 1.33 2001/10/07 18:04:49 oes
136 * Changed server_http11 to server_http and its pattern to "HTTP".
137 * Additional functionality: it now saves the HTTP status into
138 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
140 * Revision 1.32 2001/10/07 15:43:28 oes
141 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
142 * client_te and client_accept_encoding_adder, triggered by the new
143 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
144 * changed to allow only identity and chunked, and the TE header is
145 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
147 * parse_http_request no longer does anything than parsing. The rewriting
148 * of http->cmd and version mangling are gone. It now also recognizes
149 * the put and delete methods and saves the url in http->url. Removed
152 * renamed content_type and content_length to have the server_ prefix
154 * server_content_type now only works if csp->content_type != CT_TABOO
156 * added server_transfer_encoding, which
157 * - Sets CT_TABOO to prohibit filtering if encoding compresses
158 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
159 * - Change from "chunked" to "identity" if body was chunked
160 * but has been de-chunked for filtering.
162 * added server_content_md5 which crunches any Content-MD5 headers
163 * if the body was modified.
165 * made server_http11 conditional on +downgrade action
167 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
169 * Revision 1.31 2001/10/05 14:25:02 oes
170 * Crumble Keep-Alive from Server
172 * Revision 1.30 2001/09/29 12:56:03 joergs
173 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
175 * Revision 1.29 2001/09/24 21:09:24 jongfoster
176 * Fixing 2 memory leaks that Guy spotted, where the paramater to
177 * enlist() was not being free()d.
179 * Revision 1.28 2001/09/22 16:32:28 jongfoster
180 * Removing unused #includes.
182 * Revision 1.27 2001/09/20 15:45:25 steudten
184 * add casting from size_t to int for printf()
185 * remove local variable shadow s2
187 * Revision 1.26 2001/09/16 17:05:14 jongfoster
188 * Removing unused #include showarg.h
190 * Revision 1.25 2001/09/16 13:21:27 jongfoster
191 * Changes to use new list functions.
193 * Revision 1.24 2001/09/13 23:05:50 jongfoster
194 * Changing the string paramater to the header parsers a "const".
196 * Revision 1.23 2001/09/12 18:08:19 steudten
198 * In parse_http_request() header rewriting miss the host value, so
199 * from http://www.mydomain.com the result was just " / " not
200 * http://www.mydomain.com/ in case we forward.
202 * Revision 1.22 2001/09/10 10:58:53 oes
203 * Silenced compiler warnings
205 * Revision 1.21 2001/07/31 14:46:00 oes
206 * - Persistant connections now suppressed
207 * - sed() no longer appends empty header to csp->headers
209 * Revision 1.20 2001/07/30 22:08:36 jongfoster
210 * Tidying up #defines:
211 * - All feature #defines are now of the form FEATURE_xxx
212 * - Permanently turned off WIN_GUI_EDIT
213 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
215 * Revision 1.19 2001/07/25 17:21:54 oes
216 * client_uagent now saves copy of User-Agent: header value
218 * Revision 1.18 2001/07/13 14:02:46 oes
219 * - Included fix to repair broken HTTP requests that
220 * don't contain a path, not even '/'.
221 * - Removed all #ifdef PCRS
222 * - content_type now always inspected and classified as
223 * text, gif or other.
224 * - formatting / comments
226 * Revision 1.17 2001/06/29 21:45:41 oes
227 * Indentation, CRLF->LF, Tab-> Space
229 * Revision 1.16 2001/06/29 13:32:42 oes
231 * - Adapted free_http_request
232 * - Removed logentry from cancelled commit
234 * Revision 1.15 2001/06/03 19:12:38 oes
235 * deleted const struct interceptors
237 * Revision 1.14 2001/06/01 18:49:17 jongfoster
238 * Replaced "list_share" with "list" - the tiny memory gain was not
239 * worth the extra complexity.
241 * Revision 1.13 2001/05/31 21:30:33 jongfoster
242 * Removed list code - it's now in list.[ch]
243 * Renamed "permission" to "action", and changed many features
244 * to use the actions file rather than the global config.
246 * Revision 1.12 2001/05/31 17:33:13 oes
250 * Revision 1.11 2001/05/29 20:11:19 joergs
251 * '/ * inside comment' warning removed.
253 * Revision 1.10 2001/05/29 09:50:24 jongfoster
254 * Unified blocklist/imagelist/permissionslist.
255 * File format is still under discussion, but the internal changes
258 * Also modified interceptor behaviour:
259 * - We now intercept all URLs beginning with one of the following
260 * prefixes (and *only* these prefixes):
262 * * http://ijbswa.sf.net/config/
263 * * http://ijbswa.sourceforge.net/config/
264 * - New interceptors "home page" - go to http://i.j.b/ to see it.
265 * - Internal changes so that intercepted and fast redirect pages
266 * are not replaced with an image.
267 * - Interceptors now have the option to send a binary page direct
268 * to the client. (i.e. ijb-send-banner uses this)
269 * - Implemented show-url-info interceptor. (Which is why I needed
270 * the above interceptors changes - a typical URL is
271 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
272 * The previous mechanism would not have intercepted that, and
273 * if it had been intercepted then it then it would have replaced
276 * Revision 1.9 2001/05/28 17:26:33 jongfoster
277 * Fixing segfault if last header was crunched.
278 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
279 * can use the cross-platform sprintf() instead.)
281 * Revision 1.8 2001/05/27 22:17:04 oes
283 * - re_process_buffer no longer writes the modified buffer
284 * to the client, which was very ugly. It now returns the
285 * buffer, which it is then written by chat.
287 * - content_length now adjusts the Content-Length: header
288 * for modified documents rather than crunch()ing it.
289 * (Length info in csp->content_length, which is 0 for
290 * unmodified documents)
292 * - For this to work, sed() is called twice when filtering.
294 * Revision 1.7 2001/05/27 13:19:06 oes
295 * Patched Joergs solution for the content-length in.
297 * Revision 1.6 2001/05/26 13:39:32 jongfoster
298 * Only crunches Content-Length header if applying RE filtering.
299 * Without this fix, Microsoft Windows Update wouldn't work.
301 * Revision 1.5 2001/05/26 00:28:36 jongfoster
302 * Automatic reloading of config file.
303 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
304 * Most of the global variables have been moved to a new
305 * struct configuration_spec, accessed through csp->config->globalname
306 * Most of the globals remaining are used by the Win32 GUI.
308 * Revision 1.4 2001/05/22 18:46:04 oes
310 * - Enabled filtering banners by size rather than URL
311 * by adding patterns that replace all standard banner
312 * sizes with the "Junkbuster" gif to the re_filterfile
314 * - Enabled filtering WebBugs by providing a pattern
315 * which kills all 1x1 images
317 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
318 * which is selected by the (nonstandard and therefore
319 * capital) letter 'U' in the option string.
320 * It causes the quantifiers to be ungreedy by default.
321 * Appending a ? turns back to greedy (!).
323 * - Added a new interceptor ijb-send-banner, which
324 * sends back the "Junkbuster" gif. Without imagelist or
325 * MSIE detection support, or if tinygif = 1, or the
326 * URL isn't recognized as an imageurl, a lame HTML
327 * explanation is sent instead.
329 * - Added new feature, which permits blocking remote
330 * script redirects and firing back a local redirect
332 * The feature is conditionally compiled, i.e. it
333 * can be disabled with --disable-fast-redirects,
334 * plus it must be activated by a "fast-redirects"
335 * line in the config file, has its own log level
336 * and of course wants to be displayed by show-proxy-args
337 * Note: Boy, all the #ifdefs in 1001 locations and
338 * all the fumbling with configure.in and acconfig.h
339 * were *way* more work than the feature itself :-(
341 * - Because a generic redirect template was needed for
342 * this, tinygif = 3 now uses the same.
344 * - Moved GIFs, and other static HTTP response templates
349 * - Removed some >400 CRs again (Jon, you really worked
352 * Revision 1.3 2001/05/20 01:21:20 jongfoster
353 * Version 2.9.4 checkin.
354 * - Merged popupfile and cookiefile, and added control over PCRS
355 * filtering, in new "permissionsfile".
356 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
357 * file error you now get a message box (in the Win32 GUI) rather
358 * than the program exiting with no explanation.
359 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
361 * - Removed tabs from "config"
362 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
363 * - Bumped up version number.
365 * Revision 1.2 2001/05/17 23:02:36 oes
366 * - Made referrer option accept 'L' as a substitute for '§'
368 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
369 * Initial import of version 2.9.3 source tree
372 *********************************************************************/
379 #include <sys/types.h>
387 #if !defined(_WIN32) && !defined(__OS2__)
397 #include "jbsockets.h"
398 #include "miscutil.h"
401 const char parsers_h_rcs[] = PARSERS_H_VERSION;
403 /* Fix a problem with Solaris. There should be no effect on other
405 * Solaris's isspace() is a macro which uses it's argument directly
406 * as an array index. Therefore we need to make sure that high-bit
407 * characters generate +ve values, and ideally we also want to make
408 * the argument match the declared parameter type of "int".
410 * Why did they write a character function that can't take a simple
411 * "char" argument? Doh!
413 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
414 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
417 const struct parsers client_patterns[] = {
418 { "referer:", 8, client_referrer },
419 { "user-agent:", 11, client_uagent },
420 { "ua-", 3, client_ua },
421 { "from:", 5, client_from },
422 { "cookie:", 7, client_send_cookie },
423 { "x-forwarded-for:", 16, client_x_forwarded },
424 { "Accept-Encoding:", 16, client_accept_encoding },
425 { "TE:", 3, client_te },
426 { "Host:", 5, crumble },
427 /* { "if-modified-since:", 18, crumble }, */
428 { "Keep-Alive:", 11, crumble },
429 { "connection:", 11, crumble },
430 { "proxy-connection:", 17, crumble },
435 const struct parsers server_patterns[] = {
436 { "HTTP", 4, server_http },
437 { "set-cookie:", 11, server_set_cookie },
438 { "connection:", 11, crumble },
439 { "Content-Type:", 13, server_content_type },
440 { "Content-Length:", 15, server_content_length },
441 { "Content-MD5:", 12, server_content_md5 },
442 { "Content-Encoding:", 17, server_content_encoding },
443 { "Transfer-Encoding:", 18, server_transfer_coding },
444 { "Keep-Alive:", 11, crumble },
449 const add_header_func_ptr add_client_headers[] = {
452 client_x_forwarded_adder,
454 client_accept_encoding_adder,
455 connection_close_adder,
460 const add_header_func_ptr add_server_headers[] = {
461 connection_close_adder,
466 /*********************************************************************
468 * Function : flush_socket
470 * Description : Write any pending "buffered" content.
473 * 1 : fd = file descriptor of the socket to read
474 * 2 : csp = Current client state (buffers, headers, etc...)
476 * Returns : On success, the number of bytes written are returned (zero
477 * indicates nothing was written). On error, -1 is returned,
478 * and errno is set appropriately. If count is zero and the
479 * file descriptor refers to a regular file, 0 will be
480 * returned without causing any other effect. For a special
481 * file, the results are not portable.
483 *********************************************************************/
484 int flush_socket(jb_socket fd, struct client_state *csp)
486 struct iob *iob = csp->iob;
487 int len = iob->eod - iob->cur;
494 if (write_socket(fd, iob->cur, (size_t)len))
498 iob->eod = iob->cur = iob->buf;
504 /*********************************************************************
506 * Function : add_to_iob
508 * Description : Add content to the buffered page.
511 * 1 : csp = Current client state (buffers, headers, etc...)
512 * 2 : buf = holds the content to be added to the page
513 * 3 : n = number of bytes to be added
517 *********************************************************************/
518 void add_to_iob(struct client_state *csp, char *buf, int n)
520 struct iob *iob = csp->iob;
524 have = iob->eod - iob->cur;
533 if ((p = (char *)malloc(need + 1)) == NULL)
535 log_error(LOG_LEVEL_FATAL, "malloc() iob failed: %E");
540 /* there is something in the buffer - save it */
541 memcpy(p, iob->cur, have);
543 /* replace the buffer with the new space */
547 /* point to the end of the data */
552 /* the buffer is empty, free it and reinitialize */
557 /* copy the new data into the iob buffer */
558 memcpy(p, buf, (size_t)n);
560 /* point to the end of the data */
563 /* null terminate == cheap insurance */
566 /* set the pointers to the new values */
575 /*********************************************************************
577 * Function : get_header
579 * Description : This (odd) routine will parse the csp->iob
582 * 1 : csp = Current client state (buffers, headers, etc...)
584 * Returns : Any one of the following:
586 * 1) a pointer to a dynamically allocated string that contains a header line
587 * 2) NULL indicating that the end of the header was reached
588 * 3) "" indicating that the end of the iob was reached before finding
589 * a complete header line.
591 *********************************************************************/
592 char *get_header(struct client_state *csp)
598 if ((iob->cur == NULL)
599 || ((p = strchr(iob->cur, '\n')) == NULL))
601 return(""); /* couldn't find a complete header */
606 ret = strdup(iob->cur);
609 /* FIXME No way to handle error properly */
610 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
615 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
617 /* is this a blank linke (i.e. the end of the header) ? */
629 /*********************************************************************
631 * Function : get_header_value
633 * Description : Get the value of a given header from a chained list
634 * of header lines or return NULL if no such header is
635 * present in the list.
638 * 1 : header_list = pointer to list
639 * 2 : header_name = string with name of header to look for.
640 * Trailing colon required, capitalization
643 * Returns : NULL if not found, else value of header
645 *********************************************************************/
646 char *get_header_value(const struct list *header_list, const char *header_name)
648 struct list_entry *cur_entry;
654 length = strlen(header_name);
656 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
660 if (!strncmpic(cur_entry->str, header_name, length))
663 * Found: return pointer to start of value
665 ret = (char *) (cur_entry->str + length);
666 while (*ret && ijb_isspace(*ret)) ret++;
679 /*********************************************************************
683 * Description : add, delete or modify lines in the HTTP header streams.
684 * On entry, it receives a linked list of headers space
685 * that was allocated dynamically (both the list nodes
686 * and the header contents).
688 * As a side effect it frees the space used by the original
692 * 1 : pats = list of patterns to match against headers
693 * 2 : more_headers = list of functions to add more
694 * headers (client or server)
695 * 3 : csp = Current client state (buffers, headers, etc...)
697 * Returns : Single pointer to a fully formed header, or NULL
698 * on out-of-memory error.
700 *********************************************************************/
701 char *sed(const struct parsers pats[],
702 const add_header_func_ptr more_headers[],
703 struct client_state *csp)
705 struct list_entry *p;
706 const struct parsers *v;
707 const add_header_func_ptr *f;
708 jb_err err = JB_ERR_OK;
710 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
712 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
714 /* Header crunch()ed in previous run? -> ignore */
715 if (p->str == NULL) continue;
717 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
719 if (strncmpic(p->str, v->str, v->len) == 0)
721 err = v->parser(csp, (char **)&(p->str));
726 /* place any additional headers on the csp->headers list */
727 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
732 if (err != JB_ERR_OK)
737 return list_to_text(csp->headers);
741 /* here begins the family of parser functions that reformat header lines */
744 /*********************************************************************
748 * Description : This is called if a header matches a pattern to "crunch"
751 * 1 : csp = Current client state (buffers, headers, etc...)
752 * 2 : header = On input, pointer to header to modify.
753 * On output, pointer to the modified header, or NULL
754 * to remove the header. This function frees the
755 * original string if necessary.
757 * Returns : JB_ERR_OK on success, or
758 * JB_ERR_MEMORY on out-of-memory error.
760 *********************************************************************/
761 jb_err crumble(struct client_state *csp, char **header)
763 log_error(LOG_LEVEL_HEADER, "crunch!");
769 /*********************************************************************
771 * Function : server_content_type
773 * Description : Set the content-type for filterable types (text/.*,
774 * javascript and image/gif) unless filtering has been
775 * forbidden (CT_TABOO) while parsing earlier headers.
778 * 1 : csp = Current client state (buffers, headers, etc...)
779 * 2 : header = On input, pointer to header to modify.
780 * On output, pointer to the modified header, or NULL
781 * to remove the header. This function frees the
782 * original string if necessary.
784 * Returns : JB_ERR_OK on success, or
785 * JB_ERR_MEMORY on out-of-memory error.
787 *********************************************************************/
788 jb_err server_content_type(struct client_state *csp, char **header)
790 if (csp->content_type != CT_TABOO)
792 if (strstr(*header, " text/")
793 || strstr(*header, "application/x-javascript"))
794 csp->content_type = CT_TEXT;
795 else if (strstr(*header, " image/gif"))
796 csp->content_type = CT_GIF;
798 csp->content_type = 0;
805 /*********************************************************************
807 * Function : server_transfer_coding
809 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
810 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
811 * - Change from "chunked" to "identity" if body was chunked
812 * but has been de-chunked for filtering.
815 * 1 : csp = Current client state (buffers, headers, etc...)
816 * 2 : header = On input, pointer to header to modify.
817 * On output, pointer to the modified header, or NULL
818 * to remove the header. This function frees the
819 * original string if necessary.
821 * Returns : JB_ERR_OK on success, or
822 * JB_ERR_MEMORY on out-of-memory error.
824 *********************************************************************/
825 jb_err server_transfer_coding(struct client_state *csp, char **header)
828 * Turn off pcrs and gif filtering if body compressed
830 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
832 csp->content_type = CT_TABOO;
836 * Raise flag if body chunked
838 if (strstr(*header, "chunked"))
840 csp->flags |= CSP_FLAG_CHUNKED;
843 * If the body was modified, it has been
844 * de-chunked first, so adjust the header:
846 if (csp->flags & CSP_FLAG_MODIFIED)
849 *header = strdup("Transfer-Encoding: identity");
850 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
858 /*********************************************************************
860 * Function : server_content_encoding
862 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
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_content_encoding(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;
890 /*********************************************************************
892 * Function : server_content_length
894 * Description : Adjust Content-Length header if we modified
898 * 1 : csp = Current client state (buffers, headers, etc...)
899 * 2 : header = On input, pointer to header to modify.
900 * On output, pointer to the modified header, or NULL
901 * to remove the header. This function frees the
902 * original string if necessary.
904 * Returns : JB_ERR_OK on success, or
905 * JB_ERR_MEMORY on out-of-memory error.
907 *********************************************************************/
908 jb_err server_content_length(struct client_state *csp, char **header)
910 if (csp->content_length != 0) /* Content length has been modified */
913 *header = (char *) zalloc(100);
916 return JB_ERR_MEMORY;
919 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
921 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
928 /*********************************************************************
930 * Function : server_content_md5
932 * Description : Crumble any Content-MD5 headers if the document was
933 * modified. FIXME: Should we re-compute instead?
936 * 1 : csp = Current client state (buffers, headers, etc...)
937 * 2 : header = On input, pointer to header to modify.
938 * On output, pointer to the modified header, or NULL
939 * to remove the header. This function frees the
940 * original string if necessary.
942 * Returns : JB_ERR_OK on success, or
943 * JB_ERR_MEMORY on out-of-memory error.
945 *********************************************************************/
946 jb_err server_content_md5(struct client_state *csp, char **header)
948 if (csp->flags & CSP_FLAG_MODIFIED)
950 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
958 /*********************************************************************
960 * Function : client_accept_encoding
962 * Description : Rewrite the client's Accept-Encoding header so that
963 * if doesn't allow compression, if the action applies.
964 * Note: For HTTP/1.0 the absence of the header is enough.
967 * 1 : csp = Current client state (buffers, headers, etc...)
968 * 2 : header = On input, pointer to header to modify.
969 * On output, pointer to the modified header, or NULL
970 * to remove the header. This function frees the
971 * original string if necessary.
973 * Returns : JB_ERR_OK on success, or
974 * JB_ERR_MEMORY on out-of-memory error.
976 *********************************************************************/
977 jb_err client_accept_encoding(struct client_state *csp, char **header)
979 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
981 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
984 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
986 *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
989 return JB_ERR_MEMORY;
998 /*********************************************************************
1000 * Function : client_te
1002 * Description : Rewrite the client's TE header so that
1003 * if doesn't allow compression, if the action applies.
1006 * 1 : csp = Current client state (buffers, headers, etc...)
1007 * 2 : header = On input, pointer to header to modify.
1008 * On output, pointer to the modified header, or NULL
1009 * to remove the header. This function frees the
1010 * original string if necessary.
1012 * Returns : JB_ERR_OK on success, or
1013 * JB_ERR_MEMORY on out-of-memory error.
1015 *********************************************************************/
1016 jb_err client_te(struct client_state *csp, char **header)
1018 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1021 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1027 /*********************************************************************
1029 * Function : client_referrer
1031 * Description : Handle the "referer" config setting properly.
1032 * Called from `sed'.
1035 * 1 : csp = Current client state (buffers, headers, etc...)
1036 * 2 : header = On input, pointer to header to modify.
1037 * On output, pointer to the modified header, or NULL
1038 * to remove the header. This function frees the
1039 * original string if necessary.
1041 * Returns : JB_ERR_OK on success, or
1042 * JB_ERR_MEMORY on out-of-memory error.
1044 *********************************************************************/
1045 jb_err client_referrer(struct client_state *csp, char **header)
1049 #ifdef FEATURE_FORCE_LOAD
1050 /* Since the referrer can include the prefix even
1051 * even if the request itself is non-forced, we must
1052 * clean it unconditionally
1054 strclean(*header, FORCE_PREFIX);
1055 #endif /* def FEATURE_FORCE_LOAD */
1058 * Are we sending referer?
1060 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1067 newval = csp->action->string[ACTION_STRING_REFERER];
1069 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1074 log_error(LOG_LEVEL_HEADER, "crunch!");
1077 else if (0 == strncmpic(newval, "http://", 7))
1080 * We have a specific (fixed) referer we want to send.
1082 log_error(LOG_LEVEL_HEADER, "modified");
1084 *header = strdup("Referer: ");
1085 string_append(header, newval);
1087 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1092 * Forge a referer as http://[hostname:port of REQUEST]/
1093 * to fool stupid checks for in-site links
1095 if (0 != strcmpic(newval, "forge"))
1098 * Invalid choice - but forge is probably the best default.
1100 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1103 *header = strdup("Referer: http://");
1104 string_append(header, csp->http->hostport);
1105 string_append(header, "/");
1106 log_error(LOG_LEVEL_HEADER, "crunch+forge to %s", *header);
1108 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1113 /*********************************************************************
1115 * Function : client_uagent
1117 * Description : Handle the "user-agent" config setting properly
1118 * and remember its original value to enable browser
1119 * bug workarounds. Called from `sed'.
1122 * 1 : csp = Current client state (buffers, headers, etc...)
1123 * 2 : header = On input, pointer to header to modify.
1124 * On output, pointer to the modified header, or NULL
1125 * to remove the header. This function frees the
1126 * original string if necessary.
1128 * Returns : JB_ERR_OK on success, or
1129 * JB_ERR_MEMORY on out-of-memory error.
1131 *********************************************************************/
1132 jb_err client_uagent(struct client_state *csp, char **header)
1136 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1141 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1147 log_error(LOG_LEVEL_HEADER, "modified");
1150 *header = strdup("User-Agent: ");
1151 string_append(header, newval);
1153 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1157 /*********************************************************************
1159 * Function : client_ua
1161 * Description : Handle "ua-" headers properly. Called from `sed'.
1164 * 1 : csp = Current client state (buffers, headers, etc...)
1165 * 2 : header = On input, pointer to header to modify.
1166 * On output, pointer to the modified header, or NULL
1167 * to remove the header. This function frees the
1168 * original string if necessary.
1170 * Returns : JB_ERR_OK on success, or
1171 * JB_ERR_MEMORY on out-of-memory error.
1173 *********************************************************************/
1174 jb_err client_ua(struct client_state *csp, char **header)
1176 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1178 log_error(LOG_LEVEL_HEADER, "crunch!");
1186 /*********************************************************************
1188 * Function : client_from
1190 * Description : Handle the "from" config setting properly.
1191 * Called from `sed'.
1194 * 1 : csp = Current client state (buffers, headers, etc...)
1195 * 2 : header = On input, pointer to header to modify.
1196 * On output, pointer to the modified header, or NULL
1197 * to remove the header. This function frees the
1198 * original string if necessary.
1200 * Returns : JB_ERR_OK on success, or
1201 * JB_ERR_MEMORY on out-of-memory error.
1203 *********************************************************************/
1204 jb_err client_from(struct client_state *csp, char **header)
1208 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1215 newval = csp->action->string[ACTION_STRING_FROM];
1218 * Are we blocking the e-mail address?
1220 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1222 log_error(LOG_LEVEL_HEADER, "crunch!");
1226 log_error(LOG_LEVEL_HEADER, " modified");
1228 *header = strdup("From: ");
1229 string_append(header, newval);
1231 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1235 /*********************************************************************
1237 * Function : client_send_cookie
1239 * Description : Handle the "cookie" header properly. Called from `sed'.
1240 * If cookie is accepted, add it to the cookie_list,
1241 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1244 * 1 : csp = Current client state (buffers, headers, etc...)
1245 * 2 : header = On input, pointer to header to modify.
1246 * On output, pointer to the modified header, or NULL
1247 * to remove the header. This function frees the
1248 * original string if necessary.
1250 * Returns : JB_ERR_OK on success, or
1251 * JB_ERR_MEMORY on out-of-memory error.
1253 *********************************************************************/
1254 jb_err client_send_cookie(struct client_state *csp, char **header)
1256 jb_err result = JB_ERR_OK;
1258 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1260 /* strlen("cookie: ") == 8 */
1261 result = enlist(csp->cookie_list, *header + 8);
1265 log_error(LOG_LEVEL_HEADER, " crunch!");
1269 * Always remove the cookie here. The cookie header
1270 * will be sent at the end of the header.
1278 /*********************************************************************
1280 * Function : client_x_forwarded
1282 * Description : Handle the "x-forwarded-for" config setting properly,
1283 * also used in the add_client_headers list. Called from `sed'.
1286 * 1 : csp = Current client state (buffers, headers, etc...)
1287 * 2 : header = On input, pointer to header to modify.
1288 * On output, pointer to the modified header, or NULL
1289 * to remove the header. This function frees the
1290 * original string if necessary.
1292 * Returns : JB_ERR_OK on success, or
1293 * JB_ERR_MEMORY on out-of-memory error.
1295 *********************************************************************/
1296 jb_err client_x_forwarded(struct client_state *csp, char **header)
1298 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1300 /* Save it so we can re-add it later */
1301 freez(csp->x_forwarded);
1302 csp->x_forwarded = *header;
1305 * Always set *header = NULL, since this information
1306 * will be sent at the end of the header.
1313 log_error(LOG_LEVEL_HEADER, " crunch!");
1319 /* the following functions add headers directly to the header list */
1321 /*********************************************************************
1323 * Function : client_host_adder
1325 * Description : (re)adds the host header. Called from `sed'.
1328 * 1 : csp = Current client state (buffers, headers, etc...)
1330 * Returns : JB_ERR_OK on success, or
1331 * JB_ERR_MEMORY on out-of-memory error.
1333 *********************************************************************/
1334 jb_err client_host_adder(struct client_state *csp)
1340 if ( !csp->http->hostport || !*(csp->http->hostport))
1345 p = strdup("Host: ");
1347 ** remove 'user:pass@' from 'proto://user:pass@host'
1349 if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
1351 string_append(&p, pos+1);
1355 string_append(&p, csp->http->hostport);
1360 return JB_ERR_MEMORY;
1363 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1365 err = enlist(csp->headers, p);
1373 /*********************************************************************
1375 * Function : client_cookie_adder
1377 * Description : Used in the add_client_headers list. Called from `sed'.
1380 * 1 : csp = Current client state (buffers, headers, etc...)
1382 * Returns : JB_ERR_OK on success, or
1383 * JB_ERR_MEMORY on out-of-memory error.
1385 *********************************************************************/
1386 jb_err client_cookie_adder(struct client_state *csp)
1388 struct list_entry *lst;
1390 struct list_entry *list1 = csp->cookie_list->first;
1391 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1392 int first_cookie = 1;
1395 if ((list1 == NULL) && (list2 == NULL))
1401 tmp = strdup("Cookie: ");
1403 for (lst = list1; lst ; lst = lst->next)
1411 string_append(&tmp, "; ");
1413 string_append(&tmp, lst->str);
1416 for (lst = list2; lst ; lst = lst->next)
1424 string_append(&tmp, "; ");
1426 string_join(&tmp, cookie_encode(lst->str));
1431 return JB_ERR_MEMORY;
1434 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1435 err = enlist(csp->headers, tmp);
1441 /*********************************************************************
1443 * Function : client_accept_encoding_adder
1445 * Description : Add an Accept-Encoding header to the client's request
1446 * that disables compression if the action applies, and
1447 * the header is not already there. Called from `sed'.
1448 * Note: For HTTP/1.0, the absence of the header is enough.
1451 * 1 : csp = Current client state (buffers, headers, etc...)
1453 * Returns : JB_ERR_OK on success, or
1454 * JB_ERR_MEMORY on out-of-memory error.
1456 *********************************************************************/
1457 jb_err client_accept_encoding_adder(struct client_state *csp)
1459 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1460 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1462 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1469 /*********************************************************************
1471 * Function : client_xtra_adder
1473 * Description : Used in the add_client_headers list. Called from `sed'.
1476 * 1 : csp = Current client state (buffers, headers, etc...)
1478 * Returns : JB_ERR_OK on success, or
1479 * JB_ERR_MEMORY on out-of-memory error.
1481 *********************************************************************/
1482 jb_err client_xtra_adder(struct client_state *csp)
1484 struct list_entry *lst;
1487 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1488 lst ; lst = lst->next)
1490 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1491 err = enlist(csp->headers, lst->str);
1503 /*********************************************************************
1505 * Function : client_x_forwarded_adder
1507 * Description : Used in the add_client_headers list. Called from `sed'.
1510 * 1 : csp = Current client state (buffers, headers, etc...)
1512 * Returns : JB_ERR_OK on success, or
1513 * JB_ERR_MEMORY on out-of-memory error.
1515 *********************************************************************/
1516 jb_err client_x_forwarded_adder(struct client_state *csp)
1521 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1526 if (csp->x_forwarded)
1528 p = strdup(csp->x_forwarded);
1529 string_append(&p, ", ");
1533 p = strdup("X-Forwarded-For: ");
1535 string_append(&p, csp->ip_addr_str);
1539 return JB_ERR_MEMORY;
1542 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1543 err = enlist(csp->headers, p);
1550 /*********************************************************************
1552 * Function : connection_close_adder
1554 * Description : Adds a "Connection: close" header to csp->headers
1555 * as a temporary fix for the needed but missing HTTP/1.1
1556 * support. Called from `sed'.
1557 * FIXME: This whole function shouldn't be neccessary!
1560 * 1 : csp = Current client state (buffers, headers, etc...)
1562 * Returns : JB_ERR_OK on success, or
1563 * JB_ERR_MEMORY on out-of-memory error.
1565 *********************************************************************/
1566 jb_err connection_close_adder(struct client_state *csp)
1568 return enlist(csp->headers, "Connection: close");
1572 /*********************************************************************
1574 * Function : server_http
1576 * Description : - Save the HTTP Status into csp->http->status
1577 * - Set CT_TABOO to prevent filtering if the answer
1578 * is a partial range (HTTP status 206)
1579 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1583 * 1 : csp = Current client state (buffers, headers, etc...)
1584 * 2 : header = On input, pointer to header to modify.
1585 * On output, pointer to the modified header, or NULL
1586 * to remove the header. This function frees the
1587 * original string if necessary.
1589 * Returns : JB_ERR_OK on success, or
1590 * JB_ERR_MEMORY on out-of-memory error.
1592 *********************************************************************/
1593 jb_err server_http(struct client_state *csp, char **header)
1595 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1596 if (csp->http->status == 206)
1598 csp->content_type = CT_TABOO;
1601 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1604 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1611 /*********************************************************************
1613 * Function : server_set_cookie
1615 * Description : Handle the server "cookie" header properly.
1616 * Log cookie to the jar file. Then "crunch" it,
1617 * or accept it. Called from `sed'.
1620 * 1 : csp = Current client state (buffers, headers, etc...)
1621 * 2 : header = On input, pointer to header to modify.
1622 * On output, pointer to the modified header, or NULL
1623 * to remove the header. This function frees the
1624 * original string if necessary.
1626 * Returns : JB_ERR_OK on success, or
1627 * JB_ERR_MEMORY on out-of-memory error.
1629 *********************************************************************/
1630 jb_err server_set_cookie(struct client_state *csp, char **header)
1632 #ifdef FEATURE_COOKIE_JAR
1633 if (csp->config->jar)
1636 * Write timestamp into outbuf.
1638 * Complex because not all OSs have tm_gmtoff or
1639 * the %z field in strftime()
1641 char tempbuf[ BUFFER_SIZE ];
1645 #ifdef HAVE_LOCALTIME_R
1646 tm_now = *localtime_r(&now, &tm_now);
1648 tm_now = *localtime (&now);
1650 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1652 /* strlen("set-cookie: ") = 12 */
1653 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1655 #endif /* def FEATURE_COOKIE_JAR */
1657 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1659 return crumble(csp, header);
1661 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1663 /* Flag whether or not to log a message */
1666 /* A variable to store the tag we're working on */
1669 /* Skip "Set-Cookie:" (11 characters) in header */
1670 cur_tag = *header + 11;
1672 /* skip whitespace between "Set-Cookie:" and value */
1673 while (*cur_tag && ijb_isspace(*cur_tag))
1678 /* Loop through each tag in the cookie */
1682 char *next_tag = strchr(cur_tag, ';');
1683 if (next_tag != NULL)
1685 /* Skip the ';' character itself */
1688 /* skip whitespace ";" and start of tag */
1689 while (*next_tag && ijb_isspace(*next_tag))
1696 /* "Next tag" is the end of the string */
1697 next_tag = cur_tag + strlen(cur_tag);
1700 /* Is this the "Expires" tag? */
1701 if (strncmpic(cur_tag, "expires=", 8) == 0)
1703 /* Delete the tag by copying the rest of the string over it.
1704 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1705 * since the behaviour of strcpy is undefined for overlapping
1708 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1710 /* That changed the header, need to issue a log message */
1713 /* Note that the next tag has now been moved to *cur_tag,
1714 * so we do not need to update the cur_tag pointer.
1719 /* Move on to next cookie tag */
1726 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1734 #ifdef FEATURE_FORCE_LOAD
1735 /*********************************************************************
1737 * Function : strclean
1739 * Description : In-Situ-Eliminate all occurances of substring in
1743 * 1 : string = string to clean
1744 * 2 : substring = substring to eliminate
1746 * Returns : Number of eliminations
1748 *********************************************************************/
1749 int strclean(const char *string, const char *substring)
1751 int hits = 0, len = strlen(substring);
1754 while((pos = strstr(string, substring)) != NULL)
1761 while (*p++ != '\0');
1768 #endif /* def FEATURE_FORCE_LOAD */