X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=filters.c;h=256b0c9dedda4fed5a1ef8a514a51d233d4c773d;hp=065a2f9b90726bfa1466e9845e7d1a40d15e9352;hb=52bd067225c1e12ecf7156e5707161ed7df8e8c4;hpb=3fae4c0f831bf0d681a20ab55e1d082d24cf5522 diff --git a/filters.c b/filters.c index 065a2f9b..256b0c9d 100644 --- a/filters.c +++ b/filters.c @@ -1,4 +1,4 @@ -const char filters_rcs[] = "$Id: filters.c,v 1.41 2001/11/13 00:14:07 jongfoster Exp $"; +const char filters_rcs[] = "$Id: filters.c,v 1.45 2002/03/08 16:47:50 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/filters.c,v $ @@ -38,6 +38,34 @@ const char filters_rcs[] = "$Id: filters.c,v 1.41 2001/11/13 00:14:07 jongfoster * * Revisions : * $Log: filters.c,v $ + * Revision 1.45 2002/03/08 16:47:50 oes + * Added choice beween GIF and PNG built-in images + * + * Revision 1.44 2002/03/07 03:49:31 oes + * - Fixed compiler warnings etc + * - Changed built-in images from GIF to PNG + * (with regard to Unisys patent issue) + * - Added a 4x4 pattern PNG which is less intrusive + * than the logo but also clearly marks the deleted banners + * + * Revision 1.43 2002/01/22 23:51:59 jongfoster + * Replacing strsav() with the safer string_append(). + * + * Adding missing html_encode() to error message generators. Where encoded + * and unencoded versions of a string were provided, removing the unencoded + * one. + * + * Revision 1.42 2002/01/17 21:00:32 jongfoster + * Moving all our URL and URL pattern parsing code to urlmatch.c. + * + * Using a single, simple url_match(pattern,url) function - rather than + * the 3-line match routine which was repeated all over the place. + * + * Renaming free_url to free_url_spec, since it frees a struct url_spec. + * + * Using parse_http_url() to parse URLs without faking a HTTP + * request line for parse_http_request(). + * * Revision 1.41 2001/11/13 00:14:07 jongfoster * Fixing stupid bug now I've figured out what || means. * (It always returns 0 or 1, not one of it's paramaters.) @@ -445,7 +473,7 @@ int block_acl(struct access_control_addr *dst, struct client_state *csp) * * Function : acl_addr * - * Description : Called from `load_aclfile' to parse an ACL address. + * Description : Called from `load_config' to parse an ACL address. * * Parameters : * 1 : aspec = String specifying ACL address. @@ -493,9 +521,8 @@ int acl_addr(char *aspec, struct access_control_addr *aca) aca->addr = ntohl(resolve_hostname_to_ip(aspec)); - if (aca->addr == -1) + if (aca->addr == INADDR_NONE) { - log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec); return(-1); } @@ -646,15 +673,15 @@ struct http_response *block_url(struct client_state *csp) /* and handle accordingly: */ if ((p == NULL) || (0 == strcmpic(p, "logo"))) { - rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length); + rsp->body = bindup(image_logo_data, image_logo_length); if (rsp->body == NULL) { free_http_response(rsp); return cgi_error_memory(); } - rsp->content_length = image_junkbuster_gif_length; + rsp->content_length = image_logo_length; - if (enlist_unique_header(rsp->headers, "Content-Type", "image/gif")) + if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE)) { free_http_response(rsp); return cgi_error_memory(); @@ -663,15 +690,32 @@ struct http_response *block_url(struct client_state *csp) else if (0 == strcmpic(p, "blank")) { - rsp->body = bindup(image_blank_gif_data, image_blank_gif_length); + rsp->body = bindup(image_blank_data, image_blank_length); + if (rsp->body == NULL) + { + free_http_response(rsp); + return cgi_error_memory(); + } + rsp->content_length = image_blank_length; + + if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE)) + { + free_http_response(rsp); + return cgi_error_memory(); + } + } + + else if (0 == strcmpic(p, "pattern")) + { + rsp->body = bindup(image_pattern_data, image_pattern_length); if (rsp->body == NULL) { free_http_response(rsp); return cgi_error_memory(); } - rsp->content_length = image_blank_gif_length; + rsp->content_length = image_pattern_length; - if (enlist_unique_header(rsp->headers, "Content-Type", "image/gif")) + if (enlist_unique_header(rsp->headers, "Content-Type", BUILTIN_IMAGE_MIMETYPE)) { free_http_response(rsp); return cgi_error_memory(); @@ -817,18 +861,14 @@ struct http_response *trust_url(struct client_state *csp) */ err = map(exports, "hostport", 1, csp->http->hostport, 1); if (!err) err = map(exports, "path", 1, csp->http->path, 1); - if (!err) err = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0); - if (!err) err = map(exports, "path-html", 1, html_encode(csp->http->path), 0); if (NULL != (p = get_header_value(csp->headers, "Referer:"))) { - if (!err) err = map(exports, "referrer", 1, p, 1); - if (!err) err = map(exports, "referrer-html", 1, html_encode(p), 0); + if (!err) err = map(exports, "referrer", 1, html_encode(p), 0); } else { if (!err) err = map(exports, "referrer", 1, "unknown", 1); - if (!err) err = map(exports, "referrer-html", 1, "unknown", 1); } if (err) @@ -1044,7 +1084,7 @@ int is_untrusted_url(struct client_state *csp) struct block_spec *b; struct url_spec **trusted_url; struct http_request rhttp[1]; - char *p, *h; + const char * referer; jb_err err; /* @@ -1068,7 +1108,7 @@ int is_untrusted_url(struct client_state *csp) } } - if (NULL == (h = get_header_value(csp->headers, "Referer:"))) + if (NULL == (referer = get_header_value(csp->headers, "Referer:"))) { /* no referrer was supplied */ return 1; @@ -1083,7 +1123,7 @@ int is_untrusted_url(struct client_state *csp) * Parse the URL from the referrer */ - err = parse_http_url(h, rhttp, csp); + err = parse_http_url(referer, rhttp, csp); if (err) { return 1; @@ -1102,28 +1142,41 @@ int is_untrusted_url(struct client_state *csp) if ((fp = fopen(csp->config->trustfile, "a"))) { - h = NULL; + char * path; + char * path_end; + char * new_entry = strdup("~"); - h = strsav(h, "~"); - h = strsav(h, csp->http->hostport); + string_append(&new_entry, csp->http->hostport); - p = csp->http->path; - if ((*p++ == '/') - && (*p++ == '~')) + path = csp->http->path; + if ( (path[0] == '/') + && (path[1] == '~') + && ((path_end = strchr(path + 2, '/')) != NULL)) { /* since this path points into a user's home space * be sure to include this spec in the trustfile. */ - if ((p = strchr(p, '/'))) + int path_len = path_end - path; /* save offset */ + path = strdup(path); /* Copy string */ + if (path != NULL) { - *p = '\0'; /* FIXME: writing to csp->http->path is a bad idea */ - h = strsav(h, csp->http->path); - h = strsav(h, "/"); + path_end = path + path_len; /* regenerate ptr to new buffer */ + *(path_end + 1) = '\0'; /* Truncate path after '/' */ } + string_join(&new_entry, path); + } + + if (new_entry != NULL) + { + fprintf(fp, "%s\n", new_entry); + free(new_entry); + } + else + { + /* FIXME: No way to handle out-of memory, so mostly ignoring it */ + log_error(LOG_LEVEL_ERROR, "Out of memory adding pattern to trust file"); } - fprintf(fp, "%s\n", h); - freez(h); fclose(fp); } return 0; @@ -1138,16 +1191,17 @@ int is_untrusted_url(struct client_state *csp) * * Function : pcrs_filter_response * - * Description : Apply all the pcrs jobs from the joblist (re_filterfile) - * to the text buffer that's been accumulated in - * csp->iob->buf and set csp->content_length to the modified - * size and raise the CSP_FLAG_MODIFIED flag if appropriate. + * Description : Ecexute all text substitutions from all applying + * +filter actions on the text buffer that's been accumulated + * in csp->iob->buf. If this changes the contents, set + * csp->content_length to the modified size and raise the + * CSP_FLAG_MODIFIED flag. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * * Returns : a pointer to the (newly allocated) modified buffer. - * or NULL in case something went wrong + * or NULL if there were no hits or something went wrong * *********************************************************************/ char *pcrs_filter_response(struct client_state *csp) @@ -1160,14 +1214,23 @@ char *pcrs_filter_response(struct client_state *csp) struct file_list *fl; struct re_filterfile_spec *b; + struct list_entry *filtername; - /* Sanity first */ + /* + * Sanity first + */ if (csp->iob->cur >= csp->iob->eod) { return(NULL); } size = csp->iob->eod - csp->iob->cur; + if ( ( NULL == (fl = csp->rlist) ) || ( NULL == fl->f) ) + { + log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering."); + return(NULL); + } + /* * If the body has a "chunked" transfer-encoding, * get rid of it first, adjusting size and iob->eod @@ -1183,31 +1246,40 @@ char *pcrs_filter_response(struct client_state *csp) csp->flags |= CSP_FLAG_MODIFIED; } - if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) ) + /* + * For all applying +filter actions, look if a filter by that + * name exists and if yes, execute it's pcrs_joblist on the + * buffer. + */ + for (b = fl->f; b; b = b->next) { - log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering."); - return(NULL); - } + for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first; + filtername ; filtername = filtername->next) + { + if (strcmp(b->filtername, filtername->str) == 0) + { + if ( NULL == b->joblist ) + { + log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->filtername); + return(NULL); + } - if ( NULL == b->joblist ) - { - log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do."); - return(NULL); - } + log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) with filter %s...", + csp->http->hostport, csp->http->path, size, b->filtername); - log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...", - csp->http->hostport, csp->http->path, size); + /* Apply all jobs from the joblist */ + for (job = b->joblist; NULL != job; job = job->next) + { + hits += pcrs_execute(job, old, size, &new, &size); + if (old != csp->iob->cur) free(old); + old=new; + } - /* Apply all jobs from the joblist */ - for (job = b->joblist; NULL != job; job = job->next) - { - hits += pcrs_execute(job, old, size, &new, &size); - if (old != csp->iob->cur) free(old); - old=new; + log_error(LOG_LEVEL_RE_FILTER, " ...produced %d hits (new size %d).", hits, size); + } + } } - log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size); - /* * If there were no hits, destroy our copy and let * chat() use the original in csp->iob @@ -1246,7 +1318,7 @@ char *gif_deanimate_response(struct client_state *csp) { struct binbuffer *in, *out; char *p; - int size = csp->iob->eod - csp->iob->cur; + size_t size = csp->iob->eod - csp->iob->cur; /* * If the body has a "chunked" transfer-encoding,