1 const char parsers_rcs[] = "$Id: parsers.c,v 1.37 2001/10/23 21:36:02 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.37 2001/10/23 21:36:02 jongfoster
45 * Documenting sed()'s error behaviou (doc change only)
47 * Revision 1.36 2001/10/13 12:51:51 joergs
48 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
49 * force-load), instead crumble Host: and add it (again) in client_host_adder
50 * (in case we get a HTTP/1.0 request without Host: header and forward it to
51 * a HTTP/1.1 server/proxy).
53 * Revision 1.35 2001/10/09 22:39:21 jongfoster
54 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
57 * Revision 1.34 2001/10/07 18:50:55 oes
58 * Added server_content_encoding, renamed server_transfer_encoding
60 * Revision 1.33 2001/10/07 18:04:49 oes
61 * Changed server_http11 to server_http and its pattern to "HTTP".
62 * Additional functionality: it now saves the HTTP status into
63 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
65 * Revision 1.32 2001/10/07 15:43:28 oes
66 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
67 * client_te and client_accept_encoding_adder, triggered by the new
68 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
69 * changed to allow only identity and chunked, and the TE header is
70 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
72 * parse_http_request no longer does anything than parsing. The rewriting
73 * of http->cmd and version mangling are gone. It now also recognizes
74 * the put and delete methods and saves the url in http->url. Removed
77 * renamed content_type and content_length to have the server_ prefix
79 * server_content_type now only works if csp->content_type != CT_TABOO
81 * added server_transfer_encoding, which
82 * - Sets CT_TABOO to prohibit filtering if encoding compresses
83 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
84 * - Change from "chunked" to "identity" if body was chunked
85 * but has been de-chunked for filtering.
87 * added server_content_md5 which crunches any Content-MD5 headers
88 * if the body was modified.
90 * made server_http11 conditional on +downgrade action
92 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
94 * Revision 1.31 2001/10/05 14:25:02 oes
95 * Crumble Keep-Alive from Server
97 * Revision 1.30 2001/09/29 12:56:03 joergs
98 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
100 * Revision 1.29 2001/09/24 21:09:24 jongfoster
101 * Fixing 2 memory leaks that Guy spotted, where the paramater to
102 * enlist() was not being free()d.
104 * Revision 1.28 2001/09/22 16:32:28 jongfoster
105 * Removing unused #includes.
107 * Revision 1.27 2001/09/20 15:45:25 steudten
109 * add casting from size_t to int for printf()
110 * remove local variable shadow s2
112 * Revision 1.26 2001/09/16 17:05:14 jongfoster
113 * Removing unused #include showarg.h
115 * Revision 1.25 2001/09/16 13:21:27 jongfoster
116 * Changes to use new list functions.
118 * Revision 1.24 2001/09/13 23:05:50 jongfoster
119 * Changing the string paramater to the header parsers a "const".
121 * Revision 1.23 2001/09/12 18:08:19 steudten
123 * In parse_http_request() header rewriting miss the host value, so
124 * from http://www.mydomain.com the result was just " / " not
125 * http://www.mydomain.com/ in case we forward.
127 * Revision 1.22 2001/09/10 10:58:53 oes
128 * Silenced compiler warnings
130 * Revision 1.21 2001/07/31 14:46:00 oes
131 * - Persistant connections now suppressed
132 * - sed() no longer appends empty header to csp->headers
134 * Revision 1.20 2001/07/30 22:08:36 jongfoster
135 * Tidying up #defines:
136 * - All feature #defines are now of the form FEATURE_xxx
137 * - Permanently turned off WIN_GUI_EDIT
138 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
140 * Revision 1.19 2001/07/25 17:21:54 oes
141 * client_uagent now saves copy of User-Agent: header value
143 * Revision 1.18 2001/07/13 14:02:46 oes
144 * - Included fix to repair broken HTTP requests that
145 * don't contain a path, not even '/'.
146 * - Removed all #ifdef PCRS
147 * - content_type now always inspected and classified as
148 * text, gif or other.
149 * - formatting / comments
151 * Revision 1.17 2001/06/29 21:45:41 oes
152 * Indentation, CRLF->LF, Tab-> Space
154 * Revision 1.16 2001/06/29 13:32:42 oes
156 * - Adapted free_http_request
157 * - Removed logentry from cancelled commit
159 * Revision 1.15 2001/06/03 19:12:38 oes
160 * deleted const struct interceptors
162 * Revision 1.14 2001/06/01 18:49:17 jongfoster
163 * Replaced "list_share" with "list" - the tiny memory gain was not
164 * worth the extra complexity.
166 * Revision 1.13 2001/05/31 21:30:33 jongfoster
167 * Removed list code - it's now in list.[ch]
168 * Renamed "permission" to "action", and changed many features
169 * to use the actions file rather than the global config.
171 * Revision 1.12 2001/05/31 17:33:13 oes
175 * Revision 1.11 2001/05/29 20:11:19 joergs
176 * '/ * inside comment' warning removed.
178 * Revision 1.10 2001/05/29 09:50:24 jongfoster
179 * Unified blocklist/imagelist/permissionslist.
180 * File format is still under discussion, but the internal changes
183 * Also modified interceptor behaviour:
184 * - We now intercept all URLs beginning with one of the following
185 * prefixes (and *only* these prefixes):
187 * * http://ijbswa.sf.net/config/
188 * * http://ijbswa.sourceforge.net/config/
189 * - New interceptors "home page" - go to http://i.j.b/ to see it.
190 * - Internal changes so that intercepted and fast redirect pages
191 * are not replaced with an image.
192 * - Interceptors now have the option to send a binary page direct
193 * to the client. (i.e. ijb-send-banner uses this)
194 * - Implemented show-url-info interceptor. (Which is why I needed
195 * the above interceptors changes - a typical URL is
196 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
197 * The previous mechanism would not have intercepted that, and
198 * if it had been intercepted then it then it would have replaced
201 * Revision 1.9 2001/05/28 17:26:33 jongfoster
202 * Fixing segfault if last header was crunched.
203 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
204 * can use the cross-platform sprintf() instead.)
206 * Revision 1.8 2001/05/27 22:17:04 oes
208 * - re_process_buffer no longer writes the modified buffer
209 * to the client, which was very ugly. It now returns the
210 * buffer, which it is then written by chat.
212 * - content_length now adjusts the Content-Length: header
213 * for modified documents rather than crunch()ing it.
214 * (Length info in csp->content_length, which is 0 for
215 * unmodified documents)
217 * - For this to work, sed() is called twice when filtering.
219 * Revision 1.7 2001/05/27 13:19:06 oes
220 * Patched Joergs solution for the content-length in.
222 * Revision 1.6 2001/05/26 13:39:32 jongfoster
223 * Only crunches Content-Length header if applying RE filtering.
224 * Without this fix, Microsoft Windows Update wouldn't work.
226 * Revision 1.5 2001/05/26 00:28:36 jongfoster
227 * Automatic reloading of config file.
228 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
229 * Most of the global variables have been moved to a new
230 * struct configuration_spec, accessed through csp->config->globalname
231 * Most of the globals remaining are used by the Win32 GUI.
233 * Revision 1.4 2001/05/22 18:46:04 oes
235 * - Enabled filtering banners by size rather than URL
236 * by adding patterns that replace all standard banner
237 * sizes with the "Junkbuster" gif to the re_filterfile
239 * - Enabled filtering WebBugs by providing a pattern
240 * which kills all 1x1 images
242 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
243 * which is selected by the (nonstandard and therefore
244 * capital) letter 'U' in the option string.
245 * It causes the quantifiers to be ungreedy by default.
246 * Appending a ? turns back to greedy (!).
248 * - Added a new interceptor ijb-send-banner, which
249 * sends back the "Junkbuster" gif. Without imagelist or
250 * MSIE detection support, or if tinygif = 1, or the
251 * URL isn't recognized as an imageurl, a lame HTML
252 * explanation is sent instead.
254 * - Added new feature, which permits blocking remote
255 * script redirects and firing back a local redirect
257 * The feature is conditionally compiled, i.e. it
258 * can be disabled with --disable-fast-redirects,
259 * plus it must be activated by a "fast-redirects"
260 * line in the config file, has its own log level
261 * and of course wants to be displayed by show-proxy-args
262 * Note: Boy, all the #ifdefs in 1001 locations and
263 * all the fumbling with configure.in and acconfig.h
264 * were *way* more work than the feature itself :-(
266 * - Because a generic redirect template was needed for
267 * this, tinygif = 3 now uses the same.
269 * - Moved GIFs, and other static HTTP response templates
274 * - Removed some >400 CRs again (Jon, you really worked
277 * Revision 1.3 2001/05/20 01:21:20 jongfoster
278 * Version 2.9.4 checkin.
279 * - Merged popupfile and cookiefile, and added control over PCRS
280 * filtering, in new "permissionsfile".
281 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
282 * file error you now get a message box (in the Win32 GUI) rather
283 * than the program exiting with no explanation.
284 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
286 * - Removed tabs from "config"
287 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
288 * - Bumped up version number.
290 * Revision 1.2 2001/05/17 23:02:36 oes
291 * - Made referrer option accept 'L' as a substitute for '§'
293 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
294 * Initial import of version 2.9.3 source tree
297 *********************************************************************/
304 #include <sys/types.h>
312 #if !defined(_WIN32) && !defined(__OS2__)
322 #include "jbsockets.h"
323 #include "miscutil.h"
325 const char parsers_h_rcs[] = PARSERS_H_VERSION;
327 /* Fix a problem with Solaris. There should be no effect on other
329 * Solaris's isspace() is a macro which uses it's argument directly
330 * as an array index. Therefore we need to make sure that high-bit
331 * characters generate +ve values, and ideally we also want to make
332 * the argument match the declared parameter type of "int".
334 * Why did they write a character function that can't take a simple
335 * "char" argument? Doh!
337 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
338 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
341 const struct parsers client_patterns[] = {
342 { "referer:", 8, client_referrer },
343 { "user-agent:", 11, client_uagent },
344 { "ua-", 3, client_ua },
345 { "from:", 5, client_from },
346 { "cookie:", 7, client_send_cookie },
347 { "x-forwarded-for:", 16, client_x_forwarded },
348 { "Accept-Encoding:", 16, client_accept_encoding },
349 { "TE:", 3, client_te },
350 #if defined(FEATURE_IMAGE_DETECT_MSIE)
351 { "Accept:", 7, client_accept },
352 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
353 { "Host:", 5, crumble },
354 /* { "if-modified-since:", 18, crumble }, */
355 { "Keep-Alive:", 11, crumble },
356 { "connection:", 11, crumble },
357 { "proxy-connection:", 17, crumble },
362 const struct parsers server_patterns[] = {
363 { "HTTP", 4, server_http },
364 { "set-cookie:", 11, server_set_cookie },
365 { "connection:", 11, crumble },
366 { "Content-Type:", 13, server_content_type },
367 { "Content-Length:", 15, server_content_length },
368 { "Content-MD5:", 12, server_content_md5 },
369 { "Content-Encoding:", 17, server_content_encoding },
370 { "Transfer-Encoding:", 18, server_transfer_coding },
371 { "Keep-Alive:", 11, crumble },
376 void (* const add_client_headers[])(struct client_state *) = {
379 client_x_forwarded_adder,
381 client_accept_encoding_adder,
382 connection_close_adder,
387 void (* const add_server_headers[])(struct client_state *) = {
388 connection_close_adder,
393 /*********************************************************************
395 * Function : flush_socket
397 * Description : Write any pending "buffered" content.
400 * 1 : fd = file descriptor of the socket to read
401 * 2 : csp = Current client state (buffers, headers, etc...)
403 * Returns : On success, the number of bytes written are returned (zero
404 * indicates nothing was written). On error, -1 is returned,
405 * and errno is set appropriately. If count is zero and the
406 * file descriptor refers to a regular file, 0 will be
407 * returned without causing any other effect. For a special
408 * file, the results are not portable.
410 *********************************************************************/
411 int flush_socket(int fd, struct client_state *csp)
413 struct iob *iob = csp->iob;
414 int n = iob->eod - iob->cur;
421 n = write_socket(fd, iob->cur, n);
422 iob->eod = iob->cur = iob->buf;
428 /*********************************************************************
430 * Function : add_to_iob
432 * Description : Add content to the buffered page.
435 * 1 : csp = Current client state (buffers, headers, etc...)
436 * 2 : buf = holds the content to be added to the page
437 * 3 : n = number of bytes to be added
439 * Returns : Number of bytes in the content buffer.
441 *********************************************************************/
442 int add_to_iob(struct client_state *csp, char *buf, int n)
444 struct iob *iob = csp->iob;
448 have = iob->eod - iob->cur;
457 if ((p = (char *)malloc(need + 1)) == NULL)
459 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
465 /* there is something in the buffer - save it */
466 memcpy(p, iob->cur, have);
468 /* replace the buffer with the new space */
472 /* point to the end of the data */
477 /* the buffer is empty, free it and reinitialize */
482 /* copy the new data into the iob buffer */
485 /* point to the end of the data */
488 /* null terminate == cheap insurance */
491 /* set the pointers to the new values */
500 /*********************************************************************
502 * Function : get_header
504 * Description : This (odd) routine will parse the csp->iob
507 * 1 : csp = Current client state (buffers, headers, etc...)
509 * Returns : Any one of the following:
511 * 1) a pointer to a dynamically allocated string that contains a header line
512 * 2) NULL indicating that the end of the header was reached
513 * 3) "" indicating that the end of the iob was reached before finding
514 * a complete header line.
516 *********************************************************************/
517 char *get_header(struct client_state *csp)
523 if ((iob->cur == NULL)
524 || ((p = strchr(iob->cur, '\n')) == NULL))
526 return(""); /* couldn't find a complete header */
531 ret = strdup(iob->cur);
535 if ((q = strchr(ret, '\r'))) *q = '\0';
537 /* is this a blank linke (i.e. the end of the header) ? */
549 /*********************************************************************
553 * Description : add, delete or modify lines in the HTTP header streams.
554 * On entry, it receives a linked list of headers space
555 * that was allocated dynamically (both the list nodes
556 * and the header contents).
558 * As a side effect it frees the space used by the original
562 * 1 : pats = list of patterns to match against headers
563 * 2 : more_headers = list of functions to add more
564 * headers (client or server)
565 * 3 : csp = Current client state (buffers, headers, etc...)
567 * Returns : Single pointer to a fully formed header, or NULL
568 * on out-of-memory error.
570 *********************************************************************/
571 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
573 struct list_entry *p;
574 const struct parsers *v;
578 for (v = pats; v->str ; v++)
580 for (p = csp->headers->first; p ; p = p->next)
582 /* Header crunch()ed in previous run? -> ignore */
583 if (p->str == NULL) continue;
585 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
587 if (strncmpic(p->str, v->str, v->len) == 0)
589 hdr = v->parser(v, p->str, csp);
590 freez(p->str); /* FIXME: Yuck! patching a list...*/
596 /* place any additional headers on the csp->headers list */
597 for (f = more_headers; *f ; f++)
602 hdr = list_to_text(csp->headers);
609 /*********************************************************************
611 * Function : free_http_request
613 * Description : Freez a http_request structure
616 * 1 : http = points to a http_request structure to free
620 *********************************************************************/
621 void free_http_request(struct http_request *http)
629 freez(http->hostport);
632 freez(http->host_ip_addr_str);
633 freez(http->user_agent);
638 /*********************************************************************
640 * Function : parse_http_request
642 * Description : Parse out the host and port from the URL. Find the
643 * hostname & path, port (if ':'), and/or password (if '@')
646 * 1 : req = URL (or is it URI?) to break down
647 * 2 : http = pointer to the http structure to hold elements
648 * 3 : csp = Current client state (buffers, headers, etc...)
652 *********************************************************************/
653 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
655 char *buf, *v[10], *url, *p;
658 memset(http, '\0', sizeof(*http));
659 http->cmd = strdup(req);
662 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
666 /* this could be a CONNECT request */
667 if (strcmpic(v[0], "connect") == 0)
670 http->gpc = strdup(v[0]);
671 http->hostport = strdup(v[1]);
672 http->ver = strdup(v[2]);
675 /* or it could be any other basic HTTP request type */
676 if ((0 == strcmpic(v[0], "get"))
677 || (0 == strcmpic(v[0], "head"))
678 || (0 == strcmpic(v[0], "post"))
679 || (0 == strcmpic(v[0], "put"))
680 || (0 == strcmpic(v[0], "delete"))
682 /* or a webDAV extension (RFC2518) */
683 || (0 == strcmpic(v[0], "propfind"))
684 || (0 == strcmpic(v[0], "proppatch"))
685 || (0 == strcmpic(v[0], "move"))
686 || (0 == strcmpic(v[0], "copy"))
687 || (0 == strcmpic(v[0], "mkcol"))
688 || (0 == strcmpic(v[0], "lock"))
689 || (0 == strcmpic(v[0], "unlock"))
693 http->gpc = strdup(v[0]);
694 http->url = strdup(v[1]);
695 http->ver = strdup(v[2]);
698 if (strncmpic(url, "http://", 7) == 0)
702 else if (strncmpic(url, "https://", 8) == 0)
713 if ((p = strchr(url, '/')))
715 http->path = strdup(p);
717 http->hostport = strdup(url);
720 * Repair broken HTTP requests that don't contain a path
724 http->path = strdup("/");
725 http->hostport = strdup(url);
734 if (http->hostport == NULL)
736 free_http_request(http);
740 buf = strdup(http->hostport);
743 /* check if url contains password */
744 n = ssplit(buf, "@", v, SZ(v), 1, 1);
747 char * newbuf = NULL;
748 newbuf = strdup(v[1]);
753 n = ssplit(buf, ":", v, SZ(v), 1, 1);
757 http->host = strdup(v[0]);
763 http->host = strdup(v[0]);
764 http->port = atoi(v[1]);
769 if (http->host == NULL)
771 free_http_request(http);
774 if (http->path == NULL)
776 http->path = strdup("/");
782 /* here begins the family of parser functions that reformat header lines */
785 /*********************************************************************
789 * Description : This is called if a header matches a pattern to "crunch"
792 * 1 : v = Pointer to parsers structure, which basically holds
793 * headers (client or server) that we want to "crunch"
794 * 2 : s = header (from sed) to "crunch"
795 * 3 : csp = Current client state (buffers, headers, etc...)
797 * Returns : Always NULL.
799 *********************************************************************/
800 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
802 log_error(LOG_LEVEL_HEADER, "crunch!");
808 /*********************************************************************
810 * Function : server_content_type
812 * Description : Set the content-type for filterable types (text/.*,
813 * javascript and image/gif) unless filtering has been
814 * forbidden (CT_TABOO) while parsing earlier headers.
818 * 2 : s = header string we are "considering"
819 * 3 : csp = Current client state (buffers, headers, etc...)
821 * Returns : A duplicate string pointer to this header (ie. pass thru)
823 *********************************************************************/
824 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
826 if (csp->content_type != CT_TABOO)
828 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
829 csp->content_type = CT_TEXT;
830 else if (strstr(s, " image/gif"))
831 csp->content_type = CT_GIF;
833 csp->content_type = 0;
841 /*********************************************************************
843 * Function : server_transfer_coding
845 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
846 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
847 * - Change from "chunked" to "identity" if body was chunked
848 * but has been de-chunked for filtering.
852 * 2 : s = header string we are "considering"
853 * 3 : csp = Current client state (buffers, headers, etc...)
855 * Returns : A duplicate string pointer to this header (ie. pass thru)
857 *********************************************************************/
858 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
861 * Turn off pcrs and gif filtering if body compressed
863 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
865 csp->content_type = CT_TABOO;
869 * Raise flag if body chunked
871 if (strstr(s, "chunked"))
873 csp->flags |= CSP_FLAG_CHUNKED;
876 * If the body was modified, it has been
877 * de-chunked first, so adjust the header:
879 if (csp->flags & CSP_FLAG_MODIFIED)
881 return(strdup("Transfer-Encoding: identity"));
890 /*********************************************************************
892 * Function : server_content_encoding
894 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
898 * 2 : s = header string we are "considering"
899 * 3 : csp = Current client state (buffers, headers, etc...)
901 * Returns : A duplicate string pointer to this header (ie. pass thru)
903 *********************************************************************/
904 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
907 * Turn off pcrs and gif filtering if body compressed
909 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
911 csp->content_type = CT_TABOO;
919 /*********************************************************************
921 * Function : server_content_length
923 * Description : Adjust Content-Length header if we modified
928 * 2 : s = header string we are "considering"
929 * 3 : csp = Current client state (buffers, headers, etc...)
931 * Returns : A duplicate string pointer to this header (ie. pass thru)
933 *********************************************************************/
934 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
936 if (csp->content_length != 0) /* Content length has been modified */
938 char * s2 = (char *) zalloc(100);
939 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
941 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
952 /*********************************************************************
954 * Function : server_content_md5
956 * Description : Crumble any Content-MD5 headers if the document was
957 * modified. FIXME: Should we re-compute instead?
961 * 2 : s = header string we are "considering"
962 * 3 : csp = Current client state (buffers, headers, etc...)
964 * Returns : A duplicate string pointer to this header (ie. pass thru)
966 *********************************************************************/
967 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
969 if (csp->flags & CSP_FLAG_MODIFIED)
971 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
982 /*********************************************************************
984 * Function : client_accept_encoding
986 * Description : Rewrite the client's Accept-Encoding header so that
987 * if doesn't allow compression, if the action applies.
988 * Note: For HTTP/1.0 the absence of the header is enough.
992 * 2 : s = header string we are "considering"
993 * 3 : csp = Current client state (buffers, headers, etc...)
995 * Returns : A copy of the client's original or the modified header.
997 *********************************************************************/
998 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
1000 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1006 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1008 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1010 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1021 /*********************************************************************
1023 * Function : client_te
1025 * Description : Rewrite the client's TE header so that
1026 * if doesn't allow compression, if the action applies.
1030 * 2 : s = header string we are "considering"
1031 * 3 : csp = Current client state (buffers, headers, etc...)
1033 * Returns : A copy of the client's original or the modified header.
1035 *********************************************************************/
1036 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1038 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1044 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1050 /*********************************************************************
1052 * Function : client_referrer
1054 * Description : Handle the "referer" config setting properly.
1055 * Called from `sed'.
1059 * 2 : s = header (from sed) to "crunch"
1060 * 3 : csp = Current client state (buffers, headers, etc...)
1062 * Returns : NULL if crunched, or a malloc'ed string with the original
1063 * or modified header
1065 *********************************************************************/
1066 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1068 const char * newval;
1070 #ifdef FEATURE_FORCE_LOAD
1071 /* Since the referrer can include the prefix even
1072 * even if the request itself is non-forced, we must
1073 * clean it unconditionally
1075 strclean(s, FORCE_PREFIX);
1076 #endif /* def FEATURE_FORCE_LOAD */
1078 #ifdef FEATURE_TRUST
1079 csp->referrer = strdup(s);
1080 #endif /* def FEATURE_TRUST */
1083 * Are we sending referer?
1085 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1090 newval = csp->action->string[ACTION_STRING_REFERER];
1093 * Are we blocking referer?
1095 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1097 log_error(LOG_LEVEL_HEADER, "crunch!");
1102 * Are we forging referer?
1104 if (0 == strcmpic(newval, "forge"))
1107 * Forge a referer as http://[hostname:port of REQUEST]/
1108 * to fool stupid checks for in-site links
1110 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1111 s2 = strsav(NULL, "Referer: ");
1112 s2 = strsav(s2, "http://");
1113 s2 = strsav(s2, csp->http->hostport);
1114 s2 = strsav(s2, "/");
1119 * Have we got a fixed referer?
1121 if (0 == strncmpic(newval, "http://", 7))
1124 * We have a specific (fixed) referer we want to send.
1128 log_error(LOG_LEVEL_HEADER, "modified");
1130 s3 = strsav( NULL, "Referer: " );
1131 s3 = strsav( s3, newval );
1135 /* Should never get here! */
1136 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1139 * Forge is probably the best default.
1141 * Forge a referer as http://[hostname:port of REQUEST]/
1142 * to fool stupid checks for in-site links
1144 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1145 s2 = strsav(NULL, "Referer: ");
1146 s2 = strsav(s2, "http://");
1147 s2 = strsav(s2, csp->http->hostport);
1148 s2 = strsav(s2, "/");
1153 /*********************************************************************
1155 * Function : client_uagent
1157 * Description : Handle the "user-agent" config setting properly
1158 * and remember its original value to enable browser
1159 * bug workarounds. Called from `sed'.
1163 * 2 : s = header (from sed) to "crunch"
1164 * 3 : csp = Current client state (buffers, headers, etc...)
1166 * Returns : A malloc'ed pointer to the default agent, or
1167 * a malloc'ed string pointer to this header (ie. pass thru).
1169 *********************************************************************/
1170 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1172 const char * newval;
1175 /* Save the client's User-Agent: value */
1176 if (strlen(s) >= 12)
1178 csp->http->user_agent = strdup(s + 12);
1181 #ifdef FEATURE_IMAGE_DETECT_MSIE
1182 if (strstr (s, "MSIE "))
1184 /* This is Microsoft Internet Explorer.
1185 * Enable auto-detect.
1187 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
1189 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1191 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1196 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1202 log_error(LOG_LEVEL_HEADER, "modified");
1204 s2 = strsav( NULL, "User-Agent: " );
1205 s2 = strsav( s2, newval );
1211 /*********************************************************************
1213 * Function : client_ua
1215 * Description : Handle "ua-" headers properly. Called from `sed'.
1219 * 2 : s = header (from sed) to "crunch"
1220 * 3 : csp = Current client state (buffers, headers, etc...)
1222 * Returns : NULL if crunched, or a malloc'ed string to original header
1224 *********************************************************************/
1225 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1227 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1233 log_error(LOG_LEVEL_HEADER, "crunch!");
1239 /*********************************************************************
1241 * Function : client_from
1243 * Description : Handle the "from" config setting properly.
1244 * Called from `sed'.
1248 * 2 : s = header (from sed) to "crunch"
1249 * 3 : csp = Current client state (buffers, headers, etc...)
1251 * Returns : NULL if crunched, or a malloc'ed string to
1252 * modified/original header.
1254 *********************************************************************/
1255 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1257 const char * newval;
1260 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1265 newval = csp->action->string[ACTION_STRING_FROM];
1268 * Are we blocking referer?
1270 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1272 log_error(LOG_LEVEL_HEADER, "crunch!");
1276 log_error(LOG_LEVEL_HEADER, " modified");
1278 s2 = strsav( NULL, "From: " );
1279 s2 = strsav( s2, newval );
1285 /*********************************************************************
1287 * Function : client_send_cookie
1289 * Description : Handle the "cookie" header properly. Called from `sed'.
1290 * If cookie is accepted, add it to the cookie_list,
1291 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1294 * 1 : v = pattern of cookie `sed' found matching
1295 * 2 : s = header (from sed) to "crunch"
1296 * 3 : csp = Current client state (buffers, headers, etc...)
1298 * Returns : Always NULL.
1300 *********************************************************************/
1301 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1303 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1305 enlist(csp->cookie_list, s + v->len + 1);
1309 log_error(LOG_LEVEL_HEADER, " crunch!");
1313 * Always return NULL here. The cookie header
1314 * will be sent at the end of the header.
1321 /*********************************************************************
1323 * Function : client_x_forwarded
1325 * Description : Handle the "x-forwarded-for" config setting properly,
1326 * also used in the add_client_headers list. Called from `sed'.
1330 * 2 : s = header (from sed) to "crunch"
1331 * 3 : csp = Current client state (buffers, headers, etc...)
1333 * Returns : Always NULL.
1335 *********************************************************************/
1336 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1338 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1340 /* Save it so we can re-add it later */
1341 csp->x_forwarded = strdup(s);
1345 * Always return NULL, since this information
1346 * will be sent at the end of the header.
1353 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1354 /*********************************************************************
1356 * Function : client_accept
1358 * Description : Detect whether the client wants HTML or an image.
1359 * Clients do not always make this information available
1360 * in a sane way. Always passes the header through
1361 * the proxy unchanged.
1365 * 2 : s = Header string. Null terminated.
1366 * 3 : csp = Current client state (buffers, headers, etc...)
1368 * Returns : Duplicate of argument s.
1370 *********************************************************************/
1371 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1373 #ifdef FEATURE_IMAGE_DETECT_MSIE
1374 if (strstr (s, "image/gif"))
1376 /* Client will accept HTML. If this seems counterintuitive,
1379 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1383 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1385 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1390 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1394 /* the following functions add headers directly to the header list */
1397 /*********************************************************************
1399 * Function : client_host_adder
1401 * Description : (re)adds the host header. Called from `sed'.
1404 * 1 : csp = Current client state (buffers, headers, etc...)
1408 *********************************************************************/
1409 void client_host_adder(struct client_state *csp)
1413 p = strsav(p, "Host: ");
1414 p = strsav(p, csp->http->hostport);
1416 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1417 enlist(csp->headers, p);
1423 /*********************************************************************
1425 * Function : client_cookie_adder
1427 * Description : Used in the add_client_headers list. Called from `sed'.
1430 * 1 : csp = Current client state (buffers, headers, etc...)
1434 *********************************************************************/
1435 void client_cookie_adder(struct client_state *csp)
1437 struct list_entry *lst;
1441 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1445 tmp = strsav(tmp, "; ");
1447 tmp = strsav(tmp, lst->str);
1450 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1454 tmp = strsav(tmp, "; ");
1457 if ((e = cookie_encode(lst->str)))
1459 tmp = strsav(tmp, e);
1468 ret = strdup("Cookie: ");
1469 ret = strsav(ret, tmp);
1470 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1471 enlist(csp->headers, ret);
1479 /*********************************************************************
1481 * Function : client_accept_encoding_adder
1483 * Description : Add an Accept-Encoding header to the client's request
1484 * that disables compression if the action applies, and
1485 * the header is not already there. Called from `sed'.
1486 * Note: For HTTP/1.0, the absence of the header is enough.
1489 * 1 : csp = Current client state (buffers, headers, etc...)
1493 *********************************************************************/
1494 void client_accept_encoding_adder(struct client_state *csp)
1496 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1497 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1499 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1505 /*********************************************************************
1507 * Function : client_xtra_adder
1509 * Description : Used in the add_client_headers list. Called from `sed'.
1512 * 1 : csp = Current client state (buffers, headers, etc...)
1516 *********************************************************************/
1517 void client_xtra_adder(struct client_state *csp)
1519 struct list_entry *lst;
1521 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1522 lst ; lst = lst->next)
1524 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1525 enlist(csp->headers, lst->str);
1531 /*********************************************************************
1533 * Function : client_x_forwarded_adder
1535 * Description : Used in the add_client_headers list. Called from `sed'.
1538 * 1 : csp = Current client state (buffers, headers, etc...)
1542 *********************************************************************/
1543 void client_x_forwarded_adder(struct client_state *csp)
1547 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1552 if (csp->x_forwarded)
1554 p = strsav(p, csp->x_forwarded);
1555 p = strsav(p, ", ");
1556 p = strsav(p, csp->ip_addr_str);
1560 p = strsav(p, "X-Forwarded-For: ");
1561 p = strsav(p, csp->ip_addr_str);
1564 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1565 enlist(csp->headers, p);
1571 /*********************************************************************
1573 * Function : connection_close_adder
1575 * Description : Adds a "Connection: close" header to csp->headers
1576 * as a temporary fix for the needed but missing HTTP/1.1
1577 * support. Called from `sed'.
1578 * FIXME: This whole function shouldn't be neccessary!
1581 * 1 : csp = Current client state (buffers, headers, etc...)
1585 *********************************************************************/
1586 void connection_close_adder(struct client_state *csp)
1588 enlist(csp->headers, "Connection: close");
1592 /*********************************************************************
1594 * Function : server_http
1596 * Description : - Save the HTTP Status into csp->http->status
1597 * - Set CT_TABOO to prevent filtering if the answer
1598 * is a partial range (HTTP status 206)
1599 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1603 * 1 : v = parser pattern that matched this header
1604 * 2 : s = header that matched this pattern
1605 * 3 : csp = Current client state (buffers, headers, etc...)
1607 * Returns : Copy of changed or original answer.
1609 *********************************************************************/
1610 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1612 char *ret = strdup(s);
1614 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1615 if (csp->http->status == 206)
1617 csp->content_type = CT_TABOO;
1620 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1623 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1630 /*********************************************************************
1632 * Function : server_set_cookie
1634 * Description : Handle the server "cookie" header properly.
1635 * Log cookie to the jar file. Then "crunch" it,
1636 * or accept it. Called from `sed'.
1639 * 1 : v = parser pattern that matched this header
1640 * 2 : s = header that matched this pattern
1641 * 3 : csp = Current client state (buffers, headers, etc...)
1643 * Returns : `crumble' or a newly malloc'ed string.
1645 *********************************************************************/
1646 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1648 #ifdef FEATURE_COOKIE_JAR
1649 if (csp->config->jar)
1651 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1653 #endif /* def FEATURE_COOKIE_JAR */
1655 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1657 return(crumble(v, s, csp));
1665 #ifdef FEATURE_FORCE_LOAD
1666 /*********************************************************************
1668 * Function : strclean
1670 * Description : In-Situ-Eliminate all occurances of substring in
1674 * 1 : string = string to clean
1675 * 2 : substring = substring to eliminate
1677 * Returns : Number of eliminations
1679 *********************************************************************/
1680 int strclean(const char *string, const char *substring)
1682 int hits = 0, len = strlen(substring);
1685 while((pos = strstr(string, substring)))
1692 while (*p++ != '\0');
1699 #endif /* def FEATURE_FORCE_LOAD */