1 const char parsers_rcs[] = "$Id: parsers.c,v 1.44 2001/12/14 01:22:54 steudten 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', `free_http_request', `get_header',
14 * `list_to_text', `parse_http_request', `sed',
15 * and `server_set_cookie'.
17 * Copyright : Written by and Copyright (C) 2001 the SourceForge
18 * IJBSWA team. http://ijbswa.sourceforge.net
20 * Based on the Internet Junkbuster originally written
21 * by and Copyright (C) 1997 Anonymous Coders and
22 * Junkbusters Corporation. http://www.junkbusters.com
24 * This program is free software; you can redistribute it
25 * and/or modify it under the terms of the GNU General
26 * Public License as published by the Free Software
27 * Foundation; either version 2 of the License, or (at
28 * your option) any later version.
30 * This program is distributed in the hope that it will
31 * be useful, but WITHOUT ANY WARRANTY; without even the
32 * implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU General Public
34 * License for more details.
36 * The GNU General Public License should be included with
37 * this file. If not, you can view it at
38 * http://www.gnu.org/copyleft/gpl.html
39 * or write to the Free Software Foundation, Inc., 59
40 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 * Revision 1.44 2001/12/14 01:22:54 steudten
45 * Remove 'user:pass@' from 'proto://user:pass@host' for the
46 * new added header 'Host: ..'. (See Req ID 491818)
48 * Revision 1.43 2001/11/23 00:26:38 jongfoster
49 * Fixing two really stupid errors in my previous commit
51 * Revision 1.42 2001/11/22 21:59:30 jongfoster
52 * Adding code to handle +no-cookies-keep
54 * Revision 1.41 2001/11/05 23:43:05 steudten
55 * Add time+date to log files.
57 * Revision 1.40 2001/10/26 20:13:09 jongfoster
58 * ctype.h is needed in Windows, too.
60 * Revision 1.39 2001/10/26 17:40:04 oes
61 * Introduced get_header_value()
62 * Removed http->user_agent, csp->referrer and csp->accept_types
63 * Removed client_accept()
65 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
66 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
67 * threads to call select() simultaneously. So, it's time to do a real, live,
68 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
69 * (native). Both versions will work, but using __OS2__ offers multi-threading.
71 * Revision 1.37 2001/10/23 21:36:02 jongfoster
72 * Documenting sed()'s error behaviou (doc change only)
74 * Revision 1.36 2001/10/13 12:51:51 joergs
75 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
76 * force-load), instead crumble Host: and add it (again) in client_host_adder
77 * (in case we get a HTTP/1.0 request without Host: header and forward it to
78 * a HTTP/1.1 server/proxy).
80 * Revision 1.35 2001/10/09 22:39:21 jongfoster
81 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
84 * Revision 1.34 2001/10/07 18:50:55 oes
85 * Added server_content_encoding, renamed server_transfer_encoding
87 * Revision 1.33 2001/10/07 18:04:49 oes
88 * Changed server_http11 to server_http and its pattern to "HTTP".
89 * Additional functionality: it now saves the HTTP status into
90 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
92 * Revision 1.32 2001/10/07 15:43:28 oes
93 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
94 * client_te and client_accept_encoding_adder, triggered by the new
95 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
96 * changed to allow only identity and chunked, and the TE header is
97 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
99 * parse_http_request no longer does anything than parsing. The rewriting
100 * of http->cmd and version mangling are gone. It now also recognizes
101 * the put and delete methods and saves the url in http->url. Removed
104 * renamed content_type and content_length to have the server_ prefix
106 * server_content_type now only works if csp->content_type != CT_TABOO
108 * added server_transfer_encoding, which
109 * - Sets CT_TABOO to prohibit filtering if encoding compresses
110 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
111 * - Change from "chunked" to "identity" if body was chunked
112 * but has been de-chunked for filtering.
114 * added server_content_md5 which crunches any Content-MD5 headers
115 * if the body was modified.
117 * made server_http11 conditional on +downgrade action
119 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
121 * Revision 1.31 2001/10/05 14:25:02 oes
122 * Crumble Keep-Alive from Server
124 * Revision 1.30 2001/09/29 12:56:03 joergs
125 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
127 * Revision 1.29 2001/09/24 21:09:24 jongfoster
128 * Fixing 2 memory leaks that Guy spotted, where the paramater to
129 * enlist() was not being free()d.
131 * Revision 1.28 2001/09/22 16:32:28 jongfoster
132 * Removing unused #includes.
134 * Revision 1.27 2001/09/20 15:45:25 steudten
136 * add casting from size_t to int for printf()
137 * remove local variable shadow s2
139 * Revision 1.26 2001/09/16 17:05:14 jongfoster
140 * Removing unused #include showarg.h
142 * Revision 1.25 2001/09/16 13:21:27 jongfoster
143 * Changes to use new list functions.
145 * Revision 1.24 2001/09/13 23:05:50 jongfoster
146 * Changing the string paramater to the header parsers a "const".
148 * Revision 1.23 2001/09/12 18:08:19 steudten
150 * In parse_http_request() header rewriting miss the host value, so
151 * from http://www.mydomain.com the result was just " / " not
152 * http://www.mydomain.com/ in case we forward.
154 * Revision 1.22 2001/09/10 10:58:53 oes
155 * Silenced compiler warnings
157 * Revision 1.21 2001/07/31 14:46:00 oes
158 * - Persistant connections now suppressed
159 * - sed() no longer appends empty header to csp->headers
161 * Revision 1.20 2001/07/30 22:08:36 jongfoster
162 * Tidying up #defines:
163 * - All feature #defines are now of the form FEATURE_xxx
164 * - Permanently turned off WIN_GUI_EDIT
165 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
167 * Revision 1.19 2001/07/25 17:21:54 oes
168 * client_uagent now saves copy of User-Agent: header value
170 * Revision 1.18 2001/07/13 14:02:46 oes
171 * - Included fix to repair broken HTTP requests that
172 * don't contain a path, not even '/'.
173 * - Removed all #ifdef PCRS
174 * - content_type now always inspected and classified as
175 * text, gif or other.
176 * - formatting / comments
178 * Revision 1.17 2001/06/29 21:45:41 oes
179 * Indentation, CRLF->LF, Tab-> Space
181 * Revision 1.16 2001/06/29 13:32:42 oes
183 * - Adapted free_http_request
184 * - Removed logentry from cancelled commit
186 * Revision 1.15 2001/06/03 19:12:38 oes
187 * deleted const struct interceptors
189 * Revision 1.14 2001/06/01 18:49:17 jongfoster
190 * Replaced "list_share" with "list" - the tiny memory gain was not
191 * worth the extra complexity.
193 * Revision 1.13 2001/05/31 21:30:33 jongfoster
194 * Removed list code - it's now in list.[ch]
195 * Renamed "permission" to "action", and changed many features
196 * to use the actions file rather than the global config.
198 * Revision 1.12 2001/05/31 17:33:13 oes
202 * Revision 1.11 2001/05/29 20:11:19 joergs
203 * '/ * inside comment' warning removed.
205 * Revision 1.10 2001/05/29 09:50:24 jongfoster
206 * Unified blocklist/imagelist/permissionslist.
207 * File format is still under discussion, but the internal changes
210 * Also modified interceptor behaviour:
211 * - We now intercept all URLs beginning with one of the following
212 * prefixes (and *only* these prefixes):
214 * * http://ijbswa.sf.net/config/
215 * * http://ijbswa.sourceforge.net/config/
216 * - New interceptors "home page" - go to http://i.j.b/ to see it.
217 * - Internal changes so that intercepted and fast redirect pages
218 * are not replaced with an image.
219 * - Interceptors now have the option to send a binary page direct
220 * to the client. (i.e. ijb-send-banner uses this)
221 * - Implemented show-url-info interceptor. (Which is why I needed
222 * the above interceptors changes - a typical URL is
223 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
224 * The previous mechanism would not have intercepted that, and
225 * if it had been intercepted then it then it would have replaced
228 * Revision 1.9 2001/05/28 17:26:33 jongfoster
229 * Fixing segfault if last header was crunched.
230 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
231 * can use the cross-platform sprintf() instead.)
233 * Revision 1.8 2001/05/27 22:17:04 oes
235 * - re_process_buffer no longer writes the modified buffer
236 * to the client, which was very ugly. It now returns the
237 * buffer, which it is then written by chat.
239 * - content_length now adjusts the Content-Length: header
240 * for modified documents rather than crunch()ing it.
241 * (Length info in csp->content_length, which is 0 for
242 * unmodified documents)
244 * - For this to work, sed() is called twice when filtering.
246 * Revision 1.7 2001/05/27 13:19:06 oes
247 * Patched Joergs solution for the content-length in.
249 * Revision 1.6 2001/05/26 13:39:32 jongfoster
250 * Only crunches Content-Length header if applying RE filtering.
251 * Without this fix, Microsoft Windows Update wouldn't work.
253 * Revision 1.5 2001/05/26 00:28:36 jongfoster
254 * Automatic reloading of config file.
255 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
256 * Most of the global variables have been moved to a new
257 * struct configuration_spec, accessed through csp->config->globalname
258 * Most of the globals remaining are used by the Win32 GUI.
260 * Revision 1.4 2001/05/22 18:46:04 oes
262 * - Enabled filtering banners by size rather than URL
263 * by adding patterns that replace all standard banner
264 * sizes with the "Junkbuster" gif to the re_filterfile
266 * - Enabled filtering WebBugs by providing a pattern
267 * which kills all 1x1 images
269 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
270 * which is selected by the (nonstandard and therefore
271 * capital) letter 'U' in the option string.
272 * It causes the quantifiers to be ungreedy by default.
273 * Appending a ? turns back to greedy (!).
275 * - Added a new interceptor ijb-send-banner, which
276 * sends back the "Junkbuster" gif. Without imagelist or
277 * MSIE detection support, or if tinygif = 1, or the
278 * URL isn't recognized as an imageurl, a lame HTML
279 * explanation is sent instead.
281 * - Added new feature, which permits blocking remote
282 * script redirects and firing back a local redirect
284 * The feature is conditionally compiled, i.e. it
285 * can be disabled with --disable-fast-redirects,
286 * plus it must be activated by a "fast-redirects"
287 * line in the config file, has its own log level
288 * and of course wants to be displayed by show-proxy-args
289 * Note: Boy, all the #ifdefs in 1001 locations and
290 * all the fumbling with configure.in and acconfig.h
291 * were *way* more work than the feature itself :-(
293 * - Because a generic redirect template was needed for
294 * this, tinygif = 3 now uses the same.
296 * - Moved GIFs, and other static HTTP response templates
301 * - Removed some >400 CRs again (Jon, you really worked
304 * Revision 1.3 2001/05/20 01:21:20 jongfoster
305 * Version 2.9.4 checkin.
306 * - Merged popupfile and cookiefile, and added control over PCRS
307 * filtering, in new "permissionsfile".
308 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
309 * file error you now get a message box (in the Win32 GUI) rather
310 * than the program exiting with no explanation.
311 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
313 * - Removed tabs from "config"
314 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
315 * - Bumped up version number.
317 * Revision 1.2 2001/05/17 23:02:36 oes
318 * - Made referrer option accept 'L' as a substitute for '§'
320 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
321 * Initial import of version 2.9.3 source tree
324 *********************************************************************/
331 #include <sys/types.h>
339 #if !defined(_WIN32) && !defined(__OS2__)
349 #include "jbsockets.h"
350 #include "miscutil.h"
353 const char parsers_h_rcs[] = PARSERS_H_VERSION;
355 /* Fix a problem with Solaris. There should be no effect on other
357 * Solaris's isspace() is a macro which uses it's argument directly
358 * as an array index. Therefore we need to make sure that high-bit
359 * characters generate +ve values, and ideally we also want to make
360 * the argument match the declared parameter type of "int".
362 * Why did they write a character function that can't take a simple
363 * "char" argument? Doh!
365 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
366 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
369 const struct parsers client_patterns[] = {
370 { "referer:", 8, client_referrer },
371 { "user-agent:", 11, client_uagent },
372 { "ua-", 3, client_ua },
373 { "from:", 5, client_from },
374 { "cookie:", 7, client_send_cookie },
375 { "x-forwarded-for:", 16, client_x_forwarded },
376 { "Accept-Encoding:", 16, client_accept_encoding },
377 { "TE:", 3, client_te },
378 { "Host:", 5, crumble },
379 /* { "if-modified-since:", 18, crumble }, */
380 { "Keep-Alive:", 11, crumble },
381 { "connection:", 11, crumble },
382 { "proxy-connection:", 17, crumble },
387 const struct parsers server_patterns[] = {
388 { "HTTP", 4, server_http },
389 { "set-cookie:", 11, server_set_cookie },
390 { "connection:", 11, crumble },
391 { "Content-Type:", 13, server_content_type },
392 { "Content-Length:", 15, server_content_length },
393 { "Content-MD5:", 12, server_content_md5 },
394 { "Content-Encoding:", 17, server_content_encoding },
395 { "Transfer-Encoding:", 18, server_transfer_coding },
396 { "Keep-Alive:", 11, crumble },
401 void (* const add_client_headers[])(struct client_state *) = {
404 client_x_forwarded_adder,
406 client_accept_encoding_adder,
407 connection_close_adder,
412 void (* const add_server_headers[])(struct client_state *) = {
413 connection_close_adder,
418 /*********************************************************************
420 * Function : flush_socket
422 * Description : Write any pending "buffered" content.
425 * 1 : fd = file descriptor of the socket to read
426 * 2 : csp = Current client state (buffers, headers, etc...)
428 * Returns : On success, the number of bytes written are returned (zero
429 * indicates nothing was written). On error, -1 is returned,
430 * and errno is set appropriately. If count is zero and the
431 * file descriptor refers to a regular file, 0 will be
432 * returned without causing any other effect. For a special
433 * file, the results are not portable.
435 *********************************************************************/
436 int flush_socket(int fd, struct client_state *csp)
438 struct iob *iob = csp->iob;
439 int n = iob->eod - iob->cur;
446 n = write_socket(fd, iob->cur, n);
447 iob->eod = iob->cur = iob->buf;
453 /*********************************************************************
455 * Function : add_to_iob
457 * Description : Add content to the buffered page.
460 * 1 : csp = Current client state (buffers, headers, etc...)
461 * 2 : buf = holds the content to be added to the page
462 * 3 : n = number of bytes to be added
464 * Returns : Number of bytes in the content buffer.
466 *********************************************************************/
467 int add_to_iob(struct client_state *csp, char *buf, int n)
469 struct iob *iob = csp->iob;
473 have = iob->eod - iob->cur;
482 if ((p = (char *)malloc(need + 1)) == NULL)
484 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
490 /* there is something in the buffer - save it */
491 memcpy(p, iob->cur, have);
493 /* replace the buffer with the new space */
497 /* point to the end of the data */
502 /* the buffer is empty, free it and reinitialize */
507 /* copy the new data into the iob buffer */
510 /* point to the end of the data */
513 /* null terminate == cheap insurance */
516 /* set the pointers to the new values */
525 /*********************************************************************
527 * Function : get_header
529 * Description : This (odd) routine will parse the csp->iob
532 * 1 : csp = Current client state (buffers, headers, etc...)
534 * Returns : Any one of the following:
536 * 1) a pointer to a dynamically allocated string that contains a header line
537 * 2) NULL indicating that the end of the header was reached
538 * 3) "" indicating that the end of the iob was reached before finding
539 * a complete header line.
541 *********************************************************************/
542 char *get_header(struct client_state *csp)
548 if ((iob->cur == NULL)
549 || ((p = strchr(iob->cur, '\n')) == NULL))
551 return(""); /* couldn't find a complete header */
556 ret = strdup(iob->cur);
560 if ((q = strchr(ret, '\r'))) *q = '\0';
562 /* is this a blank linke (i.e. the end of the header) ? */
574 /*********************************************************************
576 * Function : get_header_value
578 * Description : Get the value of a given header from a chained list
579 * of header lines or return NULL if no such header is
580 * present in the list.
583 * 1 : header_list = pointer to list
584 * 2 : header_name = string with name of header to look for.
585 * Trailing colon required, capitalization
588 * Returns : NULL if not found, else value of header
590 *********************************************************************/
591 char *get_header_value(const struct list *header_list, const char *header_name)
593 struct list_entry *cur_entry;
599 length = strlen(header_name);
601 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
605 if (!strncmpic(cur_entry->str, header_name, length))
608 * Found: return pointer to start of value
610 ret = (char *) (cur_entry->str + length);
611 while (*ret && ijb_isspace(*ret)) ret++;
624 /*********************************************************************
628 * Description : add, delete or modify lines in the HTTP header streams.
629 * On entry, it receives a linked list of headers space
630 * that was allocated dynamically (both the list nodes
631 * and the header contents).
633 * As a side effect it frees the space used by the original
637 * 1 : pats = list of patterns to match against headers
638 * 2 : more_headers = list of functions to add more
639 * headers (client or server)
640 * 3 : csp = Current client state (buffers, headers, etc...)
642 * Returns : Single pointer to a fully formed header, or NULL
643 * on out-of-memory error.
645 *********************************************************************/
646 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
648 struct list_entry *p;
649 const struct parsers *v;
653 for (v = pats; v->str ; v++)
655 for (p = csp->headers->first; p ; p = p->next)
657 /* Header crunch()ed in previous run? -> ignore */
658 if (p->str == NULL) continue;
660 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
662 if (strncmpic(p->str, v->str, v->len) == 0)
664 hdr = v->parser(v, p->str, csp);
665 freez(p->str); /* FIXME: Yuck! patching a list...*/
671 /* place any additional headers on the csp->headers list */
672 for (f = more_headers; *f ; f++)
677 hdr = list_to_text(csp->headers);
684 /*********************************************************************
686 * Function : free_http_request
688 * Description : Freez a http_request structure
691 * 1 : http = points to a http_request structure to free
695 *********************************************************************/
696 void free_http_request(struct http_request *http)
704 freez(http->hostport);
707 freez(http->host_ip_addr_str);
712 /*********************************************************************
714 * Function : parse_http_request
716 * Description : Parse out the host and port from the URL. Find the
717 * hostname & path, port (if ':'), and/or password (if '@')
720 * 1 : req = URL (or is it URI?) to break down
721 * 2 : http = pointer to the http structure to hold elements
722 * 3 : csp = Current client state (buffers, headers, etc...)
726 *********************************************************************/
727 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
729 char *buf, *v[10], *url, *p;
732 memset(http, '\0', sizeof(*http));
733 http->cmd = strdup(req);
736 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
740 /* this could be a CONNECT request */
741 if (strcmpic(v[0], "connect") == 0)
744 http->gpc = strdup(v[0]);
745 http->hostport = strdup(v[1]);
746 http->ver = strdup(v[2]);
749 /* or it could be any other basic HTTP request type */
750 if ((0 == strcmpic(v[0], "get"))
751 || (0 == strcmpic(v[0], "head"))
752 || (0 == strcmpic(v[0], "post"))
753 || (0 == strcmpic(v[0], "put"))
754 || (0 == strcmpic(v[0], "delete"))
756 /* or a webDAV extension (RFC2518) */
757 || (0 == strcmpic(v[0], "propfind"))
758 || (0 == strcmpic(v[0], "proppatch"))
759 || (0 == strcmpic(v[0], "move"))
760 || (0 == strcmpic(v[0], "copy"))
761 || (0 == strcmpic(v[0], "mkcol"))
762 || (0 == strcmpic(v[0], "lock"))
763 || (0 == strcmpic(v[0], "unlock"))
767 http->gpc = strdup(v[0]);
768 http->url = strdup(v[1]);
769 http->ver = strdup(v[2]);
772 if (strncmpic(url, "http://", 7) == 0)
776 else if (strncmpic(url, "https://", 8) == 0)
787 if ((p = strchr(url, '/')))
789 http->path = strdup(p);
791 http->hostport = strdup(url);
794 * Repair broken HTTP requests that don't contain a path
798 http->path = strdup("/");
799 http->hostport = strdup(url);
808 if (http->hostport == NULL)
810 free_http_request(http);
814 buf = strdup(http->hostport);
817 /* check if url contains password */
818 n = ssplit(buf, "@", v, SZ(v), 1, 1);
821 char * newbuf = NULL;
822 newbuf = strdup(v[1]);
827 n = ssplit(buf, ":", v, SZ(v), 1, 1);
831 http->host = strdup(v[0]);
837 http->host = strdup(v[0]);
838 http->port = atoi(v[1]);
843 if (http->host == NULL)
845 free_http_request(http);
848 if (http->path == NULL)
850 http->path = strdup("/");
856 /* here begins the family of parser functions that reformat header lines */
859 /*********************************************************************
863 * Description : This is called if a header matches a pattern to "crunch"
866 * 1 : v = Pointer to parsers structure, which basically holds
867 * headers (client or server) that we want to "crunch"
868 * 2 : s = header (from sed) to "crunch"
869 * 3 : csp = Current client state (buffers, headers, etc...)
871 * Returns : Always NULL.
873 *********************************************************************/
874 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
876 log_error(LOG_LEVEL_HEADER, "crunch!");
882 /*********************************************************************
884 * Function : server_content_type
886 * Description : Set the content-type for filterable types (text/.*,
887 * javascript and image/gif) unless filtering has been
888 * forbidden (CT_TABOO) while parsing earlier headers.
892 * 2 : s = header string we are "considering"
893 * 3 : csp = Current client state (buffers, headers, etc...)
895 * Returns : A duplicate string pointer to this header (ie. pass thru)
897 *********************************************************************/
898 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
900 if (csp->content_type != CT_TABOO)
902 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
903 csp->content_type = CT_TEXT;
904 else if (strstr(s, " image/gif"))
905 csp->content_type = CT_GIF;
907 csp->content_type = 0;
915 /*********************************************************************
917 * Function : server_transfer_coding
919 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
920 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
921 * - Change from "chunked" to "identity" if body was chunked
922 * but has been de-chunked for filtering.
926 * 2 : s = header string we are "considering"
927 * 3 : csp = Current client state (buffers, headers, etc...)
929 * Returns : A duplicate string pointer to this header (ie. pass thru)
931 *********************************************************************/
932 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
935 * Turn off pcrs and gif filtering if body compressed
937 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
939 csp->content_type = CT_TABOO;
943 * Raise flag if body chunked
945 if (strstr(s, "chunked"))
947 csp->flags |= CSP_FLAG_CHUNKED;
950 * If the body was modified, it has been
951 * de-chunked first, so adjust the header:
953 if (csp->flags & CSP_FLAG_MODIFIED)
955 return(strdup("Transfer-Encoding: identity"));
964 /*********************************************************************
966 * Function : server_content_encoding
968 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
972 * 2 : s = header string we are "considering"
973 * 3 : csp = Current client state (buffers, headers, etc...)
975 * Returns : A duplicate string pointer to this header (ie. pass thru)
977 *********************************************************************/
978 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
981 * Turn off pcrs and gif filtering if body compressed
983 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
985 csp->content_type = CT_TABOO;
993 /*********************************************************************
995 * Function : server_content_length
997 * Description : Adjust Content-Length header if we modified
1002 * 2 : s = header string we are "considering"
1003 * 3 : csp = Current client state (buffers, headers, etc...)
1005 * Returns : A duplicate string pointer to this header (ie. pass thru)
1007 *********************************************************************/
1008 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
1010 if (csp->content_length != 0) /* Content length has been modified */
1012 char * s2 = (char *) zalloc(100);
1013 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
1015 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1026 /*********************************************************************
1028 * Function : server_content_md5
1030 * Description : Crumble any Content-MD5 headers if the document was
1031 * modified. FIXME: Should we re-compute instead?
1035 * 2 : s = header string we are "considering"
1036 * 3 : csp = Current client state (buffers, headers, etc...)
1038 * Returns : A duplicate string pointer to this header (ie. pass thru)
1040 *********************************************************************/
1041 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
1043 if (csp->flags & CSP_FLAG_MODIFIED)
1045 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1056 /*********************************************************************
1058 * Function : client_accept_encoding
1060 * Description : Rewrite the client's Accept-Encoding header so that
1061 * if doesn't allow compression, if the action applies.
1062 * Note: For HTTP/1.0 the absence of the header is enough.
1066 * 2 : s = header string we are "considering"
1067 * 3 : csp = Current client state (buffers, headers, etc...)
1069 * Returns : A copy of the client's original or the modified header.
1071 *********************************************************************/
1072 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
1074 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1080 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1082 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1084 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1095 /*********************************************************************
1097 * Function : client_te
1099 * Description : Rewrite the client's TE header so that
1100 * if doesn't allow compression, if the action applies.
1104 * 2 : s = header string we are "considering"
1105 * 3 : csp = Current client state (buffers, headers, etc...)
1107 * Returns : A copy of the client's original or the modified header.
1109 *********************************************************************/
1110 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1112 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1118 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1124 /*********************************************************************
1126 * Function : client_referrer
1128 * Description : Handle the "referer" config setting properly.
1129 * Called from `sed'.
1133 * 2 : s = header (from sed) to "crunch"
1134 * 3 : csp = Current client state (buffers, headers, etc...)
1136 * Returns : NULL if crunched, or a malloc'ed string with the original
1137 * or modified header
1139 *********************************************************************/
1140 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1142 const char * newval;
1144 #ifdef FEATURE_FORCE_LOAD
1145 /* Since the referrer can include the prefix even
1146 * even if the request itself is non-forced, we must
1147 * clean it unconditionally
1149 strclean(s, FORCE_PREFIX);
1150 #endif /* def FEATURE_FORCE_LOAD */
1153 * Are we sending referer?
1155 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1160 newval = csp->action->string[ACTION_STRING_REFERER];
1163 * Are we blocking referer?
1165 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1167 log_error(LOG_LEVEL_HEADER, "crunch!");
1172 * Are we forging referer?
1174 if (0 == strcmpic(newval, "forge"))
1177 * Forge a referer as http://[hostname:port of REQUEST]/
1178 * to fool stupid checks for in-site links
1180 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1181 s2 = strsav(NULL, "Referer: ");
1182 s2 = strsav(s2, "http://");
1183 s2 = strsav(s2, csp->http->hostport);
1184 s2 = strsav(s2, "/");
1189 * Have we got a fixed referer?
1191 if (0 == strncmpic(newval, "http://", 7))
1194 * We have a specific (fixed) referer we want to send.
1198 log_error(LOG_LEVEL_HEADER, "modified");
1200 s3 = strsav( NULL, "Referer: " );
1201 s3 = strsav( s3, newval );
1205 /* Should never get here! */
1206 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1209 * Forge is probably the best default.
1211 * Forge a referer as http://[hostname:port of REQUEST]/
1212 * to fool stupid checks for in-site links
1214 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1215 s2 = strsav(NULL, "Referer: ");
1216 s2 = strsav(s2, "http://");
1217 s2 = strsav(s2, csp->http->hostport);
1218 s2 = strsav(s2, "/");
1223 /*********************************************************************
1225 * Function : client_uagent
1227 * Description : Handle the "user-agent" config setting properly
1228 * and remember its original value to enable browser
1229 * bug workarounds. Called from `sed'.
1233 * 2 : s = header (from sed) to "crunch"
1234 * 3 : csp = Current client state (buffers, headers, etc...)
1236 * Returns : A malloc'ed pointer to the default agent, or
1237 * a malloc'ed string pointer to this header (ie. pass thru).
1239 *********************************************************************/
1240 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1242 const char * newval;
1245 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1250 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1256 log_error(LOG_LEVEL_HEADER, "modified");
1258 s2 = strsav( NULL, "User-Agent: " );
1259 s2 = strsav( s2, newval );
1265 /*********************************************************************
1267 * Function : client_ua
1269 * Description : Handle "ua-" headers properly. Called from `sed'.
1273 * 2 : s = header (from sed) to "crunch"
1274 * 3 : csp = Current client state (buffers, headers, etc...)
1276 * Returns : NULL if crunched, or a malloc'ed string to original header
1278 *********************************************************************/
1279 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1281 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1287 log_error(LOG_LEVEL_HEADER, "crunch!");
1293 /*********************************************************************
1295 * Function : client_from
1297 * Description : Handle the "from" config setting properly.
1298 * Called from `sed'.
1302 * 2 : s = header (from sed) to "crunch"
1303 * 3 : csp = Current client state (buffers, headers, etc...)
1305 * Returns : NULL if crunched, or a malloc'ed string to
1306 * modified/original header.
1308 *********************************************************************/
1309 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1311 const char * newval;
1314 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1319 newval = csp->action->string[ACTION_STRING_FROM];
1322 * Are we blocking referer?
1324 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1326 log_error(LOG_LEVEL_HEADER, "crunch!");
1330 log_error(LOG_LEVEL_HEADER, " modified");
1332 s2 = strsav( NULL, "From: " );
1333 s2 = strsav( s2, newval );
1339 /*********************************************************************
1341 * Function : client_send_cookie
1343 * Description : Handle the "cookie" header properly. Called from `sed'.
1344 * If cookie is accepted, add it to the cookie_list,
1345 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1348 * 1 : v = pattern of cookie `sed' found matching
1349 * 2 : s = header (from sed) to "crunch"
1350 * 3 : csp = Current client state (buffers, headers, etc...)
1352 * Returns : Always NULL.
1354 *********************************************************************/
1355 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1357 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1359 enlist(csp->cookie_list, s + v->len + 1);
1363 log_error(LOG_LEVEL_HEADER, " crunch!");
1367 * Always return NULL here. The cookie header
1368 * will be sent at the end of the header.
1375 /*********************************************************************
1377 * Function : client_x_forwarded
1379 * Description : Handle the "x-forwarded-for" config setting properly,
1380 * also used in the add_client_headers list. Called from `sed'.
1384 * 2 : s = header (from sed) to "crunch"
1385 * 3 : csp = Current client state (buffers, headers, etc...)
1387 * Returns : Always NULL.
1389 *********************************************************************/
1390 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1392 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1394 /* Save it so we can re-add it later */
1395 csp->x_forwarded = strdup(s);
1399 * Always return NULL, since this information
1400 * will be sent at the end of the header.
1407 /* the following functions add headers directly to the header list */
1409 /*********************************************************************
1411 * Function : client_host_adder
1413 * Description : (re)adds the host header. Called from `sed'.
1416 * 1 : csp = Current client state (buffers, headers, etc...)
1420 *********************************************************************/
1421 void client_host_adder(struct client_state *csp)
1426 if ( !csp->http->hostport || !*(csp->http->hostport)) return;
1427 p = strsav(p, "Host: ");
1429 ** remove 'user:pass@' from 'proto://user:pass@host'
1431 if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
1433 p = strsav(p, pos+1);
1437 p = strsav(p, csp->http->hostport);
1439 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1440 enlist(csp->headers, p);
1446 /*********************************************************************
1448 * Function : client_cookie_adder
1450 * Description : Used in the add_client_headers list. Called from `sed'.
1453 * 1 : csp = Current client state (buffers, headers, etc...)
1457 *********************************************************************/
1458 void client_cookie_adder(struct client_state *csp)
1460 struct list_entry *lst;
1464 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1468 tmp = strsav(tmp, "; ");
1470 tmp = strsav(tmp, lst->str);
1473 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1477 tmp = strsav(tmp, "; ");
1480 if ((e = cookie_encode(lst->str)))
1482 tmp = strsav(tmp, e);
1491 ret = strdup("Cookie: ");
1492 ret = strsav(ret, tmp);
1493 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1494 enlist(csp->headers, ret);
1502 /*********************************************************************
1504 * Function : client_accept_encoding_adder
1506 * Description : Add an Accept-Encoding header to the client's request
1507 * that disables compression if the action applies, and
1508 * the header is not already there. Called from `sed'.
1509 * Note: For HTTP/1.0, the absence of the header is enough.
1512 * 1 : csp = Current client state (buffers, headers, etc...)
1516 *********************************************************************/
1517 void client_accept_encoding_adder(struct client_state *csp)
1519 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1520 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1522 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1528 /*********************************************************************
1530 * Function : client_xtra_adder
1532 * Description : Used in the add_client_headers list. Called from `sed'.
1535 * 1 : csp = Current client state (buffers, headers, etc...)
1539 *********************************************************************/
1540 void client_xtra_adder(struct client_state *csp)
1542 struct list_entry *lst;
1544 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1545 lst ; lst = lst->next)
1547 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1548 enlist(csp->headers, lst->str);
1554 /*********************************************************************
1556 * Function : client_x_forwarded_adder
1558 * Description : Used in the add_client_headers list. Called from `sed'.
1561 * 1 : csp = Current client state (buffers, headers, etc...)
1565 *********************************************************************/
1566 void client_x_forwarded_adder(struct client_state *csp)
1570 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1575 if (csp->x_forwarded)
1577 p = strsav(p, csp->x_forwarded);
1578 p = strsav(p, ", ");
1579 p = strsav(p, csp->ip_addr_str);
1583 p = strsav(p, "X-Forwarded-For: ");
1584 p = strsav(p, csp->ip_addr_str);
1587 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1588 enlist(csp->headers, p);
1594 /*********************************************************************
1596 * Function : connection_close_adder
1598 * Description : Adds a "Connection: close" header to csp->headers
1599 * as a temporary fix for the needed but missing HTTP/1.1
1600 * support. Called from `sed'.
1601 * FIXME: This whole function shouldn't be neccessary!
1604 * 1 : csp = Current client state (buffers, headers, etc...)
1608 *********************************************************************/
1609 void connection_close_adder(struct client_state *csp)
1611 enlist(csp->headers, "Connection: close");
1615 /*********************************************************************
1617 * Function : server_http
1619 * Description : - Save the HTTP Status into csp->http->status
1620 * - Set CT_TABOO to prevent filtering if the answer
1621 * is a partial range (HTTP status 206)
1622 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1626 * 1 : v = parser pattern that matched this header
1627 * 2 : s = header that matched this pattern
1628 * 3 : csp = Current client state (buffers, headers, etc...)
1630 * Returns : Copy of changed or original answer.
1632 *********************************************************************/
1633 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1635 char *ret = strdup(s);
1637 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1638 if (csp->http->status == 206)
1640 csp->content_type = CT_TABOO;
1643 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1646 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1653 /*********************************************************************
1655 * Function : server_set_cookie
1657 * Description : Handle the server "cookie" header properly.
1658 * Log cookie to the jar file. Then "crunch" it,
1659 * or accept it. Called from `sed'.
1662 * 1 : v = parser pattern that matched this header
1663 * 2 : s = header that matched this pattern
1664 * 3 : csp = Current client state (buffers, headers, etc...)
1666 * Returns : `crumble' or a newly malloc'ed string.
1668 *********************************************************************/
1669 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1671 #ifdef FEATURE_COOKIE_JAR
1672 if (csp->config->jar)
1675 * Write timestamp into outbuf.
1677 * Complex because not all OSs have tm_gmtoff or
1678 * the %z field in strftime()
1680 char tempbuf[ BUFFER_SIZE ];
1684 #ifdef HAVE_LOCALTIME_R
1685 tm_now = *localtime_r(&now, &tm_now);
1687 tm_now = *localtime (&now);
1689 strftime(tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", &tm_now);
1691 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, (s + v->len + 1));
1693 #endif /* def FEATURE_COOKIE_JAR */
1695 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1697 return(crumble(v, s, csp));
1699 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1701 /* Flag whether or not to log a message */
1704 /* A variable to store the tag we're working on */
1707 /* Make a copy of the header we can write to */
1708 char * result = strdup(s);
1711 /* FIXME: This error handling is incorrect */
1715 /* Skip "Set-Cookie:" (11 characters) in header */
1716 cur_tag = result + 11;
1718 /* skip whitespace between "Set-Cookie:" and value */
1719 while (*cur_tag && ijb_isspace(*cur_tag))
1724 /* Loop through each tag in the cookie */
1728 char * next_tag = strchr(cur_tag, ';');
1729 if (next_tag != NULL)
1731 /* Skip the ';' character itself */
1734 /* skip whitespace ";" and start of tag */
1735 while (*next_tag && ijb_isspace(*next_tag))
1742 /* "Next tag" is the end of the string */
1743 next_tag = cur_tag + strlen(cur_tag);
1746 /* Is this the "Expires" tag? */
1747 if (strncmpic(cur_tag, "expires=", 8) == 0)
1749 /* Delete the tag by copying the rest of the string over it.
1750 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1751 * since the behaviour of strcpy is undefined for overlapping
1754 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1756 /* That changed the header, need to issue a log message */
1759 /* Note that the next tag has now been moved to *cur_tag,
1760 * so we do not need to update the cur_tag pointer.
1765 /* Move on to next cookie tag */
1772 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1782 #ifdef FEATURE_FORCE_LOAD
1783 /*********************************************************************
1785 * Function : strclean
1787 * Description : In-Situ-Eliminate all occurances of substring in
1791 * 1 : string = string to clean
1792 * 2 : substring = substring to eliminate
1794 * Returns : Number of eliminations
1796 *********************************************************************/
1797 int strclean(const char *string, const char *substring)
1799 int hits = 0, len = strlen(substring);
1802 while((pos = strstr(string, substring)))
1809 while (*p++ != '\0');
1816 #endif /* def FEATURE_FORCE_LOAD */