1 const char parsers_rcs[] = "$Id: parsers.c,v 1.34 2001/10/07 18:50:55 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', `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.34 2001/10/07 18:50:55 oes
45 * Added server_content_encoding, renamed server_transfer_encoding
47 * Revision 1.33 2001/10/07 18:04:49 oes
48 * Changed server_http11 to server_http and its pattern to "HTTP".
49 * Additional functionality: it now saves the HTTP status into
50 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
52 * Revision 1.32 2001/10/07 15:43:28 oes
53 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
54 * client_te and client_accept_encoding_adder, triggered by the new
55 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
56 * changed to allow only identity and chunked, and the TE header is
57 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
59 * parse_http_request no longer does anything than parsing. The rewriting
60 * of http->cmd and version mangling are gone. It now also recognizes
61 * the put and delete methods and saves the url in http->url. Removed
64 * renamed content_type and content_length to have the server_ prefix
66 * server_content_type now only works if csp->content_type != CT_TABOO
68 * added server_transfer_encoding, which
69 * - Sets CT_TABOO to prohibit filtering if encoding compresses
70 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
71 * - Change from "chunked" to "identity" if body was chunked
72 * but has been de-chunked for filtering.
74 * added server_content_md5 which crunches any Content-MD5 headers
75 * if the body was modified.
77 * made server_http11 conditional on +downgrade action
79 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
81 * Revision 1.31 2001/10/05 14:25:02 oes
82 * Crumble Keep-Alive from Server
84 * Revision 1.30 2001/09/29 12:56:03 joergs
85 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
87 * Revision 1.29 2001/09/24 21:09:24 jongfoster
88 * Fixing 2 memory leaks that Guy spotted, where the paramater to
89 * enlist() was not being free()d.
91 * Revision 1.28 2001/09/22 16:32:28 jongfoster
92 * Removing unused #includes.
94 * Revision 1.27 2001/09/20 15:45:25 steudten
96 * add casting from size_t to int for printf()
97 * remove local variable shadow s2
99 * Revision 1.26 2001/09/16 17:05:14 jongfoster
100 * Removing unused #include showarg.h
102 * Revision 1.25 2001/09/16 13:21:27 jongfoster
103 * Changes to use new list functions.
105 * Revision 1.24 2001/09/13 23:05:50 jongfoster
106 * Changing the string paramater to the header parsers a "const".
108 * Revision 1.23 2001/09/12 18:08:19 steudten
110 * In parse_http_request() header rewriting miss the host value, so
111 * from http://www.mydomain.com the result was just " / " not
112 * http://www.mydomain.com/ in case we forward.
114 * Revision 1.22 2001/09/10 10:58:53 oes
115 * Silenced compiler warnings
117 * Revision 1.21 2001/07/31 14:46:00 oes
118 * - Persistant connections now suppressed
119 * - sed() no longer appends empty header to csp->headers
121 * Revision 1.20 2001/07/30 22:08:36 jongfoster
122 * Tidying up #defines:
123 * - All feature #defines are now of the form FEATURE_xxx
124 * - Permanently turned off WIN_GUI_EDIT
125 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
127 * Revision 1.19 2001/07/25 17:21:54 oes
128 * client_uagent now saves copy of User-Agent: header value
130 * Revision 1.18 2001/07/13 14:02:46 oes
131 * - Included fix to repair broken HTTP requests that
132 * don't contain a path, not even '/'.
133 * - Removed all #ifdef PCRS
134 * - content_type now always inspected and classified as
135 * text, gif or other.
136 * - formatting / comments
138 * Revision 1.17 2001/06/29 21:45:41 oes
139 * Indentation, CRLF->LF, Tab-> Space
141 * Revision 1.16 2001/06/29 13:32:42 oes
143 * - Adapted free_http_request
144 * - Removed logentry from cancelled commit
146 * Revision 1.15 2001/06/03 19:12:38 oes
147 * deleted const struct interceptors
149 * Revision 1.14 2001/06/01 18:49:17 jongfoster
150 * Replaced "list_share" with "list" - the tiny memory gain was not
151 * worth the extra complexity.
153 * Revision 1.13 2001/05/31 21:30:33 jongfoster
154 * Removed list code - it's now in list.[ch]
155 * Renamed "permission" to "action", and changed many features
156 * to use the actions file rather than the global config.
158 * Revision 1.12 2001/05/31 17:33:13 oes
162 * Revision 1.11 2001/05/29 20:11:19 joergs
163 * '/ * inside comment' warning removed.
165 * Revision 1.10 2001/05/29 09:50:24 jongfoster
166 * Unified blocklist/imagelist/permissionslist.
167 * File format is still under discussion, but the internal changes
170 * Also modified interceptor behaviour:
171 * - We now intercept all URLs beginning with one of the following
172 * prefixes (and *only* these prefixes):
174 * * http://ijbswa.sf.net/config/
175 * * http://ijbswa.sourceforge.net/config/
176 * - New interceptors "home page" - go to http://i.j.b/ to see it.
177 * - Internal changes so that intercepted and fast redirect pages
178 * are not replaced with an image.
179 * - Interceptors now have the option to send a binary page direct
180 * to the client. (i.e. ijb-send-banner uses this)
181 * - Implemented show-url-info interceptor. (Which is why I needed
182 * the above interceptors changes - a typical URL is
183 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
184 * The previous mechanism would not have intercepted that, and
185 * if it had been intercepted then it then it would have replaced
188 * Revision 1.9 2001/05/28 17:26:33 jongfoster
189 * Fixing segfault if last header was crunched.
190 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
191 * can use the cross-platform sprintf() instead.)
193 * Revision 1.8 2001/05/27 22:17:04 oes
195 * - re_process_buffer no longer writes the modified buffer
196 * to the client, which was very ugly. It now returns the
197 * buffer, which it is then written by chat.
199 * - content_length now adjusts the Content-Length: header
200 * for modified documents rather than crunch()ing it.
201 * (Length info in csp->content_length, which is 0 for
202 * unmodified documents)
204 * - For this to work, sed() is called twice when filtering.
206 * Revision 1.7 2001/05/27 13:19:06 oes
207 * Patched Joergs solution for the content-length in.
209 * Revision 1.6 2001/05/26 13:39:32 jongfoster
210 * Only crunches Content-Length header if applying RE filtering.
211 * Without this fix, Microsoft Windows Update wouldn't work.
213 * Revision 1.5 2001/05/26 00:28:36 jongfoster
214 * Automatic reloading of config file.
215 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
216 * Most of the global variables have been moved to a new
217 * struct configuration_spec, accessed through csp->config->globalname
218 * Most of the globals remaining are used by the Win32 GUI.
220 * Revision 1.4 2001/05/22 18:46:04 oes
222 * - Enabled filtering banners by size rather than URL
223 * by adding patterns that replace all standard banner
224 * sizes with the "Junkbuster" gif to the re_filterfile
226 * - Enabled filtering WebBugs by providing a pattern
227 * which kills all 1x1 images
229 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
230 * which is selected by the (nonstandard and therefore
231 * capital) letter 'U' in the option string.
232 * It causes the quantifiers to be ungreedy by default.
233 * Appending a ? turns back to greedy (!).
235 * - Added a new interceptor ijb-send-banner, which
236 * sends back the "Junkbuster" gif. Without imagelist or
237 * MSIE detection support, or if tinygif = 1, or the
238 * URL isn't recognized as an imageurl, a lame HTML
239 * explanation is sent instead.
241 * - Added new feature, which permits blocking remote
242 * script redirects and firing back a local redirect
244 * The feature is conditionally compiled, i.e. it
245 * can be disabled with --disable-fast-redirects,
246 * plus it must be activated by a "fast-redirects"
247 * line in the config file, has its own log level
248 * and of course wants to be displayed by show-proxy-args
249 * Note: Boy, all the #ifdefs in 1001 locations and
250 * all the fumbling with configure.in and acconfig.h
251 * were *way* more work than the feature itself :-(
253 * - Because a generic redirect template was needed for
254 * this, tinygif = 3 now uses the same.
256 * - Moved GIFs, and other static HTTP response templates
261 * - Removed some >400 CRs again (Jon, you really worked
264 * Revision 1.3 2001/05/20 01:21:20 jongfoster
265 * Version 2.9.4 checkin.
266 * - Merged popupfile and cookiefile, and added control over PCRS
267 * filtering, in new "permissionsfile".
268 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
269 * file error you now get a message box (in the Win32 GUI) rather
270 * than the program exiting with no explanation.
271 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
273 * - Removed tabs from "config"
274 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
275 * - Bumped up version number.
277 * Revision 1.2 2001/05/17 23:02:36 oes
278 * - Made referrer option accept 'L' as a substitute for '§'
280 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
281 * Initial import of version 2.9.3 source tree
284 *********************************************************************/
291 #include <sys/types.h>
309 #include "jbsockets.h"
310 #include "miscutil.h"
312 const char parsers_h_rcs[] = PARSERS_H_VERSION;
314 /* Fix a problem with Solaris. There should be no effect on other
316 * Solaris's isspace() is a macro which uses it's argument directly
317 * as an array index. Therefore we need to make sure that high-bit
318 * characters generate +ve values, and ideally we also want to make
319 * the argument match the declared parameter type of "int".
321 * Why did they write a character function that can't take a simple
322 * "char" argument? Doh!
324 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
325 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
328 const struct parsers client_patterns[] = {
329 { "referer:", 8, client_referrer },
330 { "user-agent:", 11, client_uagent },
331 { "ua-", 3, client_ua },
332 { "from:", 5, client_from },
333 { "cookie:", 7, client_send_cookie },
334 { "x-forwarded-for:", 16, client_x_forwarded },
335 { "Accept-Encoding:", 16, client_accept_encoding },
336 { "TE:", 3, client_te },
337 #if defined(FEATURE_IMAGE_DETECT_MSIE)
338 { "Accept:", 7, client_accept },
339 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
340 #ifdef FEATURE_FORCE_LOAD
341 { "Host:", 5, client_host },
342 #endif /* def FEATURE_FORCE_LOAD */
343 /* { "if-modified-since:", 18, crumble }, */
344 { "Keep-Alive:", 11, crumble },
345 { "connection:", 11, crumble },
346 { "proxy-connection:", 17, crumble },
351 const struct parsers server_patterns[] = {
352 { "HTTP", 4, server_http },
353 { "set-cookie:", 11, server_set_cookie },
354 { "connection:", 11, crumble },
355 { "Content-Type:", 13, server_content_type },
356 { "Content-Length:", 15, server_content_length },
357 { "Content-MD5:", 12, server_content_md5 },
358 { "Content-Encoding:", 17, server_content_encoding },
359 { "Transfer-Encoding:", 18, server_transfer_coding },
360 { "Keep-Alive:", 11, crumble },
365 void (* const add_client_headers[])(struct client_state *) = {
367 client_x_forwarded_adder,
369 client_accept_encoding_adder,
370 connection_close_adder,
375 void (* const add_server_headers[])(struct client_state *) = {
376 connection_close_adder,
381 /*********************************************************************
383 * Function : flush_socket
385 * Description : Write any pending "buffered" content.
388 * 1 : fd = file descriptor of the socket to read
389 * 2 : csp = Current client state (buffers, headers, etc...)
391 * Returns : On success, the number of bytes written are returned (zero
392 * indicates nothing was written). On error, -1 is returned,
393 * and errno is set appropriately. If count is zero and the
394 * file descriptor refers to a regular file, 0 will be
395 * returned without causing any other effect. For a special
396 * file, the results are not portable.
398 *********************************************************************/
399 int flush_socket(int fd, struct client_state *csp)
401 struct iob *iob = csp->iob;
402 int n = iob->eod - iob->cur;
409 n = write_socket(fd, iob->cur, n);
410 iob->eod = iob->cur = iob->buf;
416 /*********************************************************************
418 * Function : add_to_iob
420 * Description : Add content to the buffered page.
423 * 1 : csp = Current client state (buffers, headers, etc...)
424 * 2 : buf = holds the content to be added to the page
425 * 3 : n = number of bytes to be added
427 * Returns : Number of bytes in the content buffer.
429 *********************************************************************/
430 int add_to_iob(struct client_state *csp, char *buf, int n)
432 struct iob *iob = csp->iob;
436 have = iob->eod - iob->cur;
445 if ((p = (char *)malloc(need + 1)) == NULL)
447 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
453 /* there is something in the buffer - save it */
454 memcpy(p, iob->cur, have);
456 /* replace the buffer with the new space */
460 /* point to the end of the data */
465 /* the buffer is empty, free it and reinitialize */
470 /* copy the new data into the iob buffer */
473 /* point to the end of the data */
476 /* null terminate == cheap insurance */
479 /* set the pointers to the new values */
488 /*********************************************************************
490 * Function : get_header
492 * Description : This (odd) routine will parse the csp->iob
495 * 1 : csp = Current client state (buffers, headers, etc...)
497 * Returns : Any one of the following:
499 * 1) a pointer to a dynamically allocated string that contains a header line
500 * 2) NULL indicating that the end of the header was reached
501 * 3) "" indicating that the end of the iob was reached before finding
502 * a complete header line.
504 *********************************************************************/
505 char *get_header(struct client_state *csp)
511 if ((iob->cur == NULL)
512 || ((p = strchr(iob->cur, '\n')) == NULL))
514 return(""); /* couldn't find a complete header */
519 ret = strdup(iob->cur);
523 if ((q = strchr(ret, '\r'))) *q = '\0';
525 /* is this a blank linke (i.e. the end of the header) ? */
537 /*********************************************************************
541 * Description : add, delete or modify lines in the HTTP header streams.
542 * On entry, it receives a linked list of headers space
543 * that was allocated dynamically (both the list nodes
544 * and the header contents).
546 * As a side effect it frees the space used by the original
550 * 1 : pats = list of patterns to match against headers
551 * 2 : more_headers = list of functions to add more
552 * headers (client or server)
553 * 3 : csp = Current client state (buffers, headers, etc...)
555 * Returns : Single pointer to a fully formed header.
557 *********************************************************************/
558 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
560 struct list_entry *p;
561 const struct parsers *v;
565 for (v = pats; v->str ; v++)
567 for (p = csp->headers->first; p ; p = p->next)
569 /* Header crunch()ed in previous run? -> ignore */
570 if (p->str == NULL) continue;
572 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
574 if (strncmpic(p->str, v->str, v->len) == 0)
576 hdr = v->parser(v, p->str, csp);
577 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
583 /* place any additional headers on the csp->headers list */
584 for (f = more_headers; *f ; f++)
589 hdr = list_to_text(csp->headers);
596 /*********************************************************************
598 * Function : free_http_request
600 * Description : Freez a http_request structure
603 * 1 : http = points to a http_request structure to free
607 *********************************************************************/
608 void free_http_request(struct http_request *http)
616 freez(http->hostport);
619 freez(http->host_ip_addr_str);
620 freez(http->user_agent);
625 /*********************************************************************
627 * Function : parse_http_request
629 * Description : Parse out the host and port from the URL. Find the
630 * hostname & path, port (if ':'), and/or password (if '@')
633 * 1 : req = URL (or is it URI?) to break down
634 * 2 : http = pointer to the http structure to hold elements
635 * 3 : csp = Current client state (buffers, headers, etc...)
639 *********************************************************************/
640 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
642 char *buf, *v[10], *url, *p;
645 memset(http, '\0', sizeof(*http));
646 http->cmd = strdup(req);
649 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
653 /* this could be a CONNECT request */
654 if (strcmpic(v[0], "connect") == 0)
657 http->gpc = strdup(v[0]);
658 http->hostport = strdup(v[1]);
659 http->ver = strdup(v[2]);
662 /* or it could be any other basic HTTP request type */
663 if ((0 == strcmpic(v[0], "get"))
664 || (0 == strcmpic(v[0], "head"))
665 || (0 == strcmpic(v[0], "post"))
666 || (0 == strcmpic(v[0], "put"))
667 || (0 == strcmpic(v[0], "delete"))
669 /* or a webDAV extension (RFC2518) */
670 || (0 == strcmpic(v[0], "propfind"))
671 || (0 == strcmpic(v[0], "proppatch"))
672 || (0 == strcmpic(v[0], "move"))
673 || (0 == strcmpic(v[0], "copy"))
674 || (0 == strcmpic(v[0], "mkcol"))
675 || (0 == strcmpic(v[0], "lock"))
676 || (0 == strcmpic(v[0], "unlock"))
680 http->gpc = strdup(v[0]);
681 http->url = strdup(v[1]);
682 http->ver = strdup(v[2]);
685 if (strncmpic(url, "http://", 7) == 0)
689 else if (strncmpic(url, "https://", 8) == 0)
700 if ((p = strchr(url, '/')))
702 http->path = strdup(p);
704 http->hostport = strdup(url);
707 * Repair broken HTTP requests that don't contain a path
711 http->path = strdup("/");
712 http->hostport = strdup(url);
721 if (http->hostport == NULL)
723 free_http_request(http);
727 buf = strdup(http->hostport);
730 /* check if url contains password */
731 n = ssplit(buf, "@", v, SZ(v), 1, 1);
734 char * newbuf = NULL;
735 newbuf = strdup(v[1]);
740 n = ssplit(buf, ":", v, SZ(v), 1, 1);
744 http->host = strdup(v[0]);
750 http->host = strdup(v[0]);
751 http->port = atoi(v[1]);
756 if (http->host == NULL)
758 free_http_request(http);
761 if (http->path == NULL)
763 http->path = strdup("/");
769 /* here begins the family of parser functions that reformat header lines */
772 /*********************************************************************
776 * Description : This is called if a header matches a pattern to "crunch"
779 * 1 : v = Pointer to parsers structure, which basically holds
780 * headers (client or server) that we want to "crunch"
781 * 2 : s = header (from sed) to "crunch"
782 * 3 : csp = Current client state (buffers, headers, etc...)
784 * Returns : Always NULL.
786 *********************************************************************/
787 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
789 log_error(LOG_LEVEL_HEADER, "crunch!");
795 /*********************************************************************
797 * Function : server_content_type
799 * Description : Set the content-type for filterable types (text/.*,
800 * javascript and image/gif) unless filtering has been
801 * forbidden (CT_TABOO) while parsing earlier headers.
805 * 2 : s = header string we are "considering"
806 * 3 : csp = Current client state (buffers, headers, etc...)
808 * Returns : A duplicate string pointer to this header (ie. pass thru)
810 *********************************************************************/
811 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
813 if (csp->content_type != CT_TABOO)
815 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
816 csp->content_type = CT_TEXT;
817 else if (strstr(s, " image/gif"))
818 csp->content_type = CT_GIF;
820 csp->content_type = 0;
828 /*********************************************************************
830 * Function : server_transfer_coding
832 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
833 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
834 * - Change from "chunked" to "identity" if body was chunked
835 * but has been de-chunked for filtering.
839 * 2 : s = header string we are "considering"
840 * 3 : csp = Current client state (buffers, headers, etc...)
842 * Returns : A duplicate string pointer to this header (ie. pass thru)
844 *********************************************************************/
845 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
848 * Turn off pcrs and gif filtering if body compressed
850 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
852 csp->content_type = CT_TABOO;
856 * Raise flag if body chunked
858 if (strstr(s, "chunked"))
860 csp->flags |= CSP_FLAG_CHUNKED;
863 * If the body was modified, it has been
864 * de-chunked first, so adjust the header:
866 if (csp->flags & CSP_FLAG_MODIFIED)
868 return(strdup("Transfer-Encoding: identity"));
877 /*********************************************************************
879 * Function : server_content_encoding
881 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
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_encoding(const struct parsers *v, const char *s, struct client_state *csp)
894 * Turn off pcrs and gif filtering if body compressed
896 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
898 csp->content_type = CT_TABOO;
906 /*********************************************************************
908 * Function : server_content_length
910 * Description : Adjust Content-Length header if we modified
915 * 2 : s = header string we are "considering"
916 * 3 : csp = Current client state (buffers, headers, etc...)
918 * Returns : A duplicate string pointer to this header (ie. pass thru)
920 *********************************************************************/
921 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
923 if (csp->content_length != 0) /* Content length has been modified */
925 char * s2 = (char *) zalloc(100);
926 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
928 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
939 /*********************************************************************
941 * Function : server_content_md5
943 * Description : Crumble any Content-MD5 headers if the document was
944 * modified. FIXME: Should we re-compute instead?
948 * 2 : s = header string we are "considering"
949 * 3 : csp = Current client state (buffers, headers, etc...)
951 * Returns : A duplicate string pointer to this header (ie. pass thru)
953 *********************************************************************/
954 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
956 if (csp->flags & CSP_FLAG_MODIFIED)
958 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
969 /*********************************************************************
971 * Function : client_accept_encoding
973 * Description : Rewrite the client's Accept-Encoding header so that
974 * if doesn't allow compression, if the action applies.
975 * Note: For HTTP/1.0 the absence of the header is enough.
979 * 2 : s = header string we are "considering"
980 * 3 : csp = Current client state (buffers, headers, etc...)
982 * Returns : A copy of the client's original or the modified header.
984 *********************************************************************/
985 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
987 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
993 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
995 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
997 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1008 /*********************************************************************
1010 * Function : client_te
1012 * Description : Rewrite the client's TE header so that
1013 * if doesn't allow compression, if the action applies.
1017 * 2 : s = header string we are "considering"
1018 * 3 : csp = Current client state (buffers, headers, etc...)
1020 * Returns : A copy of the client's original or the modified header.
1022 *********************************************************************/
1023 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1025 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1031 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1037 /*********************************************************************
1039 * Function : client_referrer
1041 * Description : Handle the "referer" config setting properly.
1042 * Called from `sed'.
1046 * 2 : s = header (from sed) to "crunch"
1047 * 3 : csp = Current client state (buffers, headers, etc...)
1049 * Returns : NULL if crunched, or a malloc'ed string with the original
1050 * or modified header
1052 *********************************************************************/
1053 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1055 const char * newval;
1057 #ifdef FEATURE_FORCE_LOAD
1058 /* Since the referrer can include the prefix even
1059 * even if the request itself is non-forced, we must
1060 * clean it unconditionally
1062 strclean(s, FORCE_PREFIX);
1063 #endif /* def FEATURE_FORCE_LOAD */
1065 #ifdef FEATURE_TRUST
1066 csp->referrer = strdup(s);
1067 #endif /* def FEATURE_TRUST */
1070 * Are we sending referer?
1072 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1077 newval = csp->action->string[ACTION_STRING_REFERER];
1080 * Are we blocking referer?
1082 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1084 log_error(LOG_LEVEL_HEADER, "crunch!");
1089 * Are we forging referer?
1091 if (0 == strcmpic(newval, "forge"))
1094 * Forge a referer as http://[hostname:port of REQUEST]/
1095 * to fool stupid checks for in-site links
1097 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1098 s2 = strsav(NULL, "Referer: ");
1099 s2 = strsav(s2, "http://");
1100 s2 = strsav(s2, csp->http->hostport);
1101 s2 = strsav(s2, "/");
1106 * Have we got a fixed referer?
1108 if (0 == strncmpic(newval, "http://", 7))
1111 * We have a specific (fixed) referer we want to send.
1115 log_error(LOG_LEVEL_HEADER, "modified");
1117 s3 = strsav( NULL, "Referer: " );
1118 s3 = strsav( s3, newval );
1122 /* Should never get here! */
1123 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1126 * Forge is probably the best default.
1128 * Forge a referer as http://[hostname:port of REQUEST]/
1129 * to fool stupid checks for in-site links
1131 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1132 s2 = strsav(NULL, "Referer: ");
1133 s2 = strsav(s2, "http://");
1134 s2 = strsav(s2, csp->http->hostport);
1135 s2 = strsav(s2, "/");
1140 /*********************************************************************
1142 * Function : client_uagent
1144 * Description : Handle the "user-agent" config setting properly
1145 * and remember its original value to enable browser
1146 * bug workarounds. Called from `sed'.
1150 * 2 : s = header (from sed) to "crunch"
1151 * 3 : csp = Current client state (buffers, headers, etc...)
1153 * Returns : A malloc'ed pointer to the default agent, or
1154 * a malloc'ed string pointer to this header (ie. pass thru).
1156 *********************************************************************/
1157 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1159 const char * newval;
1162 /* Save the client's User-Agent: value */
1163 if (strlen(s) >= 12)
1165 csp->http->user_agent = strdup(s + 12);
1168 #ifdef FEATURE_IMAGE_DETECT_MSIE
1169 if (strstr (s, "MSIE "))
1171 /* This is Microsoft Internet Explorer.
1172 * Enable auto-detect.
1174 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
1176 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1178 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1183 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1189 log_error(LOG_LEVEL_HEADER, "modified");
1191 s2 = strsav( NULL, "User-Agent: " );
1192 s2 = strsav( s2, newval );
1198 /*********************************************************************
1200 * Function : client_ua
1202 * Description : Handle "ua-" headers properly. Called from `sed'.
1206 * 2 : s = header (from sed) to "crunch"
1207 * 3 : csp = Current client state (buffers, headers, etc...)
1209 * Returns : NULL if crunched, or a malloc'ed string to original header
1211 *********************************************************************/
1212 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1214 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1220 log_error(LOG_LEVEL_HEADER, "crunch!");
1226 /*********************************************************************
1228 * Function : client_from
1230 * Description : Handle the "from" config setting properly.
1231 * Called from `sed'.
1235 * 2 : s = header (from sed) to "crunch"
1236 * 3 : csp = Current client state (buffers, headers, etc...)
1238 * Returns : NULL if crunched, or a malloc'ed string to
1239 * modified/original header.
1241 *********************************************************************/
1242 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1244 const char * newval;
1247 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1252 newval = csp->action->string[ACTION_STRING_FROM];
1255 * Are we blocking referer?
1257 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1259 log_error(LOG_LEVEL_HEADER, "crunch!");
1263 log_error(LOG_LEVEL_HEADER, " modified");
1265 s2 = strsav( NULL, "From: " );
1266 s2 = strsav( s2, newval );
1272 /*********************************************************************
1274 * Function : client_send_cookie
1276 * Description : Handle the "cookie" header properly. Called from `sed'.
1277 * If cookie is accepted, add it to the cookie_list,
1278 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1281 * 1 : v = pattern of cookie `sed' found matching
1282 * 2 : s = header (from sed) to "crunch"
1283 * 3 : csp = Current client state (buffers, headers, etc...)
1285 * Returns : Always NULL.
1287 *********************************************************************/
1288 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1290 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1292 enlist(csp->cookie_list, s + v->len + 1);
1296 log_error(LOG_LEVEL_HEADER, " crunch!");
1300 * Always return NULL here. The cookie header
1301 * will be sent at the end of the header.
1308 /*********************************************************************
1310 * Function : client_x_forwarded
1312 * Description : Handle the "x-forwarded-for" config setting properly,
1313 * also used in the add_client_headers list. Called from `sed'.
1317 * 2 : s = header (from sed) to "crunch"
1318 * 3 : csp = Current client state (buffers, headers, etc...)
1320 * Returns : Always NULL.
1322 *********************************************************************/
1323 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1325 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1327 /* Save it so we can re-add it later */
1328 csp->x_forwarded = strdup(s);
1332 * Always return NULL, since this information
1333 * will be sent at the end of the header.
1340 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1341 /*********************************************************************
1343 * Function : client_accept
1345 * Description : Detect whether the client wants HTML or an image.
1346 * Clients do not always make this information available
1347 * in a sane way. Always passes the header through
1348 * the proxy unchanged.
1352 * 2 : s = Header string. Null terminated.
1353 * 3 : csp = Current client state (buffers, headers, etc...)
1355 * Returns : Duplicate of argument s.
1357 *********************************************************************/
1358 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1360 #ifdef FEATURE_IMAGE_DETECT_MSIE
1361 if (strstr (s, "image/gif"))
1363 /* Client will accept HTML. If this seems counterintuitive,
1366 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1370 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1372 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1377 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1381 /* the following functions add headers directly to the header list */
1384 /*********************************************************************
1386 * Function : client_cookie_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_cookie_adder(struct client_state *csp)
1398 struct list_entry *lst;
1402 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1406 tmp = strsav(tmp, "; ");
1408 tmp = strsav(tmp, lst->str);
1411 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1415 tmp = strsav(tmp, "; ");
1418 if ((e = cookie_encode(lst->str)))
1420 tmp = strsav(tmp, e);
1429 ret = strdup("Cookie: ");
1430 ret = strsav(ret, tmp);
1431 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1432 enlist(csp->headers, ret);
1440 /*********************************************************************
1442 * Function : client_accept_encoding_adder
1444 * Description : Add an Accept-Encoding header to the client's request
1445 * that disables compression if the action applies, and
1446 * the header is not already there. Called from `sed'.
1447 * Note: For HTTP/1.0, the absence of the header is enough.
1450 * 1 : csp = Current client state (buffers, headers, etc...)
1454 *********************************************************************/
1455 void client_accept_encoding_adder(struct client_state *csp)
1457 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1458 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1460 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1466 /*********************************************************************
1468 * Function : client_xtra_adder
1470 * Description : Used in the add_client_headers list. Called from `sed'.
1473 * 1 : csp = Current client state (buffers, headers, etc...)
1477 *********************************************************************/
1478 void client_xtra_adder(struct client_state *csp)
1480 struct list_entry *lst;
1482 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1483 lst ; lst = lst->next)
1485 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1486 enlist(csp->headers, lst->str);
1492 /*********************************************************************
1494 * Function : client_x_forwarded_adder
1496 * Description : Used in the add_client_headers list. Called from `sed'.
1499 * 1 : csp = Current client state (buffers, headers, etc...)
1503 *********************************************************************/
1504 void client_x_forwarded_adder(struct client_state *csp)
1508 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1513 if (csp->x_forwarded)
1515 p = strsav(p, csp->x_forwarded);
1516 p = strsav(p, ", ");
1517 p = strsav(p, csp->ip_addr_str);
1521 p = strsav(p, "X-Forwarded-For: ");
1522 p = strsav(p, csp->ip_addr_str);
1525 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1526 enlist(csp->headers, p);
1532 /*********************************************************************
1534 * Function : connection_close_adder
1536 * Description : Adds a "Connection: close" header to csp->headers
1537 * as a temporary fix for the needed but missing HTTP/1.1
1538 * support. Called from `sed'.
1539 * FIXME: This whole function shouldn't be neccessary!
1542 * 1 : csp = Current client state (buffers, headers, etc...)
1546 *********************************************************************/
1547 void connection_close_adder(struct client_state *csp)
1549 enlist(csp->headers, "Connection: close");
1553 /*********************************************************************
1555 * Function : server_http
1557 * Description : - Save the HTTP Status into csp->http->status
1558 * - Set CT_TABOO to prevent filtering if the answer
1559 * is a partial range (HTTP status 206)
1560 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1564 * 1 : v = parser pattern that matched this header
1565 * 2 : s = header that matched this pattern
1566 * 3 : csp = Current client state (buffers, headers, etc...)
1568 * Returns : Copy of changed or original answer.
1570 *********************************************************************/
1571 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1573 char *ret = strdup(s);
1575 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1576 if (csp->http->status == 206)
1578 csp->content_type = CT_TABOO;
1581 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1584 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1591 /*********************************************************************
1593 * Function : server_set_cookie
1595 * Description : Handle the server "cookie" header properly.
1596 * Log cookie to the jar file. Then "crunch" it,
1597 * or accept it. Called from `sed'.
1600 * 1 : v = parser pattern that matched this header
1601 * 2 : s = header that matched this pattern
1602 * 3 : csp = Current client state (buffers, headers, etc...)
1604 * Returns : `crumble' or a newly malloc'ed string.
1606 *********************************************************************/
1607 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1609 #ifdef FEATURE_COOKIE_JAR
1610 if (csp->config->jar)
1612 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1614 #endif /* def FEATURE_COOKIE_JAR */
1616 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1618 return(crumble(v, s, csp));
1626 #ifdef FEATURE_FORCE_LOAD
1627 /*********************************************************************
1629 * Function : client_host
1631 * Description : Clean the FORCE_PREFIX out of the 'host' http
1632 * header, if we use force
1636 * 2 : s = header (from sed) to clean
1637 * 3 : csp = Current client state (buffers, headers, etc...)
1639 * Returns : A malloc'ed pointer to the cleaned host header
1641 *********************************************************************/
1642 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1644 char *cleanhost = strdup(s);
1646 if(csp->flags & CSP_FLAG_FORCED)
1648 strclean(cleanhost, FORCE_PREFIX);
1653 #endif /* def FEATURE_FORCE_LOAD */
1656 #ifdef FEATURE_FORCE_LOAD
1657 /*********************************************************************
1659 * Function : strclean
1661 * Description : In-Situ-Eliminate all occurances of substring in
1665 * 1 : string = string to clean
1666 * 2 : substring = substring to eliminate
1668 * Returns : Number of eliminations
1670 *********************************************************************/
1671 int strclean(const char *string, const char *substring)
1673 int hits = 0, len = strlen(substring);
1676 while((pos = strstr(string, substring)))
1683 while (*p++ != '\0');
1690 #endif /* def FEATURE_FORCE_LOAD */