1 const char parsers_rcs[] = "$Id: parsers.c,v 1.23 2001/09/12 18:08:19 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.23 2001/09/12 18:08:19 steudten
46 * In parse_http_request() header rewriting miss the host value, so
47 * from http://www.mydomain.com the result was just " / " not
48 * http://www.mydomain.com/ in case we forward.
50 * Revision 1.22 2001/09/10 10:58:53 oes
51 * Silenced compiler warnings
53 * Revision 1.21 2001/07/31 14:46:00 oes
54 * - Persistant connections now suppressed
55 * - sed() no longer appends empty header to csp->headers
57 * Revision 1.20 2001/07/30 22:08:36 jongfoster
58 * Tidying up #defines:
59 * - All feature #defines are now of the form FEATURE_xxx
60 * - Permanently turned off WIN_GUI_EDIT
61 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
63 * Revision 1.19 2001/07/25 17:21:54 oes
64 * client_uagent now saves copy of User-Agent: header value
66 * Revision 1.18 2001/07/13 14:02:46 oes
67 * - Included fix to repair broken HTTP requests that
68 * don't contain a path, not even '/'.
69 * - Removed all #ifdef PCRS
70 * - content_type now always inspected and classified as
72 * - formatting / comments
74 * Revision 1.17 2001/06/29 21:45:41 oes
75 * Indentation, CRLF->LF, Tab-> Space
77 * Revision 1.16 2001/06/29 13:32:42 oes
79 * - Adapted free_http_request
80 * - Removed logentry from cancelled commit
82 * Revision 1.15 2001/06/03 19:12:38 oes
83 * deleted const struct interceptors
85 * Revision 1.14 2001/06/01 18:49:17 jongfoster
86 * Replaced "list_share" with "list" - the tiny memory gain was not
87 * worth the extra complexity.
89 * Revision 1.13 2001/05/31 21:30:33 jongfoster
90 * Removed list code - it's now in list.[ch]
91 * Renamed "permission" to "action", and changed many features
92 * to use the actions file rather than the global config.
94 * Revision 1.12 2001/05/31 17:33:13 oes
98 * Revision 1.11 2001/05/29 20:11:19 joergs
99 * '/ * inside comment' warning removed.
101 * Revision 1.10 2001/05/29 09:50:24 jongfoster
102 * Unified blocklist/imagelist/permissionslist.
103 * File format is still under discussion, but the internal changes
106 * Also modified interceptor behaviour:
107 * - We now intercept all URLs beginning with one of the following
108 * prefixes (and *only* these prefixes):
110 * * http://ijbswa.sf.net/config/
111 * * http://ijbswa.sourceforge.net/config/
112 * - New interceptors "home page" - go to http://i.j.b/ to see it.
113 * - Internal changes so that intercepted and fast redirect pages
114 * are not replaced with an image.
115 * - Interceptors now have the option to send a binary page direct
116 * to the client. (i.e. ijb-send-banner uses this)
117 * - Implemented show-url-info interceptor. (Which is why I needed
118 * the above interceptors changes - a typical URL is
119 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
120 * The previous mechanism would not have intercepted that, and
121 * if it had been intercepted then it then it would have replaced
124 * Revision 1.9 2001/05/28 17:26:33 jongfoster
125 * Fixing segfault if last header was crunched.
126 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
127 * can use the cross-platform sprintf() instead.)
129 * Revision 1.8 2001/05/27 22:17:04 oes
131 * - re_process_buffer no longer writes the modified buffer
132 * to the client, which was very ugly. It now returns the
133 * buffer, which it is then written by chat.
135 * - content_length now adjusts the Content-Length: header
136 * for modified documents rather than crunch()ing it.
137 * (Length info in csp->content_length, which is 0 for
138 * unmodified documents)
140 * - For this to work, sed() is called twice when filtering.
142 * Revision 1.7 2001/05/27 13:19:06 oes
143 * Patched Joergs solution for the content-length in.
145 * Revision 1.6 2001/05/26 13:39:32 jongfoster
146 * Only crunches Content-Length header if applying RE filtering.
147 * Without this fix, Microsoft Windows Update wouldn't work.
149 * Revision 1.5 2001/05/26 00:28:36 jongfoster
150 * Automatic reloading of config file.
151 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
152 * Most of the global variables have been moved to a new
153 * struct configuration_spec, accessed through csp->config->globalname
154 * Most of the globals remaining are used by the Win32 GUI.
156 * Revision 1.4 2001/05/22 18:46:04 oes
158 * - Enabled filtering banners by size rather than URL
159 * by adding patterns that replace all standard banner
160 * sizes with the "Junkbuster" gif to the re_filterfile
162 * - Enabled filtering WebBugs by providing a pattern
163 * which kills all 1x1 images
165 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
166 * which is selected by the (nonstandard and therefore
167 * capital) letter 'U' in the option string.
168 * It causes the quantifiers to be ungreedy by default.
169 * Appending a ? turns back to greedy (!).
171 * - Added a new interceptor ijb-send-banner, which
172 * sends back the "Junkbuster" gif. Without imagelist or
173 * MSIE detection support, or if tinygif = 1, or the
174 * URL isn't recognized as an imageurl, a lame HTML
175 * explanation is sent instead.
177 * - Added new feature, which permits blocking remote
178 * script redirects and firing back a local redirect
180 * The feature is conditionally compiled, i.e. it
181 * can be disabled with --disable-fast-redirects,
182 * plus it must be activated by a "fast-redirects"
183 * line in the config file, has its own log level
184 * and of course wants to be displayed by show-proxy-args
185 * Note: Boy, all the #ifdefs in 1001 locations and
186 * all the fumbling with configure.in and acconfig.h
187 * were *way* more work than the feature itself :-(
189 * - Because a generic redirect template was needed for
190 * this, tinygif = 3 now uses the same.
192 * - Moved GIFs, and other static HTTP response templates
197 * - Removed some >400 CRs again (Jon, you really worked
200 * Revision 1.3 2001/05/20 01:21:20 jongfoster
201 * Version 2.9.4 checkin.
202 * - Merged popupfile and cookiefile, and added control over PCRS
203 * filtering, in new "permissionsfile".
204 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
205 * file error you now get a message box (in the Win32 GUI) rather
206 * than the program exiting with no explanation.
207 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
209 * - Removed tabs from "config"
210 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
211 * - Bumped up version number.
213 * Revision 1.2 2001/05/17 23:02:36 oes
214 * - Made referrer option accept 'L' as a substitute for '§'
216 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
217 * Initial import of version 2.9.3 source tree
220 *********************************************************************/
226 #include <sys/types.h>
241 #include "showargs.h"
245 #include "jbsockets.h"
246 #include "miscutil.h"
249 const char parsers_h_rcs[] = PARSERS_H_VERSION;
251 /* Fix a problem with Solaris. There should be no effect on other
253 * Solaris's isspace() is a macro which uses it's argument directly
254 * as an array index. Therefore we need to make sure that high-bit
255 * characters generate +ve values, and ideally we also want to make
256 * the argument match the declared parameter type of "int".
258 * Why did they write a character function that can't take a simple
259 * "char" argument? Doh!
261 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
262 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
265 const struct parsers client_patterns[] = {
266 { "referer:", 8, client_referrer },
267 { "user-agent:", 11, client_uagent },
268 { "ua-", 3, client_ua },
269 { "from:", 5, client_from },
270 { "cookie:", 7, client_send_cookie },
271 { "x-forwarded-for:", 16, client_x_forwarded },
272 #ifdef FEATURE_DENY_GZIP
273 { "Accept-Encoding: gzip", 21, crumble },
274 #endif /* def FEATURE_DENY_GZIP */
275 #if defined(FEATURE_IMAGE_DETECT_MSIE)
276 { "Accept:", 7, client_accept },
277 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
278 #ifdef FEATURE_FORCE_LOAD
279 { "Host:", 5, client_host },
280 #endif /* def FEATURE_FORCE_LOAD */
281 /* { "if-modified-since:", 18, crumble }, */
282 { "Keep-Alive:", 11, crumble },
283 { "connection:", 11, crumble },
284 { "proxy-connection:", 17, crumble },
289 const struct parsers server_patterns[] = {
290 { "set-cookie:", 11, server_set_cookie },
291 { "connection:", 11, crumble },
292 { "Content-Type:", 13, content_type },
293 { "Content-Length:", 15, content_length },
298 void (* const add_client_headers[])(struct client_state *) = {
300 client_x_forwarded_adder,
302 connection_close_adder,
307 void (* const add_server_headers[])(struct client_state *) = {
308 connection_close_adder,
313 /*********************************************************************
315 * Function : flush_socket
317 * Description : Write any pending "buffered" content.
320 * 1 : fd = file descriptor of the socket to read
321 * 2 : csp = Current client state (buffers, headers, etc...)
323 * Returns : On success, the number of bytes written are returned (zero
324 * indicates nothing was written). On error, -1 is returned,
325 * and errno is set appropriately. If count is zero and the
326 * file descriptor refers to a regular file, 0 will be
327 * returned without causing any other effect. For a special
328 * file, the results are not portable.
330 *********************************************************************/
331 int flush_socket(int fd, struct client_state *csp)
333 struct iob *iob = csp->iob;
334 int n = iob->eod - iob->cur;
341 n = write_socket(fd, iob->cur, n);
342 iob->eod = iob->cur = iob->buf;
348 /*********************************************************************
350 * Function : add_to_iob
352 * Description : Add content to the buffered page.
355 * 1 : csp = Current client state (buffers, headers, etc...)
356 * 2 : buf = holds the content to be added to the page
357 * 3 : n = number of bytes to be added
359 * Returns : Number of bytes in the content buffer.
361 *********************************************************************/
362 int add_to_iob(struct client_state *csp, char *buf, int n)
364 struct iob *iob = csp->iob;
368 have = iob->eod - iob->cur;
377 if ((p = (char *)malloc(need + 1)) == NULL)
379 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
385 /* there is something in the buffer - save it */
386 memcpy(p, iob->cur, have);
388 /* replace the buffer with the new space */
392 /* point to the end of the data */
397 /* the buffer is empty, free it and reinitialize */
402 /* copy the new data into the iob buffer */
405 /* point to the end of the data */
408 /* null terminate == cheap insurance */
411 /* set the pointers to the new values */
420 /*********************************************************************
422 * Function : get_header
424 * Description : This (odd) routine will parse the csp->iob
427 * 1 : csp = Current client state (buffers, headers, etc...)
429 * Returns : Any one of the following:
431 * 1) a pointer to a dynamically allocated string that contains a header line
432 * 2) NULL indicating that the end of the header was reached
433 * 3) "" indicating that the end of the iob was reached before finding
434 * a complete header line.
436 *********************************************************************/
437 char *get_header(struct client_state *csp)
443 if ((iob->cur == NULL)
444 || ((p = strchr(iob->cur, '\n')) == NULL))
446 return(""); /* couldn't find a complete header */
451 ret = strdup(iob->cur);
455 if ((q = strchr(ret, '\r'))) *q = '\0';
457 /* is this a blank linke (i.e. the end of the header) ? */
469 /*********************************************************************
473 * Description : add, delete or modify lines in the HTTP header streams.
474 * On entry, it receives a linked list of headers space
475 * that was allocated dynamically (both the list nodes
476 * and the header contents).
478 * As a side effect it frees the space used by the original
482 * 1 : pats = list of patterns to match against headers
483 * 2 : more_headers = list of functions to add more
484 * headers (client or server)
485 * 3 : csp = Current client state (buffers, headers, etc...)
487 * Returns : Single pointer to a fully formed header.
489 *********************************************************************/
490 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
493 const struct parsers *v;
497 for (v = pats; v->str ; v++)
499 for (p = csp->headers->next; p ; p = p->next)
501 /* Header crunch()ed in previous run? -> ignore */
502 if (p->str == NULL) continue;
504 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
506 if (strncmpic(p->str, v->str, v->len) == 0)
508 hdr = v->parser(v, p->str, csp);
515 /* place any additional headers on the csp->headers list */
516 for (f = more_headers; *f ; f++)
521 hdr = list_to_text(csp->headers);
528 /*********************************************************************
530 * Function : free_http_request
532 * Description : Freez a http_request structure
535 * 1 : http = points to a http_request structure to free
539 *********************************************************************/
540 void free_http_request(struct http_request *http)
545 freez(http->hostport);
548 freez(http->host_ip_addr_str);
549 freez(http->user_agent);
554 /*********************************************************************
556 * Function : parse_http_request
558 * Description : Parse out the host and port from the URL. Find the
559 * hostname & path, port (if ':'), and/or password (if '@')
562 * 1 : req = URL (or is it URI?) to break down
563 * 2 : http = pointer to the http structure to hold elements
564 * 3 : csp = Current client state (buffers, headers, etc...)
568 *********************************************************************/
569 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
571 char *buf, *v[10], *url, *p, *save_url;
574 memset(http, '\0', sizeof(*http));
576 http->cmd = strdup(req);
579 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
583 /* this could be a CONNECT request */
584 if (strcmpic(v[0], "connect") == 0)
587 http->gpc = strdup(v[0]);
588 http->hostport = strdup(v[1]);
589 http->ver = strdup(v[2]);
592 /* or it could be a GET or a POST (possibly webDAV too) */
593 if ((0 == strcmpic(v[0], "get"))
594 || (0 == strcmpic(v[0], "head"))
595 || (0 == strcmpic(v[0], "post"))
596 /* These are the headers as defined in RFC2518 to add webDAV support: */
597 || (0 == strcmpic(v[0], "propfind"))
598 || (0 == strcmpic(v[0], "proppatch"))
599 || (0 == strcmpic(v[0], "move"))
600 || (0 == strcmpic(v[0], "copy"))
601 || (0 == strcmpic(v[0], "mkcol"))
602 || (0 == strcmpic(v[0], "lock"))
603 || (0 == strcmpic(v[0], "unlock"))
607 http->gpc = strdup(v[0]);
609 http->ver = strdup(v[2]);
612 if (strncmpic(url, "http://", 7) == 0)
616 else if (strncmpic(url, "https://", 8) == 0)
627 if ((p = strchr(url, '/')))
629 http->path = strdup(p);
631 http->hostport = strdup(url);
634 * Repair broken HTTP requests that don't contain a path
638 /* Repair hostport & path */
639 http->path = strdup("/");
640 http->hostport = strdup(url);
642 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
644 http->cmd = strsav(http->cmd, http->gpc);
645 http->cmd = strsav(http->cmd, " ");
646 http->cmd = strsav(http->cmd, save_url);
647 http->cmd = strsav(http->cmd, "/ ");
648 http->cmd = strsav(http->cmd, http->ver);
657 if (http->hostport == NULL)
659 free_http_request(http);
663 buf = strdup(http->hostport);
666 /* check if url contains password */
667 n = ssplit(buf, "@", v, SZ(v), 1, 1);
670 char * newbuf = NULL;
671 newbuf = strdup(v[1]);
676 n = ssplit(buf, ":", v, SZ(v), 1, 1);
680 http->host = strdup(v[0]);
686 http->host = strdup(v[0]);
687 http->port = atoi(v[1]);
692 if (http->host == NULL)
694 free_http_request(http);
697 if (http->path == NULL)
699 http->path = strdup("");
705 /* here begins the family of parser functions that reformat header lines */
708 /*********************************************************************
712 * Description : This is called if a header matches a pattern to "crunch"
715 * 1 : v = Pointer to parsers structure, which basically holds
716 * headers (client or server) that we want to "crunch"
717 * 2 : s = header (from sed) to "crunch"
718 * 3 : csp = Current client state (buffers, headers, etc...)
720 * Returns : Always NULL.
722 *********************************************************************/
723 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
725 log_error(LOG_LEVEL_HEADER, "crunch!");
731 /*********************************************************************
733 * Function : content_type
735 * Description : Is this a text/.* or javascript MIME Type?
739 * 2 : s = header string we are "considering"
740 * 3 : csp = Current client state (buffers, headers, etc...)
742 * Returns : A duplicate string pointer to this header (ie. pass thru)
744 *********************************************************************/
745 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
747 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
748 csp->content_type = CT_TEXT;
749 else if (strstr(s, " image/gif"))
750 csp->content_type = CT_GIF;
752 csp->content_type = 0;
759 /*********************************************************************
761 * Function : content_length
763 * Description : Adjust Content-Length header if we modified
768 * 2 : s = header string we are "considering"
769 * 3 : csp = Current client state (buffers, headers, etc...)
771 * Returns : A duplicate string pointer to this header (ie. pass thru)
773 *********************************************************************/
774 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
776 if (csp->content_length != 0) /* Content has been modified */
778 char * s2 = (char *) zalloc(100);
779 sprintf(s2, "Content-Length: %d", csp->content_length);
781 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
792 /*********************************************************************
794 * Function : client_referrer
796 * Description : Handle the "referer" config setting properly.
801 * 2 : s = header (from sed) to "crunch"
802 * 3 : csp = Current client state (buffers, headers, etc...)
804 * Returns : NULL if crunched, or a malloc'ed string with the original
807 *********************************************************************/
808 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
812 #ifdef FEATURE_FORCE_LOAD
813 /* Since the referrer can include the prefix even
814 * even if the request itself is non-forced, we must
815 * clean it unconditionally
817 strclean(s, FORCE_PREFIX);
818 #endif /* def FEATURE_FORCE_LOAD */
821 csp->referrer = strdup(s);
822 #endif /* def FEATURE_TRUST */
825 * Are we sending referer?
827 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
832 newval = csp->action->string[ACTION_STRING_REFERER];
835 * Are we blocking referer?
837 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
839 log_error(LOG_LEVEL_HEADER, "crunch!");
844 * Are we forging referer?
846 if (0 == strcmpic(newval, "forge"))
849 * Forge a referer as http://[hostname:port of REQUEST]/
850 * to fool stupid checks for in-site links
852 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
853 s2 = strsav(NULL, "Referer: ");
854 s2 = strsav(s2, "http://");
855 s2 = strsav(s2, csp->http->hostport);
856 s2 = strsav(s2, "/");
861 * Have we got a fixed referer?
863 if (0 == strncmpic(newval, "http://", 7))
866 * We have a specific (fixed) referer we want to send.
870 log_error(LOG_LEVEL_HEADER, "modified");
872 s2 = strsav( NULL, "Referer: " );
873 s2 = strsav( s2, newval );
877 /* Should never get here! */
878 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
881 * Forge is probably the best default.
883 * Forge a referer as http://[hostname:port of REQUEST]/
884 * to fool stupid checks for in-site links
886 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
887 s2 = strsav(NULL, "Referer: ");
888 s2 = strsav(s2, "http://");
889 s2 = strsav(s2, csp->http->hostport);
890 s2 = strsav(s2, "/");
895 /*********************************************************************
897 * Function : client_uagent
899 * Description : Handle the "user-agent" config setting properly
900 * and remember its original value to enable browser
901 * bug workarounds. Called from `sed'.
905 * 2 : s = header (from sed) to "crunch"
906 * 3 : csp = Current client state (buffers, headers, etc...)
908 * Returns : A malloc'ed pointer to the default agent, or
909 * a malloc'ed string pointer to this header (ie. pass thru).
911 *********************************************************************/
912 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
917 /* Save the client's User-Agent: value */
920 csp->http->user_agent = strdup(s + 12);
923 #ifdef FEATURE_IMAGE_DETECT_MSIE
924 if (strstr (s, "MSIE "))
926 /* This is Microsoft Internet Explorer.
927 * Enable auto-detect.
929 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
931 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
933 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
938 newval = csp->action->string[ACTION_STRING_USER_AGENT];
944 log_error(LOG_LEVEL_HEADER, "modified");
946 s2 = strsav( NULL, "User-Agent: " );
947 s2 = strsav( s2, newval );
953 /*********************************************************************
955 * Function : client_ua
957 * Description : Handle "ua-" headers properly. Called from `sed'.
961 * 2 : s = header (from sed) to "crunch"
962 * 3 : csp = Current client state (buffers, headers, etc...)
964 * Returns : NULL if crunched, or a malloc'ed string to original header
966 *********************************************************************/
967 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
969 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
975 log_error(LOG_LEVEL_HEADER, "crunch!");
981 /*********************************************************************
983 * Function : client_from
985 * Description : Handle the "from" config setting properly.
990 * 2 : s = header (from sed) to "crunch"
991 * 3 : csp = Current client state (buffers, headers, etc...)
993 * Returns : NULL if crunched, or a malloc'ed string to
994 * modified/original header.
996 *********************************************************************/
997 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1002 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1007 newval = csp->action->string[ACTION_STRING_FROM];
1010 * Are we blocking referer?
1012 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1014 log_error(LOG_LEVEL_HEADER, "crunch!");
1018 log_error(LOG_LEVEL_HEADER, " modified");
1020 s2 = strsav( NULL, "From: " );
1021 s2 = strsav( s2, newval );
1027 /*********************************************************************
1029 * Function : client_send_cookie
1031 * Description : Handle the "cookie" header properly. Called from `sed'.
1032 * If cookie is accepted, add it to the cookie_list,
1033 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1036 * 1 : v = pattern of cookie `sed' found matching
1037 * 2 : s = header (from sed) to "crunch"
1038 * 3 : csp = Current client state (buffers, headers, etc...)
1040 * Returns : Always NULL.
1042 *********************************************************************/
1043 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1045 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1047 enlist(csp->cookie_list, s + v->len + 1);
1051 log_error(LOG_LEVEL_HEADER, " crunch!");
1055 * Always return NULL here. The cookie header
1056 * will be sent at the end of the header.
1063 /*********************************************************************
1065 * Function : client_x_forwarded
1067 * Description : Handle the "x-forwarded-for" config setting properly,
1068 * also used in the add_client_headers list. Called from `sed'.
1072 * 2 : s = header (from sed) to "crunch"
1073 * 3 : csp = Current client state (buffers, headers, etc...)
1075 * Returns : Always NULL.
1077 *********************************************************************/
1078 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1080 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1082 /* Save it so we can re-add it later */
1083 csp->x_forwarded = strdup(s);
1087 * Always return NULL, since this information
1088 * will be sent at the end of the header.
1095 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1096 /*********************************************************************
1098 * Function : client_accept
1100 * Description : Detect whether the client wants HTML or an image.
1101 * Clients do not always make this information available
1102 * in a sane way. Always passes the header through
1103 * the proxy unchanged.
1107 * 2 : s = Header string. Null terminated.
1108 * 3 : csp = Current client state (buffers, headers, etc...)
1110 * Returns : Duplicate of argument s.
1112 *********************************************************************/
1113 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1115 #ifdef FEATURE_IMAGE_DETECT_MSIE
1116 if (strstr (s, "image/gif"))
1118 /* Client will accept HTML. If this seems counterintuitive,
1121 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1125 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1127 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1132 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1136 /* the following functions add headers directly to the header list */
1139 /*********************************************************************
1141 * Function : client_cookie_adder
1143 * Description : Used in the add_client_headers list. Called from `sed'.
1146 * 1 : csp = Current client state (buffers, headers, etc...)
1150 *********************************************************************/
1151 void client_cookie_adder(struct client_state *csp)
1157 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1161 tmp = strsav(tmp, "; ");
1163 tmp = strsav(tmp, lst->str);
1166 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1170 tmp = strsav(tmp, "; ");
1173 if ((e = cookie_encode(lst->str)))
1175 tmp = strsav(tmp, e);
1184 ret = strdup("Cookie: ");
1185 ret = strsav(ret, tmp);
1186 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1187 enlist(csp->headers, ret);
1195 /*********************************************************************
1197 * Function : client_xtra_adder
1199 * Description : Used in the add_client_headers list. Called from `sed'.
1202 * 1 : csp = Current client state (buffers, headers, etc...)
1206 *********************************************************************/
1207 void client_xtra_adder(struct client_state *csp)
1211 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->next;
1212 lst ; lst = lst->next)
1214 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1215 enlist(csp->headers, lst->str);
1221 /*********************************************************************
1223 * Function : client_x_forwarded_adder
1225 * Description : Used in the add_client_headers list. Called from `sed'.
1228 * 1 : csp = Current client state (buffers, headers, etc...)
1232 *********************************************************************/
1233 void client_x_forwarded_adder(struct client_state *csp)
1237 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1242 if (csp->x_forwarded)
1244 p = strsav(p, csp->x_forwarded);
1245 p = strsav(p, ", ");
1246 p = strsav(p, csp->ip_addr_str);
1250 p = strsav(p, "X-Forwarded-For: ");
1251 p = strsav(p, csp->ip_addr_str);
1254 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1255 enlist(csp->headers, p);
1260 /*********************************************************************
1262 * Function : connection_close_adder
1264 * Description : Adds a "Connection: close" header to csp->headers
1265 * as a temporary fix for the needed but missing HTTP/1.1
1266 * support. Called from `sed'.
1267 * FIXME: This whole function shouldn't be neccessary!
1270 * 1 : csp = Current client state (buffers, headers, etc...)
1274 *********************************************************************/
1275 void connection_close_adder(struct client_state *csp)
1277 enlist(csp->headers, strdup("Connection: close"));
1282 /*********************************************************************
1284 * Function : server_set_cookie
1286 * Description : Handle the server "cookie" header properly.
1287 * Log cookie to the jar file. Then "crunch" it,
1288 * or accept it. Called from `sed'.
1291 * 1 : v = parser pattern that matched this header
1292 * 2 : s = header that matched this pattern
1293 * 3 : csp = Current client state (buffers, headers, etc...)
1295 * Returns : `crumble' or a newly malloc'ed string.
1297 *********************************************************************/
1298 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1300 #ifdef FEATURE_COOKIE_JAR
1301 if (csp->config->jar)
1303 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1305 #endif /* def FEATURE_COOKIE_JAR */
1307 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1309 return(crumble(v, s, csp));
1317 #ifdef FEATURE_FORCE_LOAD
1318 /*********************************************************************
1320 * Function : client_host
1322 * Description : Clean the FORCE_PREFIX out of the 'host' http
1323 * header, if we use force
1327 * 2 : s = header (from sed) to clean
1328 * 3 : csp = Current client state (buffers, headers, etc...)
1330 * Returns : A malloc'ed pointer to the cleaned host header
1332 *********************************************************************/
1333 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1335 char *cleanhost = strdup(s);
1338 strclean(cleanhost, FORCE_PREFIX);
1342 #endif /* def FEATURE_FORCE_LOAD */
1345 #ifdef FEATURE_FORCE_LOAD
1346 /*********************************************************************
1348 * Function : strclean
1350 * Description : In-Situ-Eliminate all occurances of substring in
1354 * 1 : string = string to clean
1355 * 2 : substring = substring to eliminate
1357 * Returns : Number of eliminations
1359 *********************************************************************/
1360 int strclean(const char *string, const char *substring)
1362 int hits = 0, len = strlen(substring);
1365 while((pos = strstr(string, substring)))
1372 while (*p++ != '\0');
1379 #endif /* def FEATURE_FORCE_LOAD */