1 const char parsers_rcs[] = "$Id: parsers.c,v 1.29 2001/09/24 21:09:24 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `add_to_iob', `client_cookie_adder', `client_from',
9 * `client_referrer', `client_send_cookie', `client_ua',
10 * `client_uagent', `client_x_forwarded',
11 * `client_x_forwarded_adder', `client_xtra_adder',
12 * `content_type', `crumble', `destroy_list', `enlist',
13 * `flush_socket', `free_http_request', `get_header',
14 * `list_to_text', `parse_http_request', `sed',
15 * and `server_set_cookie'.
17 * Copyright : Written by and Copyright (C) 2001 the SourceForge
18 * IJBSWA team. http://ijbswa.sourceforge.net
20 * Based on the Internet Junkbuster originally written
21 * by and Copyright (C) 1997 Anonymous Coders and
22 * Junkbusters Corporation. http://www.junkbusters.com
24 * This program is free software; you can redistribute it
25 * and/or modify it under the terms of the GNU General
26 * Public License as published by the Free Software
27 * Foundation; either version 2 of the License, or (at
28 * your option) any later version.
30 * This program is distributed in the hope that it will
31 * be useful, but WITHOUT ANY WARRANTY; without even the
32 * implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU General Public
34 * License for more details.
36 * The GNU General Public License should be included with
37 * this file. If not, you can view it at
38 * http://www.gnu.org/copyleft/gpl.html
39 * or write to the Free Software Foundation, Inc., 59
40 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 * Revision 1.29 2001/09/24 21:09:24 jongfoster
45 * Fixing 2 memory leaks that Guy spotted, where the paramater to
46 * enlist() was not being free()d.
48 * Revision 1.28 2001/09/22 16:32:28 jongfoster
49 * Removing unused #includes.
51 * Revision 1.27 2001/09/20 15:45:25 steudten
53 * add casting from size_t to int for printf()
54 * remove local variable shadow s2
56 * Revision 1.26 2001/09/16 17:05:14 jongfoster
57 * Removing unused #include showarg.h
59 * Revision 1.25 2001/09/16 13:21:27 jongfoster
60 * Changes to use new list functions.
62 * Revision 1.24 2001/09/13 23:05:50 jongfoster
63 * Changing the string paramater to the header parsers a "const".
65 * Revision 1.23 2001/09/12 18:08:19 steudten
67 * In parse_http_request() header rewriting miss the host value, so
68 * from http://www.mydomain.com the result was just " / " not
69 * http://www.mydomain.com/ in case we forward.
71 * Revision 1.22 2001/09/10 10:58:53 oes
72 * Silenced compiler warnings
74 * Revision 1.21 2001/07/31 14:46:00 oes
75 * - Persistant connections now suppressed
76 * - sed() no longer appends empty header to csp->headers
78 * Revision 1.20 2001/07/30 22:08:36 jongfoster
79 * Tidying up #defines:
80 * - All feature #defines are now of the form FEATURE_xxx
81 * - Permanently turned off WIN_GUI_EDIT
82 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
84 * Revision 1.19 2001/07/25 17:21:54 oes
85 * client_uagent now saves copy of User-Agent: header value
87 * Revision 1.18 2001/07/13 14:02:46 oes
88 * - Included fix to repair broken HTTP requests that
89 * don't contain a path, not even '/'.
90 * - Removed all #ifdef PCRS
91 * - content_type now always inspected and classified as
93 * - formatting / comments
95 * Revision 1.17 2001/06/29 21:45:41 oes
96 * Indentation, CRLF->LF, Tab-> Space
98 * Revision 1.16 2001/06/29 13:32:42 oes
100 * - Adapted free_http_request
101 * - Removed logentry from cancelled commit
103 * Revision 1.15 2001/06/03 19:12:38 oes
104 * deleted const struct interceptors
106 * Revision 1.14 2001/06/01 18:49:17 jongfoster
107 * Replaced "list_share" with "list" - the tiny memory gain was not
108 * worth the extra complexity.
110 * Revision 1.13 2001/05/31 21:30:33 jongfoster
111 * Removed list code - it's now in list.[ch]
112 * Renamed "permission" to "action", and changed many features
113 * to use the actions file rather than the global config.
115 * Revision 1.12 2001/05/31 17:33:13 oes
119 * Revision 1.11 2001/05/29 20:11:19 joergs
120 * '/ * inside comment' warning removed.
122 * Revision 1.10 2001/05/29 09:50:24 jongfoster
123 * Unified blocklist/imagelist/permissionslist.
124 * File format is still under discussion, but the internal changes
127 * Also modified interceptor behaviour:
128 * - We now intercept all URLs beginning with one of the following
129 * prefixes (and *only* these prefixes):
131 * * http://ijbswa.sf.net/config/
132 * * http://ijbswa.sourceforge.net/config/
133 * - New interceptors "home page" - go to http://i.j.b/ to see it.
134 * - Internal changes so that intercepted and fast redirect pages
135 * are not replaced with an image.
136 * - Interceptors now have the option to send a binary page direct
137 * to the client. (i.e. ijb-send-banner uses this)
138 * - Implemented show-url-info interceptor. (Which is why I needed
139 * the above interceptors changes - a typical URL is
140 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
141 * The previous mechanism would not have intercepted that, and
142 * if it had been intercepted then it then it would have replaced
145 * Revision 1.9 2001/05/28 17:26:33 jongfoster
146 * Fixing segfault if last header was crunched.
147 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
148 * can use the cross-platform sprintf() instead.)
150 * Revision 1.8 2001/05/27 22:17:04 oes
152 * - re_process_buffer no longer writes the modified buffer
153 * to the client, which was very ugly. It now returns the
154 * buffer, which it is then written by chat.
156 * - content_length now adjusts the Content-Length: header
157 * for modified documents rather than crunch()ing it.
158 * (Length info in csp->content_length, which is 0 for
159 * unmodified documents)
161 * - For this to work, sed() is called twice when filtering.
163 * Revision 1.7 2001/05/27 13:19:06 oes
164 * Patched Joergs solution for the content-length in.
166 * Revision 1.6 2001/05/26 13:39:32 jongfoster
167 * Only crunches Content-Length header if applying RE filtering.
168 * Without this fix, Microsoft Windows Update wouldn't work.
170 * Revision 1.5 2001/05/26 00:28:36 jongfoster
171 * Automatic reloading of config file.
172 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
173 * Most of the global variables have been moved to a new
174 * struct configuration_spec, accessed through csp->config->globalname
175 * Most of the globals remaining are used by the Win32 GUI.
177 * Revision 1.4 2001/05/22 18:46:04 oes
179 * - Enabled filtering banners by size rather than URL
180 * by adding patterns that replace all standard banner
181 * sizes with the "Junkbuster" gif to the re_filterfile
183 * - Enabled filtering WebBugs by providing a pattern
184 * which kills all 1x1 images
186 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
187 * which is selected by the (nonstandard and therefore
188 * capital) letter 'U' in the option string.
189 * It causes the quantifiers to be ungreedy by default.
190 * Appending a ? turns back to greedy (!).
192 * - Added a new interceptor ijb-send-banner, which
193 * sends back the "Junkbuster" gif. Without imagelist or
194 * MSIE detection support, or if tinygif = 1, or the
195 * URL isn't recognized as an imageurl, a lame HTML
196 * explanation is sent instead.
198 * - Added new feature, which permits blocking remote
199 * script redirects and firing back a local redirect
201 * The feature is conditionally compiled, i.e. it
202 * can be disabled with --disable-fast-redirects,
203 * plus it must be activated by a "fast-redirects"
204 * line in the config file, has its own log level
205 * and of course wants to be displayed by show-proxy-args
206 * Note: Boy, all the #ifdefs in 1001 locations and
207 * all the fumbling with configure.in and acconfig.h
208 * were *way* more work than the feature itself :-(
210 * - Because a generic redirect template was needed for
211 * this, tinygif = 3 now uses the same.
213 * - Moved GIFs, and other static HTTP response templates
218 * - Removed some >400 CRs again (Jon, you really worked
221 * Revision 1.3 2001/05/20 01:21:20 jongfoster
222 * Version 2.9.4 checkin.
223 * - Merged popupfile and cookiefile, and added control over PCRS
224 * filtering, in new "permissionsfile".
225 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
226 * file error you now get a message box (in the Win32 GUI) rather
227 * than the program exiting with no explanation.
228 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
230 * - Removed tabs from "config"
231 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
232 * - Bumped up version number.
234 * Revision 1.2 2001/05/17 23:02:36 oes
235 * - Made referrer option accept 'L' as a substitute for '§'
237 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
238 * Initial import of version 2.9.3 source tree
241 *********************************************************************/
248 #include <sys/types.h>
265 #include "jbsockets.h"
266 #include "miscutil.h"
268 const char parsers_h_rcs[] = PARSERS_H_VERSION;
270 /* Fix a problem with Solaris. There should be no effect on other
272 * Solaris's isspace() is a macro which uses it's argument directly
273 * as an array index. Therefore we need to make sure that high-bit
274 * characters generate +ve values, and ideally we also want to make
275 * the argument match the declared parameter type of "int".
277 * Why did they write a character function that can't take a simple
278 * "char" argument? Doh!
280 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
281 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
284 const struct parsers client_patterns[] = {
285 { "referer:", 8, client_referrer },
286 { "user-agent:", 11, client_uagent },
287 { "ua-", 3, client_ua },
288 { "from:", 5, client_from },
289 { "cookie:", 7, client_send_cookie },
290 { "x-forwarded-for:", 16, client_x_forwarded },
291 #ifdef FEATURE_DENY_GZIP
292 { "Accept-Encoding: gzip", 21, crumble },
293 #endif /* def FEATURE_DENY_GZIP */
294 #if defined(FEATURE_IMAGE_DETECT_MSIE)
295 { "Accept:", 7, client_accept },
296 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
297 #ifdef FEATURE_FORCE_LOAD
298 { "Host:", 5, client_host },
299 #endif /* def FEATURE_FORCE_LOAD */
300 /* { "if-modified-since:", 18, crumble }, */
301 { "Keep-Alive:", 11, crumble },
302 { "connection:", 11, crumble },
303 { "proxy-connection:", 17, crumble },
308 const struct parsers server_patterns[] = {
309 { "HTTP/1.1 ", 9, server_http11 },
310 { "set-cookie:", 11, server_set_cookie },
311 { "connection:", 11, crumble },
312 { "Content-Type:", 13, content_type },
313 { "Content-Length:", 15, content_length },
318 void (* const add_client_headers[])(struct client_state *) = {
320 client_x_forwarded_adder,
322 connection_close_adder,
327 void (* const add_server_headers[])(struct client_state *) = {
328 connection_close_adder,
333 /*********************************************************************
335 * Function : flush_socket
337 * Description : Write any pending "buffered" content.
340 * 1 : fd = file descriptor of the socket to read
341 * 2 : csp = Current client state (buffers, headers, etc...)
343 * Returns : On success, the number of bytes written are returned (zero
344 * indicates nothing was written). On error, -1 is returned,
345 * and errno is set appropriately. If count is zero and the
346 * file descriptor refers to a regular file, 0 will be
347 * returned without causing any other effect. For a special
348 * file, the results are not portable.
350 *********************************************************************/
351 int flush_socket(int fd, struct client_state *csp)
353 struct iob *iob = csp->iob;
354 int n = iob->eod - iob->cur;
361 n = write_socket(fd, iob->cur, n);
362 iob->eod = iob->cur = iob->buf;
368 /*********************************************************************
370 * Function : add_to_iob
372 * Description : Add content to the buffered page.
375 * 1 : csp = Current client state (buffers, headers, etc...)
376 * 2 : buf = holds the content to be added to the page
377 * 3 : n = number of bytes to be added
379 * Returns : Number of bytes in the content buffer.
381 *********************************************************************/
382 int add_to_iob(struct client_state *csp, char *buf, int n)
384 struct iob *iob = csp->iob;
388 have = iob->eod - iob->cur;
397 if ((p = (char *)malloc(need + 1)) == NULL)
399 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
405 /* there is something in the buffer - save it */
406 memcpy(p, iob->cur, have);
408 /* replace the buffer with the new space */
412 /* point to the end of the data */
417 /* the buffer is empty, free it and reinitialize */
422 /* copy the new data into the iob buffer */
425 /* point to the end of the data */
428 /* null terminate == cheap insurance */
431 /* set the pointers to the new values */
440 /*********************************************************************
442 * Function : get_header
444 * Description : This (odd) routine will parse the csp->iob
447 * 1 : csp = Current client state (buffers, headers, etc...)
449 * Returns : Any one of the following:
451 * 1) a pointer to a dynamically allocated string that contains a header line
452 * 2) NULL indicating that the end of the header was reached
453 * 3) "" indicating that the end of the iob was reached before finding
454 * a complete header line.
456 *********************************************************************/
457 char *get_header(struct client_state *csp)
463 if ((iob->cur == NULL)
464 || ((p = strchr(iob->cur, '\n')) == NULL))
466 return(""); /* couldn't find a complete header */
471 ret = strdup(iob->cur);
475 if ((q = strchr(ret, '\r'))) *q = '\0';
477 /* is this a blank linke (i.e. the end of the header) ? */
489 /*********************************************************************
493 * Description : add, delete or modify lines in the HTTP header streams.
494 * On entry, it receives a linked list of headers space
495 * that was allocated dynamically (both the list nodes
496 * and the header contents).
498 * As a side effect it frees the space used by the original
502 * 1 : pats = list of patterns to match against headers
503 * 2 : more_headers = list of functions to add more
504 * headers (client or server)
505 * 3 : csp = Current client state (buffers, headers, etc...)
507 * Returns : Single pointer to a fully formed header.
509 *********************************************************************/
510 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
512 struct list_entry *p;
513 const struct parsers *v;
517 for (v = pats; v->str ; v++)
519 for (p = csp->headers->first; p ; p = p->next)
521 /* Header crunch()ed in previous run? -> ignore */
522 if (p->str == NULL) continue;
524 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
526 if (strncmpic(p->str, v->str, v->len) == 0)
528 hdr = v->parser(v, p->str, csp);
529 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
535 /* place any additional headers on the csp->headers list */
536 for (f = more_headers; *f ; f++)
541 hdr = list_to_text(csp->headers);
548 /*********************************************************************
550 * Function : free_http_request
552 * Description : Freez a http_request structure
555 * 1 : http = points to a http_request structure to free
559 *********************************************************************/
560 void free_http_request(struct http_request *http)
565 freez(http->hostport);
568 freez(http->host_ip_addr_str);
569 freez(http->user_agent);
574 /*********************************************************************
576 * Function : parse_http_request
578 * Description : Parse out the host and port from the URL. Find the
579 * hostname & path, port (if ':'), and/or password (if '@')
582 * 1 : req = URL (or is it URI?) to break down
583 * 2 : http = pointer to the http structure to hold elements
584 * 3 : csp = Current client state (buffers, headers, etc...)
588 *********************************************************************/
589 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
591 char *buf, *v[10], *url, *p, *save_url;
594 memset(http, '\0', sizeof(*http));
596 http->cmd = strdup(req);
599 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
603 /* this could be a CONNECT request */
604 if (strcmpic(v[0], "connect") == 0)
607 http->gpc = strdup(v[0]);
608 http->hostport = strdup(v[1]);
609 http->ver = strdup(v[2]);
612 /* or it could be a GET or a POST (possibly webDAV too) */
613 if ((0 == strcmpic(v[0], "get"))
614 || (0 == strcmpic(v[0], "head"))
615 || (0 == strcmpic(v[0], "post"))
616 /* These are the headers as defined in RFC2518 to add webDAV support: */
617 || (0 == strcmpic(v[0], "propfind"))
618 || (0 == strcmpic(v[0], "proppatch"))
619 || (0 == strcmpic(v[0], "move"))
620 || (0 == strcmpic(v[0], "copy"))
621 || (0 == strcmpic(v[0], "mkcol"))
622 || (0 == strcmpic(v[0], "lock"))
623 || (0 == strcmpic(v[0], "unlock"))
627 http->gpc = strdup(v[0]);
629 /* since we don't support HTTP/1.1 we must not send it */
630 if (!strcmpic(v[2], "HTTP/1.1"))
632 http->ver = strdup("HTTP/1.0");
633 /* change cmd too (forwaring) */
635 http->cmd = strsav(http->cmd, http->gpc);
636 http->cmd = strsav(http->cmd, " ");
637 http->cmd = strsav(http->cmd, url);
638 http->cmd = strsav(http->cmd, " ");
639 http->cmd = strsav(http->cmd, http->ver);
643 http->ver = strdup(v[2]);
647 if (strncmpic(url, "http://", 7) == 0)
651 else if (strncmpic(url, "https://", 8) == 0)
662 if ((p = strchr(url, '/')))
664 http->path = strdup(p);
666 http->hostport = strdup(url);
669 * Repair broken HTTP requests that don't contain a path
673 /* Repair hostport & path */
674 http->path = strdup("/");
675 http->hostport = strdup(url);
677 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
679 http->cmd = strsav(http->cmd, http->gpc);
680 http->cmd = strsav(http->cmd, " ");
681 http->cmd = strsav(http->cmd, save_url);
682 http->cmd = strsav(http->cmd, "/ ");
683 http->cmd = strsav(http->cmd, http->ver);
692 if (http->hostport == NULL)
694 free_http_request(http);
698 buf = strdup(http->hostport);
701 /* check if url contains password */
702 n = ssplit(buf, "@", v, SZ(v), 1, 1);
705 char * newbuf = NULL;
706 newbuf = strdup(v[1]);
711 n = ssplit(buf, ":", v, SZ(v), 1, 1);
715 http->host = strdup(v[0]);
721 http->host = strdup(v[0]);
722 http->port = atoi(v[1]);
727 if (http->host == NULL)
729 free_http_request(http);
732 if (http->path == NULL)
734 http->path = strdup("");
740 /* here begins the family of parser functions that reformat header lines */
743 /*********************************************************************
747 * Description : This is called if a header matches a pattern to "crunch"
750 * 1 : v = Pointer to parsers structure, which basically holds
751 * headers (client or server) that we want to "crunch"
752 * 2 : s = header (from sed) to "crunch"
753 * 3 : csp = Current client state (buffers, headers, etc...)
755 * Returns : Always NULL.
757 *********************************************************************/
758 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
760 log_error(LOG_LEVEL_HEADER, "crunch!");
766 /*********************************************************************
768 * Function : content_type
770 * Description : Is this a text/.* or javascript MIME Type?
774 * 2 : s = header string we are "considering"
775 * 3 : csp = Current client state (buffers, headers, etc...)
777 * Returns : A duplicate string pointer to this header (ie. pass thru)
779 *********************************************************************/
780 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
782 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
783 csp->content_type = CT_TEXT;
784 else if (strstr(s, " image/gif"))
785 csp->content_type = CT_GIF;
787 csp->content_type = 0;
794 /*********************************************************************
796 * Function : content_length
798 * Description : Adjust Content-Length header if we modified
803 * 2 : s = header string we are "considering"
804 * 3 : csp = Current client state (buffers, headers, etc...)
806 * Returns : A duplicate string pointer to this header (ie. pass thru)
808 *********************************************************************/
809 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
811 if (csp->content_length != 0) /* Content has been modified */
813 char * s2 = (char *) zalloc(100);
814 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
816 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
827 /*********************************************************************
829 * Function : client_referrer
831 * Description : Handle the "referer" config setting properly.
836 * 2 : s = header (from sed) to "crunch"
837 * 3 : csp = Current client state (buffers, headers, etc...)
839 * Returns : NULL if crunched, or a malloc'ed string with the original
842 *********************************************************************/
843 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
847 #ifdef FEATURE_FORCE_LOAD
848 /* Since the referrer can include the prefix even
849 * even if the request itself is non-forced, we must
850 * clean it unconditionally
852 strclean(s, FORCE_PREFIX);
853 #endif /* def FEATURE_FORCE_LOAD */
856 csp->referrer = strdup(s);
857 #endif /* def FEATURE_TRUST */
860 * Are we sending referer?
862 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
867 newval = csp->action->string[ACTION_STRING_REFERER];
870 * Are we blocking referer?
872 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
874 log_error(LOG_LEVEL_HEADER, "crunch!");
879 * Are we forging referer?
881 if (0 == strcmpic(newval, "forge"))
884 * Forge a referer as http://[hostname:port of REQUEST]/
885 * to fool stupid checks for in-site links
887 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
888 s2 = strsav(NULL, "Referer: ");
889 s2 = strsav(s2, "http://");
890 s2 = strsav(s2, csp->http->hostport);
891 s2 = strsav(s2, "/");
896 * Have we got a fixed referer?
898 if (0 == strncmpic(newval, "http://", 7))
901 * We have a specific (fixed) referer we want to send.
905 log_error(LOG_LEVEL_HEADER, "modified");
907 s3 = strsav( NULL, "Referer: " );
908 s3 = strsav( s3, newval );
912 /* Should never get here! */
913 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
916 * Forge is probably the best default.
918 * Forge a referer as http://[hostname:port of REQUEST]/
919 * to fool stupid checks for in-site links
921 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
922 s2 = strsav(NULL, "Referer: ");
923 s2 = strsav(s2, "http://");
924 s2 = strsav(s2, csp->http->hostport);
925 s2 = strsav(s2, "/");
930 /*********************************************************************
932 * Function : client_uagent
934 * Description : Handle the "user-agent" config setting properly
935 * and remember its original value to enable browser
936 * bug workarounds. Called from `sed'.
940 * 2 : s = header (from sed) to "crunch"
941 * 3 : csp = Current client state (buffers, headers, etc...)
943 * Returns : A malloc'ed pointer to the default agent, or
944 * a malloc'ed string pointer to this header (ie. pass thru).
946 *********************************************************************/
947 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
952 /* Save the client's User-Agent: value */
955 csp->http->user_agent = strdup(s + 12);
958 #ifdef FEATURE_IMAGE_DETECT_MSIE
959 if (strstr (s, "MSIE "))
961 /* This is Microsoft Internet Explorer.
962 * Enable auto-detect.
964 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
966 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
968 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
973 newval = csp->action->string[ACTION_STRING_USER_AGENT];
979 log_error(LOG_LEVEL_HEADER, "modified");
981 s2 = strsav( NULL, "User-Agent: " );
982 s2 = strsav( s2, newval );
988 /*********************************************************************
990 * Function : client_ua
992 * Description : Handle "ua-" headers properly. Called from `sed'.
996 * 2 : s = header (from sed) to "crunch"
997 * 3 : csp = Current client state (buffers, headers, etc...)
999 * Returns : NULL if crunched, or a malloc'ed string to original header
1001 *********************************************************************/
1002 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1004 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1010 log_error(LOG_LEVEL_HEADER, "crunch!");
1016 /*********************************************************************
1018 * Function : client_from
1020 * Description : Handle the "from" config setting properly.
1021 * Called from `sed'.
1025 * 2 : s = header (from sed) to "crunch"
1026 * 3 : csp = Current client state (buffers, headers, etc...)
1028 * Returns : NULL if crunched, or a malloc'ed string to
1029 * modified/original header.
1031 *********************************************************************/
1032 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1034 const char * newval;
1037 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1042 newval = csp->action->string[ACTION_STRING_FROM];
1045 * Are we blocking referer?
1047 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1049 log_error(LOG_LEVEL_HEADER, "crunch!");
1053 log_error(LOG_LEVEL_HEADER, " modified");
1055 s2 = strsav( NULL, "From: " );
1056 s2 = strsav( s2, newval );
1062 /*********************************************************************
1064 * Function : client_send_cookie
1066 * Description : Handle the "cookie" header properly. Called from `sed'.
1067 * If cookie is accepted, add it to the cookie_list,
1068 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1071 * 1 : v = pattern of cookie `sed' found matching
1072 * 2 : s = header (from sed) to "crunch"
1073 * 3 : csp = Current client state (buffers, headers, etc...)
1075 * Returns : Always NULL.
1077 *********************************************************************/
1078 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1080 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1082 enlist(csp->cookie_list, s + v->len + 1);
1086 log_error(LOG_LEVEL_HEADER, " crunch!");
1090 * Always return NULL here. The cookie header
1091 * will be sent at the end of the header.
1098 /*********************************************************************
1100 * Function : client_x_forwarded
1102 * Description : Handle the "x-forwarded-for" config setting properly,
1103 * also used in the add_client_headers list. Called from `sed'.
1107 * 2 : s = header (from sed) to "crunch"
1108 * 3 : csp = Current client state (buffers, headers, etc...)
1110 * Returns : Always NULL.
1112 *********************************************************************/
1113 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1115 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1117 /* Save it so we can re-add it later */
1118 csp->x_forwarded = strdup(s);
1122 * Always return NULL, since this information
1123 * will be sent at the end of the header.
1130 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1131 /*********************************************************************
1133 * Function : client_accept
1135 * Description : Detect whether the client wants HTML or an image.
1136 * Clients do not always make this information available
1137 * in a sane way. Always passes the header through
1138 * the proxy unchanged.
1142 * 2 : s = Header string. Null terminated.
1143 * 3 : csp = Current client state (buffers, headers, etc...)
1145 * Returns : Duplicate of argument s.
1147 *********************************************************************/
1148 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1150 #ifdef FEATURE_IMAGE_DETECT_MSIE
1151 if (strstr (s, "image/gif"))
1153 /* Client will accept HTML. If this seems counterintuitive,
1156 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1160 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1162 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1167 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1171 /* the following functions add headers directly to the header list */
1174 /*********************************************************************
1176 * Function : client_cookie_adder
1178 * Description : Used in the add_client_headers list. Called from `sed'.
1181 * 1 : csp = Current client state (buffers, headers, etc...)
1185 *********************************************************************/
1186 void client_cookie_adder(struct client_state *csp)
1188 struct list_entry *lst;
1192 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1196 tmp = strsav(tmp, "; ");
1198 tmp = strsav(tmp, lst->str);
1201 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1205 tmp = strsav(tmp, "; ");
1208 if ((e = cookie_encode(lst->str)))
1210 tmp = strsav(tmp, e);
1219 ret = strdup("Cookie: ");
1220 ret = strsav(ret, tmp);
1221 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1222 enlist(csp->headers, ret);
1230 /*********************************************************************
1232 * Function : client_xtra_adder
1234 * Description : Used in the add_client_headers list. Called from `sed'.
1237 * 1 : csp = Current client state (buffers, headers, etc...)
1241 *********************************************************************/
1242 void client_xtra_adder(struct client_state *csp)
1244 struct list_entry *lst;
1246 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1247 lst ; lst = lst->next)
1249 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1250 enlist(csp->headers, lst->str);
1256 /*********************************************************************
1258 * Function : client_x_forwarded_adder
1260 * Description : Used in the add_client_headers list. Called from `sed'.
1263 * 1 : csp = Current client state (buffers, headers, etc...)
1267 *********************************************************************/
1268 void client_x_forwarded_adder(struct client_state *csp)
1272 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1277 if (csp->x_forwarded)
1279 p = strsav(p, csp->x_forwarded);
1280 p = strsav(p, ", ");
1281 p = strsav(p, csp->ip_addr_str);
1285 p = strsav(p, "X-Forwarded-For: ");
1286 p = strsav(p, csp->ip_addr_str);
1289 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1290 enlist(csp->headers, p);
1296 /*********************************************************************
1298 * Function : connection_close_adder
1300 * Description : Adds a "Connection: close" header to csp->headers
1301 * as a temporary fix for the needed but missing HTTP/1.1
1302 * support. Called from `sed'.
1303 * FIXME: This whole function shouldn't be neccessary!
1306 * 1 : csp = Current client state (buffers, headers, etc...)
1310 *********************************************************************/
1311 void connection_close_adder(struct client_state *csp)
1313 enlist(csp->headers, "Connection: close");
1317 /*********************************************************************
1319 * Function : server_http11
1321 * Description : Rewrite HTTP/1.1 answers to HTTP/1.0 until we add
1322 * HTTP/1.1 support. Called from `sed'.
1325 * 1 : v = parser pattern that matched this header
1326 * 2 : s = header that matched this pattern
1327 * 3 : csp = Current client state (buffers, headers, etc...)
1329 * Returns : "HTTP/1.0" answer.
1331 *********************************************************************/
1332 char *server_http11(const struct parsers *v, const char *s, struct client_state *csp)
1337 ret[7] = '0'; /* "HTTP/1.1 ..." -> "HTTP/1.0 ..." */
1343 /*********************************************************************
1345 * Function : server_set_cookie
1347 * Description : Handle the server "cookie" header properly.
1348 * Log cookie to the jar file. Then "crunch" it,
1349 * or accept it. Called from `sed'.
1352 * 1 : v = parser pattern that matched this header
1353 * 2 : s = header that matched this pattern
1354 * 3 : csp = Current client state (buffers, headers, etc...)
1356 * Returns : `crumble' or a newly malloc'ed string.
1358 *********************************************************************/
1359 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1361 #ifdef FEATURE_COOKIE_JAR
1362 if (csp->config->jar)
1364 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1366 #endif /* def FEATURE_COOKIE_JAR */
1368 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1370 return(crumble(v, s, csp));
1378 #ifdef FEATURE_FORCE_LOAD
1379 /*********************************************************************
1381 * Function : client_host
1383 * Description : Clean the FORCE_PREFIX out of the 'host' http
1384 * header, if we use force
1388 * 2 : s = header (from sed) to clean
1389 * 3 : csp = Current client state (buffers, headers, etc...)
1391 * Returns : A malloc'ed pointer to the cleaned host header
1393 *********************************************************************/
1394 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1396 char *cleanhost = strdup(s);
1400 strclean(cleanhost, FORCE_PREFIX);
1405 #endif /* def FEATURE_FORCE_LOAD */
1408 #ifdef FEATURE_FORCE_LOAD
1409 /*********************************************************************
1411 * Function : strclean
1413 * Description : In-Situ-Eliminate all occurances of substring in
1417 * 1 : string = string to clean
1418 * 2 : substring = substring to eliminate
1420 * Returns : Number of eliminations
1422 *********************************************************************/
1423 int strclean(const char *string, const char *substring)
1425 int hits = 0, len = strlen(substring);
1428 while((pos = strstr(string, substring)))
1435 while (*p++ != '\0');
1442 #endif /* def FEATURE_FORCE_LOAD */