1 const char parsers_rcs[] = "$Id: parsers.c,v 1.39 2001/10/26 17:40:04 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `add_to_iob', `client_cookie_adder', `client_from',
9 * `client_referrer', `client_send_cookie', `client_ua',
10 * `client_uagent', `client_x_forwarded',
11 * `client_x_forwarded_adder', `client_xtra_adder',
12 * `content_type', `crumble', `destroy_list', `enlist',
13 * `flush_socket', `free_http_request', `get_header',
14 * `list_to_text', `parse_http_request', `sed',
15 * and `server_set_cookie'.
17 * Copyright : Written by and Copyright (C) 2001 the SourceForge
18 * IJBSWA team. http://ijbswa.sourceforge.net
20 * Based on the Internet Junkbuster originally written
21 * by and Copyright (C) 1997 Anonymous Coders and
22 * Junkbusters Corporation. http://www.junkbusters.com
24 * This program is free software; you can redistribute it
25 * and/or modify it under the terms of the GNU General
26 * Public License as published by the Free Software
27 * Foundation; either version 2 of the License, or (at
28 * your option) any later version.
30 * This program is distributed in the hope that it will
31 * be useful, but WITHOUT ANY WARRANTY; without even the
32 * implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU General Public
34 * License for more details.
36 * The GNU General Public License should be included with
37 * this file. If not, you can view it at
38 * http://www.gnu.org/copyleft/gpl.html
39 * or write to the Free Software Foundation, Inc., 59
40 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 * Revision 1.39 2001/10/26 17:40:04 oes
45 * Introduced get_header_value()
46 * Removed http->user_agent, csp->referrer and csp->accept_types
47 * Removed client_accept()
49 * Revision 1.38 2001/10/25 03:40:48 david__schmidt
50 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
51 * threads to call select() simultaneously. So, it's time to do a real, live,
52 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
53 * (native). Both versions will work, but using __OS2__ offers multi-threading.
55 * Revision 1.37 2001/10/23 21:36:02 jongfoster
56 * Documenting sed()'s error behaviou (doc change only)
58 * Revision 1.36 2001/10/13 12:51:51 joergs
59 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
60 * force-load), instead crumble Host: and add it (again) in client_host_adder
61 * (in case we get a HTTP/1.0 request without Host: header and forward it to
62 * a HTTP/1.1 server/proxy).
64 * Revision 1.35 2001/10/09 22:39:21 jongfoster
65 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
68 * Revision 1.34 2001/10/07 18:50:55 oes
69 * Added server_content_encoding, renamed server_transfer_encoding
71 * Revision 1.33 2001/10/07 18:04:49 oes
72 * Changed server_http11 to server_http and its pattern to "HTTP".
73 * Additional functionality: it now saves the HTTP status into
74 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
76 * Revision 1.32 2001/10/07 15:43:28 oes
77 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
78 * client_te and client_accept_encoding_adder, triggered by the new
79 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
80 * changed to allow only identity and chunked, and the TE header is
81 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
83 * parse_http_request no longer does anything than parsing. The rewriting
84 * of http->cmd and version mangling are gone. It now also recognizes
85 * the put and delete methods and saves the url in http->url. Removed
88 * renamed content_type and content_length to have the server_ prefix
90 * server_content_type now only works if csp->content_type != CT_TABOO
92 * added server_transfer_encoding, which
93 * - Sets CT_TABOO to prohibit filtering if encoding compresses
94 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
95 * - Change from "chunked" to "identity" if body was chunked
96 * but has been de-chunked for filtering.
98 * added server_content_md5 which crunches any Content-MD5 headers
99 * if the body was modified.
101 * made server_http11 conditional on +downgrade action
103 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
105 * Revision 1.31 2001/10/05 14:25:02 oes
106 * Crumble Keep-Alive from Server
108 * Revision 1.30 2001/09/29 12:56:03 joergs
109 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
111 * Revision 1.29 2001/09/24 21:09:24 jongfoster
112 * Fixing 2 memory leaks that Guy spotted, where the paramater to
113 * enlist() was not being free()d.
115 * Revision 1.28 2001/09/22 16:32:28 jongfoster
116 * Removing unused #includes.
118 * Revision 1.27 2001/09/20 15:45:25 steudten
120 * add casting from size_t to int for printf()
121 * remove local variable shadow s2
123 * Revision 1.26 2001/09/16 17:05:14 jongfoster
124 * Removing unused #include showarg.h
126 * Revision 1.25 2001/09/16 13:21:27 jongfoster
127 * Changes to use new list functions.
129 * Revision 1.24 2001/09/13 23:05:50 jongfoster
130 * Changing the string paramater to the header parsers a "const".
132 * Revision 1.23 2001/09/12 18:08:19 steudten
134 * In parse_http_request() header rewriting miss the host value, so
135 * from http://www.mydomain.com the result was just " / " not
136 * http://www.mydomain.com/ in case we forward.
138 * Revision 1.22 2001/09/10 10:58:53 oes
139 * Silenced compiler warnings
141 * Revision 1.21 2001/07/31 14:46:00 oes
142 * - Persistant connections now suppressed
143 * - sed() no longer appends empty header to csp->headers
145 * Revision 1.20 2001/07/30 22:08:36 jongfoster
146 * Tidying up #defines:
147 * - All feature #defines are now of the form FEATURE_xxx
148 * - Permanently turned off WIN_GUI_EDIT
149 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
151 * Revision 1.19 2001/07/25 17:21:54 oes
152 * client_uagent now saves copy of User-Agent: header value
154 * Revision 1.18 2001/07/13 14:02:46 oes
155 * - Included fix to repair broken HTTP requests that
156 * don't contain a path, not even '/'.
157 * - Removed all #ifdef PCRS
158 * - content_type now always inspected and classified as
159 * text, gif or other.
160 * - formatting / comments
162 * Revision 1.17 2001/06/29 21:45:41 oes
163 * Indentation, CRLF->LF, Tab-> Space
165 * Revision 1.16 2001/06/29 13:32:42 oes
167 * - Adapted free_http_request
168 * - Removed logentry from cancelled commit
170 * Revision 1.15 2001/06/03 19:12:38 oes
171 * deleted const struct interceptors
173 * Revision 1.14 2001/06/01 18:49:17 jongfoster
174 * Replaced "list_share" with "list" - the tiny memory gain was not
175 * worth the extra complexity.
177 * Revision 1.13 2001/05/31 21:30:33 jongfoster
178 * Removed list code - it's now in list.[ch]
179 * Renamed "permission" to "action", and changed many features
180 * to use the actions file rather than the global config.
182 * Revision 1.12 2001/05/31 17:33:13 oes
186 * Revision 1.11 2001/05/29 20:11:19 joergs
187 * '/ * inside comment' warning removed.
189 * Revision 1.10 2001/05/29 09:50:24 jongfoster
190 * Unified blocklist/imagelist/permissionslist.
191 * File format is still under discussion, but the internal changes
194 * Also modified interceptor behaviour:
195 * - We now intercept all URLs beginning with one of the following
196 * prefixes (and *only* these prefixes):
198 * * http://ijbswa.sf.net/config/
199 * * http://ijbswa.sourceforge.net/config/
200 * - New interceptors "home page" - go to http://i.j.b/ to see it.
201 * - Internal changes so that intercepted and fast redirect pages
202 * are not replaced with an image.
203 * - Interceptors now have the option to send a binary page direct
204 * to the client. (i.e. ijb-send-banner uses this)
205 * - Implemented show-url-info interceptor. (Which is why I needed
206 * the above interceptors changes - a typical URL is
207 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
208 * The previous mechanism would not have intercepted that, and
209 * if it had been intercepted then it then it would have replaced
212 * Revision 1.9 2001/05/28 17:26:33 jongfoster
213 * Fixing segfault if last header was crunched.
214 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
215 * can use the cross-platform sprintf() instead.)
217 * Revision 1.8 2001/05/27 22:17:04 oes
219 * - re_process_buffer no longer writes the modified buffer
220 * to the client, which was very ugly. It now returns the
221 * buffer, which it is then written by chat.
223 * - content_length now adjusts the Content-Length: header
224 * for modified documents rather than crunch()ing it.
225 * (Length info in csp->content_length, which is 0 for
226 * unmodified documents)
228 * - For this to work, sed() is called twice when filtering.
230 * Revision 1.7 2001/05/27 13:19:06 oes
231 * Patched Joergs solution for the content-length in.
233 * Revision 1.6 2001/05/26 13:39:32 jongfoster
234 * Only crunches Content-Length header if applying RE filtering.
235 * Without this fix, Microsoft Windows Update wouldn't work.
237 * Revision 1.5 2001/05/26 00:28:36 jongfoster
238 * Automatic reloading of config file.
239 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
240 * Most of the global variables have been moved to a new
241 * struct configuration_spec, accessed through csp->config->globalname
242 * Most of the globals remaining are used by the Win32 GUI.
244 * Revision 1.4 2001/05/22 18:46:04 oes
246 * - Enabled filtering banners by size rather than URL
247 * by adding patterns that replace all standard banner
248 * sizes with the "Junkbuster" gif to the re_filterfile
250 * - Enabled filtering WebBugs by providing a pattern
251 * which kills all 1x1 images
253 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
254 * which is selected by the (nonstandard and therefore
255 * capital) letter 'U' in the option string.
256 * It causes the quantifiers to be ungreedy by default.
257 * Appending a ? turns back to greedy (!).
259 * - Added a new interceptor ijb-send-banner, which
260 * sends back the "Junkbuster" gif. Without imagelist or
261 * MSIE detection support, or if tinygif = 1, or the
262 * URL isn't recognized as an imageurl, a lame HTML
263 * explanation is sent instead.
265 * - Added new feature, which permits blocking remote
266 * script redirects and firing back a local redirect
268 * The feature is conditionally compiled, i.e. it
269 * can be disabled with --disable-fast-redirects,
270 * plus it must be activated by a "fast-redirects"
271 * line in the config file, has its own log level
272 * and of course wants to be displayed by show-proxy-args
273 * Note: Boy, all the #ifdefs in 1001 locations and
274 * all the fumbling with configure.in and acconfig.h
275 * were *way* more work than the feature itself :-(
277 * - Because a generic redirect template was needed for
278 * this, tinygif = 3 now uses the same.
280 * - Moved GIFs, and other static HTTP response templates
285 * - Removed some >400 CRs again (Jon, you really worked
288 * Revision 1.3 2001/05/20 01:21:20 jongfoster
289 * Version 2.9.4 checkin.
290 * - Merged popupfile and cookiefile, and added control over PCRS
291 * filtering, in new "permissionsfile".
292 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
293 * file error you now get a message box (in the Win32 GUI) rather
294 * than the program exiting with no explanation.
295 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
297 * - Removed tabs from "config"
298 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
299 * - Bumped up version number.
301 * Revision 1.2 2001/05/17 23:02:36 oes
302 * - Made referrer option accept 'L' as a substitute for '§'
304 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
305 * Initial import of version 2.9.3 source tree
308 *********************************************************************/
315 #include <sys/types.h>
323 #if !defined(_WIN32) && !defined(__OS2__)
333 #include "jbsockets.h"
334 #include "miscutil.h"
337 const char parsers_h_rcs[] = PARSERS_H_VERSION;
339 /* Fix a problem with Solaris. There should be no effect on other
341 * Solaris's isspace() is a macro which uses it's argument directly
342 * as an array index. Therefore we need to make sure that high-bit
343 * characters generate +ve values, and ideally we also want to make
344 * the argument match the declared parameter type of "int".
346 * Why did they write a character function that can't take a simple
347 * "char" argument? Doh!
349 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
350 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
353 const struct parsers client_patterns[] = {
354 { "referer:", 8, client_referrer },
355 { "user-agent:", 11, client_uagent },
356 { "ua-", 3, client_ua },
357 { "from:", 5, client_from },
358 { "cookie:", 7, client_send_cookie },
359 { "x-forwarded-for:", 16, client_x_forwarded },
360 { "Accept-Encoding:", 16, client_accept_encoding },
361 { "TE:", 3, client_te },
362 { "Host:", 5, crumble },
363 /* { "if-modified-since:", 18, crumble }, */
364 { "Keep-Alive:", 11, crumble },
365 { "connection:", 11, crumble },
366 { "proxy-connection:", 17, crumble },
371 const struct parsers server_patterns[] = {
372 { "HTTP", 4, server_http },
373 { "set-cookie:", 11, server_set_cookie },
374 { "connection:", 11, crumble },
375 { "Content-Type:", 13, server_content_type },
376 { "Content-Length:", 15, server_content_length },
377 { "Content-MD5:", 12, server_content_md5 },
378 { "Content-Encoding:", 17, server_content_encoding },
379 { "Transfer-Encoding:", 18, server_transfer_coding },
380 { "Keep-Alive:", 11, crumble },
385 void (* const add_client_headers[])(struct client_state *) = {
388 client_x_forwarded_adder,
390 client_accept_encoding_adder,
391 connection_close_adder,
396 void (* const add_server_headers[])(struct client_state *) = {
397 connection_close_adder,
402 /*********************************************************************
404 * Function : flush_socket
406 * Description : Write any pending "buffered" content.
409 * 1 : fd = file descriptor of the socket to read
410 * 2 : csp = Current client state (buffers, headers, etc...)
412 * Returns : On success, the number of bytes written are returned (zero
413 * indicates nothing was written). On error, -1 is returned,
414 * and errno is set appropriately. If count is zero and the
415 * file descriptor refers to a regular file, 0 will be
416 * returned without causing any other effect. For a special
417 * file, the results are not portable.
419 *********************************************************************/
420 int flush_socket(int fd, struct client_state *csp)
422 struct iob *iob = csp->iob;
423 int n = iob->eod - iob->cur;
430 n = write_socket(fd, iob->cur, n);
431 iob->eod = iob->cur = iob->buf;
437 /*********************************************************************
439 * Function : add_to_iob
441 * Description : Add content to the buffered page.
444 * 1 : csp = Current client state (buffers, headers, etc...)
445 * 2 : buf = holds the content to be added to the page
446 * 3 : n = number of bytes to be added
448 * Returns : Number of bytes in the content buffer.
450 *********************************************************************/
451 int add_to_iob(struct client_state *csp, char *buf, int n)
453 struct iob *iob = csp->iob;
457 have = iob->eod - iob->cur;
466 if ((p = (char *)malloc(need + 1)) == NULL)
468 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
474 /* there is something in the buffer - save it */
475 memcpy(p, iob->cur, have);
477 /* replace the buffer with the new space */
481 /* point to the end of the data */
486 /* the buffer is empty, free it and reinitialize */
491 /* copy the new data into the iob buffer */
494 /* point to the end of the data */
497 /* null terminate == cheap insurance */
500 /* set the pointers to the new values */
509 /*********************************************************************
511 * Function : get_header
513 * Description : This (odd) routine will parse the csp->iob
516 * 1 : csp = Current client state (buffers, headers, etc...)
518 * Returns : Any one of the following:
520 * 1) a pointer to a dynamically allocated string that contains a header line
521 * 2) NULL indicating that the end of the header was reached
522 * 3) "" indicating that the end of the iob was reached before finding
523 * a complete header line.
525 *********************************************************************/
526 char *get_header(struct client_state *csp)
532 if ((iob->cur == NULL)
533 || ((p = strchr(iob->cur, '\n')) == NULL))
535 return(""); /* couldn't find a complete header */
540 ret = strdup(iob->cur);
544 if ((q = strchr(ret, '\r'))) *q = '\0';
546 /* is this a blank linke (i.e. the end of the header) ? */
558 /*********************************************************************
560 * Function : get_header_value
562 * Description : Get the value of a given header from a chained list
563 * of header lines or return NULL if no such header is
564 * present in the list.
567 * 1 : header_list = pointer to list
568 * 2 : header_name = string with name of header to look for.
569 * Trailing colon required, capitalization
572 * Returns : NULL if not found, else value of header
574 *********************************************************************/
575 char *get_header_value(const struct list *header_list, const char *header_name)
577 struct list_entry *cur_entry;
583 length = strlen(header_name);
585 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
589 if (!strncmpic(cur_entry->str, header_name, length))
592 * Found: return pointer to start of value
594 ret = (char *) (cur_entry->str + length);
595 while (*ret && ijb_isspace(*ret)) ret++;
608 /*********************************************************************
612 * Description : add, delete or modify lines in the HTTP header streams.
613 * On entry, it receives a linked list of headers space
614 * that was allocated dynamically (both the list nodes
615 * and the header contents).
617 * As a side effect it frees the space used by the original
621 * 1 : pats = list of patterns to match against headers
622 * 2 : more_headers = list of functions to add more
623 * headers (client or server)
624 * 3 : csp = Current client state (buffers, headers, etc...)
626 * Returns : Single pointer to a fully formed header, or NULL
627 * on out-of-memory error.
629 *********************************************************************/
630 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
632 struct list_entry *p;
633 const struct parsers *v;
637 for (v = pats; v->str ; v++)
639 for (p = csp->headers->first; p ; p = p->next)
641 /* Header crunch()ed in previous run? -> ignore */
642 if (p->str == NULL) continue;
644 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
646 if (strncmpic(p->str, v->str, v->len) == 0)
648 hdr = v->parser(v, p->str, csp);
649 freez(p->str); /* FIXME: Yuck! patching a list...*/
655 /* place any additional headers on the csp->headers list */
656 for (f = more_headers; *f ; f++)
661 hdr = list_to_text(csp->headers);
668 /*********************************************************************
670 * Function : free_http_request
672 * Description : Freez a http_request structure
675 * 1 : http = points to a http_request structure to free
679 *********************************************************************/
680 void free_http_request(struct http_request *http)
688 freez(http->hostport);
691 freez(http->host_ip_addr_str);
696 /*********************************************************************
698 * Function : parse_http_request
700 * Description : Parse out the host and port from the URL. Find the
701 * hostname & path, port (if ':'), and/or password (if '@')
704 * 1 : req = URL (or is it URI?) to break down
705 * 2 : http = pointer to the http structure to hold elements
706 * 3 : csp = Current client state (buffers, headers, etc...)
710 *********************************************************************/
711 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
713 char *buf, *v[10], *url, *p;
716 memset(http, '\0', sizeof(*http));
717 http->cmd = strdup(req);
720 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
724 /* this could be a CONNECT request */
725 if (strcmpic(v[0], "connect") == 0)
728 http->gpc = strdup(v[0]);
729 http->hostport = strdup(v[1]);
730 http->ver = strdup(v[2]);
733 /* or it could be any other basic HTTP request type */
734 if ((0 == strcmpic(v[0], "get"))
735 || (0 == strcmpic(v[0], "head"))
736 || (0 == strcmpic(v[0], "post"))
737 || (0 == strcmpic(v[0], "put"))
738 || (0 == strcmpic(v[0], "delete"))
740 /* or a webDAV extension (RFC2518) */
741 || (0 == strcmpic(v[0], "propfind"))
742 || (0 == strcmpic(v[0], "proppatch"))
743 || (0 == strcmpic(v[0], "move"))
744 || (0 == strcmpic(v[0], "copy"))
745 || (0 == strcmpic(v[0], "mkcol"))
746 || (0 == strcmpic(v[0], "lock"))
747 || (0 == strcmpic(v[0], "unlock"))
751 http->gpc = strdup(v[0]);
752 http->url = strdup(v[1]);
753 http->ver = strdup(v[2]);
756 if (strncmpic(url, "http://", 7) == 0)
760 else if (strncmpic(url, "https://", 8) == 0)
771 if ((p = strchr(url, '/')))
773 http->path = strdup(p);
775 http->hostport = strdup(url);
778 * Repair broken HTTP requests that don't contain a path
782 http->path = strdup("/");
783 http->hostport = strdup(url);
792 if (http->hostport == NULL)
794 free_http_request(http);
798 buf = strdup(http->hostport);
801 /* check if url contains password */
802 n = ssplit(buf, "@", v, SZ(v), 1, 1);
805 char * newbuf = NULL;
806 newbuf = strdup(v[1]);
811 n = ssplit(buf, ":", v, SZ(v), 1, 1);
815 http->host = strdup(v[0]);
821 http->host = strdup(v[0]);
822 http->port = atoi(v[1]);
827 if (http->host == NULL)
829 free_http_request(http);
832 if (http->path == NULL)
834 http->path = strdup("/");
840 /* here begins the family of parser functions that reformat header lines */
843 /*********************************************************************
847 * Description : This is called if a header matches a pattern to "crunch"
850 * 1 : v = Pointer to parsers structure, which basically holds
851 * headers (client or server) that we want to "crunch"
852 * 2 : s = header (from sed) to "crunch"
853 * 3 : csp = Current client state (buffers, headers, etc...)
855 * Returns : Always NULL.
857 *********************************************************************/
858 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
860 log_error(LOG_LEVEL_HEADER, "crunch!");
866 /*********************************************************************
868 * Function : server_content_type
870 * Description : Set the content-type for filterable types (text/.*,
871 * javascript and image/gif) unless filtering has been
872 * forbidden (CT_TABOO) while parsing earlier headers.
876 * 2 : s = header string we are "considering"
877 * 3 : csp = Current client state (buffers, headers, etc...)
879 * Returns : A duplicate string pointer to this header (ie. pass thru)
881 *********************************************************************/
882 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
884 if (csp->content_type != CT_TABOO)
886 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
887 csp->content_type = CT_TEXT;
888 else if (strstr(s, " image/gif"))
889 csp->content_type = CT_GIF;
891 csp->content_type = 0;
899 /*********************************************************************
901 * Function : server_transfer_coding
903 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
904 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
905 * - Change from "chunked" to "identity" if body was chunked
906 * but has been de-chunked for filtering.
910 * 2 : s = header string we are "considering"
911 * 3 : csp = Current client state (buffers, headers, etc...)
913 * Returns : A duplicate string pointer to this header (ie. pass thru)
915 *********************************************************************/
916 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
919 * Turn off pcrs and gif filtering if body compressed
921 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
923 csp->content_type = CT_TABOO;
927 * Raise flag if body chunked
929 if (strstr(s, "chunked"))
931 csp->flags |= CSP_FLAG_CHUNKED;
934 * If the body was modified, it has been
935 * de-chunked first, so adjust the header:
937 if (csp->flags & CSP_FLAG_MODIFIED)
939 return(strdup("Transfer-Encoding: identity"));
948 /*********************************************************************
950 * Function : server_content_encoding
952 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
956 * 2 : s = header string we are "considering"
957 * 3 : csp = Current client state (buffers, headers, etc...)
959 * Returns : A duplicate string pointer to this header (ie. pass thru)
961 *********************************************************************/
962 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
965 * Turn off pcrs and gif filtering if body compressed
967 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
969 csp->content_type = CT_TABOO;
977 /*********************************************************************
979 * Function : server_content_length
981 * Description : Adjust Content-Length header if we modified
986 * 2 : s = header string we are "considering"
987 * 3 : csp = Current client state (buffers, headers, etc...)
989 * Returns : A duplicate string pointer to this header (ie. pass thru)
991 *********************************************************************/
992 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
994 if (csp->content_length != 0) /* Content length has been modified */
996 char * s2 = (char *) zalloc(100);
997 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
999 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
1010 /*********************************************************************
1012 * Function : server_content_md5
1014 * Description : Crumble any Content-MD5 headers if the document was
1015 * modified. FIXME: Should we re-compute instead?
1019 * 2 : s = header string we are "considering"
1020 * 3 : csp = Current client state (buffers, headers, etc...)
1022 * Returns : A duplicate string pointer to this header (ie. pass thru)
1024 *********************************************************************/
1025 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
1027 if (csp->flags & CSP_FLAG_MODIFIED)
1029 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
1040 /*********************************************************************
1042 * Function : client_accept_encoding
1044 * Description : Rewrite the client's Accept-Encoding header so that
1045 * if doesn't allow compression, if the action applies.
1046 * Note: For HTTP/1.0 the absence of the header is enough.
1050 * 2 : s = header string we are "considering"
1051 * 3 : csp = Current client state (buffers, headers, etc...)
1053 * Returns : A copy of the client's original or the modified header.
1055 *********************************************************************/
1056 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
1058 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1064 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1066 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1068 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1079 /*********************************************************************
1081 * Function : client_te
1083 * Description : Rewrite the client's TE header so that
1084 * if doesn't allow compression, if the action applies.
1088 * 2 : s = header string we are "considering"
1089 * 3 : csp = Current client state (buffers, headers, etc...)
1091 * Returns : A copy of the client's original or the modified header.
1093 *********************************************************************/
1094 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1096 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1102 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1108 /*********************************************************************
1110 * Function : client_referrer
1112 * Description : Handle the "referer" config setting properly.
1113 * Called from `sed'.
1117 * 2 : s = header (from sed) to "crunch"
1118 * 3 : csp = Current client state (buffers, headers, etc...)
1120 * Returns : NULL if crunched, or a malloc'ed string with the original
1121 * or modified header
1123 *********************************************************************/
1124 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1126 const char * newval;
1128 #ifdef FEATURE_FORCE_LOAD
1129 /* Since the referrer can include the prefix even
1130 * even if the request itself is non-forced, we must
1131 * clean it unconditionally
1133 strclean(s, FORCE_PREFIX);
1134 #endif /* def FEATURE_FORCE_LOAD */
1137 * Are we sending referer?
1139 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1144 newval = csp->action->string[ACTION_STRING_REFERER];
1147 * Are we blocking referer?
1149 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1151 log_error(LOG_LEVEL_HEADER, "crunch!");
1156 * Are we forging referer?
1158 if (0 == strcmpic(newval, "forge"))
1161 * Forge a referer as http://[hostname:port of REQUEST]/
1162 * to fool stupid checks for in-site links
1164 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1165 s2 = strsav(NULL, "Referer: ");
1166 s2 = strsav(s2, "http://");
1167 s2 = strsav(s2, csp->http->hostport);
1168 s2 = strsav(s2, "/");
1173 * Have we got a fixed referer?
1175 if (0 == strncmpic(newval, "http://", 7))
1178 * We have a specific (fixed) referer we want to send.
1182 log_error(LOG_LEVEL_HEADER, "modified");
1184 s3 = strsav( NULL, "Referer: " );
1185 s3 = strsav( s3, newval );
1189 /* Should never get here! */
1190 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1193 * Forge is probably the best default.
1195 * Forge a referer as http://[hostname:port of REQUEST]/
1196 * to fool stupid checks for in-site links
1198 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1199 s2 = strsav(NULL, "Referer: ");
1200 s2 = strsav(s2, "http://");
1201 s2 = strsav(s2, csp->http->hostport);
1202 s2 = strsav(s2, "/");
1207 /*********************************************************************
1209 * Function : client_uagent
1211 * Description : Handle the "user-agent" config setting properly
1212 * and remember its original value to enable browser
1213 * bug workarounds. Called from `sed'.
1217 * 2 : s = header (from sed) to "crunch"
1218 * 3 : csp = Current client state (buffers, headers, etc...)
1220 * Returns : A malloc'ed pointer to the default agent, or
1221 * a malloc'ed string pointer to this header (ie. pass thru).
1223 *********************************************************************/
1224 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1226 const char * newval;
1229 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1234 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1240 log_error(LOG_LEVEL_HEADER, "modified");
1242 s2 = strsav( NULL, "User-Agent: " );
1243 s2 = strsav( s2, newval );
1249 /*********************************************************************
1251 * Function : client_ua
1253 * Description : Handle "ua-" headers properly. Called from `sed'.
1257 * 2 : s = header (from sed) to "crunch"
1258 * 3 : csp = Current client state (buffers, headers, etc...)
1260 * Returns : NULL if crunched, or a malloc'ed string to original header
1262 *********************************************************************/
1263 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1265 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1271 log_error(LOG_LEVEL_HEADER, "crunch!");
1277 /*********************************************************************
1279 * Function : client_from
1281 * Description : Handle the "from" config setting properly.
1282 * Called from `sed'.
1286 * 2 : s = header (from sed) to "crunch"
1287 * 3 : csp = Current client state (buffers, headers, etc...)
1289 * Returns : NULL if crunched, or a malloc'ed string to
1290 * modified/original header.
1292 *********************************************************************/
1293 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1295 const char * newval;
1298 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1303 newval = csp->action->string[ACTION_STRING_FROM];
1306 * Are we blocking referer?
1308 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1310 log_error(LOG_LEVEL_HEADER, "crunch!");
1314 log_error(LOG_LEVEL_HEADER, " modified");
1316 s2 = strsav( NULL, "From: " );
1317 s2 = strsav( s2, newval );
1323 /*********************************************************************
1325 * Function : client_send_cookie
1327 * Description : Handle the "cookie" header properly. Called from `sed'.
1328 * If cookie is accepted, add it to the cookie_list,
1329 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1332 * 1 : v = pattern of cookie `sed' found matching
1333 * 2 : s = header (from sed) to "crunch"
1334 * 3 : csp = Current client state (buffers, headers, etc...)
1336 * Returns : Always NULL.
1338 *********************************************************************/
1339 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1341 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1343 enlist(csp->cookie_list, s + v->len + 1);
1347 log_error(LOG_LEVEL_HEADER, " crunch!");
1351 * Always return NULL here. The cookie header
1352 * will be sent at the end of the header.
1359 /*********************************************************************
1361 * Function : client_x_forwarded
1363 * Description : Handle the "x-forwarded-for" config setting properly,
1364 * also used in the add_client_headers list. Called from `sed'.
1368 * 2 : s = header (from sed) to "crunch"
1369 * 3 : csp = Current client state (buffers, headers, etc...)
1371 * Returns : Always NULL.
1373 *********************************************************************/
1374 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1376 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1378 /* Save it so we can re-add it later */
1379 csp->x_forwarded = strdup(s);
1383 * Always return NULL, since this information
1384 * will be sent at the end of the header.
1391 /* the following functions add headers directly to the header list */
1393 /*********************************************************************
1395 * Function : client_host_adder
1397 * Description : (re)adds the host header. Called from `sed'.
1400 * 1 : csp = Current client state (buffers, headers, etc...)
1404 *********************************************************************/
1405 void client_host_adder(struct client_state *csp)
1409 p = strsav(p, "Host: ");
1410 p = strsav(p, csp->http->hostport);
1412 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1413 enlist(csp->headers, p);
1419 /*********************************************************************
1421 * Function : client_cookie_adder
1423 * Description : Used in the add_client_headers list. Called from `sed'.
1426 * 1 : csp = Current client state (buffers, headers, etc...)
1430 *********************************************************************/
1431 void client_cookie_adder(struct client_state *csp)
1433 struct list_entry *lst;
1437 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1441 tmp = strsav(tmp, "; ");
1443 tmp = strsav(tmp, lst->str);
1446 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1450 tmp = strsav(tmp, "; ");
1453 if ((e = cookie_encode(lst->str)))
1455 tmp = strsav(tmp, e);
1464 ret = strdup("Cookie: ");
1465 ret = strsav(ret, tmp);
1466 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1467 enlist(csp->headers, ret);
1475 /*********************************************************************
1477 * Function : client_accept_encoding_adder
1479 * Description : Add an Accept-Encoding header to the client's request
1480 * that disables compression if the action applies, and
1481 * the header is not already there. Called from `sed'.
1482 * Note: For HTTP/1.0, the absence of the header is enough.
1485 * 1 : csp = Current client state (buffers, headers, etc...)
1489 *********************************************************************/
1490 void client_accept_encoding_adder(struct client_state *csp)
1492 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1493 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1495 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1501 /*********************************************************************
1503 * Function : client_xtra_adder
1505 * Description : Used in the add_client_headers list. Called from `sed'.
1508 * 1 : csp = Current client state (buffers, headers, etc...)
1512 *********************************************************************/
1513 void client_xtra_adder(struct client_state *csp)
1515 struct list_entry *lst;
1517 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1518 lst ; lst = lst->next)
1520 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1521 enlist(csp->headers, lst->str);
1527 /*********************************************************************
1529 * Function : client_x_forwarded_adder
1531 * Description : Used in the add_client_headers list. Called from `sed'.
1534 * 1 : csp = Current client state (buffers, headers, etc...)
1538 *********************************************************************/
1539 void client_x_forwarded_adder(struct client_state *csp)
1543 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1548 if (csp->x_forwarded)
1550 p = strsav(p, csp->x_forwarded);
1551 p = strsav(p, ", ");
1552 p = strsav(p, csp->ip_addr_str);
1556 p = strsav(p, "X-Forwarded-For: ");
1557 p = strsav(p, csp->ip_addr_str);
1560 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1561 enlist(csp->headers, p);
1567 /*********************************************************************
1569 * Function : connection_close_adder
1571 * Description : Adds a "Connection: close" header to csp->headers
1572 * as a temporary fix for the needed but missing HTTP/1.1
1573 * support. Called from `sed'.
1574 * FIXME: This whole function shouldn't be neccessary!
1577 * 1 : csp = Current client state (buffers, headers, etc...)
1581 *********************************************************************/
1582 void connection_close_adder(struct client_state *csp)
1584 enlist(csp->headers, "Connection: close");
1588 /*********************************************************************
1590 * Function : server_http
1592 * Description : - Save the HTTP Status into csp->http->status
1593 * - Set CT_TABOO to prevent filtering if the answer
1594 * is a partial range (HTTP status 206)
1595 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1599 * 1 : v = parser pattern that matched this header
1600 * 2 : s = header that matched this pattern
1601 * 3 : csp = Current client state (buffers, headers, etc...)
1603 * Returns : Copy of changed or original answer.
1605 *********************************************************************/
1606 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1608 char *ret = strdup(s);
1610 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1611 if (csp->http->status == 206)
1613 csp->content_type = CT_TABOO;
1616 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1619 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1626 /*********************************************************************
1628 * Function : server_set_cookie
1630 * Description : Handle the server "cookie" header properly.
1631 * Log cookie to the jar file. Then "crunch" it,
1632 * or accept it. Called from `sed'.
1635 * 1 : v = parser pattern that matched this header
1636 * 2 : s = header that matched this pattern
1637 * 3 : csp = Current client state (buffers, headers, etc...)
1639 * Returns : `crumble' or a newly malloc'ed string.
1641 *********************************************************************/
1642 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1644 #ifdef FEATURE_COOKIE_JAR
1645 if (csp->config->jar)
1647 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1649 #endif /* def FEATURE_COOKIE_JAR */
1651 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1653 return(crumble(v, s, csp));
1661 #ifdef FEATURE_FORCE_LOAD
1662 /*********************************************************************
1664 * Function : strclean
1666 * Description : In-Situ-Eliminate all occurances of substring in
1670 * 1 : string = string to clean
1671 * 2 : substring = substring to eliminate
1673 * Returns : Number of eliminations
1675 *********************************************************************/
1676 int strclean(const char *string, const char *substring)
1678 int hits = 0, len = strlen(substring);
1681 while((pos = strstr(string, substring)))
1688 while (*p++ != '\0');
1695 #endif /* def FEATURE_FORCE_LOAD */