1 const char parsers_rcs[] = "$Id: parsers.c,v 1.35 2001/10/09 22:39:21 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.35 2001/10/09 22:39:21 jongfoster
45 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
48 * Revision 1.34 2001/10/07 18:50:55 oes
49 * Added server_content_encoding, renamed server_transfer_encoding
51 * Revision 1.33 2001/10/07 18:04:49 oes
52 * Changed server_http11 to server_http and its pattern to "HTTP".
53 * Additional functionality: it now saves the HTTP status into
54 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
56 * Revision 1.32 2001/10/07 15:43:28 oes
57 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
58 * client_te and client_accept_encoding_adder, triggered by the new
59 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
60 * changed to allow only identity and chunked, and the TE header is
61 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
63 * parse_http_request no longer does anything than parsing. The rewriting
64 * of http->cmd and version mangling are gone. It now also recognizes
65 * the put and delete methods and saves the url in http->url. Removed
68 * renamed content_type and content_length to have the server_ prefix
70 * server_content_type now only works if csp->content_type != CT_TABOO
72 * added server_transfer_encoding, which
73 * - Sets CT_TABOO to prohibit filtering if encoding compresses
74 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
75 * - Change from "chunked" to "identity" if body was chunked
76 * but has been de-chunked for filtering.
78 * added server_content_md5 which crunches any Content-MD5 headers
79 * if the body was modified.
81 * made server_http11 conditional on +downgrade action
83 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
85 * Revision 1.31 2001/10/05 14:25:02 oes
86 * Crumble Keep-Alive from Server
88 * Revision 1.30 2001/09/29 12:56:03 joergs
89 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
91 * Revision 1.29 2001/09/24 21:09:24 jongfoster
92 * Fixing 2 memory leaks that Guy spotted, where the paramater to
93 * enlist() was not being free()d.
95 * Revision 1.28 2001/09/22 16:32:28 jongfoster
96 * Removing unused #includes.
98 * Revision 1.27 2001/09/20 15:45:25 steudten
100 * add casting from size_t to int for printf()
101 * remove local variable shadow s2
103 * Revision 1.26 2001/09/16 17:05:14 jongfoster
104 * Removing unused #include showarg.h
106 * Revision 1.25 2001/09/16 13:21:27 jongfoster
107 * Changes to use new list functions.
109 * Revision 1.24 2001/09/13 23:05:50 jongfoster
110 * Changing the string paramater to the header parsers a "const".
112 * Revision 1.23 2001/09/12 18:08:19 steudten
114 * In parse_http_request() header rewriting miss the host value, so
115 * from http://www.mydomain.com the result was just " / " not
116 * http://www.mydomain.com/ in case we forward.
118 * Revision 1.22 2001/09/10 10:58:53 oes
119 * Silenced compiler warnings
121 * Revision 1.21 2001/07/31 14:46:00 oes
122 * - Persistant connections now suppressed
123 * - sed() no longer appends empty header to csp->headers
125 * Revision 1.20 2001/07/30 22:08:36 jongfoster
126 * Tidying up #defines:
127 * - All feature #defines are now of the form FEATURE_xxx
128 * - Permanently turned off WIN_GUI_EDIT
129 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
131 * Revision 1.19 2001/07/25 17:21:54 oes
132 * client_uagent now saves copy of User-Agent: header value
134 * Revision 1.18 2001/07/13 14:02:46 oes
135 * - Included fix to repair broken HTTP requests that
136 * don't contain a path, not even '/'.
137 * - Removed all #ifdef PCRS
138 * - content_type now always inspected and classified as
139 * text, gif or other.
140 * - formatting / comments
142 * Revision 1.17 2001/06/29 21:45:41 oes
143 * Indentation, CRLF->LF, Tab-> Space
145 * Revision 1.16 2001/06/29 13:32:42 oes
147 * - Adapted free_http_request
148 * - Removed logentry from cancelled commit
150 * Revision 1.15 2001/06/03 19:12:38 oes
151 * deleted const struct interceptors
153 * Revision 1.14 2001/06/01 18:49:17 jongfoster
154 * Replaced "list_share" with "list" - the tiny memory gain was not
155 * worth the extra complexity.
157 * Revision 1.13 2001/05/31 21:30:33 jongfoster
158 * Removed list code - it's now in list.[ch]
159 * Renamed "permission" to "action", and changed many features
160 * to use the actions file rather than the global config.
162 * Revision 1.12 2001/05/31 17:33:13 oes
166 * Revision 1.11 2001/05/29 20:11:19 joergs
167 * '/ * inside comment' warning removed.
169 * Revision 1.10 2001/05/29 09:50:24 jongfoster
170 * Unified blocklist/imagelist/permissionslist.
171 * File format is still under discussion, but the internal changes
174 * Also modified interceptor behaviour:
175 * - We now intercept all URLs beginning with one of the following
176 * prefixes (and *only* these prefixes):
178 * * http://ijbswa.sf.net/config/
179 * * http://ijbswa.sourceforge.net/config/
180 * - New interceptors "home page" - go to http://i.j.b/ to see it.
181 * - Internal changes so that intercepted and fast redirect pages
182 * are not replaced with an image.
183 * - Interceptors now have the option to send a binary page direct
184 * to the client. (i.e. ijb-send-banner uses this)
185 * - Implemented show-url-info interceptor. (Which is why I needed
186 * the above interceptors changes - a typical URL is
187 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
188 * The previous mechanism would not have intercepted that, and
189 * if it had been intercepted then it then it would have replaced
192 * Revision 1.9 2001/05/28 17:26:33 jongfoster
193 * Fixing segfault if last header was crunched.
194 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
195 * can use the cross-platform sprintf() instead.)
197 * Revision 1.8 2001/05/27 22:17:04 oes
199 * - re_process_buffer no longer writes the modified buffer
200 * to the client, which was very ugly. It now returns the
201 * buffer, which it is then written by chat.
203 * - content_length now adjusts the Content-Length: header
204 * for modified documents rather than crunch()ing it.
205 * (Length info in csp->content_length, which is 0 for
206 * unmodified documents)
208 * - For this to work, sed() is called twice when filtering.
210 * Revision 1.7 2001/05/27 13:19:06 oes
211 * Patched Joergs solution for the content-length in.
213 * Revision 1.6 2001/05/26 13:39:32 jongfoster
214 * Only crunches Content-Length header if applying RE filtering.
215 * Without this fix, Microsoft Windows Update wouldn't work.
217 * Revision 1.5 2001/05/26 00:28:36 jongfoster
218 * Automatic reloading of config file.
219 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
220 * Most of the global variables have been moved to a new
221 * struct configuration_spec, accessed through csp->config->globalname
222 * Most of the globals remaining are used by the Win32 GUI.
224 * Revision 1.4 2001/05/22 18:46:04 oes
226 * - Enabled filtering banners by size rather than URL
227 * by adding patterns that replace all standard banner
228 * sizes with the "Junkbuster" gif to the re_filterfile
230 * - Enabled filtering WebBugs by providing a pattern
231 * which kills all 1x1 images
233 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
234 * which is selected by the (nonstandard and therefore
235 * capital) letter 'U' in the option string.
236 * It causes the quantifiers to be ungreedy by default.
237 * Appending a ? turns back to greedy (!).
239 * - Added a new interceptor ijb-send-banner, which
240 * sends back the "Junkbuster" gif. Without imagelist or
241 * MSIE detection support, or if tinygif = 1, or the
242 * URL isn't recognized as an imageurl, a lame HTML
243 * explanation is sent instead.
245 * - Added new feature, which permits blocking remote
246 * script redirects and firing back a local redirect
248 * The feature is conditionally compiled, i.e. it
249 * can be disabled with --disable-fast-redirects,
250 * plus it must be activated by a "fast-redirects"
251 * line in the config file, has its own log level
252 * and of course wants to be displayed by show-proxy-args
253 * Note: Boy, all the #ifdefs in 1001 locations and
254 * all the fumbling with configure.in and acconfig.h
255 * were *way* more work than the feature itself :-(
257 * - Because a generic redirect template was needed for
258 * this, tinygif = 3 now uses the same.
260 * - Moved GIFs, and other static HTTP response templates
265 * - Removed some >400 CRs again (Jon, you really worked
268 * Revision 1.3 2001/05/20 01:21:20 jongfoster
269 * Version 2.9.4 checkin.
270 * - Merged popupfile and cookiefile, and added control over PCRS
271 * filtering, in new "permissionsfile".
272 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
273 * file error you now get a message box (in the Win32 GUI) rather
274 * than the program exiting with no explanation.
275 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
277 * - Removed tabs from "config"
278 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
279 * - Bumped up version number.
281 * Revision 1.2 2001/05/17 23:02:36 oes
282 * - Made referrer option accept 'L' as a substitute for '§'
284 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
285 * Initial import of version 2.9.3 source tree
288 *********************************************************************/
295 #include <sys/types.h>
313 #include "jbsockets.h"
314 #include "miscutil.h"
316 const char parsers_h_rcs[] = PARSERS_H_VERSION;
318 /* Fix a problem with Solaris. There should be no effect on other
320 * Solaris's isspace() is a macro which uses it's argument directly
321 * as an array index. Therefore we need to make sure that high-bit
322 * characters generate +ve values, and ideally we also want to make
323 * the argument match the declared parameter type of "int".
325 * Why did they write a character function that can't take a simple
326 * "char" argument? Doh!
328 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
329 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
332 const struct parsers client_patterns[] = {
333 { "referer:", 8, client_referrer },
334 { "user-agent:", 11, client_uagent },
335 { "ua-", 3, client_ua },
336 { "from:", 5, client_from },
337 { "cookie:", 7, client_send_cookie },
338 { "x-forwarded-for:", 16, client_x_forwarded },
339 { "Accept-Encoding:", 16, client_accept_encoding },
340 { "TE:", 3, client_te },
341 #if defined(FEATURE_IMAGE_DETECT_MSIE)
342 { "Accept:", 7, client_accept },
343 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
344 { "Host:", 5, crumble },
345 /* { "if-modified-since:", 18, crumble }, */
346 { "Keep-Alive:", 11, crumble },
347 { "connection:", 11, crumble },
348 { "proxy-connection:", 17, crumble },
353 const struct parsers server_patterns[] = {
354 { "HTTP", 4, server_http },
355 { "set-cookie:", 11, server_set_cookie },
356 { "connection:", 11, crumble },
357 { "Content-Type:", 13, server_content_type },
358 { "Content-Length:", 15, server_content_length },
359 { "Content-MD5:", 12, server_content_md5 },
360 { "Content-Encoding:", 17, server_content_encoding },
361 { "Transfer-Encoding:", 18, server_transfer_coding },
362 { "Keep-Alive:", 11, crumble },
367 void (* const add_client_headers[])(struct client_state *) = {
370 client_x_forwarded_adder,
372 client_accept_encoding_adder,
373 connection_close_adder,
378 void (* const add_server_headers[])(struct client_state *) = {
379 connection_close_adder,
384 /*********************************************************************
386 * Function : flush_socket
388 * Description : Write any pending "buffered" content.
391 * 1 : fd = file descriptor of the socket to read
392 * 2 : csp = Current client state (buffers, headers, etc...)
394 * Returns : On success, the number of bytes written are returned (zero
395 * indicates nothing was written). On error, -1 is returned,
396 * and errno is set appropriately. If count is zero and the
397 * file descriptor refers to a regular file, 0 will be
398 * returned without causing any other effect. For a special
399 * file, the results are not portable.
401 *********************************************************************/
402 int flush_socket(int fd, struct client_state *csp)
404 struct iob *iob = csp->iob;
405 int n = iob->eod - iob->cur;
412 n = write_socket(fd, iob->cur, n);
413 iob->eod = iob->cur = iob->buf;
419 /*********************************************************************
421 * Function : add_to_iob
423 * Description : Add content to the buffered page.
426 * 1 : csp = Current client state (buffers, headers, etc...)
427 * 2 : buf = holds the content to be added to the page
428 * 3 : n = number of bytes to be added
430 * Returns : Number of bytes in the content buffer.
432 *********************************************************************/
433 int add_to_iob(struct client_state *csp, char *buf, int n)
435 struct iob *iob = csp->iob;
439 have = iob->eod - iob->cur;
448 if ((p = (char *)malloc(need + 1)) == NULL)
450 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
456 /* there is something in the buffer - save it */
457 memcpy(p, iob->cur, have);
459 /* replace the buffer with the new space */
463 /* point to the end of the data */
468 /* the buffer is empty, free it and reinitialize */
473 /* copy the new data into the iob buffer */
476 /* point to the end of the data */
479 /* null terminate == cheap insurance */
482 /* set the pointers to the new values */
491 /*********************************************************************
493 * Function : get_header
495 * Description : This (odd) routine will parse the csp->iob
498 * 1 : csp = Current client state (buffers, headers, etc...)
500 * Returns : Any one of the following:
502 * 1) a pointer to a dynamically allocated string that contains a header line
503 * 2) NULL indicating that the end of the header was reached
504 * 3) "" indicating that the end of the iob was reached before finding
505 * a complete header line.
507 *********************************************************************/
508 char *get_header(struct client_state *csp)
514 if ((iob->cur == NULL)
515 || ((p = strchr(iob->cur, '\n')) == NULL))
517 return(""); /* couldn't find a complete header */
522 ret = strdup(iob->cur);
526 if ((q = strchr(ret, '\r'))) *q = '\0';
528 /* is this a blank linke (i.e. the end of the header) ? */
540 /*********************************************************************
544 * Description : add, delete or modify lines in the HTTP header streams.
545 * On entry, it receives a linked list of headers space
546 * that was allocated dynamically (both the list nodes
547 * and the header contents).
549 * As a side effect it frees the space used by the original
553 * 1 : pats = list of patterns to match against headers
554 * 2 : more_headers = list of functions to add more
555 * headers (client or server)
556 * 3 : csp = Current client state (buffers, headers, etc...)
558 * Returns : Single pointer to a fully formed header.
560 *********************************************************************/
561 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
563 struct list_entry *p;
564 const struct parsers *v;
568 for (v = pats; v->str ; v++)
570 for (p = csp->headers->first; p ; p = p->next)
572 /* Header crunch()ed in previous run? -> ignore */
573 if (p->str == NULL) continue;
575 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
577 if (strncmpic(p->str, v->str, v->len) == 0)
579 hdr = v->parser(v, p->str, csp);
580 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
586 /* place any additional headers on the csp->headers list */
587 for (f = more_headers; *f ; f++)
592 hdr = list_to_text(csp->headers);
599 /*********************************************************************
601 * Function : free_http_request
603 * Description : Freez a http_request structure
606 * 1 : http = points to a http_request structure to free
610 *********************************************************************/
611 void free_http_request(struct http_request *http)
619 freez(http->hostport);
622 freez(http->host_ip_addr_str);
623 freez(http->user_agent);
628 /*********************************************************************
630 * Function : parse_http_request
632 * Description : Parse out the host and port from the URL. Find the
633 * hostname & path, port (if ':'), and/or password (if '@')
636 * 1 : req = URL (or is it URI?) to break down
637 * 2 : http = pointer to the http structure to hold elements
638 * 3 : csp = Current client state (buffers, headers, etc...)
642 *********************************************************************/
643 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
645 char *buf, *v[10], *url, *p;
648 memset(http, '\0', sizeof(*http));
649 http->cmd = strdup(req);
652 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
656 /* this could be a CONNECT request */
657 if (strcmpic(v[0], "connect") == 0)
660 http->gpc = strdup(v[0]);
661 http->hostport = strdup(v[1]);
662 http->ver = strdup(v[2]);
665 /* or it could be any other basic HTTP request type */
666 if ((0 == strcmpic(v[0], "get"))
667 || (0 == strcmpic(v[0], "head"))
668 || (0 == strcmpic(v[0], "post"))
669 || (0 == strcmpic(v[0], "put"))
670 || (0 == strcmpic(v[0], "delete"))
672 /* or a webDAV extension (RFC2518) */
673 || (0 == strcmpic(v[0], "propfind"))
674 || (0 == strcmpic(v[0], "proppatch"))
675 || (0 == strcmpic(v[0], "move"))
676 || (0 == strcmpic(v[0], "copy"))
677 || (0 == strcmpic(v[0], "mkcol"))
678 || (0 == strcmpic(v[0], "lock"))
679 || (0 == strcmpic(v[0], "unlock"))
683 http->gpc = strdup(v[0]);
684 http->url = strdup(v[1]);
685 http->ver = strdup(v[2]);
688 if (strncmpic(url, "http://", 7) == 0)
692 else if (strncmpic(url, "https://", 8) == 0)
703 if ((p = strchr(url, '/')))
705 http->path = strdup(p);
707 http->hostport = strdup(url);
710 * Repair broken HTTP requests that don't contain a path
714 http->path = strdup("/");
715 http->hostport = strdup(url);
724 if (http->hostport == NULL)
726 free_http_request(http);
730 buf = strdup(http->hostport);
733 /* check if url contains password */
734 n = ssplit(buf, "@", v, SZ(v), 1, 1);
737 char * newbuf = NULL;
738 newbuf = strdup(v[1]);
743 n = ssplit(buf, ":", v, SZ(v), 1, 1);
747 http->host = strdup(v[0]);
753 http->host = strdup(v[0]);
754 http->port = atoi(v[1]);
759 if (http->host == NULL)
761 free_http_request(http);
764 if (http->path == NULL)
766 http->path = strdup("/");
772 /* here begins the family of parser functions that reformat header lines */
775 /*********************************************************************
779 * Description : This is called if a header matches a pattern to "crunch"
782 * 1 : v = Pointer to parsers structure, which basically holds
783 * headers (client or server) that we want to "crunch"
784 * 2 : s = header (from sed) to "crunch"
785 * 3 : csp = Current client state (buffers, headers, etc...)
787 * Returns : Always NULL.
789 *********************************************************************/
790 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
792 log_error(LOG_LEVEL_HEADER, "crunch!");
798 /*********************************************************************
800 * Function : server_content_type
802 * Description : Set the content-type for filterable types (text/.*,
803 * javascript and image/gif) unless filtering has been
804 * forbidden (CT_TABOO) while parsing earlier headers.
808 * 2 : s = header string we are "considering"
809 * 3 : csp = Current client state (buffers, headers, etc...)
811 * Returns : A duplicate string pointer to this header (ie. pass thru)
813 *********************************************************************/
814 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
816 if (csp->content_type != CT_TABOO)
818 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
819 csp->content_type = CT_TEXT;
820 else if (strstr(s, " image/gif"))
821 csp->content_type = CT_GIF;
823 csp->content_type = 0;
831 /*********************************************************************
833 * Function : server_transfer_coding
835 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
836 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
837 * - Change from "chunked" to "identity" if body was chunked
838 * but has been de-chunked for filtering.
842 * 2 : s = header string we are "considering"
843 * 3 : csp = Current client state (buffers, headers, etc...)
845 * Returns : A duplicate string pointer to this header (ie. pass thru)
847 *********************************************************************/
848 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
851 * Turn off pcrs and gif filtering if body compressed
853 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
855 csp->content_type = CT_TABOO;
859 * Raise flag if body chunked
861 if (strstr(s, "chunked"))
863 csp->flags |= CSP_FLAG_CHUNKED;
866 * If the body was modified, it has been
867 * de-chunked first, so adjust the header:
869 if (csp->flags & CSP_FLAG_MODIFIED)
871 return(strdup("Transfer-Encoding: identity"));
880 /*********************************************************************
882 * Function : server_content_encoding
884 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
888 * 2 : s = header string we are "considering"
889 * 3 : csp = Current client state (buffers, headers, etc...)
891 * Returns : A duplicate string pointer to this header (ie. pass thru)
893 *********************************************************************/
894 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
897 * Turn off pcrs and gif filtering if body compressed
899 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
901 csp->content_type = CT_TABOO;
909 /*********************************************************************
911 * Function : server_content_length
913 * Description : Adjust Content-Length header if we modified
918 * 2 : s = header string we are "considering"
919 * 3 : csp = Current client state (buffers, headers, etc...)
921 * Returns : A duplicate string pointer to this header (ie. pass thru)
923 *********************************************************************/
924 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
926 if (csp->content_length != 0) /* Content length has been modified */
928 char * s2 = (char *) zalloc(100);
929 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
931 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
942 /*********************************************************************
944 * Function : server_content_md5
946 * Description : Crumble any Content-MD5 headers if the document was
947 * modified. FIXME: Should we re-compute instead?
951 * 2 : s = header string we are "considering"
952 * 3 : csp = Current client state (buffers, headers, etc...)
954 * Returns : A duplicate string pointer to this header (ie. pass thru)
956 *********************************************************************/
957 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
959 if (csp->flags & CSP_FLAG_MODIFIED)
961 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
972 /*********************************************************************
974 * Function : client_accept_encoding
976 * Description : Rewrite the client's Accept-Encoding header so that
977 * if doesn't allow compression, if the action applies.
978 * Note: For HTTP/1.0 the absence of the header is enough.
982 * 2 : s = header string we are "considering"
983 * 3 : csp = Current client state (buffers, headers, etc...)
985 * Returns : A copy of the client's original or the modified header.
987 *********************************************************************/
988 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
990 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
996 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
998 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1000 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1011 /*********************************************************************
1013 * Function : client_te
1015 * Description : Rewrite the client's TE header so that
1016 * if doesn't allow compression, if the action applies.
1020 * 2 : s = header string we are "considering"
1021 * 3 : csp = Current client state (buffers, headers, etc...)
1023 * Returns : A copy of the client's original or the modified header.
1025 *********************************************************************/
1026 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1028 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1034 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1040 /*********************************************************************
1042 * Function : client_referrer
1044 * Description : Handle the "referer" config setting properly.
1045 * Called from `sed'.
1049 * 2 : s = header (from sed) to "crunch"
1050 * 3 : csp = Current client state (buffers, headers, etc...)
1052 * Returns : NULL if crunched, or a malloc'ed string with the original
1053 * or modified header
1055 *********************************************************************/
1056 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1058 const char * newval;
1060 #ifdef FEATURE_FORCE_LOAD
1061 /* Since the referrer can include the prefix even
1062 * even if the request itself is non-forced, we must
1063 * clean it unconditionally
1065 strclean(s, FORCE_PREFIX);
1066 #endif /* def FEATURE_FORCE_LOAD */
1068 #ifdef FEATURE_TRUST
1069 csp->referrer = strdup(s);
1070 #endif /* def FEATURE_TRUST */
1073 * Are we sending referer?
1075 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1080 newval = csp->action->string[ACTION_STRING_REFERER];
1083 * Are we blocking referer?
1085 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1087 log_error(LOG_LEVEL_HEADER, "crunch!");
1092 * Are we forging referer?
1094 if (0 == strcmpic(newval, "forge"))
1097 * Forge a referer as http://[hostname:port of REQUEST]/
1098 * to fool stupid checks for in-site links
1100 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1101 s2 = strsav(NULL, "Referer: ");
1102 s2 = strsav(s2, "http://");
1103 s2 = strsav(s2, csp->http->hostport);
1104 s2 = strsav(s2, "/");
1109 * Have we got a fixed referer?
1111 if (0 == strncmpic(newval, "http://", 7))
1114 * We have a specific (fixed) referer we want to send.
1118 log_error(LOG_LEVEL_HEADER, "modified");
1120 s3 = strsav( NULL, "Referer: " );
1121 s3 = strsav( s3, newval );
1125 /* Should never get here! */
1126 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1129 * Forge is probably the best default.
1131 * Forge a referer as http://[hostname:port of REQUEST]/
1132 * to fool stupid checks for in-site links
1134 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1135 s2 = strsav(NULL, "Referer: ");
1136 s2 = strsav(s2, "http://");
1137 s2 = strsav(s2, csp->http->hostport);
1138 s2 = strsav(s2, "/");
1143 /*********************************************************************
1145 * Function : client_uagent
1147 * Description : Handle the "user-agent" config setting properly
1148 * and remember its original value to enable browser
1149 * bug workarounds. Called from `sed'.
1153 * 2 : s = header (from sed) to "crunch"
1154 * 3 : csp = Current client state (buffers, headers, etc...)
1156 * Returns : A malloc'ed pointer to the default agent, or
1157 * a malloc'ed string pointer to this header (ie. pass thru).
1159 *********************************************************************/
1160 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1162 const char * newval;
1165 /* Save the client's User-Agent: value */
1166 if (strlen(s) >= 12)
1168 csp->http->user_agent = strdup(s + 12);
1171 #ifdef FEATURE_IMAGE_DETECT_MSIE
1172 if (strstr (s, "MSIE "))
1174 /* This is Microsoft Internet Explorer.
1175 * Enable auto-detect.
1177 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
1179 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1181 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1186 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1192 log_error(LOG_LEVEL_HEADER, "modified");
1194 s2 = strsav( NULL, "User-Agent: " );
1195 s2 = strsav( s2, newval );
1201 /*********************************************************************
1203 * Function : client_ua
1205 * Description : Handle "ua-" headers properly. Called from `sed'.
1209 * 2 : s = header (from sed) to "crunch"
1210 * 3 : csp = Current client state (buffers, headers, etc...)
1212 * Returns : NULL if crunched, or a malloc'ed string to original header
1214 *********************************************************************/
1215 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1217 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1223 log_error(LOG_LEVEL_HEADER, "crunch!");
1229 /*********************************************************************
1231 * Function : client_from
1233 * Description : Handle the "from" config setting properly.
1234 * Called from `sed'.
1238 * 2 : s = header (from sed) to "crunch"
1239 * 3 : csp = Current client state (buffers, headers, etc...)
1241 * Returns : NULL if crunched, or a malloc'ed string to
1242 * modified/original header.
1244 *********************************************************************/
1245 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1247 const char * newval;
1250 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1255 newval = csp->action->string[ACTION_STRING_FROM];
1258 * Are we blocking referer?
1260 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1262 log_error(LOG_LEVEL_HEADER, "crunch!");
1266 log_error(LOG_LEVEL_HEADER, " modified");
1268 s2 = strsav( NULL, "From: " );
1269 s2 = strsav( s2, newval );
1275 /*********************************************************************
1277 * Function : client_send_cookie
1279 * Description : Handle the "cookie" header properly. Called from `sed'.
1280 * If cookie is accepted, add it to the cookie_list,
1281 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1284 * 1 : v = pattern of cookie `sed' found matching
1285 * 2 : s = header (from sed) to "crunch"
1286 * 3 : csp = Current client state (buffers, headers, etc...)
1288 * Returns : Always NULL.
1290 *********************************************************************/
1291 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1293 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1295 enlist(csp->cookie_list, s + v->len + 1);
1299 log_error(LOG_LEVEL_HEADER, " crunch!");
1303 * Always return NULL here. The cookie header
1304 * will be sent at the end of the header.
1311 /*********************************************************************
1313 * Function : client_x_forwarded
1315 * Description : Handle the "x-forwarded-for" config setting properly,
1316 * also used in the add_client_headers list. Called from `sed'.
1320 * 2 : s = header (from sed) to "crunch"
1321 * 3 : csp = Current client state (buffers, headers, etc...)
1323 * Returns : Always NULL.
1325 *********************************************************************/
1326 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1328 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1330 /* Save it so we can re-add it later */
1331 csp->x_forwarded = strdup(s);
1335 * Always return NULL, since this information
1336 * will be sent at the end of the header.
1343 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1344 /*********************************************************************
1346 * Function : client_accept
1348 * Description : Detect whether the client wants HTML or an image.
1349 * Clients do not always make this information available
1350 * in a sane way. Always passes the header through
1351 * the proxy unchanged.
1355 * 2 : s = Header string. Null terminated.
1356 * 3 : csp = Current client state (buffers, headers, etc...)
1358 * Returns : Duplicate of argument s.
1360 *********************************************************************/
1361 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1363 #ifdef FEATURE_IMAGE_DETECT_MSIE
1364 if (strstr (s, "image/gif"))
1366 /* Client will accept HTML. If this seems counterintuitive,
1369 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1373 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1375 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1380 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1384 /* the following functions add headers directly to the header list */
1387 /*********************************************************************
1389 * Function : client_host_adder
1391 * Description : (re)adds the host header. Called from `sed'.
1394 * 1 : csp = Current client state (buffers, headers, etc...)
1398 *********************************************************************/
1399 void client_host_adder(struct client_state *csp)
1403 p = strsav(p, "Host: ");
1404 p = strsav(p, csp->http->hostport);
1406 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1407 enlist(csp->headers, p);
1413 /*********************************************************************
1415 * Function : client_cookie_adder
1417 * Description : Used in the add_client_headers list. Called from `sed'.
1420 * 1 : csp = Current client state (buffers, headers, etc...)
1424 *********************************************************************/
1425 void client_cookie_adder(struct client_state *csp)
1427 struct list_entry *lst;
1431 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1435 tmp = strsav(tmp, "; ");
1437 tmp = strsav(tmp, lst->str);
1440 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1444 tmp = strsav(tmp, "; ");
1447 if ((e = cookie_encode(lst->str)))
1449 tmp = strsav(tmp, e);
1458 ret = strdup("Cookie: ");
1459 ret = strsav(ret, tmp);
1460 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1461 enlist(csp->headers, ret);
1469 /*********************************************************************
1471 * Function : client_accept_encoding_adder
1473 * Description : Add an Accept-Encoding header to the client's request
1474 * that disables compression if the action applies, and
1475 * the header is not already there. Called from `sed'.
1476 * Note: For HTTP/1.0, the absence of the header is enough.
1479 * 1 : csp = Current client state (buffers, headers, etc...)
1483 *********************************************************************/
1484 void client_accept_encoding_adder(struct client_state *csp)
1486 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1487 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1489 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1495 /*********************************************************************
1497 * Function : client_xtra_adder
1499 * Description : Used in the add_client_headers list. Called from `sed'.
1502 * 1 : csp = Current client state (buffers, headers, etc...)
1506 *********************************************************************/
1507 void client_xtra_adder(struct client_state *csp)
1509 struct list_entry *lst;
1511 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1512 lst ; lst = lst->next)
1514 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1515 enlist(csp->headers, lst->str);
1521 /*********************************************************************
1523 * Function : client_x_forwarded_adder
1525 * Description : Used in the add_client_headers list. Called from `sed'.
1528 * 1 : csp = Current client state (buffers, headers, etc...)
1532 *********************************************************************/
1533 void client_x_forwarded_adder(struct client_state *csp)
1537 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1542 if (csp->x_forwarded)
1544 p = strsav(p, csp->x_forwarded);
1545 p = strsav(p, ", ");
1546 p = strsav(p, csp->ip_addr_str);
1550 p = strsav(p, "X-Forwarded-For: ");
1551 p = strsav(p, csp->ip_addr_str);
1554 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1555 enlist(csp->headers, p);
1561 /*********************************************************************
1563 * Function : connection_close_adder
1565 * Description : Adds a "Connection: close" header to csp->headers
1566 * as a temporary fix for the needed but missing HTTP/1.1
1567 * support. Called from `sed'.
1568 * FIXME: This whole function shouldn't be neccessary!
1571 * 1 : csp = Current client state (buffers, headers, etc...)
1575 *********************************************************************/
1576 void connection_close_adder(struct client_state *csp)
1578 enlist(csp->headers, "Connection: close");
1582 /*********************************************************************
1584 * Function : server_http
1586 * Description : - Save the HTTP Status into csp->http->status
1587 * - Set CT_TABOO to prevent filtering if the answer
1588 * is a partial range (HTTP status 206)
1589 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1593 * 1 : v = parser pattern that matched this header
1594 * 2 : s = header that matched this pattern
1595 * 3 : csp = Current client state (buffers, headers, etc...)
1597 * Returns : Copy of changed or original answer.
1599 *********************************************************************/
1600 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1602 char *ret = strdup(s);
1604 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1605 if (csp->http->status == 206)
1607 csp->content_type = CT_TABOO;
1610 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1613 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1620 /*********************************************************************
1622 * Function : server_set_cookie
1624 * Description : Handle the server "cookie" header properly.
1625 * Log cookie to the jar file. Then "crunch" it,
1626 * or accept it. Called from `sed'.
1629 * 1 : v = parser pattern that matched this header
1630 * 2 : s = header that matched this pattern
1631 * 3 : csp = Current client state (buffers, headers, etc...)
1633 * Returns : `crumble' or a newly malloc'ed string.
1635 *********************************************************************/
1636 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1638 #ifdef FEATURE_COOKIE_JAR
1639 if (csp->config->jar)
1641 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1643 #endif /* def FEATURE_COOKIE_JAR */
1645 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1647 return(crumble(v, s, csp));
1655 #ifdef FEATURE_FORCE_LOAD
1656 /*********************************************************************
1658 * Function : strclean
1660 * Description : In-Situ-Eliminate all occurances of substring in
1664 * 1 : string = string to clean
1665 * 2 : substring = substring to eliminate
1667 * Returns : Number of eliminations
1669 *********************************************************************/
1670 int strclean(const char *string, const char *substring)
1672 int hits = 0, len = strlen(substring);
1675 while((pos = strstr(string, substring)))
1682 while (*p++ != '\0');
1689 #endif /* def FEATURE_FORCE_LOAD */