1 const char parsers_rcs[] = "$Id: parsers.c,v 1.38 2001/10/25 03:40:48 david__schmidt 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.38 2001/10/25 03:40:48 david__schmidt
45 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
46 * threads to call select() simultaneously. So, it's time to do a real, live,
47 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
48 * (native). Both versions will work, but using __OS2__ offers multi-threading.
50 * Revision 1.37 2001/10/23 21:36:02 jongfoster
51 * Documenting sed()'s error behaviou (doc change only)
53 * Revision 1.36 2001/10/13 12:51:51 joergs
54 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
55 * force-load), instead crumble Host: and add it (again) in client_host_adder
56 * (in case we get a HTTP/1.0 request without Host: header and forward it to
57 * a HTTP/1.1 server/proxy).
59 * Revision 1.35 2001/10/09 22:39:21 jongfoster
60 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
63 * Revision 1.34 2001/10/07 18:50:55 oes
64 * Added server_content_encoding, renamed server_transfer_encoding
66 * Revision 1.33 2001/10/07 18:04:49 oes
67 * Changed server_http11 to server_http and its pattern to "HTTP".
68 * Additional functionality: it now saves the HTTP status into
69 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
71 * Revision 1.32 2001/10/07 15:43:28 oes
72 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
73 * client_te and client_accept_encoding_adder, triggered by the new
74 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
75 * changed to allow only identity and chunked, and the TE header is
76 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
78 * parse_http_request no longer does anything than parsing. The rewriting
79 * of http->cmd and version mangling are gone. It now also recognizes
80 * the put and delete methods and saves the url in http->url. Removed
83 * renamed content_type and content_length to have the server_ prefix
85 * server_content_type now only works if csp->content_type != CT_TABOO
87 * added server_transfer_encoding, which
88 * - Sets CT_TABOO to prohibit filtering if encoding compresses
89 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
90 * - Change from "chunked" to "identity" if body was chunked
91 * but has been de-chunked for filtering.
93 * added server_content_md5 which crunches any Content-MD5 headers
94 * if the body was modified.
96 * made server_http11 conditional on +downgrade action
98 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
100 * Revision 1.31 2001/10/05 14:25:02 oes
101 * Crumble Keep-Alive from Server
103 * Revision 1.30 2001/09/29 12:56:03 joergs
104 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
106 * Revision 1.29 2001/09/24 21:09:24 jongfoster
107 * Fixing 2 memory leaks that Guy spotted, where the paramater to
108 * enlist() was not being free()d.
110 * Revision 1.28 2001/09/22 16:32:28 jongfoster
111 * Removing unused #includes.
113 * Revision 1.27 2001/09/20 15:45:25 steudten
115 * add casting from size_t to int for printf()
116 * remove local variable shadow s2
118 * Revision 1.26 2001/09/16 17:05:14 jongfoster
119 * Removing unused #include showarg.h
121 * Revision 1.25 2001/09/16 13:21:27 jongfoster
122 * Changes to use new list functions.
124 * Revision 1.24 2001/09/13 23:05:50 jongfoster
125 * Changing the string paramater to the header parsers a "const".
127 * Revision 1.23 2001/09/12 18:08:19 steudten
129 * In parse_http_request() header rewriting miss the host value, so
130 * from http://www.mydomain.com the result was just " / " not
131 * http://www.mydomain.com/ in case we forward.
133 * Revision 1.22 2001/09/10 10:58:53 oes
134 * Silenced compiler warnings
136 * Revision 1.21 2001/07/31 14:46:00 oes
137 * - Persistant connections now suppressed
138 * - sed() no longer appends empty header to csp->headers
140 * Revision 1.20 2001/07/30 22:08:36 jongfoster
141 * Tidying up #defines:
142 * - All feature #defines are now of the form FEATURE_xxx
143 * - Permanently turned off WIN_GUI_EDIT
144 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
146 * Revision 1.19 2001/07/25 17:21:54 oes
147 * client_uagent now saves copy of User-Agent: header value
149 * Revision 1.18 2001/07/13 14:02:46 oes
150 * - Included fix to repair broken HTTP requests that
151 * don't contain a path, not even '/'.
152 * - Removed all #ifdef PCRS
153 * - content_type now always inspected and classified as
154 * text, gif or other.
155 * - formatting / comments
157 * Revision 1.17 2001/06/29 21:45:41 oes
158 * Indentation, CRLF->LF, Tab-> Space
160 * Revision 1.16 2001/06/29 13:32:42 oes
162 * - Adapted free_http_request
163 * - Removed logentry from cancelled commit
165 * Revision 1.15 2001/06/03 19:12:38 oes
166 * deleted const struct interceptors
168 * Revision 1.14 2001/06/01 18:49:17 jongfoster
169 * Replaced "list_share" with "list" - the tiny memory gain was not
170 * worth the extra complexity.
172 * Revision 1.13 2001/05/31 21:30:33 jongfoster
173 * Removed list code - it's now in list.[ch]
174 * Renamed "permission" to "action", and changed many features
175 * to use the actions file rather than the global config.
177 * Revision 1.12 2001/05/31 17:33:13 oes
181 * Revision 1.11 2001/05/29 20:11:19 joergs
182 * '/ * inside comment' warning removed.
184 * Revision 1.10 2001/05/29 09:50:24 jongfoster
185 * Unified blocklist/imagelist/permissionslist.
186 * File format is still under discussion, but the internal changes
189 * Also modified interceptor behaviour:
190 * - We now intercept all URLs beginning with one of the following
191 * prefixes (and *only* these prefixes):
193 * * http://ijbswa.sf.net/config/
194 * * http://ijbswa.sourceforge.net/config/
195 * - New interceptors "home page" - go to http://i.j.b/ to see it.
196 * - Internal changes so that intercepted and fast redirect pages
197 * are not replaced with an image.
198 * - Interceptors now have the option to send a binary page direct
199 * to the client. (i.e. ijb-send-banner uses this)
200 * - Implemented show-url-info interceptor. (Which is why I needed
201 * the above interceptors changes - a typical URL is
202 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
203 * The previous mechanism would not have intercepted that, and
204 * if it had been intercepted then it then it would have replaced
207 * Revision 1.9 2001/05/28 17:26:33 jongfoster
208 * Fixing segfault if last header was crunched.
209 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
210 * can use the cross-platform sprintf() instead.)
212 * Revision 1.8 2001/05/27 22:17:04 oes
214 * - re_process_buffer no longer writes the modified buffer
215 * to the client, which was very ugly. It now returns the
216 * buffer, which it is then written by chat.
218 * - content_length now adjusts the Content-Length: header
219 * for modified documents rather than crunch()ing it.
220 * (Length info in csp->content_length, which is 0 for
221 * unmodified documents)
223 * - For this to work, sed() is called twice when filtering.
225 * Revision 1.7 2001/05/27 13:19:06 oes
226 * Patched Joergs solution for the content-length in.
228 * Revision 1.6 2001/05/26 13:39:32 jongfoster
229 * Only crunches Content-Length header if applying RE filtering.
230 * Without this fix, Microsoft Windows Update wouldn't work.
232 * Revision 1.5 2001/05/26 00:28:36 jongfoster
233 * Automatic reloading of config file.
234 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
235 * Most of the global variables have been moved to a new
236 * struct configuration_spec, accessed through csp->config->globalname
237 * Most of the globals remaining are used by the Win32 GUI.
239 * Revision 1.4 2001/05/22 18:46:04 oes
241 * - Enabled filtering banners by size rather than URL
242 * by adding patterns that replace all standard banner
243 * sizes with the "Junkbuster" gif to the re_filterfile
245 * - Enabled filtering WebBugs by providing a pattern
246 * which kills all 1x1 images
248 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
249 * which is selected by the (nonstandard and therefore
250 * capital) letter 'U' in the option string.
251 * It causes the quantifiers to be ungreedy by default.
252 * Appending a ? turns back to greedy (!).
254 * - Added a new interceptor ijb-send-banner, which
255 * sends back the "Junkbuster" gif. Without imagelist or
256 * MSIE detection support, or if tinygif = 1, or the
257 * URL isn't recognized as an imageurl, a lame HTML
258 * explanation is sent instead.
260 * - Added new feature, which permits blocking remote
261 * script redirects and firing back a local redirect
263 * The feature is conditionally compiled, i.e. it
264 * can be disabled with --disable-fast-redirects,
265 * plus it must be activated by a "fast-redirects"
266 * line in the config file, has its own log level
267 * and of course wants to be displayed by show-proxy-args
268 * Note: Boy, all the #ifdefs in 1001 locations and
269 * all the fumbling with configure.in and acconfig.h
270 * were *way* more work than the feature itself :-(
272 * - Because a generic redirect template was needed for
273 * this, tinygif = 3 now uses the same.
275 * - Moved GIFs, and other static HTTP response templates
280 * - Removed some >400 CRs again (Jon, you really worked
283 * Revision 1.3 2001/05/20 01:21:20 jongfoster
284 * Version 2.9.4 checkin.
285 * - Merged popupfile and cookiefile, and added control over PCRS
286 * filtering, in new "permissionsfile".
287 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
288 * file error you now get a message box (in the Win32 GUI) rather
289 * than the program exiting with no explanation.
290 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
292 * - Removed tabs from "config"
293 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
294 * - Bumped up version number.
296 * Revision 1.2 2001/05/17 23:02:36 oes
297 * - Made referrer option accept 'L' as a substitute for '§'
299 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
300 * Initial import of version 2.9.3 source tree
303 *********************************************************************/
310 #include <sys/types.h>
318 #if !defined(_WIN32) && !defined(__OS2__)
328 #include "jbsockets.h"
329 #include "miscutil.h"
332 const char parsers_h_rcs[] = PARSERS_H_VERSION;
334 /* Fix a problem with Solaris. There should be no effect on other
336 * Solaris's isspace() is a macro which uses it's argument directly
337 * as an array index. Therefore we need to make sure that high-bit
338 * characters generate +ve values, and ideally we also want to make
339 * the argument match the declared parameter type of "int".
341 * Why did they write a character function that can't take a simple
342 * "char" argument? Doh!
344 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
345 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
348 const struct parsers client_patterns[] = {
349 { "referer:", 8, client_referrer },
350 { "user-agent:", 11, client_uagent },
351 { "ua-", 3, client_ua },
352 { "from:", 5, client_from },
353 { "cookie:", 7, client_send_cookie },
354 { "x-forwarded-for:", 16, client_x_forwarded },
355 { "Accept-Encoding:", 16, client_accept_encoding },
356 { "TE:", 3, client_te },
357 { "Host:", 5, crumble },
358 /* { "if-modified-since:", 18, crumble }, */
359 { "Keep-Alive:", 11, crumble },
360 { "connection:", 11, crumble },
361 { "proxy-connection:", 17, crumble },
366 const struct parsers server_patterns[] = {
367 { "HTTP", 4, server_http },
368 { "set-cookie:", 11, server_set_cookie },
369 { "connection:", 11, crumble },
370 { "Content-Type:", 13, server_content_type },
371 { "Content-Length:", 15, server_content_length },
372 { "Content-MD5:", 12, server_content_md5 },
373 { "Content-Encoding:", 17, server_content_encoding },
374 { "Transfer-Encoding:", 18, server_transfer_coding },
375 { "Keep-Alive:", 11, crumble },
380 void (* const add_client_headers[])(struct client_state *) = {
383 client_x_forwarded_adder,
385 client_accept_encoding_adder,
386 connection_close_adder,
391 void (* const add_server_headers[])(struct client_state *) = {
392 connection_close_adder,
397 /*********************************************************************
399 * Function : flush_socket
401 * Description : Write any pending "buffered" content.
404 * 1 : fd = file descriptor of the socket to read
405 * 2 : csp = Current client state (buffers, headers, etc...)
407 * Returns : On success, the number of bytes written are returned (zero
408 * indicates nothing was written). On error, -1 is returned,
409 * and errno is set appropriately. If count is zero and the
410 * file descriptor refers to a regular file, 0 will be
411 * returned without causing any other effect. For a special
412 * file, the results are not portable.
414 *********************************************************************/
415 int flush_socket(int fd, struct client_state *csp)
417 struct iob *iob = csp->iob;
418 int n = iob->eod - iob->cur;
425 n = write_socket(fd, iob->cur, n);
426 iob->eod = iob->cur = iob->buf;
432 /*********************************************************************
434 * Function : add_to_iob
436 * Description : Add content to the buffered page.
439 * 1 : csp = Current client state (buffers, headers, etc...)
440 * 2 : buf = holds the content to be added to the page
441 * 3 : n = number of bytes to be added
443 * Returns : Number of bytes in the content buffer.
445 *********************************************************************/
446 int add_to_iob(struct client_state *csp, char *buf, int n)
448 struct iob *iob = csp->iob;
452 have = iob->eod - iob->cur;
461 if ((p = (char *)malloc(need + 1)) == NULL)
463 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
469 /* there is something in the buffer - save it */
470 memcpy(p, iob->cur, have);
472 /* replace the buffer with the new space */
476 /* point to the end of the data */
481 /* the buffer is empty, free it and reinitialize */
486 /* copy the new data into the iob buffer */
489 /* point to the end of the data */
492 /* null terminate == cheap insurance */
495 /* set the pointers to the new values */
504 /*********************************************************************
506 * Function : get_header
508 * Description : This (odd) routine will parse the csp->iob
511 * 1 : csp = Current client state (buffers, headers, etc...)
513 * Returns : Any one of the following:
515 * 1) a pointer to a dynamically allocated string that contains a header line
516 * 2) NULL indicating that the end of the header was reached
517 * 3) "" indicating that the end of the iob was reached before finding
518 * a complete header line.
520 *********************************************************************/
521 char *get_header(struct client_state *csp)
527 if ((iob->cur == NULL)
528 || ((p = strchr(iob->cur, '\n')) == NULL))
530 return(""); /* couldn't find a complete header */
535 ret = strdup(iob->cur);
539 if ((q = strchr(ret, '\r'))) *q = '\0';
541 /* is this a blank linke (i.e. the end of the header) ? */
553 /*********************************************************************
555 * Function : get_header_value
557 * Description : Get the value of a given header from a chained list
558 * of header lines or return NULL if no such header is
559 * present in the list.
562 * 1 : header_list = pointer to list
563 * 2 : header_name = string with name of header to look for.
564 * Trailing colon required, capitalization
567 * Returns : NULL if not found, else value of header
569 *********************************************************************/
570 char *get_header_value(const struct list *header_list, const char *header_name)
572 struct list_entry *cur_entry;
578 length = strlen(header_name);
580 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
584 if (!strncmpic(cur_entry->str, header_name, length))
587 * Found: return pointer to start of value
589 ret = (char *) (cur_entry->str + length);
590 while (*ret && ijb_isspace(*ret)) ret++;
603 /*********************************************************************
607 * Description : add, delete or modify lines in the HTTP header streams.
608 * On entry, it receives a linked list of headers space
609 * that was allocated dynamically (both the list nodes
610 * and the header contents).
612 * As a side effect it frees the space used by the original
616 * 1 : pats = list of patterns to match against headers
617 * 2 : more_headers = list of functions to add more
618 * headers (client or server)
619 * 3 : csp = Current client state (buffers, headers, etc...)
621 * Returns : Single pointer to a fully formed header, or NULL
622 * on out-of-memory error.
624 *********************************************************************/
625 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
627 struct list_entry *p;
628 const struct parsers *v;
632 for (v = pats; v->str ; v++)
634 for (p = csp->headers->first; p ; p = p->next)
636 /* Header crunch()ed in previous run? -> ignore */
637 if (p->str == NULL) continue;
639 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
641 if (strncmpic(p->str, v->str, v->len) == 0)
643 hdr = v->parser(v, p->str, csp);
644 freez(p->str); /* FIXME: Yuck! patching a list...*/
650 /* place any additional headers on the csp->headers list */
651 for (f = more_headers; *f ; f++)
656 hdr = list_to_text(csp->headers);
663 /*********************************************************************
665 * Function : free_http_request
667 * Description : Freez a http_request structure
670 * 1 : http = points to a http_request structure to free
674 *********************************************************************/
675 void free_http_request(struct http_request *http)
683 freez(http->hostport);
686 freez(http->host_ip_addr_str);
691 /*********************************************************************
693 * Function : parse_http_request
695 * Description : Parse out the host and port from the URL. Find the
696 * hostname & path, port (if ':'), and/or password (if '@')
699 * 1 : req = URL (or is it URI?) to break down
700 * 2 : http = pointer to the http structure to hold elements
701 * 3 : csp = Current client state (buffers, headers, etc...)
705 *********************************************************************/
706 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
708 char *buf, *v[10], *url, *p;
711 memset(http, '\0', sizeof(*http));
712 http->cmd = strdup(req);
715 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
719 /* this could be a CONNECT request */
720 if (strcmpic(v[0], "connect") == 0)
723 http->gpc = strdup(v[0]);
724 http->hostport = strdup(v[1]);
725 http->ver = strdup(v[2]);
728 /* or it could be any other basic HTTP request type */
729 if ((0 == strcmpic(v[0], "get"))
730 || (0 == strcmpic(v[0], "head"))
731 || (0 == strcmpic(v[0], "post"))
732 || (0 == strcmpic(v[0], "put"))
733 || (0 == strcmpic(v[0], "delete"))
735 /* or a webDAV extension (RFC2518) */
736 || (0 == strcmpic(v[0], "propfind"))
737 || (0 == strcmpic(v[0], "proppatch"))
738 || (0 == strcmpic(v[0], "move"))
739 || (0 == strcmpic(v[0], "copy"))
740 || (0 == strcmpic(v[0], "mkcol"))
741 || (0 == strcmpic(v[0], "lock"))
742 || (0 == strcmpic(v[0], "unlock"))
746 http->gpc = strdup(v[0]);
747 http->url = strdup(v[1]);
748 http->ver = strdup(v[2]);
751 if (strncmpic(url, "http://", 7) == 0)
755 else if (strncmpic(url, "https://", 8) == 0)
766 if ((p = strchr(url, '/')))
768 http->path = strdup(p);
770 http->hostport = strdup(url);
773 * Repair broken HTTP requests that don't contain a path
777 http->path = strdup("/");
778 http->hostport = strdup(url);
787 if (http->hostport == NULL)
789 free_http_request(http);
793 buf = strdup(http->hostport);
796 /* check if url contains password */
797 n = ssplit(buf, "@", v, SZ(v), 1, 1);
800 char * newbuf = NULL;
801 newbuf = strdup(v[1]);
806 n = ssplit(buf, ":", v, SZ(v), 1, 1);
810 http->host = strdup(v[0]);
816 http->host = strdup(v[0]);
817 http->port = atoi(v[1]);
822 if (http->host == NULL)
824 free_http_request(http);
827 if (http->path == NULL)
829 http->path = strdup("/");
835 /* here begins the family of parser functions that reformat header lines */
838 /*********************************************************************
842 * Description : This is called if a header matches a pattern to "crunch"
845 * 1 : v = Pointer to parsers structure, which basically holds
846 * headers (client or server) that we want to "crunch"
847 * 2 : s = header (from sed) to "crunch"
848 * 3 : csp = Current client state (buffers, headers, etc...)
850 * Returns : Always NULL.
852 *********************************************************************/
853 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
855 log_error(LOG_LEVEL_HEADER, "crunch!");
861 /*********************************************************************
863 * Function : server_content_type
865 * Description : Set the content-type for filterable types (text/.*,
866 * javascript and image/gif) unless filtering has been
867 * forbidden (CT_TABOO) while parsing earlier headers.
871 * 2 : s = header string we are "considering"
872 * 3 : csp = Current client state (buffers, headers, etc...)
874 * Returns : A duplicate string pointer to this header (ie. pass thru)
876 *********************************************************************/
877 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
879 if (csp->content_type != CT_TABOO)
881 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
882 csp->content_type = CT_TEXT;
883 else if (strstr(s, " image/gif"))
884 csp->content_type = CT_GIF;
886 csp->content_type = 0;
894 /*********************************************************************
896 * Function : server_transfer_coding
898 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
899 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
900 * - Change from "chunked" to "identity" if body was chunked
901 * but has been de-chunked for filtering.
905 * 2 : s = header string we are "considering"
906 * 3 : csp = Current client state (buffers, headers, etc...)
908 * Returns : A duplicate string pointer to this header (ie. pass thru)
910 *********************************************************************/
911 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
914 * Turn off pcrs and gif filtering if body compressed
916 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
918 csp->content_type = CT_TABOO;
922 * Raise flag if body chunked
924 if (strstr(s, "chunked"))
926 csp->flags |= CSP_FLAG_CHUNKED;
929 * If the body was modified, it has been
930 * de-chunked first, so adjust the header:
932 if (csp->flags & CSP_FLAG_MODIFIED)
934 return(strdup("Transfer-Encoding: identity"));
943 /*********************************************************************
945 * Function : server_content_encoding
947 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
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_encoding(const struct parsers *v, const char *s, struct client_state *csp)
960 * Turn off pcrs and gif filtering if body compressed
962 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
964 csp->content_type = CT_TABOO;
972 /*********************************************************************
974 * Function : server_content_length
976 * Description : Adjust Content-Length header if we modified
981 * 2 : s = header string we are "considering"
982 * 3 : csp = Current client state (buffers, headers, etc...)
984 * Returns : A duplicate string pointer to this header (ie. pass thru)
986 *********************************************************************/
987 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
989 if (csp->content_length != 0) /* Content length has been modified */
991 char * s2 = (char *) zalloc(100);
992 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
994 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1005 /*********************************************************************
1007 * Function : server_content_md5
1009 * Description : Crumble any Content-MD5 headers if the document was
1010 * modified. FIXME: Should we re-compute instead?
1014 * 2 : s = header string we are "considering"
1015 * 3 : csp = Current client state (buffers, headers, etc...)
1017 * Returns : A duplicate string pointer to this header (ie. pass thru)
1019 *********************************************************************/
1020 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
1022 if (csp->flags & CSP_FLAG_MODIFIED)
1024 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1035 /*********************************************************************
1037 * Function : client_accept_encoding
1039 * Description : Rewrite the client's Accept-Encoding header so that
1040 * if doesn't allow compression, if the action applies.
1041 * Note: For HTTP/1.0 the absence of the header is enough.
1045 * 2 : s = header string we are "considering"
1046 * 3 : csp = Current client state (buffers, headers, etc...)
1048 * Returns : A copy of the client's original or the modified header.
1050 *********************************************************************/
1051 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
1053 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1059 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1061 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1063 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1074 /*********************************************************************
1076 * Function : client_te
1078 * Description : Rewrite the client's TE header so that
1079 * if doesn't allow compression, if the action applies.
1083 * 2 : s = header string we are "considering"
1084 * 3 : csp = Current client state (buffers, headers, etc...)
1086 * Returns : A copy of the client's original or the modified header.
1088 *********************************************************************/
1089 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1091 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1097 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1103 /*********************************************************************
1105 * Function : client_referrer
1107 * Description : Handle the "referer" config setting properly.
1108 * Called from `sed'.
1112 * 2 : s = header (from sed) to "crunch"
1113 * 3 : csp = Current client state (buffers, headers, etc...)
1115 * Returns : NULL if crunched, or a malloc'ed string with the original
1116 * or modified header
1118 *********************************************************************/
1119 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1121 const char * newval;
1123 #ifdef FEATURE_FORCE_LOAD
1124 /* Since the referrer can include the prefix even
1125 * even if the request itself is non-forced, we must
1126 * clean it unconditionally
1128 strclean(s, FORCE_PREFIX);
1129 #endif /* def FEATURE_FORCE_LOAD */
1132 * Are we sending referer?
1134 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1139 newval = csp->action->string[ACTION_STRING_REFERER];
1142 * Are we blocking referer?
1144 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1146 log_error(LOG_LEVEL_HEADER, "crunch!");
1151 * Are we forging referer?
1153 if (0 == strcmpic(newval, "forge"))
1156 * Forge a referer as http://[hostname:port of REQUEST]/
1157 * to fool stupid checks for in-site links
1159 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1160 s2 = strsav(NULL, "Referer: ");
1161 s2 = strsav(s2, "http://");
1162 s2 = strsav(s2, csp->http->hostport);
1163 s2 = strsav(s2, "/");
1168 * Have we got a fixed referer?
1170 if (0 == strncmpic(newval, "http://", 7))
1173 * We have a specific (fixed) referer we want to send.
1177 log_error(LOG_LEVEL_HEADER, "modified");
1179 s3 = strsav( NULL, "Referer: " );
1180 s3 = strsav( s3, newval );
1184 /* Should never get here! */
1185 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1188 * Forge is probably the best default.
1190 * Forge a referer as http://[hostname:port of REQUEST]/
1191 * to fool stupid checks for in-site links
1193 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1194 s2 = strsav(NULL, "Referer: ");
1195 s2 = strsav(s2, "http://");
1196 s2 = strsav(s2, csp->http->hostport);
1197 s2 = strsav(s2, "/");
1202 /*********************************************************************
1204 * Function : client_uagent
1206 * Description : Handle the "user-agent" config setting properly
1207 * and remember its original value to enable browser
1208 * bug workarounds. Called from `sed'.
1212 * 2 : s = header (from sed) to "crunch"
1213 * 3 : csp = Current client state (buffers, headers, etc...)
1215 * Returns : A malloc'ed pointer to the default agent, or
1216 * a malloc'ed string pointer to this header (ie. pass thru).
1218 *********************************************************************/
1219 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1221 const char * newval;
1224 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1229 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1235 log_error(LOG_LEVEL_HEADER, "modified");
1237 s2 = strsav( NULL, "User-Agent: " );
1238 s2 = strsav( s2, newval );
1244 /*********************************************************************
1246 * Function : client_ua
1248 * Description : Handle "ua-" headers properly. Called from `sed'.
1252 * 2 : s = header (from sed) to "crunch"
1253 * 3 : csp = Current client state (buffers, headers, etc...)
1255 * Returns : NULL if crunched, or a malloc'ed string to original header
1257 *********************************************************************/
1258 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1260 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1266 log_error(LOG_LEVEL_HEADER, "crunch!");
1272 /*********************************************************************
1274 * Function : client_from
1276 * Description : Handle the "from" config setting properly.
1277 * Called from `sed'.
1281 * 2 : s = header (from sed) to "crunch"
1282 * 3 : csp = Current client state (buffers, headers, etc...)
1284 * Returns : NULL if crunched, or a malloc'ed string to
1285 * modified/original header.
1287 *********************************************************************/
1288 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1290 const char * newval;
1293 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1298 newval = csp->action->string[ACTION_STRING_FROM];
1301 * Are we blocking referer?
1303 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1305 log_error(LOG_LEVEL_HEADER, "crunch!");
1309 log_error(LOG_LEVEL_HEADER, " modified");
1311 s2 = strsav( NULL, "From: " );
1312 s2 = strsav( s2, newval );
1318 /*********************************************************************
1320 * Function : client_send_cookie
1322 * Description : Handle the "cookie" header properly. Called from `sed'.
1323 * If cookie is accepted, add it to the cookie_list,
1324 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1327 * 1 : v = pattern of cookie `sed' found matching
1328 * 2 : s = header (from sed) to "crunch"
1329 * 3 : csp = Current client state (buffers, headers, etc...)
1331 * Returns : Always NULL.
1333 *********************************************************************/
1334 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1336 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1338 enlist(csp->cookie_list, s + v->len + 1);
1342 log_error(LOG_LEVEL_HEADER, " crunch!");
1346 * Always return NULL here. The cookie header
1347 * will be sent at the end of the header.
1354 /*********************************************************************
1356 * Function : client_x_forwarded
1358 * Description : Handle the "x-forwarded-for" config setting properly,
1359 * also used in the add_client_headers list. Called from `sed'.
1363 * 2 : s = header (from sed) to "crunch"
1364 * 3 : csp = Current client state (buffers, headers, etc...)
1366 * Returns : Always NULL.
1368 *********************************************************************/
1369 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1371 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1373 /* Save it so we can re-add it later */
1374 csp->x_forwarded = strdup(s);
1378 * Always return NULL, since this information
1379 * will be sent at the end of the header.
1386 /* the following functions add headers directly to the header list */
1388 /*********************************************************************
1390 * Function : client_host_adder
1392 * Description : (re)adds the host header. Called from `sed'.
1395 * 1 : csp = Current client state (buffers, headers, etc...)
1399 *********************************************************************/
1400 void client_host_adder(struct client_state *csp)
1404 p = strsav(p, "Host: ");
1405 p = strsav(p, csp->http->hostport);
1407 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1408 enlist(csp->headers, p);
1414 /*********************************************************************
1416 * Function : client_cookie_adder
1418 * Description : Used in the add_client_headers list. Called from `sed'.
1421 * 1 : csp = Current client state (buffers, headers, etc...)
1425 *********************************************************************/
1426 void client_cookie_adder(struct client_state *csp)
1428 struct list_entry *lst;
1432 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1436 tmp = strsav(tmp, "; ");
1438 tmp = strsav(tmp, lst->str);
1441 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1445 tmp = strsav(tmp, "; ");
1448 if ((e = cookie_encode(lst->str)))
1450 tmp = strsav(tmp, e);
1459 ret = strdup("Cookie: ");
1460 ret = strsav(ret, tmp);
1461 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1462 enlist(csp->headers, ret);
1470 /*********************************************************************
1472 * Function : client_accept_encoding_adder
1474 * Description : Add an Accept-Encoding header to the client's request
1475 * that disables compression if the action applies, and
1476 * the header is not already there. Called from `sed'.
1477 * Note: For HTTP/1.0, the absence of the header is enough.
1480 * 1 : csp = Current client state (buffers, headers, etc...)
1484 *********************************************************************/
1485 void client_accept_encoding_adder(struct client_state *csp)
1487 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1488 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1490 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1496 /*********************************************************************
1498 * Function : client_xtra_adder
1500 * Description : Used in the add_client_headers list. Called from `sed'.
1503 * 1 : csp = Current client state (buffers, headers, etc...)
1507 *********************************************************************/
1508 void client_xtra_adder(struct client_state *csp)
1510 struct list_entry *lst;
1512 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1513 lst ; lst = lst->next)
1515 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1516 enlist(csp->headers, lst->str);
1522 /*********************************************************************
1524 * Function : client_x_forwarded_adder
1526 * Description : Used in the add_client_headers list. Called from `sed'.
1529 * 1 : csp = Current client state (buffers, headers, etc...)
1533 *********************************************************************/
1534 void client_x_forwarded_adder(struct client_state *csp)
1538 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1543 if (csp->x_forwarded)
1545 p = strsav(p, csp->x_forwarded);
1546 p = strsav(p, ", ");
1547 p = strsav(p, csp->ip_addr_str);
1551 p = strsav(p, "X-Forwarded-For: ");
1552 p = strsav(p, csp->ip_addr_str);
1555 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1556 enlist(csp->headers, p);
1562 /*********************************************************************
1564 * Function : connection_close_adder
1566 * Description : Adds a "Connection: close" header to csp->headers
1567 * as a temporary fix for the needed but missing HTTP/1.1
1568 * support. Called from `sed'.
1569 * FIXME: This whole function shouldn't be neccessary!
1572 * 1 : csp = Current client state (buffers, headers, etc...)
1576 *********************************************************************/
1577 void connection_close_adder(struct client_state *csp)
1579 enlist(csp->headers, "Connection: close");
1583 /*********************************************************************
1585 * Function : server_http
1587 * Description : - Save the HTTP Status into csp->http->status
1588 * - Set CT_TABOO to prevent filtering if the answer
1589 * is a partial range (HTTP status 206)
1590 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1594 * 1 : v = parser pattern that matched this header
1595 * 2 : s = header that matched this pattern
1596 * 3 : csp = Current client state (buffers, headers, etc...)
1598 * Returns : Copy of changed or original answer.
1600 *********************************************************************/
1601 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1603 char *ret = strdup(s);
1605 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1606 if (csp->http->status == 206)
1608 csp->content_type = CT_TABOO;
1611 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1614 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1621 /*********************************************************************
1623 * Function : server_set_cookie
1625 * Description : Handle the server "cookie" header properly.
1626 * Log cookie to the jar file. Then "crunch" it,
1627 * or accept it. Called from `sed'.
1630 * 1 : v = parser pattern that matched this header
1631 * 2 : s = header that matched this pattern
1632 * 3 : csp = Current client state (buffers, headers, etc...)
1634 * Returns : `crumble' or a newly malloc'ed string.
1636 *********************************************************************/
1637 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1639 #ifdef FEATURE_COOKIE_JAR
1640 if (csp->config->jar)
1642 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1644 #endif /* def FEATURE_COOKIE_JAR */
1646 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1648 return(crumble(v, s, csp));
1656 #ifdef FEATURE_FORCE_LOAD
1657 /*********************************************************************
1659 * Function : strclean
1661 * Description : In-Situ-Eliminate all occurances of substring in
1665 * 1 : string = string to clean
1666 * 2 : substring = substring to eliminate
1668 * Returns : Number of eliminations
1670 *********************************************************************/
1671 int strclean(const char *string, const char *substring)
1673 int hits = 0, len = strlen(substring);
1676 while((pos = strstr(string, substring)))
1683 while (*p++ != '\0');
1690 #endif /* def FEATURE_FORCE_LOAD */