From 1fda133fc65a0233b02d614adbfe7ad03bcc20a0 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Sun, 6 Apr 2008 14:54:26 +0000 Subject: [PATCH 1/1] Use PCRE syntax in host patterns when configured with --enable-pcre-host-patterns. --- acconfig.h | 8 ++++++ configure.in | 13 +++++++-- project.h | 13 ++++++++- urlmatch.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/acconfig.h b/acconfig.h index 2274f848..81eb72ab 100644 --- a/acconfig.h +++ b/acconfig.h @@ -37,6 +37,9 @@ * * Revisions : * $Log: acconfig.h,v $ + * Revision 1.33 2006/12/17 19:15:26 fabiankeil + * Added ./configure switch for FEATURE_GRACEFUL_TERMINATION. + * * Revision 1.32 2006/07/18 14:48:45 david__schmidt * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch) * with what was really the latest development (the v_3_0_branch branch) @@ -349,6 +352,11 @@ */ #undef FEATURE_GRACEFUL_TERMINATION +/* + * Allow PCRE syntax in host patterns. + */ +#undef FEATURE_PCRE_HOST_PATTERNS + /* * Use POSIX threads instead of native threads. */ diff --git a/configure.in b/configure.in index dbd2ec20..8a0e5269 100644 --- a/configure.in +++ b/configure.in @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. dnl -dnl $Id: configure.in,v 1.111 2008/02/03 14:40:47 fabiankeil Exp $ +dnl $Id: configure.in,v 1.112 2008/03/27 18:27:22 fabiankeil Exp $ dnl dnl Written by and Copyright (C) 2001 - 2007 the SourceForge dnl Privoxy team. http://www.privoxy.org/ @@ -28,6 +28,9 @@ dnl or write to the Free Software Foundation, Inc., 59 dnl Temple Place - Suite 330, Boston, MA 02111-1307, USA. dnl dnl $Log: configure.in,v $ +dnl Revision 1.112 2008/03/27 18:27:22 fabiankeil +dnl Remove kill-popups action. +dnl dnl Revision 1.111 2008/02/03 14:40:47 fabiankeil dnl Remove unused OSX_DARWIN macro. Reported by Mark Miller in #1852529. dnl @@ -514,7 +517,7 @@ dnl ================================================================= dnl AutoConf Initialization dnl ================================================================= -AC_REVISION($Revision: 1.111 $) +AC_REVISION($Revision: 1.112 $) AC_INIT(jcc.c) if test ! -f config.h.in; then @@ -1313,6 +1316,12 @@ AC_ARG_ENABLE(graceful-termination, AC_DEFINE(FEATURE_GRACEFUL_TERMINATION) fi]) +AC_ARG_ENABLE(pcre-host-patterns, +[ --enable-pcre-host-patterns Allow PCRE syntax in host patterns.], +[if test $enableval = yes; then + AC_DEFINE(FEATURE_PCRE_HOST_PATTERNS) +fi]) + dnl pcre/pcrs is needed for CGI anyway, so dnl the choice is only between static and dnl dynamic: diff --git a/project.h b/project.h index 21c71d3a..ae3836b0 100644 --- a/project.h +++ b/project.h @@ -1,7 +1,7 @@ #ifndef PROJECT_H_INCLUDED #define PROJECT_H_INCLUDED /** Version string. */ -#define PROJECT_H_VERSION "$Id: project.h,v 1.109 2008/03/28 15:13:41 fabiankeil Exp $" +#define PROJECT_H_VERSION "$Id: project.h,v 1.110 2008/03/29 12:13:46 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/project.h,v $ @@ -37,6 +37,9 @@ * * Revisions : * $Log: project.h,v $ + * Revision 1.110 2008/03/29 12:13:46 fabiankeil + * Remove send-wafer and send-vanilla-wafer actions. + * * Revision 1.109 2008/03/28 15:13:41 fabiankeil * Remove inspect-jpegs action. * @@ -930,10 +933,14 @@ struct url_spec Used for debugging or display only. */ char *spec; +#ifdef FEATURE_PCRE_HOST_PATTERNS + regex_t *host_regex;/**< Regex for host matching */ +#else char *dbuffer; /**< Buffer with '\0'-delimited domain name, or NULL to match all hosts. */ char **dvec; /**< List of pointers to the strings in dbuffer. */ int dcount; /**< How many parts to this domain? (length of dvec) */ int unanchored; /**< Bitmap - flags are ANCHOR_LEFT and ANCHOR_RIGHT. */ +#endif /* defined FEATURE_PCRE_HOST_PATTERNS */ char *port_list; /**< List of acceptable ports, or NULL to match all ports */ @@ -945,7 +952,11 @@ struct url_spec /** * If you declare a static url_spec, this is the value to initialize it to zero. */ +#ifndef FEATURE_PCRE_HOST_PATTERNS #define URL_SPEC_INITIALIZER { NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL } +#else +#define URL_SPEC_INITIALIZER { NULL, NULL, NULL, NULL, NULL, NULL } +#endif /* def FEATURE_PCRE_HOST_PATTERNS */ /** * Constant for host part matching in URLs. If set, indicates that the start of diff --git a/urlmatch.c b/urlmatch.c index 735e8f85..54089a72 100644 --- a/urlmatch.c +++ b/urlmatch.c @@ -1,4 +1,4 @@ -const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.22 2008/03/30 15:02:32 fabiankeil Exp $"; +const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.23 2008/04/05 12:19:20 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $ @@ -6,7 +6,7 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.22 2008/03/30 15:02:32 fabianke * Purpose : Declares functions to match URLs against URL * patterns. * - * Copyright : Written by and Copyright (C) 2001-2003, 2006-2007 the SourceForge + * Copyright : Written by and Copyright (C) 2001-2003, 2006-2008 the SourceForge * Privoxy team. http://www.privoxy.org/ * * Based on the Internet Junkbuster originally written @@ -33,6 +33,9 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.22 2008/03/30 15:02:32 fabianke * * Revisions : * $Log: urlmatch.c,v $ + * Revision 1.23 2008/04/05 12:19:20 fabiankeil + * Factor compile_host_pattern() out of create_url_spec(). + * * Revision 1.22 2008/03/30 15:02:32 fabiankeil * SZitify unknown_method(). * @@ -632,6 +635,62 @@ jb_err parse_http_request(const char *req, } +#ifdef FEATURE_PCRE_HOST_PATTERNS +/********************************************************************* + * + * Function : compile_host_pattern + * + * Description : Parses and compiles a PCRE host pattern.. + * + * Parameters : + * 1 : url = Target url_spec to be filled in. + * 2 : host_pattern = Host pattern to compile. + * + * Returns : JB_ERR_OK - Success + * JB_ERR_MEMORY - Out of memory + * JB_ERR_PARSE - Cannot parse regex + * + *********************************************************************/ +static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern) +{ + int errcode; + char rebuf[BUFFER_SIZE]; + + assert(host_pattern); + assert(strlen(host_pattern) < sizeof(rebuf) - 2); + + url->host_regex = zalloc(sizeof(*url->host_regex)); + if (NULL == url->host_regex) + { + free_url_spec(url); + return JB_ERR_MEMORY; + } + + snprintf(rebuf, sizeof(rebuf), "%s$", host_pattern); + + errcode = regcomp(url->host_regex, rebuf, + (REG_EXTENDED|REG_NOSUB|REG_ICASE)); + + if (errcode) + { + size_t errlen = regerror(errcode, url->host_regex, rebuf, sizeof(rebuf)); + if (errlen > (sizeof(rebuf) - (size_t)1)) + { + errlen = sizeof(rebuf) - (size_t)1; + } + rebuf[errlen] = '\0'; + log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf); + free_url_spec(url); + + return JB_ERR_PARSE; + } + + return JB_ERR_OK; + +} + +#else + /********************************************************************* * * Function : compile_host_pattern @@ -848,6 +907,7 @@ static int domain_match(const struct url_spec *pattern, const struct http_reques } } +#endif /* def FEATURE_PCRE_HOST_PATTERNS */ /********************************************************************* @@ -1021,8 +1081,16 @@ void free_url_spec(struct url_spec *url) if (url == NULL) return; freez(url->spec); +#ifdef FEATURE_PCRE_HOST_PATTERNS + if (url->host_regex) + { + regfree(url->host_regex); + freez(url->host_regex); + } +#else freez(url->dbuffer); freez(url->dvec); +#endif /* ndef FEATURE_PCRE_HOST_PATTERNS */ freez(url->path); freez(url->port_list); if (url->preg) @@ -1056,7 +1124,11 @@ int url_match(const struct url_spec *pattern, { /* XXX: these should probably be functions. */ #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, url->port)) +#ifdef FEATURE_PCRE_HOST_PATTERNS +#define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, url->host, 0, NULL, 0))) +#else #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, url))) +#endif #define PATH_MATCHES ((NULL == pattern->path) || (0 == regexec(pattern->preg, url->path, 0, NULL, 0))) if (pattern->tag_regex != NULL) -- 2.39.2