1 const char parsers_rcs[] = "$Id: parsers.c,v 1.30 2001/09/29 12:56:03 joergs 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.30 2001/09/29 12:56:03 joergs
45 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
47 * Revision 1.29 2001/09/24 21:09:24 jongfoster
48 * Fixing 2 memory leaks that Guy spotted, where the paramater to
49 * enlist() was not being free()d.
51 * Revision 1.28 2001/09/22 16:32:28 jongfoster
52 * Removing unused #includes.
54 * Revision 1.27 2001/09/20 15:45:25 steudten
56 * add casting from size_t to int for printf()
57 * remove local variable shadow s2
59 * Revision 1.26 2001/09/16 17:05:14 jongfoster
60 * Removing unused #include showarg.h
62 * Revision 1.25 2001/09/16 13:21:27 jongfoster
63 * Changes to use new list functions.
65 * Revision 1.24 2001/09/13 23:05:50 jongfoster
66 * Changing the string paramater to the header parsers a "const".
68 * Revision 1.23 2001/09/12 18:08:19 steudten
70 * In parse_http_request() header rewriting miss the host value, so
71 * from http://www.mydomain.com the result was just " / " not
72 * http://www.mydomain.com/ in case we forward.
74 * Revision 1.22 2001/09/10 10:58:53 oes
75 * Silenced compiler warnings
77 * Revision 1.21 2001/07/31 14:46:00 oes
78 * - Persistant connections now suppressed
79 * - sed() no longer appends empty header to csp->headers
81 * Revision 1.20 2001/07/30 22:08:36 jongfoster
82 * Tidying up #defines:
83 * - All feature #defines are now of the form FEATURE_xxx
84 * - Permanently turned off WIN_GUI_EDIT
85 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
87 * Revision 1.19 2001/07/25 17:21:54 oes
88 * client_uagent now saves copy of User-Agent: header value
90 * Revision 1.18 2001/07/13 14:02:46 oes
91 * - Included fix to repair broken HTTP requests that
92 * don't contain a path, not even '/'.
93 * - Removed all #ifdef PCRS
94 * - content_type now always inspected and classified as
96 * - formatting / comments
98 * Revision 1.17 2001/06/29 21:45:41 oes
99 * Indentation, CRLF->LF, Tab-> Space
101 * Revision 1.16 2001/06/29 13:32:42 oes
103 * - Adapted free_http_request
104 * - Removed logentry from cancelled commit
106 * Revision 1.15 2001/06/03 19:12:38 oes
107 * deleted const struct interceptors
109 * Revision 1.14 2001/06/01 18:49:17 jongfoster
110 * Replaced "list_share" with "list" - the tiny memory gain was not
111 * worth the extra complexity.
113 * Revision 1.13 2001/05/31 21:30:33 jongfoster
114 * Removed list code - it's now in list.[ch]
115 * Renamed "permission" to "action", and changed many features
116 * to use the actions file rather than the global config.
118 * Revision 1.12 2001/05/31 17:33:13 oes
122 * Revision 1.11 2001/05/29 20:11:19 joergs
123 * '/ * inside comment' warning removed.
125 * Revision 1.10 2001/05/29 09:50:24 jongfoster
126 * Unified blocklist/imagelist/permissionslist.
127 * File format is still under discussion, but the internal changes
130 * Also modified interceptor behaviour:
131 * - We now intercept all URLs beginning with one of the following
132 * prefixes (and *only* these prefixes):
134 * * http://ijbswa.sf.net/config/
135 * * http://ijbswa.sourceforge.net/config/
136 * - New interceptors "home page" - go to http://i.j.b/ to see it.
137 * - Internal changes so that intercepted and fast redirect pages
138 * are not replaced with an image.
139 * - Interceptors now have the option to send a binary page direct
140 * to the client. (i.e. ijb-send-banner uses this)
141 * - Implemented show-url-info interceptor. (Which is why I needed
142 * the above interceptors changes - a typical URL is
143 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
144 * The previous mechanism would not have intercepted that, and
145 * if it had been intercepted then it then it would have replaced
148 * Revision 1.9 2001/05/28 17:26:33 jongfoster
149 * Fixing segfault if last header was crunched.
150 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
151 * can use the cross-platform sprintf() instead.)
153 * Revision 1.8 2001/05/27 22:17:04 oes
155 * - re_process_buffer no longer writes the modified buffer
156 * to the client, which was very ugly. It now returns the
157 * buffer, which it is then written by chat.
159 * - content_length now adjusts the Content-Length: header
160 * for modified documents rather than crunch()ing it.
161 * (Length info in csp->content_length, which is 0 for
162 * unmodified documents)
164 * - For this to work, sed() is called twice when filtering.
166 * Revision 1.7 2001/05/27 13:19:06 oes
167 * Patched Joergs solution for the content-length in.
169 * Revision 1.6 2001/05/26 13:39:32 jongfoster
170 * Only crunches Content-Length header if applying RE filtering.
171 * Without this fix, Microsoft Windows Update wouldn't work.
173 * Revision 1.5 2001/05/26 00:28:36 jongfoster
174 * Automatic reloading of config file.
175 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
176 * Most of the global variables have been moved to a new
177 * struct configuration_spec, accessed through csp->config->globalname
178 * Most of the globals remaining are used by the Win32 GUI.
180 * Revision 1.4 2001/05/22 18:46:04 oes
182 * - Enabled filtering banners by size rather than URL
183 * by adding patterns that replace all standard banner
184 * sizes with the "Junkbuster" gif to the re_filterfile
186 * - Enabled filtering WebBugs by providing a pattern
187 * which kills all 1x1 images
189 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
190 * which is selected by the (nonstandard and therefore
191 * capital) letter 'U' in the option string.
192 * It causes the quantifiers to be ungreedy by default.
193 * Appending a ? turns back to greedy (!).
195 * - Added a new interceptor ijb-send-banner, which
196 * sends back the "Junkbuster" gif. Without imagelist or
197 * MSIE detection support, or if tinygif = 1, or the
198 * URL isn't recognized as an imageurl, a lame HTML
199 * explanation is sent instead.
201 * - Added new feature, which permits blocking remote
202 * script redirects and firing back a local redirect
204 * The feature is conditionally compiled, i.e. it
205 * can be disabled with --disable-fast-redirects,
206 * plus it must be activated by a "fast-redirects"
207 * line in the config file, has its own log level
208 * and of course wants to be displayed by show-proxy-args
209 * Note: Boy, all the #ifdefs in 1001 locations and
210 * all the fumbling with configure.in and acconfig.h
211 * were *way* more work than the feature itself :-(
213 * - Because a generic redirect template was needed for
214 * this, tinygif = 3 now uses the same.
216 * - Moved GIFs, and other static HTTP response templates
221 * - Removed some >400 CRs again (Jon, you really worked
224 * Revision 1.3 2001/05/20 01:21:20 jongfoster
225 * Version 2.9.4 checkin.
226 * - Merged popupfile and cookiefile, and added control over PCRS
227 * filtering, in new "permissionsfile".
228 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
229 * file error you now get a message box (in the Win32 GUI) rather
230 * than the program exiting with no explanation.
231 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
233 * - Removed tabs from "config"
234 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
235 * - Bumped up version number.
237 * Revision 1.2 2001/05/17 23:02:36 oes
238 * - Made referrer option accept 'L' as a substitute for '§'
240 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
241 * Initial import of version 2.9.3 source tree
244 *********************************************************************/
251 #include <sys/types.h>
268 #include "jbsockets.h"
269 #include "miscutil.h"
271 const char parsers_h_rcs[] = PARSERS_H_VERSION;
273 /* Fix a problem with Solaris. There should be no effect on other
275 * Solaris's isspace() is a macro which uses it's argument directly
276 * as an array index. Therefore we need to make sure that high-bit
277 * characters generate +ve values, and ideally we also want to make
278 * the argument match the declared parameter type of "int".
280 * Why did they write a character function that can't take a simple
281 * "char" argument? Doh!
283 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
284 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
287 const struct parsers client_patterns[] = {
288 { "referer:", 8, client_referrer },
289 { "user-agent:", 11, client_uagent },
290 { "ua-", 3, client_ua },
291 { "from:", 5, client_from },
292 { "cookie:", 7, client_send_cookie },
293 { "x-forwarded-for:", 16, client_x_forwarded },
294 #ifdef FEATURE_DENY_GZIP
295 { "Accept-Encoding: gzip", 21, crumble },
296 #endif /* def FEATURE_DENY_GZIP */
297 #if defined(FEATURE_IMAGE_DETECT_MSIE)
298 { "Accept:", 7, client_accept },
299 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
300 #ifdef FEATURE_FORCE_LOAD
301 { "Host:", 5, client_host },
302 #endif /* def FEATURE_FORCE_LOAD */
303 /* { "if-modified-since:", 18, crumble }, */
304 { "Keep-Alive:", 11, crumble },
305 { "connection:", 11, crumble },
306 { "proxy-connection:", 17, crumble },
311 const struct parsers server_patterns[] = {
312 { "HTTP/1.1 ", 9, server_http11 },
313 { "set-cookie:", 11, server_set_cookie },
314 { "connection:", 11, crumble },
315 { "Content-Type:", 13, content_type },
316 { "Content-Length:", 15, content_length },
317 { "Keep-Alive:", 11, crumble },
322 void (* const add_client_headers[])(struct client_state *) = {
324 client_x_forwarded_adder,
326 connection_close_adder,
331 void (* const add_server_headers[])(struct client_state *) = {
332 connection_close_adder,
337 /*********************************************************************
339 * Function : flush_socket
341 * Description : Write any pending "buffered" content.
344 * 1 : fd = file descriptor of the socket to read
345 * 2 : csp = Current client state (buffers, headers, etc...)
347 * Returns : On success, the number of bytes written are returned (zero
348 * indicates nothing was written). On error, -1 is returned,
349 * and errno is set appropriately. If count is zero and the
350 * file descriptor refers to a regular file, 0 will be
351 * returned without causing any other effect. For a special
352 * file, the results are not portable.
354 *********************************************************************/
355 int flush_socket(int fd, struct client_state *csp)
357 struct iob *iob = csp->iob;
358 int n = iob->eod - iob->cur;
365 n = write_socket(fd, iob->cur, n);
366 iob->eod = iob->cur = iob->buf;
372 /*********************************************************************
374 * Function : add_to_iob
376 * Description : Add content to the buffered page.
379 * 1 : csp = Current client state (buffers, headers, etc...)
380 * 2 : buf = holds the content to be added to the page
381 * 3 : n = number of bytes to be added
383 * Returns : Number of bytes in the content buffer.
385 *********************************************************************/
386 int add_to_iob(struct client_state *csp, char *buf, int n)
388 struct iob *iob = csp->iob;
392 have = iob->eod - iob->cur;
401 if ((p = (char *)malloc(need + 1)) == NULL)
403 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
409 /* there is something in the buffer - save it */
410 memcpy(p, iob->cur, have);
412 /* replace the buffer with the new space */
416 /* point to the end of the data */
421 /* the buffer is empty, free it and reinitialize */
426 /* copy the new data into the iob buffer */
429 /* point to the end of the data */
432 /* null terminate == cheap insurance */
435 /* set the pointers to the new values */
444 /*********************************************************************
446 * Function : get_header
448 * Description : This (odd) routine will parse the csp->iob
451 * 1 : csp = Current client state (buffers, headers, etc...)
453 * Returns : Any one of the following:
455 * 1) a pointer to a dynamically allocated string that contains a header line
456 * 2) NULL indicating that the end of the header was reached
457 * 3) "" indicating that the end of the iob was reached before finding
458 * a complete header line.
460 *********************************************************************/
461 char *get_header(struct client_state *csp)
467 if ((iob->cur == NULL)
468 || ((p = strchr(iob->cur, '\n')) == NULL))
470 return(""); /* couldn't find a complete header */
475 ret = strdup(iob->cur);
479 if ((q = strchr(ret, '\r'))) *q = '\0';
481 /* is this a blank linke (i.e. the end of the header) ? */
493 /*********************************************************************
497 * Description : add, delete or modify lines in the HTTP header streams.
498 * On entry, it receives a linked list of headers space
499 * that was allocated dynamically (both the list nodes
500 * and the header contents).
502 * As a side effect it frees the space used by the original
506 * 1 : pats = list of patterns to match against headers
507 * 2 : more_headers = list of functions to add more
508 * headers (client or server)
509 * 3 : csp = Current client state (buffers, headers, etc...)
511 * Returns : Single pointer to a fully formed header.
513 *********************************************************************/
514 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
516 struct list_entry *p;
517 const struct parsers *v;
521 for (v = pats; v->str ; v++)
523 for (p = csp->headers->first; p ; p = p->next)
525 /* Header crunch()ed in previous run? -> ignore */
526 if (p->str == NULL) continue;
528 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
530 if (strncmpic(p->str, v->str, v->len) == 0)
532 hdr = v->parser(v, p->str, csp);
533 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
539 /* place any additional headers on the csp->headers list */
540 for (f = more_headers; *f ; f++)
545 hdr = list_to_text(csp->headers);
552 /*********************************************************************
554 * Function : free_http_request
556 * Description : Freez a http_request structure
559 * 1 : http = points to a http_request structure to free
563 *********************************************************************/
564 void free_http_request(struct http_request *http)
569 freez(http->hostport);
572 freez(http->host_ip_addr_str);
573 freez(http->user_agent);
578 /*********************************************************************
580 * Function : parse_http_request
582 * Description : Parse out the host and port from the URL. Find the
583 * hostname & path, port (if ':'), and/or password (if '@')
586 * 1 : req = URL (or is it URI?) to break down
587 * 2 : http = pointer to the http structure to hold elements
588 * 3 : csp = Current client state (buffers, headers, etc...)
592 *********************************************************************/
593 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
595 char *buf, *v[10], *url, *p, *save_url;
598 memset(http, '\0', sizeof(*http));
600 http->cmd = strdup(req);
603 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
607 /* this could be a CONNECT request */
608 if (strcmpic(v[0], "connect") == 0)
611 http->gpc = strdup(v[0]);
612 http->hostport = strdup(v[1]);
613 http->ver = strdup(v[2]);
616 /* or it could be a GET or a POST (possibly webDAV too) */
617 if ((0 == strcmpic(v[0], "get"))
618 || (0 == strcmpic(v[0], "head"))
619 || (0 == strcmpic(v[0], "post"))
620 /* These are the headers as defined in RFC2518 to add webDAV support: */
621 || (0 == strcmpic(v[0], "propfind"))
622 || (0 == strcmpic(v[0], "proppatch"))
623 || (0 == strcmpic(v[0], "move"))
624 || (0 == strcmpic(v[0], "copy"))
625 || (0 == strcmpic(v[0], "mkcol"))
626 || (0 == strcmpic(v[0], "lock"))
627 || (0 == strcmpic(v[0], "unlock"))
631 http->gpc = strdup(v[0]);
633 /* since we don't support HTTP/1.1 we must not send it */
634 if (!strcmpic(v[2], "HTTP/1.1"))
636 http->ver = strdup("HTTP/1.0");
637 /* change cmd too (forwaring) */
639 http->cmd = strsav(http->cmd, http->gpc);
640 http->cmd = strsav(http->cmd, " ");
641 http->cmd = strsav(http->cmd, url);
642 http->cmd = strsav(http->cmd, " ");
643 http->cmd = strsav(http->cmd, http->ver);
647 http->ver = strdup(v[2]);
651 if (strncmpic(url, "http://", 7) == 0)
655 else if (strncmpic(url, "https://", 8) == 0)
666 if ((p = strchr(url, '/')))
668 http->path = strdup(p);
670 http->hostport = strdup(url);
673 * Repair broken HTTP requests that don't contain a path
677 /* Repair hostport & path */
678 http->path = strdup("/");
679 http->hostport = strdup(url);
681 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
683 http->cmd = strsav(http->cmd, http->gpc);
684 http->cmd = strsav(http->cmd, " ");
685 http->cmd = strsav(http->cmd, save_url);
686 http->cmd = strsav(http->cmd, "/ ");
687 http->cmd = strsav(http->cmd, http->ver);
696 if (http->hostport == NULL)
698 free_http_request(http);
702 buf = strdup(http->hostport);
705 /* check if url contains password */
706 n = ssplit(buf, "@", v, SZ(v), 1, 1);
709 char * newbuf = NULL;
710 newbuf = strdup(v[1]);
715 n = ssplit(buf, ":", v, SZ(v), 1, 1);
719 http->host = strdup(v[0]);
725 http->host = strdup(v[0]);
726 http->port = atoi(v[1]);
731 if (http->host == NULL)
733 free_http_request(http);
736 if (http->path == NULL)
738 http->path = strdup("");
744 /* here begins the family of parser functions that reformat header lines */
747 /*********************************************************************
751 * Description : This is called if a header matches a pattern to "crunch"
754 * 1 : v = Pointer to parsers structure, which basically holds
755 * headers (client or server) that we want to "crunch"
756 * 2 : s = header (from sed) to "crunch"
757 * 3 : csp = Current client state (buffers, headers, etc...)
759 * Returns : Always NULL.
761 *********************************************************************/
762 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
764 log_error(LOG_LEVEL_HEADER, "crunch!");
770 /*********************************************************************
772 * Function : content_type
774 * Description : Is this a text/.* or javascript MIME Type?
778 * 2 : s = header string we are "considering"
779 * 3 : csp = Current client state (buffers, headers, etc...)
781 * Returns : A duplicate string pointer to this header (ie. pass thru)
783 *********************************************************************/
784 char *content_type(const struct parsers *v, const char *s, struct client_state *csp)
786 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
787 csp->content_type = CT_TEXT;
788 else if (strstr(s, " image/gif"))
789 csp->content_type = CT_GIF;
791 csp->content_type = 0;
798 /*********************************************************************
800 * Function : content_length
802 * Description : Adjust Content-Length header if we modified
807 * 2 : s = header string we are "considering"
808 * 3 : csp = Current client state (buffers, headers, etc...)
810 * Returns : A duplicate string pointer to this header (ie. pass thru)
812 *********************************************************************/
813 char *content_length(const struct parsers *v, const char *s, struct client_state *csp)
815 if (csp->content_length != 0) /* Content has been modified */
817 char * s2 = (char *) zalloc(100);
818 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
820 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
831 /*********************************************************************
833 * Function : client_referrer
835 * Description : Handle the "referer" config setting properly.
840 * 2 : s = header (from sed) to "crunch"
841 * 3 : csp = Current client state (buffers, headers, etc...)
843 * Returns : NULL if crunched, or a malloc'ed string with the original
846 *********************************************************************/
847 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
851 #ifdef FEATURE_FORCE_LOAD
852 /* Since the referrer can include the prefix even
853 * even if the request itself is non-forced, we must
854 * clean it unconditionally
856 strclean(s, FORCE_PREFIX);
857 #endif /* def FEATURE_FORCE_LOAD */
860 csp->referrer = strdup(s);
861 #endif /* def FEATURE_TRUST */
864 * Are we sending referer?
866 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
871 newval = csp->action->string[ACTION_STRING_REFERER];
874 * Are we blocking referer?
876 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
878 log_error(LOG_LEVEL_HEADER, "crunch!");
883 * Are we forging referer?
885 if (0 == strcmpic(newval, "forge"))
888 * Forge a referer as http://[hostname:port of REQUEST]/
889 * to fool stupid checks for in-site links
891 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
892 s2 = strsav(NULL, "Referer: ");
893 s2 = strsav(s2, "http://");
894 s2 = strsav(s2, csp->http->hostport);
895 s2 = strsav(s2, "/");
900 * Have we got a fixed referer?
902 if (0 == strncmpic(newval, "http://", 7))
905 * We have a specific (fixed) referer we want to send.
909 log_error(LOG_LEVEL_HEADER, "modified");
911 s3 = strsav( NULL, "Referer: " );
912 s3 = strsav( s3, newval );
916 /* Should never get here! */
917 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
920 * Forge is probably the best default.
922 * Forge a referer as http://[hostname:port of REQUEST]/
923 * to fool stupid checks for in-site links
925 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
926 s2 = strsav(NULL, "Referer: ");
927 s2 = strsav(s2, "http://");
928 s2 = strsav(s2, csp->http->hostport);
929 s2 = strsav(s2, "/");
934 /*********************************************************************
936 * Function : client_uagent
938 * Description : Handle the "user-agent" config setting properly
939 * and remember its original value to enable browser
940 * bug workarounds. Called from `sed'.
944 * 2 : s = header (from sed) to "crunch"
945 * 3 : csp = Current client state (buffers, headers, etc...)
947 * Returns : A malloc'ed pointer to the default agent, or
948 * a malloc'ed string pointer to this header (ie. pass thru).
950 *********************************************************************/
951 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
956 /* Save the client's User-Agent: value */
959 csp->http->user_agent = strdup(s + 12);
962 #ifdef FEATURE_IMAGE_DETECT_MSIE
963 if (strstr (s, "MSIE "))
965 /* This is Microsoft Internet Explorer.
966 * Enable auto-detect.
968 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
970 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
972 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
977 newval = csp->action->string[ACTION_STRING_USER_AGENT];
983 log_error(LOG_LEVEL_HEADER, "modified");
985 s2 = strsav( NULL, "User-Agent: " );
986 s2 = strsav( s2, newval );
992 /*********************************************************************
994 * Function : client_ua
996 * Description : Handle "ua-" headers properly. Called from `sed'.
1000 * 2 : s = header (from sed) to "crunch"
1001 * 3 : csp = Current client state (buffers, headers, etc...)
1003 * Returns : NULL if crunched, or a malloc'ed string to original header
1005 *********************************************************************/
1006 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1008 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1014 log_error(LOG_LEVEL_HEADER, "crunch!");
1020 /*********************************************************************
1022 * Function : client_from
1024 * Description : Handle the "from" config setting properly.
1025 * Called from `sed'.
1029 * 2 : s = header (from sed) to "crunch"
1030 * 3 : csp = Current client state (buffers, headers, etc...)
1032 * Returns : NULL if crunched, or a malloc'ed string to
1033 * modified/original header.
1035 *********************************************************************/
1036 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1038 const char * newval;
1041 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1046 newval = csp->action->string[ACTION_STRING_FROM];
1049 * Are we blocking referer?
1051 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1053 log_error(LOG_LEVEL_HEADER, "crunch!");
1057 log_error(LOG_LEVEL_HEADER, " modified");
1059 s2 = strsav( NULL, "From: " );
1060 s2 = strsav( s2, newval );
1066 /*********************************************************************
1068 * Function : client_send_cookie
1070 * Description : Handle the "cookie" header properly. Called from `sed'.
1071 * If cookie is accepted, add it to the cookie_list,
1072 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1075 * 1 : v = pattern of cookie `sed' found matching
1076 * 2 : s = header (from sed) to "crunch"
1077 * 3 : csp = Current client state (buffers, headers, etc...)
1079 * Returns : Always NULL.
1081 *********************************************************************/
1082 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1084 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1086 enlist(csp->cookie_list, s + v->len + 1);
1090 log_error(LOG_LEVEL_HEADER, " crunch!");
1094 * Always return NULL here. The cookie header
1095 * will be sent at the end of the header.
1102 /*********************************************************************
1104 * Function : client_x_forwarded
1106 * Description : Handle the "x-forwarded-for" config setting properly,
1107 * also used in the add_client_headers list. Called from `sed'.
1111 * 2 : s = header (from sed) to "crunch"
1112 * 3 : csp = Current client state (buffers, headers, etc...)
1114 * Returns : Always NULL.
1116 *********************************************************************/
1117 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1119 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1121 /* Save it so we can re-add it later */
1122 csp->x_forwarded = strdup(s);
1126 * Always return NULL, since this information
1127 * will be sent at the end of the header.
1134 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1135 /*********************************************************************
1137 * Function : client_accept
1139 * Description : Detect whether the client wants HTML or an image.
1140 * Clients do not always make this information available
1141 * in a sane way. Always passes the header through
1142 * the proxy unchanged.
1146 * 2 : s = Header string. Null terminated.
1147 * 3 : csp = Current client state (buffers, headers, etc...)
1149 * Returns : Duplicate of argument s.
1151 *********************************************************************/
1152 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1154 #ifdef FEATURE_IMAGE_DETECT_MSIE
1155 if (strstr (s, "image/gif"))
1157 /* Client will accept HTML. If this seems counterintuitive,
1160 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1164 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1166 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1171 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1175 /* the following functions add headers directly to the header list */
1178 /*********************************************************************
1180 * Function : client_cookie_adder
1182 * Description : Used in the add_client_headers list. Called from `sed'.
1185 * 1 : csp = Current client state (buffers, headers, etc...)
1189 *********************************************************************/
1190 void client_cookie_adder(struct client_state *csp)
1192 struct list_entry *lst;
1196 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1200 tmp = strsav(tmp, "; ");
1202 tmp = strsav(tmp, lst->str);
1205 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1209 tmp = strsav(tmp, "; ");
1212 if ((e = cookie_encode(lst->str)))
1214 tmp = strsav(tmp, e);
1223 ret = strdup("Cookie: ");
1224 ret = strsav(ret, tmp);
1225 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1226 enlist(csp->headers, ret);
1234 /*********************************************************************
1236 * Function : client_xtra_adder
1238 * Description : Used in the add_client_headers list. Called from `sed'.
1241 * 1 : csp = Current client state (buffers, headers, etc...)
1245 *********************************************************************/
1246 void client_xtra_adder(struct client_state *csp)
1248 struct list_entry *lst;
1250 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1251 lst ; lst = lst->next)
1253 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1254 enlist(csp->headers, lst->str);
1260 /*********************************************************************
1262 * Function : client_x_forwarded_adder
1264 * Description : Used in the add_client_headers list. Called from `sed'.
1267 * 1 : csp = Current client state (buffers, headers, etc...)
1271 *********************************************************************/
1272 void client_x_forwarded_adder(struct client_state *csp)
1276 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1281 if (csp->x_forwarded)
1283 p = strsav(p, csp->x_forwarded);
1284 p = strsav(p, ", ");
1285 p = strsav(p, csp->ip_addr_str);
1289 p = strsav(p, "X-Forwarded-For: ");
1290 p = strsav(p, csp->ip_addr_str);
1293 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1294 enlist(csp->headers, p);
1300 /*********************************************************************
1302 * Function : connection_close_adder
1304 * Description : Adds a "Connection: close" header to csp->headers
1305 * as a temporary fix for the needed but missing HTTP/1.1
1306 * support. Called from `sed'.
1307 * FIXME: This whole function shouldn't be neccessary!
1310 * 1 : csp = Current client state (buffers, headers, etc...)
1314 *********************************************************************/
1315 void connection_close_adder(struct client_state *csp)
1317 enlist(csp->headers, "Connection: close");
1321 /*********************************************************************
1323 * Function : server_http11
1325 * Description : Rewrite HTTP/1.1 answers to HTTP/1.0 until we add
1326 * HTTP/1.1 support. Called from `sed'.
1329 * 1 : v = parser pattern that matched this header
1330 * 2 : s = header that matched this pattern
1331 * 3 : csp = Current client state (buffers, headers, etc...)
1333 * Returns : "HTTP/1.0" answer.
1335 *********************************************************************/
1336 char *server_http11(const struct parsers *v, const char *s, struct client_state *csp)
1341 ret[7] = '0'; /* "HTTP/1.1 ..." -> "HTTP/1.0 ..." */
1347 /*********************************************************************
1349 * Function : server_set_cookie
1351 * Description : Handle the server "cookie" header properly.
1352 * Log cookie to the jar file. Then "crunch" it,
1353 * or accept it. Called from `sed'.
1356 * 1 : v = parser pattern that matched this header
1357 * 2 : s = header that matched this pattern
1358 * 3 : csp = Current client state (buffers, headers, etc...)
1360 * Returns : `crumble' or a newly malloc'ed string.
1362 *********************************************************************/
1363 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1365 #ifdef FEATURE_COOKIE_JAR
1366 if (csp->config->jar)
1368 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1370 #endif /* def FEATURE_COOKIE_JAR */
1372 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1374 return(crumble(v, s, csp));
1382 #ifdef FEATURE_FORCE_LOAD
1383 /*********************************************************************
1385 * Function : client_host
1387 * Description : Clean the FORCE_PREFIX out of the 'host' http
1388 * header, if we use force
1392 * 2 : s = header (from sed) to clean
1393 * 3 : csp = Current client state (buffers, headers, etc...)
1395 * Returns : A malloc'ed pointer to the cleaned host header
1397 *********************************************************************/
1398 char *client_host(const struct parsers *v, const char *s, struct client_state *csp)
1400 char *cleanhost = strdup(s);
1404 strclean(cleanhost, FORCE_PREFIX);
1409 #endif /* def FEATURE_FORCE_LOAD */
1412 #ifdef FEATURE_FORCE_LOAD
1413 /*********************************************************************
1415 * Function : strclean
1417 * Description : In-Situ-Eliminate all occurances of substring in
1421 * 1 : string = string to clean
1422 * 2 : substring = substring to eliminate
1424 * Returns : Number of eliminations
1426 *********************************************************************/
1427 int strclean(const char *string, const char *substring)
1429 int hits = 0, len = strlen(substring);
1432 while((pos = strstr(string, substring)))
1439 while (*p++ != '\0');
1446 #endif /* def FEATURE_FORCE_LOAD */