1 const char parsers_rcs[] = "$Id: parsers.c,v 1.28 2001/09/22 16:32:28 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `add_to_iob', `client_cookie_adder', `client_from',
9 * `client_referrer', `client_send_cookie', `client_ua',
10 * `client_uagent', `client_x_forwarded',
11 * `client_x_forwarded_adder', `client_xtra_adder',
12 * `content_type', `crumble', `destroy_list', `enlist',
13 * `flush_socket', `free_http_request', `get_header',
14 * `list_to_text', `parse_http_request', `sed',
15 * and `server_set_cookie'.
17 * Copyright : Written by and Copyright (C) 2001 the SourceForge
18 * IJBSWA team. http://ijbswa.sourceforge.net
20 * Based on the Internet Junkbuster originally written
21 * by and Copyright (C) 1997 Anonymous Coders and
22 * Junkbusters Corporation. http://www.junkbusters.com
24 * This program is free software; you can redistribute it
25 * and/or modify it under the terms of the GNU General
26 * Public License as published by the Free Software
27 * Foundation; either version 2 of the License, or (at
28 * your option) any later version.
30 * This program is distributed in the hope that it will
31 * be useful, but WITHOUT ANY WARRANTY; without even the
32 * implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU General Public
34 * License for more details.
36 * The GNU General Public License should be included with
37 * this file. If not, you can view it at
38 * http://www.gnu.org/copyleft/gpl.html
39 * or write to the Free Software Foundation, Inc., 59
40 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 * Revision 1.28 2001/09/22 16:32:28 jongfoster
45 * Removing unused #includes.
47 * Revision 1.27 2001/09/20 15:45:25 steudten
49 * add casting from size_t to int for printf()
50 * remove local variable shadow s2
52 * Revision 1.26 2001/09/16 17:05:14 jongfoster
53 * Removing unused #include showarg.h
55 * Revision 1.25 2001/09/16 13:21:27 jongfoster
56 * Changes to use new list functions.
58 * Revision 1.24 2001/09/13 23:05:50 jongfoster
59 * Changing the string paramater to the header parsers a "const".
61 * Revision 1.23 2001/09/12 18:08:19 steudten
63 * In parse_http_request() header rewriting miss the host value, so
64 * from http://www.mydomain.com the result was just " / " not
65 * http://www.mydomain.com/ in case we forward.
67 * Revision 1.22 2001/09/10 10:58:53 oes
68 * Silenced compiler warnings
70 * Revision 1.21 2001/07/31 14:46:00 oes
71 * - Persistant connections now suppressed
72 * - sed() no longer appends empty header to csp->headers
74 * Revision 1.20 2001/07/30 22:08:36 jongfoster
75 * Tidying up #defines:
76 * - All feature #defines are now of the form FEATURE_xxx
77 * - Permanently turned off WIN_GUI_EDIT
78 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
80 * Revision 1.19 2001/07/25 17:21:54 oes
81 * client_uagent now saves copy of User-Agent: header value
83 * Revision 1.18 2001/07/13 14:02:46 oes
84 * - Included fix to repair broken HTTP requests that
85 * don't contain a path, not even '/'.
86 * - Removed all #ifdef PCRS
87 * - content_type now always inspected and classified as
89 * - formatting / comments
91 * Revision 1.17 2001/06/29 21:45:41 oes
92 * Indentation, CRLF->LF, Tab-> Space
94 * Revision 1.16 2001/06/29 13:32:42 oes
96 * - Adapted free_http_request
97 * - Removed logentry from cancelled commit
99 * Revision 1.15 2001/06/03 19:12:38 oes
100 * deleted const struct interceptors
102 * Revision 1.14 2001/06/01 18:49:17 jongfoster
103 * Replaced "list_share" with "list" - the tiny memory gain was not
104 * worth the extra complexity.
106 * Revision 1.13 2001/05/31 21:30:33 jongfoster
107 * Removed list code - it's now in list.[ch]
108 * Renamed "permission" to "action", and changed many features
109 * to use the actions file rather than the global config.
111 * Revision 1.12 2001/05/31 17:33:13 oes
115 * Revision 1.11 2001/05/29 20:11:19 joergs
116 * '/ * inside comment' warning removed.
118 * Revision 1.10 2001/05/29 09:50:24 jongfoster
119 * Unified blocklist/imagelist/permissionslist.
120 * File format is still under discussion, but the internal changes
123 * Also modified interceptor behaviour:
124 * - We now intercept all URLs beginning with one of the following
125 * prefixes (and *only* these prefixes):
127 * * http://ijbswa.sf.net/config/
128 * * http://ijbswa.sourceforge.net/config/
129 * - New interceptors "home page" - go to http://i.j.b/ to see it.
130 * - Internal changes so that intercepted and fast redirect pages
131 * are not replaced with an image.
132 * - Interceptors now have the option to send a binary page direct
133 * to the client. (i.e. ijb-send-banner uses this)
134 * - Implemented show-url-info interceptor. (Which is why I needed
135 * the above interceptors changes - a typical URL is
136 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
137 * The previous mechanism would not have intercepted that, and
138 * if it had been intercepted then it then it would have replaced
141 * Revision 1.9 2001/05/28 17:26:33 jongfoster
142 * Fixing segfault if last header was crunched.
143 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
144 * can use the cross-platform sprintf() instead.)
146 * Revision 1.8 2001/05/27 22:17:04 oes
148 * - re_process_buffer no longer writes the modified buffer
149 * to the client, which was very ugly. It now returns the
150 * buffer, which it is then written by chat.
152 * - content_length now adjusts the Content-Length: header
153 * for modified documents rather than crunch()ing it.
154 * (Length info in csp->content_length, which is 0 for
155 * unmodified documents)
157 * - For this to work, sed() is called twice when filtering.
159 * Revision 1.7 2001/05/27 13:19:06 oes
160 * Patched Joergs solution for the content-length in.
162 * Revision 1.6 2001/05/26 13:39:32 jongfoster
163 * Only crunches Content-Length header if applying RE filtering.
164 * Without this fix, Microsoft Windows Update wouldn't work.
166 * Revision 1.5 2001/05/26 00:28:36 jongfoster
167 * Automatic reloading of config file.
168 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
169 * Most of the global variables have been moved to a new
170 * struct configuration_spec, accessed through csp->config->globalname
171 * Most of the globals remaining are used by the Win32 GUI.
173 * Revision 1.4 2001/05/22 18:46:04 oes
175 * - Enabled filtering banners by size rather than URL
176 * by adding patterns that replace all standard banner
177 * sizes with the "Junkbuster" gif to the re_filterfile
179 * - Enabled filtering WebBugs by providing a pattern
180 * which kills all 1x1 images
182 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
183 * which is selected by the (nonstandard and therefore
184 * capital) letter 'U' in the option string.
185 * It causes the quantifiers to be ungreedy by default.
186 * Appending a ? turns back to greedy (!).
188 * - Added a new interceptor ijb-send-banner, which
189 * sends back the "Junkbuster" gif. Without imagelist or
190 * MSIE detection support, or if tinygif = 1, or the
191 * URL isn't recognized as an imageurl, a lame HTML
192 * explanation is sent instead.
194 * - Added new feature, which permits blocking remote
195 * script redirects and firing back a local redirect
197 * The feature is conditionally compiled, i.e. it
198 * can be disabled with --disable-fast-redirects,
199 * plus it must be activated by a "fast-redirects"
200 * line in the config file, has its own log level
201 * and of course wants to be displayed by show-proxy-args
202 * Note: Boy, all the #ifdefs in 1001 locations and
203 * all the fumbling with configure.in and acconfig.h
204 * were *way* more work than the feature itself :-(
206 * - Because a generic redirect template was needed for
207 * this, tinygif = 3 now uses the same.
209 * - Moved GIFs, and other static HTTP response templates
214 * - Removed some >400 CRs again (Jon, you really worked
217 * Revision 1.3 2001/05/20 01:21:20 jongfoster
218 * Version 2.9.4 checkin.
219 * - Merged popupfile and cookiefile, and added control over PCRS
220 * filtering, in new "permissionsfile".
221 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
222 * file error you now get a message box (in the Win32 GUI) rather
223 * than the program exiting with no explanation.
224 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
226 * - Removed tabs from "config"
227 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
228 * - Bumped up version number.
230 * Revision 1.2 2001/05/17 23:02:36 oes
231 * - Made referrer option accept 'L' as a substitute for '§'
233 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
234 * Initial import of version 2.9.3 source tree
237 *********************************************************************/
244 #include <sys/types.h>
261 #include "jbsockets.h"
262 #include "miscutil.h"
264 const char parsers_h_rcs[] = PARSERS_H_VERSION;
266 /* Fix a problem with Solaris. There should be no effect on other
268 * Solaris's isspace() is a macro which uses it's argument directly
269 * as an array index. Therefore we need to make sure that high-bit
270 * characters generate +ve values, and ideally we also want to make
271 * the argument match the declared parameter type of "int".
273 * Why did they write a character function that can't take a simple
274 * "char" argument? Doh!
276 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
277 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
280 const struct parsers client_patterns[] = {
281 { "referer:", 8, client_referrer },
282 { "user-agent:", 11, client_uagent },
283 { "ua-", 3, client_ua },
284 { "from:", 5, client_from },
285 { "cookie:", 7, client_send_cookie },
286 { "x-forwarded-for:", 16, client_x_forwarded },
287 #ifdef FEATURE_DENY_GZIP
288 { "Accept-Encoding: gzip", 21, crumble },
289 #endif /* def FEATURE_DENY_GZIP */
290 #if defined(FEATURE_IMAGE_DETECT_MSIE)
291 { "Accept:", 7, client_accept },
292 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
293 #ifdef FEATURE_FORCE_LOAD
294 { "Host:", 5, client_host },
295 #endif /* def FEATURE_FORCE_LOAD */
296 /* { "if-modified-since:", 18, crumble }, */
297 { "Keep-Alive:", 11, crumble },
298 { "connection:", 11, crumble },
299 { "proxy-connection:", 17, crumble },
304 const struct parsers server_patterns[] = {
305 { "set-cookie:", 11, server_set_cookie },
306 { "connection:", 11, crumble },
307 { "Content-Type:", 13, content_type },
308 { "Content-Length:", 15, content_length },
313 void (* const add_client_headers[])(struct client_state *) = {
315 client_x_forwarded_adder,
317 connection_close_adder,
322 void (* const add_server_headers[])(struct client_state *) = {
323 connection_close_adder,
328 /*********************************************************************
330 * Function : flush_socket
332 * Description : Write any pending "buffered" content.
335 * 1 : fd = file descriptor of the socket to read
336 * 2 : csp = Current client state (buffers, headers, etc...)
338 * Returns : On success, the number of bytes written are returned (zero
339 * indicates nothing was written). On error, -1 is returned,
340 * and errno is set appropriately. If count is zero and the
341 * file descriptor refers to a regular file, 0 will be
342 * returned without causing any other effect. For a special
343 * file, the results are not portable.
345 *********************************************************************/
346 int flush_socket(int fd, struct client_state *csp)
348 struct iob *iob = csp->iob;
349 int n = iob->eod - iob->cur;
356 n = write_socket(fd, iob->cur, n);
357 iob->eod = iob->cur = iob->buf;
363 /*********************************************************************
365 * Function : add_to_iob
367 * Description : Add content to the buffered page.
370 * 1 : csp = Current client state (buffers, headers, etc...)
371 * 2 : buf = holds the content to be added to the page
372 * 3 : n = number of bytes to be added
374 * Returns : Number of bytes in the content buffer.
376 *********************************************************************/
377 int add_to_iob(struct client_state *csp, char *buf, int n)
379 struct iob *iob = csp->iob;
383 have = iob->eod - iob->cur;
392 if ((p = (char *)malloc(need + 1)) == NULL)
394 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
400 /* there is something in the buffer - save it */
401 memcpy(p, iob->cur, have);
403 /* replace the buffer with the new space */
407 /* point to the end of the data */
412 /* the buffer is empty, free it and reinitialize */
417 /* copy the new data into the iob buffer */
420 /* point to the end of the data */
423 /* null terminate == cheap insurance */
426 /* set the pointers to the new values */
435 /*********************************************************************
437 * Function : get_header
439 * Description : This (odd) routine will parse the csp->iob
442 * 1 : csp = Current client state (buffers, headers, etc...)
444 * Returns : Any one of the following:
446 * 1) a pointer to a dynamically allocated string that contains a header line
447 * 2) NULL indicating that the end of the header was reached
448 * 3) "" indicating that the end of the iob was reached before finding
449 * a complete header line.
451 *********************************************************************/
452 char *get_header(struct client_state *csp)
458 if ((iob->cur == NULL)
459 || ((p = strchr(iob->cur, '\n')) == NULL))
461 return(""); /* couldn't find a complete header */
466 ret = strdup(iob->cur);
470 if ((q = strchr(ret, '\r'))) *q = '\0';
472 /* is this a blank linke (i.e. the end of the header) ? */
484 /*********************************************************************
488 * Description : add, delete or modify lines in the HTTP header streams.
489 * On entry, it receives a linked list of headers space
490 * that was allocated dynamically (both the list nodes
491 * and the header contents).
493 * As a side effect it frees the space used by the original
497 * 1 : pats = list of patterns to match against headers
498 * 2 : more_headers = list of functions to add more
499 * headers (client or server)
500 * 3 : csp = Current client state (buffers, headers, etc...)
502 * Returns : Single pointer to a fully formed header.
504 *********************************************************************/
505 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
507 struct list_entry *p;
508 const struct parsers *v;
512 for (v = pats; v->str ; v++)
514 for (p = csp->headers->first; p ; p = p->next)
516 /* Header crunch()ed in previous run? -> ignore */
517 if (p->str == NULL) continue;
519 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
521 if (strncmpic(p->str, v->str, v->len) == 0)
523 hdr = v->parser(v, p->str, csp);
524 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
530 /* place any additional headers on the csp->headers list */
531 for (f = more_headers; *f ; f++)
536 hdr = list_to_text(csp->headers);
543 /*********************************************************************
545 * Function : free_http_request
547 * Description : Freez a http_request structure
550 * 1 : http = points to a http_request structure to free
554 *********************************************************************/
555 void free_http_request(struct http_request *http)
560 freez(http->hostport);
563 freez(http->host_ip_addr_str);
564 freez(http->user_agent);
569 /*********************************************************************
571 * Function : parse_http_request
573 * Description : Parse out the host and port from the URL. Find the
574 * hostname & path, port (if ':'), and/or password (if '@')
577 * 1 : req = URL (or is it URI?) to break down
578 * 2 : http = pointer to the http structure to hold elements
579 * 3 : csp = Current client state (buffers, headers, etc...)
583 *********************************************************************/
584 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
586 char *buf, *v[10], *url, *p, *save_url;
589 memset(http, '\0', sizeof(*http));
591 http->cmd = strdup(req);
594 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
598 /* this could be a CONNECT request */
599 if (strcmpic(v[0], "connect") == 0)
602 http->gpc = strdup(v[0]);
603 http->hostport = strdup(v[1]);
604 http->ver = strdup(v[2]);
607 /* or it could be a GET or a POST (possibly webDAV too) */
608 if ((0 == strcmpic(v[0], "get"))
609 || (0 == strcmpic(v[0], "head"))
610 || (0 == strcmpic(v[0], "post"))
611 /* These are the headers as defined in RFC2518 to add webDAV support: */
612 || (0 == strcmpic(v[0], "propfind"))
613 || (0 == strcmpic(v[0], "proppatch"))
614 || (0 == strcmpic(v[0], "move"))
615 || (0 == strcmpic(v[0], "copy"))
616 || (0 == strcmpic(v[0], "mkcol"))
617 || (0 == strcmpic(v[0], "lock"))
618 || (0 == strcmpic(v[0], "unlock"))
622 http->gpc = strdup(v[0]);
624 http->ver = strdup(v[2]);
627 if (strncmpic(url, "http://", 7) == 0)
631 else if (strncmpic(url, "https://", 8) == 0)
642 if ((p = strchr(url, '/')))
644 http->path = strdup(p);
646 http->hostport = strdup(url);
649 * Repair broken HTTP requests that don't contain a path
653 /* Repair hostport & path */
654 http->path = strdup("/");
655 http->hostport = strdup(url);
657 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
659 http->cmd = strsav(http->cmd, http->gpc);
660 http->cmd = strsav(http->cmd, " ");
661 http->cmd = strsav(http->cmd, save_url);
662 http->cmd = strsav(http->cmd, "/ ");
663 http->cmd = strsav(http->cmd, http->ver);
672 if (http->hostport == NULL)
674 free_http_request(http);
678 buf = strdup(http->hostport);
681 /* check if url contains password */
682 n = ssplit(buf, "@", v, SZ(v), 1, 1);
685 char * newbuf = NULL;
686 newbuf = strdup(v[1]);
691 n = ssplit(buf, ":", v, SZ(v), 1, 1);
695 http->host = strdup(v[0]);
701 http->host = strdup(v[0]);
702 http->port = atoi(v[1]);
707 if (http->host == NULL)
709 free_http_request(http);
712 if (http->path == NULL)
714 http->path = strdup("");
720 /* here begins the family of parser functions that reformat header lines */
723 /*********************************************************************
727 * Description : This is called if a header matches a pattern to "crunch"
730 * 1 : v = Pointer to parsers structure, which basically holds
731 * headers (client or server) that we want to "crunch"
732 * 2 : s = header (from sed) to "crunch"
733 * 3 : csp = Current client state (buffers, headers, etc...)
735 * Returns : Always NULL.
737 *********************************************************************/
738 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
740 log_error(LOG_LEVEL_HEADER, "crunch!");
746 /*********************************************************************
748 * Function : content_type
750 * Description : Is this a text/.* or javascript MIME Type?
754 * 2 : s = header string we are "considering"
755 * 3 : csp = Current client state (buffers, headers, etc...)
757 * Returns : A duplicate string pointer to this header (ie. pass thru)
759 *********************************************************************/
760 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
762 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
763 csp->content_type = CT_TEXT;
764 else if (strstr(s, " image/gif"))
765 csp->content_type = CT_GIF;
767 csp->content_type = 0;
774 /*********************************************************************
776 * Function : content_length
778 * Description : Adjust Content-Length header if we modified
783 * 2 : s = header string we are "considering"
784 * 3 : csp = Current client state (buffers, headers, etc...)
786 * Returns : A duplicate string pointer to this header (ie. pass thru)
788 *********************************************************************/
789 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
791 if (csp->content_length != 0) /* Content has been modified */
793 char * s2 = (char *) zalloc(100);
794 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
796 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
807 /*********************************************************************
809 * Function : client_referrer
811 * Description : Handle the "referer" config setting properly.
816 * 2 : s = header (from sed) to "crunch"
817 * 3 : csp = Current client state (buffers, headers, etc...)
819 * Returns : NULL if crunched, or a malloc'ed string with the original
822 *********************************************************************/
823 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
827 #ifdef FEATURE_FORCE_LOAD
828 /* Since the referrer can include the prefix even
829 * even if the request itself is non-forced, we must
830 * clean it unconditionally
832 strclean(s, FORCE_PREFIX);
833 #endif /* def FEATURE_FORCE_LOAD */
836 csp->referrer = strdup(s);
837 #endif /* def FEATURE_TRUST */
840 * Are we sending referer?
842 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
847 newval = csp->action->string[ACTION_STRING_REFERER];
850 * Are we blocking referer?
852 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
854 log_error(LOG_LEVEL_HEADER, "crunch!");
859 * Are we forging referer?
861 if (0 == strcmpic(newval, "forge"))
864 * Forge a referer as http://[hostname:port of REQUEST]/
865 * to fool stupid checks for in-site links
867 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
868 s2 = strsav(NULL, "Referer: ");
869 s2 = strsav(s2, "http://");
870 s2 = strsav(s2, csp->http->hostport);
871 s2 = strsav(s2, "/");
876 * Have we got a fixed referer?
878 if (0 == strncmpic(newval, "http://", 7))
881 * We have a specific (fixed) referer we want to send.
885 log_error(LOG_LEVEL_HEADER, "modified");
887 s3 = strsav( NULL, "Referer: " );
888 s3 = strsav( s3, newval );
892 /* Should never get here! */
893 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
896 * Forge is probably the best default.
898 * Forge a referer as http://[hostname:port of REQUEST]/
899 * to fool stupid checks for in-site links
901 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
902 s2 = strsav(NULL, "Referer: ");
903 s2 = strsav(s2, "http://");
904 s2 = strsav(s2, csp->http->hostport);
905 s2 = strsav(s2, "/");
910 /*********************************************************************
912 * Function : client_uagent
914 * Description : Handle the "user-agent" config setting properly
915 * and remember its original value to enable browser
916 * bug workarounds. Called from `sed'.
920 * 2 : s = header (from sed) to "crunch"
921 * 3 : csp = Current client state (buffers, headers, etc...)
923 * Returns : A malloc'ed pointer to the default agent, or
924 * a malloc'ed string pointer to this header (ie. pass thru).
926 *********************************************************************/
927 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
932 /* Save the client's User-Agent: value */
935 csp->http->user_agent = strdup(s + 12);
938 #ifdef FEATURE_IMAGE_DETECT_MSIE
939 if (strstr (s, "MSIE "))
941 /* This is Microsoft Internet Explorer.
942 * Enable auto-detect.
944 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
946 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
948 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
953 newval = csp->action->string[ACTION_STRING_USER_AGENT];
959 log_error(LOG_LEVEL_HEADER, "modified");
961 s2 = strsav( NULL, "User-Agent: " );
962 s2 = strsav( s2, newval );
968 /*********************************************************************
970 * Function : client_ua
972 * Description : Handle "ua-" headers properly. Called from `sed'.
976 * 2 : s = header (from sed) to "crunch"
977 * 3 : csp = Current client state (buffers, headers, etc...)
979 * Returns : NULL if crunched, or a malloc'ed string to original header
981 *********************************************************************/
982 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
984 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
990 log_error(LOG_LEVEL_HEADER, "crunch!");
996 /*********************************************************************
998 * Function : client_from
1000 * Description : Handle the "from" config setting properly.
1001 * Called from `sed'.
1005 * 2 : s = header (from sed) to "crunch"
1006 * 3 : csp = Current client state (buffers, headers, etc...)
1008 * Returns : NULL if crunched, or a malloc'ed string to
1009 * modified/original header.
1011 *********************************************************************/
1012 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1014 const char * newval;
1017 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1022 newval = csp->action->string[ACTION_STRING_FROM];
1025 * Are we blocking referer?
1027 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1029 log_error(LOG_LEVEL_HEADER, "crunch!");
1033 log_error(LOG_LEVEL_HEADER, " modified");
1035 s2 = strsav( NULL, "From: " );
1036 s2 = strsav( s2, newval );
1042 /*********************************************************************
1044 * Function : client_send_cookie
1046 * Description : Handle the "cookie" header properly. Called from `sed'.
1047 * If cookie is accepted, add it to the cookie_list,
1048 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1051 * 1 : v = pattern of cookie `sed' found matching
1052 * 2 : s = header (from sed) to "crunch"
1053 * 3 : csp = Current client state (buffers, headers, etc...)
1055 * Returns : Always NULL.
1057 *********************************************************************/
1058 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1060 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1062 enlist(csp->cookie_list, s + v->len + 1);
1066 log_error(LOG_LEVEL_HEADER, " crunch!");
1070 * Always return NULL here. The cookie header
1071 * will be sent at the end of the header.
1078 /*********************************************************************
1080 * Function : client_x_forwarded
1082 * Description : Handle the "x-forwarded-for" config setting properly,
1083 * also used in the add_client_headers list. Called from `sed'.
1087 * 2 : s = header (from sed) to "crunch"
1088 * 3 : csp = Current client state (buffers, headers, etc...)
1090 * Returns : Always NULL.
1092 *********************************************************************/
1093 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1095 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1097 /* Save it so we can re-add it later */
1098 csp->x_forwarded = strdup(s);
1102 * Always return NULL, since this information
1103 * will be sent at the end of the header.
1110 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1111 /*********************************************************************
1113 * Function : client_accept
1115 * Description : Detect whether the client wants HTML or an image.
1116 * Clients do not always make this information available
1117 * in a sane way. Always passes the header through
1118 * the proxy unchanged.
1122 * 2 : s = Header string. Null terminated.
1123 * 3 : csp = Current client state (buffers, headers, etc...)
1125 * Returns : Duplicate of argument s.
1127 *********************************************************************/
1128 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1130 #ifdef FEATURE_IMAGE_DETECT_MSIE
1131 if (strstr (s, "image/gif"))
1133 /* Client will accept HTML. If this seems counterintuitive,
1136 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1140 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1142 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1147 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1151 /* the following functions add headers directly to the header list */
1154 /*********************************************************************
1156 * Function : client_cookie_adder
1158 * Description : Used in the add_client_headers list. Called from `sed'.
1161 * 1 : csp = Current client state (buffers, headers, etc...)
1165 *********************************************************************/
1166 void client_cookie_adder(struct client_state *csp)
1168 struct list_entry *lst;
1172 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1176 tmp = strsav(tmp, "; ");
1178 tmp = strsav(tmp, lst->str);
1181 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1185 tmp = strsav(tmp, "; ");
1188 if ((e = cookie_encode(lst->str)))
1190 tmp = strsav(tmp, e);
1199 ret = strdup("Cookie: ");
1200 ret = strsav(ret, tmp);
1201 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1202 enlist(csp->headers, ret);
1210 /*********************************************************************
1212 * Function : client_xtra_adder
1214 * Description : Used in the add_client_headers list. Called from `sed'.
1217 * 1 : csp = Current client state (buffers, headers, etc...)
1221 *********************************************************************/
1222 void client_xtra_adder(struct client_state *csp)
1224 struct list_entry *lst;
1226 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1227 lst ; lst = lst->next)
1229 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1230 enlist(csp->headers, lst->str);
1236 /*********************************************************************
1238 * Function : client_x_forwarded_adder
1240 * Description : Used in the add_client_headers list. Called from `sed'.
1243 * 1 : csp = Current client state (buffers, headers, etc...)
1247 *********************************************************************/
1248 void client_x_forwarded_adder(struct client_state *csp)
1252 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1257 if (csp->x_forwarded)
1259 p = strsav(p, csp->x_forwarded);
1260 p = strsav(p, ", ");
1261 p = strsav(p, csp->ip_addr_str);
1265 p = strsav(p, "X-Forwarded-For: ");
1266 p = strsav(p, csp->ip_addr_str);
1269 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1270 enlist(csp->headers, p);
1276 /*********************************************************************
1278 * Function : connection_close_adder
1280 * Description : Adds a "Connection: close" header to csp->headers
1281 * as a temporary fix for the needed but missing HTTP/1.1
1282 * support. Called from `sed'.
1283 * FIXME: This whole function shouldn't be neccessary!
1286 * 1 : csp = Current client state (buffers, headers, etc...)
1290 *********************************************************************/
1291 void connection_close_adder(struct client_state *csp)
1293 enlist(csp->headers, "Connection: close");
1297 /*********************************************************************
1299 * Function : server_set_cookie
1301 * Description : Handle the server "cookie" header properly.
1302 * Log cookie to the jar file. Then "crunch" it,
1303 * or accept it. Called from `sed'.
1306 * 1 : v = parser pattern that matched this header
1307 * 2 : s = header that matched this pattern
1308 * 3 : csp = Current client state (buffers, headers, etc...)
1310 * Returns : `crumble' or a newly malloc'ed string.
1312 *********************************************************************/
1313 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1315 #ifdef FEATURE_COOKIE_JAR
1316 if (csp->config->jar)
1318 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1320 #endif /* def FEATURE_COOKIE_JAR */
1322 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1324 return(crumble(v, s, csp));
1332 #ifdef FEATURE_FORCE_LOAD
1333 /*********************************************************************
1335 * Function : client_host
1337 * Description : Clean the FORCE_PREFIX out of the 'host' http
1338 * header, if we use force
1342 * 2 : s = header (from sed) to clean
1343 * 3 : csp = Current client state (buffers, headers, etc...)
1345 * Returns : A malloc'ed pointer to the cleaned host header
1347 *********************************************************************/
1348 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1350 char *cleanhost = strdup(s);
1354 strclean(cleanhost, FORCE_PREFIX);
1359 #endif /* def FEATURE_FORCE_LOAD */
1362 #ifdef FEATURE_FORCE_LOAD
1363 /*********************************************************************
1365 * Function : strclean
1367 * Description : In-Situ-Eliminate all occurances of substring in
1371 * 1 : string = string to clean
1372 * 2 : substring = substring to eliminate
1374 * Returns : Number of eliminations
1376 *********************************************************************/
1377 int strclean(const char *string, const char *substring)
1379 int hits = 0, len = strlen(substring);
1382 while((pos = strstr(string, substring)))
1389 while (*p++ != '\0');
1396 #endif /* def FEATURE_FORCE_LOAD */