1 const char parsers_rcs[] = "$Id: parsers.c,v 1.32 2001/10/07 15:43:28 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.32 2001/10/07 15:43:28 oes
45 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
46 * client_te and client_accept_encoding_adder, triggered by the new
47 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
48 * changed to allow only identity and chunked, and the TE header is
49 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
51 * parse_http_request no longer does anything than parsing. The rewriting
52 * of http->cmd and version mangling are gone. It now also recognizes
53 * the put and delete methods and saves the url in http->url. Removed
56 * renamed content_type and content_length to have the server_ prefix
58 * server_content_type now only works if csp->content_type != CT_TABOO
60 * added server_transfer_encoding, which
61 * - Sets CT_TABOO to prohibit filtering if encoding compresses
62 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
63 * - Change from "chunked" to "identity" if body was chunked
64 * but has been de-chunked for filtering.
66 * added server_content_md5 which crunches any Content-MD5 headers
67 * if the body was modified.
69 * made server_http11 conditional on +downgrade action
71 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
73 * Revision 1.31 2001/10/05 14:25:02 oes
74 * Crumble Keep-Alive from Server
76 * Revision 1.30 2001/09/29 12:56:03 joergs
77 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
79 * Revision 1.29 2001/09/24 21:09:24 jongfoster
80 * Fixing 2 memory leaks that Guy spotted, where the paramater to
81 * enlist() was not being free()d.
83 * Revision 1.28 2001/09/22 16:32:28 jongfoster
84 * Removing unused #includes.
86 * Revision 1.27 2001/09/20 15:45:25 steudten
88 * add casting from size_t to int for printf()
89 * remove local variable shadow s2
91 * Revision 1.26 2001/09/16 17:05:14 jongfoster
92 * Removing unused #include showarg.h
94 * Revision 1.25 2001/09/16 13:21:27 jongfoster
95 * Changes to use new list functions.
97 * Revision 1.24 2001/09/13 23:05:50 jongfoster
98 * Changing the string paramater to the header parsers a "const".
100 * Revision 1.23 2001/09/12 18:08:19 steudten
102 * In parse_http_request() header rewriting miss the host value, so
103 * from http://www.mydomain.com the result was just " / " not
104 * http://www.mydomain.com/ in case we forward.
106 * Revision 1.22 2001/09/10 10:58:53 oes
107 * Silenced compiler warnings
109 * Revision 1.21 2001/07/31 14:46:00 oes
110 * - Persistant connections now suppressed
111 * - sed() no longer appends empty header to csp->headers
113 * Revision 1.20 2001/07/30 22:08:36 jongfoster
114 * Tidying up #defines:
115 * - All feature #defines are now of the form FEATURE_xxx
116 * - Permanently turned off WIN_GUI_EDIT
117 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
119 * Revision 1.19 2001/07/25 17:21:54 oes
120 * client_uagent now saves copy of User-Agent: header value
122 * Revision 1.18 2001/07/13 14:02:46 oes
123 * - Included fix to repair broken HTTP requests that
124 * don't contain a path, not even '/'.
125 * - Removed all #ifdef PCRS
126 * - content_type now always inspected and classified as
127 * text, gif or other.
128 * - formatting / comments
130 * Revision 1.17 2001/06/29 21:45:41 oes
131 * Indentation, CRLF->LF, Tab-> Space
133 * Revision 1.16 2001/06/29 13:32:42 oes
135 * - Adapted free_http_request
136 * - Removed logentry from cancelled commit
138 * Revision 1.15 2001/06/03 19:12:38 oes
139 * deleted const struct interceptors
141 * Revision 1.14 2001/06/01 18:49:17 jongfoster
142 * Replaced "list_share" with "list" - the tiny memory gain was not
143 * worth the extra complexity.
145 * Revision 1.13 2001/05/31 21:30:33 jongfoster
146 * Removed list code - it's now in list.[ch]
147 * Renamed "permission" to "action", and changed many features
148 * to use the actions file rather than the global config.
150 * Revision 1.12 2001/05/31 17:33:13 oes
154 * Revision 1.11 2001/05/29 20:11:19 joergs
155 * '/ * inside comment' warning removed.
157 * Revision 1.10 2001/05/29 09:50:24 jongfoster
158 * Unified blocklist/imagelist/permissionslist.
159 * File format is still under discussion, but the internal changes
162 * Also modified interceptor behaviour:
163 * - We now intercept all URLs beginning with one of the following
164 * prefixes (and *only* these prefixes):
166 * * http://ijbswa.sf.net/config/
167 * * http://ijbswa.sourceforge.net/config/
168 * - New interceptors "home page" - go to http://i.j.b/ to see it.
169 * - Internal changes so that intercepted and fast redirect pages
170 * are not replaced with an image.
171 * - Interceptors now have the option to send a binary page direct
172 * to the client. (i.e. ijb-send-banner uses this)
173 * - Implemented show-url-info interceptor. (Which is why I needed
174 * the above interceptors changes - a typical URL is
175 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
176 * The previous mechanism would not have intercepted that, and
177 * if it had been intercepted then it then it would have replaced
180 * Revision 1.9 2001/05/28 17:26:33 jongfoster
181 * Fixing segfault if last header was crunched.
182 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
183 * can use the cross-platform sprintf() instead.)
185 * Revision 1.8 2001/05/27 22:17:04 oes
187 * - re_process_buffer no longer writes the modified buffer
188 * to the client, which was very ugly. It now returns the
189 * buffer, which it is then written by chat.
191 * - content_length now adjusts the Content-Length: header
192 * for modified documents rather than crunch()ing it.
193 * (Length info in csp->content_length, which is 0 for
194 * unmodified documents)
196 * - For this to work, sed() is called twice when filtering.
198 * Revision 1.7 2001/05/27 13:19:06 oes
199 * Patched Joergs solution for the content-length in.
201 * Revision 1.6 2001/05/26 13:39:32 jongfoster
202 * Only crunches Content-Length header if applying RE filtering.
203 * Without this fix, Microsoft Windows Update wouldn't work.
205 * Revision 1.5 2001/05/26 00:28:36 jongfoster
206 * Automatic reloading of config file.
207 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
208 * Most of the global variables have been moved to a new
209 * struct configuration_spec, accessed through csp->config->globalname
210 * Most of the globals remaining are used by the Win32 GUI.
212 * Revision 1.4 2001/05/22 18:46:04 oes
214 * - Enabled filtering banners by size rather than URL
215 * by adding patterns that replace all standard banner
216 * sizes with the "Junkbuster" gif to the re_filterfile
218 * - Enabled filtering WebBugs by providing a pattern
219 * which kills all 1x1 images
221 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
222 * which is selected by the (nonstandard and therefore
223 * capital) letter 'U' in the option string.
224 * It causes the quantifiers to be ungreedy by default.
225 * Appending a ? turns back to greedy (!).
227 * - Added a new interceptor ijb-send-banner, which
228 * sends back the "Junkbuster" gif. Without imagelist or
229 * MSIE detection support, or if tinygif = 1, or the
230 * URL isn't recognized as an imageurl, a lame HTML
231 * explanation is sent instead.
233 * - Added new feature, which permits blocking remote
234 * script redirects and firing back a local redirect
236 * The feature is conditionally compiled, i.e. it
237 * can be disabled with --disable-fast-redirects,
238 * plus it must be activated by a "fast-redirects"
239 * line in the config file, has its own log level
240 * and of course wants to be displayed by show-proxy-args
241 * Note: Boy, all the #ifdefs in 1001 locations and
242 * all the fumbling with configure.in and acconfig.h
243 * were *way* more work than the feature itself :-(
245 * - Because a generic redirect template was needed for
246 * this, tinygif = 3 now uses the same.
248 * - Moved GIFs, and other static HTTP response templates
253 * - Removed some >400 CRs again (Jon, you really worked
256 * Revision 1.3 2001/05/20 01:21:20 jongfoster
257 * Version 2.9.4 checkin.
258 * - Merged popupfile and cookiefile, and added control over PCRS
259 * filtering, in new "permissionsfile".
260 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
261 * file error you now get a message box (in the Win32 GUI) rather
262 * than the program exiting with no explanation.
263 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
265 * - Removed tabs from "config"
266 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
267 * - Bumped up version number.
269 * Revision 1.2 2001/05/17 23:02:36 oes
270 * - Made referrer option accept 'L' as a substitute for '§'
272 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
273 * Initial import of version 2.9.3 source tree
276 *********************************************************************/
283 #include <sys/types.h>
301 #include "jbsockets.h"
302 #include "miscutil.h"
304 const char parsers_h_rcs[] = PARSERS_H_VERSION;
306 /* Fix a problem with Solaris. There should be no effect on other
308 * Solaris's isspace() is a macro which uses it's argument directly
309 * as an array index. Therefore we need to make sure that high-bit
310 * characters generate +ve values, and ideally we also want to make
311 * the argument match the declared parameter type of "int".
313 * Why did they write a character function that can't take a simple
314 * "char" argument? Doh!
316 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
317 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
320 const struct parsers client_patterns[] = {
321 { "referer:", 8, client_referrer },
322 { "user-agent:", 11, client_uagent },
323 { "ua-", 3, client_ua },
324 { "from:", 5, client_from },
325 { "cookie:", 7, client_send_cookie },
326 { "x-forwarded-for:", 16, client_x_forwarded },
327 { "Accept-Encoding:", 16, client_accept_encoding },
328 { "TE:", 3, client_te },
329 #if defined(FEATURE_IMAGE_DETECT_MSIE)
330 { "Accept:", 7, client_accept },
331 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
332 #ifdef FEATURE_FORCE_LOAD
333 { "Host:", 5, client_host },
334 #endif /* def FEATURE_FORCE_LOAD */
335 /* { "if-modified-since:", 18, crumble }, */
336 { "Keep-Alive:", 11, crumble },
337 { "connection:", 11, crumble },
338 { "proxy-connection:", 17, crumble },
343 const struct parsers server_patterns[] = {
344 { "HTTP", 4, server_http },
345 { "set-cookie:", 11, server_set_cookie },
346 { "connection:", 11, crumble },
347 { "Content-Type:", 13, server_content_type },
348 { "Content-Length:", 15, server_content_length },
349 { "Content-MD5:", 12, server_content_md5 },
350 { "Transfer-Encoding:", 18, server_transfer_encoding },
351 { "Keep-Alive:", 11, crumble },
356 void (* const add_client_headers[])(struct client_state *) = {
358 client_x_forwarded_adder,
360 client_accept_encoding_adder,
361 connection_close_adder,
366 void (* const add_server_headers[])(struct client_state *) = {
367 connection_close_adder,
372 /*********************************************************************
374 * Function : flush_socket
376 * Description : Write any pending "buffered" content.
379 * 1 : fd = file descriptor of the socket to read
380 * 2 : csp = Current client state (buffers, headers, etc...)
382 * Returns : On success, the number of bytes written are returned (zero
383 * indicates nothing was written). On error, -1 is returned,
384 * and errno is set appropriately. If count is zero and the
385 * file descriptor refers to a regular file, 0 will be
386 * returned without causing any other effect. For a special
387 * file, the results are not portable.
389 *********************************************************************/
390 int flush_socket(int fd, struct client_state *csp)
392 struct iob *iob = csp->iob;
393 int n = iob->eod - iob->cur;
400 n = write_socket(fd, iob->cur, n);
401 iob->eod = iob->cur = iob->buf;
407 /*********************************************************************
409 * Function : add_to_iob
411 * Description : Add content to the buffered page.
414 * 1 : csp = Current client state (buffers, headers, etc...)
415 * 2 : buf = holds the content to be added to the page
416 * 3 : n = number of bytes to be added
418 * Returns : Number of bytes in the content buffer.
420 *********************************************************************/
421 int add_to_iob(struct client_state *csp, char *buf, int n)
423 struct iob *iob = csp->iob;
427 have = iob->eod - iob->cur;
436 if ((p = (char *)malloc(need + 1)) == NULL)
438 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
444 /* there is something in the buffer - save it */
445 memcpy(p, iob->cur, have);
447 /* replace the buffer with the new space */
451 /* point to the end of the data */
456 /* the buffer is empty, free it and reinitialize */
461 /* copy the new data into the iob buffer */
464 /* point to the end of the data */
467 /* null terminate == cheap insurance */
470 /* set the pointers to the new values */
479 /*********************************************************************
481 * Function : get_header
483 * Description : This (odd) routine will parse the csp->iob
486 * 1 : csp = Current client state (buffers, headers, etc...)
488 * Returns : Any one of the following:
490 * 1) a pointer to a dynamically allocated string that contains a header line
491 * 2) NULL indicating that the end of the header was reached
492 * 3) "" indicating that the end of the iob was reached before finding
493 * a complete header line.
495 *********************************************************************/
496 char *get_header(struct client_state *csp)
502 if ((iob->cur == NULL)
503 || ((p = strchr(iob->cur, '\n')) == NULL))
505 return(""); /* couldn't find a complete header */
510 ret = strdup(iob->cur);
514 if ((q = strchr(ret, '\r'))) *q = '\0';
516 /* is this a blank linke (i.e. the end of the header) ? */
528 /*********************************************************************
532 * Description : add, delete or modify lines in the HTTP header streams.
533 * On entry, it receives a linked list of headers space
534 * that was allocated dynamically (both the list nodes
535 * and the header contents).
537 * As a side effect it frees the space used by the original
541 * 1 : pats = list of patterns to match against headers
542 * 2 : more_headers = list of functions to add more
543 * headers (client or server)
544 * 3 : csp = Current client state (buffers, headers, etc...)
546 * Returns : Single pointer to a fully formed header.
548 *********************************************************************/
549 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
551 struct list_entry *p;
552 const struct parsers *v;
556 for (v = pats; v->str ; v++)
558 for (p = csp->headers->first; p ; p = p->next)
560 /* Header crunch()ed in previous run? -> ignore */
561 if (p->str == NULL) continue;
563 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
565 if (strncmpic(p->str, v->str, v->len) == 0)
567 hdr = v->parser(v, p->str, csp);
568 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
574 /* place any additional headers on the csp->headers list */
575 for (f = more_headers; *f ; f++)
580 hdr = list_to_text(csp->headers);
587 /*********************************************************************
589 * Function : free_http_request
591 * Description : Freez a http_request structure
594 * 1 : http = points to a http_request structure to free
598 *********************************************************************/
599 void free_http_request(struct http_request *http)
607 freez(http->hostport);
610 freez(http->host_ip_addr_str);
611 freez(http->user_agent);
616 /*********************************************************************
618 * Function : parse_http_request
620 * Description : Parse out the host and port from the URL. Find the
621 * hostname & path, port (if ':'), and/or password (if '@')
624 * 1 : req = URL (or is it URI?) to break down
625 * 2 : http = pointer to the http structure to hold elements
626 * 3 : csp = Current client state (buffers, headers, etc...)
630 *********************************************************************/
631 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
633 char *buf, *v[10], *url, *p;
636 memset(http, '\0', sizeof(*http));
637 http->cmd = strdup(req);
640 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
644 /* this could be a CONNECT request */
645 if (strcmpic(v[0], "connect") == 0)
648 http->gpc = strdup(v[0]);
649 http->hostport = strdup(v[1]);
650 http->ver = strdup(v[2]);
653 /* or it could be any other basic HTTP request type */
654 if ((0 == strcmpic(v[0], "get"))
655 || (0 == strcmpic(v[0], "head"))
656 || (0 == strcmpic(v[0], "post"))
657 || (0 == strcmpic(v[0], "put"))
658 || (0 == strcmpic(v[0], "delete"))
660 /* or a webDAV extension (RFC2518) */
661 || (0 == strcmpic(v[0], "propfind"))
662 || (0 == strcmpic(v[0], "proppatch"))
663 || (0 == strcmpic(v[0], "move"))
664 || (0 == strcmpic(v[0], "copy"))
665 || (0 == strcmpic(v[0], "mkcol"))
666 || (0 == strcmpic(v[0], "lock"))
667 || (0 == strcmpic(v[0], "unlock"))
671 http->gpc = strdup(v[0]);
672 http->url = strdup(v[1]);
673 http->ver = strdup(v[2]);
676 if (strncmpic(url, "http://", 7) == 0)
680 else if (strncmpic(url, "https://", 8) == 0)
691 if ((p = strchr(url, '/')))
693 http->path = strdup(p);
695 http->hostport = strdup(url);
698 * Repair broken HTTP requests that don't contain a path
702 http->path = strdup("/");
703 http->hostport = strdup(url);
712 if (http->hostport == NULL)
714 free_http_request(http);
718 buf = strdup(http->hostport);
721 /* check if url contains password */
722 n = ssplit(buf, "@", v, SZ(v), 1, 1);
725 char * newbuf = NULL;
726 newbuf = strdup(v[1]);
731 n = ssplit(buf, ":", v, SZ(v), 1, 1);
735 http->host = strdup(v[0]);
741 http->host = strdup(v[0]);
742 http->port = atoi(v[1]);
747 if (http->host == NULL)
749 free_http_request(http);
752 if (http->path == NULL)
754 http->path = strdup("/");
760 /* here begins the family of parser functions that reformat header lines */
763 /*********************************************************************
767 * Description : This is called if a header matches a pattern to "crunch"
770 * 1 : v = Pointer to parsers structure, which basically holds
771 * headers (client or server) that we want to "crunch"
772 * 2 : s = header (from sed) to "crunch"
773 * 3 : csp = Current client state (buffers, headers, etc...)
775 * Returns : Always NULL.
777 *********************************************************************/
778 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
780 log_error(LOG_LEVEL_HEADER, "crunch!");
786 /*********************************************************************
788 * Function : server_content_type
790 * Description : Set the content-type for filterable types (text/.*,
791 * javascript and image/gif) unless filtering has been
792 * forbidden (CT_TABOO) while parsing earlier headers.
796 * 2 : s = header string we are "considering"
797 * 3 : csp = Current client state (buffers, headers, etc...)
799 * Returns : A duplicate string pointer to this header (ie. pass thru)
801 *********************************************************************/
802 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
804 if (csp->content_type != CT_TABOO)
806 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
807 csp->content_type = CT_TEXT;
808 else if (strstr(s, " image/gif"))
809 csp->content_type = CT_GIF;
811 csp->content_type = 0;
819 /*********************************************************************
821 * Function : server_transfer_encoding
823 * Description : - Prohibit filtering (CT_TABOO) if encoding compresses
824 * - Raise the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
825 * - Change from "chunked" to "identity" if body was chunked
826 * but has been de-chunked for filtering.
830 * 2 : s = header string we are "considering"
831 * 3 : csp = Current client state (buffers, headers, etc...)
833 * Returns : A duplicate string pointer to this header (ie. pass thru)
835 *********************************************************************/
836 char *server_transfer_encoding(const struct parsers *v, const char *s, struct client_state *csp)
839 * Turn off pcrs and gif filtering if body compressed
841 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
843 csp->content_type = CT_TABOO;
847 * Raise flag if body chunked
849 if (strstr(s, "chunked"))
851 csp->flags |= CSP_FLAG_CHUNKED;
854 * If the body was modified, it has been
855 * de-chunked first, so adjust the header:
857 if (csp->flags & CSP_FLAG_MODIFIED)
859 return(strdup("Transfer-Encoding: identity"));
868 /*********************************************************************
870 * Function : server_content_length
872 * Description : Adjust Content-Length header if we modified
877 * 2 : s = header string we are "considering"
878 * 3 : csp = Current client state (buffers, headers, etc...)
880 * Returns : A duplicate string pointer to this header (ie. pass thru)
882 *********************************************************************/
883 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
885 if (csp->content_length != 0) /* Content length has been modified */
887 char * s2 = (char *) zalloc(100);
888 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
890 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
901 /*********************************************************************
903 * Function : server_content_md5
905 * Description : Crumble any Content-MD5 headers if the document was
906 * modified. FIXME: Should we re-compute instead?
910 * 2 : s = header string we are "considering"
911 * 3 : csp = Current client state (buffers, headers, etc...)
913 * Returns : A duplicate string pointer to this header (ie. pass thru)
915 *********************************************************************/
916 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
918 if (csp->flags & CSP_FLAG_MODIFIED)
920 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
931 /*********************************************************************
933 * Function : client_accept_encoding
935 * Description : Rewrite the client's Accept-Encoding header so that
936 * if doesn't allow compression, if the action applies.
937 * Note: For HTTP/1.0 the absence of the header is enough.
941 * 2 : s = header string we are "considering"
942 * 3 : csp = Current client state (buffers, headers, etc...)
944 * Returns : A copy of the client's original or the modified header.
946 *********************************************************************/
947 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
949 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
955 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
957 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
959 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
970 /*********************************************************************
972 * Function : client_te
974 * Description : Rewrite the client's TE header so that
975 * if doesn't allow compression, if the action applies.
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_te(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 transfer");
999 /*********************************************************************
1001 * Function : client_referrer
1003 * Description : Handle the "referer" config setting properly.
1004 * Called from `sed'.
1008 * 2 : s = header (from sed) to "crunch"
1009 * 3 : csp = Current client state (buffers, headers, etc...)
1011 * Returns : NULL if crunched, or a malloc'ed string with the original
1012 * or modified header
1014 *********************************************************************/
1015 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1017 const char * newval;
1019 #ifdef FEATURE_FORCE_LOAD
1020 /* Since the referrer can include the prefix even
1021 * even if the request itself is non-forced, we must
1022 * clean it unconditionally
1024 strclean(s, FORCE_PREFIX);
1025 #endif /* def FEATURE_FORCE_LOAD */
1027 #ifdef FEATURE_TRUST
1028 csp->referrer = strdup(s);
1029 #endif /* def FEATURE_TRUST */
1032 * Are we sending referer?
1034 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1039 newval = csp->action->string[ACTION_STRING_REFERER];
1042 * Are we blocking referer?
1044 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1046 log_error(LOG_LEVEL_HEADER, "crunch!");
1051 * Are we forging referer?
1053 if (0 == strcmpic(newval, "forge"))
1056 * Forge a referer as http://[hostname:port of REQUEST]/
1057 * to fool stupid checks for in-site links
1059 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1060 s2 = strsav(NULL, "Referer: ");
1061 s2 = strsav(s2, "http://");
1062 s2 = strsav(s2, csp->http->hostport);
1063 s2 = strsav(s2, "/");
1068 * Have we got a fixed referer?
1070 if (0 == strncmpic(newval, "http://", 7))
1073 * We have a specific (fixed) referer we want to send.
1077 log_error(LOG_LEVEL_HEADER, "modified");
1079 s3 = strsav( NULL, "Referer: " );
1080 s3 = strsav( s3, newval );
1084 /* Should never get here! */
1085 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1088 * Forge is probably the best default.
1090 * Forge a referer as http://[hostname:port of REQUEST]/
1091 * to fool stupid checks for in-site links
1093 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1094 s2 = strsav(NULL, "Referer: ");
1095 s2 = strsav(s2, "http://");
1096 s2 = strsav(s2, csp->http->hostport);
1097 s2 = strsav(s2, "/");
1102 /*********************************************************************
1104 * Function : client_uagent
1106 * Description : Handle the "user-agent" config setting properly
1107 * and remember its original value to enable browser
1108 * bug workarounds. Called from `sed'.
1112 * 2 : s = header (from sed) to "crunch"
1113 * 3 : csp = Current client state (buffers, headers, etc...)
1115 * Returns : A malloc'ed pointer to the default agent, or
1116 * a malloc'ed string pointer to this header (ie. pass thru).
1118 *********************************************************************/
1119 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1121 const char * newval;
1124 /* Save the client's User-Agent: value */
1125 if (strlen(s) >= 12)
1127 csp->http->user_agent = strdup(s + 12);
1130 #ifdef FEATURE_IMAGE_DETECT_MSIE
1131 if (strstr (s, "MSIE "))
1133 /* This is Microsoft Internet Explorer.
1134 * Enable auto-detect.
1136 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
1138 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1140 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1145 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1151 log_error(LOG_LEVEL_HEADER, "modified");
1153 s2 = strsav( NULL, "User-Agent: " );
1154 s2 = strsav( s2, newval );
1160 /*********************************************************************
1162 * Function : client_ua
1164 * Description : Handle "ua-" headers properly. Called from `sed'.
1168 * 2 : s = header (from sed) to "crunch"
1169 * 3 : csp = Current client state (buffers, headers, etc...)
1171 * Returns : NULL if crunched, or a malloc'ed string to original header
1173 *********************************************************************/
1174 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1176 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1182 log_error(LOG_LEVEL_HEADER, "crunch!");
1188 /*********************************************************************
1190 * Function : client_from
1192 * Description : Handle the "from" config setting properly.
1193 * Called from `sed'.
1197 * 2 : s = header (from sed) to "crunch"
1198 * 3 : csp = Current client state (buffers, headers, etc...)
1200 * Returns : NULL if crunched, or a malloc'ed string to
1201 * modified/original header.
1203 *********************************************************************/
1204 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1206 const char * newval;
1209 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1214 newval = csp->action->string[ACTION_STRING_FROM];
1217 * Are we blocking referer?
1219 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1221 log_error(LOG_LEVEL_HEADER, "crunch!");
1225 log_error(LOG_LEVEL_HEADER, " modified");
1227 s2 = strsav( NULL, "From: " );
1228 s2 = strsav( s2, newval );
1234 /*********************************************************************
1236 * Function : client_send_cookie
1238 * Description : Handle the "cookie" header properly. Called from `sed'.
1239 * If cookie is accepted, add it to the cookie_list,
1240 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1243 * 1 : v = pattern of cookie `sed' found matching
1244 * 2 : s = header (from sed) to "crunch"
1245 * 3 : csp = Current client state (buffers, headers, etc...)
1247 * Returns : Always NULL.
1249 *********************************************************************/
1250 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1252 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1254 enlist(csp->cookie_list, s + v->len + 1);
1258 log_error(LOG_LEVEL_HEADER, " crunch!");
1262 * Always return NULL here. The cookie header
1263 * will be sent at the end of the header.
1270 /*********************************************************************
1272 * Function : client_x_forwarded
1274 * Description : Handle the "x-forwarded-for" config setting properly,
1275 * also used in the add_client_headers list. Called from `sed'.
1279 * 2 : s = header (from sed) to "crunch"
1280 * 3 : csp = Current client state (buffers, headers, etc...)
1282 * Returns : Always NULL.
1284 *********************************************************************/
1285 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1287 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1289 /* Save it so we can re-add it later */
1290 csp->x_forwarded = strdup(s);
1294 * Always return NULL, since this information
1295 * will be sent at the end of the header.
1302 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1303 /*********************************************************************
1305 * Function : client_accept
1307 * Description : Detect whether the client wants HTML or an image.
1308 * Clients do not always make this information available
1309 * in a sane way. Always passes the header through
1310 * the proxy unchanged.
1314 * 2 : s = Header string. Null terminated.
1315 * 3 : csp = Current client state (buffers, headers, etc...)
1317 * Returns : Duplicate of argument s.
1319 *********************************************************************/
1320 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1322 #ifdef FEATURE_IMAGE_DETECT_MSIE
1323 if (strstr (s, "image/gif"))
1325 /* Client will accept HTML. If this seems counterintuitive,
1328 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1332 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1334 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1339 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1343 /* the following functions add headers directly to the header list */
1346 /*********************************************************************
1348 * Function : client_cookie_adder
1350 * Description : Used in the add_client_headers list. Called from `sed'.
1353 * 1 : csp = Current client state (buffers, headers, etc...)
1357 *********************************************************************/
1358 void client_cookie_adder(struct client_state *csp)
1360 struct list_entry *lst;
1364 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1368 tmp = strsav(tmp, "; ");
1370 tmp = strsav(tmp, lst->str);
1373 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1377 tmp = strsav(tmp, "; ");
1380 if ((e = cookie_encode(lst->str)))
1382 tmp = strsav(tmp, e);
1391 ret = strdup("Cookie: ");
1392 ret = strsav(ret, tmp);
1393 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1394 enlist(csp->headers, ret);
1402 /*********************************************************************
1404 * Function : client_accept_encoding_adder
1406 * Description : Add an Accept-Encoding header to the client's request
1407 * that disables compression if the action applies, and
1408 * the header is not already there. Called from `sed'.
1409 * Note: For HTTP/1.0, the absence of the header is enough.
1412 * 1 : csp = Current client state (buffers, headers, etc...)
1416 *********************************************************************/
1417 void client_accept_encoding_adder(struct client_state *csp)
1419 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1420 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1422 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1428 /*********************************************************************
1430 * Function : client_xtra_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_xtra_adder(struct client_state *csp)
1442 struct list_entry *lst;
1444 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1445 lst ; lst = lst->next)
1447 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1448 enlist(csp->headers, lst->str);
1454 /*********************************************************************
1456 * Function : client_x_forwarded_adder
1458 * Description : Used in the add_client_headers list. Called from `sed'.
1461 * 1 : csp = Current client state (buffers, headers, etc...)
1465 *********************************************************************/
1466 void client_x_forwarded_adder(struct client_state *csp)
1470 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1475 if (csp->x_forwarded)
1477 p = strsav(p, csp->x_forwarded);
1478 p = strsav(p, ", ");
1479 p = strsav(p, csp->ip_addr_str);
1483 p = strsav(p, "X-Forwarded-For: ");
1484 p = strsav(p, csp->ip_addr_str);
1487 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1488 enlist(csp->headers, p);
1494 /*********************************************************************
1496 * Function : connection_close_adder
1498 * Description : Adds a "Connection: close" header to csp->headers
1499 * as a temporary fix for the needed but missing HTTP/1.1
1500 * support. Called from `sed'.
1501 * FIXME: This whole function shouldn't be neccessary!
1504 * 1 : csp = Current client state (buffers, headers, etc...)
1508 *********************************************************************/
1509 void connection_close_adder(struct client_state *csp)
1511 enlist(csp->headers, "Connection: close");
1515 /*********************************************************************
1517 * Function : server_http
1519 * Description : - Save the HTTP Status into csp->http->status
1520 * - Set CT_TABOO to prevent filtering if the answer
1521 * is a partial range (HTTP status 206)
1522 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1526 * 1 : v = parser pattern that matched this header
1527 * 2 : s = header that matched this pattern
1528 * 3 : csp = Current client state (buffers, headers, etc...)
1530 * Returns : Copy of changed or original answer.
1532 *********************************************************************/
1533 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1535 char *ret = strdup(s);
1537 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1538 if (csp->http->status == 206)
1540 csp->content_type = CT_TABOO;
1543 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1546 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1553 /*********************************************************************
1555 * Function : server_set_cookie
1557 * Description : Handle the server "cookie" header properly.
1558 * Log cookie to the jar file. Then "crunch" it,
1559 * or accept it. Called from `sed'.
1562 * 1 : v = parser pattern that matched this header
1563 * 2 : s = header that matched this pattern
1564 * 3 : csp = Current client state (buffers, headers, etc...)
1566 * Returns : `crumble' or a newly malloc'ed string.
1568 *********************************************************************/
1569 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1571 #ifdef FEATURE_COOKIE_JAR
1572 if (csp->config->jar)
1574 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1576 #endif /* def FEATURE_COOKIE_JAR */
1578 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1580 return(crumble(v, s, csp));
1588 #ifdef FEATURE_FORCE_LOAD
1589 /*********************************************************************
1591 * Function : client_host
1593 * Description : Clean the FORCE_PREFIX out of the 'host' http
1594 * header, if we use force
1598 * 2 : s = header (from sed) to clean
1599 * 3 : csp = Current client state (buffers, headers, etc...)
1601 * Returns : A malloc'ed pointer to the cleaned host header
1603 *********************************************************************/
1604 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1606 char *cleanhost = strdup(s);
1608 if(csp->flags & CSP_FLAG_FORCED)
1610 strclean(cleanhost, FORCE_PREFIX);
1615 #endif /* def FEATURE_FORCE_LOAD */
1618 #ifdef FEATURE_FORCE_LOAD
1619 /*********************************************************************
1621 * Function : strclean
1623 * Description : In-Situ-Eliminate all occurances of substring in
1627 * 1 : string = string to clean
1628 * 2 : substring = substring to eliminate
1630 * Returns : Number of eliminations
1632 *********************************************************************/
1633 int strclean(const char *string, const char *substring)
1635 int hits = 0, len = strlen(substring);
1638 while((pos = strstr(string, substring)))
1645 while (*p++ != '\0');
1652 #endif /* def FEATURE_FORCE_LOAD */