1 const char parsers_rcs[] = "$Id: parsers.c,v 1.27 2001/09/20 15:45:25 steudten 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.27 2001/09/20 15:45:25 steudten
46 * add casting from size_t to int for printf()
47 * remove local variable shadow s2
49 * Revision 1.26 2001/09/16 17:05:14 jongfoster
50 * Removing unused #include showarg.h
52 * Revision 1.25 2001/09/16 13:21:27 jongfoster
53 * Changes to use new list functions.
55 * Revision 1.24 2001/09/13 23:05:50 jongfoster
56 * Changing the string paramater to the header parsers a "const".
58 * Revision 1.23 2001/09/12 18:08:19 steudten
60 * In parse_http_request() header rewriting miss the host value, so
61 * from http://www.mydomain.com the result was just " / " not
62 * http://www.mydomain.com/ in case we forward.
64 * Revision 1.22 2001/09/10 10:58:53 oes
65 * Silenced compiler warnings
67 * Revision 1.21 2001/07/31 14:46:00 oes
68 * - Persistant connections now suppressed
69 * - sed() no longer appends empty header to csp->headers
71 * Revision 1.20 2001/07/30 22:08:36 jongfoster
72 * Tidying up #defines:
73 * - All feature #defines are now of the form FEATURE_xxx
74 * - Permanently turned off WIN_GUI_EDIT
75 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
77 * Revision 1.19 2001/07/25 17:21:54 oes
78 * client_uagent now saves copy of User-Agent: header value
80 * Revision 1.18 2001/07/13 14:02:46 oes
81 * - Included fix to repair broken HTTP requests that
82 * don't contain a path, not even '/'.
83 * - Removed all #ifdef PCRS
84 * - content_type now always inspected and classified as
86 * - formatting / comments
88 * Revision 1.17 2001/06/29 21:45:41 oes
89 * Indentation, CRLF->LF, Tab-> Space
91 * Revision 1.16 2001/06/29 13:32:42 oes
93 * - Adapted free_http_request
94 * - Removed logentry from cancelled commit
96 * Revision 1.15 2001/06/03 19:12:38 oes
97 * deleted const struct interceptors
99 * Revision 1.14 2001/06/01 18:49:17 jongfoster
100 * Replaced "list_share" with "list" - the tiny memory gain was not
101 * worth the extra complexity.
103 * Revision 1.13 2001/05/31 21:30:33 jongfoster
104 * Removed list code - it's now in list.[ch]
105 * Renamed "permission" to "action", and changed many features
106 * to use the actions file rather than the global config.
108 * Revision 1.12 2001/05/31 17:33:13 oes
112 * Revision 1.11 2001/05/29 20:11:19 joergs
113 * '/ * inside comment' warning removed.
115 * Revision 1.10 2001/05/29 09:50:24 jongfoster
116 * Unified blocklist/imagelist/permissionslist.
117 * File format is still under discussion, but the internal changes
120 * Also modified interceptor behaviour:
121 * - We now intercept all URLs beginning with one of the following
122 * prefixes (and *only* these prefixes):
124 * * http://ijbswa.sf.net/config/
125 * * http://ijbswa.sourceforge.net/config/
126 * - New interceptors "home page" - go to http://i.j.b/ to see it.
127 * - Internal changes so that intercepted and fast redirect pages
128 * are not replaced with an image.
129 * - Interceptors now have the option to send a binary page direct
130 * to the client. (i.e. ijb-send-banner uses this)
131 * - Implemented show-url-info interceptor. (Which is why I needed
132 * the above interceptors changes - a typical URL is
133 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
134 * The previous mechanism would not have intercepted that, and
135 * if it had been intercepted then it then it would have replaced
138 * Revision 1.9 2001/05/28 17:26:33 jongfoster
139 * Fixing segfault if last header was crunched.
140 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
141 * can use the cross-platform sprintf() instead.)
143 * Revision 1.8 2001/05/27 22:17:04 oes
145 * - re_process_buffer no longer writes the modified buffer
146 * to the client, which was very ugly. It now returns the
147 * buffer, which it is then written by chat.
149 * - content_length now adjusts the Content-Length: header
150 * for modified documents rather than crunch()ing it.
151 * (Length info in csp->content_length, which is 0 for
152 * unmodified documents)
154 * - For this to work, sed() is called twice when filtering.
156 * Revision 1.7 2001/05/27 13:19:06 oes
157 * Patched Joergs solution for the content-length in.
159 * Revision 1.6 2001/05/26 13:39:32 jongfoster
160 * Only crunches Content-Length header if applying RE filtering.
161 * Without this fix, Microsoft Windows Update wouldn't work.
163 * Revision 1.5 2001/05/26 00:28:36 jongfoster
164 * Automatic reloading of config file.
165 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
166 * Most of the global variables have been moved to a new
167 * struct configuration_spec, accessed through csp->config->globalname
168 * Most of the globals remaining are used by the Win32 GUI.
170 * Revision 1.4 2001/05/22 18:46:04 oes
172 * - Enabled filtering banners by size rather than URL
173 * by adding patterns that replace all standard banner
174 * sizes with the "Junkbuster" gif to the re_filterfile
176 * - Enabled filtering WebBugs by providing a pattern
177 * which kills all 1x1 images
179 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
180 * which is selected by the (nonstandard and therefore
181 * capital) letter 'U' in the option string.
182 * It causes the quantifiers to be ungreedy by default.
183 * Appending a ? turns back to greedy (!).
185 * - Added a new interceptor ijb-send-banner, which
186 * sends back the "Junkbuster" gif. Without imagelist or
187 * MSIE detection support, or if tinygif = 1, or the
188 * URL isn't recognized as an imageurl, a lame HTML
189 * explanation is sent instead.
191 * - Added new feature, which permits blocking remote
192 * script redirects and firing back a local redirect
194 * The feature is conditionally compiled, i.e. it
195 * can be disabled with --disable-fast-redirects,
196 * plus it must be activated by a "fast-redirects"
197 * line in the config file, has its own log level
198 * and of course wants to be displayed by show-proxy-args
199 * Note: Boy, all the #ifdefs in 1001 locations and
200 * all the fumbling with configure.in and acconfig.h
201 * were *way* more work than the feature itself :-(
203 * - Because a generic redirect template was needed for
204 * this, tinygif = 3 now uses the same.
206 * - Moved GIFs, and other static HTTP response templates
211 * - Removed some >400 CRs again (Jon, you really worked
214 * Revision 1.3 2001/05/20 01:21:20 jongfoster
215 * Version 2.9.4 checkin.
216 * - Merged popupfile and cookiefile, and added control over PCRS
217 * filtering, in new "permissionsfile".
218 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
219 * file error you now get a message box (in the Win32 GUI) rather
220 * than the program exiting with no explanation.
221 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
223 * - Removed tabs from "config"
224 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
225 * - Bumped up version number.
227 * Revision 1.2 2001/05/17 23:02:36 oes
228 * - Made referrer option accept 'L' as a substitute for 'ยง'
230 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
231 * Initial import of version 2.9.3 source tree
234 *********************************************************************/
241 #include <sys/types.h>
258 #include "jbsockets.h"
259 #include "miscutil.h"
261 const char parsers_h_rcs[] = PARSERS_H_VERSION;
263 /* Fix a problem with Solaris. There should be no effect on other
265 * Solaris's isspace() is a macro which uses it's argument directly
266 * as an array index. Therefore we need to make sure that high-bit
267 * characters generate +ve values, and ideally we also want to make
268 * the argument match the declared parameter type of "int".
270 * Why did they write a character function that can't take a simple
271 * "char" argument? Doh!
273 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
274 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
277 const struct parsers client_patterns[] = {
278 { "referer:", 8, client_referrer },
279 { "user-agent:", 11, client_uagent },
280 { "ua-", 3, client_ua },
281 { "from:", 5, client_from },
282 { "cookie:", 7, client_send_cookie },
283 { "x-forwarded-for:", 16, client_x_forwarded },
284 #ifdef FEATURE_DENY_GZIP
285 { "Accept-Encoding: gzip", 21, crumble },
286 #endif /* def FEATURE_DENY_GZIP */
287 #if defined(FEATURE_IMAGE_DETECT_MSIE)
288 { "Accept:", 7, client_accept },
289 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
290 #ifdef FEATURE_FORCE_LOAD
291 { "Host:", 5, client_host },
292 #endif /* def FEATURE_FORCE_LOAD */
293 /* { "if-modified-since:", 18, crumble }, */
294 { "Keep-Alive:", 11, crumble },
295 { "connection:", 11, crumble },
296 { "proxy-connection:", 17, crumble },
301 const struct parsers server_patterns[] = {
302 { "set-cookie:", 11, server_set_cookie },
303 { "connection:", 11, crumble },
304 { "Content-Type:", 13, content_type },
305 { "Content-Length:", 15, content_length },
310 void (* const add_client_headers[])(struct client_state *) = {
312 client_x_forwarded_adder,
314 connection_close_adder,
319 void (* const add_server_headers[])(struct client_state *) = {
320 connection_close_adder,
325 /*********************************************************************
327 * Function : flush_socket
329 * Description : Write any pending "buffered" content.
332 * 1 : fd = file descriptor of the socket to read
333 * 2 : csp = Current client state (buffers, headers, etc...)
335 * Returns : On success, the number of bytes written are returned (zero
336 * indicates nothing was written). On error, -1 is returned,
337 * and errno is set appropriately. If count is zero and the
338 * file descriptor refers to a regular file, 0 will be
339 * returned without causing any other effect. For a special
340 * file, the results are not portable.
342 *********************************************************************/
343 int flush_socket(int fd, struct client_state *csp)
345 struct iob *iob = csp->iob;
346 int n = iob->eod - iob->cur;
353 n = write_socket(fd, iob->cur, n);
354 iob->eod = iob->cur = iob->buf;
360 /*********************************************************************
362 * Function : add_to_iob
364 * Description : Add content to the buffered page.
367 * 1 : csp = Current client state (buffers, headers, etc...)
368 * 2 : buf = holds the content to be added to the page
369 * 3 : n = number of bytes to be added
371 * Returns : Number of bytes in the content buffer.
373 *********************************************************************/
374 int add_to_iob(struct client_state *csp, char *buf, int n)
376 struct iob *iob = csp->iob;
380 have = iob->eod - iob->cur;
389 if ((p = (char *)malloc(need + 1)) == NULL)
391 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
397 /* there is something in the buffer - save it */
398 memcpy(p, iob->cur, have);
400 /* replace the buffer with the new space */
404 /* point to the end of the data */
409 /* the buffer is empty, free it and reinitialize */
414 /* copy the new data into the iob buffer */
417 /* point to the end of the data */
420 /* null terminate == cheap insurance */
423 /* set the pointers to the new values */
432 /*********************************************************************
434 * Function : get_header
436 * Description : This (odd) routine will parse the csp->iob
439 * 1 : csp = Current client state (buffers, headers, etc...)
441 * Returns : Any one of the following:
443 * 1) a pointer to a dynamically allocated string that contains a header line
444 * 2) NULL indicating that the end of the header was reached
445 * 3) "" indicating that the end of the iob was reached before finding
446 * a complete header line.
448 *********************************************************************/
449 char *get_header(struct client_state *csp)
455 if ((iob->cur == NULL)
456 || ((p = strchr(iob->cur, '\n')) == NULL))
458 return(""); /* couldn't find a complete header */
463 ret = strdup(iob->cur);
467 if ((q = strchr(ret, '\r'))) *q = '\0';
469 /* is this a blank linke (i.e. the end of the header) ? */
481 /*********************************************************************
485 * Description : add, delete or modify lines in the HTTP header streams.
486 * On entry, it receives a linked list of headers space
487 * that was allocated dynamically (both the list nodes
488 * and the header contents).
490 * As a side effect it frees the space used by the original
494 * 1 : pats = list of patterns to match against headers
495 * 2 : more_headers = list of functions to add more
496 * headers (client or server)
497 * 3 : csp = Current client state (buffers, headers, etc...)
499 * Returns : Single pointer to a fully formed header.
501 *********************************************************************/
502 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
504 struct list_entry *p;
505 const struct parsers *v;
509 for (v = pats; v->str ; v++)
511 for (p = csp->headers->first; p ; p = p->next)
513 /* Header crunch()ed in previous run? -> ignore */
514 if (p->str == NULL) continue;
516 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
518 if (strncmpic(p->str, v->str, v->len) == 0)
520 hdr = v->parser(v, p->str, csp);
521 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
527 /* place any additional headers on the csp->headers list */
528 for (f = more_headers; *f ; f++)
533 hdr = list_to_text(csp->headers);
540 /*********************************************************************
542 * Function : free_http_request
544 * Description : Freez a http_request structure
547 * 1 : http = points to a http_request structure to free
551 *********************************************************************/
552 void free_http_request(struct http_request *http)
557 freez(http->hostport);
560 freez(http->host_ip_addr_str);
561 freez(http->user_agent);
566 /*********************************************************************
568 * Function : parse_http_request
570 * Description : Parse out the host and port from the URL. Find the
571 * hostname & path, port (if ':'), and/or password (if '@')
574 * 1 : req = URL (or is it URI?) to break down
575 * 2 : http = pointer to the http structure to hold elements
576 * 3 : csp = Current client state (buffers, headers, etc...)
580 *********************************************************************/
581 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
583 char *buf, *v[10], *url, *p, *save_url;
586 memset(http, '\0', sizeof(*http));
588 http->cmd = strdup(req);
591 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
595 /* this could be a CONNECT request */
596 if (strcmpic(v[0], "connect") == 0)
599 http->gpc = strdup(v[0]);
600 http->hostport = strdup(v[1]);
601 http->ver = strdup(v[2]);
604 /* or it could be a GET or a POST (possibly webDAV too) */
605 if ((0 == strcmpic(v[0], "get"))
606 || (0 == strcmpic(v[0], "head"))
607 || (0 == strcmpic(v[0], "post"))
608 /* These are the headers as defined in RFC2518 to add webDAV support: */
609 || (0 == strcmpic(v[0], "propfind"))
610 || (0 == strcmpic(v[0], "proppatch"))
611 || (0 == strcmpic(v[0], "move"))
612 || (0 == strcmpic(v[0], "copy"))
613 || (0 == strcmpic(v[0], "mkcol"))
614 || (0 == strcmpic(v[0], "lock"))
615 || (0 == strcmpic(v[0], "unlock"))
619 http->gpc = strdup(v[0]);
621 http->ver = strdup(v[2]);
624 if (strncmpic(url, "http://", 7) == 0)
628 else if (strncmpic(url, "https://", 8) == 0)
639 if ((p = strchr(url, '/')))
641 http->path = strdup(p);
643 http->hostport = strdup(url);
646 * Repair broken HTTP requests that don't contain a path
650 /* Repair hostport & path */
651 http->path = strdup("/");
652 http->hostport = strdup(url);
654 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
656 http->cmd = strsav(http->cmd, http->gpc);
657 http->cmd = strsav(http->cmd, " ");
658 http->cmd = strsav(http->cmd, save_url);
659 http->cmd = strsav(http->cmd, "/ ");
660 http->cmd = strsav(http->cmd, http->ver);
669 if (http->hostport == NULL)
671 free_http_request(http);
675 buf = strdup(http->hostport);
678 /* check if url contains password */
679 n = ssplit(buf, "@", v, SZ(v), 1, 1);
682 char * newbuf = NULL;
683 newbuf = strdup(v[1]);
688 n = ssplit(buf, ":", v, SZ(v), 1, 1);
692 http->host = strdup(v[0]);
698 http->host = strdup(v[0]);
699 http->port = atoi(v[1]);
704 if (http->host == NULL)
706 free_http_request(http);
709 if (http->path == NULL)
711 http->path = strdup("");
717 /* here begins the family of parser functions that reformat header lines */
720 /*********************************************************************
724 * Description : This is called if a header matches a pattern to "crunch"
727 * 1 : v = Pointer to parsers structure, which basically holds
728 * headers (client or server) that we want to "crunch"
729 * 2 : s = header (from sed) to "crunch"
730 * 3 : csp = Current client state (buffers, headers, etc...)
732 * Returns : Always NULL.
734 *********************************************************************/
735 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
737 log_error(LOG_LEVEL_HEADER, "crunch!");
743 /*********************************************************************
745 * Function : content_type
747 * Description : Is this a text/.* or javascript MIME Type?
751 * 2 : s = header string we are "considering"
752 * 3 : csp = Current client state (buffers, headers, etc...)
754 * Returns : A duplicate string pointer to this header (ie. pass thru)
756 *********************************************************************/
757 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
759 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
760 csp->content_type = CT_TEXT;
761 else if (strstr(s, " image/gif"))
762 csp->content_type = CT_GIF;
764 csp->content_type = 0;
771 /*********************************************************************
773 * Function : content_length
775 * Description : Adjust Content-Length header if we modified
780 * 2 : s = header string we are "considering"
781 * 3 : csp = Current client state (buffers, headers, etc...)
783 * Returns : A duplicate string pointer to this header (ie. pass thru)
785 *********************************************************************/
786 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
788 if (csp->content_length != 0) /* Content has been modified */
790 char * s2 = (char *) zalloc(100);
791 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
793 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
804 /*********************************************************************
806 * Function : client_referrer
808 * Description : Handle the "referer" config setting properly.
813 * 2 : s = header (from sed) to "crunch"
814 * 3 : csp = Current client state (buffers, headers, etc...)
816 * Returns : NULL if crunched, or a malloc'ed string with the original
819 *********************************************************************/
820 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
824 #ifdef FEATURE_FORCE_LOAD
825 /* Since the referrer can include the prefix even
826 * even if the request itself is non-forced, we must
827 * clean it unconditionally
829 strclean(s, FORCE_PREFIX);
830 #endif /* def FEATURE_FORCE_LOAD */
833 csp->referrer = strdup(s);
834 #endif /* def FEATURE_TRUST */
837 * Are we sending referer?
839 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
844 newval = csp->action->string[ACTION_STRING_REFERER];
847 * Are we blocking referer?
849 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
851 log_error(LOG_LEVEL_HEADER, "crunch!");
856 * Are we forging referer?
858 if (0 == strcmpic(newval, "forge"))
861 * Forge a referer as http://[hostname:port of REQUEST]/
862 * to fool stupid checks for in-site links
864 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
865 s2 = strsav(NULL, "Referer: ");
866 s2 = strsav(s2, "http://");
867 s2 = strsav(s2, csp->http->hostport);
868 s2 = strsav(s2, "/");
873 * Have we got a fixed referer?
875 if (0 == strncmpic(newval, "http://", 7))
878 * We have a specific (fixed) referer we want to send.
882 log_error(LOG_LEVEL_HEADER, "modified");
884 s3 = strsav( NULL, "Referer: " );
885 s3 = strsav( s3, newval );
889 /* Should never get here! */
890 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
893 * Forge is probably the best default.
895 * Forge a referer as http://[hostname:port of REQUEST]/
896 * to fool stupid checks for in-site links
898 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
899 s2 = strsav(NULL, "Referer: ");
900 s2 = strsav(s2, "http://");
901 s2 = strsav(s2, csp->http->hostport);
902 s2 = strsav(s2, "/");
907 /*********************************************************************
909 * Function : client_uagent
911 * Description : Handle the "user-agent" config setting properly
912 * and remember its original value to enable browser
913 * bug workarounds. Called from `sed'.
917 * 2 : s = header (from sed) to "crunch"
918 * 3 : csp = Current client state (buffers, headers, etc...)
920 * Returns : A malloc'ed pointer to the default agent, or
921 * a malloc'ed string pointer to this header (ie. pass thru).
923 *********************************************************************/
924 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
929 /* Save the client's User-Agent: value */
932 csp->http->user_agent = strdup(s + 12);
935 #ifdef FEATURE_IMAGE_DETECT_MSIE
936 if (strstr (s, "MSIE "))
938 /* This is Microsoft Internet Explorer.
939 * Enable auto-detect.
941 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
943 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
945 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
950 newval = csp->action->string[ACTION_STRING_USER_AGENT];
956 log_error(LOG_LEVEL_HEADER, "modified");
958 s2 = strsav( NULL, "User-Agent: " );
959 s2 = strsav( s2, newval );
965 /*********************************************************************
967 * Function : client_ua
969 * Description : Handle "ua-" headers properly. Called from `sed'.
973 * 2 : s = header (from sed) to "crunch"
974 * 3 : csp = Current client state (buffers, headers, etc...)
976 * Returns : NULL if crunched, or a malloc'ed string to original header
978 *********************************************************************/
979 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
981 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
987 log_error(LOG_LEVEL_HEADER, "crunch!");
993 /*********************************************************************
995 * Function : client_from
997 * Description : Handle the "from" config setting properly.
1002 * 2 : s = header (from sed) to "crunch"
1003 * 3 : csp = Current client state (buffers, headers, etc...)
1005 * Returns : NULL if crunched, or a malloc'ed string to
1006 * modified/original header.
1008 *********************************************************************/
1009 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1011 const char * newval;
1014 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1019 newval = csp->action->string[ACTION_STRING_FROM];
1022 * Are we blocking referer?
1024 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1026 log_error(LOG_LEVEL_HEADER, "crunch!");
1030 log_error(LOG_LEVEL_HEADER, " modified");
1032 s2 = strsav( NULL, "From: " );
1033 s2 = strsav( s2, newval );
1039 /*********************************************************************
1041 * Function : client_send_cookie
1043 * Description : Handle the "cookie" header properly. Called from `sed'.
1044 * If cookie is accepted, add it to the cookie_list,
1045 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1048 * 1 : v = pattern of cookie `sed' found matching
1049 * 2 : s = header (from sed) to "crunch"
1050 * 3 : csp = Current client state (buffers, headers, etc...)
1052 * Returns : Always NULL.
1054 *********************************************************************/
1055 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1057 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1059 enlist(csp->cookie_list, s + v->len + 1);
1063 log_error(LOG_LEVEL_HEADER, " crunch!");
1067 * Always return NULL here. The cookie header
1068 * will be sent at the end of the header.
1075 /*********************************************************************
1077 * Function : client_x_forwarded
1079 * Description : Handle the "x-forwarded-for" config setting properly,
1080 * also used in the add_client_headers list. Called from `sed'.
1084 * 2 : s = header (from sed) to "crunch"
1085 * 3 : csp = Current client state (buffers, headers, etc...)
1087 * Returns : Always NULL.
1089 *********************************************************************/
1090 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1092 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1094 /* Save it so we can re-add it later */
1095 csp->x_forwarded = strdup(s);
1099 * Always return NULL, since this information
1100 * will be sent at the end of the header.
1107 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1108 /*********************************************************************
1110 * Function : client_accept
1112 * Description : Detect whether the client wants HTML or an image.
1113 * Clients do not always make this information available
1114 * in a sane way. Always passes the header through
1115 * the proxy unchanged.
1119 * 2 : s = Header string. Null terminated.
1120 * 3 : csp = Current client state (buffers, headers, etc...)
1122 * Returns : Duplicate of argument s.
1124 *********************************************************************/
1125 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1127 #ifdef FEATURE_IMAGE_DETECT_MSIE
1128 if (strstr (s, "image/gif"))
1130 /* Client will accept HTML. If this seems counterintuitive,
1133 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1137 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1139 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1144 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1148 /* the following functions add headers directly to the header list */
1151 /*********************************************************************
1153 * Function : client_cookie_adder
1155 * Description : Used in the add_client_headers list. Called from `sed'.
1158 * 1 : csp = Current client state (buffers, headers, etc...)
1162 *********************************************************************/
1163 void client_cookie_adder(struct client_state *csp)
1165 struct list_entry *lst;
1169 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1173 tmp = strsav(tmp, "; ");
1175 tmp = strsav(tmp, lst->str);
1178 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1182 tmp = strsav(tmp, "; ");
1185 if ((e = cookie_encode(lst->str)))
1187 tmp = strsav(tmp, e);
1196 ret = strdup("Cookie: ");
1197 ret = strsav(ret, tmp);
1198 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1199 enlist(csp->headers, ret);
1207 /*********************************************************************
1209 * Function : client_xtra_adder
1211 * Description : Used in the add_client_headers list. Called from `sed'.
1214 * 1 : csp = Current client state (buffers, headers, etc...)
1218 *********************************************************************/
1219 void client_xtra_adder(struct client_state *csp)
1221 struct list_entry *lst;
1223 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1224 lst ; lst = lst->next)
1226 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1227 enlist(csp->headers, lst->str);
1233 /*********************************************************************
1235 * Function : client_x_forwarded_adder
1237 * Description : Used in the add_client_headers list. Called from `sed'.
1240 * 1 : csp = Current client state (buffers, headers, etc...)
1244 *********************************************************************/
1245 void client_x_forwarded_adder(struct client_state *csp)
1249 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1254 if (csp->x_forwarded)
1256 p = strsav(p, csp->x_forwarded);
1257 p = strsav(p, ", ");
1258 p = strsav(p, csp->ip_addr_str);
1262 p = strsav(p, "X-Forwarded-For: ");
1263 p = strsav(p, csp->ip_addr_str);
1266 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1267 enlist(csp->headers, p);
1272 /*********************************************************************
1274 * Function : connection_close_adder
1276 * Description : Adds a "Connection: close" header to csp->headers
1277 * as a temporary fix for the needed but missing HTTP/1.1
1278 * support. Called from `sed'.
1279 * FIXME: This whole function shouldn't be neccessary!
1282 * 1 : csp = Current client state (buffers, headers, etc...)
1286 *********************************************************************/
1287 void connection_close_adder(struct client_state *csp)
1289 enlist(csp->headers, strdup("Connection: close"));
1294 /*********************************************************************
1296 * Function : server_set_cookie
1298 * Description : Handle the server "cookie" header properly.
1299 * Log cookie to the jar file. Then "crunch" it,
1300 * or accept it. Called from `sed'.
1303 * 1 : v = parser pattern that matched this header
1304 * 2 : s = header that matched this pattern
1305 * 3 : csp = Current client state (buffers, headers, etc...)
1307 * Returns : `crumble' or a newly malloc'ed string.
1309 *********************************************************************/
1310 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1312 #ifdef FEATURE_COOKIE_JAR
1313 if (csp->config->jar)
1315 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1317 #endif /* def FEATURE_COOKIE_JAR */
1319 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1321 return(crumble(v, s, csp));
1329 #ifdef FEATURE_FORCE_LOAD
1330 /*********************************************************************
1332 * Function : client_host
1334 * Description : Clean the FORCE_PREFIX out of the 'host' http
1335 * header, if we use force
1339 * 2 : s = header (from sed) to clean
1340 * 3 : csp = Current client state (buffers, headers, etc...)
1342 * Returns : A malloc'ed pointer to the cleaned host header
1344 *********************************************************************/
1345 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1347 char *cleanhost = strdup(s);
1350 strclean(cleanhost, FORCE_PREFIX);
1354 #endif /* def FEATURE_FORCE_LOAD */
1357 #ifdef FEATURE_FORCE_LOAD
1358 /*********************************************************************
1360 * Function : strclean
1362 * Description : In-Situ-Eliminate all occurances of substring in
1366 * 1 : string = string to clean
1367 * 2 : substring = substring to eliminate
1369 * Returns : Number of eliminations
1371 *********************************************************************/
1372 int strclean(const char *string, const char *substring)
1374 int hits = 0, len = strlen(substring);
1377 while((pos = strstr(string, substring)))
1384 while (*p++ != '\0');
1391 #endif /* def FEATURE_FORCE_LOAD */