process.c
author Mahlon E. Smith <mahlon@martini.nu>
Sat, 05 Nov 2011 12:52:29 -0700
changeset 15 2706fc514dea
parent 14 51eb85ae4de4
child 16 e6a640ad2cc2
permissions -rw-r--r--
Add whitelisting rules, to negate other matches if they come first in the ruleset. Remove the results set array completely, it's faster and requires less memory to do the comparisons inline.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
/* vim: set noet nosta sw=4 ts=4 ft=c : */
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
/*
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
All rights reserved.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
Redistribution and use in source and binary forms, with or without
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
modification, are permitted provided that the following conditions are met:
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
    * Redistributions of source code must retain the above copyright
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
      notice, this list of conditions and the following disclaimer.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
    * Redistributions in binary form must reproduce the above copyright
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
      notice, this list of conditions and the following disclaimer in the
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
      documentation and/or other materials provided with the distribution.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
    * Neither the name of Mahlon E. Smith nor the names of his
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
      contributors may be used to endorse or promote products derived
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
      from this software without specific prior written permission.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
*/
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
#include "volta.h"
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
#include "db.h"
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    34
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    35
/*
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    36
 * Given a redirect +line+ from squid, send it to the parser,
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    37
 * perform database lookups, and conditonally perform the rewrite.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    38
 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    39
 */
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
void
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
process( char *line )
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
{
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    43
	parsed *p_request = parse_request( line ), *rule = NULL;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
	/* count lines in debugmode */
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
	if ( v.debugmode > 2 ) v.timer.lines++;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    48
	/* If request parsing failed, return a blank line to squid
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    49
	   to allow the request to pass through unmolested. */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    50
	if ( p_request == NULL ) {
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    51
		out( "\n" );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    52
		finish_parsed( p_request );
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
		return;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
	}
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    56
	/*
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    57
	 * Main rewrite logic.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    58
	 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    59
	 * First, try and match the host exactly.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    60
	 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    61
	 * Second, match the TLD of the host, so separate rules aren't needed for
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    62
	 * every possible subdomain of one particular domain.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    63
	 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    64
	 * Finally, look for '*', if for some reason the rules provided don't care to
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    65
	 * match specific hosts, and instead just match on any path.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    66
	 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    67
	 * If DB matches are found at any step above, the rules are tried in order
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    68
	 * to attempt a match against the path.  Exact string match attempted
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    69
	 * first, then fallback to regexp.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    70
	 * 
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    71
	 * First rule match wins, and elements of the URL are rewritten based on
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    72
	 * what is present in the rule -- any missing parts just use the original
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    73
	 * URL element.  (this way, you can rewrite just the host and leave the
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    74
	 * path intact, or redir to https, for example.)
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    75
	 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    76
	 */
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    77
	rule = find_rule( p_request->host, p_request );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    78
	if ( rule == NULL ) rule = find_rule( p_request->tld, p_request );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    79
	if ( rule == NULL ) rule = find_rule( "*", p_request );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    80
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    81
	/* no matching rule still or whitelist rule?  no need to rewrite anything. */
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    82
	if ( rule == NULL || rule->wl ) {
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    83
		out( "\n" );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    84
	}
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    85
	/* otherwise, perform the rewrite. */
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    86
	else {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    87
		rewrite( p_request, rule );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    88
	}
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    89
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
    90
	finish_parsed( rule );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    91
	finish_parsed( p_request );
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
	return;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
}
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    95
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    96
/*
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    97
 * Output a rewritten URL for squid.
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    98
 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    99
 */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   100
void
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   101
rewrite( parsed *request, parsed *rule )
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   102
{
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   103
	if ( rule == NULL || v.debugmode >= 5 ) return;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   104
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   105
	if ( rule->redir ) printf( "%s:", rule->redir );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   106
	printf( "%s%s", (rule->scheme ? rule->scheme : request->scheme), rule->host );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   107
	if ( rule->port ) printf( ":%s", rule->port );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   108
	printf( "%s", rule->path ? rule->path : request->path );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   109
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   110
	printf("\n");
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   111
	return;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   112
}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   113
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   114
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   115
/*
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   116
 * Compare a parsed +rule+ against the +request+.
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   117
 * Returns 1 on a match, 0 otherwise.
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   118
 *
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   119
 */
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   120
unsigned short int
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   121
check_rule( parsed *rule, parsed *p_request )
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   122
{
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   123
	int re_rv;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   124
	regex_t re;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   125
	char re_err[128];
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   126
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   127
	if ( rule == NULL || p_request->path == NULL ) return( 0 );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   128
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   129
	/* quick comparison */
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   130
	if ( (strcasecmp( rule->path_re, p_request->path ) == 0) ||
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   131
			(strcmp( rule->path_re, "*" ) == 0) ) {
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   132
		debug( 4, LOC, "Rule match \"%s\" (non regexp)\n", rule->path_re );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   133
		return( 1 );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   134
	}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   135
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   136
	/* compile the regexp */
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   137
	if ( (re_rv = regcomp( &re, rule->path_re, REG_EXTENDED | REG_NOSUB )) != 0 ) {
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   138
		regerror( re_rv, &re, re_err, 128 );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   139
		debug( 4, LOC, "Invalid regex: \"%s\": %s\n", rule->path_re, re_err );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   140
		regfree( &re );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   141
		return( 0 );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   142
	}
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   143
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   144
	/* compare! */
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   145
	if ( (regexec( &re, p_request->path, 0, NULL, 0 )) == 0 ) {
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   146
		debug( 4, LOC, "Rule match \"%s\" (regexp)\n", rule->path_re );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   147
		regfree( &re );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   148
		return( 1 );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   149
	}
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   150
	else {
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   151
		regfree( &re );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   152
		return( 0 );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   153
	}
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   154
}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   155