1 const char parsers_rcs[] = "$Id: parsers.c,v 1.48 2002/03/07 03:46:53 oes 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 * IJBSWA team. http://ijbswa.sourceforge.net
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.48 2002/03/07 03:46:53 oes
44 * Fixed compiler warnings etc
46 * Revision 1.47 2002/02/20 23:15:13 jongfoster
47 * Parsing functions now handle out-of-memory gracefully by returning
50 * Revision 1.46 2002/01/17 21:03:47 jongfoster
51 * Moving all our URL and URL pattern parsing code to urlmatch.c.
53 * Revision 1.45 2002/01/09 14:33:03 oes
54 * Added support for localtime_r.
56 * Revision 1.44 2001/12/14 01:22:54 steudten
57 * Remove 'user:pass@' from 'proto://user:pass@host' for the
58 * new added header 'Host: ..'. (See Req ID 491818)
60 * Revision 1.43 2001/11/23 00:26:38 jongfoster
61 * Fixing two really stupid errors in my previous commit
63 * Revision 1.42 2001/11/22 21:59:30 jongfoster
64 * Adding code to handle +no-cookies-keep
66 * Revision 1.41 2001/11/05 23:43:05 steudten
67 * Add time+date to log files.
69 * Revision 1.40 2001/10/26 20:13:09 jongfoster
70 * ctype.h is needed in Windows, too.
72 * Revision 1.39 2001/10/26 17:40:04 oes
73 * Introduced get_header_value()
74 * Removed http->user_agent, csp->referrer and csp->accept_types
75 * Removed client_accept()
77 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
78 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
79 * threads to call select() simultaneously. So, it's time to do a real, live,
80 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
81 * (native). Both versions will work, but using __OS2__ offers multi-threading.
83 * Revision 1.37 2001/10/23 21:36:02 jongfoster
84 * Documenting sed()'s error behaviou (doc change only)
86 * Revision 1.36 2001/10/13 12:51:51 joergs
87 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
88 * force-load), instead crumble Host: and add it (again) in client_host_adder
89 * (in case we get a HTTP/1.0 request without Host: header and forward it to
90 * a HTTP/1.1 server/proxy).
92 * Revision 1.35 2001/10/09 22:39:21 jongfoster
93 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
96 * Revision 1.34 2001/10/07 18:50:55 oes
97 * Added server_content_encoding, renamed server_transfer_encoding
99 * Revision 1.33 2001/10/07 18:04:49 oes
100 * Changed server_http11 to server_http and its pattern to "HTTP".
101 * Additional functionality: it now saves the HTTP status into
102 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
104 * Revision 1.32 2001/10/07 15:43:28 oes
105 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
106 * client_te and client_accept_encoding_adder, triggered by the new
107 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
108 * changed to allow only identity and chunked, and the TE header is
109 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
111 * parse_http_request no longer does anything than parsing. The rewriting
112 * of http->cmd and version mangling are gone. It now also recognizes
113 * the put and delete methods and saves the url in http->url. Removed
116 * renamed content_type and content_length to have the server_ prefix
118 * server_content_type now only works if csp->content_type != CT_TABOO
120 * added server_transfer_encoding, which
121 * - Sets CT_TABOO to prohibit filtering if encoding compresses
122 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
123 * - Change from "chunked" to "identity" if body was chunked
124 * but has been de-chunked for filtering.
126 * added server_content_md5 which crunches any Content-MD5 headers
127 * if the body was modified.
129 * made server_http11 conditional on +downgrade action
131 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
133 * Revision 1.31 2001/10/05 14:25:02 oes
134 * Crumble Keep-Alive from Server
136 * Revision 1.30 2001/09/29 12:56:03 joergs
137 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
139 * Revision 1.29 2001/09/24 21:09:24 jongfoster
140 * Fixing 2 memory leaks that Guy spotted, where the paramater to
141 * enlist() was not being free()d.
143 * Revision 1.28 2001/09/22 16:32:28 jongfoster
144 * Removing unused #includes.
146 * Revision 1.27 2001/09/20 15:45:25 steudten
148 * add casting from size_t to int for printf()
149 * remove local variable shadow s2
151 * Revision 1.26 2001/09/16 17:05:14 jongfoster
152 * Removing unused #include showarg.h
154 * Revision 1.25 2001/09/16 13:21:27 jongfoster
155 * Changes to use new list functions.
157 * Revision 1.24 2001/09/13 23:05:50 jongfoster
158 * Changing the string paramater to the header parsers a "const".
160 * Revision 1.23 2001/09/12 18:08:19 steudten
162 * In parse_http_request() header rewriting miss the host value, so
163 * from http://www.mydomain.com the result was just " / " not
164 * http://www.mydomain.com/ in case we forward.
166 * Revision 1.22 2001/09/10 10:58:53 oes
167 * Silenced compiler warnings
169 * Revision 1.21 2001/07/31 14:46:00 oes
170 * - Persistant connections now suppressed
171 * - sed() no longer appends empty header to csp->headers
173 * Revision 1.20 2001/07/30 22:08:36 jongfoster
174 * Tidying up #defines:
175 * - All feature #defines are now of the form FEATURE_xxx
176 * - Permanently turned off WIN_GUI_EDIT
177 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
179 * Revision 1.19 2001/07/25 17:21:54 oes
180 * client_uagent now saves copy of User-Agent: header value
182 * Revision 1.18 2001/07/13 14:02:46 oes
183 * - Included fix to repair broken HTTP requests that
184 * don't contain a path, not even '/'.
185 * - Removed all #ifdef PCRS
186 * - content_type now always inspected and classified as
187 * text, gif or other.
188 * - formatting / comments
190 * Revision 1.17 2001/06/29 21:45:41 oes
191 * Indentation, CRLF->LF, Tab-> Space
193 * Revision 1.16 2001/06/29 13:32:42 oes
195 * - Adapted free_http_request
196 * - Removed logentry from cancelled commit
198 * Revision 1.15 2001/06/03 19:12:38 oes
199 * deleted const struct interceptors
201 * Revision 1.14 2001/06/01 18:49:17 jongfoster
202 * Replaced "list_share" with "list" - the tiny memory gain was not
203 * worth the extra complexity.
205 * Revision 1.13 2001/05/31 21:30:33 jongfoster
206 * Removed list code - it's now in list.[ch]
207 * Renamed "permission" to "action", and changed many features
208 * to use the actions file rather than the global config.
210 * Revision 1.12 2001/05/31 17:33:13 oes
214 * Revision 1.11 2001/05/29 20:11:19 joergs
215 * '/ * inside comment' warning removed.
217 * Revision 1.10 2001/05/29 09:50:24 jongfoster
218 * Unified blocklist/imagelist/permissionslist.
219 * File format is still under discussion, but the internal changes
222 * Also modified interceptor behaviour:
223 * - We now intercept all URLs beginning with one of the following
224 * prefixes (and *only* these prefixes):
226 * * http://ijbswa.sf.net/config/
227 * * http://ijbswa.sourceforge.net/config/
228 * - New interceptors "home page" - go to http://i.j.b/ to see it.
229 * - Internal changes so that intercepted and fast redirect pages
230 * are not replaced with an image.
231 * - Interceptors now have the option to send a binary page direct
232 * to the client. (i.e. ijb-send-banner uses this)
233 * - Implemented show-url-info interceptor. (Which is why I needed
234 * the above interceptors changes - a typical URL is
235 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
236 * The previous mechanism would not have intercepted that, and
237 * if it had been intercepted then it then it would have replaced
240 * Revision 1.9 2001/05/28 17:26:33 jongfoster
241 * Fixing segfault if last header was crunched.
242 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
243 * can use the cross-platform sprintf() instead.)
245 * Revision 1.8 2001/05/27 22:17:04 oes
247 * - re_process_buffer no longer writes the modified buffer
248 * to the client, which was very ugly. It now returns the
249 * buffer, which it is then written by chat.
251 * - content_length now adjusts the Content-Length: header
252 * for modified documents rather than crunch()ing it.
253 * (Length info in csp->content_length, which is 0 for
254 * unmodified documents)
256 * - For this to work, sed() is called twice when filtering.
258 * Revision 1.7 2001/05/27 13:19:06 oes
259 * Patched Joergs solution for the content-length in.
261 * Revision 1.6 2001/05/26 13:39:32 jongfoster
262 * Only crunches Content-Length header if applying RE filtering.
263 * Without this fix, Microsoft Windows Update wouldn't work.
265 * Revision 1.5 2001/05/26 00:28:36 jongfoster
266 * Automatic reloading of config file.
267 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
268 * Most of the global variables have been moved to a new
269 * struct configuration_spec, accessed through csp->config->globalname
270 * Most of the globals remaining are used by the Win32 GUI.
272 * Revision 1.4 2001/05/22 18:46:04 oes
274 * - Enabled filtering banners by size rather than URL
275 * by adding patterns that replace all standard banner
276 * sizes with the "Junkbuster" gif to the re_filterfile
278 * - Enabled filtering WebBugs by providing a pattern
279 * which kills all 1x1 images
281 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
282 * which is selected by the (nonstandard and therefore
283 * capital) letter 'U' in the option string.
284 * It causes the quantifiers to be ungreedy by default.
285 * Appending a ? turns back to greedy (!).
287 * - Added a new interceptor ijb-send-banner, which
288 * sends back the "Junkbuster" gif. Without imagelist or
289 * MSIE detection support, or if tinygif = 1, or the
290 * URL isn't recognized as an imageurl, a lame HTML
291 * explanation is sent instead.
293 * - Added new feature, which permits blocking remote
294 * script redirects and firing back a local redirect
296 * The feature is conditionally compiled, i.e. it
297 * can be disabled with --disable-fast-redirects,
298 * plus it must be activated by a "fast-redirects"
299 * line in the config file, has its own log level
300 * and of course wants to be displayed by show-proxy-args
301 * Note: Boy, all the #ifdefs in 1001 locations and
302 * all the fumbling with configure.in and acconfig.h
303 * were *way* more work than the feature itself :-(
305 * - Because a generic redirect template was needed for
306 * this, tinygif = 3 now uses the same.
308 * - Moved GIFs, and other static HTTP response templates
313 * - Removed some >400 CRs again (Jon, you really worked
316 * Revision 1.3 2001/05/20 01:21:20 jongfoster
317 * Version 2.9.4 checkin.
318 * - Merged popupfile and cookiefile, and added control over PCRS
319 * filtering, in new "permissionsfile".
320 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
321 * file error you now get a message box (in the Win32 GUI) rather
322 * than the program exiting with no explanation.
323 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
325 * - Removed tabs from "config"
326 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
327 * - Bumped up version number.
329 * Revision 1.2 2001/05/17 23:02:36 oes
330 * - Made referrer option accept 'L' as a substitute for '§'
332 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
333 * Initial import of version 2.9.3 source tree
336 *********************************************************************/
343 #include <sys/types.h>
351 #if !defined(_WIN32) && !defined(__OS2__)
361 #include "jbsockets.h"
362 #include "miscutil.h"
365 const char parsers_h_rcs[] = PARSERS_H_VERSION;
367 /* Fix a problem with Solaris. There should be no effect on other
369 * Solaris's isspace() is a macro which uses it's argument directly
370 * as an array index. Therefore we need to make sure that high-bit
371 * characters generate +ve values, and ideally we also want to make
372 * the argument match the declared parameter type of "int".
374 * Why did they write a character function that can't take a simple
375 * "char" argument? Doh!
377 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
378 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
381 const struct parsers client_patterns[] = {
382 { "referer:", 8, client_referrer },
383 { "user-agent:", 11, client_uagent },
384 { "ua-", 3, client_ua },
385 { "from:", 5, client_from },
386 { "cookie:", 7, client_send_cookie },
387 { "x-forwarded-for:", 16, client_x_forwarded },
388 { "Accept-Encoding:", 16, client_accept_encoding },
389 { "TE:", 3, client_te },
390 { "Host:", 5, crumble },
391 /* { "if-modified-since:", 18, crumble }, */
392 { "Keep-Alive:", 11, crumble },
393 { "connection:", 11, crumble },
394 { "proxy-connection:", 17, crumble },
399 const struct parsers server_patterns[] = {
400 { "HTTP", 4, server_http },
401 { "set-cookie:", 11, server_set_cookie },
402 { "connection:", 11, crumble },
403 { "Content-Type:", 13, server_content_type },
404 { "Content-Length:", 15, server_content_length },
405 { "Content-MD5:", 12, server_content_md5 },
406 { "Content-Encoding:", 17, server_content_encoding },
407 { "Transfer-Encoding:", 18, server_transfer_coding },
408 { "Keep-Alive:", 11, crumble },
413 const add_header_func_ptr add_client_headers[] = {
416 client_x_forwarded_adder,
418 client_accept_encoding_adder,
419 connection_close_adder,
424 const add_header_func_ptr add_server_headers[] = {
425 connection_close_adder,
430 /*********************************************************************
432 * Function : flush_socket
434 * Description : Write any pending "buffered" content.
437 * 1 : fd = file descriptor of the socket to read
438 * 2 : csp = Current client state (buffers, headers, etc...)
440 * Returns : On success, the number of bytes written are returned (zero
441 * indicates nothing was written). On error, -1 is returned,
442 * and errno is set appropriately. If count is zero and the
443 * file descriptor refers to a regular file, 0 will be
444 * returned without causing any other effect. For a special
445 * file, the results are not portable.
447 *********************************************************************/
448 int flush_socket(jb_socket fd, struct client_state *csp)
450 struct iob *iob = csp->iob;
451 int len = iob->eod - iob->cur;
458 if (write_socket(fd, iob->cur, len))
462 iob->eod = iob->cur = iob->buf;
468 /*********************************************************************
470 * Function : add_to_iob
472 * Description : Add content to the buffered page.
475 * 1 : csp = Current client state (buffers, headers, etc...)
476 * 2 : buf = holds the content to be added to the page
477 * 3 : n = number of bytes to be added
479 * Returns : Number of bytes in the content buffer.
481 *********************************************************************/
482 size_t add_to_iob(struct client_state *csp, char *buf, size_t n)
484 struct iob *iob = csp->iob;
488 have = iob->eod - iob->cur;
497 if ((p = (char *)malloc(need + 1)) == NULL)
499 log_error(LOG_LEVEL_FATAL, "malloc() iob failed: %E");
504 /* there is something in the buffer - save it */
505 memcpy(p, iob->cur, have);
507 /* replace the buffer with the new space */
511 /* point to the end of the data */
516 /* the buffer is empty, free it and reinitialize */
521 /* copy the new data into the iob buffer */
524 /* point to the end of the data */
527 /* null terminate == cheap insurance */
530 /* set the pointers to the new values */
539 /*********************************************************************
541 * Function : get_header
543 * Description : This (odd) routine will parse the csp->iob
546 * 1 : csp = Current client state (buffers, headers, etc...)
548 * Returns : Any one of the following:
550 * 1) a pointer to a dynamically allocated string that contains a header line
551 * 2) NULL indicating that the end of the header was reached
552 * 3) "" indicating that the end of the iob was reached before finding
553 * a complete header line.
555 *********************************************************************/
556 char *get_header(struct client_state *csp)
562 if ((iob->cur == NULL)
563 || ((p = strchr(iob->cur, '\n')) == NULL))
565 return(""); /* couldn't find a complete header */
570 ret = strdup(iob->cur);
573 /* FIXME No way to handle error properly */
574 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header()");
579 if ((q = strchr(ret, '\r'))) *q = '\0';
581 /* is this a blank linke (i.e. the end of the header) ? */
593 /*********************************************************************
595 * Function : get_header_value
597 * Description : Get the value of a given header from a chained list
598 * of header lines or return NULL if no such header is
599 * present in the list.
602 * 1 : header_list = pointer to list
603 * 2 : header_name = string with name of header to look for.
604 * Trailing colon required, capitalization
607 * Returns : NULL if not found, else value of header
609 *********************************************************************/
610 char *get_header_value(const struct list *header_list, const char *header_name)
612 struct list_entry *cur_entry;
618 length = strlen(header_name);
620 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
624 if (!strncmpic(cur_entry->str, header_name, length))
627 * Found: return pointer to start of value
629 ret = (char *) (cur_entry->str + length);
630 while (*ret && ijb_isspace(*ret)) ret++;
643 /*********************************************************************
647 * Description : add, delete or modify lines in the HTTP header streams.
648 * On entry, it receives a linked list of headers space
649 * that was allocated dynamically (both the list nodes
650 * and the header contents).
652 * As a side effect it frees the space used by the original
656 * 1 : pats = list of patterns to match against headers
657 * 2 : more_headers = list of functions to add more
658 * headers (client or server)
659 * 3 : csp = Current client state (buffers, headers, etc...)
661 * Returns : Single pointer to a fully formed header, or NULL
662 * on out-of-memory error.
664 *********************************************************************/
665 char *sed(const struct parsers pats[],
666 const add_header_func_ptr more_headers[],
667 struct client_state *csp)
669 struct list_entry *p;
670 const struct parsers *v;
671 const add_header_func_ptr *f;
672 jb_err err = JB_ERR_OK;
674 for (v = pats; (err == JB_ERR_OK) && (v->str != NULL) ; v++)
676 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL) ; p = p->next)
678 /* Header crunch()ed in previous run? -> ignore */
679 if (p->str == NULL) continue;
681 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
683 if (strncmpic(p->str, v->str, v->len) == 0)
685 err = v->parser(csp, (char **)&(p->str));
690 /* place any additional headers on the csp->headers list */
691 for (f = more_headers; (err == JB_ERR_OK) && (*f) ; f++)
696 if (err != JB_ERR_OK)
701 return list_to_text(csp->headers);
705 /* here begins the family of parser functions that reformat header lines */
708 /*********************************************************************
712 * Description : This is called if a header matches a pattern to "crunch"
715 * 1 : csp = Current client state (buffers, headers, etc...)
716 * 2 : header = On input, pointer to header to modify.
717 * On output, pointer to the modified header, or NULL
718 * to remove the header. This function frees the
719 * original string if necessary.
721 * Returns : JB_ERR_OK on success, or
722 * JB_ERR_MEMORY on out-of-memory error.
724 *********************************************************************/
725 jb_err crumble(struct client_state *csp, char **header)
727 log_error(LOG_LEVEL_HEADER, "crunch!");
733 /*********************************************************************
735 * Function : server_content_type
737 * Description : Set the content-type for filterable types (text/.*,
738 * javascript and image/gif) unless filtering has been
739 * forbidden (CT_TABOO) while parsing earlier headers.
742 * 1 : csp = Current client state (buffers, headers, etc...)
743 * 2 : header = On input, pointer to header to modify.
744 * On output, pointer to the modified header, or NULL
745 * to remove the header. This function frees the
746 * original string if necessary.
748 * Returns : JB_ERR_OK on success, or
749 * JB_ERR_MEMORY on out-of-memory error.
751 *********************************************************************/
752 jb_err server_content_type(struct client_state *csp, char **header)
754 if (csp->content_type != CT_TABOO)
756 if (strstr(*header, " text/")
757 || strstr(*header, "application/x-javascript"))
758 csp->content_type = CT_TEXT;
759 else if (strstr(*header, " image/gif"))
760 csp->content_type = CT_GIF;
762 csp->content_type = 0;
769 /*********************************************************************
771 * Function : server_transfer_coding
773 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
774 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
775 * - Change from "chunked" to "identity" if body was chunked
776 * but has been de-chunked for filtering.
779 * 1 : csp = Current client state (buffers, headers, etc...)
780 * 2 : header = On input, pointer to header to modify.
781 * On output, pointer to the modified header, or NULL
782 * to remove the header. This function frees the
783 * original string if necessary.
785 * Returns : JB_ERR_OK on success, or
786 * JB_ERR_MEMORY on out-of-memory error.
788 *********************************************************************/
789 jb_err server_transfer_coding(struct client_state *csp, char **header)
792 * Turn off pcrs and gif filtering if body compressed
794 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
796 csp->content_type = CT_TABOO;
800 * Raise flag if body chunked
802 if (strstr(*header, "chunked"))
804 csp->flags |= CSP_FLAG_CHUNKED;
807 * If the body was modified, it has been
808 * de-chunked first, so adjust the header:
810 if (csp->flags & CSP_FLAG_MODIFIED)
813 *header = strdup("Transfer-Encoding: identity");
814 return (header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
822 /*********************************************************************
824 * Function : server_content_encoding
826 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
829 * 1 : csp = Current client state (buffers, headers, etc...)
830 * 2 : header = On input, pointer to header to modify.
831 * On output, pointer to the modified header, or NULL
832 * to remove the header. This function frees the
833 * original string if necessary.
835 * Returns : JB_ERR_OK on success, or
836 * JB_ERR_MEMORY on out-of-memory error.
838 *********************************************************************/
839 jb_err server_content_encoding(struct client_state *csp, char **header)
842 * Turn off pcrs and gif filtering if body compressed
844 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
846 csp->content_type = CT_TABOO;
854 /*********************************************************************
856 * Function : server_content_length
858 * Description : Adjust Content-Length header if we modified
862 * 1 : csp = Current client state (buffers, headers, etc...)
863 * 2 : header = On input, pointer to header to modify.
864 * On output, pointer to the modified header, or NULL
865 * to remove the header. This function frees the
866 * original string if necessary.
868 * Returns : JB_ERR_OK on success, or
869 * JB_ERR_MEMORY on out-of-memory error.
871 *********************************************************************/
872 jb_err server_content_length(struct client_state *csp, char **header)
874 if (csp->content_length != 0) /* Content length has been modified */
877 *header = (char *) zalloc(100);
880 return JB_ERR_MEMORY;
883 sprintf(*header, "Content-Length: %d", (int) csp->content_length);
885 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
892 /*********************************************************************
894 * Function : server_content_md5
896 * Description : Crumble any Content-MD5 headers if the document was
897 * modified. FIXME: Should we re-compute instead?
900 * 1 : csp = Current client state (buffers, headers, etc...)
901 * 2 : header = On input, pointer to header to modify.
902 * On output, pointer to the modified header, or NULL
903 * to remove the header. This function frees the
904 * original string if necessary.
906 * Returns : JB_ERR_OK on success, or
907 * JB_ERR_MEMORY on out-of-memory error.
909 *********************************************************************/
910 jb_err server_content_md5(struct client_state *csp, char **header)
912 if (csp->flags & CSP_FLAG_MODIFIED)
914 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
922 /*********************************************************************
924 * Function : client_accept_encoding
926 * Description : Rewrite the client's Accept-Encoding header so that
927 * if doesn't allow compression, if the action applies.
928 * Note: For HTTP/1.0 the absence of the header is enough.
931 * 1 : csp = Current client state (buffers, headers, etc...)
932 * 2 : header = On input, pointer to header to modify.
933 * On output, pointer to the modified header, or NULL
934 * to remove the header. This function frees the
935 * original string if necessary.
937 * Returns : JB_ERR_OK on success, or
938 * JB_ERR_MEMORY on out-of-memory error.
940 *********************************************************************/
941 jb_err client_accept_encoding(struct client_state *csp, char **header)
943 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
945 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
948 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
950 *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
953 return JB_ERR_MEMORY;
962 /*********************************************************************
964 * Function : client_te
966 * Description : Rewrite the client's TE header so that
967 * if doesn't allow compression, if the action applies.
970 * 1 : csp = Current client state (buffers, headers, etc...)
971 * 2 : header = On input, pointer to header to modify.
972 * On output, pointer to the modified header, or NULL
973 * to remove the header. This function frees the
974 * original string if necessary.
976 * Returns : JB_ERR_OK on success, or
977 * JB_ERR_MEMORY on out-of-memory error.
979 *********************************************************************/
980 jb_err client_te(struct client_state *csp, char **header)
982 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
985 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
991 /*********************************************************************
993 * Function : client_referrer
995 * Description : Handle the "referer" config setting properly.
999 * 1 : csp = Current client state (buffers, headers, etc...)
1000 * 2 : header = On input, pointer to header to modify.
1001 * On output, pointer to the modified header, or NULL
1002 * to remove the header. This function frees the
1003 * original string if necessary.
1005 * Returns : JB_ERR_OK on success, or
1006 * JB_ERR_MEMORY on out-of-memory error.
1008 *********************************************************************/
1009 jb_err client_referrer(struct client_state *csp, char **header)
1011 const char * newval;
1013 #ifdef FEATURE_FORCE_LOAD
1014 /* Since the referrer can include the prefix even
1015 * even if the request itself is non-forced, we must
1016 * clean it unconditionally
1018 strclean(*header, FORCE_PREFIX);
1019 #endif /* def FEATURE_FORCE_LOAD */
1022 * Are we sending referer?
1024 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1031 newval = csp->action->string[ACTION_STRING_REFERER];
1033 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1038 log_error(LOG_LEVEL_HEADER, "crunch!");
1041 else if (0 == strncmpic(newval, "http://", 7))
1044 * We have a specific (fixed) referer we want to send.
1046 log_error(LOG_LEVEL_HEADER, "modified");
1048 *header = strdup("Referer: ");
1049 string_append(header, newval);
1051 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1056 * Forge a referer as http://[hostname:port of REQUEST]/
1057 * to fool stupid checks for in-site links
1059 if (0 != strcmpic(newval, "forge"))
1062 * Invalid choice - but forge is probably the best default.
1064 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1067 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1068 *header = strdup("Referer: http://");
1069 string_append(header, csp->http->hostport);
1070 string_append(header, "/");
1072 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1077 /*********************************************************************
1079 * Function : client_uagent
1081 * Description : Handle the "user-agent" config setting properly
1082 * and remember its original value to enable browser
1083 * bug workarounds. Called from `sed'.
1086 * 1 : csp = Current client state (buffers, headers, etc...)
1087 * 2 : header = On input, pointer to header to modify.
1088 * On output, pointer to the modified header, or NULL
1089 * to remove the header. This function frees the
1090 * original string if necessary.
1092 * Returns : JB_ERR_OK on success, or
1093 * JB_ERR_MEMORY on out-of-memory error.
1095 *********************************************************************/
1096 jb_err client_uagent(struct client_state *csp, char **header)
1098 const char * newval;
1100 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1105 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1111 log_error(LOG_LEVEL_HEADER, "modified");
1114 *header = strdup("User-Agent: ");
1115 string_append(header, newval);
1117 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1121 /*********************************************************************
1123 * Function : client_ua
1125 * Description : Handle "ua-" headers properly. Called from `sed'.
1128 * 1 : csp = Current client state (buffers, headers, etc...)
1129 * 2 : header = On input, pointer to header to modify.
1130 * On output, pointer to the modified header, or NULL
1131 * to remove the header. This function frees the
1132 * original string if necessary.
1134 * Returns : JB_ERR_OK on success, or
1135 * JB_ERR_MEMORY on out-of-memory error.
1137 *********************************************************************/
1138 jb_err client_ua(struct client_state *csp, char **header)
1140 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
1142 log_error(LOG_LEVEL_HEADER, "crunch!");
1150 /*********************************************************************
1152 * Function : client_from
1154 * Description : Handle the "from" config setting properly.
1155 * Called from `sed'.
1158 * 1 : csp = Current client state (buffers, headers, etc...)
1159 * 2 : header = On input, pointer to header to modify.
1160 * On output, pointer to the modified header, or NULL
1161 * to remove the header. This function frees the
1162 * original string if necessary.
1164 * Returns : JB_ERR_OK on success, or
1165 * JB_ERR_MEMORY on out-of-memory error.
1167 *********************************************************************/
1168 jb_err client_from(struct client_state *csp, char **header)
1170 const char * newval;
1172 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1179 newval = csp->action->string[ACTION_STRING_FROM];
1182 * Are we blocking the e-mail address?
1184 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1186 log_error(LOG_LEVEL_HEADER, "crunch!");
1190 log_error(LOG_LEVEL_HEADER, " modified");
1192 *header = strdup("From: ");
1193 string_append(header, newval);
1195 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
1199 /*********************************************************************
1201 * Function : client_send_cookie
1203 * Description : Handle the "cookie" header properly. Called from `sed'.
1204 * If cookie is accepted, add it to the cookie_list,
1205 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1208 * 1 : csp = Current client state (buffers, headers, etc...)
1209 * 2 : header = On input, pointer to header to modify.
1210 * On output, pointer to the modified header, or NULL
1211 * to remove the header. This function frees the
1212 * original string if necessary.
1214 * Returns : JB_ERR_OK on success, or
1215 * JB_ERR_MEMORY on out-of-memory error.
1217 *********************************************************************/
1218 jb_err client_send_cookie(struct client_state *csp, char **header)
1220 jb_err result = JB_ERR_OK;
1222 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1224 /* strlen("cookie: ") == 8 */
1225 result = enlist(csp->cookie_list, *header + 8);
1229 log_error(LOG_LEVEL_HEADER, " crunch!");
1233 * Always remove the cookie here. The cookie header
1234 * will be sent at the end of the header.
1242 /*********************************************************************
1244 * Function : client_x_forwarded
1246 * Description : Handle the "x-forwarded-for" config setting properly,
1247 * also used in the add_client_headers list. Called from `sed'.
1250 * 1 : csp = Current client state (buffers, headers, etc...)
1251 * 2 : header = On input, pointer to header to modify.
1252 * On output, pointer to the modified header, or NULL
1253 * to remove the header. This function frees the
1254 * original string if necessary.
1256 * Returns : JB_ERR_OK on success, or
1257 * JB_ERR_MEMORY on out-of-memory error.
1259 *********************************************************************/
1260 jb_err client_x_forwarded(struct client_state *csp, char **header)
1262 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1264 /* Save it so we can re-add it later */
1265 freez(csp->x_forwarded);
1266 csp->x_forwarded = *header;
1269 * Always set *header = NULL, since this information
1270 * will be sent at the end of the header.
1277 log_error(LOG_LEVEL_HEADER, " crunch!");
1283 /* the following functions add headers directly to the header list */
1285 /*********************************************************************
1287 * Function : client_host_adder
1289 * Description : (re)adds the host header. Called from `sed'.
1292 * 1 : csp = Current client state (buffers, headers, etc...)
1294 * Returns : JB_ERR_OK on success, or
1295 * JB_ERR_MEMORY on out-of-memory error.
1297 *********************************************************************/
1298 jb_err client_host_adder(struct client_state *csp)
1304 if ( !csp->http->hostport || !*(csp->http->hostport))
1309 p = strdup("Host: ");
1311 ** remove 'user:pass@' from 'proto://user:pass@host'
1313 if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
1315 string_append(&p, pos+1);
1319 string_append(&p, csp->http->hostport);
1324 return JB_ERR_MEMORY;
1327 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1329 err = enlist(csp->headers, p);
1337 /*********************************************************************
1339 * Function : client_cookie_adder
1341 * Description : Used in the add_client_headers list. Called from `sed'.
1344 * 1 : csp = Current client state (buffers, headers, etc...)
1346 * Returns : JB_ERR_OK on success, or
1347 * JB_ERR_MEMORY on out-of-memory error.
1349 *********************************************************************/
1350 jb_err client_cookie_adder(struct client_state *csp)
1352 struct list_entry *lst;
1354 struct list_entry *list1 = csp->cookie_list->first;
1355 struct list_entry *list2 = csp->action->multi[ACTION_MULTI_WAFER]->first;
1356 int first_cookie = 1;
1359 if ((list1 == NULL) && (list2 == NULL))
1365 tmp = strdup("Cookie: ");
1367 for (lst = list1; lst ; lst = lst->next)
1375 string_append(&tmp, "; ");
1377 string_append(&tmp, lst->str);
1380 for (lst = list2; lst ; lst = lst->next)
1388 string_append(&tmp, "; ");
1390 string_join(&tmp, cookie_encode(lst->str));
1395 return JB_ERR_MEMORY;
1398 log_error(LOG_LEVEL_HEADER, "addh: %s", tmp);
1399 err = enlist(csp->headers, tmp);
1405 /*********************************************************************
1407 * Function : client_accept_encoding_adder
1409 * Description : Add an Accept-Encoding header to the client's request
1410 * that disables compression if the action applies, and
1411 * the header is not already there. Called from `sed'.
1412 * Note: For HTTP/1.0, the absence of the header is enough.
1415 * 1 : csp = Current client state (buffers, headers, etc...)
1417 * Returns : JB_ERR_OK on success, or
1418 * JB_ERR_MEMORY on out-of-memory error.
1420 *********************************************************************/
1421 jb_err client_accept_encoding_adder(struct client_state *csp)
1423 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1424 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1426 return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1433 /*********************************************************************
1435 * Function : client_xtra_adder
1437 * Description : Used in the add_client_headers list. Called from `sed'.
1440 * 1 : csp = Current client state (buffers, headers, etc...)
1442 * Returns : JB_ERR_OK on success, or
1443 * JB_ERR_MEMORY on out-of-memory error.
1445 *********************************************************************/
1446 jb_err client_xtra_adder(struct client_state *csp)
1448 struct list_entry *lst;
1451 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1452 lst ; lst = lst->next)
1454 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1455 err = enlist(csp->headers, lst->str);
1467 /*********************************************************************
1469 * Function : client_x_forwarded_adder
1471 * Description : Used in the add_client_headers list. Called from `sed'.
1474 * 1 : csp = Current client state (buffers, headers, etc...)
1476 * Returns : JB_ERR_OK on success, or
1477 * JB_ERR_MEMORY on out-of-memory error.
1479 *********************************************************************/
1480 jb_err client_x_forwarded_adder(struct client_state *csp)
1485 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1490 if (csp->x_forwarded)
1492 p = strdup(csp->x_forwarded);
1493 string_append(&p, ", ");
1497 p = strdup("X-Forwarded-For: ");
1499 string_append(&p, csp->ip_addr_str);
1503 return JB_ERR_MEMORY;
1506 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1507 err = enlist(csp->headers, p);
1514 /*********************************************************************
1516 * Function : connection_close_adder
1518 * Description : Adds a "Connection: close" header to csp->headers
1519 * as a temporary fix for the needed but missing HTTP/1.1
1520 * support. Called from `sed'.
1521 * FIXME: This whole function shouldn't be neccessary!
1524 * 1 : csp = Current client state (buffers, headers, etc...)
1526 * Returns : JB_ERR_OK on success, or
1527 * JB_ERR_MEMORY on out-of-memory error.
1529 *********************************************************************/
1530 jb_err connection_close_adder(struct client_state *csp)
1532 return enlist(csp->headers, "Connection: close");
1536 /*********************************************************************
1538 * Function : server_http
1540 * Description : - Save the HTTP Status into csp->http->status
1541 * - Set CT_TABOO to prevent filtering if the answer
1542 * is a partial range (HTTP status 206)
1543 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1547 * 1 : csp = Current client state (buffers, headers, etc...)
1548 * 2 : header = On input, pointer to header to modify.
1549 * On output, pointer to the modified header, or NULL
1550 * to remove the header. This function frees the
1551 * original string if necessary.
1553 * Returns : JB_ERR_OK on success, or
1554 * JB_ERR_MEMORY on out-of-memory error.
1556 *********************************************************************/
1557 jb_err server_http(struct client_state *csp, char **header)
1559 sscanf(*header, "HTTP/%*d.%*d %d", &(csp->http->status));
1560 if (csp->http->status == 206)
1562 csp->content_type = CT_TABOO;
1565 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1568 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1575 /*********************************************************************
1577 * Function : server_set_cookie
1579 * Description : Handle the server "cookie" header properly.
1580 * Log cookie to the jar file. Then "crunch" it,
1581 * or accept it. Called from `sed'.
1584 * 1 : csp = Current client state (buffers, headers, etc...)
1585 * 2 : header = On input, pointer to header to modify.
1586 * On output, pointer to the modified header, or NULL
1587 * to remove the header. This function frees the
1588 * original string if necessary.
1590 * Returns : JB_ERR_OK on success, or
1591 * JB_ERR_MEMORY on out-of-memory error.
1593 *********************************************************************/
1594 jb_err server_set_cookie(struct client_state *csp, char **header)
1596 #ifdef FEATURE_COOKIE_JAR
1597 if (csp->config->jar)
1600 * Write timestamp into outbuf.
1602 * Complex because not all OSs have tm_gmtoff or
1603 * the %z field in strftime()
1605 char tempbuf[ BUFFER_SIZE ];
1609 #ifdef HAVE_LOCALTIME_R
1610 tm_now = *localtime_r(&now, &tm_now);
1612 tm_now = *localtime (&now);
1614 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1616 /* strlen("set-cookie: ") = 12 */
1617 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, *header + 12);
1619 #endif /* def FEATURE_COOKIE_JAR */
1621 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1623 return crumble(csp, header);
1625 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1627 /* Flag whether or not to log a message */
1630 /* A variable to store the tag we're working on */
1633 /* Skip "Set-Cookie:" (11 characters) in header */
1634 cur_tag = *header + 11;
1636 /* skip whitespace between "Set-Cookie:" and value */
1637 while (*cur_tag && ijb_isspace(*cur_tag))
1642 /* Loop through each tag in the cookie */
1646 char * next_tag = strchr(cur_tag, ';');
1647 if (next_tag != NULL)
1649 /* Skip the ';' character itself */
1652 /* skip whitespace ";" and start of tag */
1653 while (*next_tag && ijb_isspace(*next_tag))
1660 /* "Next tag" is the end of the string */
1661 next_tag = cur_tag + strlen(cur_tag);
1664 /* Is this the "Expires" tag? */
1665 if (strncmpic(cur_tag, "expires=", 8) == 0)
1667 /* Delete the tag by copying the rest of the string over it.
1668 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1669 * since the behaviour of strcpy is undefined for overlapping
1672 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1674 /* That changed the header, need to issue a log message */
1677 /* Note that the next tag has now been moved to *cur_tag,
1678 * so we do not need to update the cur_tag pointer.
1683 /* Move on to next cookie tag */
1690 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1698 #ifdef FEATURE_FORCE_LOAD
1699 /*********************************************************************
1701 * Function : strclean
1703 * Description : In-Situ-Eliminate all occurances of substring in
1707 * 1 : string = string to clean
1708 * 2 : substring = substring to eliminate
1710 * Returns : Number of eliminations
1712 *********************************************************************/
1713 int strclean(const char *string, const char *substring)
1715 int hits = 0, len = strlen(substring);
1718 while((pos = strstr(string, substring)))
1725 while (*p++ != '\0');
1732 #endif /* def FEATURE_FORCE_LOAD */