X-Git-Url: http://www.privoxy.org/gitweb/?a=blobdiff_plain;f=urlmatch.c;h=6cb6013f3371e9ff1ad5aa67d7976ced67b9387d;hb=c11f44ef9db82f0495e416db5f0555388c6bc4ff;hp=05ec84ac161fec4627a5ec919f155bcba951661e;hpb=0ecd3de12dd83bbe337886f8ad22f88485f3cb37;p=privoxy.git diff --git a/urlmatch.c b/urlmatch.c index 05ec84ac..6cb6013f 100644 --- a/urlmatch.c +++ b/urlmatch.c @@ -1,4 +1,4 @@ -const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.56 2009/06/03 16:43:50 fabiankeil Exp $"; +const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.61 2011/05/22 10:25:26 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $ @@ -58,7 +58,13 @@ const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.56 2009/06/03 16:43:50 fabianke const char urlmatch_h_rcs[] = URLMATCH_H_VERSION; -enum regex_anchoring {NO_ANCHORING, LEFT_ANCHORED, RIGHT_ANCHORED}; +enum regex_anchoring +{ + NO_ANCHORING, + LEFT_ANCHORED, + RIGHT_ANCHORED, + RIGHT_ANCHORED_HOST +}; static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern); /********************************************************************* @@ -197,11 +203,11 @@ jb_err parse_http_url(const char *url, struct http_request *http, int require_pr /* * Check for * URI. If found, we're done. - */ + */ if (*http->url == '*') { if ( NULL == (http->path = strdup("*")) - || NULL == (http->hostport = strdup("")) ) + || NULL == (http->hostport = strdup("")) ) { return JB_ERR_MEMORY; } @@ -414,14 +420,14 @@ static int unknown_method(const char *method) * Microsoft webDAV extension for Exchange 2000. See: * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp - */ + */ "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH", /* * Another Microsoft webDAV extension for Exchange 2000. See: * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp - */ + */ "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL", /* * Yet another WebDAV extension, this time for @@ -546,9 +552,10 @@ jb_err parse_http_request(const char *req, struct http_request *http) * * Parameters : * 1 : pattern = The pattern to compile. - * 2 : anchoring = How the regex should be anchored. - * Can be either one of NO_ANCHORING, - * LEFT_ANCHORED or RIGHT_ANCHORED. + * 2 : anchoring = How the regex should be modified + * before compilation. Can be either + * one of NO_ANCHORING, LEFT_ANCHORED, + * RIGHT_ANCHORED or RIGHT_ANCHORED_HOST. * 3 : url = In case of failures, the spec member is * logged and the structure freed. * 4 : regex = Where the compiled regex should be stored. @@ -582,6 +589,9 @@ static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchorin case RIGHT_ANCHORED: fmt = "%s$"; break; + case RIGHT_ANCHORED_HOST: + fmt = "%s\\.?$"; + break; case LEFT_ANCHORED: fmt = "^%s"; break; @@ -731,7 +741,7 @@ static jb_err compile_url_pattern(struct url_spec *url, char *buf) *********************************************************************/ static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern) { - return compile_pattern(host_pattern, RIGHT_ANCHORED, url, &url->host_regex); + return compile_pattern(host_pattern, RIGHT_ANCHORED_HOST, url, &url->host_regex); } #else @@ -769,7 +779,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter url->unanchored |= ANCHOR_LEFT; } - /* + /* * Split domain into components */ url->dbuffer = strdup(host_pattern); @@ -779,7 +789,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter return JB_ERR_MEMORY; } - /* + /* * Map to lower case */ for (p = url->dbuffer; *p ; p++) @@ -787,7 +797,7 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter *p = (char)tolower((int)(unsigned char)*p); } - /* + /* * Split the domain name into components */ url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1); @@ -799,11 +809,11 @@ static jb_err compile_host_pattern(struct url_spec *url, const char *host_patter } else if (url->dcount != 0) { - /* + /* * Save a copy of the pointers in dvec */ size = (size_t)url->dcount * sizeof(*url->dvec); - + url->dvec = (char **)malloc(size); if (NULL == url->dvec) { @@ -842,13 +852,13 @@ static int simplematch(const char *pattern, const char *text) { const unsigned char *pat = (const unsigned char *)pattern; const unsigned char *txt = (const unsigned char *)text; - const unsigned char *fallback = pat; + const unsigned char *fallback = pat; int wildcard = 0; - + unsigned char lastchar = 'a'; unsigned i; unsigned char charmap[32]; - + while (*txt) { @@ -866,15 +876,15 @@ static int simplematch(const char *pattern, const char *text) } /* '*' in the pattern? */ - if (*pat == '*') + if (*pat == '*') { - + /* The pattern ends afterwards? Speed up the return. */ if (*++pat == '\0') { return 0; } - + /* Else, set wildcard mode and remember position after '*' */ wildcard = 1; fallback = pat; @@ -888,7 +898,7 @@ static int simplematch(const char *pattern, const char *text) while (*++pat != ']') { if (!*pat) - { + { return 1; } else if (*pat == '-') @@ -900,7 +910,7 @@ static int simplematch(const char *pattern, const char *text) for (i = lastchar; i <= *pat; i++) { charmap[i / 8] |= (unsigned char)(1 << (i % 8)); - } + } } else { @@ -911,21 +921,21 @@ static int simplematch(const char *pattern, const char *text) } /* -END- if Character range specification */ - /* - * Char match, or char range match? + /* + * Char match, or char range match? */ if ( (*pat == *txt) || (*pat == '?') || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) ) { - /* - * Sucess: Go ahead + /* + * Success: Go ahead */ pat++; } else if (!wildcard) { - /* + /* * No match && no wildcard: No luck */ return 1; @@ -1118,7 +1128,7 @@ jb_err create_url_spec(struct url_spec *url, char *buf) } /* Is it a tag pattern? */ - if (0 == strncmpic("TAG:", url->spec, 4)) + if (0 == strncmpic(url->spec, "TAG:", 4)) { /* The pattern starts with the first character after "TAG:" */ const char *tag_pattern = buf + 4; @@ -1217,6 +1227,26 @@ static int host_matches(const struct http_request *http, } +/********************************************************************* + * + * Function : path_matches + * + * Description : Compares a path against a path pattern. + * + * Parameters : + * 1 : path = The path to match + * 2 : pattern = The URL pattern + * + * Returns : TRUE for yes, FALSE otherwise. + * + *********************************************************************/ +static int path_matches(const char *path, const struct url_spec *pattern) +{ + return ((NULL == pattern->preg) + || (0 == regexec(pattern->preg, path, 0, NULL, 0))); +} + + /********************************************************************* * * Function : url_match @@ -1233,17 +1263,14 @@ static int host_matches(const struct http_request *http, int url_match(const struct url_spec *pattern, const struct http_request *http) { - /* XXX: these should probably be functions. */ -#define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0))) - if (pattern->tag_regex != NULL) { /* It's a tag pattern and shouldn't be matched against URLs */ return 0; - } + } return (port_matches(http->port, pattern->port_list) - && host_matches(http, pattern) && PATH_MATCHES); + && host_matches(http, pattern) && path_matches(http->path, pattern)); }