1 const char parsers_rcs[] = "$Id: parsers.c,v 1.45 2002/01/09 14:33:03 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.45 2002/01/09 14:33:03 oes
44 * Added support for localtime_r.
46 * Revision 1.44 2001/12/14 01:22:54 steudten
47 * Remove 'user:pass@' from 'proto://user:pass@host' for the
48 * new added header 'Host: ..'. (See Req ID 491818)
50 * Revision 1.43 2001/11/23 00:26:38 jongfoster
51 * Fixing two really stupid errors in my previous commit
53 * Revision 1.42 2001/11/22 21:59:30 jongfoster
54 * Adding code to handle +no-cookies-keep
56 * Revision 1.41 2001/11/05 23:43:05 steudten
57 * Add time+date to log files.
59 * Revision 1.40 2001/10/26 20:13:09 jongfoster
60 * ctype.h is needed in Windows, too.
62 * Revision 1.39 2001/10/26 17:40:04 oes
63 * Introduced get_header_value()
64 * Removed http->user_agent, csp->referrer and csp->accept_types
65 * Removed client_accept()
67 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
68 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
69 * threads to call select() simultaneously. So, it's time to do a real, live,
70 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
71 * (native). Both versions will work, but using __OS2__ offers multi-threading.
73 * Revision 1.37 2001/10/23 21:36:02 jongfoster
74 * Documenting sed()'s error behaviou (doc change only)
76 * Revision 1.36 2001/10/13 12:51:51 joergs
77 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
78 * force-load), instead crumble Host: and add it (again) in client_host_adder
79 * (in case we get a HTTP/1.0 request without Host: header and forward it to
80 * a HTTP/1.1 server/proxy).
82 * Revision 1.35 2001/10/09 22:39:21 jongfoster
83 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
86 * Revision 1.34 2001/10/07 18:50:55 oes
87 * Added server_content_encoding, renamed server_transfer_encoding
89 * Revision 1.33 2001/10/07 18:04:49 oes
90 * Changed server_http11 to server_http and its pattern to "HTTP".
91 * Additional functionality: it now saves the HTTP status into
92 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
94 * Revision 1.32 2001/10/07 15:43:28 oes
95 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
96 * client_te and client_accept_encoding_adder, triggered by the new
97 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
98 * changed to allow only identity and chunked, and the TE header is
99 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
101 * parse_http_request no longer does anything than parsing. The rewriting
102 * of http->cmd and version mangling are gone. It now also recognizes
103 * the put and delete methods and saves the url in http->url. Removed
106 * renamed content_type and content_length to have the server_ prefix
108 * server_content_type now only works if csp->content_type != CT_TABOO
110 * added server_transfer_encoding, which
111 * - Sets CT_TABOO to prohibit filtering if encoding compresses
112 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
113 * - Change from "chunked" to "identity" if body was chunked
114 * but has been de-chunked for filtering.
116 * added server_content_md5 which crunches any Content-MD5 headers
117 * if the body was modified.
119 * made server_http11 conditional on +downgrade action
121 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
123 * Revision 1.31 2001/10/05 14:25:02 oes
124 * Crumble Keep-Alive from Server
126 * Revision 1.30 2001/09/29 12:56:03 joergs
127 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
129 * Revision 1.29 2001/09/24 21:09:24 jongfoster
130 * Fixing 2 memory leaks that Guy spotted, where the paramater to
131 * enlist() was not being free()d.
133 * Revision 1.28 2001/09/22 16:32:28 jongfoster
134 * Removing unused #includes.
136 * Revision 1.27 2001/09/20 15:45:25 steudten
138 * add casting from size_t to int for printf()
139 * remove local variable shadow s2
141 * Revision 1.26 2001/09/16 17:05:14 jongfoster
142 * Removing unused #include showarg.h
144 * Revision 1.25 2001/09/16 13:21:27 jongfoster
145 * Changes to use new list functions.
147 * Revision 1.24 2001/09/13 23:05:50 jongfoster
148 * Changing the string paramater to the header parsers a "const".
150 * Revision 1.23 2001/09/12 18:08:19 steudten
152 * In parse_http_request() header rewriting miss the host value, so
153 * from http://www.mydomain.com the result was just " / " not
154 * http://www.mydomain.com/ in case we forward.
156 * Revision 1.22 2001/09/10 10:58:53 oes
157 * Silenced compiler warnings
159 * Revision 1.21 2001/07/31 14:46:00 oes
160 * - Persistant connections now suppressed
161 * - sed() no longer appends empty header to csp->headers
163 * Revision 1.20 2001/07/30 22:08:36 jongfoster
164 * Tidying up #defines:
165 * - All feature #defines are now of the form FEATURE_xxx
166 * - Permanently turned off WIN_GUI_EDIT
167 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
169 * Revision 1.19 2001/07/25 17:21:54 oes
170 * client_uagent now saves copy of User-Agent: header value
172 * Revision 1.18 2001/07/13 14:02:46 oes
173 * - Included fix to repair broken HTTP requests that
174 * don't contain a path, not even '/'.
175 * - Removed all #ifdef PCRS
176 * - content_type now always inspected and classified as
177 * text, gif or other.
178 * - formatting / comments
180 * Revision 1.17 2001/06/29 21:45:41 oes
181 * Indentation, CRLF->LF, Tab-> Space
183 * Revision 1.16 2001/06/29 13:32:42 oes
185 * - Adapted free_http_request
186 * - Removed logentry from cancelled commit
188 * Revision 1.15 2001/06/03 19:12:38 oes
189 * deleted const struct interceptors
191 * Revision 1.14 2001/06/01 18:49:17 jongfoster
192 * Replaced "list_share" with "list" - the tiny memory gain was not
193 * worth the extra complexity.
195 * Revision 1.13 2001/05/31 21:30:33 jongfoster
196 * Removed list code - it's now in list.[ch]
197 * Renamed "permission" to "action", and changed many features
198 * to use the actions file rather than the global config.
200 * Revision 1.12 2001/05/31 17:33:13 oes
204 * Revision 1.11 2001/05/29 20:11:19 joergs
205 * '/ * inside comment' warning removed.
207 * Revision 1.10 2001/05/29 09:50:24 jongfoster
208 * Unified blocklist/imagelist/permissionslist.
209 * File format is still under discussion, but the internal changes
212 * Also modified interceptor behaviour:
213 * - We now intercept all URLs beginning with one of the following
214 * prefixes (and *only* these prefixes):
216 * * http://ijbswa.sf.net/config/
217 * * http://ijbswa.sourceforge.net/config/
218 * - New interceptors "home page" - go to http://i.j.b/ to see it.
219 * - Internal changes so that intercepted and fast redirect pages
220 * are not replaced with an image.
221 * - Interceptors now have the option to send a binary page direct
222 * to the client. (i.e. ijb-send-banner uses this)
223 * - Implemented show-url-info interceptor. (Which is why I needed
224 * the above interceptors changes - a typical URL is
225 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
226 * The previous mechanism would not have intercepted that, and
227 * if it had been intercepted then it then it would have replaced
230 * Revision 1.9 2001/05/28 17:26:33 jongfoster
231 * Fixing segfault if last header was crunched.
232 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
233 * can use the cross-platform sprintf() instead.)
235 * Revision 1.8 2001/05/27 22:17:04 oes
237 * - re_process_buffer no longer writes the modified buffer
238 * to the client, which was very ugly. It now returns the
239 * buffer, which it is then written by chat.
241 * - content_length now adjusts the Content-Length: header
242 * for modified documents rather than crunch()ing it.
243 * (Length info in csp->content_length, which is 0 for
244 * unmodified documents)
246 * - For this to work, sed() is called twice when filtering.
248 * Revision 1.7 2001/05/27 13:19:06 oes
249 * Patched Joergs solution for the content-length in.
251 * Revision 1.6 2001/05/26 13:39:32 jongfoster
252 * Only crunches Content-Length header if applying RE filtering.
253 * Without this fix, Microsoft Windows Update wouldn't work.
255 * Revision 1.5 2001/05/26 00:28:36 jongfoster
256 * Automatic reloading of config file.
257 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
258 * Most of the global variables have been moved to a new
259 * struct configuration_spec, accessed through csp->config->globalname
260 * Most of the globals remaining are used by the Win32 GUI.
262 * Revision 1.4 2001/05/22 18:46:04 oes
264 * - Enabled filtering banners by size rather than URL
265 * by adding patterns that replace all standard banner
266 * sizes with the "Junkbuster" gif to the re_filterfile
268 * - Enabled filtering WebBugs by providing a pattern
269 * which kills all 1x1 images
271 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
272 * which is selected by the (nonstandard and therefore
273 * capital) letter 'U' in the option string.
274 * It causes the quantifiers to be ungreedy by default.
275 * Appending a ? turns back to greedy (!).
277 * - Added a new interceptor ijb-send-banner, which
278 * sends back the "Junkbuster" gif. Without imagelist or
279 * MSIE detection support, or if tinygif = 1, or the
280 * URL isn't recognized as an imageurl, a lame HTML
281 * explanation is sent instead.
283 * - Added new feature, which permits blocking remote
284 * script redirects and firing back a local redirect
286 * The feature is conditionally compiled, i.e. it
287 * can be disabled with --disable-fast-redirects,
288 * plus it must be activated by a "fast-redirects"
289 * line in the config file, has its own log level
290 * and of course wants to be displayed by show-proxy-args
291 * Note: Boy, all the #ifdefs in 1001 locations and
292 * all the fumbling with configure.in and acconfig.h
293 * were *way* more work than the feature itself :-(
295 * - Because a generic redirect template was needed for
296 * this, tinygif = 3 now uses the same.
298 * - Moved GIFs, and other static HTTP response templates
303 * - Removed some >400 CRs again (Jon, you really worked
306 * Revision 1.3 2001/05/20 01:21:20 jongfoster
307 * Version 2.9.4 checkin.
308 * - Merged popupfile and cookiefile, and added control over PCRS
309 * filtering, in new "permissionsfile".
310 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
311 * file error you now get a message box (in the Win32 GUI) rather
312 * than the program exiting with no explanation.
313 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
315 * - Removed tabs from "config"
316 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
317 * - Bumped up version number.
319 * Revision 1.2 2001/05/17 23:02:36 oes
320 * - Made referrer option accept 'L' as a substitute for '§'
322 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
323 * Initial import of version 2.9.3 source tree
326 *********************************************************************/
333 #include <sys/types.h>
341 #if !defined(_WIN32) && !defined(__OS2__)
351 #include "jbsockets.h"
352 #include "miscutil.h"
355 const char parsers_h_rcs[] = PARSERS_H_VERSION;
357 /* Fix a problem with Solaris. There should be no effect on other
359 * Solaris's isspace() is a macro which uses it's argument directly
360 * as an array index. Therefore we need to make sure that high-bit
361 * characters generate +ve values, and ideally we also want to make
362 * the argument match the declared parameter type of "int".
364 * Why did they write a character function that can't take a simple
365 * "char" argument? Doh!
367 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
368 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
371 const struct parsers client_patterns[] = {
372 { "referer:", 8, client_referrer },
373 { "user-agent:", 11, client_uagent },
374 { "ua-", 3, client_ua },
375 { "from:", 5, client_from },
376 { "cookie:", 7, client_send_cookie },
377 { "x-forwarded-for:", 16, client_x_forwarded },
378 { "Accept-Encoding:", 16, client_accept_encoding },
379 { "TE:", 3, client_te },
380 { "Host:", 5, crumble },
381 /* { "if-modified-since:", 18, crumble }, */
382 { "Keep-Alive:", 11, crumble },
383 { "connection:", 11, crumble },
384 { "proxy-connection:", 17, crumble },
389 const struct parsers server_patterns[] = {
390 { "HTTP", 4, server_http },
391 { "set-cookie:", 11, server_set_cookie },
392 { "connection:", 11, crumble },
393 { "Content-Type:", 13, server_content_type },
394 { "Content-Length:", 15, server_content_length },
395 { "Content-MD5:", 12, server_content_md5 },
396 { "Content-Encoding:", 17, server_content_encoding },
397 { "Transfer-Encoding:", 18, server_transfer_coding },
398 { "Keep-Alive:", 11, crumble },
403 void (* const add_client_headers[])(struct client_state *) = {
406 client_x_forwarded_adder,
408 client_accept_encoding_adder,
409 connection_close_adder,
414 void (* const add_server_headers[])(struct client_state *) = {
415 connection_close_adder,
420 /*********************************************************************
422 * Function : flush_socket
424 * Description : Write any pending "buffered" content.
427 * 1 : fd = file descriptor of the socket to read
428 * 2 : csp = Current client state (buffers, headers, etc...)
430 * Returns : On success, the number of bytes written are returned (zero
431 * indicates nothing was written). On error, -1 is returned,
432 * and errno is set appropriately. If count is zero and the
433 * file descriptor refers to a regular file, 0 will be
434 * returned without causing any other effect. For a special
435 * file, the results are not portable.
437 *********************************************************************/
438 int flush_socket(int fd, struct client_state *csp)
440 struct iob *iob = csp->iob;
441 int n = iob->eod - iob->cur;
448 n = write_socket(fd, iob->cur, n);
449 iob->eod = iob->cur = iob->buf;
455 /*********************************************************************
457 * Function : add_to_iob
459 * Description : Add content to the buffered page.
462 * 1 : csp = Current client state (buffers, headers, etc...)
463 * 2 : buf = holds the content to be added to the page
464 * 3 : n = number of bytes to be added
466 * Returns : Number of bytes in the content buffer.
468 *********************************************************************/
469 int add_to_iob(struct client_state *csp, char *buf, int n)
471 struct iob *iob = csp->iob;
475 have = iob->eod - iob->cur;
484 if ((p = (char *)malloc(need + 1)) == NULL)
486 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
492 /* there is something in the buffer - save it */
493 memcpy(p, iob->cur, have);
495 /* replace the buffer with the new space */
499 /* point to the end of the data */
504 /* the buffer is empty, free it and reinitialize */
509 /* copy the new data into the iob buffer */
512 /* point to the end of the data */
515 /* null terminate == cheap insurance */
518 /* set the pointers to the new values */
527 /*********************************************************************
529 * Function : get_header
531 * Description : This (odd) routine will parse the csp->iob
534 * 1 : csp = Current client state (buffers, headers, etc...)
536 * Returns : Any one of the following:
538 * 1) a pointer to a dynamically allocated string that contains a header line
539 * 2) NULL indicating that the end of the header was reached
540 * 3) "" indicating that the end of the iob was reached before finding
541 * a complete header line.
543 *********************************************************************/
544 char *get_header(struct client_state *csp)
550 if ((iob->cur == NULL)
551 || ((p = strchr(iob->cur, '\n')) == NULL))
553 return(""); /* couldn't find a complete header */
558 ret = strdup(iob->cur);
562 if ((q = strchr(ret, '\r'))) *q = '\0';
564 /* is this a blank linke (i.e. the end of the header) ? */
576 /*********************************************************************
578 * Function : get_header_value
580 * Description : Get the value of a given header from a chained list
581 * of header lines or return NULL if no such header is
582 * present in the list.
585 * 1 : header_list = pointer to list
586 * 2 : header_name = string with name of header to look for.
587 * Trailing colon required, capitalization
590 * Returns : NULL if not found, else value of header
592 *********************************************************************/
593 char *get_header_value(const struct list *header_list, const char *header_name)
595 struct list_entry *cur_entry;
601 length = strlen(header_name);
603 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
607 if (!strncmpic(cur_entry->str, header_name, length))
610 * Found: return pointer to start of value
612 ret = (char *) (cur_entry->str + length);
613 while (*ret && ijb_isspace(*ret)) ret++;
626 /*********************************************************************
630 * Description : add, delete or modify lines in the HTTP header streams.
631 * On entry, it receives a linked list of headers space
632 * that was allocated dynamically (both the list nodes
633 * and the header contents).
635 * As a side effect it frees the space used by the original
639 * 1 : pats = list of patterns to match against headers
640 * 2 : more_headers = list of functions to add more
641 * headers (client or server)
642 * 3 : csp = Current client state (buffers, headers, etc...)
644 * Returns : Single pointer to a fully formed header, or NULL
645 * on out-of-memory error.
647 *********************************************************************/
648 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
650 struct list_entry *p;
651 const struct parsers *v;
655 for (v = pats; v->str ; v++)
657 for (p = csp->headers->first; p ; p = p->next)
659 /* Header crunch()ed in previous run? -> ignore */
660 if (p->str == NULL) continue;
662 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
664 if (strncmpic(p->str, v->str, v->len) == 0)
666 hdr = v->parser(v, p->str, csp);
667 freez(p->str); /* FIXME: Yuck! patching a list...*/
673 /* place any additional headers on the csp->headers list */
674 for (f = more_headers; *f ; f++)
679 hdr = list_to_text(csp->headers);
686 /* here begins the family of parser functions that reformat header lines */
689 /*********************************************************************
693 * Description : This is called if a header matches a pattern to "crunch"
696 * 1 : v = Pointer to parsers structure, which basically holds
697 * headers (client or server) that we want to "crunch"
698 * 2 : s = header (from sed) to "crunch"
699 * 3 : csp = Current client state (buffers, headers, etc...)
701 * Returns : Always NULL.
703 *********************************************************************/
704 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
706 log_error(LOG_LEVEL_HEADER, "crunch!");
712 /*********************************************************************
714 * Function : server_content_type
716 * Description : Set the content-type for filterable types (text/.*,
717 * javascript and image/gif) unless filtering has been
718 * forbidden (CT_TABOO) while parsing earlier headers.
722 * 2 : s = header string we are "considering"
723 * 3 : csp = Current client state (buffers, headers, etc...)
725 * Returns : A duplicate string pointer to this header (ie. pass thru)
727 *********************************************************************/
728 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
730 if (csp->content_type != CT_TABOO)
732 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
733 csp->content_type = CT_TEXT;
734 else if (strstr(s, " image/gif"))
735 csp->content_type = CT_GIF;
737 csp->content_type = 0;
745 /*********************************************************************
747 * Function : server_transfer_coding
749 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
750 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
751 * - Change from "chunked" to "identity" if body was chunked
752 * but has been de-chunked for filtering.
756 * 2 : s = header string we are "considering"
757 * 3 : csp = Current client state (buffers, headers, etc...)
759 * Returns : A duplicate string pointer to this header (ie. pass thru)
761 *********************************************************************/
762 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
765 * Turn off pcrs and gif filtering if body compressed
767 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
769 csp->content_type = CT_TABOO;
773 * Raise flag if body chunked
775 if (strstr(s, "chunked"))
777 csp->flags |= CSP_FLAG_CHUNKED;
780 * If the body was modified, it has been
781 * de-chunked first, so adjust the header:
783 if (csp->flags & CSP_FLAG_MODIFIED)
785 return(strdup("Transfer-Encoding: identity"));
794 /*********************************************************************
796 * Function : server_content_encoding
798 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
802 * 2 : s = header string we are "considering"
803 * 3 : csp = Current client state (buffers, headers, etc...)
805 * Returns : A duplicate string pointer to this header (ie. pass thru)
807 *********************************************************************/
808 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
811 * Turn off pcrs and gif filtering if body compressed
813 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
815 csp->content_type = CT_TABOO;
823 /*********************************************************************
825 * Function : server_content_length
827 * Description : Adjust Content-Length header if we modified
832 * 2 : s = header string we are "considering"
833 * 3 : csp = Current client state (buffers, headers, etc...)
835 * Returns : A duplicate string pointer to this header (ie. pass thru)
837 *********************************************************************/
838 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
840 if (csp->content_length != 0) /* Content length has been modified */
842 char * s2 = (char *) zalloc(100);
843 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
845 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
856 /*********************************************************************
858 * Function : server_content_md5
860 * Description : Crumble any Content-MD5 headers if the document was
861 * modified. FIXME: Should we re-compute instead?
865 * 2 : s = header string we are "considering"
866 * 3 : csp = Current client state (buffers, headers, etc...)
868 * Returns : A duplicate string pointer to this header (ie. pass thru)
870 *********************************************************************/
871 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
873 if (csp->flags & CSP_FLAG_MODIFIED)
875 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
886 /*********************************************************************
888 * Function : client_accept_encoding
890 * Description : Rewrite the client's Accept-Encoding header so that
891 * if doesn't allow compression, if the action applies.
892 * Note: For HTTP/1.0 the absence of the header is enough.
896 * 2 : s = header string we are "considering"
897 * 3 : csp = Current client state (buffers, headers, etc...)
899 * Returns : A copy of the client's original or the modified header.
901 *********************************************************************/
902 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
904 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
910 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
912 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
914 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
925 /*********************************************************************
927 * Function : client_te
929 * Description : Rewrite the client's TE header so that
930 * if doesn't allow compression, if the action applies.
934 * 2 : s = header string we are "considering"
935 * 3 : csp = Current client state (buffers, headers, etc...)
937 * Returns : A copy of the client's original or the modified header.
939 *********************************************************************/
940 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
942 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
948 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
954 /*********************************************************************
956 * Function : client_referrer
958 * Description : Handle the "referer" config setting properly.
963 * 2 : s = header (from sed) to "crunch"
964 * 3 : csp = Current client state (buffers, headers, etc...)
966 * Returns : NULL if crunched, or a malloc'ed string with the original
969 *********************************************************************/
970 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
974 #ifdef FEATURE_FORCE_LOAD
975 /* Since the referrer can include the prefix even
976 * even if the request itself is non-forced, we must
977 * clean it unconditionally
979 strclean(s, FORCE_PREFIX);
980 #endif /* def FEATURE_FORCE_LOAD */
983 * Are we sending referer?
985 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
990 newval = csp->action->string[ACTION_STRING_REFERER];
993 * Are we blocking referer?
995 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
997 log_error(LOG_LEVEL_HEADER, "crunch!");
1002 * Are we forging referer?
1004 if (0 == strcmpic(newval, "forge"))
1007 * Forge a referer as http://[hostname:port of REQUEST]/
1008 * to fool stupid checks for in-site links
1010 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1011 s2 = strsav(NULL, "Referer: ");
1012 s2 = strsav(s2, "http://");
1013 s2 = strsav(s2, csp->http->hostport);
1014 s2 = strsav(s2, "/");
1019 * Have we got a fixed referer?
1021 if (0 == strncmpic(newval, "http://", 7))
1024 * We have a specific (fixed) referer we want to send.
1028 log_error(LOG_LEVEL_HEADER, "modified");
1030 s3 = strsav( NULL, "Referer: " );
1031 s3 = strsav( s3, newval );
1035 /* Should never get here! */
1036 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1039 * Forge is probably the best default.
1041 * Forge a referer as http://[hostname:port of REQUEST]/
1042 * to fool stupid checks for in-site links
1044 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1045 s2 = strsav(NULL, "Referer: ");
1046 s2 = strsav(s2, "http://");
1047 s2 = strsav(s2, csp->http->hostport);
1048 s2 = strsav(s2, "/");
1053 /*********************************************************************
1055 * Function : client_uagent
1057 * Description : Handle the "user-agent" config setting properly
1058 * and remember its original value to enable browser
1059 * bug workarounds. Called from `sed'.
1063 * 2 : s = header (from sed) to "crunch"
1064 * 3 : csp = Current client state (buffers, headers, etc...)
1066 * Returns : A malloc'ed pointer to the default agent, or
1067 * a malloc'ed string pointer to this header (ie. pass thru).
1069 *********************************************************************/
1070 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1072 const char * newval;
1075 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1080 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1086 log_error(LOG_LEVEL_HEADER, "modified");
1088 s2 = strsav( NULL, "User-Agent: " );
1089 s2 = strsav( s2, newval );
1095 /*********************************************************************
1097 * Function : client_ua
1099 * Description : Handle "ua-" headers properly. Called from `sed'.
1103 * 2 : s = header (from sed) to "crunch"
1104 * 3 : csp = Current client state (buffers, headers, etc...)
1106 * Returns : NULL if crunched, or a malloc'ed string to original header
1108 *********************************************************************/
1109 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1111 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1117 log_error(LOG_LEVEL_HEADER, "crunch!");
1123 /*********************************************************************
1125 * Function : client_from
1127 * Description : Handle the "from" config setting properly.
1128 * Called from `sed'.
1132 * 2 : s = header (from sed) to "crunch"
1133 * 3 : csp = Current client state (buffers, headers, etc...)
1135 * Returns : NULL if crunched, or a malloc'ed string to
1136 * modified/original header.
1138 *********************************************************************/
1139 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1141 const char * newval;
1144 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1149 newval = csp->action->string[ACTION_STRING_FROM];
1152 * Are we blocking referer?
1154 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1156 log_error(LOG_LEVEL_HEADER, "crunch!");
1160 log_error(LOG_LEVEL_HEADER, " modified");
1162 s2 = strsav( NULL, "From: " );
1163 s2 = strsav( s2, newval );
1169 /*********************************************************************
1171 * Function : client_send_cookie
1173 * Description : Handle the "cookie" header properly. Called from `sed'.
1174 * If cookie is accepted, add it to the cookie_list,
1175 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1178 * 1 : v = pattern of cookie `sed' found matching
1179 * 2 : s = header (from sed) to "crunch"
1180 * 3 : csp = Current client state (buffers, headers, etc...)
1182 * Returns : Always NULL.
1184 *********************************************************************/
1185 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1187 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1189 enlist(csp->cookie_list, s + v->len + 1);
1193 log_error(LOG_LEVEL_HEADER, " crunch!");
1197 * Always return NULL here. The cookie header
1198 * will be sent at the end of the header.
1205 /*********************************************************************
1207 * Function : client_x_forwarded
1209 * Description : Handle the "x-forwarded-for" config setting properly,
1210 * also used in the add_client_headers list. Called from `sed'.
1214 * 2 : s = header (from sed) to "crunch"
1215 * 3 : csp = Current client state (buffers, headers, etc...)
1217 * Returns : Always NULL.
1219 *********************************************************************/
1220 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1222 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1224 /* Save it so we can re-add it later */
1225 csp->x_forwarded = strdup(s);
1229 * Always return NULL, since this information
1230 * will be sent at the end of the header.
1237 /* the following functions add headers directly to the header list */
1239 /*********************************************************************
1241 * Function : client_host_adder
1243 * Description : (re)adds the host header. Called from `sed'.
1246 * 1 : csp = Current client state (buffers, headers, etc...)
1250 *********************************************************************/
1251 void client_host_adder(struct client_state *csp)
1256 if ( !csp->http->hostport || !*(csp->http->hostport)) return;
1257 p = strsav(p, "Host: ");
1259 ** remove 'user:pass@' from 'proto://user:pass@host'
1261 if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
1263 p = strsav(p, pos+1);
1267 p = strsav(p, csp->http->hostport);
1269 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1270 enlist(csp->headers, p);
1276 /*********************************************************************
1278 * Function : client_cookie_adder
1280 * Description : Used in the add_client_headers list. Called from `sed'.
1283 * 1 : csp = Current client state (buffers, headers, etc...)
1287 *********************************************************************/
1288 void client_cookie_adder(struct client_state *csp)
1290 struct list_entry *lst;
1294 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1298 tmp = strsav(tmp, "; ");
1300 tmp = strsav(tmp, lst->str);
1303 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1307 tmp = strsav(tmp, "; ");
1310 if ((e = cookie_encode(lst->str)))
1312 tmp = strsav(tmp, e);
1321 ret = strdup("Cookie: ");
1322 ret = strsav(ret, tmp);
1323 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1324 enlist(csp->headers, ret);
1332 /*********************************************************************
1334 * Function : client_accept_encoding_adder
1336 * Description : Add an Accept-Encoding header to the client's request
1337 * that disables compression if the action applies, and
1338 * the header is not already there. Called from `sed'.
1339 * Note: For HTTP/1.0, the absence of the header is enough.
1342 * 1 : csp = Current client state (buffers, headers, etc...)
1346 *********************************************************************/
1347 void client_accept_encoding_adder(struct client_state *csp)
1349 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1350 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1352 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1358 /*********************************************************************
1360 * Function : client_xtra_adder
1362 * Description : Used in the add_client_headers list. Called from `sed'.
1365 * 1 : csp = Current client state (buffers, headers, etc...)
1369 *********************************************************************/
1370 void client_xtra_adder(struct client_state *csp)
1372 struct list_entry *lst;
1374 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1375 lst ; lst = lst->next)
1377 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1378 enlist(csp->headers, lst->str);
1384 /*********************************************************************
1386 * Function : client_x_forwarded_adder
1388 * Description : Used in the add_client_headers list. Called from `sed'.
1391 * 1 : csp = Current client state (buffers, headers, etc...)
1395 *********************************************************************/
1396 void client_x_forwarded_adder(struct client_state *csp)
1400 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1405 if (csp->x_forwarded)
1407 p = strsav(p, csp->x_forwarded);
1408 p = strsav(p, ", ");
1409 p = strsav(p, csp->ip_addr_str);
1413 p = strsav(p, "X-Forwarded-For: ");
1414 p = strsav(p, csp->ip_addr_str);
1417 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1418 enlist(csp->headers, p);
1424 /*********************************************************************
1426 * Function : connection_close_adder
1428 * Description : Adds a "Connection: close" header to csp->headers
1429 * as a temporary fix for the needed but missing HTTP/1.1
1430 * support. Called from `sed'.
1431 * FIXME: This whole function shouldn't be neccessary!
1434 * 1 : csp = Current client state (buffers, headers, etc...)
1438 *********************************************************************/
1439 void connection_close_adder(struct client_state *csp)
1441 enlist(csp->headers, "Connection: close");
1445 /*********************************************************************
1447 * Function : server_http
1449 * Description : - Save the HTTP Status into csp->http->status
1450 * - Set CT_TABOO to prevent filtering if the answer
1451 * is a partial range (HTTP status 206)
1452 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1456 * 1 : v = parser pattern that matched this header
1457 * 2 : s = header that matched this pattern
1458 * 3 : csp = Current client state (buffers, headers, etc...)
1460 * Returns : Copy of changed or original answer.
1462 *********************************************************************/
1463 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1465 char *ret = strdup(s);
1467 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1468 if (csp->http->status == 206)
1470 csp->content_type = CT_TABOO;
1473 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1476 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1483 /*********************************************************************
1485 * Function : server_set_cookie
1487 * Description : Handle the server "cookie" header properly.
1488 * Log cookie to the jar file. Then "crunch" it,
1489 * or accept it. Called from `sed'.
1492 * 1 : v = parser pattern that matched this header
1493 * 2 : s = header that matched this pattern
1494 * 3 : csp = Current client state (buffers, headers, etc...)
1496 * Returns : `crumble' or a newly malloc'ed string.
1498 *********************************************************************/
1499 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1501 #ifdef FEATURE_COOKIE_JAR
1502 if (csp->config->jar)
1505 * Write timestamp into outbuf.
1507 * Complex because not all OSs have tm_gmtoff or
1508 * the %z field in strftime()
1510 char tempbuf[ BUFFER_SIZE ];
1514 #ifdef HAVE_LOCALTIME_R
1515 tm_now = *localtime_r(&now, &tm_now);
1517 tm_now = *localtime (&now);
1519 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1521 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, (s + v->len + 1));
1523 #endif /* def FEATURE_COOKIE_JAR */
1525 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1527 return(crumble(v, s, csp));
1529 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1531 /* Flag whether or not to log a message */
1534 /* A variable to store the tag we're working on */
1537 /* Make a copy of the header we can write to */
1538 char * result = strdup(s);
1541 /* FIXME: This error handling is incorrect */
1545 /* Skip "Set-Cookie:" (11 characters) in header */
1546 cur_tag = result + 11;
1548 /* skip whitespace between "Set-Cookie:" and value */
1549 while (*cur_tag && ijb_isspace(*cur_tag))
1554 /* Loop through each tag in the cookie */
1558 char * next_tag = strchr(cur_tag, ';');
1559 if (next_tag != NULL)
1561 /* Skip the ';' character itself */
1564 /* skip whitespace ";" and start of tag */
1565 while (*next_tag && ijb_isspace(*next_tag))
1572 /* "Next tag" is the end of the string */
1573 next_tag = cur_tag + strlen(cur_tag);
1576 /* Is this the "Expires" tag? */
1577 if (strncmpic(cur_tag, "expires=", 8) == 0)
1579 /* Delete the tag by copying the rest of the string over it.
1580 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1581 * since the behaviour of strcpy is undefined for overlapping
1584 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1586 /* That changed the header, need to issue a log message */
1589 /* Note that the next tag has now been moved to *cur_tag,
1590 * so we do not need to update the cur_tag pointer.
1595 /* Move on to next cookie tag */
1602 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1612 #ifdef FEATURE_FORCE_LOAD
1613 /*********************************************************************
1615 * Function : strclean
1617 * Description : In-Situ-Eliminate all occurances of substring in
1621 * 1 : string = string to clean
1622 * 2 : substring = substring to eliminate
1624 * Returns : Number of eliminations
1626 *********************************************************************/
1627 int strclean(const char *string, const char *substring)
1629 int hits = 0, len = strlen(substring);
1632 while((pos = strstr(string, substring)))
1639 while (*p++ != '\0');
1646 #endif /* def FEATURE_FORCE_LOAD */