1 const char parsers_rcs[] = "$Id: parsers.c,v 1.26 2001/09/16 17:05:14 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.26 2001/09/16 17:05:14 jongfoster
45 * Removing unused #include showarg.h
47 * Revision 1.25 2001/09/16 13:21:27 jongfoster
48 * Changes to use new list functions.
50 * Revision 1.24 2001/09/13 23:05:50 jongfoster
51 * Changing the string paramater to the header parsers a "const".
53 * Revision 1.23 2001/09/12 18:08:19 steudten
55 * In parse_http_request() header rewriting miss the host value, so
56 * from http://www.mydomain.com the result was just " / " not
57 * http://www.mydomain.com/ in case we forward.
59 * Revision 1.22 2001/09/10 10:58:53 oes
60 * Silenced compiler warnings
62 * Revision 1.21 2001/07/31 14:46:00 oes
63 * - Persistant connections now suppressed
64 * - sed() no longer appends empty header to csp->headers
66 * Revision 1.20 2001/07/30 22:08:36 jongfoster
67 * Tidying up #defines:
68 * - All feature #defines are now of the form FEATURE_xxx
69 * - Permanently turned off WIN_GUI_EDIT
70 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
72 * Revision 1.19 2001/07/25 17:21:54 oes
73 * client_uagent now saves copy of User-Agent: header value
75 * Revision 1.18 2001/07/13 14:02:46 oes
76 * - Included fix to repair broken HTTP requests that
77 * don't contain a path, not even '/'.
78 * - Removed all #ifdef PCRS
79 * - content_type now always inspected and classified as
81 * - formatting / comments
83 * Revision 1.17 2001/06/29 21:45:41 oes
84 * Indentation, CRLF->LF, Tab-> Space
86 * Revision 1.16 2001/06/29 13:32:42 oes
88 * - Adapted free_http_request
89 * - Removed logentry from cancelled commit
91 * Revision 1.15 2001/06/03 19:12:38 oes
92 * deleted const struct interceptors
94 * Revision 1.14 2001/06/01 18:49:17 jongfoster
95 * Replaced "list_share" with "list" - the tiny memory gain was not
96 * worth the extra complexity.
98 * Revision 1.13 2001/05/31 21:30:33 jongfoster
99 * Removed list code - it's now in list.[ch]
100 * Renamed "permission" to "action", and changed many features
101 * to use the actions file rather than the global config.
103 * Revision 1.12 2001/05/31 17:33:13 oes
107 * Revision 1.11 2001/05/29 20:11:19 joergs
108 * '/ * inside comment' warning removed.
110 * Revision 1.10 2001/05/29 09:50:24 jongfoster
111 * Unified blocklist/imagelist/permissionslist.
112 * File format is still under discussion, but the internal changes
115 * Also modified interceptor behaviour:
116 * - We now intercept all URLs beginning with one of the following
117 * prefixes (and *only* these prefixes):
119 * * http://ijbswa.sf.net/config/
120 * * http://ijbswa.sourceforge.net/config/
121 * - New interceptors "home page" - go to http://i.j.b/ to see it.
122 * - Internal changes so that intercepted and fast redirect pages
123 * are not replaced with an image.
124 * - Interceptors now have the option to send a binary page direct
125 * to the client. (i.e. ijb-send-banner uses this)
126 * - Implemented show-url-info interceptor. (Which is why I needed
127 * the above interceptors changes - a typical URL is
128 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
129 * The previous mechanism would not have intercepted that, and
130 * if it had been intercepted then it then it would have replaced
133 * Revision 1.9 2001/05/28 17:26:33 jongfoster
134 * Fixing segfault if last header was crunched.
135 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
136 * can use the cross-platform sprintf() instead.)
138 * Revision 1.8 2001/05/27 22:17:04 oes
140 * - re_process_buffer no longer writes the modified buffer
141 * to the client, which was very ugly. It now returns the
142 * buffer, which it is then written by chat.
144 * - content_length now adjusts the Content-Length: header
145 * for modified documents rather than crunch()ing it.
146 * (Length info in csp->content_length, which is 0 for
147 * unmodified documents)
149 * - For this to work, sed() is called twice when filtering.
151 * Revision 1.7 2001/05/27 13:19:06 oes
152 * Patched Joergs solution for the content-length in.
154 * Revision 1.6 2001/05/26 13:39:32 jongfoster
155 * Only crunches Content-Length header if applying RE filtering.
156 * Without this fix, Microsoft Windows Update wouldn't work.
158 * Revision 1.5 2001/05/26 00:28:36 jongfoster
159 * Automatic reloading of config file.
160 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
161 * Most of the global variables have been moved to a new
162 * struct configuration_spec, accessed through csp->config->globalname
163 * Most of the globals remaining are used by the Win32 GUI.
165 * Revision 1.4 2001/05/22 18:46:04 oes
167 * - Enabled filtering banners by size rather than URL
168 * by adding patterns that replace all standard banner
169 * sizes with the "Junkbuster" gif to the re_filterfile
171 * - Enabled filtering WebBugs by providing a pattern
172 * which kills all 1x1 images
174 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
175 * which is selected by the (nonstandard and therefore
176 * capital) letter 'U' in the option string.
177 * It causes the quantifiers to be ungreedy by default.
178 * Appending a ? turns back to greedy (!).
180 * - Added a new interceptor ijb-send-banner, which
181 * sends back the "Junkbuster" gif. Without imagelist or
182 * MSIE detection support, or if tinygif = 1, or the
183 * URL isn't recognized as an imageurl, a lame HTML
184 * explanation is sent instead.
186 * - Added new feature, which permits blocking remote
187 * script redirects and firing back a local redirect
189 * The feature is conditionally compiled, i.e. it
190 * can be disabled with --disable-fast-redirects,
191 * plus it must be activated by a "fast-redirects"
192 * line in the config file, has its own log level
193 * and of course wants to be displayed by show-proxy-args
194 * Note: Boy, all the #ifdefs in 1001 locations and
195 * all the fumbling with configure.in and acconfig.h
196 * were *way* more work than the feature itself :-(
198 * - Because a generic redirect template was needed for
199 * this, tinygif = 3 now uses the same.
201 * - Moved GIFs, and other static HTTP response templates
206 * - Removed some >400 CRs again (Jon, you really worked
209 * Revision 1.3 2001/05/20 01:21:20 jongfoster
210 * Version 2.9.4 checkin.
211 * - Merged popupfile and cookiefile, and added control over PCRS
212 * filtering, in new "permissionsfile".
213 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
214 * file error you now get a message box (in the Win32 GUI) rather
215 * than the program exiting with no explanation.
216 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
218 * - Removed tabs from "config"
219 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
220 * - Bumped up version number.
222 * Revision 1.2 2001/05/17 23:02:36 oes
223 * - Made referrer option accept 'L' as a substitute for 'ยง'
225 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
226 * Initial import of version 2.9.3 source tree
229 *********************************************************************/
235 #include <sys/types.h>
253 #include "jbsockets.h"
254 #include "miscutil.h"
257 const char parsers_h_rcs[] = PARSERS_H_VERSION;
259 /* Fix a problem with Solaris. There should be no effect on other
261 * Solaris's isspace() is a macro which uses it's argument directly
262 * as an array index. Therefore we need to make sure that high-bit
263 * characters generate +ve values, and ideally we also want to make
264 * the argument match the declared parameter type of "int".
266 * Why did they write a character function that can't take a simple
267 * "char" argument? Doh!
269 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
270 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
273 const struct parsers client_patterns[] = {
274 { "referer:", 8, client_referrer },
275 { "user-agent:", 11, client_uagent },
276 { "ua-", 3, client_ua },
277 { "from:", 5, client_from },
278 { "cookie:", 7, client_send_cookie },
279 { "x-forwarded-for:", 16, client_x_forwarded },
280 #ifdef FEATURE_DENY_GZIP
281 { "Accept-Encoding: gzip", 21, crumble },
282 #endif /* def FEATURE_DENY_GZIP */
283 #if defined(FEATURE_IMAGE_DETECT_MSIE)
284 { "Accept:", 7, client_accept },
285 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
286 #ifdef FEATURE_FORCE_LOAD
287 { "Host:", 5, client_host },
288 #endif /* def FEATURE_FORCE_LOAD */
289 /* { "if-modified-since:", 18, crumble }, */
290 { "Keep-Alive:", 11, crumble },
291 { "connection:", 11, crumble },
292 { "proxy-connection:", 17, crumble },
297 const struct parsers server_patterns[] = {
298 { "set-cookie:", 11, server_set_cookie },
299 { "connection:", 11, crumble },
300 { "Content-Type:", 13, content_type },
301 { "Content-Length:", 15, content_length },
306 void (* const add_client_headers[])(struct client_state *) = {
308 client_x_forwarded_adder,
310 connection_close_adder,
315 void (* const add_server_headers[])(struct client_state *) = {
316 connection_close_adder,
321 /*********************************************************************
323 * Function : flush_socket
325 * Description : Write any pending "buffered" content.
328 * 1 : fd = file descriptor of the socket to read
329 * 2 : csp = Current client state (buffers, headers, etc...)
331 * Returns : On success, the number of bytes written are returned (zero
332 * indicates nothing was written). On error, -1 is returned,
333 * and errno is set appropriately. If count is zero and the
334 * file descriptor refers to a regular file, 0 will be
335 * returned without causing any other effect. For a special
336 * file, the results are not portable.
338 *********************************************************************/
339 int flush_socket(int fd, struct client_state *csp)
341 struct iob *iob = csp->iob;
342 int n = iob->eod - iob->cur;
349 n = write_socket(fd, iob->cur, n);
350 iob->eod = iob->cur = iob->buf;
356 /*********************************************************************
358 * Function : add_to_iob
360 * Description : Add content to the buffered page.
363 * 1 : csp = Current client state (buffers, headers, etc...)
364 * 2 : buf = holds the content to be added to the page
365 * 3 : n = number of bytes to be added
367 * Returns : Number of bytes in the content buffer.
369 *********************************************************************/
370 int add_to_iob(struct client_state *csp, char *buf, int n)
372 struct iob *iob = csp->iob;
376 have = iob->eod - iob->cur;
385 if ((p = (char *)malloc(need + 1)) == NULL)
387 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
393 /* there is something in the buffer - save it */
394 memcpy(p, iob->cur, have);
396 /* replace the buffer with the new space */
400 /* point to the end of the data */
405 /* the buffer is empty, free it and reinitialize */
410 /* copy the new data into the iob buffer */
413 /* point to the end of the data */
416 /* null terminate == cheap insurance */
419 /* set the pointers to the new values */
428 /*********************************************************************
430 * Function : get_header
432 * Description : This (odd) routine will parse the csp->iob
435 * 1 : csp = Current client state (buffers, headers, etc...)
437 * Returns : Any one of the following:
439 * 1) a pointer to a dynamically allocated string that contains a header line
440 * 2) NULL indicating that the end of the header was reached
441 * 3) "" indicating that the end of the iob was reached before finding
442 * a complete header line.
444 *********************************************************************/
445 char *get_header(struct client_state *csp)
451 if ((iob->cur == NULL)
452 || ((p = strchr(iob->cur, '\n')) == NULL))
454 return(""); /* couldn't find a complete header */
459 ret = strdup(iob->cur);
463 if ((q = strchr(ret, '\r'))) *q = '\0';
465 /* is this a blank linke (i.e. the end of the header) ? */
477 /*********************************************************************
481 * Description : add, delete or modify lines in the HTTP header streams.
482 * On entry, it receives a linked list of headers space
483 * that was allocated dynamically (both the list nodes
484 * and the header contents).
486 * As a side effect it frees the space used by the original
490 * 1 : pats = list of patterns to match against headers
491 * 2 : more_headers = list of functions to add more
492 * headers (client or server)
493 * 3 : csp = Current client state (buffers, headers, etc...)
495 * Returns : Single pointer to a fully formed header.
497 *********************************************************************/
498 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
500 struct list_entry *p;
501 const struct parsers *v;
505 for (v = pats; v->str ; v++)
507 for (p = csp->headers->first; p ; p = p->next)
509 /* Header crunch()ed in previous run? -> ignore */
510 if (p->str == NULL) continue;
512 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
514 if (strncmpic(p->str, v->str, v->len) == 0)
516 hdr = v->parser(v, p->str, csp);
517 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
523 /* place any additional headers on the csp->headers list */
524 for (f = more_headers; *f ; f++)
529 hdr = list_to_text(csp->headers);
536 /*********************************************************************
538 * Function : free_http_request
540 * Description : Freez a http_request structure
543 * 1 : http = points to a http_request structure to free
547 *********************************************************************/
548 void free_http_request(struct http_request *http)
553 freez(http->hostport);
556 freez(http->host_ip_addr_str);
557 freez(http->user_agent);
562 /*********************************************************************
564 * Function : parse_http_request
566 * Description : Parse out the host and port from the URL. Find the
567 * hostname & path, port (if ':'), and/or password (if '@')
570 * 1 : req = URL (or is it URI?) to break down
571 * 2 : http = pointer to the http structure to hold elements
572 * 3 : csp = Current client state (buffers, headers, etc...)
576 *********************************************************************/
577 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
579 char *buf, *v[10], *url, *p, *save_url;
582 memset(http, '\0', sizeof(*http));
584 http->cmd = strdup(req);
587 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
591 /* this could be a CONNECT request */
592 if (strcmpic(v[0], "connect") == 0)
595 http->gpc = strdup(v[0]);
596 http->hostport = strdup(v[1]);
597 http->ver = strdup(v[2]);
600 /* or it could be a GET or a POST (possibly webDAV too) */
601 if ((0 == strcmpic(v[0], "get"))
602 || (0 == strcmpic(v[0], "head"))
603 || (0 == strcmpic(v[0], "post"))
604 /* These are the headers as defined in RFC2518 to add webDAV support: */
605 || (0 == strcmpic(v[0], "propfind"))
606 || (0 == strcmpic(v[0], "proppatch"))
607 || (0 == strcmpic(v[0], "move"))
608 || (0 == strcmpic(v[0], "copy"))
609 || (0 == strcmpic(v[0], "mkcol"))
610 || (0 == strcmpic(v[0], "lock"))
611 || (0 == strcmpic(v[0], "unlock"))
615 http->gpc = strdup(v[0]);
617 http->ver = strdup(v[2]);
620 if (strncmpic(url, "http://", 7) == 0)
624 else if (strncmpic(url, "https://", 8) == 0)
635 if ((p = strchr(url, '/')))
637 http->path = strdup(p);
639 http->hostport = strdup(url);
642 * Repair broken HTTP requests that don't contain a path
646 /* Repair hostport & path */
647 http->path = strdup("/");
648 http->hostport = strdup(url);
650 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
652 http->cmd = strsav(http->cmd, http->gpc);
653 http->cmd = strsav(http->cmd, " ");
654 http->cmd = strsav(http->cmd, save_url);
655 http->cmd = strsav(http->cmd, "/ ");
656 http->cmd = strsav(http->cmd, http->ver);
665 if (http->hostport == NULL)
667 free_http_request(http);
671 buf = strdup(http->hostport);
674 /* check if url contains password */
675 n = ssplit(buf, "@", v, SZ(v), 1, 1);
678 char * newbuf = NULL;
679 newbuf = strdup(v[1]);
684 n = ssplit(buf, ":", v, SZ(v), 1, 1);
688 http->host = strdup(v[0]);
694 http->host = strdup(v[0]);
695 http->port = atoi(v[1]);
700 if (http->host == NULL)
702 free_http_request(http);
705 if (http->path == NULL)
707 http->path = strdup("");
713 /* here begins the family of parser functions that reformat header lines */
716 /*********************************************************************
720 * Description : This is called if a header matches a pattern to "crunch"
723 * 1 : v = Pointer to parsers structure, which basically holds
724 * headers (client or server) that we want to "crunch"
725 * 2 : s = header (from sed) to "crunch"
726 * 3 : csp = Current client state (buffers, headers, etc...)
728 * Returns : Always NULL.
730 *********************************************************************/
731 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
733 log_error(LOG_LEVEL_HEADER, "crunch!");
739 /*********************************************************************
741 * Function : content_type
743 * Description : Is this a text/.* or javascript MIME Type?
747 * 2 : s = header string we are "considering"
748 * 3 : csp = Current client state (buffers, headers, etc...)
750 * Returns : A duplicate string pointer to this header (ie. pass thru)
752 *********************************************************************/
753 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
755 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
756 csp->content_type = CT_TEXT;
757 else if (strstr(s, " image/gif"))
758 csp->content_type = CT_GIF;
760 csp->content_type = 0;
767 /*********************************************************************
769 * Function : content_length
771 * Description : Adjust Content-Length header if we modified
776 * 2 : s = header string we are "considering"
777 * 3 : csp = Current client state (buffers, headers, etc...)
779 * Returns : A duplicate string pointer to this header (ie. pass thru)
781 *********************************************************************/
782 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
784 if (csp->content_length != 0) /* Content has been modified */
786 char * s2 = (char *) zalloc(100);
787 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
789 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
800 /*********************************************************************
802 * Function : client_referrer
804 * Description : Handle the "referer" config setting properly.
809 * 2 : s = header (from sed) to "crunch"
810 * 3 : csp = Current client state (buffers, headers, etc...)
812 * Returns : NULL if crunched, or a malloc'ed string with the original
815 *********************************************************************/
816 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
820 #ifdef FEATURE_FORCE_LOAD
821 /* Since the referrer can include the prefix even
822 * even if the request itself is non-forced, we must
823 * clean it unconditionally
825 strclean(s, FORCE_PREFIX);
826 #endif /* def FEATURE_FORCE_LOAD */
829 csp->referrer = strdup(s);
830 #endif /* def FEATURE_TRUST */
833 * Are we sending referer?
835 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
840 newval = csp->action->string[ACTION_STRING_REFERER];
843 * Are we blocking referer?
845 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
847 log_error(LOG_LEVEL_HEADER, "crunch!");
852 * Are we forging referer?
854 if (0 == strcmpic(newval, "forge"))
857 * Forge a referer as http://[hostname:port of REQUEST]/
858 * to fool stupid checks for in-site links
860 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
861 s2 = strsav(NULL, "Referer: ");
862 s2 = strsav(s2, "http://");
863 s2 = strsav(s2, csp->http->hostport);
864 s2 = strsav(s2, "/");
869 * Have we got a fixed referer?
871 if (0 == strncmpic(newval, "http://", 7))
874 * We have a specific (fixed) referer we want to send.
878 log_error(LOG_LEVEL_HEADER, "modified");
880 s3 = strsav( NULL, "Referer: " );
881 s3 = strsav( s3, newval );
885 /* Should never get here! */
886 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
889 * Forge is probably the best default.
891 * Forge a referer as http://[hostname:port of REQUEST]/
892 * to fool stupid checks for in-site links
894 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
895 s2 = strsav(NULL, "Referer: ");
896 s2 = strsav(s2, "http://");
897 s2 = strsav(s2, csp->http->hostport);
898 s2 = strsav(s2, "/");
903 /*********************************************************************
905 * Function : client_uagent
907 * Description : Handle the "user-agent" config setting properly
908 * and remember its original value to enable browser
909 * bug workarounds. Called from `sed'.
913 * 2 : s = header (from sed) to "crunch"
914 * 3 : csp = Current client state (buffers, headers, etc...)
916 * Returns : A malloc'ed pointer to the default agent, or
917 * a malloc'ed string pointer to this header (ie. pass thru).
919 *********************************************************************/
920 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
925 /* Save the client's User-Agent: value */
928 csp->http->user_agent = strdup(s + 12);
931 #ifdef FEATURE_IMAGE_DETECT_MSIE
932 if (strstr (s, "MSIE "))
934 /* This is Microsoft Internet Explorer.
935 * Enable auto-detect.
937 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
939 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
941 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
946 newval = csp->action->string[ACTION_STRING_USER_AGENT];
952 log_error(LOG_LEVEL_HEADER, "modified");
954 s2 = strsav( NULL, "User-Agent: " );
955 s2 = strsav( s2, newval );
961 /*********************************************************************
963 * Function : client_ua
965 * Description : Handle "ua-" headers properly. Called from `sed'.
969 * 2 : s = header (from sed) to "crunch"
970 * 3 : csp = Current client state (buffers, headers, etc...)
972 * Returns : NULL if crunched, or a malloc'ed string to original header
974 *********************************************************************/
975 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
977 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
983 log_error(LOG_LEVEL_HEADER, "crunch!");
989 /*********************************************************************
991 * Function : client_from
993 * Description : Handle the "from" config setting properly.
998 * 2 : s = header (from sed) to "crunch"
999 * 3 : csp = Current client state (buffers, headers, etc...)
1001 * Returns : NULL if crunched, or a malloc'ed string to
1002 * modified/original header.
1004 *********************************************************************/
1005 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1007 const char * newval;
1010 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1015 newval = csp->action->string[ACTION_STRING_FROM];
1018 * Are we blocking referer?
1020 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1022 log_error(LOG_LEVEL_HEADER, "crunch!");
1026 log_error(LOG_LEVEL_HEADER, " modified");
1028 s2 = strsav( NULL, "From: " );
1029 s2 = strsav( s2, newval );
1035 /*********************************************************************
1037 * Function : client_send_cookie
1039 * Description : Handle the "cookie" header properly. Called from `sed'.
1040 * If cookie is accepted, add it to the cookie_list,
1041 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1044 * 1 : v = pattern of cookie `sed' found matching
1045 * 2 : s = header (from sed) to "crunch"
1046 * 3 : csp = Current client state (buffers, headers, etc...)
1048 * Returns : Always NULL.
1050 *********************************************************************/
1051 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1053 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1055 enlist(csp->cookie_list, s + v->len + 1);
1059 log_error(LOG_LEVEL_HEADER, " crunch!");
1063 * Always return NULL here. The cookie header
1064 * will be sent at the end of the header.
1071 /*********************************************************************
1073 * Function : client_x_forwarded
1075 * Description : Handle the "x-forwarded-for" config setting properly,
1076 * also used in the add_client_headers list. Called from `sed'.
1080 * 2 : s = header (from sed) to "crunch"
1081 * 3 : csp = Current client state (buffers, headers, etc...)
1083 * Returns : Always NULL.
1085 *********************************************************************/
1086 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1088 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1090 /* Save it so we can re-add it later */
1091 csp->x_forwarded = strdup(s);
1095 * Always return NULL, since this information
1096 * will be sent at the end of the header.
1103 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1104 /*********************************************************************
1106 * Function : client_accept
1108 * Description : Detect whether the client wants HTML or an image.
1109 * Clients do not always make this information available
1110 * in a sane way. Always passes the header through
1111 * the proxy unchanged.
1115 * 2 : s = Header string. Null terminated.
1116 * 3 : csp = Current client state (buffers, headers, etc...)
1118 * Returns : Duplicate of argument s.
1120 *********************************************************************/
1121 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1123 #ifdef FEATURE_IMAGE_DETECT_MSIE
1124 if (strstr (s, "image/gif"))
1126 /* Client will accept HTML. If this seems counterintuitive,
1129 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1133 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1135 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1140 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1144 /* the following functions add headers directly to the header list */
1147 /*********************************************************************
1149 * Function : client_cookie_adder
1151 * Description : Used in the add_client_headers list. Called from `sed'.
1154 * 1 : csp = Current client state (buffers, headers, etc...)
1158 *********************************************************************/
1159 void client_cookie_adder(struct client_state *csp)
1161 struct list_entry *lst;
1165 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1169 tmp = strsav(tmp, "; ");
1171 tmp = strsav(tmp, lst->str);
1174 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1178 tmp = strsav(tmp, "; ");
1181 if ((e = cookie_encode(lst->str)))
1183 tmp = strsav(tmp, e);
1192 ret = strdup("Cookie: ");
1193 ret = strsav(ret, tmp);
1194 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1195 enlist(csp->headers, ret);
1203 /*********************************************************************
1205 * Function : client_xtra_adder
1207 * Description : Used in the add_client_headers list. Called from `sed'.
1210 * 1 : csp = Current client state (buffers, headers, etc...)
1214 *********************************************************************/
1215 void client_xtra_adder(struct client_state *csp)
1217 struct list_entry *lst;
1219 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1220 lst ; lst = lst->next)
1222 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1223 enlist(csp->headers, lst->str);
1229 /*********************************************************************
1231 * Function : client_x_forwarded_adder
1233 * Description : Used in the add_client_headers list. Called from `sed'.
1236 * 1 : csp = Current client state (buffers, headers, etc...)
1240 *********************************************************************/
1241 void client_x_forwarded_adder(struct client_state *csp)
1245 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1250 if (csp->x_forwarded)
1252 p = strsav(p, csp->x_forwarded);
1253 p = strsav(p, ", ");
1254 p = strsav(p, csp->ip_addr_str);
1258 p = strsav(p, "X-Forwarded-For: ");
1259 p = strsav(p, csp->ip_addr_str);
1262 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1263 enlist(csp->headers, p);
1268 /*********************************************************************
1270 * Function : connection_close_adder
1272 * Description : Adds a "Connection: close" header to csp->headers
1273 * as a temporary fix for the needed but missing HTTP/1.1
1274 * support. Called from `sed'.
1275 * FIXME: This whole function shouldn't be neccessary!
1278 * 1 : csp = Current client state (buffers, headers, etc...)
1282 *********************************************************************/
1283 void connection_close_adder(struct client_state *csp)
1285 enlist(csp->headers, strdup("Connection: close"));
1290 /*********************************************************************
1292 * Function : server_set_cookie
1294 * Description : Handle the server "cookie" header properly.
1295 * Log cookie to the jar file. Then "crunch" it,
1296 * or accept it. Called from `sed'.
1299 * 1 : v = parser pattern that matched this header
1300 * 2 : s = header that matched this pattern
1301 * 3 : csp = Current client state (buffers, headers, etc...)
1303 * Returns : `crumble' or a newly malloc'ed string.
1305 *********************************************************************/
1306 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1308 #ifdef FEATURE_COOKIE_JAR
1309 if (csp->config->jar)
1311 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1313 #endif /* def FEATURE_COOKIE_JAR */
1315 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1317 return(crumble(v, s, csp));
1325 #ifdef FEATURE_FORCE_LOAD
1326 /*********************************************************************
1328 * Function : client_host
1330 * Description : Clean the FORCE_PREFIX out of the 'host' http
1331 * header, if we use force
1335 * 2 : s = header (from sed) to clean
1336 * 3 : csp = Current client state (buffers, headers, etc...)
1338 * Returns : A malloc'ed pointer to the cleaned host header
1340 *********************************************************************/
1341 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1343 char *cleanhost = strdup(s);
1346 strclean(cleanhost, FORCE_PREFIX);
1350 #endif /* def FEATURE_FORCE_LOAD */
1353 #ifdef FEATURE_FORCE_LOAD
1354 /*********************************************************************
1356 * Function : strclean
1358 * Description : In-Situ-Eliminate all occurances of substring in
1362 * 1 : string = string to clean
1363 * 2 : substring = substring to eliminate
1365 * Returns : Number of eliminations
1367 *********************************************************************/
1368 int strclean(const char *string, const char *substring)
1370 int hits = 0, len = strlen(substring);
1373 while((pos = strstr(string, substring)))
1380 while (*p++ != '\0');
1387 #endif /* def FEATURE_FORCE_LOAD */