parser.rl
changeset 2 8c88756f81b0
parent 1 823d42546cea
child 10 d07309450285
equal deleted inserted replaced
1:823d42546cea 2:8c88756f81b0
     1 /* vim: set noet nosta sw=4 ts=4 ft=ragel : */
     1 /* vim: set noet nosta sw=4 ts=4 ft=ragel : */
       
     2 /*
       
     3 Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
       
     4 All rights reserved.
       
     5 Redistribution and use in source and binary forms, with or without
       
     6 modification, are permitted provided that the following conditions are met:
       
     7 
       
     8     * Redistributions of source code must retain the above copyright
       
     9       notice, this list of conditions and the following disclaimer.
       
    10 
       
    11     * Redistributions in binary form must reproduce the above copyright
       
    12       notice, this list of conditions and the following disclaimer in the
       
    13       documentation and/or other materials provided with the distribution.
       
    14 
       
    15     * Neither the name of Mahlon E. Smith nor the names of his
       
    16       contributors may be used to endorse or promote products derived
       
    17       from this software without specific prior written permission.
       
    18 
       
    19 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
       
    20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
       
    23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29 */
       
    30 
       
    31 /*
       
    32 Squid docs:
       
    33 ---------------------------------------------------------------------------
       
    34 TAG: url_rewrite_program
       
    35 Specify the location of the executable for the URL rewriter.
       
    36 Since they can perform almost any function there isn't one included.
       
    37 
       
    38 For each requested URL rewriter will receive on line with the format
       
    39 
       
    40 URL <SP> client_ip "/" fqdn <SP> user <SP> method [<SP> kvpairs]<NL>
       
    41 
       
    42 In the future, the rewriter interface will be extended with
       
    43 key=value pairs ("kvpairs" shown above).  Rewriter programs
       
    44 should be prepared to receive and possibly ignore additional
       
    45 whitespace-separated tokens on each input line.
       
    46 
       
    47 And the rewriter may return a rewritten URL. The other components of
       
    48 the request line does not need to be returned (ignored if they are).
       
    49 
       
    50 The rewriter can also indicate that a client-side redirect should
       
    51 be performed to the new URL. This is done by prefixing the returned
       
    52 URL with "301:" (moved permanently) or 302: (moved temporarily).
       
    53 
       
    54 By default, a URL rewriter is not used.
       
    55 ---------------------------------------------------------------------------
       
    56 */
     2 
    57 
     3 #include "volta.h"
    58 #include "volta.h"
     4 
    59 
     5 %%{
    60 %%{
     6 	machine redirector;
    61 	machine redirector;
     7 
    62 
       
    63 	action parse_error {
       
    64 		debug( 2, LOC, "parse error\n" );
       
    65 		return( NULL );
       
    66 	}
       
    67 
     8 	action yay {
    68 	action yay {
     9 		printf( "YAH\n" );
    69 		printf( "I saw: %s", p+1 );
    10 	}
    70 	}
    11 
    71 
    12 	# http://, ftp://, https://, etc
    72 	# http://, ftp://, https://, etc
    13 	proto = alpha{3,5} . '://';
    73 	proto = alpha{3,5} . '://';
    14 
    74 
    15 	# http://mahlon:password@example.com or http://mahlon@example.com
    75 	# http://mahlon:password@example.com or http://mahlon@example.com
    16     #       username              optional password
    76     #       username              optional password
    17 	creds = ( alnum | [+._\-] )+ . ( ':' . any+ )? . '@';
    77 	creds = ( alnum | [+._\-] )+ . ( ':' . any+ )? . '@';
    18 
    78 
    19 	main := ' ' @yay;
    79 	main  := ( proto . creds ) | proto @yay '\n';
    20 }%%
    80 }%%
    21 %% write data;
    81 %% write data;
    22 
    82 
    23 /* state machine data */
       
    24 /*
    83 /*
    25 %%{
    84 %%{
    26 	machine redirector;
    85 	machine redirector;
    27 
    86 
    28 	action yay {
    87 	action yay {
    29 		printf( "YAH\n" );
    88 		printf( "I saw: %s", p+1 );
    30 	}
    89 	}
    31 
    90 
    32 	# http://, ftp://, https://, etc
    91 	# http://, ftp://, https://, etc
    33 	proto = alpha{3,5} . '://';
    92 	proto = alpha{3,5} . '://';
    34 
    93 
    39 	main := ( proto . creds ) | proto @yay '\n';
    98 	main := ( proto . creds ) | proto @yay '\n';
    40 }%%
    99 }%%
    41 %% write data;
   100 %% write data;
    42 */
   101 */
    43 
   102 
    44 int
   103 
       
   104 /*
       
   105 %%{
       
   106 	machine foo;
       
   107 
       
   108 	OPEN = 0;
       
   109 	CLOSE = 1;
       
   110 
       
   111 	main :=
       
   112 		start:
       
   113 		door_closed: (
       
   114 			OPEN -> door_open -> final
       
   115 		),
       
   116 		door_open: (
       
   117 			CLOSE -> door_closed
       
   118 		);
       
   119 }%%
       
   120 */
       
   121 
       
   122 struct request *
    45 parse( char *p )
   123 parse( char *p )
    46 {
   124 {
    47    	/* initial machine state */
   125    	/* initial machine state */
    48 	short int cs = 0;
   126 	short int cs = 0;
    49 
   127 
    50 	/* the client request object */
   128 	/* the client request object */
    51 	/* request c_request; */
   129 	request c_request;
    52 	/* request *cp_request = &c_request; */
   130 	request *cp_request = &c_request;
    53 
   131 
    54 	/*
   132 	/*
    55 	char ip[ INET_ADDRSTRLEN ];
   133 	char ip[ INET_ADDRSTRLEN ];
    56 	inet_pton( AF_INET, "127.0.0.1", &cp_request->ip );
   134 	inet_pton( AF_INET, "127.0.0.1", &cp_request->ip );
    57 	inet_ntop( AF_INET, &cp_request->ip, ip, INET_ADDRSTRLEN );
   135 	inet_ntop( AF_INET, &cp_request->ip, ip, INET_ADDRSTRLEN );
    64 	%% write init;
   142 	%% write init;
    65 	%% write exec;
   143 	%% write exec;
    66 
   144 
    67 	/* reset the request */
   145 	/* reset the request */
    68 	/* c_request = reset_request; */
   146 	/* c_request = reset_request; */
    69 	return( 0 );
   147 	return( cp_request );
    70 }
   148 }
    71 
   149