1 const char parsers_rcs[] = "$Id: parsers.c,v 1.42 2001/11/22 21:59:30 jongfoster 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.42 2001/11/22 21:59:30 jongfoster
45 * Adding code to handle +no-cookies-keep
47 * Revision 1.41 2001/11/05 23:43:05 steudten
48 * Add time+date to log files.
50 * Revision 1.40 2001/10/26 20:13:09 jongfoster
51 * ctype.h is needed in Windows, too.
53 * Revision 1.39 2001/10/26 17:40:04 oes
54 * Introduced get_header_value()
55 * Removed http->user_agent, csp->referrer and csp->accept_types
56 * Removed client_accept()
58 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
59 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
60 * threads to call select() simultaneously. So, it's time to do a real, live,
61 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
62 * (native). Both versions will work, but using __OS2__ offers multi-threading.
64 * Revision 1.37 2001/10/23 21:36:02 jongfoster
65 * Documenting sed()'s error behaviou (doc change only)
67 * Revision 1.36 2001/10/13 12:51:51 joergs
68 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
69 * force-load), instead crumble Host: and add it (again) in client_host_adder
70 * (in case we get a HTTP/1.0 request without Host: header and forward it to
71 * a HTTP/1.1 server/proxy).
73 * Revision 1.35 2001/10/09 22:39:21 jongfoster
74 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
77 * Revision 1.34 2001/10/07 18:50:55 oes
78 * Added server_content_encoding, renamed server_transfer_encoding
80 * Revision 1.33 2001/10/07 18:04:49 oes
81 * Changed server_http11 to server_http and its pattern to "HTTP".
82 * Additional functionality: it now saves the HTTP status into
83 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
85 * Revision 1.32 2001/10/07 15:43:28 oes
86 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
87 * client_te and client_accept_encoding_adder, triggered by the new
88 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
89 * changed to allow only identity and chunked, and the TE header is
90 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
92 * parse_http_request no longer does anything than parsing. The rewriting
93 * of http->cmd and version mangling are gone. It now also recognizes
94 * the put and delete methods and saves the url in http->url. Removed
97 * renamed content_type and content_length to have the server_ prefix
99 * server_content_type now only works if csp->content_type != CT_TABOO
101 * added server_transfer_encoding, which
102 * - Sets CT_TABOO to prohibit filtering if encoding compresses
103 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
104 * - Change from "chunked" to "identity" if body was chunked
105 * but has been de-chunked for filtering.
107 * added server_content_md5 which crunches any Content-MD5 headers
108 * if the body was modified.
110 * made server_http11 conditional on +downgrade action
112 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
114 * Revision 1.31 2001/10/05 14:25:02 oes
115 * Crumble Keep-Alive from Server
117 * Revision 1.30 2001/09/29 12:56:03 joergs
118 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
120 * Revision 1.29 2001/09/24 21:09:24 jongfoster
121 * Fixing 2 memory leaks that Guy spotted, where the paramater to
122 * enlist() was not being free()d.
124 * Revision 1.28 2001/09/22 16:32:28 jongfoster
125 * Removing unused #includes.
127 * Revision 1.27 2001/09/20 15:45:25 steudten
129 * add casting from size_t to int for printf()
130 * remove local variable shadow s2
132 * Revision 1.26 2001/09/16 17:05:14 jongfoster
133 * Removing unused #include showarg.h
135 * Revision 1.25 2001/09/16 13:21:27 jongfoster
136 * Changes to use new list functions.
138 * Revision 1.24 2001/09/13 23:05:50 jongfoster
139 * Changing the string paramater to the header parsers a "const".
141 * Revision 1.23 2001/09/12 18:08:19 steudten
143 * In parse_http_request() header rewriting miss the host value, so
144 * from http://www.mydomain.com the result was just " / " not
145 * http://www.mydomain.com/ in case we forward.
147 * Revision 1.22 2001/09/10 10:58:53 oes
148 * Silenced compiler warnings
150 * Revision 1.21 2001/07/31 14:46:00 oes
151 * - Persistant connections now suppressed
152 * - sed() no longer appends empty header to csp->headers
154 * Revision 1.20 2001/07/30 22:08:36 jongfoster
155 * Tidying up #defines:
156 * - All feature #defines are now of the form FEATURE_xxx
157 * - Permanently turned off WIN_GUI_EDIT
158 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
160 * Revision 1.19 2001/07/25 17:21:54 oes
161 * client_uagent now saves copy of User-Agent: header value
163 * Revision 1.18 2001/07/13 14:02:46 oes
164 * - Included fix to repair broken HTTP requests that
165 * don't contain a path, not even '/'.
166 * - Removed all #ifdef PCRS
167 * - content_type now always inspected and classified as
168 * text, gif or other.
169 * - formatting / comments
171 * Revision 1.17 2001/06/29 21:45:41 oes
172 * Indentation, CRLF->LF, Tab-> Space
174 * Revision 1.16 2001/06/29 13:32:42 oes
176 * - Adapted free_http_request
177 * - Removed logentry from cancelled commit
179 * Revision 1.15 2001/06/03 19:12:38 oes
180 * deleted const struct interceptors
182 * Revision 1.14 2001/06/01 18:49:17 jongfoster
183 * Replaced "list_share" with "list" - the tiny memory gain was not
184 * worth the extra complexity.
186 * Revision 1.13 2001/05/31 21:30:33 jongfoster
187 * Removed list code - it's now in list.[ch]
188 * Renamed "permission" to "action", and changed many features
189 * to use the actions file rather than the global config.
191 * Revision 1.12 2001/05/31 17:33:13 oes
195 * Revision 1.11 2001/05/29 20:11:19 joergs
196 * '/ * inside comment' warning removed.
198 * Revision 1.10 2001/05/29 09:50:24 jongfoster
199 * Unified blocklist/imagelist/permissionslist.
200 * File format is still under discussion, but the internal changes
203 * Also modified interceptor behaviour:
204 * - We now intercept all URLs beginning with one of the following
205 * prefixes (and *only* these prefixes):
207 * * http://ijbswa.sf.net/config/
208 * * http://ijbswa.sourceforge.net/config/
209 * - New interceptors "home page" - go to http://i.j.b/ to see it.
210 * - Internal changes so that intercepted and fast redirect pages
211 * are not replaced with an image.
212 * - Interceptors now have the option to send a binary page direct
213 * to the client. (i.e. ijb-send-banner uses this)
214 * - Implemented show-url-info interceptor. (Which is why I needed
215 * the above interceptors changes - a typical URL is
216 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
217 * The previous mechanism would not have intercepted that, and
218 * if it had been intercepted then it then it would have replaced
221 * Revision 1.9 2001/05/28 17:26:33 jongfoster
222 * Fixing segfault if last header was crunched.
223 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
224 * can use the cross-platform sprintf() instead.)
226 * Revision 1.8 2001/05/27 22:17:04 oes
228 * - re_process_buffer no longer writes the modified buffer
229 * to the client, which was very ugly. It now returns the
230 * buffer, which it is then written by chat.
232 * - content_length now adjusts the Content-Length: header
233 * for modified documents rather than crunch()ing it.
234 * (Length info in csp->content_length, which is 0 for
235 * unmodified documents)
237 * - For this to work, sed() is called twice when filtering.
239 * Revision 1.7 2001/05/27 13:19:06 oes
240 * Patched Joergs solution for the content-length in.
242 * Revision 1.6 2001/05/26 13:39:32 jongfoster
243 * Only crunches Content-Length header if applying RE filtering.
244 * Without this fix, Microsoft Windows Update wouldn't work.
246 * Revision 1.5 2001/05/26 00:28:36 jongfoster
247 * Automatic reloading of config file.
248 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
249 * Most of the global variables have been moved to a new
250 * struct configuration_spec, accessed through csp->config->globalname
251 * Most of the globals remaining are used by the Win32 GUI.
253 * Revision 1.4 2001/05/22 18:46:04 oes
255 * - Enabled filtering banners by size rather than URL
256 * by adding patterns that replace all standard banner
257 * sizes with the "Junkbuster" gif to the re_filterfile
259 * - Enabled filtering WebBugs by providing a pattern
260 * which kills all 1x1 images
262 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
263 * which is selected by the (nonstandard and therefore
264 * capital) letter 'U' in the option string.
265 * It causes the quantifiers to be ungreedy by default.
266 * Appending a ? turns back to greedy (!).
268 * - Added a new interceptor ijb-send-banner, which
269 * sends back the "Junkbuster" gif. Without imagelist or
270 * MSIE detection support, or if tinygif = 1, or the
271 * URL isn't recognized as an imageurl, a lame HTML
272 * explanation is sent instead.
274 * - Added new feature, which permits blocking remote
275 * script redirects and firing back a local redirect
277 * The feature is conditionally compiled, i.e. it
278 * can be disabled with --disable-fast-redirects,
279 * plus it must be activated by a "fast-redirects"
280 * line in the config file, has its own log level
281 * and of course wants to be displayed by show-proxy-args
282 * Note: Boy, all the #ifdefs in 1001 locations and
283 * all the fumbling with configure.in and acconfig.h
284 * were *way* more work than the feature itself :-(
286 * - Because a generic redirect template was needed for
287 * this, tinygif = 3 now uses the same.
289 * - Moved GIFs, and other static HTTP response templates
294 * - Removed some >400 CRs again (Jon, you really worked
297 * Revision 1.3 2001/05/20 01:21:20 jongfoster
298 * Version 2.9.4 checkin.
299 * - Merged popupfile and cookiefile, and added control over PCRS
300 * filtering, in new "permissionsfile".
301 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
302 * file error you now get a message box (in the Win32 GUI) rather
303 * than the program exiting with no explanation.
304 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
306 * - Removed tabs from "config"
307 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
308 * - Bumped up version number.
310 * Revision 1.2 2001/05/17 23:02:36 oes
311 * - Made referrer option accept 'L' as a substitute for '§'
313 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
314 * Initial import of version 2.9.3 source tree
317 *********************************************************************/
324 #include <sys/types.h>
332 #if !defined(_WIN32) && !defined(__OS2__)
342 #include "jbsockets.h"
343 #include "miscutil.h"
346 const char parsers_h_rcs[] = PARSERS_H_VERSION;
348 /* Fix a problem with Solaris. There should be no effect on other
350 * Solaris's isspace() is a macro which uses it's argument directly
351 * as an array index. Therefore we need to make sure that high-bit
352 * characters generate +ve values, and ideally we also want to make
353 * the argument match the declared parameter type of "int".
355 * Why did they write a character function that can't take a simple
356 * "char" argument? Doh!
358 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
359 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
362 const struct parsers client_patterns[] = {
363 { "referer:", 8, client_referrer },
364 { "user-agent:", 11, client_uagent },
365 { "ua-", 3, client_ua },
366 { "from:", 5, client_from },
367 { "cookie:", 7, client_send_cookie },
368 { "x-forwarded-for:", 16, client_x_forwarded },
369 { "Accept-Encoding:", 16, client_accept_encoding },
370 { "TE:", 3, client_te },
371 { "Host:", 5, crumble },
372 /* { "if-modified-since:", 18, crumble }, */
373 { "Keep-Alive:", 11, crumble },
374 { "connection:", 11, crumble },
375 { "proxy-connection:", 17, crumble },
380 const struct parsers server_patterns[] = {
381 { "HTTP", 4, server_http },
382 { "set-cookie:", 11, server_set_cookie },
383 { "connection:", 11, crumble },
384 { "Content-Type:", 13, server_content_type },
385 { "Content-Length:", 15, server_content_length },
386 { "Content-MD5:", 12, server_content_md5 },
387 { "Content-Encoding:", 17, server_content_encoding },
388 { "Transfer-Encoding:", 18, server_transfer_coding },
389 { "Keep-Alive:", 11, crumble },
394 void (* const add_client_headers[])(struct client_state *) = {
397 client_x_forwarded_adder,
399 client_accept_encoding_adder,
400 connection_close_adder,
405 void (* const add_server_headers[])(struct client_state *) = {
406 connection_close_adder,
411 /*********************************************************************
413 * Function : flush_socket
415 * Description : Write any pending "buffered" content.
418 * 1 : fd = file descriptor of the socket to read
419 * 2 : csp = Current client state (buffers, headers, etc...)
421 * Returns : On success, the number of bytes written are returned (zero
422 * indicates nothing was written). On error, -1 is returned,
423 * and errno is set appropriately. If count is zero and the
424 * file descriptor refers to a regular file, 0 will be
425 * returned without causing any other effect. For a special
426 * file, the results are not portable.
428 *********************************************************************/
429 int flush_socket(int fd, struct client_state *csp)
431 struct iob *iob = csp->iob;
432 int n = iob->eod - iob->cur;
439 n = write_socket(fd, iob->cur, n);
440 iob->eod = iob->cur = iob->buf;
446 /*********************************************************************
448 * Function : add_to_iob
450 * Description : Add content to the buffered page.
453 * 1 : csp = Current client state (buffers, headers, etc...)
454 * 2 : buf = holds the content to be added to the page
455 * 3 : n = number of bytes to be added
457 * Returns : Number of bytes in the content buffer.
459 *********************************************************************/
460 int add_to_iob(struct client_state *csp, char *buf, int n)
462 struct iob *iob = csp->iob;
466 have = iob->eod - iob->cur;
475 if ((p = (char *)malloc(need + 1)) == NULL)
477 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
483 /* there is something in the buffer - save it */
484 memcpy(p, iob->cur, have);
486 /* replace the buffer with the new space */
490 /* point to the end of the data */
495 /* the buffer is empty, free it and reinitialize */
500 /* copy the new data into the iob buffer */
503 /* point to the end of the data */
506 /* null terminate == cheap insurance */
509 /* set the pointers to the new values */
518 /*********************************************************************
520 * Function : get_header
522 * Description : This (odd) routine will parse the csp->iob
525 * 1 : csp = Current client state (buffers, headers, etc...)
527 * Returns : Any one of the following:
529 * 1) a pointer to a dynamically allocated string that contains a header line
530 * 2) NULL indicating that the end of the header was reached
531 * 3) "" indicating that the end of the iob was reached before finding
532 * a complete header line.
534 *********************************************************************/
535 char *get_header(struct client_state *csp)
541 if ((iob->cur == NULL)
542 || ((p = strchr(iob->cur, '\n')) == NULL))
544 return(""); /* couldn't find a complete header */
549 ret = strdup(iob->cur);
553 if ((q = strchr(ret, '\r'))) *q = '\0';
555 /* is this a blank linke (i.e. the end of the header) ? */
567 /*********************************************************************
569 * Function : get_header_value
571 * Description : Get the value of a given header from a chained list
572 * of header lines or return NULL if no such header is
573 * present in the list.
576 * 1 : header_list = pointer to list
577 * 2 : header_name = string with name of header to look for.
578 * Trailing colon required, capitalization
581 * Returns : NULL if not found, else value of header
583 *********************************************************************/
584 char *get_header_value(const struct list *header_list, const char *header_name)
586 struct list_entry *cur_entry;
592 length = strlen(header_name);
594 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
598 if (!strncmpic(cur_entry->str, header_name, length))
601 * Found: return pointer to start of value
603 ret = (char *) (cur_entry->str + length);
604 while (*ret && ijb_isspace(*ret)) ret++;
617 /*********************************************************************
621 * Description : add, delete or modify lines in the HTTP header streams.
622 * On entry, it receives a linked list of headers space
623 * that was allocated dynamically (both the list nodes
624 * and the header contents).
626 * As a side effect it frees the space used by the original
630 * 1 : pats = list of patterns to match against headers
631 * 2 : more_headers = list of functions to add more
632 * headers (client or server)
633 * 3 : csp = Current client state (buffers, headers, etc...)
635 * Returns : Single pointer to a fully formed header, or NULL
636 * on out-of-memory error.
638 *********************************************************************/
639 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
641 struct list_entry *p;
642 const struct parsers *v;
646 for (v = pats; v->str ; v++)
648 for (p = csp->headers->first; p ; p = p->next)
650 /* Header crunch()ed in previous run? -> ignore */
651 if (p->str == NULL) continue;
653 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
655 if (strncmpic(p->str, v->str, v->len) == 0)
657 hdr = v->parser(v, p->str, csp);
658 freez(p->str); /* FIXME: Yuck! patching a list...*/
664 /* place any additional headers on the csp->headers list */
665 for (f = more_headers; *f ; f++)
670 hdr = list_to_text(csp->headers);
677 /*********************************************************************
679 * Function : free_http_request
681 * Description : Freez a http_request structure
684 * 1 : http = points to a http_request structure to free
688 *********************************************************************/
689 void free_http_request(struct http_request *http)
697 freez(http->hostport);
700 freez(http->host_ip_addr_str);
705 /*********************************************************************
707 * Function : parse_http_request
709 * Description : Parse out the host and port from the URL. Find the
710 * hostname & path, port (if ':'), and/or password (if '@')
713 * 1 : req = URL (or is it URI?) to break down
714 * 2 : http = pointer to the http structure to hold elements
715 * 3 : csp = Current client state (buffers, headers, etc...)
719 *********************************************************************/
720 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
722 char *buf, *v[10], *url, *p;
725 memset(http, '\0', sizeof(*http));
726 http->cmd = strdup(req);
729 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
733 /* this could be a CONNECT request */
734 if (strcmpic(v[0], "connect") == 0)
737 http->gpc = strdup(v[0]);
738 http->hostport = strdup(v[1]);
739 http->ver = strdup(v[2]);
742 /* or it could be any other basic HTTP request type */
743 if ((0 == strcmpic(v[0], "get"))
744 || (0 == strcmpic(v[0], "head"))
745 || (0 == strcmpic(v[0], "post"))
746 || (0 == strcmpic(v[0], "put"))
747 || (0 == strcmpic(v[0], "delete"))
749 /* or a webDAV extension (RFC2518) */
750 || (0 == strcmpic(v[0], "propfind"))
751 || (0 == strcmpic(v[0], "proppatch"))
752 || (0 == strcmpic(v[0], "move"))
753 || (0 == strcmpic(v[0], "copy"))
754 || (0 == strcmpic(v[0], "mkcol"))
755 || (0 == strcmpic(v[0], "lock"))
756 || (0 == strcmpic(v[0], "unlock"))
760 http->gpc = strdup(v[0]);
761 http->url = strdup(v[1]);
762 http->ver = strdup(v[2]);
765 if (strncmpic(url, "http://", 7) == 0)
769 else if (strncmpic(url, "https://", 8) == 0)
780 if ((p = strchr(url, '/')))
782 http->path = strdup(p);
784 http->hostport = strdup(url);
787 * Repair broken HTTP requests that don't contain a path
791 http->path = strdup("/");
792 http->hostport = strdup(url);
801 if (http->hostport == NULL)
803 free_http_request(http);
807 buf = strdup(http->hostport);
810 /* check if url contains password */
811 n = ssplit(buf, "@", v, SZ(v), 1, 1);
814 char * newbuf = NULL;
815 newbuf = strdup(v[1]);
820 n = ssplit(buf, ":", v, SZ(v), 1, 1);
824 http->host = strdup(v[0]);
830 http->host = strdup(v[0]);
831 http->port = atoi(v[1]);
836 if (http->host == NULL)
838 free_http_request(http);
841 if (http->path == NULL)
843 http->path = strdup("/");
849 /* here begins the family of parser functions that reformat header lines */
852 /*********************************************************************
856 * Description : This is called if a header matches a pattern to "crunch"
859 * 1 : v = Pointer to parsers structure, which basically holds
860 * headers (client or server) that we want to "crunch"
861 * 2 : s = header (from sed) to "crunch"
862 * 3 : csp = Current client state (buffers, headers, etc...)
864 * Returns : Always NULL.
866 *********************************************************************/
867 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
869 log_error(LOG_LEVEL_HEADER, "crunch!");
875 /*********************************************************************
877 * Function : server_content_type
879 * Description : Set the content-type for filterable types (text/.*,
880 * javascript and image/gif) unless filtering has been
881 * forbidden (CT_TABOO) while parsing earlier headers.
885 * 2 : s = header string we are "considering"
886 * 3 : csp = Current client state (buffers, headers, etc...)
888 * Returns : A duplicate string pointer to this header (ie. pass thru)
890 *********************************************************************/
891 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
893 if (csp->content_type != CT_TABOO)
895 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
896 csp->content_type = CT_TEXT;
897 else if (strstr(s, " image/gif"))
898 csp->content_type = CT_GIF;
900 csp->content_type = 0;
908 /*********************************************************************
910 * Function : server_transfer_coding
912 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
913 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
914 * - Change from "chunked" to "identity" if body was chunked
915 * but has been de-chunked for filtering.
919 * 2 : s = header string we are "considering"
920 * 3 : csp = Current client state (buffers, headers, etc...)
922 * Returns : A duplicate string pointer to this header (ie. pass thru)
924 *********************************************************************/
925 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
928 * Turn off pcrs and gif filtering if body compressed
930 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
932 csp->content_type = CT_TABOO;
936 * Raise flag if body chunked
938 if (strstr(s, "chunked"))
940 csp->flags |= CSP_FLAG_CHUNKED;
943 * If the body was modified, it has been
944 * de-chunked first, so adjust the header:
946 if (csp->flags & CSP_FLAG_MODIFIED)
948 return(strdup("Transfer-Encoding: identity"));
957 /*********************************************************************
959 * Function : server_content_encoding
961 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
965 * 2 : s = header string we are "considering"
966 * 3 : csp = Current client state (buffers, headers, etc...)
968 * Returns : A duplicate string pointer to this header (ie. pass thru)
970 *********************************************************************/
971 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
974 * Turn off pcrs and gif filtering if body compressed
976 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
978 csp->content_type = CT_TABOO;
986 /*********************************************************************
988 * Function : server_content_length
990 * Description : Adjust Content-Length header if we modified
995 * 2 : s = header string we are "considering"
996 * 3 : csp = Current client state (buffers, headers, etc...)
998 * Returns : A duplicate string pointer to this header (ie. pass thru)
1000 *********************************************************************/
1001 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
1003 if (csp->content_length != 0) /* Content length has been modified */
1005 char * s2 = (char *) zalloc(100);
1006 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
1008 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1019 /*********************************************************************
1021 * Function : server_content_md5
1023 * Description : Crumble any Content-MD5 headers if the document was
1024 * modified. FIXME: Should we re-compute instead?
1028 * 2 : s = header string we are "considering"
1029 * 3 : csp = Current client state (buffers, headers, etc...)
1031 * Returns : A duplicate string pointer to this header (ie. pass thru)
1033 *********************************************************************/
1034 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
1036 if (csp->flags & CSP_FLAG_MODIFIED)
1038 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1049 /*********************************************************************
1051 * Function : client_accept_encoding
1053 * Description : Rewrite the client's Accept-Encoding header so that
1054 * if doesn't allow compression, if the action applies.
1055 * Note: For HTTP/1.0 the absence of the header is enough.
1059 * 2 : s = header string we are "considering"
1060 * 3 : csp = Current client state (buffers, headers, etc...)
1062 * Returns : A copy of the client's original or the modified header.
1064 *********************************************************************/
1065 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
1067 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1073 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1075 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1077 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1088 /*********************************************************************
1090 * Function : client_te
1092 * Description : Rewrite the client's TE header so that
1093 * if doesn't allow compression, if the action applies.
1097 * 2 : s = header string we are "considering"
1098 * 3 : csp = Current client state (buffers, headers, etc...)
1100 * Returns : A copy of the client's original or the modified header.
1102 *********************************************************************/
1103 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1105 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1111 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1117 /*********************************************************************
1119 * Function : client_referrer
1121 * Description : Handle the "referer" config setting properly.
1122 * Called from `sed'.
1126 * 2 : s = header (from sed) to "crunch"
1127 * 3 : csp = Current client state (buffers, headers, etc...)
1129 * Returns : NULL if crunched, or a malloc'ed string with the original
1130 * or modified header
1132 *********************************************************************/
1133 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1135 const char * newval;
1137 #ifdef FEATURE_FORCE_LOAD
1138 /* Since the referrer can include the prefix even
1139 * even if the request itself is non-forced, we must
1140 * clean it unconditionally
1142 strclean(s, FORCE_PREFIX);
1143 #endif /* def FEATURE_FORCE_LOAD */
1146 * Are we sending referer?
1148 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1153 newval = csp->action->string[ACTION_STRING_REFERER];
1156 * Are we blocking referer?
1158 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1160 log_error(LOG_LEVEL_HEADER, "crunch!");
1165 * Are we forging referer?
1167 if (0 == strcmpic(newval, "forge"))
1170 * Forge a referer as http://[hostname:port of REQUEST]/
1171 * to fool stupid checks for in-site links
1173 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1174 s2 = strsav(NULL, "Referer: ");
1175 s2 = strsav(s2, "http://");
1176 s2 = strsav(s2, csp->http->hostport);
1177 s2 = strsav(s2, "/");
1182 * Have we got a fixed referer?
1184 if (0 == strncmpic(newval, "http://", 7))
1187 * We have a specific (fixed) referer we want to send.
1191 log_error(LOG_LEVEL_HEADER, "modified");
1193 s3 = strsav( NULL, "Referer: " );
1194 s3 = strsav( s3, newval );
1198 /* Should never get here! */
1199 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1202 * Forge is probably the best default.
1204 * Forge a referer as http://[hostname:port of REQUEST]/
1205 * to fool stupid checks for in-site links
1207 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1208 s2 = strsav(NULL, "Referer: ");
1209 s2 = strsav(s2, "http://");
1210 s2 = strsav(s2, csp->http->hostport);
1211 s2 = strsav(s2, "/");
1216 /*********************************************************************
1218 * Function : client_uagent
1220 * Description : Handle the "user-agent" config setting properly
1221 * and remember its original value to enable browser
1222 * bug workarounds. Called from `sed'.
1226 * 2 : s = header (from sed) to "crunch"
1227 * 3 : csp = Current client state (buffers, headers, etc...)
1229 * Returns : A malloc'ed pointer to the default agent, or
1230 * a malloc'ed string pointer to this header (ie. pass thru).
1232 *********************************************************************/
1233 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1235 const char * newval;
1238 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1243 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1249 log_error(LOG_LEVEL_HEADER, "modified");
1251 s2 = strsav( NULL, "User-Agent: " );
1252 s2 = strsav( s2, newval );
1258 /*********************************************************************
1260 * Function : client_ua
1262 * Description : Handle "ua-" headers properly. Called from `sed'.
1266 * 2 : s = header (from sed) to "crunch"
1267 * 3 : csp = Current client state (buffers, headers, etc...)
1269 * Returns : NULL if crunched, or a malloc'ed string to original header
1271 *********************************************************************/
1272 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1274 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1280 log_error(LOG_LEVEL_HEADER, "crunch!");
1286 /*********************************************************************
1288 * Function : client_from
1290 * Description : Handle the "from" config setting properly.
1291 * Called from `sed'.
1295 * 2 : s = header (from sed) to "crunch"
1296 * 3 : csp = Current client state (buffers, headers, etc...)
1298 * Returns : NULL if crunched, or a malloc'ed string to
1299 * modified/original header.
1301 *********************************************************************/
1302 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1304 const char * newval;
1307 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1312 newval = csp->action->string[ACTION_STRING_FROM];
1315 * Are we blocking referer?
1317 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1319 log_error(LOG_LEVEL_HEADER, "crunch!");
1323 log_error(LOG_LEVEL_HEADER, " modified");
1325 s2 = strsav( NULL, "From: " );
1326 s2 = strsav( s2, newval );
1332 /*********************************************************************
1334 * Function : client_send_cookie
1336 * Description : Handle the "cookie" header properly. Called from `sed'.
1337 * If cookie is accepted, add it to the cookie_list,
1338 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1341 * 1 : v = pattern of cookie `sed' found matching
1342 * 2 : s = header (from sed) to "crunch"
1343 * 3 : csp = Current client state (buffers, headers, etc...)
1345 * Returns : Always NULL.
1347 *********************************************************************/
1348 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1350 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1352 enlist(csp->cookie_list, s + v->len + 1);
1356 log_error(LOG_LEVEL_HEADER, " crunch!");
1360 * Always return NULL here. The cookie header
1361 * will be sent at the end of the header.
1368 /*********************************************************************
1370 * Function : client_x_forwarded
1372 * Description : Handle the "x-forwarded-for" config setting properly,
1373 * also used in the add_client_headers list. Called from `sed'.
1377 * 2 : s = header (from sed) to "crunch"
1378 * 3 : csp = Current client state (buffers, headers, etc...)
1380 * Returns : Always NULL.
1382 *********************************************************************/
1383 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1385 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1387 /* Save it so we can re-add it later */
1388 csp->x_forwarded = strdup(s);
1392 * Always return NULL, since this information
1393 * will be sent at the end of the header.
1400 /* the following functions add headers directly to the header list */
1402 /*********************************************************************
1404 * Function : client_host_adder
1406 * Description : (re)adds the host header. Called from `sed'.
1409 * 1 : csp = Current client state (buffers, headers, etc...)
1413 *********************************************************************/
1414 void client_host_adder(struct client_state *csp)
1418 p = strsav(p, "Host: ");
1419 p = strsav(p, csp->http->hostport);
1421 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1422 enlist(csp->headers, p);
1428 /*********************************************************************
1430 * Function : client_cookie_adder
1432 * Description : Used in the add_client_headers list. Called from `sed'.
1435 * 1 : csp = Current client state (buffers, headers, etc...)
1439 *********************************************************************/
1440 void client_cookie_adder(struct client_state *csp)
1442 struct list_entry *lst;
1446 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1450 tmp = strsav(tmp, "; ");
1452 tmp = strsav(tmp, lst->str);
1455 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1459 tmp = strsav(tmp, "; ");
1462 if ((e = cookie_encode(lst->str)))
1464 tmp = strsav(tmp, e);
1473 ret = strdup("Cookie: ");
1474 ret = strsav(ret, tmp);
1475 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1476 enlist(csp->headers, ret);
1484 /*********************************************************************
1486 * Function : client_accept_encoding_adder
1488 * Description : Add an Accept-Encoding header to the client's request
1489 * that disables compression if the action applies, and
1490 * the header is not already there. Called from `sed'.
1491 * Note: For HTTP/1.0, the absence of the header is enough.
1494 * 1 : csp = Current client state (buffers, headers, etc...)
1498 *********************************************************************/
1499 void client_accept_encoding_adder(struct client_state *csp)
1501 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1502 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1504 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1510 /*********************************************************************
1512 * Function : client_xtra_adder
1514 * Description : Used in the add_client_headers list. Called from `sed'.
1517 * 1 : csp = Current client state (buffers, headers, etc...)
1521 *********************************************************************/
1522 void client_xtra_adder(struct client_state *csp)
1524 struct list_entry *lst;
1526 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1527 lst ; lst = lst->next)
1529 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1530 enlist(csp->headers, lst->str);
1536 /*********************************************************************
1538 * Function : client_x_forwarded_adder
1540 * Description : Used in the add_client_headers list. Called from `sed'.
1543 * 1 : csp = Current client state (buffers, headers, etc...)
1547 *********************************************************************/
1548 void client_x_forwarded_adder(struct client_state *csp)
1552 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1557 if (csp->x_forwarded)
1559 p = strsav(p, csp->x_forwarded);
1560 p = strsav(p, ", ");
1561 p = strsav(p, csp->ip_addr_str);
1565 p = strsav(p, "X-Forwarded-For: ");
1566 p = strsav(p, csp->ip_addr_str);
1569 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1570 enlist(csp->headers, p);
1576 /*********************************************************************
1578 * Function : connection_close_adder
1580 * Description : Adds a "Connection: close" header to csp->headers
1581 * as a temporary fix for the needed but missing HTTP/1.1
1582 * support. Called from `sed'.
1583 * FIXME: This whole function shouldn't be neccessary!
1586 * 1 : csp = Current client state (buffers, headers, etc...)
1590 *********************************************************************/
1591 void connection_close_adder(struct client_state *csp)
1593 enlist(csp->headers, "Connection: close");
1597 /*********************************************************************
1599 * Function : server_http
1601 * Description : - Save the HTTP Status into csp->http->status
1602 * - Set CT_TABOO to prevent filtering if the answer
1603 * is a partial range (HTTP status 206)
1604 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1608 * 1 : v = parser pattern that matched this header
1609 * 2 : s = header that matched this pattern
1610 * 3 : csp = Current client state (buffers, headers, etc...)
1612 * Returns : Copy of changed or original answer.
1614 *********************************************************************/
1615 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1617 char *ret = strdup(s);
1619 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1620 if (csp->http->status == 206)
1622 csp->content_type = CT_TABOO;
1625 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1628 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1635 /*********************************************************************
1637 * Function : server_set_cookie
1639 * Description : Handle the server "cookie" header properly.
1640 * Log cookie to the jar file. Then "crunch" it,
1641 * or accept it. Called from `sed'.
1644 * 1 : v = parser pattern that matched this header
1645 * 2 : s = header that matched this pattern
1646 * 3 : csp = Current client state (buffers, headers, etc...)
1648 * Returns : `crumble' or a newly malloc'ed string.
1650 *********************************************************************/
1651 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1653 #ifdef FEATURE_COOKIE_JAR
1654 if (csp->config->jar)
1657 * Write timestamp into outbuf.
1659 * Complex because not all OSs have tm_gmtoff or
1660 * the %z field in strftime()
1662 char tempbuf[ BUFFER_SIZE ];
1666 tm_now = localtime (&now);
1667 strftime (tempbuf, BUFFER_SIZE-6, "%b %d %H:%M:%S ", tm_now);
1669 fprintf(csp->config->jar, "%s %s\t%s\n", tempbuf, csp->http->host, (s + v->len + 1));
1671 #endif /* def FEATURE_COOKIE_JAR */
1673 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1675 return(crumble(v, s, csp));
1677 else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)
1679 /* Flag whether or not to log a message */
1682 /* A variable to store the tag we're working on */
1685 /* Make a copy of the header we can write to */
1686 char * result = strdup(s);
1689 /* FIXME: This error handling is incorrect */
1693 /* Skip "Set-Cookie:" (11 characters) in header */
1694 cur_tag = result + 11;
1696 /* skip whitespace between "Set-Cookie:" and value */
1697 while (*cur_tag && ijb_isspace(*cur_tag))
1702 /* Loop through each tag in the cookie */
1706 char * next_tag = strchr(cur_tag, ';');
1707 if (next_tag != NULL)
1709 /* Skip the ';' character itself */
1712 /* skip whitespace ";" and start of tag */
1713 while (*next_tag && ijb_isspace(*next_tag))
1720 /* "Next tag" is the end of the string */
1721 next_tag = cur_tag + strlen(cur_tag);
1724 /* Is this the "Expires" tag? */
1725 if (strncmpic(cur_tag, "expires=", 8) == 0)
1727 /* Delete the tag by copying the rest of the string over it.
1728 * (Note that we cannot just use "strcpy(cur_tag, next_tag)",
1729 * since the behaviour of strcpy is undefined for overlapping
1732 memmove(cur_tag, next_tag, strlen(next_tag) + 1);
1734 /* That changed the header, need to issue a log message */
1737 /* Note that the next tag has now been moved to *cur_tag,
1738 * so we do not need to update the cur_tag pointer.
1743 /* Move on to next cookie tag */
1750 log_error(LOG_LEVEL_HEADER, "Changed cookie to a temporary one.");
1760 #ifdef FEATURE_FORCE_LOAD
1761 /*********************************************************************
1763 * Function : strclean
1765 * Description : In-Situ-Eliminate all occurances of substring in
1769 * 1 : string = string to clean
1770 * 2 : substring = substring to eliminate
1772 * Returns : Number of eliminations
1774 *********************************************************************/
1775 int strclean(const char *string, const char *substring)
1777 int hits = 0, len = strlen(substring);
1780 while((pos = strstr(string, substring)))
1787 while (*p++ != '\0');
1794 #endif /* def FEATURE_FORCE_LOAD */