parser.rl
author Mahlon E. Smith <mahlon@martini.nu>
Sun, 23 Oct 2011 22:59:59 -0700
changeset 12 191b3c25974a
parent 10 d07309450285
child 13 23a242d7b7fa
permissions -rw-r--r--
Clean up redundant parser actions via preprocessor macros, add a hostname->tld extractor for doing tld "wildcard" queries. Stop tracking 'parser.c' completely.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
/* vim: set noet nosta sw=4 ts=4 ft=ragel : */
2
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     2
/*
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     3
Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     4
All rights reserved.
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     5
Redistribution and use in source and binary forms, with or without
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     6
modification, are permitted provided that the following conditions are met:
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     7
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     8
    * Redistributions of source code must retain the above copyright
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     9
      notice, this list of conditions and the following disclaimer.
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    10
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    11
    * Redistributions in binary form must reproduce the above copyright
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    12
      notice, this list of conditions and the following disclaimer in the
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    13
      documentation and/or other materials provided with the distribution.
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    14
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    15
    * Neither the name of Mahlon E. Smith nor the names of his
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    16
      contributors may be used to endorse or promote products derived
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    17
      from this software without specific prior written permission.
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    18
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    19
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    20
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    21
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    22
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    23
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    24
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    25
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    26
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    27
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    29
*/
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    30
0
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
#include "volta.h"
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    33
#define MARK_S( LBL ) p_request->tokens.LBL ## _start = p;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    34
#define MARK_E( LBL ) p_request->tokens.LBL ## _length = p - ( *pe + p_request->tokens.LBL ## _start );
1
823d42546cea Dial in the Makefile and command line option parsing. Better debug
Mahlon E. Smith <mahlon@laika.com>
parents: 0
diff changeset
    35
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    36
/* 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    37
 * Tokenize an incoming line from squid, returning a parsed and populated
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    38
 * structure to make redirection decisions against.  This pointer should
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    39
 * be freed using cleanup_request() after use.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    40
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    41
 * Squid documentation about redirectors:
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    42
 * ---------------------------------------------------------------------------
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    43
 * TAG: url_rewrite_program
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    44
 * Specify the location of the executable for the URL rewriter.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    45
 * Since they can perform almost any function there isn't one included.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    46
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    47
 * For each requested URL rewriter will receive on line with the format
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    48
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    49
 * URL <SP> client_ip "/" fqdn <SP> user <SP> method [<SP> kvpairs]<NL>
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    50
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    51
 * In the future, the rewriter interface will be extended with
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    52
 * key=value pairs ("kvpairs" shown above).  Rewriter programs
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    53
 * should be prepared to receive and possibly ignore additional
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    54
 * whitespace-separated tokens on each input line.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    55
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    56
 * And the rewriter may return a rewritten URL. The other components of
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    57
 * the request line does not need to be returned (ignored if they are).
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    58
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    59
 * The rewriter can also indicate that a client-side redirect should
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    60
 * be performed to the new URL. This is done by prefixing the returned
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    61
 * URL with "301:" (moved permanently) or 302: (moved temporarily).
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    62
 * 
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    63
 * By default, a URL rewriter is not used.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    64
 * ---------------------------------------------------------------------------
1
823d42546cea Dial in the Makefile and command line option parsing. Better debug
Mahlon E. Smith <mahlon@laika.com>
parents: 0
diff changeset
    65
*/
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    66
request *
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    67
parse( char *line )
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    68
{
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    69
   	/* machine required vars */
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    70
	unsigned short int cs = 1;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    71
	char *p   = line;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    72
	char *pe  = p + strlen(p);
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    73
	char *eof = NULL;
2
8c88756f81b0 Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    74
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    75
	/* the client request pointer */
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    76
	request *p_request = init_request();
0
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    78
%%{
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    79
	machine input_parser;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    80
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    81
	action channel_id_found  {
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    82
		debug( 1, LOC, "Channel ID found in redirector input.  Set 'url_rewrite_concurrency' to '0' in squid.\n" );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    83
		fbreak;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    84
	}
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    85
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    86
	action scheme_start  { MARK_S(scheme) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    87
	action scheme_finish { MARK_E(scheme) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    88
	action host_start    { MARK_S(host) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    89
	action host_finish   { MARK_E(host) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    90
	action port_start    { MARK_S(port) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    91
	action port_finish   { MARK_E(port) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    92
	action path_start    { MARK_S(path) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    93
	action path_finish   { MARK_E(path) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    94
	action meth_start    { MARK_S(meth) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    95
	action meth_finish   { MARK_E(meth) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    96
	action c_ip_start    { MARK_S(c_ip) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    97
	action c_ip_finish   { MARK_E(c_ip) }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    98
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
    99
	action host_error   { debug( 3, LOC, "Unable to parse hostname.\n" ); }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   100
	action scheme_error { debug( 3, LOC, "Unable to parse scheme.\n" ); }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   101
	action meth_error   { debug( 3, LOC, "Unable to parse method.\n" ); }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   102
	action c_ip_error   { debug( 3, LOC, "Unable to parse the client IP address.\n" ); }
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   103
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   104
	# 
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   105
	# Squid line: URL <SP> client_ip "/" fqdn <SP> user <SP> method [<SP> kvpairs]<NL>
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   106
	# 
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   107
	# URI Syntax (RFC 3986) misc notes:
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   108
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   109
	# - Scheme isn't passed to redirectors on CONNECT method requests
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   110
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   111
	# - Hostname segments aren't supposed to be individually greater than 63 chars,
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   112
	#   and the hostname in total shouldn't exceed 255.  They also shouldn't be entirely
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   113
	#   made up of digits, or contain underscores.  In practice, these rules appear to
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   114
	#   be violated constantly by some pretty big sites.
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   115
	#   (( alnum ) | ( alnum . [a-zA-Z0-9\-]{0,63} . alnum )) & !( digit+ );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   116
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   117
	# - ipv6 has some utterly insane rules (RFC 5952) in the name of "shortcuts", which
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   118
	#   only seem like shortcuts to someone writing IP addresses by hand.  Anyone that
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   119
	#   has to parse (or even just read) them has a bunch of seemingly arbitrary work
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   120
	#   dumped in their lap.  Heck, it's impossible to even search for an ipv6 address
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   121
	#   that contains zeros in a text editor, because you have no idea what how it might
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   122
	#   be represented.  Rad!
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   123
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   124
	#   The parser just trusts any ipv6 squid hands us as being valid, without
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   125
	#   any real parsing/validation, other than it consists of hex digits and colons.
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   126
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   127
	# - This parser originally validated path/query/fragment as well, but there were
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   128
	#   enough inconsistencies with unescaped chars and other real-life RFC deviations
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   129
	#   that I opted to just accept what we get from squid.
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   130
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   131
	# - Redirectors aren't handed any userinfo (http://mahlon:password@example.com),
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   132
	#   so no need to check for that.
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   133
	#
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   134
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   135
	host_component = alnum | ( alnum [a-zA-Z0-9\-_]* alnum );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   136
	pchar          = ( alnum | [\-._~!$%&'()*+,;=] );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   137
	path_segment   = '/' ( any - space )*;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   138
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   139
	hostname       = host_component ( '.' host_component )* '.'?;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   140
	ipv4           = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   141
	ipv6           = ( xdigit | ':' )+;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   142
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   143
	channel_id     = ( digit+ space )      %channel_id_found;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   144
	scheme         = ( alpha{3,5} '://' )  >scheme_start %scheme_finish @!scheme_error;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   145
	host           = ( hostname | ipv4 )   >host_start   %host_finish   @!host_error;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   146
	port           = ( ':' digit{1,5} )    >port_start   %port_finish;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   147
	path           = path_segment*         >path_start   %path_finish;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   148
	client_ip      = ipv4                  >c_ip_start   %c_ip_finish   @!c_ip_error;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   149
	method         = upper+                >meth_start   %meth_finish   @!meth_error;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   150
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   151
	SquidLine = (
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   152
 		start:   channel_id?                            -> Url,
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   153
		Url:     scheme? host port? path? space         -> Client,
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   154
		Client:  client_ip '/' ( hostname | '-' ) space -> User,
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   155
		User:    pchar+ space                           -> Method,
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   156
		Method:  method                                 -> KVPairs,
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   157
		KVPairs: ( space any+ )?                        -> final
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   158
 	);
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   159
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   160
	main := SquidLine '\n';
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   161
}%%
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   162
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   163
	/* state machine */
0
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
	%% write exec;
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   166
	/* If we were given an invalid line, bail early */
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   167
	if ( cs < %%{ write first_final; }%% ) {
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   168
		free( p_request ), p_request = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   169
		debug( 3, LOC, "Invalid line (%d), skipped\n", v.timer.lines + 1 );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   170
		debug( 4, LOC, "%s", line );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   171
		return( NULL );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   172
	}
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   173
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   174
	(void)populate_request( p_request );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   175
	return( p_request );
0
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
}
eac7211fe522 Initial commit of an experimental little Squid redirector.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   178
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   179
/*
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   180
 * Initialize and return a pointer to a new request object.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   181
 *
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   182
 */
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   183
request *
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   184
init_request( void )
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   185
{
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   186
	request *p_request = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   187
	if ( (p_request = malloc( sizeof(request) )) == NULL ) {
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   188
		debug( 5, LOC, "Unable to allocate memory for request struct: %s\n", strerror(errno) );
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   189
		return( NULL );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   190
	}
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   191
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   192
	p_request->scheme    = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   193
	p_request->host      = NULL;
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   194
	p_request->tld       = NULL;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   195
	p_request->port      = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   196
	p_request->path      = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   197
	p_request->user      = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   198
	p_request->method    = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   199
	p_request->client_ip = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   200
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   201
	p_request->tokens.scheme_start  = NULL;
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   202
	p_request->tokens.scheme_length = 0;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   203
	p_request->tokens.host_start    = NULL;
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   204
	p_request->tokens.host_length   = 0;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   205
	p_request->tokens.port_start    = NULL;
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   206
	p_request->tokens.port_length   = 0;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   207
	p_request->tokens.path_start    = NULL;
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   208
	p_request->tokens.path_length   = 0;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   209
	p_request->tokens.meth_start    = NULL;
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   210
	p_request->tokens.meth_length   = 0;
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   211
	p_request->tokens.c_ip_start    = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   212
	p_request->tokens.c_ip_length   = 0;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   213
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   214
	return p_request;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   215
}
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   216
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   217
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   218
#define COPY_STR( LBL ) copy_string_token( p_request->tokens.LBL ## _start, p_request->tokens.LBL ## _length )
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   219
#define COPY_IP4( LBL ) copy_ipv4_token(   p_request->tokens.LBL ## _start, p_request->tokens.LBL ## _length )
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   220
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   221
/*
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   222
 * Take the previously parsed token locations and copy them into the request struct.
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   223
 *
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   224
 */
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   225
void
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   226
populate_request( request *p_request )
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   227
{
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   228
	p_request->scheme    = COPY_STR( scheme );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   229
	p_request->host      = COPY_STR( host );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   230
	p_request->port      = COPY_STR( port );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   231
	p_request->path      = COPY_STR( path );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   232
	p_request->method    = COPY_STR( meth );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   233
	p_request->client_ip = COPY_IP4( c_ip );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   234
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   235
	parse_tld( p_request );
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   236
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   237
	return;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   238
}
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   239
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   240
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   241
/*
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   242
 * Pull the top level domain out of the requested hostname.
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   243
 *
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   244
 */
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   245
void
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   246
parse_tld( request *p_request )
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   247
{
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   248
	unsigned short int cs = 5, mark = 0;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   249
	char *p   = p_request->host;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   250
	char *pe  = p + p_request->tokens.host_length;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   251
	char *ts  = 0, *te = 0, *eof = NULL;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   252
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   253
%%{
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   254
    machine tld_parser;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   255
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   256
	host_component = alnum | ( alnum [a-zA-Z0-9\-_]* alnum );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   257
	tld = ( host_component '.' host_component );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   258
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   259
	main := |*
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   260
		tld => { mark = ( p_request->tokens.host_length - (int)strlen(te) ); };
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   261
	*|;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   262
}%%
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   263
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   264
	/* It's far easier (and faster) to scan from left to right rather than
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   265
	   backtrack, so start by reversing the requested host string. */
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   266
	reverse_str( p_request->host );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   267
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   268
	/* scanner */
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   269
	%% write exec;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   270
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   271
	/* If there was a mark, then allocate memory and copy. */
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   272
	if ( mark != 0 ) {
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   273
		if ( (p_request->tld = calloc( mark + 1, sizeof(char) )) == NULL ) {
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   274
			debug( 5, LOC, "Unable to allocate memory for tld token: %s\n", strerror(errno) );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   275
			reverse_str( p_request->host );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   276
			return;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   277
		}
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   278
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   279
		memcpy( p_request->tld, p_request->host, mark );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   280
		reverse_str( p_request->tld );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   281
	}
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   282
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   283
	/* restore the hostname. */
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   284
	reverse_str( p_request->host );
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   285
	return;
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   286
}
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   287
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   288
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   289
/*
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   290
 * Release memory used by the request struct.
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   291
 *
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   292
 */
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   293
void
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   294
cleanup_request( struct request *p_request )
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   295
{
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   296
	if ( p_request == NULL ) return;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   297
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   298
	free( p_request->scheme );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   299
	free( p_request->host );
12
191b3c25974a Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 10
diff changeset
   300
	free( p_request->tld );
10
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   301
	free( p_request->port );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   302
	free( p_request->path );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   303
	free( p_request->method );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   304
	free( p_request->client_ip );
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   305
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   306
	free( p_request ), p_request = NULL;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   307
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   308
	return;
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   309
}
d07309450285 Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   310