1 const char parsers_rcs[] = "$Id: parsers.c,v 1.22 2001/09/10 10:58:53 oes 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.22 2001/09/10 10:58:53 oes
45 * Silenced compiler warnings
47 * Revision 1.21 2001/07/31 14:46:00 oes
48 * - Persistant connections now suppressed
49 * - sed() no longer appends empty header to csp->headers
51 * Revision 1.20 2001/07/30 22:08:36 jongfoster
52 * Tidying up #defines:
53 * - All feature #defines are now of the form FEATURE_xxx
54 * - Permanently turned off WIN_GUI_EDIT
55 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
57 * Revision 1.19 2001/07/25 17:21:54 oes
58 * client_uagent now saves copy of User-Agent: header value
60 * Revision 1.18 2001/07/13 14:02:46 oes
61 * - Included fix to repair broken HTTP requests that
62 * don't contain a path, not even '/'.
63 * - Removed all #ifdef PCRS
64 * - content_type now always inspected and classified as
66 * - formatting / comments
68 * Revision 1.17 2001/06/29 21:45:41 oes
69 * Indentation, CRLF->LF, Tab-> Space
71 * Revision 1.16 2001/06/29 13:32:42 oes
73 * - Adapted free_http_request
74 * - Removed logentry from cancelled commit
76 * Revision 1.15 2001/06/03 19:12:38 oes
77 * deleted const struct interceptors
79 * Revision 1.14 2001/06/01 18:49:17 jongfoster
80 * Replaced "list_share" with "list" - the tiny memory gain was not
81 * worth the extra complexity.
83 * Revision 1.13 2001/05/31 21:30:33 jongfoster
84 * Removed list code - it's now in list.[ch]
85 * Renamed "permission" to "action", and changed many features
86 * to use the actions file rather than the global config.
88 * Revision 1.12 2001/05/31 17:33:13 oes
92 * Revision 1.11 2001/05/29 20:11:19 joergs
93 * '/ * inside comment' warning removed.
95 * Revision 1.10 2001/05/29 09:50:24 jongfoster
96 * Unified blocklist/imagelist/permissionslist.
97 * File format is still under discussion, but the internal changes
100 * Also modified interceptor behaviour:
101 * - We now intercept all URLs beginning with one of the following
102 * prefixes (and *only* these prefixes):
104 * * http://ijbswa.sf.net/config/
105 * * http://ijbswa.sourceforge.net/config/
106 * - New interceptors "home page" - go to http://i.j.b/ to see it.
107 * - Internal changes so that intercepted and fast redirect pages
108 * are not replaced with an image.
109 * - Interceptors now have the option to send a binary page direct
110 * to the client. (i.e. ijb-send-banner uses this)
111 * - Implemented show-url-info interceptor. (Which is why I needed
112 * the above interceptors changes - a typical URL is
113 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
114 * The previous mechanism would not have intercepted that, and
115 * if it had been intercepted then it then it would have replaced
118 * Revision 1.9 2001/05/28 17:26:33 jongfoster
119 * Fixing segfault if last header was crunched.
120 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
121 * can use the cross-platform sprintf() instead.)
123 * Revision 1.8 2001/05/27 22:17:04 oes
125 * - re_process_buffer no longer writes the modified buffer
126 * to the client, which was very ugly. It now returns the
127 * buffer, which it is then written by chat.
129 * - content_length now adjusts the Content-Length: header
130 * for modified documents rather than crunch()ing it.
131 * (Length info in csp->content_length, which is 0 for
132 * unmodified documents)
134 * - For this to work, sed() is called twice when filtering.
136 * Revision 1.7 2001/05/27 13:19:06 oes
137 * Patched Joergs solution for the content-length in.
139 * Revision 1.6 2001/05/26 13:39:32 jongfoster
140 * Only crunches Content-Length header if applying RE filtering.
141 * Without this fix, Microsoft Windows Update wouldn't work.
143 * Revision 1.5 2001/05/26 00:28:36 jongfoster
144 * Automatic reloading of config file.
145 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
146 * Most of the global variables have been moved to a new
147 * struct configuration_spec, accessed through csp->config->globalname
148 * Most of the globals remaining are used by the Win32 GUI.
150 * Revision 1.4 2001/05/22 18:46:04 oes
152 * - Enabled filtering banners by size rather than URL
153 * by adding patterns that replace all standard banner
154 * sizes with the "Junkbuster" gif to the re_filterfile
156 * - Enabled filtering WebBugs by providing a pattern
157 * which kills all 1x1 images
159 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
160 * which is selected by the (nonstandard and therefore
161 * capital) letter 'U' in the option string.
162 * It causes the quantifiers to be ungreedy by default.
163 * Appending a ? turns back to greedy (!).
165 * - Added a new interceptor ijb-send-banner, which
166 * sends back the "Junkbuster" gif. Without imagelist or
167 * MSIE detection support, or if tinygif = 1, or the
168 * URL isn't recognized as an imageurl, a lame HTML
169 * explanation is sent instead.
171 * - Added new feature, which permits blocking remote
172 * script redirects and firing back a local redirect
174 * The feature is conditionally compiled, i.e. it
175 * can be disabled with --disable-fast-redirects,
176 * plus it must be activated by a "fast-redirects"
177 * line in the config file, has its own log level
178 * and of course wants to be displayed by show-proxy-args
179 * Note: Boy, all the #ifdefs in 1001 locations and
180 * all the fumbling with configure.in and acconfig.h
181 * were *way* more work than the feature itself :-(
183 * - Because a generic redirect template was needed for
184 * this, tinygif = 3 now uses the same.
186 * - Moved GIFs, and other static HTTP response templates
191 * - Removed some >400 CRs again (Jon, you really worked
194 * Revision 1.3 2001/05/20 01:21:20 jongfoster
195 * Version 2.9.4 checkin.
196 * - Merged popupfile and cookiefile, and added control over PCRS
197 * filtering, in new "permissionsfile".
198 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
199 * file error you now get a message box (in the Win32 GUI) rather
200 * than the program exiting with no explanation.
201 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
203 * - Removed tabs from "config"
204 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
205 * - Bumped up version number.
207 * Revision 1.2 2001/05/17 23:02:36 oes
208 * - Made referrer option accept 'L' as a substitute for 'ยง'
210 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
211 * Initial import of version 2.9.3 source tree
214 *********************************************************************/
220 #include <sys/types.h>
235 #include "showargs.h"
239 #include "jbsockets.h"
240 #include "miscutil.h"
243 const char parsers_h_rcs[] = PARSERS_H_VERSION;
245 /* Fix a problem with Solaris. There should be no effect on other
247 * Solaris's isspace() is a macro which uses it's argument directly
248 * as an array index. Therefore we need to make sure that high-bit
249 * characters generate +ve values, and ideally we also want to make
250 * the argument match the declared parameter type of "int".
252 * Why did they write a character function that can't take a simple
253 * "char" argument? Doh!
255 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
256 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
259 const struct parsers client_patterns[] = {
260 { "referer:", 8, client_referrer },
261 { "user-agent:", 11, client_uagent },
262 { "ua-", 3, client_ua },
263 { "from:", 5, client_from },
264 { "cookie:", 7, client_send_cookie },
265 { "x-forwarded-for:", 16, client_x_forwarded },
266 #ifdef FEATURE_DENY_GZIP
267 { "Accept-Encoding: gzip", 21, crumble },
268 #endif /* def FEATURE_DENY_GZIP */
269 #if defined(FEATURE_IMAGE_DETECT_MSIE)
270 { "Accept:", 7, client_accept },
271 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
272 #ifdef FEATURE_FORCE_LOAD
273 { "Host:", 5, client_host },
274 #endif /* def FEATURE_FORCE_LOAD */
275 /* { "if-modified-since:", 18, crumble }, */
276 { "Keep-Alive:", 11, crumble },
277 { "connection:", 11, crumble },
278 { "proxy-connection:", 17, crumble },
283 const struct parsers server_patterns[] = {
284 { "set-cookie:", 11, server_set_cookie },
285 { "connection:", 11, crumble },
286 { "Content-Type:", 13, content_type },
287 { "Content-Length:", 15, content_length },
292 void (* const add_client_headers[])(struct client_state *) = {
294 client_x_forwarded_adder,
296 connection_close_adder,
301 void (* const add_server_headers[])(struct client_state *) = {
302 connection_close_adder,
307 /*********************************************************************
309 * Function : flush_socket
311 * Description : Write any pending "buffered" content.
314 * 1 : fd = file descriptor of the socket to read
315 * 2 : csp = Current client state (buffers, headers, etc...)
317 * Returns : On success, the number of bytes written are returned (zero
318 * indicates nothing was written). On error, -1 is returned,
319 * and errno is set appropriately. If count is zero and the
320 * file descriptor refers to a regular file, 0 will be
321 * returned without causing any other effect. For a special
322 * file, the results are not portable.
324 *********************************************************************/
325 int flush_socket(int fd, struct client_state *csp)
327 struct iob *iob = csp->iob;
328 int n = iob->eod - iob->cur;
335 n = write_socket(fd, iob->cur, n);
336 iob->eod = iob->cur = iob->buf;
342 /*********************************************************************
344 * Function : add_to_iob
346 * Description : Add content to the buffered page.
349 * 1 : csp = Current client state (buffers, headers, etc...)
350 * 2 : buf = holds the content to be added to the page
351 * 3 : n = number of bytes to be added
353 * Returns : Number of bytes in the content buffer.
355 *********************************************************************/
356 int add_to_iob(struct client_state *csp, char *buf, int n)
358 struct iob *iob = csp->iob;
362 have = iob->eod - iob->cur;
371 if ((p = (char *)malloc(need + 1)) == NULL)
373 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
379 /* there is something in the buffer - save it */
380 memcpy(p, iob->cur, have);
382 /* replace the buffer with the new space */
386 /* point to the end of the data */
391 /* the buffer is empty, free it and reinitialize */
396 /* copy the new data into the iob buffer */
399 /* point to the end of the data */
402 /* null terminate == cheap insurance */
405 /* set the pointers to the new values */
414 /*********************************************************************
416 * Function : get_header
418 * Description : This (odd) routine will parse the csp->iob
421 * 1 : csp = Current client state (buffers, headers, etc...)
423 * Returns : Any one of the following:
425 * 1) a pointer to a dynamically allocated string that contains a header line
426 * 2) NULL indicating that the end of the header was reached
427 * 3) "" indicating that the end of the iob was reached before finding
428 * a complete header line.
430 *********************************************************************/
431 char *get_header(struct client_state *csp)
437 if ((iob->cur == NULL)
438 || ((p = strchr(iob->cur, '\n')) == NULL))
440 return(""); /* couldn't find a complete header */
445 ret = strdup(iob->cur);
449 if ((q = strchr(ret, '\r'))) *q = '\0';
451 /* is this a blank linke (i.e. the end of the header) ? */
463 /*********************************************************************
467 * Description : add, delete or modify lines in the HTTP header streams.
468 * On entry, it receives a linked list of headers space
469 * that was allocated dynamically (both the list nodes
470 * and the header contents).
472 * As a side effect it frees the space used by the original
476 * 1 : pats = list of patterns to match against headers
477 * 2 : more_headers = list of functions to add more
478 * headers (client or server)
479 * 3 : csp = Current client state (buffers, headers, etc...)
481 * Returns : Single pointer to a fully formed header.
483 *********************************************************************/
484 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
487 const struct parsers *v;
491 for (v = pats; v->str ; v++)
493 for (p = csp->headers->next; p ; p = p->next)
495 /* Header crunch()ed in previous run? -> ignore */
496 if (p->str == NULL) continue;
498 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
500 if (strncmpic(p->str, v->str, v->len) == 0)
502 hdr = v->parser(v, p->str, csp);
509 /* place any additional headers on the csp->headers list */
510 for (f = more_headers; *f ; f++)
515 hdr = list_to_text(csp->headers);
522 /*********************************************************************
524 * Function : free_http_request
526 * Description : Freez a http_request structure
529 * 1 : http = points to a http_request structure to free
533 *********************************************************************/
534 void free_http_request(struct http_request *http)
539 freez(http->hostport);
542 freez(http->host_ip_addr_str);
543 freez(http->user_agent);
548 /*********************************************************************
550 * Function : parse_http_request
552 * Description : Parse out the host and port from the URL. Find the
553 * hostname & path, port (if ':'), and/or password (if '@')
556 * 1 : req = URL (or is it URI?) to break down
557 * 2 : http = pointer to the http structure to hold elements
558 * 3 : csp = Current client state (buffers, headers, etc...)
562 *********************************************************************/
563 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
565 char *buf, *v[10], *url, *p, *save_url;
568 memset(http, '\0', sizeof(*http));
570 http->cmd = strdup(req);
573 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
577 /* this could be a CONNECT request */
578 if (strcmpic(v[0], "connect") == 0)
581 http->gpc = strdup(v[0]);
582 http->hostport = strdup(v[1]);
583 http->ver = strdup(v[2]);
586 /* or it could be a GET or a POST (possibly webDAV too) */
587 if ((0 == strcmpic(v[0], "get"))
588 || (0 == strcmpic(v[0], "head"))
589 || (0 == strcmpic(v[0], "post"))
590 /* These are the headers as defined in RFC2518 to add webDAV support: */
591 || (0 == strcmpic(v[0], "propfind"))
592 || (0 == strcmpic(v[0], "proppatch"))
593 || (0 == strcmpic(v[0], "move"))
594 || (0 == strcmpic(v[0], "copy"))
595 || (0 == strcmpic(v[0], "mkcol"))
596 || (0 == strcmpic(v[0], "lock"))
597 || (0 == strcmpic(v[0], "unlock"))
601 http->gpc = strdup(v[0]);
603 http->ver = strdup(v[2]);
606 if (strncmpic(url, "http://", 7) == 0)
610 else if (strncmpic(url, "https://", 8) == 0)
621 if ((p = strchr(url, '/')))
623 http->path = strdup(p);
625 http->hostport = strdup(url);
628 * Repair broken HTTP requests that don't contain a path
632 /* Repair hostport & path */
633 http->path = strdup("/");
634 http->hostport = strdup(url);
636 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
638 http->cmd = strsav(http->cmd, http->gpc);
639 http->cmd = strsav(http->cmd, " ");
640 http->cmd = strsav(http->cmd, save_url);
641 http->cmd = strsav(http->cmd, "/ ");
642 http->cmd = strsav(http->cmd, http->ver);
651 if (http->hostport == NULL)
653 free_http_request(http);
657 buf = strdup(http->hostport);
660 /* check if url contains password */
661 n = ssplit(buf, "@", v, SZ(v), 1, 1);
664 char * newbuf = NULL;
665 newbuf = strdup(v[1]);
670 n = ssplit(buf, ":", v, SZ(v), 1, 1);
674 http->host = strdup(v[0]);
680 http->host = strdup(v[0]);
681 http->port = atoi(v[1]);
686 if (http->host == NULL)
688 free_http_request(http);
691 if (http->path == NULL)
693 http->path = strdup("");
699 /* here begins the family of parser functions that reformat header lines */
702 /*********************************************************************
706 * Description : This is called if a header matches a pattern to "crunch"
709 * 1 : v = Pointer to parsers structure, which basically holds
710 * headers (client or server) that we want to "crunch"
711 * 2 : s = header (from sed) to "crunch"
712 * 3 : csp = Current client state (buffers, headers, etc...)
714 * Returns : Always NULL.
716 *********************************************************************/
717 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
719 log_error(LOG_LEVEL_HEADER, "crunch!");
725 /*********************************************************************
727 * Function : content_type
729 * Description : Is this a text/.* or javascript MIME Type?
733 * 2 : s = header string we are "considering"
734 * 3 : csp = Current client state (buffers, headers, etc...)
736 * Returns : A duplicate string pointer to this header (ie. pass thru)
738 *********************************************************************/
739 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
741 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
742 csp->content_type = CT_TEXT;
743 else if (strstr(s, " image/gif"))
744 csp->content_type = CT_GIF;
746 csp->content_type = 0;
753 /*********************************************************************
755 * Function : content_length
757 * Description : Adjust Content-Length header if we modified
762 * 2 : s = header string we are "considering"
763 * 3 : csp = Current client state (buffers, headers, etc...)
765 * Returns : A duplicate string pointer to this header (ie. pass thru)
767 *********************************************************************/
768 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
770 if (csp->content_length != 0) /* Content has been modified */
772 s = (char *) zalloc(100);
773 sprintf(s, "Content-Length: %d", csp->content_length);
775 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
786 /*********************************************************************
788 * Function : client_referrer
790 * Description : Handle the "referer" config setting properly.
795 * 2 : s = header (from sed) to "crunch"
796 * 3 : csp = Current client state (buffers, headers, etc...)
798 * Returns : NULL if crunched, or a malloc'ed string with the original
801 *********************************************************************/
802 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
805 #ifdef FEATURE_FORCE_LOAD
806 /* Since the referrer can include the prefix even
807 * even if the request itself is non-forced, we must
808 * clean it unconditionally
810 strclean(s, FORCE_PREFIX);
811 #endif /* def FEATURE_FORCE_LOAD */
814 csp->referrer = strdup(s);
815 #endif /* def FEATURE_TRUST */
818 * Are we sending referer?
820 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
825 newval = csp->action->string[ACTION_STRING_REFERER];
828 * Are we blocking referer?
830 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
832 log_error(LOG_LEVEL_HEADER, "crunch!");
837 * Are we forging referer?
839 if (0 == strcmpic(newval, "forge"))
842 * Forge a referer as http://[hostname:port of REQUEST]/
843 * to fool stupid checks for in-site links
845 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
846 s = strsav(NULL, "Referer: ");
847 s = strsav(s, "http://");
848 s = strsav(s, csp->http->hostport);
854 * Have we got a fixed referer?
856 if (0 == strncmpic(newval, "http://", 7))
859 * We have a specific (fixed) referer we want to send.
862 log_error(LOG_LEVEL_HEADER, "modified");
864 s = strsav( NULL, "Referer: " );
865 s = strsav( s, newval );
869 /* Should never get here! */
870 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
873 * Forge is probably the best default.
875 * Forge a referer as http://[hostname:port of REQUEST]/
876 * to fool stupid checks for in-site links
878 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
879 s = strsav(NULL, "Referer: ");
880 s = strsav(s, "http://");
881 s = strsav(s, csp->http->hostport);
887 /*********************************************************************
889 * Function : client_uagent
891 * Description : Handle the "user-agent" config setting properly
892 * and remember its original value to enable browser
893 * bug workarounds. Called from `sed'.
897 * 2 : s = header (from sed) to "crunch"
898 * 3 : csp = Current client state (buffers, headers, etc...)
900 * Returns : A malloc'ed pointer to the default agent, or
901 * a malloc'ed string pointer to this header (ie. pass thru).
903 *********************************************************************/
904 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
908 /* Save the client's User-Agent: value */
911 csp->http->user_agent = strdup(s + 12);
914 #ifdef FEATURE_IMAGE_DETECT_MSIE
915 if (strstr (s, "MSIE "))
917 /* This is Microsoft Internet Explorer.
918 * Enable auto-detect.
920 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
922 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
924 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
929 newval = csp->action->string[ACTION_STRING_USER_AGENT];
935 log_error(LOG_LEVEL_HEADER, "modified");
937 s = strsav( NULL, "User-Agent: " );
938 s = strsav( s, newval );
944 /*********************************************************************
946 * Function : client_ua
948 * Description : Handle "ua-" headers properly. Called from `sed'.
952 * 2 : s = header (from sed) to "crunch"
953 * 3 : csp = Current client state (buffers, headers, etc...)
955 * Returns : NULL if crunched, or a malloc'ed string to original header
957 *********************************************************************/
958 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
960 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
966 log_error(LOG_LEVEL_HEADER, "crunch!");
972 /*********************************************************************
974 * Function : client_from
976 * Description : Handle the "from" config setting properly.
981 * 2 : s = header (from sed) to "crunch"
982 * 3 : csp = Current client state (buffers, headers, etc...)
984 * Returns : NULL if crunched, or a malloc'ed string to
985 * modified/original header.
987 *********************************************************************/
988 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
992 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
997 newval = csp->action->string[ACTION_STRING_FROM];
1000 * Are we blocking referer?
1002 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1004 log_error(LOG_LEVEL_HEADER, "crunch!");
1008 log_error(LOG_LEVEL_HEADER, " modified");
1010 s = strsav( NULL, "From: " );
1011 s = strsav( s, newval );
1017 /*********************************************************************
1019 * Function : client_send_cookie
1021 * Description : Handle the "cookie" header properly. Called from `sed'.
1022 * If cookie is accepted, add it to the cookie_list,
1023 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1026 * 1 : v = pattern of cookie `sed' found matching
1027 * 2 : s = header (from sed) to "crunch"
1028 * 3 : csp = Current client state (buffers, headers, etc...)
1030 * Returns : Always NULL.
1032 *********************************************************************/
1033 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1035 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1037 enlist(csp->cookie_list, s + v->len + 1);
1041 log_error(LOG_LEVEL_HEADER, " crunch!");
1045 * Always return NULL here. The cookie header
1046 * will be sent at the end of the header.
1053 /*********************************************************************
1055 * Function : client_x_forwarded
1057 * Description : Handle the "x-forwarded-for" config setting properly,
1058 * also used in the add_client_headers list. Called from `sed'.
1062 * 2 : s = header (from sed) to "crunch"
1063 * 3 : csp = Current client state (buffers, headers, etc...)
1065 * Returns : Always NULL.
1067 *********************************************************************/
1068 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1070 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1072 /* Save it so we can re-add it later */
1073 csp->x_forwarded = strdup(s);
1077 * Always return NULL, since this information
1078 * will be sent at the end of the header.
1085 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1086 /*********************************************************************
1088 * Function : client_accept
1090 * Description : Detect whether the client wants HTML or an image.
1091 * Clients do not always make this information available
1092 * in a sane way. Always passes the header through
1093 * the proxy unchanged.
1097 * 2 : s = Header string. Null terminated.
1098 * 3 : csp = Current client state (buffers, headers, etc...)
1100 * Returns : Duplicate of argument s.
1102 *********************************************************************/
1103 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1105 #ifdef FEATURE_IMAGE_DETECT_MSIE
1106 if (strstr (s, "image/gif"))
1108 /* Client will accept HTML. If this seems counterintuitive,
1111 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1115 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1117 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1122 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1126 /* the following functions add headers directly to the header list */
1129 /*********************************************************************
1131 * Function : client_cookie_adder
1133 * Description : Used in the add_client_headers list. Called from `sed'.
1136 * 1 : csp = Current client state (buffers, headers, etc...)
1140 *********************************************************************/
1141 void client_cookie_adder(struct client_state *csp)
1147 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1151 tmp = strsav(tmp, "; ");
1153 tmp = strsav(tmp, lst->str);
1156 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1160 tmp = strsav(tmp, "; ");
1163 if ((e = cookie_encode(lst->str)))
1165 tmp = strsav(tmp, e);
1174 ret = strdup("Cookie: ");
1175 ret = strsav(ret, tmp);
1176 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1177 enlist(csp->headers, ret);
1185 /*********************************************************************
1187 * Function : client_xtra_adder
1189 * Description : Used in the add_client_headers list. Called from `sed'.
1192 * 1 : csp = Current client state (buffers, headers, etc...)
1196 *********************************************************************/
1197 void client_xtra_adder(struct client_state *csp)
1199 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1201 for (lst = lst->next; lst ; lst = lst->next)
1203 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1204 enlist(csp->headers, lst->str);
1210 /*********************************************************************
1212 * Function : client_x_forwarded_adder
1214 * Description : Used in the add_client_headers list. Called from `sed'.
1217 * 1 : csp = Current client state (buffers, headers, etc...)
1221 *********************************************************************/
1222 void client_x_forwarded_adder(struct client_state *csp)
1226 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1231 if (csp->x_forwarded)
1233 p = strsav(p, csp->x_forwarded);
1234 p = strsav(p, ", ");
1235 p = strsav(p, csp->ip_addr_str);
1239 p = strsav(p, "X-Forwarded-For: ");
1240 p = strsav(p, csp->ip_addr_str);
1243 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1244 enlist(csp->headers, p);
1249 /*********************************************************************
1251 * Function : connection_close_adder
1253 * Description : Adds a "Connection: close" header to csp->headers
1254 * as a temporary fix for the needed but missing HTTP/1.1
1255 * support. Called from `sed'.
1256 * FIXME: This whole function shouldn't be neccessary!
1259 * 1 : csp = Current client state (buffers, headers, etc...)
1263 *********************************************************************/
1264 void connection_close_adder(struct client_state *csp)
1266 enlist(csp->headers, strdup("Connection: close"));
1271 /*********************************************************************
1273 * Function : server_set_cookie
1275 * Description : Handle the server "cookie" header properly.
1276 * Log cookie to the jar file. Then "crunch" it,
1277 * or accept it. Called from `sed'.
1280 * 1 : v = parser pattern that matched this header
1281 * 2 : s = header that matched this pattern
1282 * 3 : csp = Current client state (buffers, headers, etc...)
1284 * Returns : `crumble' or a newly malloc'ed string.
1286 *********************************************************************/
1287 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1289 #ifdef FEATURE_COOKIE_JAR
1290 if (csp->config->jar)
1292 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1294 #endif /* def FEATURE_COOKIE_JAR */
1296 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1298 return(crumble(v, s, csp));
1306 #ifdef FEATURE_FORCE_LOAD
1307 /*********************************************************************
1309 * Function : client_host
1311 * Description : Clean the FORCE_PREFIX out of the 'host' http
1312 * header, if we use force
1316 * 2 : s = header (from sed) to clean
1317 * 3 : csp = Current client state (buffers, headers, etc...)
1319 * Returns : A malloc'ed pointer to the cleaned host header
1321 *********************************************************************/
1322 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1324 char *cleanhost = strdup(s);
1327 strclean(cleanhost, FORCE_PREFIX);
1331 #endif /* def FEATURE_FORCE_LOAD */
1334 #ifdef FEATURE_FORCE_LOAD
1335 /*********************************************************************
1337 * Function : strclean
1339 * Description : In-Situ-Eliminate all occurances of substring in
1343 * 1 : string = string to clean
1344 * 2 : substring = substring to eliminate
1346 * Returns : Number of eliminations
1348 *********************************************************************/
1349 int strclean(const char *string, const char *substring)
1351 int hits = 0, len = strlen(substring);
1354 while((pos = strstr(string, substring)))
1361 while (*p++ != '\0');
1368 #endif /* def FEATURE_FORCE_LOAD */