1 const char parsers_rcs[] = "$Id: parsers.c,v 1.19 2001/07/25 17:21:54 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.19 2001/07/25 17:21:54 oes
45 * client_uagent now saves copy of User-Agent: header value
47 * Revision 1.18 2001/07/13 14:02:46 oes
48 * - Included fix to repair broken HTTP requests that
49 * don't contain a path, not even '/'.
50 * - Removed all #ifdef PCRS
51 * - content_type now always inspected and classified as
53 * - formatting / comments
55 * Revision 1.17 2001/06/29 21:45:41 oes
56 * Indentation, CRLF->LF, Tab-> Space
58 * Revision 1.16 2001/06/29 13:32:42 oes
60 * - Adapted free_http_request
61 * - Removed logentry from cancelled commit
63 * Revision 1.15 2001/06/03 19:12:38 oes
64 * deleted const struct interceptors
66 * Revision 1.14 2001/06/01 18:49:17 jongfoster
67 * Replaced "list_share" with "list" - the tiny memory gain was not
68 * worth the extra complexity.
70 * Revision 1.13 2001/05/31 21:30:33 jongfoster
71 * Removed list code - it's now in list.[ch]
72 * Renamed "permission" to "action", and changed many features
73 * to use the actions file rather than the global config.
75 * Revision 1.12 2001/05/31 17:33:13 oes
79 * Revision 1.11 2001/05/29 20:11:19 joergs
80 * '/* inside comment' warning removed.
82 * Revision 1.10 2001/05/29 09:50:24 jongfoster
83 * Unified blocklist/imagelist/permissionslist.
84 * File format is still under discussion, but the internal changes
87 * Also modified interceptor behaviour:
88 * - We now intercept all URLs beginning with one of the following
89 * prefixes (and *only* these prefixes):
91 * * http://ijbswa.sf.net/config/
92 * * http://ijbswa.sourceforge.net/config/
93 * - New interceptors "home page" - go to http://i.j.b/ to see it.
94 * - Internal changes so that intercepted and fast redirect pages
95 * are not replaced with an image.
96 * - Interceptors now have the option to send a binary page direct
97 * to the client. (i.e. ijb-send-banner uses this)
98 * - Implemented show-url-info interceptor. (Which is why I needed
99 * the above interceptors changes - a typical URL is
100 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
101 * The previous mechanism would not have intercepted that, and
102 * if it had been intercepted then it then it would have replaced
105 * Revision 1.9 2001/05/28 17:26:33 jongfoster
106 * Fixing segfault if last header was crunched.
107 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
108 * can use the cross-platform sprintf() instead.)
110 * Revision 1.8 2001/05/27 22:17:04 oes
112 * - re_process_buffer no longer writes the modified buffer
113 * to the client, which was very ugly. It now returns the
114 * buffer, which it is then written by chat.
116 * - content_length now adjusts the Content-Length: header
117 * for modified documents rather than crunch()ing it.
118 * (Length info in csp->content_length, which is 0 for
119 * unmodified documents)
121 * - For this to work, sed() is called twice when filtering.
123 * Revision 1.7 2001/05/27 13:19:06 oes
124 * Patched Joergs solution for the content-length in.
126 * Revision 1.6 2001/05/26 13:39:32 jongfoster
127 * Only crunches Content-Length header if applying RE filtering.
128 * Without this fix, Microsoft Windows Update wouldn't work.
130 * Revision 1.5 2001/05/26 00:28:36 jongfoster
131 * Automatic reloading of config file.
132 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
133 * Most of the global variables have been moved to a new
134 * struct configuration_spec, accessed through csp->config->globalname
135 * Most of the globals remaining are used by the Win32 GUI.
137 * Revision 1.4 2001/05/22 18:46:04 oes
139 * - Enabled filtering banners by size rather than URL
140 * by adding patterns that replace all standard banner
141 * sizes with the "Junkbuster" gif to the re_filterfile
143 * - Enabled filtering WebBugs by providing a pattern
144 * which kills all 1x1 images
146 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
147 * which is selected by the (nonstandard and therefore
148 * capital) letter 'U' in the option string.
149 * It causes the quantifiers to be ungreedy by default.
150 * Appending a ? turns back to greedy (!).
152 * - Added a new interceptor ijb-send-banner, which
153 * sends back the "Junkbuster" gif. Without imagelist or
154 * MSIE detection support, or if tinygif = 1, or the
155 * URL isn't recognized as an imageurl, a lame HTML
156 * explanation is sent instead.
158 * - Added new feature, which permits blocking remote
159 * script redirects and firing back a local redirect
161 * The feature is conditionally compiled, i.e. it
162 * can be disabled with --disable-fast-redirects,
163 * plus it must be activated by a "fast-redirects"
164 * line in the config file, has its own log level
165 * and of course wants to be displayed by show-proxy-args
166 * Note: Boy, all the #ifdefs in 1001 locations and
167 * all the fumbling with configure.in and acconfig.h
168 * were *way* more work than the feature itself :-(
170 * - Because a generic redirect template was needed for
171 * this, tinygif = 3 now uses the same.
173 * - Moved GIFs, and other static HTTP response templates
178 * - Removed some >400 CRs again (Jon, you really worked
181 * Revision 1.3 2001/05/20 01:21:20 jongfoster
182 * Version 2.9.4 checkin.
183 * - Merged popupfile and cookiefile, and added control over PCRS
184 * filtering, in new "permissionsfile".
185 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
186 * file error you now get a message box (in the Win32 GUI) rather
187 * than the program exiting with no explanation.
188 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
190 * - Removed tabs from "config"
191 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
192 * - Bumped up version number.
194 * Revision 1.2 2001/05/17 23:02:36 oes
195 * - Made referrer option accept 'L' as a substitute for 'ยง'
197 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
198 * Initial import of version 2.9.3 source tree
201 *********************************************************************/
207 #include <sys/types.h>
222 #include "showargs.h"
226 #include "jbsockets.h"
227 #include "miscutil.h"
230 const char parsers_h_rcs[] = PARSERS_H_VERSION;
232 /* Fix a problem with Solaris. There should be no effect on other
234 * Solaris's isspace() is a macro which uses it's argument directly
235 * as an array index. Therefore we need to make sure that high-bit
236 * characters generate +ve values, and ideally we also want to make
237 * the argument match the declared parameter type of "int".
239 * Why did they write a character function that can't take a simple
240 * "char" argument? Doh!
242 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
243 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
246 const struct parsers client_patterns[] = {
247 { "referer:", 8, client_referrer },
248 { "user-agent:", 11, client_uagent },
249 { "ua-", 3, client_ua },
250 { "from:", 5, client_from },
251 { "cookie:", 7, client_send_cookie },
252 { "x-forwarded-for:", 16, client_x_forwarded },
253 { "proxy-connection:", 17, crumble },
254 #ifdef FEATURE_DENY_GZIP
255 { "Accept-Encoding: gzip", 21, crumble },
256 #endif /* def FEATURE_DENY_GZIP */
257 #if defined(FEATURE_IMAGE_DETECT_MSIE)
258 { "Accept:", 7, client_accept },
259 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
260 #ifdef FEATURE_FORCE_LOAD
261 { "Host:", 5, client_host },
262 #endif /* def FEATURE_FORCE_LOAD */
263 /* { "if-modified-since:", 18, crumble }, */
268 const struct parsers server_patterns[] = {
269 { "set-cookie:", 11, server_set_cookie },
270 { "connection:", 11, crumble },
271 { "Content-Type:", 13, content_type },
272 { "Content-Length:", 15, content_length },
277 void (* const add_client_headers[])(struct client_state *) = {
279 client_x_forwarded_adder,
285 void (* const add_server_headers[])(struct client_state *) = {
290 /*********************************************************************
292 * Function : flush_socket
294 * Description : Write any pending "buffered" content.
297 * 1 : fd = file descriptor of the socket to read
298 * 2 : csp = Current client state (buffers, headers, etc...)
300 * Returns : On success, the number of bytes written are returned (zero
301 * indicates nothing was written). On error, -1 is returned,
302 * and errno is set appropriately. If count is zero and the
303 * file descriptor refers to a regular file, 0 will be
304 * returned without causing any other effect. For a special
305 * file, the results are not portable.
307 *********************************************************************/
308 int flush_socket(int fd, struct client_state *csp)
310 struct iob *iob = csp->iob;
311 int n = iob->eod - iob->cur;
318 n = write_socket(fd, iob->cur, n);
319 iob->eod = iob->cur = iob->buf;
325 /*********************************************************************
327 * Function : add_to_iob
329 * Description : Add content to the buffered page.
332 * 1 : csp = Current client state (buffers, headers, etc...)
333 * 2 : buf = holds the content to be added to the page
334 * 3 : n = number of bytes to be added
336 * Returns : Number of bytes in the content buffer.
338 *********************************************************************/
339 int add_to_iob(struct client_state *csp, char *buf, int n)
341 struct iob *iob = csp->iob;
345 have = iob->eod - iob->cur;
354 if ((p = (char *)malloc(need + 1)) == NULL)
356 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
362 /* there is something in the buffer - save it */
363 memcpy(p, iob->cur, have);
365 /* replace the buffer with the new space */
369 /* point to the end of the data */
374 /* the buffer is empty, free it and reinitialize */
379 /* copy the new data into the iob buffer */
382 /* point to the end of the data */
385 /* null terminate == cheap insurance */
388 /* set the pointers to the new values */
397 /*********************************************************************
399 * Function : get_header
401 * Description : This (odd) routine will parse the csp->iob
404 * 1 : csp = Current client state (buffers, headers, etc...)
406 * Returns : Any one of the following:
408 * 1) a pointer to a dynamically allocated string that contains a header line
409 * 2) NULL indicating that the end of the header was reached
410 * 3) "" indicating that the end of the iob was reached before finding
411 * a complete header line.
413 *********************************************************************/
414 char *get_header(struct client_state *csp)
420 if ((iob->cur == NULL)
421 || ((p = strchr(iob->cur, '\n')) == NULL))
423 return(""); /* couldn't find a complete header */
428 ret = strdup(iob->cur);
432 if ((q = strchr(ret, '\r'))) *q = '\0';
434 /* is this a blank linke (i.e. the end of the header) ? */
446 /*********************************************************************
450 * Description : add, delete or modify lines in the HTTP header streams.
451 * On entry, it receives a linked list of headers space
452 * that was allocated dynamically (both the list nodes
453 * and the header contents).
455 * As a side effect it frees the space used by the original
459 * 1 : pats = list of patterns to match against headers
460 * 2 : more_headers = list of functions to add more
461 * headers (client or server)
462 * 3 : csp = Current client state (buffers, headers, etc...)
464 * Returns : Single pointer to a fully formed header.
466 *********************************************************************/
467 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
470 const struct parsers *v;
474 for (v = pats; v->str ; v++)
476 for (p = csp->headers->next; p ; p = p->next)
478 /* Header crunch()ed in previous run? -> ignore */
479 if (p->str == NULL) continue;
481 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
483 if (strncmpic(p->str, v->str, v->len) == 0)
485 hdr = v->parser(v, p->str, csp);
492 /* place any additional headers on the csp->headers list */
493 for (f = more_headers; *f ; f++)
498 /* add the blank line at the end of the header, if necessary */
499 if ( (csp->headers->last == NULL)
500 || (csp->headers->last->str == NULL)
501 || (*csp->headers->last->str != '\0') )
503 enlist(csp->headers, "");
506 hdr = list_to_text(csp->headers);
513 /*********************************************************************
515 * Function : free_http_request
517 * Description : Freez a http_request structure
520 * 1 : http = points to a http_request structure to free
524 *********************************************************************/
525 void free_http_request(struct http_request *http)
530 freez(http->hostport);
533 freez(http->host_ip_addr_str);
534 freez(http->user_agent);
539 /*********************************************************************
541 * Function : parse_http_request
543 * Description : Parse out the host and port from the URL. Find the
544 * hostname & path, port (if ':'), and/or password (if '@')
547 * 1 : req = URL (or is it URI?) to break down
548 * 2 : http = pointer to the http structure to hold elements
549 * 3 : csp = Current client state (buffers, headers, etc...)
553 *********************************************************************/
554 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
556 char *buf, *v[10], *url, *p;
559 memset(http, '\0', sizeof(*http));
561 http->cmd = strdup(req);
564 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
568 /* this could be a CONNECT request */
569 if (strcmpic(v[0], "connect") == 0)
572 http->gpc = strdup(v[0]);
573 http->hostport = strdup(v[1]);
574 http->ver = strdup(v[2]);
577 /* or it could be a GET or a POST (possibly webDAV too) */
578 if ((0 == strcmpic(v[0], "get"))
579 || (0 == strcmpic(v[0], "head"))
580 || (0 == strcmpic(v[0], "post"))
581 /* These are the headers as defined in RFC2518 to add webDAV support: */
582 || (0 == strcmpic(v[0], "propfind"))
583 || (0 == strcmpic(v[0], "proppatch"))
584 || (0 == strcmpic(v[0], "move"))
585 || (0 == strcmpic(v[0], "copy"))
586 || (0 == strcmpic(v[0], "mkcol"))
587 || (0 == strcmpic(v[0], "lock"))
588 || (0 == strcmpic(v[0], "unlock"))
592 http->gpc = strdup(v[0]);
594 http->ver = strdup(v[2]);
596 if (strncmpic(url, "http://", 7) == 0)
600 else if (strncmpic(url, "https://", 8) == 0)
611 if (p = strchr(url, '/'))
613 http->path = strdup(p);
615 http->hostport = strdup(url);
618 * Repair broken HTTP requests that don't contain a path
622 /* Repair hostport & path */
623 http->path = strdup("/");
624 http->hostport = strdup(url);
626 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
628 http->cmd = strsav(http->cmd, http->gpc);
629 http->cmd = strsav(http->cmd, " / ");
630 http->cmd = strsav(http->cmd, http->ver);
639 if (http->hostport == NULL)
641 free_http_request(http);
645 buf = strdup(http->hostport);
648 /* check if url contains password */
649 n = ssplit(buf, "@", v, SZ(v), 1, 1);
652 char * newbuf = NULL;
653 newbuf = strdup(v[1]);
658 n = ssplit(buf, ":", v, SZ(v), 1, 1);
662 http->host = strdup(v[0]);
668 http->host = strdup(v[0]);
669 http->port = atoi(v[1]);
674 if (http->host == NULL)
676 free_http_request(http);
679 if (http->path == NULL)
681 http->path = strdup("");
687 /* here begins the family of parser functions that reformat header lines */
690 /*********************************************************************
694 * Description : This is called if a header matches a pattern to "crunch"
697 * 1 : v = Pointer to parsers structure, which basically holds
698 * headers (client or server) that we want to "crunch"
699 * 2 : s = header (from sed) to "crunch"
700 * 3 : csp = Current client state (buffers, headers, etc...)
702 * Returns : Always NULL.
704 *********************************************************************/
705 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
707 log_error(LOG_LEVEL_HEADER, "crunch!");
713 /*********************************************************************
715 * Function : content_type
717 * Description : Is this a text/.* or javascript MIME Type?
721 * 2 : s = header string we are "considering"
722 * 3 : csp = Current client state (buffers, headers, etc...)
724 * Returns : A duplicate string pointer to this header (ie. pass thru)
726 *********************************************************************/
727 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
729 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
730 csp->content_type = CT_TEXT;
731 else if (strstr(s, " image/gif"))
732 csp->content_type = CT_GIF;
734 csp->content_type = 0;
741 /*********************************************************************
743 * Function : content_length
745 * Description : Adjust Content-Length header if we modified
750 * 2 : s = header string we are "considering"
751 * 3 : csp = Current client state (buffers, headers, etc...)
753 * Returns : A duplicate string pointer to this header (ie. pass thru)
755 *********************************************************************/
756 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
758 if (csp->content_length != 0) /* Content has been modified */
760 s = (char *) zalloc(100);
761 sprintf(s, "Content-Length: %d", csp->content_length);
763 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
774 /*********************************************************************
776 * Function : client_referrer
778 * Description : Handle the "referer" config setting properly.
783 * 2 : s = header (from sed) to "crunch"
784 * 3 : csp = Current client state (buffers, headers, etc...)
786 * Returns : NULL if crunched, or a malloc'ed string with the original
789 *********************************************************************/
790 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
793 #ifdef FEATURE_FORCE_LOAD
794 /* Since the referrer can include the prefix even
795 * even if the request itself is non-forced, we must
796 * clean it unconditionally
798 strclean(s, FORCE_PREFIX);
799 #endif /* def FEATURE_FORCE_LOAD */
802 csp->referrer = strdup(s);
803 #endif /* def FEATURE_TRUST */
806 * Are we sending referer?
808 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
813 newval = csp->action->string[ACTION_STRING_REFERER];
816 * Are we blocking referer?
818 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
820 log_error(LOG_LEVEL_HEADER, "crunch!");
825 * Are we forging referer?
827 if (0 == strcmpic(newval, "forge"))
830 * Forge a referer as http://[hostname:port of REQUEST]/
831 * to fool stupid checks for in-site links
833 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
834 s = strsav(NULL, "Referer: ");
835 s = strsav(s, "http://");
836 s = strsav(s, csp->http->hostport);
842 * Have we got a fixed referer?
844 if (0 == strncmpic(newval, "http://", 7))
847 * We have a specific (fixed) referer we want to send.
850 log_error(LOG_LEVEL_HEADER, "modified");
852 s = strsav( NULL, "Referer: " );
853 s = strsav( s, newval );
857 /* Should never get here! */
858 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
861 * Forge is probably the best default.
863 * Forge a referer as http://[hostname:port of REQUEST]/
864 * to fool stupid checks for in-site links
866 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
867 s = strsav(NULL, "Referer: ");
868 s = strsav(s, "http://");
869 s = strsav(s, csp->http->hostport);
875 /*********************************************************************
877 * Function : client_uagent
879 * Description : Handle the "user-agent" config setting properly
880 * and remember its original value to enable browser
881 * bug workarounds. Called from `sed'.
885 * 2 : s = header (from sed) to "crunch"
886 * 3 : csp = Current client state (buffers, headers, etc...)
888 * Returns : A malloc'ed pointer to the default agent, or
889 * a malloc'ed string pointer to this header (ie. pass thru).
891 *********************************************************************/
892 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
896 /* Save the client's User-Agent: value */
899 csp->http->user_agent = strdup(s + 12);
902 #ifdef FEATURE_IMAGE_DETECT_MSIE
903 if (strstr (s, "MSIE "))
905 /* This is Microsoft Internet Explorer.
906 * Enable auto-detect.
908 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
910 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
912 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
917 newval = csp->action->string[ACTION_STRING_USER_AGENT];
923 log_error(LOG_LEVEL_HEADER, "modified");
925 s = strsav( NULL, "User-Agent: " );
926 s = strsav( s, newval );
932 /*********************************************************************
934 * Function : client_ua
936 * Description : Handle "ua-" headers properly. Called from `sed'.
940 * 2 : s = header (from sed) to "crunch"
941 * 3 : csp = Current client state (buffers, headers, etc...)
943 * Returns : NULL if crunched, or a malloc'ed string to original header
945 *********************************************************************/
946 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
948 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
954 log_error(LOG_LEVEL_HEADER, "crunch!");
960 /*********************************************************************
962 * Function : client_from
964 * Description : Handle the "from" config setting properly.
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
973 * modified/original header.
975 *********************************************************************/
976 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
980 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
985 newval = csp->action->string[ACTION_STRING_FROM];
988 * Are we blocking referer?
990 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
992 log_error(LOG_LEVEL_HEADER, "crunch!");
996 log_error(LOG_LEVEL_HEADER, " modified");
998 s = strsav( NULL, "From: " );
999 s = strsav( s, newval );
1005 /*********************************************************************
1007 * Function : client_send_cookie
1009 * Description : Handle the "cookie" header properly. Called from `sed'.
1010 * If cookie is accepted, add it to the cookie_list,
1011 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1014 * 1 : v = pattern of cookie `sed' found matching
1015 * 2 : s = header (from sed) to "crunch"
1016 * 3 : csp = Current client state (buffers, headers, etc...)
1018 * Returns : Always NULL.
1020 *********************************************************************/
1021 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1023 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1025 enlist(csp->cookie_list, s + v->len + 1);
1029 log_error(LOG_LEVEL_HEADER, " crunch!");
1033 * Always return NULL here. The cookie header
1034 * will be sent at the end of the header.
1041 /*********************************************************************
1043 * Function : client_x_forwarded
1045 * Description : Handle the "x-forwarded-for" config setting properly,
1046 * also used in the add_client_headers list. Called from `sed'.
1050 * 2 : s = header (from sed) to "crunch"
1051 * 3 : csp = Current client state (buffers, headers, etc...)
1053 * Returns : Always NULL.
1055 *********************************************************************/
1056 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1058 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1060 /* Save it so we can re-add it later */
1061 csp->x_forwarded = strdup(s);
1065 * Always return NULL, since this information
1066 * will be sent at the end of the header.
1073 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1074 /*********************************************************************
1076 * Function : client_accept
1078 * Description : Detect whether the client wants HTML or an image.
1079 * Clients do not always make this information available
1080 * in a sane way. Always passes the header through
1081 * the proxy unchanged.
1085 * 2 : s = Header string. Null terminated.
1086 * 3 : csp = Current client state (buffers, headers, etc...)
1088 * Returns : Duplicate of argument s.
1090 *********************************************************************/
1091 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1093 #ifdef FEATURE_IMAGE_DETECT_MSIE
1094 if (strstr (s, "image/gif"))
1096 /* Client will accept HTML. If this seems counterintuitive,
1099 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1103 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1105 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1110 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1114 /* the following functions add headers directly to the header list */
1117 /*********************************************************************
1119 * Function : client_cookie_adder
1121 * Description : Used in the add_client_headers list. Called from `sed'.
1124 * 1 : csp = Current client state (buffers, headers, etc...)
1128 *********************************************************************/
1129 void client_cookie_adder(struct client_state *csp)
1135 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1139 tmp = strsav(tmp, "; ");
1141 tmp = strsav(tmp, lst->str);
1144 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1148 tmp = strsav(tmp, "; ");
1151 if ((e = cookie_encode(lst->str)))
1153 tmp = strsav(tmp, e);
1162 ret = strdup("Cookie: ");
1163 ret = strsav(ret, tmp);
1164 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1165 enlist(csp->headers, ret);
1173 /*********************************************************************
1175 * Function : client_xtra_adder
1177 * Description : Used in the add_client_headers list. Called from `sed'.
1180 * 1 : csp = Current client state (buffers, headers, etc...)
1184 *********************************************************************/
1185 void client_xtra_adder(struct client_state *csp)
1187 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1189 for (lst = lst->next; lst ; lst = lst->next)
1191 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1192 enlist(csp->headers, lst->str);
1198 /*********************************************************************
1200 * Function : client_x_forwarded_adder
1202 * Description : Used in the add_client_headers list. Called from `sed'.
1205 * 1 : csp = Current client state (buffers, headers, etc...)
1209 *********************************************************************/
1210 void client_x_forwarded_adder(struct client_state *csp)
1214 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1219 if (csp->x_forwarded)
1221 p = strsav(p, csp->x_forwarded);
1222 p = strsav(p, ", ");
1223 p = strsav(p, csp->ip_addr_str);
1227 p = strsav(p, "X-Forwarded-For: ");
1228 p = strsav(p, csp->ip_addr_str);
1231 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1232 enlist(csp->headers, p);
1237 /*********************************************************************
1239 * Function : server_set_cookie
1241 * Description : Handle the server "cookie" header properly.
1242 * Log cookie to the jar file. Then "crunch" it,
1243 * or accept it. Called from `sed'.
1246 * 1 : v = parser pattern that matched this header
1247 * 2 : s = header that matched this pattern
1248 * 3 : csp = Current client state (buffers, headers, etc...)
1250 * Returns : `crumble' or a newly malloc'ed string.
1252 *********************************************************************/
1253 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1255 #ifdef FEATURE_COOKIE_JAR
1256 if (csp->config->jar)
1258 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1260 #endif /* def FEATURE_COOKIE_JAR */
1262 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1264 return(crumble(v, s, csp));
1272 #ifdef FEATURE_FORCE_LOAD
1273 /*********************************************************************
1275 * Function : client_host
1277 * Description : Clean the FORCE_PREFIX out of the 'host' http
1278 * header, if we use force
1282 * 2 : s = header (from sed) to clean
1283 * 3 : csp = Current client state (buffers, headers, etc...)
1285 * Returns : A malloc'ed pointer to the cleaned host header
1287 *********************************************************************/
1288 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1290 char *cleanhost = strdup(s);
1293 strclean(cleanhost, FORCE_PREFIX);
1297 #endif /* def FEATURE_FORCE_LOAD */
1300 #ifdef FEATURE_FORCE_LOAD
1301 /*********************************************************************
1303 * Function : strclean
1305 * Description : In-Situ-Eliminate all occurances of substring in
1309 * 1 : string = string to clean
1310 * 2 : substring = substring to eliminate
1312 * Returns : Number of eliminations
1314 *********************************************************************/
1315 int strclean(const char *string, const char *substring)
1317 int hits = 0, len = strlen(substring);
1320 while((pos = strstr(string, substring)))
1327 while (*p++ != '\0');
1334 #endif /* def FEATURE_FORCE_LOAD */