1 const char parsers_rcs[] = "$Id: parsers.c,v 1.21 2001/07/31 14:46:00 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.21 2001/07/31 14:46:00 oes
45 * - Persistant connections now suppressed
46 * - sed() no longer appends empty header to csp->headers
48 * Revision 1.20 2001/07/30 22:08:36 jongfoster
49 * Tidying up #defines:
50 * - All feature #defines are now of the form FEATURE_xxx
51 * - Permanently turned off WIN_GUI_EDIT
52 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
54 * Revision 1.19 2001/07/25 17:21:54 oes
55 * client_uagent now saves copy of User-Agent: header value
57 * Revision 1.18 2001/07/13 14:02:46 oes
58 * - Included fix to repair broken HTTP requests that
59 * don't contain a path, not even '/'.
60 * - Removed all #ifdef PCRS
61 * - content_type now always inspected and classified as
63 * - formatting / comments
65 * Revision 1.17 2001/06/29 21:45:41 oes
66 * Indentation, CRLF->LF, Tab-> Space
68 * Revision 1.16 2001/06/29 13:32:42 oes
70 * - Adapted free_http_request
71 * - Removed logentry from cancelled commit
73 * Revision 1.15 2001/06/03 19:12:38 oes
74 * deleted const struct interceptors
76 * Revision 1.14 2001/06/01 18:49:17 jongfoster
77 * Replaced "list_share" with "list" - the tiny memory gain was not
78 * worth the extra complexity.
80 * Revision 1.13 2001/05/31 21:30:33 jongfoster
81 * Removed list code - it's now in list.[ch]
82 * Renamed "permission" to "action", and changed many features
83 * to use the actions file rather than the global config.
85 * Revision 1.12 2001/05/31 17:33:13 oes
89 * Revision 1.11 2001/05/29 20:11:19 joergs
90 * '/ * inside comment' warning removed.
92 * Revision 1.10 2001/05/29 09:50:24 jongfoster
93 * Unified blocklist/imagelist/permissionslist.
94 * File format is still under discussion, but the internal changes
97 * Also modified interceptor behaviour:
98 * - We now intercept all URLs beginning with one of the following
99 * prefixes (and *only* these prefixes):
101 * * http://ijbswa.sf.net/config/
102 * * http://ijbswa.sourceforge.net/config/
103 * - New interceptors "home page" - go to http://i.j.b/ to see it.
104 * - Internal changes so that intercepted and fast redirect pages
105 * are not replaced with an image.
106 * - Interceptors now have the option to send a binary page direct
107 * to the client. (i.e. ijb-send-banner uses this)
108 * - Implemented show-url-info interceptor. (Which is why I needed
109 * the above interceptors changes - a typical URL is
110 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
111 * The previous mechanism would not have intercepted that, and
112 * if it had been intercepted then it then it would have replaced
115 * Revision 1.9 2001/05/28 17:26:33 jongfoster
116 * Fixing segfault if last header was crunched.
117 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
118 * can use the cross-platform sprintf() instead.)
120 * Revision 1.8 2001/05/27 22:17:04 oes
122 * - re_process_buffer no longer writes the modified buffer
123 * to the client, which was very ugly. It now returns the
124 * buffer, which it is then written by chat.
126 * - content_length now adjusts the Content-Length: header
127 * for modified documents rather than crunch()ing it.
128 * (Length info in csp->content_length, which is 0 for
129 * unmodified documents)
131 * - For this to work, sed() is called twice when filtering.
133 * Revision 1.7 2001/05/27 13:19:06 oes
134 * Patched Joergs solution for the content-length in.
136 * Revision 1.6 2001/05/26 13:39:32 jongfoster
137 * Only crunches Content-Length header if applying RE filtering.
138 * Without this fix, Microsoft Windows Update wouldn't work.
140 * Revision 1.5 2001/05/26 00:28:36 jongfoster
141 * Automatic reloading of config file.
142 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
143 * Most of the global variables have been moved to a new
144 * struct configuration_spec, accessed through csp->config->globalname
145 * Most of the globals remaining are used by the Win32 GUI.
147 * Revision 1.4 2001/05/22 18:46:04 oes
149 * - Enabled filtering banners by size rather than URL
150 * by adding patterns that replace all standard banner
151 * sizes with the "Junkbuster" gif to the re_filterfile
153 * - Enabled filtering WebBugs by providing a pattern
154 * which kills all 1x1 images
156 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
157 * which is selected by the (nonstandard and therefore
158 * capital) letter 'U' in the option string.
159 * It causes the quantifiers to be ungreedy by default.
160 * Appending a ? turns back to greedy (!).
162 * - Added a new interceptor ijb-send-banner, which
163 * sends back the "Junkbuster" gif. Without imagelist or
164 * MSIE detection support, or if tinygif = 1, or the
165 * URL isn't recognized as an imageurl, a lame HTML
166 * explanation is sent instead.
168 * - Added new feature, which permits blocking remote
169 * script redirects and firing back a local redirect
171 * The feature is conditionally compiled, i.e. it
172 * can be disabled with --disable-fast-redirects,
173 * plus it must be activated by a "fast-redirects"
174 * line in the config file, has its own log level
175 * and of course wants to be displayed by show-proxy-args
176 * Note: Boy, all the #ifdefs in 1001 locations and
177 * all the fumbling with configure.in and acconfig.h
178 * were *way* more work than the feature itself :-(
180 * - Because a generic redirect template was needed for
181 * this, tinygif = 3 now uses the same.
183 * - Moved GIFs, and other static HTTP response templates
188 * - Removed some >400 CRs again (Jon, you really worked
191 * Revision 1.3 2001/05/20 01:21:20 jongfoster
192 * Version 2.9.4 checkin.
193 * - Merged popupfile and cookiefile, and added control over PCRS
194 * filtering, in new "permissionsfile".
195 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
196 * file error you now get a message box (in the Win32 GUI) rather
197 * than the program exiting with no explanation.
198 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
200 * - Removed tabs from "config"
201 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
202 * - Bumped up version number.
204 * Revision 1.2 2001/05/17 23:02:36 oes
205 * - Made referrer option accept 'L' as a substitute for 'ยง'
207 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
208 * Initial import of version 2.9.3 source tree
211 *********************************************************************/
217 #include <sys/types.h>
232 #include "showargs.h"
236 #include "jbsockets.h"
237 #include "miscutil.h"
240 const char parsers_h_rcs[] = PARSERS_H_VERSION;
242 /* Fix a problem with Solaris. There should be no effect on other
244 * Solaris's isspace() is a macro which uses it's argument directly
245 * as an array index. Therefore we need to make sure that high-bit
246 * characters generate +ve values, and ideally we also want to make
247 * the argument match the declared parameter type of "int".
249 * Why did they write a character function that can't take a simple
250 * "char" argument? Doh!
252 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
253 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
256 const struct parsers client_patterns[] = {
257 { "referer:", 8, client_referrer },
258 { "user-agent:", 11, client_uagent },
259 { "ua-", 3, client_ua },
260 { "from:", 5, client_from },
261 { "cookie:", 7, client_send_cookie },
262 { "x-forwarded-for:", 16, client_x_forwarded },
263 #ifdef FEATURE_DENY_GZIP
264 { "Accept-Encoding: gzip", 21, crumble },
265 #endif /* def FEATURE_DENY_GZIP */
266 #if defined(FEATURE_IMAGE_DETECT_MSIE)
267 { "Accept:", 7, client_accept },
268 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
269 #ifdef FEATURE_FORCE_LOAD
270 { "Host:", 5, client_host },
271 #endif /* def FEATURE_FORCE_LOAD */
272 /* { "if-modified-since:", 18, crumble }, */
273 { "Keep-Alive:", 11, crumble },
274 { "connection:", 11, crumble },
275 { "proxy-connection:", 17, crumble },
280 const struct parsers server_patterns[] = {
281 { "set-cookie:", 11, server_set_cookie },
282 { "connection:", 11, crumble },
283 { "Content-Type:", 13, content_type },
284 { "Content-Length:", 15, content_length },
289 void (* const add_client_headers[])(struct client_state *) = {
291 client_x_forwarded_adder,
293 connection_close_adder,
298 void (* const add_server_headers[])(struct client_state *) = {
299 connection_close_adder,
304 /*********************************************************************
306 * Function : flush_socket
308 * Description : Write any pending "buffered" content.
311 * 1 : fd = file descriptor of the socket to read
312 * 2 : csp = Current client state (buffers, headers, etc...)
314 * Returns : On success, the number of bytes written are returned (zero
315 * indicates nothing was written). On error, -1 is returned,
316 * and errno is set appropriately. If count is zero and the
317 * file descriptor refers to a regular file, 0 will be
318 * returned without causing any other effect. For a special
319 * file, the results are not portable.
321 *********************************************************************/
322 int flush_socket(int fd, struct client_state *csp)
324 struct iob *iob = csp->iob;
325 int n = iob->eod - iob->cur;
332 n = write_socket(fd, iob->cur, n);
333 iob->eod = iob->cur = iob->buf;
339 /*********************************************************************
341 * Function : add_to_iob
343 * Description : Add content to the buffered page.
346 * 1 : csp = Current client state (buffers, headers, etc...)
347 * 2 : buf = holds the content to be added to the page
348 * 3 : n = number of bytes to be added
350 * Returns : Number of bytes in the content buffer.
352 *********************************************************************/
353 int add_to_iob(struct client_state *csp, char *buf, int n)
355 struct iob *iob = csp->iob;
359 have = iob->eod - iob->cur;
368 if ((p = (char *)malloc(need + 1)) == NULL)
370 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
376 /* there is something in the buffer - save it */
377 memcpy(p, iob->cur, have);
379 /* replace the buffer with the new space */
383 /* point to the end of the data */
388 /* the buffer is empty, free it and reinitialize */
393 /* copy the new data into the iob buffer */
396 /* point to the end of the data */
399 /* null terminate == cheap insurance */
402 /* set the pointers to the new values */
411 /*********************************************************************
413 * Function : get_header
415 * Description : This (odd) routine will parse the csp->iob
418 * 1 : csp = Current client state (buffers, headers, etc...)
420 * Returns : Any one of the following:
422 * 1) a pointer to a dynamically allocated string that contains a header line
423 * 2) NULL indicating that the end of the header was reached
424 * 3) "" indicating that the end of the iob was reached before finding
425 * a complete header line.
427 *********************************************************************/
428 char *get_header(struct client_state *csp)
434 if ((iob->cur == NULL)
435 || ((p = strchr(iob->cur, '\n')) == NULL))
437 return(""); /* couldn't find a complete header */
442 ret = strdup(iob->cur);
446 if ((q = strchr(ret, '\r'))) *q = '\0';
448 /* is this a blank linke (i.e. the end of the header) ? */
460 /*********************************************************************
464 * Description : add, delete or modify lines in the HTTP header streams.
465 * On entry, it receives a linked list of headers space
466 * that was allocated dynamically (both the list nodes
467 * and the header contents).
469 * As a side effect it frees the space used by the original
473 * 1 : pats = list of patterns to match against headers
474 * 2 : more_headers = list of functions to add more
475 * headers (client or server)
476 * 3 : csp = Current client state (buffers, headers, etc...)
478 * Returns : Single pointer to a fully formed header.
480 *********************************************************************/
481 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
484 const struct parsers *v;
488 for (v = pats; v->str ; v++)
490 for (p = csp->headers->next; p ; p = p->next)
492 /* Header crunch()ed in previous run? -> ignore */
493 if (p->str == NULL) continue;
495 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
497 if (strncmpic(p->str, v->str, v->len) == 0)
499 hdr = v->parser(v, p->str, csp);
506 /* place any additional headers on the csp->headers list */
507 for (f = more_headers; *f ; f++)
512 hdr = list_to_text(csp->headers);
519 /*********************************************************************
521 * Function : free_http_request
523 * Description : Freez a http_request structure
526 * 1 : http = points to a http_request structure to free
530 *********************************************************************/
531 void free_http_request(struct http_request *http)
536 freez(http->hostport);
539 freez(http->host_ip_addr_str);
540 freez(http->user_agent);
545 /*********************************************************************
547 * Function : parse_http_request
549 * Description : Parse out the host and port from the URL. Find the
550 * hostname & path, port (if ':'), and/or password (if '@')
553 * 1 : req = URL (or is it URI?) to break down
554 * 2 : http = pointer to the http structure to hold elements
555 * 3 : csp = Current client state (buffers, headers, etc...)
559 *********************************************************************/
560 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
562 char *buf, *v[10], *url, *p;
565 memset(http, '\0', sizeof(*http));
567 http->cmd = strdup(req);
570 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
574 /* this could be a CONNECT request */
575 if (strcmpic(v[0], "connect") == 0)
578 http->gpc = strdup(v[0]);
579 http->hostport = strdup(v[1]);
580 http->ver = strdup(v[2]);
583 /* or it could be a GET or a POST (possibly webDAV too) */
584 if ((0 == strcmpic(v[0], "get"))
585 || (0 == strcmpic(v[0], "head"))
586 || (0 == strcmpic(v[0], "post"))
587 /* These are the headers as defined in RFC2518 to add webDAV support: */
588 || (0 == strcmpic(v[0], "propfind"))
589 || (0 == strcmpic(v[0], "proppatch"))
590 || (0 == strcmpic(v[0], "move"))
591 || (0 == strcmpic(v[0], "copy"))
592 || (0 == strcmpic(v[0], "mkcol"))
593 || (0 == strcmpic(v[0], "lock"))
594 || (0 == strcmpic(v[0], "unlock"))
598 http->gpc = strdup(v[0]);
600 http->ver = strdup(v[2]);
602 if (strncmpic(url, "http://", 7) == 0)
606 else if (strncmpic(url, "https://", 8) == 0)
617 if ((p = strchr(url, '/')))
619 http->path = strdup(p);
621 http->hostport = strdup(url);
624 * Repair broken HTTP requests that don't contain a path
628 /* Repair hostport & path */
629 http->path = strdup("/");
630 http->hostport = strdup(url);
632 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
634 http->cmd = strsav(http->cmd, http->gpc);
635 http->cmd = strsav(http->cmd, " / ");
636 http->cmd = strsav(http->cmd, http->ver);
645 if (http->hostport == NULL)
647 free_http_request(http);
651 buf = strdup(http->hostport);
654 /* check if url contains password */
655 n = ssplit(buf, "@", v, SZ(v), 1, 1);
658 char * newbuf = NULL;
659 newbuf = strdup(v[1]);
664 n = ssplit(buf, ":", v, SZ(v), 1, 1);
668 http->host = strdup(v[0]);
674 http->host = strdup(v[0]);
675 http->port = atoi(v[1]);
680 if (http->host == NULL)
682 free_http_request(http);
685 if (http->path == NULL)
687 http->path = strdup("");
693 /* here begins the family of parser functions that reformat header lines */
696 /*********************************************************************
700 * Description : This is called if a header matches a pattern to "crunch"
703 * 1 : v = Pointer to parsers structure, which basically holds
704 * headers (client or server) that we want to "crunch"
705 * 2 : s = header (from sed) to "crunch"
706 * 3 : csp = Current client state (buffers, headers, etc...)
708 * Returns : Always NULL.
710 *********************************************************************/
711 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
713 log_error(LOG_LEVEL_HEADER, "crunch!");
719 /*********************************************************************
721 * Function : content_type
723 * Description : Is this a text/.* or javascript MIME Type?
727 * 2 : s = header string we are "considering"
728 * 3 : csp = Current client state (buffers, headers, etc...)
730 * Returns : A duplicate string pointer to this header (ie. pass thru)
732 *********************************************************************/
733 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
735 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
736 csp->content_type = CT_TEXT;
737 else if (strstr(s, " image/gif"))
738 csp->content_type = CT_GIF;
740 csp->content_type = 0;
747 /*********************************************************************
749 * Function : content_length
751 * Description : Adjust Content-Length header if we modified
756 * 2 : s = header string we are "considering"
757 * 3 : csp = Current client state (buffers, headers, etc...)
759 * Returns : A duplicate string pointer to this header (ie. pass thru)
761 *********************************************************************/
762 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
764 if (csp->content_length != 0) /* Content has been modified */
766 s = (char *) zalloc(100);
767 sprintf(s, "Content-Length: %d", csp->content_length);
769 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
780 /*********************************************************************
782 * Function : client_referrer
784 * Description : Handle the "referer" config setting properly.
789 * 2 : s = header (from sed) to "crunch"
790 * 3 : csp = Current client state (buffers, headers, etc...)
792 * Returns : NULL if crunched, or a malloc'ed string with the original
795 *********************************************************************/
796 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
799 #ifdef FEATURE_FORCE_LOAD
800 /* Since the referrer can include the prefix even
801 * even if the request itself is non-forced, we must
802 * clean it unconditionally
804 strclean(s, FORCE_PREFIX);
805 #endif /* def FEATURE_FORCE_LOAD */
808 csp->referrer = strdup(s);
809 #endif /* def FEATURE_TRUST */
812 * Are we sending referer?
814 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
819 newval = csp->action->string[ACTION_STRING_REFERER];
822 * Are we blocking referer?
824 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
826 log_error(LOG_LEVEL_HEADER, "crunch!");
831 * Are we forging referer?
833 if (0 == strcmpic(newval, "forge"))
836 * Forge a referer as http://[hostname:port of REQUEST]/
837 * to fool stupid checks for in-site links
839 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
840 s = strsav(NULL, "Referer: ");
841 s = strsav(s, "http://");
842 s = strsav(s, csp->http->hostport);
848 * Have we got a fixed referer?
850 if (0 == strncmpic(newval, "http://", 7))
853 * We have a specific (fixed) referer we want to send.
856 log_error(LOG_LEVEL_HEADER, "modified");
858 s = strsav( NULL, "Referer: " );
859 s = strsav( s, newval );
863 /* Should never get here! */
864 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
867 * Forge is probably the best default.
869 * Forge a referer as http://[hostname:port of REQUEST]/
870 * to fool stupid checks for in-site links
872 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
873 s = strsav(NULL, "Referer: ");
874 s = strsav(s, "http://");
875 s = strsav(s, csp->http->hostport);
881 /*********************************************************************
883 * Function : client_uagent
885 * Description : Handle the "user-agent" config setting properly
886 * and remember its original value to enable browser
887 * bug workarounds. Called from `sed'.
891 * 2 : s = header (from sed) to "crunch"
892 * 3 : csp = Current client state (buffers, headers, etc...)
894 * Returns : A malloc'ed pointer to the default agent, or
895 * a malloc'ed string pointer to this header (ie. pass thru).
897 *********************************************************************/
898 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
902 /* Save the client's User-Agent: value */
905 csp->http->user_agent = strdup(s + 12);
908 #ifdef FEATURE_IMAGE_DETECT_MSIE
909 if (strstr (s, "MSIE "))
911 /* This is Microsoft Internet Explorer.
912 * Enable auto-detect.
914 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
916 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
918 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
923 newval = csp->action->string[ACTION_STRING_USER_AGENT];
929 log_error(LOG_LEVEL_HEADER, "modified");
931 s = strsav( NULL, "User-Agent: " );
932 s = strsav( s, newval );
938 /*********************************************************************
940 * Function : client_ua
942 * Description : Handle "ua-" headers properly. Called from `sed'.
946 * 2 : s = header (from sed) to "crunch"
947 * 3 : csp = Current client state (buffers, headers, etc...)
949 * Returns : NULL if crunched, or a malloc'ed string to original header
951 *********************************************************************/
952 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
954 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
960 log_error(LOG_LEVEL_HEADER, "crunch!");
966 /*********************************************************************
968 * Function : client_from
970 * Description : Handle the "from" config setting properly.
975 * 2 : s = header (from sed) to "crunch"
976 * 3 : csp = Current client state (buffers, headers, etc...)
978 * Returns : NULL if crunched, or a malloc'ed string to
979 * modified/original header.
981 *********************************************************************/
982 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
986 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
991 newval = csp->action->string[ACTION_STRING_FROM];
994 * Are we blocking referer?
996 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
998 log_error(LOG_LEVEL_HEADER, "crunch!");
1002 log_error(LOG_LEVEL_HEADER, " modified");
1004 s = strsav( NULL, "From: " );
1005 s = strsav( s, newval );
1011 /*********************************************************************
1013 * Function : client_send_cookie
1015 * Description : Handle the "cookie" header properly. Called from `sed'.
1016 * If cookie is accepted, add it to the cookie_list,
1017 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1020 * 1 : v = pattern of cookie `sed' found matching
1021 * 2 : s = header (from sed) to "crunch"
1022 * 3 : csp = Current client state (buffers, headers, etc...)
1024 * Returns : Always NULL.
1026 *********************************************************************/
1027 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1029 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1031 enlist(csp->cookie_list, s + v->len + 1);
1035 log_error(LOG_LEVEL_HEADER, " crunch!");
1039 * Always return NULL here. The cookie header
1040 * will be sent at the end of the header.
1047 /*********************************************************************
1049 * Function : client_x_forwarded
1051 * Description : Handle the "x-forwarded-for" config setting properly,
1052 * also used in the add_client_headers list. Called from `sed'.
1056 * 2 : s = header (from sed) to "crunch"
1057 * 3 : csp = Current client state (buffers, headers, etc...)
1059 * Returns : Always NULL.
1061 *********************************************************************/
1062 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1064 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1066 /* Save it so we can re-add it later */
1067 csp->x_forwarded = strdup(s);
1071 * Always return NULL, since this information
1072 * will be sent at the end of the header.
1079 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1080 /*********************************************************************
1082 * Function : client_accept
1084 * Description : Detect whether the client wants HTML or an image.
1085 * Clients do not always make this information available
1086 * in a sane way. Always passes the header through
1087 * the proxy unchanged.
1091 * 2 : s = Header string. Null terminated.
1092 * 3 : csp = Current client state (buffers, headers, etc...)
1094 * Returns : Duplicate of argument s.
1096 *********************************************************************/
1097 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1099 #ifdef FEATURE_IMAGE_DETECT_MSIE
1100 if (strstr (s, "image/gif"))
1102 /* Client will accept HTML. If this seems counterintuitive,
1105 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1109 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1111 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1116 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1120 /* the following functions add headers directly to the header list */
1123 /*********************************************************************
1125 * Function : client_cookie_adder
1127 * Description : Used in the add_client_headers list. Called from `sed'.
1130 * 1 : csp = Current client state (buffers, headers, etc...)
1134 *********************************************************************/
1135 void client_cookie_adder(struct client_state *csp)
1141 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1145 tmp = strsav(tmp, "; ");
1147 tmp = strsav(tmp, lst->str);
1150 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1154 tmp = strsav(tmp, "; ");
1157 if ((e = cookie_encode(lst->str)))
1159 tmp = strsav(tmp, e);
1168 ret = strdup("Cookie: ");
1169 ret = strsav(ret, tmp);
1170 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1171 enlist(csp->headers, ret);
1179 /*********************************************************************
1181 * Function : client_xtra_adder
1183 * Description : Used in the add_client_headers list. Called from `sed'.
1186 * 1 : csp = Current client state (buffers, headers, etc...)
1190 *********************************************************************/
1191 void client_xtra_adder(struct client_state *csp)
1193 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1195 for (lst = lst->next; lst ; lst = lst->next)
1197 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1198 enlist(csp->headers, lst->str);
1204 /*********************************************************************
1206 * Function : client_x_forwarded_adder
1208 * Description : Used in the add_client_headers list. Called from `sed'.
1211 * 1 : csp = Current client state (buffers, headers, etc...)
1215 *********************************************************************/
1216 void client_x_forwarded_adder(struct client_state *csp)
1220 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1225 if (csp->x_forwarded)
1227 p = strsav(p, csp->x_forwarded);
1228 p = strsav(p, ", ");
1229 p = strsav(p, csp->ip_addr_str);
1233 p = strsav(p, "X-Forwarded-For: ");
1234 p = strsav(p, csp->ip_addr_str);
1237 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1238 enlist(csp->headers, p);
1243 /*********************************************************************
1245 * Function : connection_close_adder
1247 * Description : Adds a "Connection: close" header to csp->headers
1248 * as a temporary fix for the needed but missing HTTP/1.1
1249 * support. Called from `sed'.
1250 * FIXME: This whole function shouldn't be neccessary!
1253 * 1 : csp = Current client state (buffers, headers, etc...)
1257 *********************************************************************/
1258 void connection_close_adder(struct client_state *csp)
1260 enlist(csp->headers, strdup("Connection: close"));
1265 /*********************************************************************
1267 * Function : server_set_cookie
1269 * Description : Handle the server "cookie" header properly.
1270 * Log cookie to the jar file. Then "crunch" it,
1271 * or accept it. Called from `sed'.
1274 * 1 : v = parser pattern that matched this header
1275 * 2 : s = header that matched this pattern
1276 * 3 : csp = Current client state (buffers, headers, etc...)
1278 * Returns : `crumble' or a newly malloc'ed string.
1280 *********************************************************************/
1281 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1283 #ifdef FEATURE_COOKIE_JAR
1284 if (csp->config->jar)
1286 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1288 #endif /* def FEATURE_COOKIE_JAR */
1290 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1292 return(crumble(v, s, csp));
1300 #ifdef FEATURE_FORCE_LOAD
1301 /*********************************************************************
1303 * Function : client_host
1305 * Description : Clean the FORCE_PREFIX out of the 'host' http
1306 * header, if we use force
1310 * 2 : s = header (from sed) to clean
1311 * 3 : csp = Current client state (buffers, headers, etc...)
1313 * Returns : A malloc'ed pointer to the cleaned host header
1315 *********************************************************************/
1316 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1318 char *cleanhost = strdup(s);
1321 strclean(cleanhost, FORCE_PREFIX);
1325 #endif /* def FEATURE_FORCE_LOAD */
1328 #ifdef FEATURE_FORCE_LOAD
1329 /*********************************************************************
1331 * Function : strclean
1333 * Description : In-Situ-Eliminate all occurances of substring in
1337 * 1 : string = string to clean
1338 * 2 : substring = substring to eliminate
1340 * Returns : Number of eliminations
1342 *********************************************************************/
1343 int strclean(const char *string, const char *substring)
1345 int hits = 0, len = strlen(substring);
1348 while((pos = strstr(string, substring)))
1355 while (*p++ != '\0');
1362 #endif /* def FEATURE_FORCE_LOAD */