1 const char parsers_rcs[] = "$Id: parsers.c,v 1.25 2001/09/16 13:21:27 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.25 2001/09/16 13:21:27 jongfoster
45 * Changes to use new list functions.
47 * Revision 1.24 2001/09/13 23:05:50 jongfoster
48 * Changing the string paramater to the header parsers a "const".
50 * Revision 1.23 2001/09/12 18:08:19 steudten
52 * In parse_http_request() header rewriting miss the host value, so
53 * from http://www.mydomain.com the result was just " / " not
54 * http://www.mydomain.com/ in case we forward.
56 * Revision 1.22 2001/09/10 10:58:53 oes
57 * Silenced compiler warnings
59 * Revision 1.21 2001/07/31 14:46:00 oes
60 * - Persistant connections now suppressed
61 * - sed() no longer appends empty header to csp->headers
63 * Revision 1.20 2001/07/30 22:08:36 jongfoster
64 * Tidying up #defines:
65 * - All feature #defines are now of the form FEATURE_xxx
66 * - Permanently turned off WIN_GUI_EDIT
67 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
69 * Revision 1.19 2001/07/25 17:21:54 oes
70 * client_uagent now saves copy of User-Agent: header value
72 * Revision 1.18 2001/07/13 14:02:46 oes
73 * - Included fix to repair broken HTTP requests that
74 * don't contain a path, not even '/'.
75 * - Removed all #ifdef PCRS
76 * - content_type now always inspected and classified as
78 * - formatting / comments
80 * Revision 1.17 2001/06/29 21:45:41 oes
81 * Indentation, CRLF->LF, Tab-> Space
83 * Revision 1.16 2001/06/29 13:32:42 oes
85 * - Adapted free_http_request
86 * - Removed logentry from cancelled commit
88 * Revision 1.15 2001/06/03 19:12:38 oes
89 * deleted const struct interceptors
91 * Revision 1.14 2001/06/01 18:49:17 jongfoster
92 * Replaced "list_share" with "list" - the tiny memory gain was not
93 * worth the extra complexity.
95 * Revision 1.13 2001/05/31 21:30:33 jongfoster
96 * Removed list code - it's now in list.[ch]
97 * Renamed "permission" to "action", and changed many features
98 * to use the actions file rather than the global config.
100 * Revision 1.12 2001/05/31 17:33:13 oes
104 * Revision 1.11 2001/05/29 20:11:19 joergs
105 * '/ * inside comment' warning removed.
107 * Revision 1.10 2001/05/29 09:50:24 jongfoster
108 * Unified blocklist/imagelist/permissionslist.
109 * File format is still under discussion, but the internal changes
112 * Also modified interceptor behaviour:
113 * - We now intercept all URLs beginning with one of the following
114 * prefixes (and *only* these prefixes):
116 * * http://ijbswa.sf.net/config/
117 * * http://ijbswa.sourceforge.net/config/
118 * - New interceptors "home page" - go to http://i.j.b/ to see it.
119 * - Internal changes so that intercepted and fast redirect pages
120 * are not replaced with an image.
121 * - Interceptors now have the option to send a binary page direct
122 * to the client. (i.e. ijb-send-banner uses this)
123 * - Implemented show-url-info interceptor. (Which is why I needed
124 * the above interceptors changes - a typical URL is
125 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
126 * The previous mechanism would not have intercepted that, and
127 * if it had been intercepted then it then it would have replaced
130 * Revision 1.9 2001/05/28 17:26:33 jongfoster
131 * Fixing segfault if last header was crunched.
132 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
133 * can use the cross-platform sprintf() instead.)
135 * Revision 1.8 2001/05/27 22:17:04 oes
137 * - re_process_buffer no longer writes the modified buffer
138 * to the client, which was very ugly. It now returns the
139 * buffer, which it is then written by chat.
141 * - content_length now adjusts the Content-Length: header
142 * for modified documents rather than crunch()ing it.
143 * (Length info in csp->content_length, which is 0 for
144 * unmodified documents)
146 * - For this to work, sed() is called twice when filtering.
148 * Revision 1.7 2001/05/27 13:19:06 oes
149 * Patched Joergs solution for the content-length in.
151 * Revision 1.6 2001/05/26 13:39:32 jongfoster
152 * Only crunches Content-Length header if applying RE filtering.
153 * Without this fix, Microsoft Windows Update wouldn't work.
155 * Revision 1.5 2001/05/26 00:28:36 jongfoster
156 * Automatic reloading of config file.
157 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
158 * Most of the global variables have been moved to a new
159 * struct configuration_spec, accessed through csp->config->globalname
160 * Most of the globals remaining are used by the Win32 GUI.
162 * Revision 1.4 2001/05/22 18:46:04 oes
164 * - Enabled filtering banners by size rather than URL
165 * by adding patterns that replace all standard banner
166 * sizes with the "Junkbuster" gif to the re_filterfile
168 * - Enabled filtering WebBugs by providing a pattern
169 * which kills all 1x1 images
171 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
172 * which is selected by the (nonstandard and therefore
173 * capital) letter 'U' in the option string.
174 * It causes the quantifiers to be ungreedy by default.
175 * Appending a ? turns back to greedy (!).
177 * - Added a new interceptor ijb-send-banner, which
178 * sends back the "Junkbuster" gif. Without imagelist or
179 * MSIE detection support, or if tinygif = 1, or the
180 * URL isn't recognized as an imageurl, a lame HTML
181 * explanation is sent instead.
183 * - Added new feature, which permits blocking remote
184 * script redirects and firing back a local redirect
186 * The feature is conditionally compiled, i.e. it
187 * can be disabled with --disable-fast-redirects,
188 * plus it must be activated by a "fast-redirects"
189 * line in the config file, has its own log level
190 * and of course wants to be displayed by show-proxy-args
191 * Note: Boy, all the #ifdefs in 1001 locations and
192 * all the fumbling with configure.in and acconfig.h
193 * were *way* more work than the feature itself :-(
195 * - Because a generic redirect template was needed for
196 * this, tinygif = 3 now uses the same.
198 * - Moved GIFs, and other static HTTP response templates
203 * - Removed some >400 CRs again (Jon, you really worked
206 * Revision 1.3 2001/05/20 01:21:20 jongfoster
207 * Version 2.9.4 checkin.
208 * - Merged popupfile and cookiefile, and added control over PCRS
209 * filtering, in new "permissionsfile".
210 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
211 * file error you now get a message box (in the Win32 GUI) rather
212 * than the program exiting with no explanation.
213 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
215 * - Removed tabs from "config"
216 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
217 * - Bumped up version number.
219 * Revision 1.2 2001/05/17 23:02:36 oes
220 * - Made referrer option accept 'L' as a substitute for 'ยง'
222 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
223 * Initial import of version 2.9.3 source tree
226 *********************************************************************/
232 #include <sys/types.h>
250 #include "jbsockets.h"
251 #include "miscutil.h"
254 const char parsers_h_rcs[] = PARSERS_H_VERSION;
256 /* Fix a problem with Solaris. There should be no effect on other
258 * Solaris's isspace() is a macro which uses it's argument directly
259 * as an array index. Therefore we need to make sure that high-bit
260 * characters generate +ve values, and ideally we also want to make
261 * the argument match the declared parameter type of "int".
263 * Why did they write a character function that can't take a simple
264 * "char" argument? Doh!
266 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
267 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
270 const struct parsers client_patterns[] = {
271 { "referer:", 8, client_referrer },
272 { "user-agent:", 11, client_uagent },
273 { "ua-", 3, client_ua },
274 { "from:", 5, client_from },
275 { "cookie:", 7, client_send_cookie },
276 { "x-forwarded-for:", 16, client_x_forwarded },
277 #ifdef FEATURE_DENY_GZIP
278 { "Accept-Encoding: gzip", 21, crumble },
279 #endif /* def FEATURE_DENY_GZIP */
280 #if defined(FEATURE_IMAGE_DETECT_MSIE)
281 { "Accept:", 7, client_accept },
282 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
283 #ifdef FEATURE_FORCE_LOAD
284 { "Host:", 5, client_host },
285 #endif /* def FEATURE_FORCE_LOAD */
286 /* { "if-modified-since:", 18, crumble }, */
287 { "Keep-Alive:", 11, crumble },
288 { "connection:", 11, crumble },
289 { "proxy-connection:", 17, crumble },
294 const struct parsers server_patterns[] = {
295 { "set-cookie:", 11, server_set_cookie },
296 { "connection:", 11, crumble },
297 { "Content-Type:", 13, content_type },
298 { "Content-Length:", 15, content_length },
303 void (* const add_client_headers[])(struct client_state *) = {
305 client_x_forwarded_adder,
307 connection_close_adder,
312 void (* const add_server_headers[])(struct client_state *) = {
313 connection_close_adder,
318 /*********************************************************************
320 * Function : flush_socket
322 * Description : Write any pending "buffered" content.
325 * 1 : fd = file descriptor of the socket to read
326 * 2 : csp = Current client state (buffers, headers, etc...)
328 * Returns : On success, the number of bytes written are returned (zero
329 * indicates nothing was written). On error, -1 is returned,
330 * and errno is set appropriately. If count is zero and the
331 * file descriptor refers to a regular file, 0 will be
332 * returned without causing any other effect. For a special
333 * file, the results are not portable.
335 *********************************************************************/
336 int flush_socket(int fd, struct client_state *csp)
338 struct iob *iob = csp->iob;
339 int n = iob->eod - iob->cur;
346 n = write_socket(fd, iob->cur, n);
347 iob->eod = iob->cur = iob->buf;
353 /*********************************************************************
355 * Function : add_to_iob
357 * Description : Add content to the buffered page.
360 * 1 : csp = Current client state (buffers, headers, etc...)
361 * 2 : buf = holds the content to be added to the page
362 * 3 : n = number of bytes to be added
364 * Returns : Number of bytes in the content buffer.
366 *********************************************************************/
367 int add_to_iob(struct client_state *csp, char *buf, int n)
369 struct iob *iob = csp->iob;
373 have = iob->eod - iob->cur;
382 if ((p = (char *)malloc(need + 1)) == NULL)
384 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
390 /* there is something in the buffer - save it */
391 memcpy(p, iob->cur, have);
393 /* replace the buffer with the new space */
397 /* point to the end of the data */
402 /* the buffer is empty, free it and reinitialize */
407 /* copy the new data into the iob buffer */
410 /* point to the end of the data */
413 /* null terminate == cheap insurance */
416 /* set the pointers to the new values */
425 /*********************************************************************
427 * Function : get_header
429 * Description : This (odd) routine will parse the csp->iob
432 * 1 : csp = Current client state (buffers, headers, etc...)
434 * Returns : Any one of the following:
436 * 1) a pointer to a dynamically allocated string that contains a header line
437 * 2) NULL indicating that the end of the header was reached
438 * 3) "" indicating that the end of the iob was reached before finding
439 * a complete header line.
441 *********************************************************************/
442 char *get_header(struct client_state *csp)
448 if ((iob->cur == NULL)
449 || ((p = strchr(iob->cur, '\n')) == NULL))
451 return(""); /* couldn't find a complete header */
456 ret = strdup(iob->cur);
460 if ((q = strchr(ret, '\r'))) *q = '\0';
462 /* is this a blank linke (i.e. the end of the header) ? */
474 /*********************************************************************
478 * Description : add, delete or modify lines in the HTTP header streams.
479 * On entry, it receives a linked list of headers space
480 * that was allocated dynamically (both the list nodes
481 * and the header contents).
483 * As a side effect it frees the space used by the original
487 * 1 : pats = list of patterns to match against headers
488 * 2 : more_headers = list of functions to add more
489 * headers (client or server)
490 * 3 : csp = Current client state (buffers, headers, etc...)
492 * Returns : Single pointer to a fully formed header.
494 *********************************************************************/
495 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
497 struct list_entry *p;
498 const struct parsers *v;
502 for (v = pats; v->str ; v++)
504 for (p = csp->headers->first; p ; p = p->next)
506 /* Header crunch()ed in previous run? -> ignore */
507 if (p->str == NULL) continue;
509 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
511 if (strncmpic(p->str, v->str, v->len) == 0)
513 hdr = v->parser(v, p->str, csp);
514 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
520 /* place any additional headers on the csp->headers list */
521 for (f = more_headers; *f ; f++)
526 hdr = list_to_text(csp->headers);
533 /*********************************************************************
535 * Function : free_http_request
537 * Description : Freez a http_request structure
540 * 1 : http = points to a http_request structure to free
544 *********************************************************************/
545 void free_http_request(struct http_request *http)
550 freez(http->hostport);
553 freez(http->host_ip_addr_str);
554 freez(http->user_agent);
559 /*********************************************************************
561 * Function : parse_http_request
563 * Description : Parse out the host and port from the URL. Find the
564 * hostname & path, port (if ':'), and/or password (if '@')
567 * 1 : req = URL (or is it URI?) to break down
568 * 2 : http = pointer to the http structure to hold elements
569 * 3 : csp = Current client state (buffers, headers, etc...)
573 *********************************************************************/
574 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
576 char *buf, *v[10], *url, *p, *save_url;
579 memset(http, '\0', sizeof(*http));
581 http->cmd = strdup(req);
584 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
588 /* this could be a CONNECT request */
589 if (strcmpic(v[0], "connect") == 0)
592 http->gpc = strdup(v[0]);
593 http->hostport = strdup(v[1]);
594 http->ver = strdup(v[2]);
597 /* or it could be a GET or a POST (possibly webDAV too) */
598 if ((0 == strcmpic(v[0], "get"))
599 || (0 == strcmpic(v[0], "head"))
600 || (0 == strcmpic(v[0], "post"))
601 /* These are the headers as defined in RFC2518 to add webDAV support: */
602 || (0 == strcmpic(v[0], "propfind"))
603 || (0 == strcmpic(v[0], "proppatch"))
604 || (0 == strcmpic(v[0], "move"))
605 || (0 == strcmpic(v[0], "copy"))
606 || (0 == strcmpic(v[0], "mkcol"))
607 || (0 == strcmpic(v[0], "lock"))
608 || (0 == strcmpic(v[0], "unlock"))
612 http->gpc = strdup(v[0]);
614 http->ver = strdup(v[2]);
617 if (strncmpic(url, "http://", 7) == 0)
621 else if (strncmpic(url, "https://", 8) == 0)
632 if ((p = strchr(url, '/')))
634 http->path = strdup(p);
636 http->hostport = strdup(url);
639 * Repair broken HTTP requests that don't contain a path
643 /* Repair hostport & path */
644 http->path = strdup("/");
645 http->hostport = strdup(url);
647 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
649 http->cmd = strsav(http->cmd, http->gpc);
650 http->cmd = strsav(http->cmd, " ");
651 http->cmd = strsav(http->cmd, save_url);
652 http->cmd = strsav(http->cmd, "/ ");
653 http->cmd = strsav(http->cmd, http->ver);
662 if (http->hostport == NULL)
664 free_http_request(http);
668 buf = strdup(http->hostport);
671 /* check if url contains password */
672 n = ssplit(buf, "@", v, SZ(v), 1, 1);
675 char * newbuf = NULL;
676 newbuf = strdup(v[1]);
681 n = ssplit(buf, ":", v, SZ(v), 1, 1);
685 http->host = strdup(v[0]);
691 http->host = strdup(v[0]);
692 http->port = atoi(v[1]);
697 if (http->host == NULL)
699 free_http_request(http);
702 if (http->path == NULL)
704 http->path = strdup("");
710 /* here begins the family of parser functions that reformat header lines */
713 /*********************************************************************
717 * Description : This is called if a header matches a pattern to "crunch"
720 * 1 : v = Pointer to parsers structure, which basically holds
721 * headers (client or server) that we want to "crunch"
722 * 2 : s = header (from sed) to "crunch"
723 * 3 : csp = Current client state (buffers, headers, etc...)
725 * Returns : Always NULL.
727 *********************************************************************/
728 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
730 log_error(LOG_LEVEL_HEADER, "crunch!");
736 /*********************************************************************
738 * Function : content_type
740 * Description : Is this a text/.* or javascript MIME Type?
744 * 2 : s = header string we are "considering"
745 * 3 : csp = Current client state (buffers, headers, etc...)
747 * Returns : A duplicate string pointer to this header (ie. pass thru)
749 *********************************************************************/
750 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
752 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
753 csp->content_type = CT_TEXT;
754 else if (strstr(s, " image/gif"))
755 csp->content_type = CT_GIF;
757 csp->content_type = 0;
764 /*********************************************************************
766 * Function : content_length
768 * Description : Adjust Content-Length header if we modified
773 * 2 : s = header string we are "considering"
774 * 3 : csp = Current client state (buffers, headers, etc...)
776 * Returns : A duplicate string pointer to this header (ie. pass thru)
778 *********************************************************************/
779 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
781 if (csp->content_length != 0) /* Content has been modified */
783 char * s2 = (char *) zalloc(100);
784 sprintf(s2, "Content-Length: %d", csp->content_length);
786 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
797 /*********************************************************************
799 * Function : client_referrer
801 * Description : Handle the "referer" config setting properly.
806 * 2 : s = header (from sed) to "crunch"
807 * 3 : csp = Current client state (buffers, headers, etc...)
809 * Returns : NULL if crunched, or a malloc'ed string with the original
812 *********************************************************************/
813 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
817 #ifdef FEATURE_FORCE_LOAD
818 /* Since the referrer can include the prefix even
819 * even if the request itself is non-forced, we must
820 * clean it unconditionally
822 strclean(s, FORCE_PREFIX);
823 #endif /* def FEATURE_FORCE_LOAD */
826 csp->referrer = strdup(s);
827 #endif /* def FEATURE_TRUST */
830 * Are we sending referer?
832 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
837 newval = csp->action->string[ACTION_STRING_REFERER];
840 * Are we blocking referer?
842 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
844 log_error(LOG_LEVEL_HEADER, "crunch!");
849 * Are we forging referer?
851 if (0 == strcmpic(newval, "forge"))
854 * Forge a referer as http://[hostname:port of REQUEST]/
855 * to fool stupid checks for in-site links
857 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
858 s2 = strsav(NULL, "Referer: ");
859 s2 = strsav(s2, "http://");
860 s2 = strsav(s2, csp->http->hostport);
861 s2 = strsav(s2, "/");
866 * Have we got a fixed referer?
868 if (0 == strncmpic(newval, "http://", 7))
871 * We have a specific (fixed) referer we want to send.
875 log_error(LOG_LEVEL_HEADER, "modified");
877 s2 = strsav( NULL, "Referer: " );
878 s2 = strsav( s2, newval );
882 /* Should never get here! */
883 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
886 * Forge is probably the best default.
888 * Forge a referer as http://[hostname:port of REQUEST]/
889 * to fool stupid checks for in-site links
891 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
892 s2 = strsav(NULL, "Referer: ");
893 s2 = strsav(s2, "http://");
894 s2 = strsav(s2, csp->http->hostport);
895 s2 = strsav(s2, "/");
900 /*********************************************************************
902 * Function : client_uagent
904 * Description : Handle the "user-agent" config setting properly
905 * and remember its original value to enable browser
906 * bug workarounds. Called from `sed'.
910 * 2 : s = header (from sed) to "crunch"
911 * 3 : csp = Current client state (buffers, headers, etc...)
913 * Returns : A malloc'ed pointer to the default agent, or
914 * a malloc'ed string pointer to this header (ie. pass thru).
916 *********************************************************************/
917 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
922 /* Save the client's User-Agent: value */
925 csp->http->user_agent = strdup(s + 12);
928 #ifdef FEATURE_IMAGE_DETECT_MSIE
929 if (strstr (s, "MSIE "))
931 /* This is Microsoft Internet Explorer.
932 * Enable auto-detect.
934 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
936 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
938 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
943 newval = csp->action->string[ACTION_STRING_USER_AGENT];
949 log_error(LOG_LEVEL_HEADER, "modified");
951 s2 = strsav( NULL, "User-Agent: " );
952 s2 = strsav( s2, newval );
958 /*********************************************************************
960 * Function : client_ua
962 * Description : Handle "ua-" headers properly. Called from `sed'.
966 * 2 : s = header (from sed) to "crunch"
967 * 3 : csp = Current client state (buffers, headers, etc...)
969 * Returns : NULL if crunched, or a malloc'ed string to original header
971 *********************************************************************/
972 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
974 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
980 log_error(LOG_LEVEL_HEADER, "crunch!");
986 /*********************************************************************
988 * Function : client_from
990 * Description : Handle the "from" config setting properly.
995 * 2 : s = header (from sed) to "crunch"
996 * 3 : csp = Current client state (buffers, headers, etc...)
998 * Returns : NULL if crunched, or a malloc'ed string to
999 * modified/original header.
1001 *********************************************************************/
1002 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1004 const char * newval;
1007 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1012 newval = csp->action->string[ACTION_STRING_FROM];
1015 * Are we blocking referer?
1017 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1019 log_error(LOG_LEVEL_HEADER, "crunch!");
1023 log_error(LOG_LEVEL_HEADER, " modified");
1025 s2 = strsav( NULL, "From: " );
1026 s2 = strsav( s2, newval );
1032 /*********************************************************************
1034 * Function : client_send_cookie
1036 * Description : Handle the "cookie" header properly. Called from `sed'.
1037 * If cookie is accepted, add it to the cookie_list,
1038 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1041 * 1 : v = pattern of cookie `sed' found matching
1042 * 2 : s = header (from sed) to "crunch"
1043 * 3 : csp = Current client state (buffers, headers, etc...)
1045 * Returns : Always NULL.
1047 *********************************************************************/
1048 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1050 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1052 enlist(csp->cookie_list, s + v->len + 1);
1056 log_error(LOG_LEVEL_HEADER, " crunch!");
1060 * Always return NULL here. The cookie header
1061 * will be sent at the end of the header.
1068 /*********************************************************************
1070 * Function : client_x_forwarded
1072 * Description : Handle the "x-forwarded-for" config setting properly,
1073 * also used in the add_client_headers list. Called from `sed'.
1077 * 2 : s = header (from sed) to "crunch"
1078 * 3 : csp = Current client state (buffers, headers, etc...)
1080 * Returns : Always NULL.
1082 *********************************************************************/
1083 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1085 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1087 /* Save it so we can re-add it later */
1088 csp->x_forwarded = strdup(s);
1092 * Always return NULL, since this information
1093 * will be sent at the end of the header.
1100 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1101 /*********************************************************************
1103 * Function : client_accept
1105 * Description : Detect whether the client wants HTML or an image.
1106 * Clients do not always make this information available
1107 * in a sane way. Always passes the header through
1108 * the proxy unchanged.
1112 * 2 : s = Header string. Null terminated.
1113 * 3 : csp = Current client state (buffers, headers, etc...)
1115 * Returns : Duplicate of argument s.
1117 *********************************************************************/
1118 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1120 #ifdef FEATURE_IMAGE_DETECT_MSIE
1121 if (strstr (s, "image/gif"))
1123 /* Client will accept HTML. If this seems counterintuitive,
1126 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1130 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1132 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1137 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1141 /* the following functions add headers directly to the header list */
1144 /*********************************************************************
1146 * Function : client_cookie_adder
1148 * Description : Used in the add_client_headers list. Called from `sed'.
1151 * 1 : csp = Current client state (buffers, headers, etc...)
1155 *********************************************************************/
1156 void client_cookie_adder(struct client_state *csp)
1158 struct list_entry *lst;
1162 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1166 tmp = strsav(tmp, "; ");
1168 tmp = strsav(tmp, lst->str);
1171 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1175 tmp = strsav(tmp, "; ");
1178 if ((e = cookie_encode(lst->str)))
1180 tmp = strsav(tmp, e);
1189 ret = strdup("Cookie: ");
1190 ret = strsav(ret, tmp);
1191 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1192 enlist(csp->headers, ret);
1200 /*********************************************************************
1202 * Function : client_xtra_adder
1204 * Description : Used in the add_client_headers list. Called from `sed'.
1207 * 1 : csp = Current client state (buffers, headers, etc...)
1211 *********************************************************************/
1212 void client_xtra_adder(struct client_state *csp)
1214 struct list_entry *lst;
1216 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1217 lst ; lst = lst->next)
1219 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1220 enlist(csp->headers, lst->str);
1226 /*********************************************************************
1228 * Function : client_x_forwarded_adder
1230 * Description : Used in the add_client_headers list. Called from `sed'.
1233 * 1 : csp = Current client state (buffers, headers, etc...)
1237 *********************************************************************/
1238 void client_x_forwarded_adder(struct client_state *csp)
1242 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1247 if (csp->x_forwarded)
1249 p = strsav(p, csp->x_forwarded);
1250 p = strsav(p, ", ");
1251 p = strsav(p, csp->ip_addr_str);
1255 p = strsav(p, "X-Forwarded-For: ");
1256 p = strsav(p, csp->ip_addr_str);
1259 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1260 enlist(csp->headers, p);
1265 /*********************************************************************
1267 * Function : connection_close_adder
1269 * Description : Adds a "Connection: close" header to csp->headers
1270 * as a temporary fix for the needed but missing HTTP/1.1
1271 * support. Called from `sed'.
1272 * FIXME: This whole function shouldn't be neccessary!
1275 * 1 : csp = Current client state (buffers, headers, etc...)
1279 *********************************************************************/
1280 void connection_close_adder(struct client_state *csp)
1282 enlist(csp->headers, strdup("Connection: close"));
1287 /*********************************************************************
1289 * Function : server_set_cookie
1291 * Description : Handle the server "cookie" header properly.
1292 * Log cookie to the jar file. Then "crunch" it,
1293 * or accept it. Called from `sed'.
1296 * 1 : v = parser pattern that matched this header
1297 * 2 : s = header that matched this pattern
1298 * 3 : csp = Current client state (buffers, headers, etc...)
1300 * Returns : `crumble' or a newly malloc'ed string.
1302 *********************************************************************/
1303 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1305 #ifdef FEATURE_COOKIE_JAR
1306 if (csp->config->jar)
1308 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1310 #endif /* def FEATURE_COOKIE_JAR */
1312 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1314 return(crumble(v, s, csp));
1322 #ifdef FEATURE_FORCE_LOAD
1323 /*********************************************************************
1325 * Function : client_host
1327 * Description : Clean the FORCE_PREFIX out of the 'host' http
1328 * header, if we use force
1332 * 2 : s = header (from sed) to clean
1333 * 3 : csp = Current client state (buffers, headers, etc...)
1335 * Returns : A malloc'ed pointer to the cleaned host header
1337 *********************************************************************/
1338 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1340 char *cleanhost = strdup(s);
1343 strclean(cleanhost, FORCE_PREFIX);
1347 #endif /* def FEATURE_FORCE_LOAD */
1350 #ifdef FEATURE_FORCE_LOAD
1351 /*********************************************************************
1353 * Function : strclean
1355 * Description : In-Situ-Eliminate all occurances of substring in
1359 * 1 : string = string to clean
1360 * 2 : substring = substring to eliminate
1362 * Returns : Number of eliminations
1364 *********************************************************************/
1365 int strclean(const char *string, const char *substring)
1367 int hits = 0, len = strlen(substring);
1370 while((pos = strstr(string, substring)))
1377 while (*p++ != '\0');
1384 #endif /* def FEATURE_FORCE_LOAD */