1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
5 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Copyright : Written by and Copyright (C) 2001-2017 the
8 * Privoxy team. http://www.privoxy.org/
10 * Based on the Internet Junkbuster originally written
11 * by and Copyright (C) 1997 Anonymous Coders and
12 * Junkbusters Corporation. http://www.junkbusters.com
14 * This program is free software; you can redistribute it
15 * and/or modify it under the terms of the GNU General
16 * Public License as published by the Free Software
17 * Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * This program is distributed in the hope that it will
21 * be useful, but WITHOUT ANY WARRANTY; without even the
22 * implied warranty of MERCHANTABILITY or FITNESS FOR A
23 * PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * The GNU General Public License should be included with
27 * this file. If not, you can view it at
28 * http://www.gnu.org/copyleft/gpl.html
29 * or write to the Free Software Foundation, Inc., 59
30 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 *********************************************************************/
39 #include <sys/types.h>
49 * Convince GNU's libc to provide a strptime prototype.
58 #define GZIP_IDENTIFIER_1 0x1f
59 #define GZIP_IDENTIFIER_2 0x8b
61 #define GZIP_FLAG_CHECKSUM 0x02
62 #define GZIP_FLAG_EXTRA_FIELDS 0x04
63 #define GZIP_FLAG_FILE_NAME 0x08
64 #define GZIP_FLAG_COMMENT 0x10
65 #define GZIP_FLAG_RESERVED_BITS 0xe0
68 #if !defined(_WIN32) && !defined(__OS2__)
74 #ifdef FEATURE_PTHREAD
76 /* jcc.h is for mutex semapores only */
77 #endif /* def FEATURE_PTHREAD */
82 #include "jbsockets.h"
92 static char *get_header_line(struct iob *iob);
93 static jb_err scan_headers(struct client_state *csp);
94 static jb_err header_tagger(struct client_state *csp, char *header);
95 static jb_err parse_header_time(const char *header_time, time_t *result);
96 static jb_err parse_time_header(const char *header, time_t *result);
98 static jb_err crumble (struct client_state *csp, char **header);
99 static jb_err filter_header (struct client_state *csp, char **header);
100 static jb_err client_connection (struct client_state *csp, char **header);
101 static jb_err client_referrer (struct client_state *csp, char **header);
102 static jb_err client_uagent (struct client_state *csp, char **header);
103 static jb_err client_ua (struct client_state *csp, char **header);
104 static jb_err client_from (struct client_state *csp, char **header);
105 static jb_err client_send_cookie (struct client_state *csp, char **header);
106 static jb_err client_x_forwarded (struct client_state *csp, char **header);
107 static jb_err client_accept_encoding (struct client_state *csp, char **header);
108 static jb_err client_te (struct client_state *csp, char **header);
109 static jb_err client_max_forwards (struct client_state *csp, char **header);
110 static jb_err client_host (struct client_state *csp, char **header);
111 static jb_err client_if_modified_since (struct client_state *csp, char **header);
112 static jb_err client_accept_language (struct client_state *csp, char **header);
113 static jb_err client_if_none_match (struct client_state *csp, char **header);
114 static jb_err crunch_client_header (struct client_state *csp, char **header);
115 static jb_err client_x_filter (struct client_state *csp, char **header);
116 static jb_err client_range (struct client_state *csp, char **header);
117 static jb_err client_expect (struct client_state *csp, char **header);
118 static jb_err server_set_cookie (struct client_state *csp, char **header);
119 static jb_err server_connection (struct client_state *csp, char **header);
120 static jb_err server_content_type (struct client_state *csp, char **header);
121 static jb_err server_adjust_content_length(struct client_state *csp, char **header);
122 static jb_err server_content_md5 (struct client_state *csp, char **header);
123 static jb_err server_content_encoding (struct client_state *csp, char **header);
124 static jb_err server_transfer_coding (struct client_state *csp, char **header);
125 static jb_err server_http (struct client_state *csp, char **header);
126 static jb_err crunch_server_header (struct client_state *csp, char **header);
127 static jb_err server_last_modified (struct client_state *csp, char **header);
128 static jb_err server_content_disposition(struct client_state *csp, char **header);
130 static jb_err server_adjust_content_encoding(struct client_state *csp, char **header);
133 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
134 static jb_err server_save_content_length(struct client_state *csp, char **header);
135 static jb_err server_keep_alive(struct client_state *csp, char **header);
136 static jb_err server_proxy_connection(struct client_state *csp, char **header);
137 static jb_err client_keep_alive(struct client_state *csp, char **header);
138 static jb_err client_save_content_length(struct client_state *csp, char **header);
139 static jb_err client_proxy_connection(struct client_state *csp, char **header);
140 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
142 static jb_err client_host_adder (struct client_state *csp);
143 static jb_err client_xtra_adder (struct client_state *csp);
144 static jb_err client_x_forwarded_for_adder(struct client_state *csp);
145 static jb_err client_connection_header_adder(struct client_state *csp);
146 static jb_err server_connection_adder(struct client_state *csp);
147 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
148 static jb_err server_proxy_connection_adder(struct client_state *csp);
149 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
150 static jb_err proxy_authentication(struct client_state *csp, char **header);
152 static jb_err create_forged_referrer(char **header, const char *hostport);
153 static jb_err create_fake_referrer(char **header, const char *fake_referrer);
154 static jb_err handle_conditional_hide_referrer_parameter(char **header,
155 const char *host, const int parameter_conditional_block);
156 static void create_content_length_header(unsigned long long content_length,
157 char *header, size_t buffer_length);
160 * List of functions to run on a list of headers.
164 /** The header prefix to match */
167 /** The length of the prefix to match */
170 /** The function to apply to this line */
171 const parser_func_ptr parser;
174 static const struct parsers client_patterns[] = {
175 { "referer:", 8, client_referrer },
176 { "user-agent:", 11, client_uagent },
177 { "ua-", 3, client_ua },
178 { "from:", 5, client_from },
179 { "cookie:", 7, client_send_cookie },
180 { "x-forwarded-for:", 16, client_x_forwarded },
181 { "Accept-Encoding:", 16, client_accept_encoding },
182 { "TE:", 3, client_te },
183 { "Host:", 5, client_host },
184 { "if-modified-since:", 18, client_if_modified_since },
185 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
186 { "Keep-Alive:", 11, client_keep_alive },
187 { "Content-Length:", 15, client_save_content_length },
188 { "Proxy-Connection:", 17, client_proxy_connection },
190 { "Keep-Alive:", 11, crumble },
191 { "Proxy-Connection:", 17, crumble },
193 { "connection:", 11, client_connection },
194 { "max-forwards:", 13, client_max_forwards },
195 { "Accept-Language:", 16, client_accept_language },
196 { "if-none-match:", 14, client_if_none_match },
197 { "Range:", 6, client_range },
198 { "Request-Range:", 14, client_range },
199 { "If-Range:", 9, client_range },
200 { "X-Filter:", 9, client_x_filter },
201 { "Proxy-Authorization:", 20, proxy_authentication },
203 { "Transfer-Encoding:", 18, client_transfer_encoding },
205 { "Expect:", 7, client_expect },
206 { "*", 0, crunch_client_header },
207 { "*", 0, filter_header },
211 static const struct parsers server_patterns[] = {
212 { "HTTP/", 5, server_http },
213 { "set-cookie:", 11, server_set_cookie },
214 { "connection:", 11, server_connection },
215 { "Content-Type:", 13, server_content_type },
216 { "Content-MD5:", 12, server_content_md5 },
217 { "Content-Encoding:", 17, server_content_encoding },
218 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
219 { "Content-Length:", 15, server_save_content_length },
220 { "Keep-Alive:", 11, server_keep_alive },
221 { "Proxy-Connection:", 17, server_proxy_connection },
223 { "Keep-Alive:", 11, crumble },
224 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
225 { "Transfer-Encoding:", 18, server_transfer_coding },
226 { "content-disposition:", 20, server_content_disposition },
227 { "Last-Modified:", 14, server_last_modified },
228 { "Proxy-Authenticate:", 19, proxy_authentication },
229 { "*", 0, crunch_server_header },
230 { "*", 0, filter_header },
234 static const add_header_func_ptr add_client_headers[] = {
236 client_x_forwarded_for_adder,
238 client_connection_header_adder,
242 static const add_header_func_ptr add_server_headers[] = {
243 server_connection_adder,
244 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
245 server_proxy_connection_adder,
246 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
250 /*********************************************************************
252 * Function : flush_socket
254 * Description : Write any pending "buffered" content.
257 * 1 : fd = file descriptor of the socket to read
258 * 2 : iob = The I/O buffer to flush, usually csp->iob.
260 * Returns : On success, the number of bytes written are returned (zero
261 * indicates nothing was written). On error, -1 is returned,
262 * and errno is set appropriately. If count is zero and the
263 * file descriptor refers to a regular file, 0 will be
264 * returned without causing any other effect. For a special
265 * file, the results are not portable.
267 *********************************************************************/
268 long flush_socket(jb_socket fd, struct iob *iob)
270 long len = iob->eod - iob->cur;
277 if (write_socket(fd, iob->cur, (size_t)len))
281 iob->eod = iob->cur = iob->buf;
287 /*********************************************************************
289 * Function : add_to_iob
291 * Description : Add content to the buffer, expanding the
292 * buffer if necessary.
295 * 1 : iob = Destination buffer.
296 * 2 : buffer_limit = Limit to which the destination may grow
297 * 3 : src = holds the content to be added
298 * 4 : n = number of bytes to be added
300 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
301 * or buffer limit reached.
303 *********************************************************************/
304 jb_err add_to_iob(struct iob *iob, const size_t buffer_limit, char *src, long n)
306 size_t used, offset, need;
309 if (n <= 0) return JB_ERR_OK;
311 used = (size_t)(iob->eod - iob->buf);
312 offset = (size_t)(iob->cur - iob->buf);
313 need = used + (size_t)n + 1;
316 * If the buffer can't hold the new data, extend it first.
317 * Use the next power of two if possible, else use the actual need.
319 if (need > buffer_limit)
321 log_error(LOG_LEVEL_INFO,
322 "Buffer limit reached while extending the buffer (iob). Needed: %d. Limit: %d",
324 return JB_ERR_MEMORY;
327 if (need > iob->size)
329 size_t want = iob->size ? iob->size : 512;
336 if (want <= buffer_limit && NULL != (p = (char *)realloc(iob->buf, want)))
340 else if (NULL != (p = (char *)realloc(iob->buf, need)))
346 log_error(LOG_LEVEL_ERROR, "Extending the buffer (iob) failed: %E");
347 return JB_ERR_MEMORY;
350 /* Update the iob pointers */
351 iob->cur = p + offset;
356 /* copy the new data into the iob buffer */
357 memcpy(iob->eod, src, (size_t)n);
359 /* point to the end of the data */
362 /* null terminate == cheap insurance */
370 /*********************************************************************
372 * Function : clear_iob
374 * Description : Frees the memory allocated for an I/O buffer and
375 * resets the structure.
378 * 1 : iob = I/O buffer to clear.
380 * Returns : JB_ERR_OK on success, JB_ERR_MEMORY if out-of-memory
381 * or buffer limit reached.
383 *********************************************************************/
384 void clear_iob(struct iob *iob)
387 memset(iob, '\0', sizeof(*iob));
392 /*********************************************************************
394 * Function : decompress_iob
396 * Description : Decompress buffered page, expanding the
397 * buffer as necessary. csp->iob->cur
398 * should point to the the beginning of the
399 * compressed data block.
402 * 1 : csp = Current client state (buffers, headers, etc...)
404 * Returns : JB_ERR_OK on success,
405 * JB_ERR_MEMORY if out-of-memory limit reached, and
406 * JB_ERR_COMPRESS if error decompressing buffer.
408 *********************************************************************/
409 jb_err decompress_iob(struct client_state *csp)
411 char *buf; /* new, uncompressed buffer */
412 char *cur; /* Current iob position (to keep the original
413 * iob->cur unmodified if we return early) */
414 size_t bufsize; /* allocated size of the new buffer */
415 size_t old_size; /* Content size before decompression */
416 size_t skip_size; /* Number of bytes at the beginning of the iob
417 that we should NOT decompress. */
418 int status; /* return status of the inflate() call */
419 z_stream zstr; /* used by calls to zlib */
422 assert(csp->iob->cur - csp->iob->buf >= 0);
423 assert(csp->iob->eod - csp->iob->cur >= 0);
425 assert(csp->iob->cur - csp->iob->buf > 0);
426 assert(csp->iob->eod - csp->iob->cur > 0);
429 bufsize = csp->iob->size;
430 skip_size = (size_t)(csp->iob->cur - csp->iob->buf);
431 old_size = (size_t)(csp->iob->eod - csp->iob->cur);
435 if (bufsize < (size_t)10)
438 * This is to protect the parsing of gzipped data,
439 * but it should(?) be valid for deflated data also.
441 log_error(LOG_LEVEL_ERROR,
442 "Insufficient data to start decompression. Bytes in buffer: %d",
443 csp->iob->eod - csp->iob->cur);
444 return JB_ERR_COMPRESS;
447 if (csp->content_type & CT_GZIP)
450 * Our task is slightly complicated by the facts that data
451 * compressed by gzip does not include a zlib header, and
452 * that there is no easily accessible interface in zlib to
453 * handle a gzip header. We strip off the gzip header by
454 * hand, and later inform zlib not to expect a header.
458 * Strip off the gzip header. Please see RFC 1952 for more
459 * explanation of the appropriate fields.
461 if (((*cur++ & 0xff) != GZIP_IDENTIFIER_1)
462 || ((*cur++ & 0xff) != GZIP_IDENTIFIER_2)
463 || (*cur++ != Z_DEFLATED))
465 log_error(LOG_LEVEL_ERROR, "Invalid gzip header when decompressing");
466 return JB_ERR_COMPRESS;
471 if (flags & GZIP_FLAG_RESERVED_BITS)
473 /* The gzip header has reserved bits set; bail out. */
474 log_error(LOG_LEVEL_ERROR, "Invalid gzip header flags when decompressing");
475 return JB_ERR_COMPRESS;
479 * Skip mtime (4 bytes), extra flags (1 byte)
480 * and OS type (1 byte).
484 /* Skip extra fields if necessary. */
485 if (flags & GZIP_FLAG_EXTRA_FIELDS)
488 * Skip a given number of bytes, specified
489 * as a 16-bit little-endian value.
491 * XXX: this code is untested and should probably be removed.
495 skip_bytes += *cur++ << 8;
498 * The number of bytes to skip should be positive
499 * and we'd like to stay in the buffer.
501 if ((skip_bytes < 0) || (skip_bytes >= (csp->iob->eod - cur)))
503 log_error(LOG_LEVEL_ERROR,
504 "Unreasonable amount of bytes to skip (%d). Stopping decompression",
506 return JB_ERR_COMPRESS;
508 log_error(LOG_LEVEL_INFO,
509 "Skipping %d bytes for gzip compression. Does this sound right?",
514 /* Skip the filename if necessary. */
515 if (flags & GZIP_FLAG_FILE_NAME)
517 /* A null-terminated string is supposed to follow. */
518 while (*cur++ && (cur < csp->iob->eod));
521 /* Skip the comment if necessary. */
522 if (flags & GZIP_FLAG_COMMENT)
524 /* A null-terminated string is supposed to follow. */
525 while (*cur++ && (cur < csp->iob->eod));
528 /* Skip the CRC if necessary. */
529 if (flags & GZIP_FLAG_CHECKSUM)
534 if (cur >= csp->iob->eod)
537 * If the current position pointer reached or passed
538 * the buffer end, we were obviously tricked to skip
541 log_error(LOG_LEVEL_ERROR,
542 "Malformed gzip header detected. Aborting decompression.");
543 return JB_ERR_COMPRESS;
547 else if (csp->content_type & CT_DEFLATE)
550 * In theory (that is, according to RFC 1950), deflate-compressed
551 * data should begin with a two-byte zlib header and have an
552 * adler32 checksum at the end. It seems that in practice only
553 * the raw compressed data is sent. Note that this means that
554 * we are not RFC 1950-compliant here, but the advantage is that
555 * this actually works. :)
557 * We add a dummy null byte to tell zlib where the data ends,
558 * and later inform it not to expect a header.
560 * Fortunately, add_to_iob() has thoughtfully null-terminated
561 * the buffer; we can just increment the end pointer to include
568 log_error(LOG_LEVEL_ERROR,
569 "Unable to determine compression format for decompression");
570 return JB_ERR_COMPRESS;
573 /* Set up the fields required by zlib. */
574 zstr.next_in = (Bytef *)cur;
575 zstr.avail_in = (unsigned int)(csp->iob->eod - cur);
576 zstr.zalloc = Z_NULL;
578 zstr.opaque = Z_NULL;
581 * Passing -MAX_WBITS to inflateInit2 tells the library
582 * that there is no zlib header.
584 if (inflateInit2(&zstr, -MAX_WBITS) != Z_OK)
586 log_error(LOG_LEVEL_ERROR, "Error initializing decompression");
587 return JB_ERR_COMPRESS;
591 * Next, we allocate new storage for the inflated data.
592 * We don't modify the existing iob yet, so in case there
593 * is error in decompression we can recover gracefully.
595 buf = zalloc(bufsize);
598 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
599 return JB_ERR_MEMORY;
602 assert(bufsize >= skip_size);
603 memcpy(buf, csp->iob->buf, skip_size);
604 zstr.avail_out = (uInt)(bufsize - skip_size);
605 zstr.next_out = (Bytef *)buf + skip_size;
607 /* Try to decompress the whole stream in one shot. */
608 while (Z_BUF_ERROR == (status = inflate(&zstr, Z_FINISH)))
610 /* We need to allocate more memory for the output buffer. */
612 char *tmpbuf; /* used for realloc'ing the buffer */
613 size_t oldbufsize = bufsize; /* keep track of the old bufsize */
615 if (0 == zstr.avail_in)
618 * If zlib wants more data then there's a problem, because
619 * the complete compressed file should have been buffered.
621 log_error(LOG_LEVEL_ERROR,
622 "Unexpected end of compressed iob. Using what we got so far.");
627 * If we reached the buffer limit and still didn't have enough
628 * memory, just give up. Due to the ceiling enforced by the next
629 * if block we could actually check for equality here, but as it
630 * can be easily mistaken for a bug we don't.
632 if (bufsize >= csp->config->buffer_limit)
634 log_error(LOG_LEVEL_ERROR, "Buffer limit reached while decompressing iob");
635 return JB_ERR_MEMORY;
638 /* Try doubling the buffer size each time. */
641 /* Don't exceed the buffer limit. */
642 if (bufsize > csp->config->buffer_limit)
644 bufsize = csp->config->buffer_limit;
647 /* Try to allocate the new buffer. */
648 tmpbuf = realloc(buf, bufsize);
651 log_error(LOG_LEVEL_ERROR, "Out of memory decompressing iob");
653 return JB_ERR_MEMORY;
657 char *oldnext_out = (char *)zstr.next_out;
660 * Update the fields for inflate() to use the new
661 * buffer, which may be in a location different from
664 zstr.avail_out += (uInt)(bufsize - oldbufsize);
665 zstr.next_out = (Bytef *)tmpbuf + bufsize - zstr.avail_out;
668 * Compare with an uglier method of calculating these values
669 * that doesn't require the extra oldbufsize variable.
671 assert(zstr.avail_out == tmpbuf + bufsize - (char *)zstr.next_out);
672 assert((char *)zstr.next_out == tmpbuf + ((char *)oldnext_out - buf));
678 if (Z_STREAM_ERROR == inflateEnd(&zstr))
680 log_error(LOG_LEVEL_ERROR,
681 "Inconsistent stream state after decompression: %s", zstr.msg);
683 * XXX: Intentionally no return.
685 * According to zlib.h, Z_STREAM_ERROR is returned
686 * "if the stream state was inconsistent".
688 * I assume in this case inflate()'s status
689 * would also be something different than Z_STREAM_END
690 * so this check should be redundant, but lets see.
694 if ((status != Z_STREAM_END) && (0 != zstr.avail_in))
697 * We failed to decompress the stream and it's
698 * not simply because of missing data.
700 log_error(LOG_LEVEL_ERROR,
701 "Unexpected error while decompressing to the buffer (iob): %s",
703 return JB_ERR_COMPRESS;
707 * Finally, we can actually update the iob, since the
708 * decompression was successful. First, free the old
711 freez(csp->iob->buf);
713 /* Now, update the iob to use the new buffer. */
715 csp->iob->cur = csp->iob->buf + skip_size;
716 csp->iob->eod = (char *)zstr.next_out;
717 csp->iob->size = bufsize;
720 * Make sure the new uncompressed iob obeys some minimal
721 * consistency conditions.
723 if ((csp->iob->buf <= csp->iob->cur)
724 && (csp->iob->cur <= csp->iob->eod)
725 && (csp->iob->eod <= csp->iob->buf + csp->iob->size))
727 const size_t new_size = (size_t)(csp->iob->eod - csp->iob->cur);
728 if (new_size > (size_t)0)
730 log_error(LOG_LEVEL_RE_FILTER,
731 "Decompression successful. Old size: %d, new size: %d.",
736 /* zlib thinks this is OK, so lets do the same. */
737 log_error(LOG_LEVEL_INFO, "Decompression didn't result in any content.");
742 /* It seems that zlib did something weird. */
743 log_error(LOG_LEVEL_ERROR,
744 "Unexpected error decompressing the buffer (iob): %d==%d, %d>%d, %d<%d",
745 csp->iob->cur, csp->iob->buf + skip_size, csp->iob->eod, csp->iob->buf,
746 csp->iob->eod, csp->iob->buf + csp->iob->size);
747 return JB_ERR_COMPRESS;
753 #endif /* defined(FEATURE_ZLIB) */
756 /*********************************************************************
758 * Function : normalize_lws
760 * Description : Reduces unquoted linear whitespace in headers to
761 * a single space in accordance with RFC 7230 3.2.4.
762 * This simplifies parsing and filtering later on.
765 * 1 : header = A header with linear whitespace to reduce.
769 *********************************************************************/
770 static void normalize_lws(char *header)
776 if (privoxy_isspace(*p) && privoxy_isspace(*(p+1)))
780 while (privoxy_isspace(*q))
784 log_error(LOG_LEVEL_HEADER, "Reducing whitespace in '%s'", header);
790 log_error(LOG_LEVEL_HEADER,
791 "Converting tab to space in '%s'", header);
796 char *end_of_token = strstr(p+1, "\"");
798 if (NULL != end_of_token)
800 /* Don't mess with quoted text. */
805 log_error(LOG_LEVEL_HEADER,
806 "Ignoring single quote in '%s'", header);
812 p = strchr(header, ':');
813 if ((p != NULL) && (p != header) && privoxy_isspace(*(p-1)))
816 * There's still space before the colon.
824 /*********************************************************************
826 * Function : get_header
828 * Description : This (odd) routine will parse the csp->iob
829 * to get the next complete header.
832 * 1 : iob = The I/O buffer to parse, usually csp->iob.
834 * Returns : Any one of the following:
836 * 1) a pointer to a dynamically allocated string that contains a header line
837 * 2) NULL indicating that the end of the header was reached
838 * 3) "" indicating that the end of the iob was reached before finding
839 * a complete header line.
841 *********************************************************************/
842 char *get_header(struct iob *iob)
846 header = get_header_line(iob);
848 if ((header == NULL) || (*header == '\0'))
851 * No complete header read yet, tell the client.
856 while ((iob->cur[0] == ' ') || (iob->cur[0] == '\t'))
859 * Header spans multiple lines, append the next one.
861 char *continued_header;
863 continued_header = get_header_line(iob);
864 if ((continued_header == NULL) || (*continued_header == '\0'))
867 * No complete header read yet, return what we got.
868 * XXX: Should "unread" header instead.
870 log_error(LOG_LEVEL_INFO,
871 "Failed to read a multi-line header properly: '%s'",
876 if (JB_ERR_OK != string_join(&header, continued_header))
878 log_error(LOG_LEVEL_FATAL,
879 "Out of memory while appending multiple headers.");
883 /* XXX: remove before next stable release. */
884 log_error(LOG_LEVEL_HEADER,
885 "Merged multiple header lines to: '%s'",
890 normalize_lws(header);
897 /*********************************************************************
899 * Function : get_header_line
901 * Description : This (odd) routine will parse the csp->iob
902 * to get the next header line.
905 * 1 : iob = The I/O buffer to parse, usually csp->iob.
907 * Returns : Any one of the following:
909 * 1) a pointer to a dynamically allocated string that contains a header line
910 * 2) NULL indicating that the end of the header was reached
911 * 3) "" indicating that the end of the iob was reached before finding
912 * a complete header line.
914 *********************************************************************/
915 static char *get_header_line(struct iob *iob)
919 if ((iob->cur == NULL)
920 || ((p = strchr(iob->cur, '\n')) == NULL))
922 return(""); /* couldn't find a complete header */
927 ret = strdup(iob->cur);
930 /* FIXME No way to handle error properly */
931 log_error(LOG_LEVEL_FATAL, "Out of memory in get_header_line()");
937 if ((q = strchr(ret, '\r')) != NULL) *q = '\0';
939 /* is this a blank line (i.e. the end of the header) ? */
951 /*********************************************************************
953 * Function : get_header_value
955 * Description : Get the value of a given header from a chained list
956 * of header lines or return NULL if no such header is
957 * present in the list.
960 * 1 : header_list = pointer to list
961 * 2 : header_name = string with name of header to look for.
962 * Trailing colon required, capitalization
965 * Returns : NULL if not found, else value of header
967 *********************************************************************/
968 char *get_header_value(const struct list *header_list, const char *header_name)
970 struct list_entry *cur_entry;
976 length = strlen(header_name);
978 for (cur_entry = header_list->first; cur_entry ; cur_entry = cur_entry->next)
982 if (!strncmpic(cur_entry->str, header_name, length))
985 * Found: return pointer to start of value
987 ret = cur_entry->str + length;
988 while (*ret && privoxy_isspace(*ret)) ret++;
1002 /*********************************************************************
1004 * Function : scan_headers
1006 * Description : Scans headers, applies tags and updates action bits.
1009 * 1 : csp = Current client state (buffers, headers, etc...)
1011 * Returns : JB_ERR_OK
1013 *********************************************************************/
1014 static jb_err scan_headers(struct client_state *csp)
1016 struct list_entry *h; /* Header */
1017 jb_err err = JB_ERR_OK;
1019 for (h = csp->headers->first; (err == JB_ERR_OK) && (h != NULL) ; h = h->next)
1021 /* Header crunch()ed in previous run? -> ignore */
1022 if (h->str == NULL) continue;
1023 log_error(LOG_LEVEL_HEADER, "scan: %s", h->str);
1024 err = header_tagger(csp, h->str);
1031 /*********************************************************************
1033 * Function : enforce_header_order
1035 * Description : Enforces a given header order.
1038 * 1 : headers = List of headers to order.
1039 * 2 : ordered_headers = List of ordered header names.
1043 *********************************************************************/
1044 static void enforce_header_order(struct list *headers, const struct list *ordered_headers)
1046 struct list_entry *sorted_header;
1047 struct list new_headers[1];
1048 struct list_entry *header;
1050 init_list(new_headers);
1052 /* The request line is always the first "header" */
1054 assert(NULL != headers->first->str);
1055 enlist(new_headers, headers->first->str);
1056 freez(headers->first->str)
1058 /* Enlist the specified headers in the given order */
1060 for (sorted_header = ordered_headers->first; sorted_header != NULL;
1061 sorted_header = sorted_header->next)
1063 const size_t sorted_header_length = strlen(sorted_header->str);
1064 for (header = headers->first; header != NULL; header = header->next)
1066 /* Header enlisted in previous run? -> ignore */
1067 if (header->str == NULL) continue;
1069 if (0 == strncmpic(sorted_header->str, header->str, sorted_header_length)
1070 && (header->str[sorted_header_length] == ':'))
1072 log_error(LOG_LEVEL_HEADER, "Enlisting sorted header %s", header->str);
1073 if (JB_ERR_OK != enlist(new_headers, header->str))
1075 log_error(LOG_LEVEL_HEADER, "Failed to enlist %s", header->str);
1082 /* Enlist the rest of the headers behind the ordered ones */
1083 for (header = headers->first; header != NULL; header = header->next)
1085 /* Header enlisted in previous run? -> ignore */
1086 if (header->str == NULL) continue;
1088 log_error(LOG_LEVEL_HEADER,
1089 "Enlisting left-over header %s", header->str);
1090 if (JB_ERR_OK != enlist(new_headers, header->str))
1092 log_error(LOG_LEVEL_HEADER, "Failed to enlist %s", header->str);
1097 list_remove_all(headers);
1098 list_duplicate(headers, new_headers);
1099 list_remove_all(new_headers);
1105 /*********************************************************************
1109 * Description : add, delete or modify lines in the HTTP header streams.
1110 * On entry, it receives a linked list of headers space
1111 * that was allocated dynamically (both the list nodes
1112 * and the header contents).
1114 * As a side effect it frees the space used by the original
1118 * 1 : csp = Current client state (buffers, headers, etc...)
1119 * 2 : filter_server_headers = Boolean to switch between
1120 * server and header filtering.
1122 * Returns : JB_ERR_OK in case off success, or
1123 * JB_ERR_MEMORY on some out-of-memory errors, or
1124 * JB_ERR_PARSE in case of fatal parse errors.
1126 *********************************************************************/
1127 jb_err sed(struct client_state *csp, int filter_server_headers)
1129 /* XXX: use more descriptive names. */
1130 struct list_entry *p;
1131 const struct parsers *v;
1132 const add_header_func_ptr *f;
1133 jb_err err = JB_ERR_OK;
1137 if (filter_server_headers)
1139 v = server_patterns;
1140 f = add_server_headers;
1141 check_negative_tag_patterns(csp, PATTERN_SPEC_NO_RESPONSE_TAG_PATTERN);
1145 v = client_patterns;
1146 f = add_client_headers;
1147 check_negative_tag_patterns(csp, PATTERN_SPEC_NO_REQUEST_TAG_PATTERN);
1150 while (v->str != NULL)
1152 for (p = csp->headers->first; p != NULL; p = p->next)
1154 /* Header crunch()ed in previous run? -> ignore */
1155 if (p->str == NULL) continue;
1157 /* Does the current parser handle this header? */
1158 if ((strncmpic(p->str, v->str, v->len) == 0) ||
1159 (v->len == CHECK_EVERY_HEADER_REMAINING))
1161 err = v->parser(csp, &(p->str));
1162 if (err != JB_ERR_OK)
1171 /* place additional headers on the csp->headers list */
1172 while ((err == JB_ERR_OK) && (*f))
1178 if (!filter_server_headers && !list_is_empty(csp->config->ordered_client_headers))
1180 enforce_header_order(csp->headers, csp->config->ordered_client_headers);
1187 /*********************************************************************
1189 * Function : update_server_headers
1191 * Description : Updates server headers after the body has been modified.
1194 * 1 : csp = Current client state (buffers, headers, etc...)
1196 * Returns : JB_ERR_OK in case off success, or
1197 * JB_ERR_MEMORY on out-of-memory error.
1199 *********************************************************************/
1200 jb_err update_server_headers(struct client_state *csp)
1202 jb_err err = JB_ERR_OK;
1204 static const struct parsers server_patterns_light[] = {
1205 { "Content-Length:", 15, server_adjust_content_length },
1206 { "Transfer-Encoding:", 18, server_transfer_coding },
1208 { "Content-Encoding:", 17, server_adjust_content_encoding },
1209 #endif /* def FEATURE_ZLIB */
1213 if (strncmpic(csp->http->cmd, "HEAD", 4))
1215 const struct parsers *v;
1216 struct list_entry *p;
1218 for (v = server_patterns_light; (err == JB_ERR_OK) && (v->str != NULL); v++)
1220 for (p = csp->headers->first; (err == JB_ERR_OK) && (p != NULL); p = p->next)
1222 /* Header crunch()ed in previous run? -> ignore */
1223 if (p->str == NULL) continue;
1225 /* Does the current parser handle this header? */
1226 if (strncmpic(p->str, v->str, v->len) == 0)
1228 err = v->parser(csp, (char **)&(p->str));
1234 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1235 if ((JB_ERR_OK == err)
1236 && (csp->flags & CSP_FLAG_MODIFIED)
1237 && (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
1238 && !(csp->flags & CSP_FLAG_SERVER_CONTENT_LENGTH_SET))
1242 create_content_length_header(csp->content_length, header, sizeof(header));
1243 err = enlist(csp->headers, header);
1244 if (JB_ERR_OK == err)
1246 log_error(LOG_LEVEL_HEADER,
1247 "Content modified with no Content-Length header set. "
1248 "Created: %s.", header);
1249 csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1252 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1254 #ifdef FEATURE_COMPRESSION
1255 if ((JB_ERR_OK == err)
1256 && (csp->flags & CSP_FLAG_BUFFERED_CONTENT_DEFLATED))
1258 err = enlist_unique_header(csp->headers, "Content-Encoding", "deflate");
1259 if (JB_ERR_OK == err)
1261 log_error(LOG_LEVEL_HEADER, "Added header: Content-Encoding: deflate");
1270 /*********************************************************************
1272 * Function : header_tagger
1274 * Description : Executes all text substitutions from applying
1275 * tag actions and saves the result as tag.
1277 * XXX: Shares enough code with filter_header() and
1278 * pcrs_filter_response() to warrant some helper functions.
1281 * 1 : csp = Current client state (buffers, headers, etc...)
1282 * 2 : header = Header that is used as tagger input
1284 * Returns : JB_ERR_OK on success and always succeeds
1286 *********************************************************************/
1287 static jb_err header_tagger(struct client_state *csp, char *header)
1289 enum filter_type wanted_filter_type;
1290 int multi_action_index;
1293 struct re_filterfile_spec *b;
1294 struct list_entry *tag_name;
1296 const size_t header_length = strlen(header);
1298 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
1300 wanted_filter_type = FT_SERVER_HEADER_TAGGER;
1301 multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
1305 wanted_filter_type = FT_CLIENT_HEADER_TAGGER;
1306 multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
1309 if (list_is_empty(csp->action->multi[multi_action_index])
1310 || filters_available(csp) == FALSE)
1312 /* Return early if no taggers apply or if none are available. */
1316 /* Execute all applying taggers */
1317 for (tag_name = csp->action->multi[multi_action_index]->first;
1318 NULL != tag_name; tag_name = tag_name->next)
1320 char *modified_tag = NULL;
1322 size_t size = header_length;
1325 b = get_filter(csp, tag_name->str, wanted_filter_type);
1331 joblist = b->joblist;
1333 if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
1335 if (NULL == joblist)
1337 log_error(LOG_LEVEL_RE_FILTER,
1338 "Tagger %s has empty joblist. Nothing to do.", b->name);
1342 /* execute their pcrs_joblist on the header. */
1343 for (job = joblist; NULL != job; job = job->next)
1345 const int hits = pcrs_execute(job, tag, size, &modified_tag, &size);
1349 /* Success, continue with the modified version. */
1358 /* Tagger doesn't match */
1361 /* Regex failure, log it but continue anyway. */
1362 assert(NULL != header);
1363 log_error(LOG_LEVEL_ERROR,
1364 "Problems with tagger \'%s\' and header \'%s\': %s",
1365 b->name, *header, pcrs_strerror(hits));
1367 freez(modified_tag);
1371 if (b->dynamic) pcrs_free_joblist(joblist);
1373 /* If this tagger matched */
1379 * There is no technical limitation which makes
1380 * it impossible to use empty tags, but I assume
1381 * no one would do it intentionally.
1384 log_error(LOG_LEVEL_INFO,
1385 "Tagger \'%s\' created an empty tag. Ignored.", b->name);
1389 if (!list_contains_item(csp->tags, tag))
1391 if (JB_ERR_OK != enlist(csp->tags, tag))
1393 log_error(LOG_LEVEL_ERROR,
1394 "Insufficient memory to add tag \'%s\', "
1395 "based on tagger \'%s\' and header \'%s\'",
1396 tag, b->name, *header);
1400 char *action_message;
1402 * update the action bits right away, to make
1403 * tagging based on tags set by earlier taggers
1404 * of the same kind possible.
1406 if (update_action_bits_for_tag(csp, tag))
1408 action_message = "Action bits updated accordingly.";
1412 action_message = "No action bits update necessary.";
1415 log_error(LOG_LEVEL_HEADER,
1416 "Tagger \'%s\' added tag \'%s\'. %s",
1417 b->name, tag, action_message);
1422 /* XXX: Is this log-worthy? */
1423 log_error(LOG_LEVEL_HEADER,
1424 "Tagger \'%s\' didn't add tag \'%s\'. Tag already present",
1434 /* here begins the family of parser functions that reformat header lines */
1436 /*********************************************************************
1438 * Function : filter_header
1440 * Description : Executes all text substitutions from all applying
1441 * +(server|client)-header-filter actions on the header.
1442 * Most of the code was copied from pcrs_filter_response,
1443 * including the rather short variable names
1446 * 1 : csp = Current client state (buffers, headers, etc...)
1447 * 2 : header = On input, pointer to header to modify.
1448 * On output, pointer to the modified header, or NULL
1449 * to remove the header. This function frees the
1450 * original string if necessary.
1452 * Returns : JB_ERR_OK on success and always succeeds
1454 *********************************************************************/
1455 static jb_err filter_header(struct client_state *csp, char **header)
1459 size_t size = strlen(*header);
1461 char *newheader = NULL;
1464 struct re_filterfile_spec *b;
1465 struct list_entry *filtername;
1467 enum filter_type wanted_filter_type;
1468 int multi_action_index;
1470 if (csp->flags & CSP_FLAG_NO_FILTERING)
1475 if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
1477 wanted_filter_type = FT_SERVER_HEADER_FILTER;
1478 multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
1482 wanted_filter_type = FT_CLIENT_HEADER_FILTER;
1483 multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
1486 if (list_is_empty(csp->action->multi[multi_action_index])
1487 || filters_available(csp) == FALSE)
1489 /* Return early if no filters apply or if none are available. */
1493 /* Execute all applying header filters */
1494 for (filtername = csp->action->multi[multi_action_index]->first;
1495 filtername != NULL; filtername = filtername->next)
1497 int current_hits = 0;
1500 b = get_filter(csp, filtername->str, wanted_filter_type);
1506 joblist = b->joblist;
1508 if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
1510 if (NULL == joblist)
1512 log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
1516 log_error(LOG_LEVEL_RE_FILTER, "filtering \'%s\' (size %d) with \'%s\' ...",
1517 *header, size, b->name);
1519 /* Apply all jobs from the joblist */
1520 for (job = joblist; NULL != job; job = job->next)
1522 matches = pcrs_execute(job, *header, size, &newheader, &size);
1525 current_hits += matches;
1526 log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
1528 *header = newheader;
1530 else if (0 == matches)
1532 /* Filter doesn't change header */
1538 log_error(LOG_LEVEL_ERROR, "Filtering \'%s\' with \'%s\' didn't work out: %s",
1539 *header, b->name, pcrs_strerror(matches));
1540 if (newheader != NULL)
1542 log_error(LOG_LEVEL_ERROR, "Freeing what's left: %s", newheader);
1548 if (b->dynamic) pcrs_free_joblist(joblist);
1550 log_error(LOG_LEVEL_RE_FILTER, "... produced %d hits (new size %d).", current_hits, size);
1551 hits += current_hits;
1555 * Additionally checking for hits is important because if
1556 * the continue hack is triggered, server headers can
1557 * arrive empty to separate multiple heads from each other.
1559 if ((0 == size) && hits)
1561 log_error(LOG_LEVEL_HEADER, "Removing empty header %s", *header);
1569 /*********************************************************************
1571 * Function : server_connection
1573 * Description : Makes sure a proper "Connection:" header is
1574 * set and signals connection_header_adder to
1578 * 1 : csp = Current client state (buffers, headers, etc...)
1579 * 2 : header = On input, pointer to header to modify.
1580 * On output, pointer to the modified header, or NULL
1581 * to remove the header. This function frees the
1582 * original string if necessary.
1584 * Returns : JB_ERR_OK on success.
1586 *********************************************************************/
1587 static jb_err server_connection(struct client_state *csp, char **header)
1589 if (!strcmpic(*header, "Connection: keep-alive")
1590 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1591 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
1595 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1596 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE))
1598 csp->flags |= CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE;
1601 if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
1603 log_error(LOG_LEVEL_HEADER,
1604 "Keeping the server header '%s' around.", *header);
1607 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
1609 char *old_header = *header;
1611 *header = strdup_or_die("Connection: close");
1612 log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
1617 /* Signal server_connection_adder() to return early. */
1618 csp->flags |= CSP_FLAG_SERVER_CONNECTION_HEADER_SET;
1624 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1625 /*********************************************************************
1627 * Function : server_keep_alive
1629 * Description : Stores the server's keep alive timeout.
1632 * 1 : csp = Current client state (buffers, headers, etc...)
1633 * 2 : header = On input, pointer to header to modify.
1634 * On output, pointer to the modified header, or NULL
1635 * to remove the header. This function frees the
1636 * original string if necessary.
1638 * Returns : JB_ERR_OK.
1640 *********************************************************************/
1641 static jb_err server_keep_alive(struct client_state *csp, char **header)
1643 unsigned int keep_alive_timeout;
1644 const char *timeout_position = strstr(*header, "timeout=");
1646 if ((NULL == timeout_position)
1647 || (1 != sscanf(timeout_position, "timeout=%u", &keep_alive_timeout)))
1649 log_error(LOG_LEVEL_ERROR, "Couldn't parse: %s", *header);
1653 if (keep_alive_timeout < csp->server_connection.keep_alive_timeout)
1655 log_error(LOG_LEVEL_HEADER,
1656 "Reducing keep-alive timeout from %u to %u.",
1657 csp->server_connection.keep_alive_timeout, keep_alive_timeout);
1658 csp->server_connection.keep_alive_timeout = keep_alive_timeout;
1662 /* XXX: Is this log worthy? */
1663 log_error(LOG_LEVEL_HEADER,
1664 "Server keep-alive timeout is %u. Sticking with %u.",
1665 keep_alive_timeout, csp->server_connection.keep_alive_timeout);
1667 csp->flags |= CSP_FLAG_SERVER_KEEP_ALIVE_TIMEOUT_SET;
1676 /*********************************************************************
1678 * Function : server_proxy_connection
1680 * Description : Figures out whether or not we should add a
1681 * Proxy-Connection header.
1684 * 1 : csp = Current client state (buffers, headers, etc...)
1685 * 2 : header = On input, pointer to header to modify.
1686 * On output, pointer to the modified header, or NULL
1687 * to remove the header. This function frees the
1688 * original string if necessary.
1690 * Returns : JB_ERR_OK.
1692 *********************************************************************/
1693 static jb_err server_proxy_connection(struct client_state *csp, char **header)
1695 csp->flags |= CSP_FLAG_SERVER_PROXY_CONNECTION_HEADER_SET;
1700 /*********************************************************************
1702 * Function : proxy_authentication
1704 * Description : Removes headers that are relevant for proxy
1705 * authentication unless forwarding them has
1706 * been explicitly requested.
1709 * 1 : csp = Current client state (buffers, headers, etc...)
1710 * 2 : header = On input, pointer to header to modify.
1711 * On output, pointer to the modified header, or NULL
1712 * to remove the header. This function frees the
1713 * original string if necessary.
1715 * Returns : JB_ERR_OK.
1717 *********************************************************************/
1718 static jb_err proxy_authentication(struct client_state *csp, char **header)
1720 if ((csp->config->feature_flags &
1721 RUNTIME_FEATURE_FORWARD_PROXY_AUTHENTICATION_HEADERS) == 0) {
1722 log_error(LOG_LEVEL_HEADER,
1723 "Forwarding proxy authentication headers is disabled. Crunching: %s", *header);
1730 /*********************************************************************
1732 * Function : client_keep_alive
1734 * Description : Stores the client's keep alive timeout.
1737 * 1 : csp = Current client state (buffers, headers, etc...)
1738 * 2 : header = On input, pointer to header to modify.
1739 * On output, pointer to the modified header, or NULL
1740 * to remove the header. This function frees the
1741 * original string if necessary.
1743 * Returns : JB_ERR_OK.
1745 *********************************************************************/
1746 static jb_err client_keep_alive(struct client_state *csp, char **header)
1748 unsigned int keep_alive_timeout;
1749 char *timeout_position;
1751 if (!(csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE))
1753 log_error(LOG_LEVEL_HEADER,
1754 "keep-alive support is disabled. Crunching: %s.", *header);
1759 /* Check for parameter-less format "Keep-Alive: 100" */
1760 timeout_position = strstr(*header, ": ");
1761 if ((NULL == timeout_position)
1762 || (1 != sscanf(timeout_position, ": %u", &keep_alive_timeout)))
1764 /* Assume parameter format "Keep-Alive: timeout=100" */
1765 timeout_position = strstr(*header, "timeout=");
1766 if ((NULL == timeout_position)
1767 || (1 != sscanf(timeout_position, "timeout=%u", &keep_alive_timeout)))
1769 log_error(LOG_LEVEL_HEADER,
1770 "Couldn't parse: '%s'. Using default timeout %u",
1771 *header, csp->config->keep_alive_timeout);
1778 if (keep_alive_timeout < csp->config->keep_alive_timeout)
1780 log_error(LOG_LEVEL_HEADER,
1781 "Reducing keep-alive timeout from %u to %u.",
1782 csp->config->keep_alive_timeout, keep_alive_timeout);
1783 csp->server_connection.keep_alive_timeout = keep_alive_timeout;
1787 /* XXX: Is this log worthy? */
1788 log_error(LOG_LEVEL_HEADER,
1789 "Client keep-alive timeout is %u. Sticking with %u.",
1790 keep_alive_timeout, csp->config->keep_alive_timeout);
1798 /*********************************************************************
1800 * Function : get_content_length
1802 * Description : Gets the content length specified in a
1803 * Content-Length header.
1806 * 1 : header_value = The Content-Length header value.
1807 * 2 : length = Storage to return the value.
1809 * Returns : JB_ERR_OK on success, or
1810 * JB_ERR_PARSE if no value is recognized.
1812 *********************************************************************/
1813 static jb_err get_content_length(const char *header_value, unsigned long long *length)
1816 #if SIZEOF_LONG_LONG < 8
1817 #error sizeof(unsigned long long) too small
1819 if (1 != sscanf(header_value, "%I64u", length))
1821 if (1 != sscanf(header_value, "%llu", length))
1824 return JB_ERR_PARSE;
1831 /*********************************************************************
1833 * Function : client_save_content_length
1835 * Description : Save the Content-Length sent by the client.
1838 * 1 : csp = Current client state (buffers, headers, etc...)
1839 * 2 : header = On input, pointer to header to modify.
1840 * On output, pointer to the modified header, or NULL
1841 * to remove the header. This function frees the
1842 * original string if necessary.
1844 * Returns : JB_ERR_OK on success, or
1845 * JB_ERR_MEMORY on out-of-memory error.
1847 *********************************************************************/
1848 static jb_err client_save_content_length(struct client_state *csp, char **header)
1850 unsigned long long content_length = 0;
1851 const char *header_value;
1853 assert(*(*header+14) == ':');
1855 header_value = *header + 15;
1856 if (JB_ERR_OK != get_content_length(header_value, &content_length))
1858 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
1863 csp->expected_client_content_length = content_length;
1868 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1872 /*********************************************************************
1874 * Function : client_connection
1876 * Description : Makes sure a proper "Connection:" header is
1877 * set and signals connection_header_adder
1881 * 1 : csp = Current client state (buffers, headers, etc...)
1882 * 2 : header = On input, pointer to header to modify.
1883 * On output, pointer to the modified header, or NULL
1884 * to remove the header. This function frees the
1885 * original string if necessary.
1887 * Returns : JB_ERR_OK on success.
1889 *********************************************************************/
1890 static jb_err client_connection(struct client_state *csp, char **header)
1892 static const char connection_close[] = "Connection: close";
1894 if (!strcmpic(*header, connection_close))
1896 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1897 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
1898 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
1900 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1902 log_error(LOG_LEVEL_HEADER,
1903 "Removing \'%s\' to imply keep-alive.", *header);
1906 * While we imply keep-alive to the server,
1907 * we have to remember that the client didn't.
1909 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1913 char *old_header = *header;
1915 *header = strdup_or_die("Connection: keep-alive");
1916 log_error(LOG_LEVEL_HEADER,
1917 "Replaced: \'%s\' with \'%s\'", old_header, *header);
1923 log_error(LOG_LEVEL_HEADER,
1924 "Keeping the client header '%s' around. "
1925 "The connection will not be kept alive.",
1927 csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1930 else if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1931 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
1933 log_error(LOG_LEVEL_HEADER,
1934 "Keeping the client header '%s' around. "
1935 "The server connection will be kept alive if possible.",
1937 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1938 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1942 char *old_header = *header;
1944 *header = strdup_or_die(connection_close);
1945 log_error(LOG_LEVEL_HEADER,
1946 "Replaced: \'%s\' with \'%s\'", old_header, *header);
1950 /* Signal client_connection_header_adder() to return early. */
1951 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_HEADER_SET;
1957 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1958 /*********************************************************************
1960 * Function : client_proxy_connection
1962 * Description : Sets the CLIENT_CONNECTION_KEEP_ALIVE flag when
1963 * appropriate and removes the Proxy-Connection
1967 * 1 : csp = Current client state (buffers, headers, etc...)
1968 * 2 : header = On input, pointer to header to modify.
1969 * On output, pointer to the modified header, or NULL
1970 * to remove the header. This function frees the
1971 * original string if necessary.
1973 * Returns : JB_ERR_OK
1975 *********************************************************************/
1976 static jb_err client_proxy_connection(struct client_state *csp, char **header)
1978 if (0 == (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
1979 && (csp->http->ssl == 0)
1980 && (NULL == strstr(*header, "close")))
1982 log_error(LOG_LEVEL_HEADER,
1983 "The client connection can be kept alive due to: %s", *header);
1984 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1986 crumble(csp, header);
1990 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1993 /*********************************************************************
1995 * Function : client_transfer_encoding
1997 * Description : Raise the CSP_FLAG_CHUNKED_CLIENT_BODY flag if
1998 * the request body is "chunked"
2000 * XXX: Currently not called through sed() as we
2001 * need the flag earlier on. Should be fixed.
2004 * 1 : csp = Current client state (buffers, headers, etc...)
2005 * 2 : header = On input, pointer to header to modify.
2006 * On output, pointer to the modified header, or NULL
2007 * to remove the header. This function frees the
2008 * original string if necessary.
2010 * Returns : JB_ERR_OK on success, or
2012 *********************************************************************/
2013 jb_err client_transfer_encoding(struct client_state *csp, char **header)
2015 if (strstr(*header, "chunked"))
2017 csp->flags |= CSP_FLAG_CHUNKED_CLIENT_BODY;
2018 log_error(LOG_LEVEL_HEADER, "Expecting chunked client body");
2025 /*********************************************************************
2027 * Function : client_expect
2029 * Description : Raise the CSP_FLAG_UNSUPPORTED_CLIENT_EXPECTATION
2030 * if the Expect header value is unsupported.
2032 * Rejecting unsupported expectations is a RFC 7231 5.1.1
2033 * MAY and a RFC 2616 (obsolete) MUST.
2036 * 1 : csp = Current client state (buffers, headers, etc...)
2037 * 2 : header = On input, pointer to header to modify.
2038 * On output, pointer to the modified header, or NULL
2039 * to remove the header. This function frees the
2040 * original string if necessary.
2042 * Returns : JB_ERR_OK on success, or
2044 *********************************************************************/
2045 jb_err client_expect(struct client_state *csp, char **header)
2047 if (0 != strcmpic(*header, "Expect: 100-continue"))
2049 csp->flags |= CSP_FLAG_UNSUPPORTED_CLIENT_EXPECTATION;
2050 log_error(LOG_LEVEL_HEADER,
2051 "Unsupported client expectaction: %s", *header);
2059 /*********************************************************************
2061 * Function : crumble
2063 * Description : This is called if a header matches a pattern to "crunch"
2066 * 1 : csp = Current client state (buffers, headers, etc...)
2067 * 2 : header = On input, pointer to header to modify.
2068 * On output, pointer to the modified header, or NULL
2069 * to remove the header. This function frees the
2070 * original string if necessary.
2072 * Returns : JB_ERR_OK on success, or
2073 * JB_ERR_MEMORY on out-of-memory error.
2075 *********************************************************************/
2076 static jb_err crumble(struct client_state *csp, char **header)
2079 log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
2085 /*********************************************************************
2087 * Function : crunch_server_header
2089 * Description : Crunch server header if it matches a string supplied by the
2090 * user. Called from `sed'.
2093 * 1 : csp = Current client state (buffers, headers, etc...)
2094 * 2 : header = On input, pointer to header to modify.
2095 * On output, pointer to the modified header, or NULL
2096 * to remove the header. This function frees the
2097 * original string if necessary.
2099 * Returns : JB_ERR_OK on success and always succeeds
2101 *********************************************************************/
2102 static jb_err crunch_server_header(struct client_state *csp, char **header)
2104 const char *crunch_pattern;
2106 /* Do we feel like crunching? */
2107 if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
2109 crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
2111 /* Is the current header the lucky one? */
2112 if (strstr(*header, crunch_pattern))
2114 log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
2123 /*********************************************************************
2125 * Function : server_content_type
2127 * Description : Set the content-type for filterable types (text/.*,
2128 * .*xml.*, .*script.* and image/gif) unless filtering has been
2129 * forbidden (CT_TABOO) while parsing earlier headers.
2130 * NOTE: Since text/plain is commonly used by web servers
2131 * for files whose correct type is unknown, we don't
2132 * set CT_TEXT for it.
2135 * 1 : csp = Current client state (buffers, headers, etc...)
2136 * 2 : header = On input, pointer to header to modify.
2137 * On output, pointer to the modified header, or NULL
2138 * to remove the header. This function frees the
2139 * original string if necessary.
2141 * Returns : JB_ERR_OK on success, or
2142 * JB_ERR_MEMORY on out-of-memory error.
2144 *********************************************************************/
2145 static jb_err server_content_type(struct client_state *csp, char **header)
2147 /* Remove header if it isn't the first Content-Type header */
2148 if ((csp->content_type & CT_DECLARED))
2150 if (content_filters_enabled(csp->action))
2153 * Making sure the client interprets the content the same way
2154 * Privoxy did is only relevant if Privoxy modified it.
2156 * Checking for this is "hard" as it's not yet known when
2157 * this function is called, thus go shopping and and just
2158 * check if Privoxy could filter it.
2160 * The main thing is that we don't mess with the headers
2161 * unless the user signalled that it's acceptable.
2163 log_error(LOG_LEVEL_HEADER,
2164 "Multiple Content-Type headers detected. "
2165 "Removing and ignoring: %s",
2173 * Signal that the Content-Type has been set.
2175 csp->content_type |= CT_DECLARED;
2177 if (!(csp->content_type & CT_TABOO))
2180 * XXX: The assumption that text/plain is a sign of
2181 * binary data seems to be somewhat unreasonable nowadays
2182 * and should be dropped after 3.0.8 is out.
2184 if ((strstr(*header, "text/") && !strstr(*header, "plain"))
2185 || strstr(*header, "xml")
2186 || strstr(*header, "script"))
2188 csp->content_type |= CT_TEXT;
2190 else if (strstr(*header, "image/gif"))
2192 csp->content_type |= CT_GIF;
2197 * Are we messing with the content type?
2199 if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
2202 * Make sure the user doesn't accidentally
2203 * change the content type of binary documents.
2205 if ((csp->content_type & CT_TEXT) || (csp->action->flags & ACTION_FORCE_TEXT_MODE))
2209 *header = strdup_or_die("Content-Type: ");
2211 err = string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
2212 if (JB_ERR_OK != err)
2214 log_error(LOG_LEVEL_HEADER, "Insufficient memory to replace Content-Type!");
2215 return JB_ERR_MEMORY;
2217 log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
2221 log_error(LOG_LEVEL_HEADER, "%s not replaced. "
2222 "It doesn't look like a content type that should be filtered. "
2223 "Enable force-text-mode if you know what you're doing.", *header);
2231 /*********************************************************************
2233 * Function : server_transfer_coding
2235 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
2236 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
2237 * - Remove header if body was chunked but has been
2238 * de-chunked for filtering.
2241 * 1 : csp = Current client state (buffers, headers, etc...)
2242 * 2 : header = On input, pointer to header to modify.
2243 * On output, pointer to the modified header, or NULL
2244 * to remove the header. This function frees the
2245 * original string if necessary.
2247 * Returns : JB_ERR_OK on success, or
2248 * JB_ERR_MEMORY on out-of-memory error.
2250 *********************************************************************/
2251 static jb_err server_transfer_coding(struct client_state *csp, char **header)
2254 * Turn off pcrs and gif filtering if body compressed
2256 if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
2260 * XXX: Added to test if we could use CT_GZIP and CT_DEFLATE here.
2262 log_error(LOG_LEVEL_INFO, "Marking content type for %s as CT_TABOO because of %s.",
2263 csp->http->cmd, *header);
2264 #endif /* def FEATURE_ZLIB */
2265 csp->content_type = CT_TABOO;
2269 * Raise flag if body chunked
2271 if (strstr(*header, "chunked"))
2273 csp->flags |= CSP_FLAG_CHUNKED;
2276 * If the body was modified, it has been de-chunked first
2277 * and the header must be removed.
2279 * FIXME: If there is more than one transfer encoding,
2280 * only the "chunked" part should be removed here.
2282 if (csp->flags & CSP_FLAG_MODIFIED)
2284 log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
2293 /*********************************************************************
2295 * Function : server_content_encoding
2297 * Description : Used to check if the content is compressed, and if
2298 * FEATURE_ZLIB is disabled, filtering is disabled as
2301 * If FEATURE_ZLIB is enabled and the compression type
2302 * supported, the content is marked for decompression.
2304 * XXX: Doesn't properly deal with multiple or with
2305 * unsupported but unknown encodings.
2306 * Is case-sensitive but shouldn't be.
2309 * 1 : csp = Current client state (buffers, headers, etc...)
2310 * 2 : header = On input, pointer to header to modify.
2311 * On output, pointer to the modified header, or NULL
2312 * to remove the header. This function frees the
2313 * original string if necessary.
2315 * Returns : JB_ERR_OK on success, or
2316 * JB_ERR_MEMORY on out-of-memory error.
2318 *********************************************************************/
2319 static jb_err server_content_encoding(struct client_state *csp, char **header)
2322 if (strstr(*header, "sdch"))
2325 * Shared Dictionary Compression over HTTP isn't supported,
2326 * filtering it anyway is pretty much guaranteed to mess up
2329 csp->content_type |= CT_TABOO;
2332 * Log a warning if the user expects the content to be filtered.
2334 if (content_filters_enabled(csp->action))
2336 log_error(LOG_LEVEL_INFO,
2337 "SDCH-compressed content detected, content filtering disabled. "
2338 "Consider suppressing SDCH offers made by the client.");
2341 else if (strstr(*header, "gzip"))
2343 /* Mark for gzip decompression */
2344 csp->content_type |= CT_GZIP;
2346 else if (strstr(*header, "deflate"))
2348 /* Mark for zlib decompression */
2349 csp->content_type |= CT_DEFLATE;
2351 else if (strstr(*header, "compress"))
2354 * We can't decompress this; therefore we can't filter
2357 csp->content_type |= CT_TABOO;
2359 #else /* !defined(FEATURE_ZLIB) */
2361 * XXX: Using a black list here isn't the right approach.
2363 * In case of SDCH, building with zlib support isn't
2366 if (strstr(*header, "gzip") ||
2367 strstr(*header, "compress") ||
2368 strstr(*header, "deflate") ||
2369 strstr(*header, "sdch"))
2372 * Body is compressed, turn off pcrs and gif filtering.
2374 csp->content_type |= CT_TABOO;
2377 * Log a warning if the user expects the content to be filtered.
2379 if (content_filters_enabled(csp->action))
2381 log_error(LOG_LEVEL_INFO,
2382 "Compressed content detected, content filtering disabled. "
2383 "Consider recompiling Privoxy with zlib support or "
2384 "enable the prevent-compression action.");
2387 #endif /* defined(FEATURE_ZLIB) */
2395 /*********************************************************************
2397 * Function : server_adjust_content_encoding
2399 * Description : Remove the Content-Encoding header if the
2400 * decompression was successful and the content
2404 * 1 : csp = Current client state (buffers, headers, etc...)
2405 * 2 : header = On input, pointer to header to modify.
2406 * On output, pointer to the modified header, or NULL
2407 * to remove the header. This function frees the
2408 * original string if necessary.
2410 * Returns : JB_ERR_OK on success, or
2411 * JB_ERR_MEMORY on out-of-memory error.
2413 *********************************************************************/
2414 static jb_err server_adjust_content_encoding(struct client_state *csp, char **header)
2416 if ((csp->flags & CSP_FLAG_MODIFIED)
2417 && (csp->content_type & (CT_GZIP | CT_DEFLATE)))
2420 * We successfully decompressed the content,
2421 * and have to clean the header now, so the
2422 * client no longer expects compressed data.
2424 * XXX: There is a difference between cleaning
2425 * and removing it completely.
2427 log_error(LOG_LEVEL_HEADER, "Crunching: %s", *header);
2434 #endif /* defined(FEATURE_ZLIB) */
2437 /*********************************************************************
2439 * Function : server_adjust_content_length
2441 * Description : Adjust Content-Length header if we modified
2445 * 1 : csp = Current client state (buffers, headers, etc...)
2446 * 2 : header = On input, pointer to header to modify.
2447 * On output, pointer to the modified header, or NULL
2448 * to remove the header. This function frees the
2449 * original string if necessary.
2451 * Returns : JB_ERR_OK on success, or
2452 * JB_ERR_MEMORY on out-of-memory error.
2454 *********************************************************************/
2455 static jb_err server_adjust_content_length(struct client_state *csp, char **header)
2457 /* Regenerate header if the content was modified. */
2458 if (csp->flags & CSP_FLAG_MODIFIED)
2460 const size_t header_length = 50;
2462 *header = malloc(header_length);
2463 if (*header == NULL)
2465 return JB_ERR_MEMORY;
2467 create_content_length_header(csp->content_length, *header, header_length);
2468 log_error(LOG_LEVEL_HEADER,
2469 "Adjusted Content-Length to %llu", csp->content_length);
2476 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2477 /*********************************************************************
2479 * Function : server_save_content_length
2481 * Description : Save the Content-Length sent by the server.
2484 * 1 : csp = Current client state (buffers, headers, etc...)
2485 * 2 : header = On input, pointer to header to modify.
2486 * On output, pointer to the modified header, or NULL
2487 * to remove the header. This function frees the
2488 * original string if necessary.
2490 * Returns : JB_ERR_OK on success, or
2491 * JB_ERR_MEMORY on out-of-memory error.
2493 *********************************************************************/
2494 static jb_err server_save_content_length(struct client_state *csp, char **header)
2496 unsigned long long content_length = 0;
2497 const char *header_value;
2499 assert(*(*header+14) == ':');
2501 header_value = *header + 15;
2502 if (JB_ERR_OK != get_content_length(header_value, &content_length))
2504 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
2509 csp->expected_content_length = content_length;
2510 csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
2511 csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
2516 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2519 /*********************************************************************
2521 * Function : server_content_md5
2523 * Description : Crumble any Content-MD5 headers if the document was
2524 * modified. FIXME: Should we re-compute instead?
2527 * 1 : csp = Current client state (buffers, headers, etc...)
2528 * 2 : header = On input, pointer to header to modify.
2529 * On output, pointer to the modified header, or NULL
2530 * to remove the header. This function frees the
2531 * original string if necessary.
2533 * Returns : JB_ERR_OK on success, or
2534 * JB_ERR_MEMORY on out-of-memory error.
2536 *********************************************************************/
2537 static jb_err server_content_md5(struct client_state *csp, char **header)
2539 if (csp->flags & CSP_FLAG_MODIFIED)
2541 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
2549 /*********************************************************************
2551 * Function : server_content_disposition
2553 * Description : If enabled, blocks or modifies the "Content-Disposition" header.
2554 * Called from `sed'.
2557 * 1 : csp = Current client state (buffers, headers, etc...)
2558 * 2 : header = On input, pointer to header to modify.
2559 * On output, pointer to the modified header, or NULL
2560 * to remove the header. This function frees the
2561 * original string if necessary.
2563 * Returns : JB_ERR_OK on success, or
2564 * JB_ERR_MEMORY on out-of-memory error.
2566 *********************************************************************/
2567 static jb_err server_content_disposition(struct client_state *csp, char **header)
2572 * Are we messing with the Content-Disposition header?
2574 if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
2580 newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
2582 if ((newval == NULL) || (0 == strcmpic(newval, "block")))
2585 * Blocking content-disposition header
2587 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2594 * Replacing Content-Disposition header
2597 *header = strdup("Content-Disposition: ");
2598 string_append(header, newval);
2600 if (*header != NULL)
2602 log_error(LOG_LEVEL_HEADER,
2603 "Content-Disposition header crunched and replaced with: %s", *header);
2606 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2610 /*********************************************************************
2612 * Function : server_last_modified
2614 * Description : Changes Last-Modified header to the actual date
2615 * to help hide-if-modified-since.
2616 * Called from `sed'.
2619 * 1 : csp = Current client state (buffers, headers, etc...)
2620 * 2 : header = On input, pointer to header to modify.
2621 * On output, pointer to the modified header, or NULL
2622 * to remove the header. This function frees the
2623 * original string if necessary.
2625 * Returns : JB_ERR_OK on success, or
2626 * JB_ERR_MEMORY on out-of-memory error.
2628 *********************************************************************/
2629 static jb_err server_last_modified(struct client_state *csp, char **header)
2632 time_t last_modified;
2636 * Are we messing with the Last-Modified header?
2638 if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
2644 newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
2646 if (0 == strcmpic(newval, "block"))
2649 * Blocking Last-Modified header. Useless but why not.
2651 log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
2655 else if (0 == strcmpic(newval, "reset-to-request-time"))
2658 * Setting Last-Modified Header to now.
2661 get_http_time(0, buf, sizeof(buf));
2663 *header = strdup("Last-Modified: ");
2664 string_append(header, buf);
2666 if (*header == NULL)
2668 log_error(LOG_LEVEL_HEADER, "Insufficient memory. Last-Modified header got lost, boohoo.");
2672 log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
2675 else if (0 == strcmpic(newval, "randomize"))
2677 log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
2679 if (JB_ERR_OK != parse_time_header(*header, &last_modified))
2681 log_error(LOG_LEVEL_HEADER,
2682 "Couldn't parse time in %s (crunching!)", *header);
2688 struct tm *timeptr = NULL;
2690 #ifdef HAVE_GMTIME_R
2694 rtime = (long int)difftime(now, last_modified);
2697 long int days, hours, minutes, seconds;
2698 const int negative_delta = (rtime < 0);
2703 log_error(LOG_LEVEL_HEADER, "Server time in the future.");
2705 rtime = pick_from_range(rtime);
2710 last_modified += rtime;
2711 #ifdef HAVE_GMTIME_R
2712 timeptr = gmtime_r(&last_modified, &gmt);
2713 #elif defined(MUTEX_LOCKS_AVAILABLE)
2714 privoxy_mutex_lock(&gmtime_mutex);
2715 timeptr = gmtime(&last_modified);
2716 privoxy_mutex_unlock(&gmtime_mutex);
2718 timeptr = gmtime(&last_modified);
2720 if ((NULL == timeptr) || !strftime(newheader,
2721 sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr))
2723 log_error(LOG_LEVEL_ERROR,
2724 "Randomizing '%s' failed. Crunching the header without replacement.",
2731 *header = strdup("Last-Modified: ");
2732 string_append(header, newheader);
2734 if (*header == NULL)
2736 log_error(LOG_LEVEL_ERROR, "Insufficient memory, header crunched without replacement.");
2737 return JB_ERR_MEMORY;
2740 days = rtime / (3600 * 24);
2741 hours = rtime / 3600 % 24;
2742 minutes = rtime / 60 % 60;
2743 seconds = rtime % 60;
2745 log_error(LOG_LEVEL_HEADER,
2746 "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
2747 *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
2748 minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
2752 log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
2761 /*********************************************************************
2763 * Function : client_accept_encoding
2765 * Description : Rewrite the client's Accept-Encoding header so that
2766 * if doesn't allow compression, if the action applies.
2767 * Note: For HTTP/1.0 the absence of the header is enough.
2770 * 1 : csp = Current client state (buffers, headers, etc...)
2771 * 2 : header = On input, pointer to header to modify.
2772 * On output, pointer to the modified header, or NULL
2773 * to remove the header. This function frees the
2774 * original string if necessary.
2776 * Returns : JB_ERR_OK on success, or
2777 * JB_ERR_MEMORY on out-of-memory error.
2779 *********************************************************************/
2780 static jb_err client_accept_encoding(struct client_state *csp, char **header)
2782 #ifdef FEATURE_COMPRESSION
2783 if ((csp->config->feature_flags & RUNTIME_FEATURE_COMPRESSION)
2784 && strstr(*header, "deflate"))
2786 csp->flags |= CSP_FLAG_CLIENT_SUPPORTS_DEFLATE;
2789 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2791 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
2799 /*********************************************************************
2801 * Function : client_te
2803 * Description : Rewrite the client's TE header so that
2804 * if doesn't allow compression, if the action applies.
2807 * 1 : csp = Current client state (buffers, headers, etc...)
2808 * 2 : header = On input, pointer to header to modify.
2809 * On output, pointer to the modified header, or NULL
2810 * to remove the header. This function frees the
2811 * original string if necessary.
2813 * Returns : JB_ERR_OK on success, or
2814 * JB_ERR_MEMORY on out-of-memory error.
2816 *********************************************************************/
2817 static jb_err client_te(struct client_state *csp, char **header)
2819 if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
2822 log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
2829 /*********************************************************************
2831 * Function : client_referrer
2833 * Description : Handle the "referer" config setting properly.
2834 * Called from `sed'.
2837 * 1 : csp = Current client state (buffers, headers, etc...)
2838 * 2 : header = On input, pointer to header to modify.
2839 * On output, pointer to the modified header, or NULL
2840 * to remove the header. This function frees the
2841 * original string if necessary.
2843 * Returns : JB_ERR_OK on success, or
2844 * JB_ERR_MEMORY on out-of-memory error.
2846 *********************************************************************/
2847 static jb_err client_referrer(struct client_state *csp, char **header)
2849 const char *parameter;
2850 /* booleans for parameters we have to check multiple times */
2851 int parameter_conditional_block;
2852 int parameter_conditional_forge;
2854 #ifdef FEATURE_FORCE_LOAD
2856 * Since the referrer can include the prefix even
2857 * if the request itself is non-forced, we must
2858 * clean it unconditionally.
2860 * XXX: strclean is too broad
2862 strclean(*header, FORCE_PREFIX);
2863 #endif /* def FEATURE_FORCE_LOAD */
2865 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
2867 /* Nothing left to do */
2871 parameter = csp->action->string[ACTION_STRING_REFERER];
2872 assert(parameter != NULL);
2873 parameter_conditional_block = (0 == strcmpic(parameter, "conditional-block"));
2874 parameter_conditional_forge = (0 == strcmpic(parameter, "conditional-forge"));
2876 if (!parameter_conditional_block && !parameter_conditional_forge)
2879 * As conditional-block and conditional-forge are the only
2880 * parameters that rely on the original referrer, we can
2881 * remove it now for all the others.
2886 if (0 == strcmpic(parameter, "block"))
2888 log_error(LOG_LEVEL_HEADER, "Referer crunched!");
2891 else if (parameter_conditional_block || parameter_conditional_forge)
2893 return handle_conditional_hide_referrer_parameter(header,
2894 csp->http->hostport, parameter_conditional_block);
2896 else if (0 == strcmpic(parameter, "forge"))
2898 return create_forged_referrer(header, csp->http->hostport);
2902 /* interpret parameter as user-supplied referer to fake */
2903 return create_fake_referrer(header, parameter);
2908 /*********************************************************************
2910 * Function : client_accept_language
2912 * Description : Handle the "Accept-Language" config setting properly.
2913 * Called from `sed'.
2916 * 1 : csp = Current client state (buffers, headers, etc...)
2917 * 2 : header = On input, pointer to header to modify.
2918 * On output, pointer to the modified header, or NULL
2919 * to remove the header. This function frees the
2920 * original string if necessary.
2922 * Returns : JB_ERR_OK on success, or
2923 * JB_ERR_MEMORY on out-of-memory error.
2925 *********************************************************************/
2926 static jb_err client_accept_language(struct client_state *csp, char **header)
2931 * Are we messing with the Accept-Language?
2933 if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
2935 /*I don't think so*/
2939 newval = csp->action->string[ACTION_STRING_LANGUAGE];
2941 if ((newval == NULL) || (0 == strcmpic(newval, "block")))
2944 * Blocking Accept-Language header
2946 log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
2953 * Replacing Accept-Language header
2956 *header = strdup("Accept-Language: ");
2957 string_append(header, newval);
2959 if (*header == NULL)
2961 log_error(LOG_LEVEL_ERROR,
2962 "Insufficient memory. Accept-Language header crunched without replacement.");
2966 log_error(LOG_LEVEL_HEADER,
2967 "Accept-Language header crunched and replaced with: %s", *header);
2970 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
2974 /*********************************************************************
2976 * Function : crunch_client_header
2978 * Description : Crunch client header if it matches a string supplied by the
2979 * user. Called from `sed'.
2982 * 1 : csp = Current client state (buffers, headers, etc...)
2983 * 2 : header = On input, pointer to header to modify.
2984 * On output, pointer to the modified header, or NULL
2985 * to remove the header. This function frees the
2986 * original string if necessary.
2988 * Returns : JB_ERR_OK on success and always succeeds
2990 *********************************************************************/
2991 static jb_err crunch_client_header(struct client_state *csp, char **header)
2993 const char *crunch_pattern;
2995 /* Do we feel like crunching? */
2996 if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
2998 crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
3000 /* Is the current header the lucky one? */
3001 if (strstr(*header, crunch_pattern))
3003 log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
3011 /*********************************************************************
3013 * Function : client_uagent
3015 * Description : Handle the "user-agent" config setting properly
3016 * and remember its original value to enable browser
3017 * bug workarounds. Called from `sed'.
3020 * 1 : csp = Current client state (buffers, headers, etc...)
3021 * 2 : header = On input, pointer to header to modify.
3022 * On output, pointer to the modified header, or NULL
3023 * to remove the header. This function frees the
3024 * original string if necessary.
3026 * Returns : JB_ERR_OK on success, or
3027 * JB_ERR_MEMORY on out-of-memory error.
3029 *********************************************************************/
3030 static jb_err client_uagent(struct client_state *csp, char **header)
3034 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
3039 newval = csp->action->string[ACTION_STRING_USER_AGENT];
3046 *header = strdup("User-Agent: ");
3047 string_append(header, newval);
3049 log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
3051 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3055 /*********************************************************************
3057 * Function : client_ua
3059 * Description : Handle "ua-" headers properly. Called from `sed'.
3062 * 1 : csp = Current client state (buffers, headers, etc...)
3063 * 2 : header = On input, pointer to header to modify.
3064 * On output, pointer to the modified header, or NULL
3065 * to remove the header. This function frees the
3066 * original string if necessary.
3068 * Returns : JB_ERR_OK on success, or
3069 * JB_ERR_MEMORY on out-of-memory error.
3071 *********************************************************************/
3072 static jb_err client_ua(struct client_state *csp, char **header)
3074 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
3076 log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
3084 /*********************************************************************
3086 * Function : client_from
3088 * Description : Handle the "from" config setting properly.
3089 * Called from `sed'.
3092 * 1 : csp = Current client state (buffers, headers, etc...)
3093 * 2 : header = On input, pointer to header to modify.
3094 * On output, pointer to the modified header, or NULL
3095 * to remove the header. This function frees the
3096 * original string if necessary.
3098 * Returns : JB_ERR_OK on success, or
3099 * JB_ERR_MEMORY on out-of-memory error.
3101 *********************************************************************/
3102 static jb_err client_from(struct client_state *csp, char **header)
3106 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
3113 newval = csp->action->string[ACTION_STRING_FROM];
3116 * Are we blocking the e-mail address?
3118 if ((newval == NULL) || (0 == strcmpic(newval, "block")))
3120 log_error(LOG_LEVEL_HEADER, "crunched From!");
3124 log_error(LOG_LEVEL_HEADER, " modified");
3126 *header = strdup("From: ");
3127 string_append(header, newval);
3129 return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
3133 /*********************************************************************
3135 * Function : client_send_cookie
3137 * Description : Crunches the "cookie" header if necessary.
3138 * Called from `sed'.
3140 * XXX: Stupid name, doesn't send squat.
3143 * 1 : csp = Current client state (buffers, headers, etc...)
3144 * 2 : header = On input, pointer to header to modify.
3145 * On output, pointer to the modified header, or NULL
3146 * to remove the header. This function frees the
3147 * original string if necessary.
3149 * Returns : JB_ERR_OK on success, or
3150 * JB_ERR_MEMORY on out-of-memory error.
3152 *********************************************************************/
3153 static jb_err client_send_cookie(struct client_state *csp, char **header)
3155 if (csp->action->flags & ACTION_CRUNCH_OUTGOING_COOKIES)
3157 log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie: %s", *header);
3165 /*********************************************************************
3167 * Function : client_x_forwarded
3169 * Description : Handle the "x-forwarded-for" config setting properly,
3170 * also used in the add_client_headers list. Called from `sed'.
3173 * 1 : csp = Current client state (buffers, headers, etc...)
3174 * 2 : header = On input, pointer to header to modify.
3175 * On output, pointer to the modified header, or NULL
3176 * to remove the header. This function frees the
3177 * original string if necessary.
3179 * Returns : JB_ERR_OK on success, or
3180 * JB_ERR_MEMORY on out-of-memory error.
3182 *********************************************************************/
3183 jb_err client_x_forwarded(struct client_state *csp, char **header)
3185 if (0 != (csp->action->flags & ACTION_CHANGE_X_FORWARDED_FOR))
3187 const char *parameter = csp->action->string[ACTION_STRING_CHANGE_X_FORWARDED_FOR];
3189 if (0 == strcmpic(parameter, "block"))
3192 log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
3194 else if (0 == strcmpic(parameter, "add"))
3196 string_append(header, ", ");
3197 string_append(header, csp->ip_addr_str);
3199 if (*header == NULL)
3201 return JB_ERR_MEMORY;
3203 log_error(LOG_LEVEL_HEADER,
3204 "Appended client IP address to %s", *header);
3205 csp->flags |= CSP_FLAG_X_FORWARDED_FOR_APPENDED;
3209 log_error(LOG_LEVEL_FATAL,
3210 "Invalid change-x-forwarded-for parameter: '%s'", parameter);
3218 /*********************************************************************
3220 * Function : client_max_forwards
3222 * Description : If the HTTP method is OPTIONS or TRACE, subtract one
3223 * from the value of the Max-Forwards header field.
3226 * 1 : csp = Current client state (buffers, headers, etc...)
3227 * 2 : header = On input, pointer to header to modify.
3228 * On output, pointer to the modified header, or NULL
3229 * to remove the header. This function frees the
3230 * original string if necessary.
3232 * Returns : JB_ERR_OK on success, or
3233 * JB_ERR_MEMORY on out-of-memory error.
3235 *********************************************************************/
3236 static jb_err client_max_forwards(struct client_state *csp, char **header)
3240 if ((0 == strcmpic(csp->http->gpc, "trace")) ||
3241 (0 == strcmpic(csp->http->gpc, "options")))
3243 assert(*(*header+12) == ':');
3244 if (1 == sscanf(*header+12, ": %d", &max_forwards))
3246 if (max_forwards > 0)
3248 snprintf(*header, strlen(*header)+1, "Max-Forwards: %d", --max_forwards);
3249 log_error(LOG_LEVEL_HEADER,
3250 "Max-Forwards value for %s request reduced to %d.",
3251 csp->http->gpc, max_forwards);
3253 else if (max_forwards < 0)
3255 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3261 log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
3270 /*********************************************************************
3272 * Function : client_host
3274 * Description : If the request URI did not contain host and
3275 * port information, parse and evaluate the Host
3279 * 1 : csp = Current client state (buffers, headers, etc...)
3280 * 2 : header = On input, pointer to header to modify.
3281 * On output, pointer to the modified header, or NULL
3282 * to remove the header. This function frees the
3283 * original string if necessary.
3285 * Returns : JB_ERR_OK on success, or
3286 * JB_ERR_MEMORY on out-of-memory error.
3288 *********************************************************************/
3289 static jb_err client_host(struct client_state *csp, char **header)
3293 if (strlen(*header) < 7)
3295 log_error(LOG_LEVEL_HEADER, "Removing empty Host header");
3300 if (!csp->http->hostport || (*csp->http->hostport == '*') ||
3301 *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
3304 p = strdup_or_die((*header)+6);
3306 q = strdup_or_die(p);
3308 freez(csp->http->hostport);
3309 csp->http->hostport = p;
3310 freez(csp->http->host);
3311 csp->http->host = q;
3312 q = strchr(csp->http->host, ':');
3315 /* Terminate hostname and evaluate port string */
3317 csp->http->port = atoi(q);
3321 csp->http->port = csp->http->ssl ? 443 : 80;
3324 log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
3325 csp->http->hostport, csp->http->host, csp->http->port);
3328 /* Signal client_host_adder() to return right away */
3329 csp->flags |= CSP_FLAG_HOST_HEADER_IS_SET;
3335 /*********************************************************************
3337 * Function : client_if_modified_since
3339 * Description : Remove or modify the If-Modified-Since header.
3342 * 1 : csp = Current client state (buffers, headers, etc...)
3343 * 2 : header = On input, pointer to header to modify.
3344 * On output, pointer to the modified header, or NULL
3345 * to remove the header. This function frees the
3346 * original string if necessary.
3348 * Returns : JB_ERR_OK on success, or
3349 * JB_ERR_MEMORY on out-of-memory error.
3351 *********************************************************************/
3352 static jb_err client_if_modified_since(struct client_state *csp, char **header)
3355 #ifdef HAVE_GMTIME_R
3358 struct tm *timeptr = NULL;
3363 if (0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
3366 * The client got an error message because of a temporary problem,
3367 * the problem is gone and the client now tries to revalidate our
3368 * error message on the real server. The revalidation would always
3369 * end with the transmission of the whole document and there is
3370 * no need to expose the bogus If-Modified-Since header.
3372 log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
3375 else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
3377 newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
3379 if ((0 == strcmpic(newval, "block")))
3381 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3384 else /* add random value */
3386 if (JB_ERR_OK != parse_time_header(*header, &tm))
3388 log_error(LOG_LEVEL_HEADER,
3389 "Couldn't parse time in %s (crunching!)", *header);
3394 long int hours, minutes, seconds;
3395 long int rtime = strtol(newval, &endptr, 0);
3396 const int negative_range = (rtime < 0);
3400 log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
3401 *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
3407 rtime = pick_from_range(rtime);
3411 log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
3414 tm += rtime * (negative_range ? -1 : 1);
3415 #ifdef HAVE_GMTIME_R
3416 timeptr = gmtime_r(&tm, &gmt);
3417 #elif defined(MUTEX_LOCKS_AVAILABLE)
3418 privoxy_mutex_lock(&gmtime_mutex);
3419 timeptr = gmtime(&tm);
3420 privoxy_mutex_unlock(&gmtime_mutex);
3422 timeptr = gmtime(&tm);
3424 if ((NULL == timeptr) || !strftime(newheader,
3425 sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr))
3427 log_error(LOG_LEVEL_ERROR,
3428 "Randomizing '%s' failed. Crunching the header without replacement.",
3435 *header = strdup("If-Modified-Since: ");
3436 string_append(header, newheader);
3438 if (*header == NULL)
3440 log_error(LOG_LEVEL_HEADER, "Insufficient memory, header crunched without replacement.");
3441 return JB_ERR_MEMORY;
3444 hours = rtime / 3600;
3445 minutes = rtime / 60 % 60;
3446 seconds = rtime % 60;
3448 log_error(LOG_LEVEL_HEADER,
3449 "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
3450 *header, (negative_range) ? "subtracted" : "added", hours,
3451 (hours == 1) ? "r" : "rs", minutes, (minutes == 1) ? "e" : "es",
3452 seconds, (seconds == 1) ? ")" : "s)");
3461 /*********************************************************************
3463 * Function : client_if_none_match
3465 * Description : Remove the If-None-Match header.
3468 * 1 : csp = Current client state (buffers, headers, etc...)
3469 * 2 : header = On input, pointer to header to modify.
3470 * On output, pointer to the modified header, or NULL
3471 * to remove the header. This function frees the
3472 * original string if necessary.
3474 * Returns : JB_ERR_OK on success, or
3475 * JB_ERR_MEMORY on out-of-memory error.
3477 *********************************************************************/
3478 static jb_err client_if_none_match(struct client_state *csp, char **header)
3480 if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
3482 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3490 /*********************************************************************
3492 * Function : client_x_filter
3494 * Description : Disables filtering if the client set "X-Filter: No".
3495 * Called from `sed'.
3498 * 1 : csp = Current client state (buffers, headers, etc...)
3499 * 2 : header = On input, pointer to header to modify.
3500 * On output, pointer to the modified header, or NULL
3501 * to remove the header. This function frees the
3502 * original string if necessary.
3504 * Returns : JB_ERR_OK on success
3506 *********************************************************************/
3507 jb_err client_x_filter(struct client_state *csp, char **header)
3509 if (0 == strcmpic(*header, "X-Filter: No"))
3511 if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
3513 log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
3517 if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
3519 log_error(LOG_LEVEL_HEADER,
3520 "force-text-mode overruled the client's request to fetch without filtering!");
3524 csp->content_type = CT_TABOO; /* XXX: This hack shouldn't be necessary */
3525 csp->flags |= CSP_FLAG_NO_FILTERING;
3526 log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
3528 log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
3536 /*********************************************************************
3538 * Function : client_range
3540 * Description : Removes Range, Request-Range and If-Range headers if
3541 * content filtering is enabled and the range doesn't
3544 * If the client's version of the document has been
3545 * altered by Privoxy, the server could interpret the
3546 * range differently than the client intended in which
3547 * case the user could end up with corrupted content.
3549 * If the range starts at byte 0 this isn't an issue
3550 * so the header can pass. Partial requests like this
3551 * are used to render preview images for videos without
3552 * downloading the whole video.
3554 * While HTTP doesn't require that range requests are
3555 * honoured and the client could simply abort the download
3556 * after receiving a sufficient amount of data, various
3557 * clients don't handle complete responses to range
3558 * requests gracefully and emit misleading error messages
3562 * 1 : csp = Current client state (buffers, headers, etc...)
3563 * 2 : header = On input, pointer to header to modify.
3564 * On output, pointer to the modified header, or NULL
3565 * to remove the header. This function frees the
3566 * original string if necessary.
3568 * Returns : JB_ERR_OK
3570 *********************************************************************/
3571 static jb_err client_range(struct client_state *csp, char **header)
3573 if (content_filters_enabled(csp->action)
3574 && (0 != strncmpic(strstr(*header, ":"), ": bytes=0-", 10)))
3576 log_error(LOG_LEVEL_HEADER, "Content filtering is enabled."
3577 " Crunching: \'%s\' to prevent range-mismatch problems.", *header);
3584 /* the following functions add headers directly to the header list */
3586 /*********************************************************************
3588 * Function : client_host_adder
3590 * Description : Adds the Host: header field if it is missing.
3591 * Called from `sed'.
3594 * 1 : csp = Current client state (buffers, headers, etc...)
3596 * Returns : JB_ERR_OK on success, or
3597 * JB_ERR_MEMORY on out-of-memory error.
3599 *********************************************************************/
3600 static jb_err client_host_adder(struct client_state *csp)
3605 if (csp->flags & CSP_FLAG_HOST_HEADER_IS_SET)
3607 /* Header already set by the client, nothing to do. */
3611 if (!csp->http->hostport || !*(csp->http->hostport))
3613 log_error(LOG_LEVEL_ERROR, "Destination host unknown.");
3614 return JB_ERR_PARSE;
3618 * remove 'user:pass@' from 'proto://user:pass@host'
3620 if ((p = strchr( csp->http->hostport, '@')) != NULL)
3626 p = csp->http->hostport;
3629 /* XXX: Just add it, we already made sure that it will be unique */
3630 log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
3631 err = enlist_unique_header(csp->headers, "Host", p);
3637 /*********************************************************************
3639 * Function : client_xtra_adder
3641 * Description : Used in the add_client_headers list. Called from `sed'.
3644 * 1 : csp = Current client state (buffers, headers, etc...)
3646 * Returns : JB_ERR_OK on success, or
3647 * JB_ERR_MEMORY on out-of-memory error.
3649 *********************************************************************/
3650 static jb_err client_xtra_adder(struct client_state *csp)
3652 struct list_entry *lst;
3655 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
3656 lst ; lst = lst->next)
3658 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
3659 err = enlist(csp->headers, lst->str);
3671 /*********************************************************************
3673 * Function : client_x_forwarded_for_adder
3675 * Description : Used in the add_client_headers list. Called from `sed'.
3678 * 1 : csp = Current client state (buffers, headers, etc...)
3680 * Returns : JB_ERR_OK on success, or
3681 * JB_ERR_MEMORY on out-of-memory error.
3683 *********************************************************************/
3684 static jb_err client_x_forwarded_for_adder(struct client_state *csp)
3686 char *header = NULL;
3689 if (!((csp->action->flags & ACTION_CHANGE_X_FORWARDED_FOR)
3690 && (0 == strcmpic(csp->action->string[ACTION_STRING_CHANGE_X_FORWARDED_FOR], "add")))
3691 || (csp->flags & CSP_FLAG_X_FORWARDED_FOR_APPENDED))
3694 * If we aren't adding X-Forwarded-For headers,
3695 * or we already appended an existing X-Forwarded-For
3696 * header, there's nothing left to do here.
3701 header = strdup("X-Forwarded-For: ");
3702 string_append(&header, csp->ip_addr_str);
3706 return JB_ERR_MEMORY;
3709 log_error(LOG_LEVEL_HEADER, "addh: %s", header);
3710 err = enlist(csp->headers, header);
3717 /*********************************************************************
3719 * Function : server_connection_adder
3721 * Description : Adds an appropriate "Connection:" header to csp->headers
3722 * unless the header was already present. Called from `sed'.
3725 * 1 : csp = Current client state (buffers, headers, etc...)
3727 * Returns : JB_ERR_OK on success, or
3728 * JB_ERR_MEMORY on out-of-memory error.
3730 *********************************************************************/
3731 static jb_err server_connection_adder(struct client_state *csp)
3733 const unsigned int flags = csp->flags;
3734 const char *response_status_line = csp->headers->first->str;
3735 static const char connection_close[] = "Connection: close";
3737 if ((flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
3738 && (flags & CSP_FLAG_SERVER_CONNECTION_HEADER_SET))
3744 * XXX: if we downgraded the response, this check will fail.
3746 if ((csp->config->feature_flags &
3747 RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
3748 && (NULL != response_status_line)
3749 && !strncmpic(response_status_line, "HTTP/1.1", 8)
3750 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3751 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
3755 log_error(LOG_LEVEL_HEADER, "A HTTP/1.1 response "
3756 "without Connection header implies keep-alive.");
3757 csp->flags |= CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE;
3761 log_error(LOG_LEVEL_HEADER, "Adding: %s", connection_close);
3763 return enlist(csp->headers, connection_close);
3767 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3768 /*********************************************************************
3770 * Function : server_proxy_connection_adder
3772 * Description : Adds a "Proxy-Connection: keep-alive" header to
3773 * csp->headers when appropriate.
3776 * 1 : csp = Current client state (buffers, headers, etc...)
3778 * Returns : JB_ERR_OK on success, or
3779 * JB_ERR_MEMORY on out-of-memory error.
3781 *********************************************************************/
3782 static jb_err server_proxy_connection_adder(struct client_state *csp)
3784 static const char proxy_connection_header[] = "Proxy-Connection: keep-alive";
3785 jb_err err = JB_ERR_OK;
3787 if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
3788 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
3789 && !(csp->flags & CSP_FLAG_SERVER_PROXY_CONNECTION_HEADER_SET)
3790 && ((csp->flags & CSP_FLAG_SERVER_CONTENT_LENGTH_SET)
3791 || (csp->flags & CSP_FLAG_CHUNKED)))
3793 log_error(LOG_LEVEL_HEADER, "Adding: %s", proxy_connection_header);
3794 err = enlist(csp->headers, proxy_connection_header);
3799 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
3802 /*********************************************************************
3804 * Function : client_connection_header_adder
3806 * Description : Adds a proper "Connection:" header to csp->headers
3807 * unless the header was already present or it's a
3808 * CONNECT request. Called from `sed'.
3811 * 1 : csp = Current client state (buffers, headers, etc...)
3813 * Returns : JB_ERR_OK on success, or
3814 * JB_ERR_MEMORY on out-of-memory error.
3816 *********************************************************************/
3817 static jb_err client_connection_header_adder(struct client_state *csp)
3819 static const char connection_close[] = "Connection: close";
3821 if (!(csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
3822 && (csp->flags & CSP_FLAG_CLIENT_CONNECTION_HEADER_SET))
3828 * In case of CONNECT requests "Connection: close" is implied,
3829 * but actually setting the header has been reported to cause
3830 * problems with some forwarding proxies that close the
3831 * connection prematurely.
3833 if (csp->http->ssl != 0)
3838 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3839 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
3840 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
3841 && !strcmpic(csp->http->ver, "HTTP/1.1"))
3843 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
3846 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
3848 log_error(LOG_LEVEL_HEADER, "Adding: %s", connection_close);
3850 return enlist(csp->headers, connection_close);
3854 /*********************************************************************
3856 * Function : server_http
3858 * Description : - Save the HTTP Status into csp->http->status
3859 * - Set CT_TABOO to prevent filtering if the answer
3860 * is a partial range (HTTP status 206)
3861 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
3863 * - Normalize the HTTP-version.
3866 * 1 : csp = Current client state (buffers, headers, etc...)
3867 * 2 : header = On input, pointer to header to modify.
3868 * On output, pointer to the modified header, or NULL
3869 * to remove the header. This function frees the
3870 * original string if necessary.
3872 * Returns : JB_ERR_OK on success, or
3873 * JB_ERR_PARSE on fatal parse errors.
3875 *********************************************************************/
3876 static jb_err server_http(struct client_state *csp, char **header)
3878 char *reason_phrase = NULL;
3879 char *new_response_line;
3882 unsigned int major_version;
3883 unsigned int minor_version;
3885 /* Get the reason phrase which start after the second whitespace */
3886 p = strchr(*header, ' ');
3890 reason_phrase = strchr(p, ' ');
3893 if (reason_phrase != NULL)
3899 log_error(LOG_LEVEL_ERROR,
3900 "Response line lacks reason phrase: %s", *header);
3904 if (3 != sscanf(*header, "HTTP/%u.%u %d", &major_version,
3905 &minor_version, &(csp->http->status)))
3907 log_error(LOG_LEVEL_ERROR,
3908 "Failed to parse the response line: %s", *header);
3909 return JB_ERR_PARSE;
3912 if (csp->http->status == 206)
3914 csp->content_type = CT_TABOO;
3917 if (major_version != 1 || (minor_version != 0 && minor_version != 1))
3920 * According to RFC 7230 2.6 intermediaries MUST send
3921 * their own HTTP-version in forwarded messages.
3923 log_error(LOG_LEVEL_ERROR,
3924 "Unsupported HTTP version. Downgrading to 1.1.");
3929 if (((csp->action->flags & ACTION_DOWNGRADE) != 0) && (minor_version == 1))
3931 log_error(LOG_LEVEL_HEADER, "Downgrading answer to HTTP/1.0");
3935 /* Rebuild response line. */
3936 length = sizeof("HTTP/1.1 200 ") + strlen(reason_phrase) + 1;
3937 new_response_line = malloc_or_die(length);
3939 snprintf(new_response_line, length, "HTTP/%u.%u %d %s",
3940 major_version, minor_version, csp->http->status, reason_phrase);
3942 if (0 != strcmp(*header, new_response_line))
3944 log_error(LOG_LEVEL_HEADER, "Response line '%s' changed to '%s'",
3945 *header, new_response_line);
3949 *header = new_response_line;
3954 /*********************************************************************
3956 * Function : add_cooky_expiry_date
3958 * Description : Adds a cookie expiry date to a string.
3961 * 1 : cookie = On input, pointer to cookie to modify.
3962 * On output, pointer to the modified header.
3963 * The original string is freed.
3964 * 2 : lifetime = Seconds the cookie should be valid
3968 *********************************************************************/
3969 static void add_cookie_expiry_date(char **cookie, time_t lifetime)
3972 struct tm *timeptr = NULL;
3973 time_t expiry_date = time(NULL) + lifetime;
3974 #ifdef HAVE_GMTIME_R
3977 timeptr = gmtime_r(&expiry_date, &gmt);
3978 #elif defined(MUTEX_LOCKS_AVAILABLE)
3979 privoxy_mutex_lock(&gmtime_mutex);
3980 timeptr = gmtime(&expiry_date);
3981 privoxy_mutex_unlock(&gmtime_mutex);
3983 timeptr = gmtime(&expiry_date);
3986 if (NULL == timeptr)
3988 log_error(LOG_LEVEL_FATAL,
3989 "Failed to get the time in add_cooky_expiry_date()");
3991 strftime(tmp, sizeof(tmp), "; expires=%a, %d-%b-%Y %H:%M:%S GMT", timeptr);
3992 if (JB_ERR_OK != string_append(cookie, tmp))
3994 log_error(LOG_LEVEL_FATAL, "Out of memory in add_cooky_expiry()");
3999 /*********************************************************************
4001 * Function : server_set_cookie
4003 * Description : Handle the server "cookie" header properly.
4004 * Crunch, accept or rewrite it to a session cookie.
4005 * Called from `sed'.
4008 * 1 : csp = Current client state (buffers, headers, etc...)
4009 * 2 : header = On input, pointer to header to modify.
4010 * On output, pointer to the modified header, or NULL
4011 * to remove the header. This function frees the
4012 * original string if necessary.
4014 * Returns : JB_ERR_OK on success, or
4015 * JB_ERR_MEMORY on out-of-memory error.
4017 *********************************************************************/
4018 static jb_err server_set_cookie(struct client_state *csp, char **header)
4020 if ((csp->action->flags & ACTION_CRUNCH_INCOMING_COOKIES) != 0)
4022 log_error(LOG_LEVEL_HEADER, "Crunching incoming cookie: %s", *header);
4025 else if ((0 != (csp->action->flags & ACTION_SESSION_COOKIES_ONLY))
4026 || (0 != (csp->action->flags & ACTION_LIMIT_COOKIE_LIFETIME)))
4030 long cookie_lifetime = 0;
4033 NO_EXPIRY_DATE_SPECIFIED,
4034 EXPIRY_DATE_ACCEPTABLE,
4035 EXPIRY_DATE_UNACCEPTABLE
4036 } expiry_date_status = NO_EXPIRY_DATE_SPECIFIED;
4038 /* A variable to store the tag we're working on */
4041 /* Skip "Set-Cookie:" (11 characters) in header */
4042 cur_tag = *header + 11;
4044 /* skip whitespace between "Set-Cookie:" and value */
4045 while (*cur_tag && privoxy_isspace(*cur_tag))
4052 if ((csp->action->flags & ACTION_LIMIT_COOKIE_LIFETIME) != 0)
4054 const char *param = csp->action->string[ACTION_STRING_LIMIT_COOKIE_LIFETIME];
4056 cookie_lifetime = strtol(param, NULL, 0);
4057 if (cookie_lifetime < 0)
4059 log_error(LOG_LEVEL_FATAL, "Invalid cookie lifetime limit: %s", param);
4061 cookie_lifetime *= 60;
4064 /* Loop through each tag in the cookie */
4068 char *next_tag = strchr(cur_tag, ';');
4069 if (next_tag != NULL)
4071 /* Skip the ';' character itself */
4074 /* skip whitespace ";" and start of tag */
4075 while (*next_tag && privoxy_isspace(*next_tag))
4082 /* "Next tag" is the end of the string */
4083 next_tag = cur_tag + strlen(cur_tag);
4087 * Check the expiration date to see
4088 * if the cookie is still valid, if yes,
4089 * rewrite it to a session cookie.
4091 if ((strncmpic(cur_tag, "expires=", 8) == 0) && *(cur_tag + 8))
4093 char *expiration_date = cur_tag + 8; /* Skip "[Ee]xpires=" */
4095 if ((expiration_date[0] == '"')
4096 && (expiration_date[1] != '\0'))
4099 * Skip quotation mark. RFC 2109 10.1.2 seems to hint
4100 * that the expiration date isn't supposed to be quoted,
4101 * but some servers do it anyway.
4106 /* Did we detect the date properly? */
4107 if (JB_ERR_OK != parse_header_time(expiration_date, &cookie_time))
4110 * Nope, treat it as if it was still valid.
4112 * XXX: Should we remove the whole cookie instead?
4114 log_error(LOG_LEVEL_ERROR,
4115 "Can't parse \'%s\', send by %s. Unsupported time format?", cur_tag, csp->http->url);
4116 string_move(cur_tag, next_tag);
4117 expiry_date_status = EXPIRY_DATE_UNACCEPTABLE;
4122 * Yes. Check if the cookie is still valid.
4124 * If the cookie is already expired it's probably
4125 * a delete cookie and even if it isn't, the browser
4126 * will discard it anyway.
4130 * XXX: timegm() isn't available on some AmigaOS
4131 * versions and our replacement doesn't work.
4133 * Our options are to either:
4135 * - disable session-cookies-only completely if timegm
4138 * - to simply remove all expired tags, like it has
4139 * been done until Privoxy 3.0.6 and to live with
4140 * the consequence that it can cause login/logout
4141 * problems on servers that don't validate their
4142 * input properly, or
4144 * - to replace it with mktime in which
4145 * case there is a slight chance of valid cookies
4146 * passing as already expired.
4148 * This is the way it's currently done and it's not
4149 * as bad as it sounds. If the missing GMT offset is
4150 * enough to change the result of the expiration check
4151 * the cookie will be only valid for a few hours
4152 * anyway, which in many cases will be shorter
4153 * than a browser session.
4155 if (cookie_time < now)
4157 log_error(LOG_LEVEL_HEADER,
4158 "Cookie \'%s\' is already expired and can pass unmodified.", *header);
4159 /* Just in case some clown sets more then one expiration date */
4161 expiry_date_status = EXPIRY_DATE_ACCEPTABLE;
4163 else if ((cookie_lifetime != 0) && (cookie_time < (now + cookie_lifetime)))
4165 log_error(LOG_LEVEL_HEADER, "Cookie \'%s\' can pass unmodified. "
4166 "Its lifetime is below the limit.", *header);
4167 /* Just in case some clown sets more then one expiration date */
4169 expiry_date_status = EXPIRY_DATE_ACCEPTABLE;
4174 * Still valid, delete expiration date by copying
4175 * the rest of the string over it.
4177 string_move(cur_tag, next_tag);
4179 /* That changed the header, need to issue a log message */
4180 expiry_date_status = EXPIRY_DATE_UNACCEPTABLE;
4183 * Note that the next tag has now been moved to *cur_tag,
4184 * so we do not need to update the cur_tag pointer.
4192 /* Move on to next cookie tag */
4197 if (expiry_date_status != EXPIRY_DATE_ACCEPTABLE)
4199 assert(NULL != *header);
4200 if (cookie_lifetime != 0)
4202 add_cookie_expiry_date(header, cookie_lifetime);
4203 log_error(LOG_LEVEL_HEADER, "Cookie rewritten to: %s", *header);
4205 else if (expiry_date_status != NO_EXPIRY_DATE_SPECIFIED)
4207 log_error(LOG_LEVEL_HEADER,
4208 "Cookie rewritten to a temporary one: %s", *header);
4217 #ifdef FEATURE_FORCE_LOAD
4218 /*********************************************************************
4220 * Function : strclean
4222 * Description : In-Situ-Eliminate all occurrences of substring in
4226 * 1 : string = string to clean
4227 * 2 : substring = substring to eliminate
4229 * Returns : Number of eliminations
4231 *********************************************************************/
4232 int strclean(char *string, const char *substring)
4238 len = strlen(substring);
4240 while((pos = strstr(string, substring)) != NULL)
4247 while (*p++ != '\0');
4254 #endif /* def FEATURE_FORCE_LOAD */
4257 /*********************************************************************
4259 * Function : parse_header_time
4261 * Description : Parses time formats used in HTTP header strings
4262 * to get the numerical respresentation.
4265 * 1 : header_time = HTTP header time as string.
4266 * 2 : result = storage for header_time in seconds
4268 * Returns : JB_ERR_OK if the time format was recognized, or
4269 * JB_ERR_PARSE otherwise.
4271 *********************************************************************/
4272 static jb_err parse_header_time(const char *header_time, time_t *result)
4276 * Checking for two-digit years first in an
4277 * attempt to work around GNU libc's strptime()
4278 * reporting negative year values when using %Y.
4280 static const char time_formats[][22] = {
4281 /* Tue, 02-Jun-37 20:00:00 */
4282 "%a, %d-%b-%y %H:%M:%S",
4283 /* Tue, 02 Jun 2037 20:00:00 */
4284 "%a, %d %b %Y %H:%M:%S",
4285 /* Tue, 02-Jun-2037 20:00:00 */
4286 "%a, %d-%b-%Y %H:%M:%S",
4287 /* Tuesday, 02-Jun-2037 20:00:00 */
4288 "%A, %d-%b-%Y %H:%M:%S",
4289 /* Tuesday Jun 02 20:00:00 2037 */
4290 "%A %b %d %H:%M:%S %Y"
4294 for (i = 0; i < SZ(time_formats); i++)
4297 * Zero out gmt to prevent time zone offsets.
4298 * Documented to be required for GNU libc.
4300 memset(&gmt, 0, sizeof(gmt));
4302 if (NULL != strptime(header_time, time_formats[i], &gmt))
4304 /* Sanity check for GNU libc. */
4305 if (gmt.tm_year < 0)
4307 log_error(LOG_LEVEL_HEADER,
4308 "Failed to parse '%s' using '%s'. Moving on.",
4309 header_time, time_formats[i]);
4312 *result = timegm(&gmt);
4314 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
4316 * Verify that parsing the date recreated from the first
4317 * parse operation gets the previous result. If it doesn't,
4318 * either strptime() or strftime() are malfunctioning.
4320 * We could string-compare the recreated date with the original
4321 * header date, but this leads to false positives as strptime()
4322 * may let %a accept all day formats while strftime() will only
4326 char recreated_date[100];
4330 tm = gmtime(result);
4331 if (!strftime(recreated_date, sizeof(recreated_date),
4332 time_formats[i], tm))
4334 log_error(LOG_LEVEL_ERROR, "Failed to recreate date '%s' with '%s'.",
4335 header_time, time_formats[i]);
4338 memset(&gmt, 0, sizeof(gmt));
4339 if (NULL == strptime(recreated_date, time_formats[i], &gmt))
4341 log_error(LOG_LEVEL_ERROR,
4342 "Failed to parse '%s' generated with '%s' to recreate '%s'.",
4343 recreated_date, time_formats[i], header_time);
4346 result2 = timegm(&gmt);
4347 if (*result != result2)
4349 log_error(LOG_LEVEL_ERROR, "strftime() and strptime() disagree. "
4350 "Format: '%s'. In: '%s', out: '%s'. %d != %d. Rejecting.",
4351 time_formats[i], header_time, recreated_date, *result, result2);
4361 return JB_ERR_PARSE;
4365 /*********************************************************************
4367 * Function : parse_time_header
4369 * Description : Parses the time in an HTTP time header to get
4370 * the numerical respresentation.
4373 * 1 : header = HTTP header with a time value
4374 * 2 : result = storage for header_time in seconds
4376 * Returns : JB_ERR_OK if the time format was recognized, or
4377 * JB_ERR_PARSE otherwise.
4379 *********************************************************************/
4380 static jb_err parse_time_header(const char *header, time_t *result)
4382 const char *header_time;
4384 header_time = strchr(header, ':');
4387 * Currently this can't happen as all callers are called
4388 * through sed() which requires a header name followed by
4391 assert(header_time != NULL);
4394 if (*header_time == ' ')
4399 return parse_header_time(header_time, result);
4404 /*********************************************************************
4406 * Function : get_destination_from_headers
4408 * Description : Parse the "Host:" header to get the request's destination.
4409 * Only needed if the client's request was forcefully
4410 * redirected into Privoxy.
4412 * Code mainly copied from client_host() which is currently
4413 * run too late for this purpose.
4416 * 1 : headers = List of headers (one of them hopefully being
4417 * the "Host:" header)
4418 * 2 : http = storage for the result (host, port and hostport).
4420 * Returns : JB_ERR_MEMORY (or terminates) in case of memory problems,
4421 * JB_ERR_PARSE if the host header couldn't be found,
4422 * JB_ERR_OK otherwise.
4424 *********************************************************************/
4425 jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
4433 host = get_header_value(headers, "Host:");
4437 log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
4438 return JB_ERR_PARSE;
4441 p = strdup_or_die(host);
4443 q = strdup_or_die(p);
4445 freez(http->hostport);
4449 q = strchr(http->host, ':');
4452 /* Terminate hostname and evaluate port string */
4454 http->port = atoi(q);
4461 /* Rebuild request URL */
4463 http->url = strdup("http://");
4464 string_append(&http->url, http->hostport);
4465 string_append(&http->url, http->path);
4466 if (http->url == NULL)
4468 return JB_ERR_MEMORY;
4471 log_error(LOG_LEVEL_HEADER,
4472 "Destination extracted from \"Host\" header. New request URL: %s",
4476 * Regenerate request line in "proxy format"
4477 * to make rewrites more convenient.
4479 assert(http->cmd != NULL);
4481 http->cmd = strdup_or_die(http->gpc);
4482 string_append(&http->cmd, " ");
4483 string_append(&http->cmd, http->url);
4484 string_append(&http->cmd, " ");
4485 string_append(&http->cmd, http->ver);
4486 if (http->cmd == NULL)
4488 return JB_ERR_MEMORY;
4496 /*********************************************************************
4498 * Function : create_forged_referrer
4500 * Description : Helper for client_referrer to forge a referer as
4501 * 'http://hostname[:port]/' to fool stupid
4502 * checks for in-site links
4505 * 1 : header = Pointer to header pointer
4506 * 2 : hostport = Host and optionally port as string
4508 * Returns : JB_ERR_OK in case of success, or
4509 * JB_ERR_MEMORY in case of memory problems.
4511 *********************************************************************/
4512 static jb_err create_forged_referrer(char **header, const char *hostport)
4514 assert(NULL == *header);
4516 *header = strdup("Referer: http://");
4517 string_append(header, hostport);
4518 string_append(header, "/");
4520 if (NULL == *header)
4522 return JB_ERR_MEMORY;
4525 log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
4532 /*********************************************************************
4534 * Function : create_fake_referrer
4536 * Description : Helper for client_referrer to create a fake referrer
4537 * based on a string supplied by the user.
4540 * 1 : header = Pointer to header pointer
4541 * 2 : hosthost = Referrer to fake
4543 * Returns : JB_ERR_OK in case of success, or
4544 * JB_ERR_MEMORY in case of memory problems.
4546 *********************************************************************/
4547 static jb_err create_fake_referrer(char **header, const char *fake_referrer)
4549 assert(NULL == *header);
4551 if ((0 != strncmpic(fake_referrer, "http://", 7)) && (0 != strncmpic(fake_referrer, "https://", 8)))
4553 log_error(LOG_LEVEL_HEADER,
4554 "Parameter: +hide-referrer{%s} is a bad idea, but I don't care.", fake_referrer);
4556 *header = strdup("Referer: ");
4557 string_append(header, fake_referrer);
4559 if (NULL == *header)
4561 return JB_ERR_MEMORY;
4564 log_error(LOG_LEVEL_HEADER, "Referer replaced with: %s", *header);
4571 /*********************************************************************
4573 * Function : handle_conditional_hide_referrer_parameter
4575 * Description : Helper for client_referrer to crunch or forge
4576 * the referrer header if the host has changed.
4579 * 1 : header = Pointer to header pointer
4580 * 2 : host = The target host (may include the port)
4581 * 3 : parameter_conditional_block = Boolean to signal
4582 * if we're in conditional-block mode. If not set,
4583 * we're in conditional-forge mode.
4585 * Returns : JB_ERR_OK in case of success, or
4586 * JB_ERR_MEMORY in case of memory problems.
4588 *********************************************************************/
4589 static jb_err handle_conditional_hide_referrer_parameter(char **header,
4590 const char *host, const int parameter_conditional_block)
4592 char *referer = strdup_or_die(*header);
4593 const size_t hostlength = strlen(host);
4594 const char *referer_url = NULL;
4596 /* referer begins with 'Referer: http[s]://' */
4597 if ((hostlength+17) < strlen(referer))
4600 * Shorten referer to make sure the referer is blocked
4601 * if www.example.org/www.example.com-shall-see-the-referer/
4602 * links to www.example.com/
4604 referer[hostlength+17] = '\0';
4606 referer_url = strstr(referer, "http://");
4607 if ((NULL == referer_url) || (NULL == strstr(referer_url, host)))
4609 /* Host has changed, Referer is invalid or a https URL. */
4610 if (parameter_conditional_block)
4612 log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
4619 return create_forged_referrer(header, host);
4629 /*********************************************************************
4631 * Function : create_content_length_header
4633 * Description : Creates a Content-Length header.
4636 * 1 : content_length = The content length to be used in the header.
4637 * 2 : header = Allocated space to safe the header.
4638 * 3 : buffer_length = The length of the allocated space.
4642 *********************************************************************/
4643 static void create_content_length_header(unsigned long long content_length,
4644 char *header, size_t buffer_length)
4647 #if SIZEOF_LONG_LONG < 8
4648 #error sizeof(unsigned long long) too small
4650 snprintf(header, buffer_length, "Content-Length: %I64u", content_length);
4652 snprintf(header, buffer_length, "Content-Length: %llu", content_length);
4657 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
4658 /*********************************************************************
4660 * Function : get_expected_content_length
4662 * Description : Figures out the content length from a list of headers.
4665 * 1 : headers = List of headers
4667 * Returns : Number of bytes to expect
4669 *********************************************************************/
4670 unsigned long long get_expected_content_length(struct list *headers)
4672 const char *content_length_header;
4673 unsigned long long content_length = 0;
4675 content_length_header = get_header_value(headers, "Content-Length:");
4676 if (content_length_header != NULL)
4678 if (JB_ERR_OK != get_content_length(content_length_header, &content_length))
4680 log_error(LOG_LEVEL_ERROR,
4681 "Failed to get the Content-Length in %s", content_length_header);
4682 /* XXX: The header will be removed later on */
4687 return content_length;