X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=filters.c;h=7b586bc0881f4e6254adc3c424c2709d45c49875;hp=c891f4f9f793218411beb3b0a57df788463afada;hb=36bed44da8971c4b75627ec86cc41163bfde81ae;hpb=bae8236ce82cda59cc60789a35aea5689eb2f488 diff --git a/filters.c b/filters.c index c891f4f9..7b586bc0 100644 --- a/filters.c +++ b/filters.c @@ -4,7 +4,7 @@ * * Purpose : Declares functions to parse/crunch headers and pages. * - * Copyright : Written by and Copyright (C) 2001-2016 the + * Copyright : Written by and Copyright (C) 2001-2020 the * Privoxy team. https://www.privoxy.org/ * * Based on the Internet Junkbuster originally written @@ -42,18 +42,12 @@ #include #ifndef _WIN32 -#ifndef __OS2__ #include -#endif /* ndef __OS2__ */ #include #else #include #endif /* ndef _WIN32 */ -#ifdef __OS2__ -#include -#endif /* def __OS2__ */ - #include "project.h" #include "filters.h" #include "encode.h" @@ -87,6 +81,10 @@ static void apply_url_actions(struct current_action_spec *action, #endif struct url_actions *b); +#ifdef FEATURE_EXTENDED_STATISTICS +static void increment_block_reason_counter(const char *block_reason); +#endif + #ifdef FEATURE_ACL #ifdef HAVE_RFC2553 /********************************************************************* @@ -557,6 +555,13 @@ struct http_response *block_url(struct client_state *csp) return cgi_error_memory(); } +#ifdef FEATURE_EXTENDED_STATISTICS + if (csp->action->string[ACTION_STRING_BLOCK] != NULL) + { + increment_block_reason_counter(csp->action->string[ACTION_STRING_BLOCK]); + } +#endif + /* * If it's an image-url, send back an image or redirect * as specified by the relevant +image action @@ -1039,7 +1044,7 @@ char *rewrite_url(char *old_url, const char *pcrs_command) * the last URL found. * *********************************************************************/ -char *get_last_url(char *subject, const char *redirect_mode) +static char *get_last_url(char *subject, const char *redirect_mode) { char *new_url = NULL; char *tmp; @@ -1652,9 +1657,11 @@ static char *pcrs_filter_response(struct client_state *csp) if (b->dynamic) pcrs_free_joblist(joblist); log_error(LOG_LEVEL_RE_FILTER, - "filtering %s%s (size %d) with \'%s\' produced %d hits (new size %d).", + "filtering %s%s (size %lu) with \'%s\' produced %d hits (new size %lu).", csp->http->hostport, csp->http->path, prev_size, b->name, current_hits, size); - +#ifdef FEATURE_EXTENDED_STATISTICS + update_filter_statistics(b->name, current_hits); +#endif hits += current_hits; } @@ -1664,6 +1671,10 @@ static char *pcrs_filter_response(struct client_state *csp) */ if (!hits) { + if (old != csp->iob->cur && old != new) + { + freez(old); + } freez(new); return(NULL); } @@ -1816,7 +1827,7 @@ static char *execute_external_filter(const struct client_state *csp, */ if ((*size != 0) && fwrite(content, *size, 1, fp) != 1) { - log_error(LOG_LEVEL_ERROR, "fwrite(..., %d, 1, ..) failed: %E", *size); + log_error(LOG_LEVEL_ERROR, "fwrite(..., %lu, 1, ..) failed: %E", *size); unlink(file_name); fclose(fp); return NULL; @@ -1892,7 +1903,7 @@ static char *execute_external_filter(const struct client_state *csp, { log_error(LOG_LEVEL_RE_FILTER, "Executing '%s' resulted in return value %d. " - "Read %d of up to %d bytes.", name, (ret >> 8), new_size, *size); + "Read %lu of up to %lu bytes.", name, (ret >> 8), new_size, *size); } unlink(file_name); @@ -1952,7 +1963,8 @@ static char *gif_deanimate_response(struct client_state *csp) } else { - log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset); + log_error(LOG_LEVEL_DEANIMATE, + "Success! GIF shrunk from %lu bytes to %lu.", size, out->offset); } csp->content_length = out->offset; csp->flags |= CSP_FLAG_MODIFIED; @@ -2058,8 +2070,8 @@ static jb_err remove_chunked_transfer_coding(char *buffer, size_t *size) { log_error(LOG_LEVEL_ERROR, "Chunk size %u exceeds buffered data left. " - "Already digested %u of %u buffered bytes.", - chunksize, (unsigned int)newsize, (unsigned int)*size); + "Already digested %lu of %lu buffered bytes.", + chunksize, newsize, *size); return JB_ERR_PARSE; } @@ -2113,7 +2125,8 @@ static jb_err remove_chunked_transfer_coding(char *buffer, size_t *size) } /* XXX: Should get its own loglevel. */ - log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d", *size, newsize); + log_error(LOG_LEVEL_RE_FILTER, + "De-chunking successful. Shrunk from %lu to %lu", *size, newsize); *size = newsize; @@ -2166,7 +2179,11 @@ static jb_err prepare_for_filtering(struct client_state *csp) * If the body has a supported transfer-encoding, * decompress it, adjusting size and iob->eod. */ - if (csp->content_type & (CT_GZIP|CT_DEFLATE)) + if ((csp->content_type & (CT_GZIP|CT_DEFLATE)) +#ifdef FEATURE_BROTLI + || (csp->content_type & CT_BROTLI) +#endif + ) { if (0 == csp->iob->eod - csp->iob->cur) { @@ -2184,11 +2201,14 @@ static jb_err prepare_for_filtering(struct client_state *csp) else { /* - * Unset CT_GZIP and CT_DEFLATE to remember not - * to modify the Content-Encoding header later. + * Unset content types to remember not to + * modify the Content-Encoding header later. */ csp->content_type &= ~CT_GZIP; csp->content_type &= ~CT_DEFLATE; +#ifdef FEATURE_BROTLI + csp->content_type &= ~CT_BROTLI; +#endif } } #endif @@ -2300,20 +2320,7 @@ void get_url_actions(struct client_state *csp, struct http_request *http) struct url_actions *b; int i; -#ifdef FEATURE_HTTPS_INSPECTION - if (!csp->http->client_ssl) -#endif - { - /* - * When filtering TLS traffic this function gets called a - * second time after the encrypted headers have been received. - * - * Only initialize the first time. The second time we apply - * the newly set actions on top of the ones that were set - * the first time. - */ - init_current_action(csp->action); - } + init_current_action(csp->action); for (i = 0; i < MAX_AF_FILES; i++) { @@ -2743,6 +2750,283 @@ int filters_available(const struct client_state *csp) return FALSE; } +#ifdef FEATURE_EXTENDED_STATISTICS + +struct filter_statistics_entry +{ + char *filter; + unsigned long long executions; + unsigned long long response_bodies_modified; + unsigned long long hits; + + struct filter_statistics_entry *next; +}; + +static struct filter_statistics_entry *filter_statistics = NULL; + + +/********************************************************************* + * + * Function : register_filter_for_statistics + * + * Description : Registers a filter so we can gather statistics for + * it unless the filter has already been registered + * before. + * + * Parameters : + * 1 : filter = Name of the filter to register + * + * Returns : void + * + *********************************************************************/ +void register_filter_for_statistics(const char *filter) +{ + struct filter_statistics_entry *entry; + + privoxy_mutex_lock(&filter_statistics_mutex); + + if (filter_statistics == NULL) + { + filter_statistics = zalloc_or_die(sizeof(struct filter_statistics_entry)); + entry = filter_statistics; + entry->filter = strdup_or_die(filter); + privoxy_mutex_unlock(&filter_statistics_mutex); + return; + } + entry = filter_statistics; + while (entry != NULL) + { + if (!strcmp(entry->filter, filter)) + { + /* Already registered, nothing to do. */ + break; + } + if (entry->next == NULL) + { + entry->next = zalloc_or_die(sizeof(struct filter_statistics_entry)); + entry->next->filter = strdup_or_die(filter); + break; + } + entry = entry->next; + } + + privoxy_mutex_unlock(&filter_statistics_mutex); + +} + + +/********************************************************************* + * + * Function : update_filter_statistics + * + * Description : Updates the statistics for a filter. + * + * Parameters : + * 1 : filter = Name of the filter to update + * 2 : hits = Hit count. + * + * Returns : void + * + *********************************************************************/ +void update_filter_statistics(const char *filter, int hits) +{ + struct filter_statistics_entry *entry; + + privoxy_mutex_lock(&filter_statistics_mutex); + + entry = filter_statistics; + while (entry != NULL) + { + if (!strcmp(entry->filter, filter)) + { + entry->executions++; + if (hits != 0) + { + entry->response_bodies_modified++; + entry->hits += (unsigned)hits; + } + break; + } + entry = entry->next; + } + + privoxy_mutex_unlock(&filter_statistics_mutex); + +} + + +/********************************************************************* + * + * Function : get_filter_statistics + * + * Description : Gets the statistics for a filter. + * + * Parameters : + * 1 : filter = Name of the filter to get statistics for. + * 2 : executions = Storage for the execution count. + * 3 : response_bodies_modified = Storage for the number + * of modified response bodies. + * 4 : hits = Storage for the number of hits. + * + * Returns : void + * + *********************************************************************/ +void get_filter_statistics(const char *filter, unsigned long long *executions, + unsigned long long *response_bodies_modified, + unsigned long long *hits) +{ + struct filter_statistics_entry *entry; + + privoxy_mutex_lock(&filter_statistics_mutex); + + entry = filter_statistics; + while (entry != NULL) + { + if (!strcmp(entry->filter, filter)) + { + *executions = entry->executions; + *response_bodies_modified = entry->response_bodies_modified; + *hits = entry->hits; + break; + } + entry = entry->next; + } + + privoxy_mutex_unlock(&filter_statistics_mutex); + +} + + +struct block_statistics_entry +{ + char *block_reason; + unsigned long long count; + + struct block_statistics_entry *next; +}; + +static struct block_statistics_entry *block_statistics = NULL; + +/********************************************************************* + * + * Function : register_block_reason_for_statistics + * + * Description : Registers a block reason so we can gather statistics + * for it unless the block reason has already been + * registered before. + * + * Parameters : + * 1 : block_reason = Block reason to register + * + * Returns : void + * + *********************************************************************/ +void register_block_reason_for_statistics(const char *block_reason) +{ + struct block_statistics_entry *entry; + + privoxy_mutex_lock(&block_statistics_mutex); + + if (block_statistics == NULL) + { + block_statistics = zalloc_or_die(sizeof(struct block_statistics_entry)); + entry = block_statistics; + entry->block_reason = strdup_or_die(block_reason); + privoxy_mutex_unlock(&block_statistics_mutex); + return; + } + entry = block_statistics; + while (entry != NULL) + { + if (!strcmp(entry->block_reason, block_reason)) + { + /* Already registered, nothing to do. */ + break; + } + if (entry->next == NULL) + { + entry->next = zalloc_or_die(sizeof(struct block_statistics_entry)); + entry->next->block_reason = strdup_or_die(block_reason); + break; + } + entry = entry->next; + } + + privoxy_mutex_unlock(&block_statistics_mutex); + +} + + +/********************************************************************* + * + * Function : increment_block_reason_counter + * + * Description : Updates the counter for a block reason. + * + * Parameters : + * 1 : block_reason = Block reason to count + * + * Returns : void + * + *********************************************************************/ +static void increment_block_reason_counter(const char *block_reason) +{ + struct block_statistics_entry *entry; + + privoxy_mutex_lock(&block_statistics_mutex); + + entry = block_statistics; + while (entry != NULL) + { + if (!strcmp(entry->block_reason, block_reason)) + { + entry->count++; + break; + } + entry = entry->next; + } + + privoxy_mutex_unlock(&block_statistics_mutex); + +} + + +/********************************************************************* + * + * Function : get_block_reason_count + * + * Description : Gets number of times a block reason was used. + * + * Parameters : + * 1 : block_reason = Block reason to get statistics for. + * 2 : count = Storage for the number of times the block + * reason was used. + * + * Returns : void + * + *********************************************************************/ +void get_block_reason_count(const char *block_reason, unsigned long long *count) +{ + struct block_statistics_entry *entry; + + privoxy_mutex_lock(&block_statistics_mutex); + + entry = block_statistics; + while (entry != NULL) + { + if (!strcmp(entry->block_reason, block_reason)) + { + *count = entry->count; + break; + } + entry = entry->next; + } + + privoxy_mutex_unlock(&block_statistics_mutex); + +} + +#endif /* def FEATURE_EXTENDED_STATISTICS */ /* Local Variables: