parser.rl
changeset 18 d4ce82194b64
parent 15 2706fc514dea
child 22 822094314703
equal deleted inserted replaced
17:bd746609ba46 18:d4ce82194b64
    64  * ---------------------------------------------------------------------------
    64  * ---------------------------------------------------------------------------
    65 */
    65 */
    66 parsed *
    66 parsed *
    67 parse_request( char *line )
    67 parse_request( char *line )
    68 {
    68 {
    69    	/* machine required vars */
    69 	/* machine required vars */
    70 	unsigned short int cs = 1;
    70 	unsigned short int cs = 1;
    71 	char *p   = line;
    71 	char *p   = line;
    72 	char *pe  = p + strlen(p);
    72 	char *pe  = p + strlen(p);
    73 	char *eof = pe;
    73 	char *eof = pe;
    74 
    74 
   147 	port           = ( ':' digit{1,5} )    >port_start   %port_finish;
   147 	port           = ( ':' digit{1,5} )    >port_start   %port_finish;
   148 	path           = path_segment*         >path_start   %path_finish;
   148 	path           = path_segment*         >path_start   %path_finish;
   149 	client_ip      = ipv4                  >c_ip_start   %c_ip_finish   @!c_ip_error;
   149 	client_ip      = ipv4                  >c_ip_start   %c_ip_finish   @!c_ip_error;
   150 	method         = upper+                >meth_start   %meth_finish   @!meth_error;
   150 	method         = upper+                >meth_start   %meth_finish   @!meth_error;
   151 
   151 
   152 	SquidLine = (
   152 	URL = (
   153  		start:   channel_id?                            -> Url,
   153 		start:   channel_id?                            -> Url,
   154 		Url:     scheme? host port? path?               -> Client,
   154 		Url:     scheme? host port? path?               -> final
       
   155  	);
       
   156 
       
   157 	Squidvars = (
       
   158 		start:
   155 		Client:  space client_ip '/' ( hostname | '-' ) -> User,
   159 		Client:  space client_ip '/' ( hostname | '-' ) -> User,
   156 		User:    space pchar+                           -> Method,
   160 		User:    space pchar+                           -> Method,
   157 		Method:  space method                           -> KVPairs,
   161 		Method:  space method                           -> KVPairs,
   158 		KVPairs: ( space any+ )?                        -> final
   162 		KVPairs: ( space any+ )?                        -> final
   159  	);
   163  	);
   160 
   164 
   161 	main := SquidLine '\n';
   165 	main := URL Squidvars? '\n';
   162 }%%
   166 }%%
   163 
   167 
   164 	/* state machine */
   168 	/* state machine */
   165 	%% write exec;
   169 	%% write exec;
   166 
   170 
   184  *
   188  *
   185  */
   189  */
   186 parsed *
   190 parsed *
   187 parse_rule( char *rewrite )
   191 parse_rule( char *rewrite )
   188 {
   192 {
   189    	/* machine required vars */
   193 	/* machine required vars */
   190 	unsigned short int cs = 1;
   194 	unsigned short int cs = 1;
   191 	char *p   = rewrite;
   195 	char *p   = rewrite;
   192 	char *pe  = p + strlen(p);
   196 	char *pe  = p + strlen(p);
   193 	char *eof = pe;
   197 	char *eof = pe;
   194 
   198 
   201 
   205 
   202 	action match_start   { MARK_S(path_re) }
   206 	action match_start   { MARK_S(path_re) }
   203 	action match_finish  { MARK_E(path_re) }
   207 	action match_finish  { MARK_E(path_re) }
   204 	action redir_start   { MARK_S(redir) }
   208 	action redir_start   { MARK_S(redir) }
   205 	action redir_finish  { p_parsed->tokens.redir_length = 3; } # strip trailing colon
   209 	action redir_finish  { p_parsed->tokens.redir_length = 3; } # strip trailing colon
   206 	action wl_finish     { p_parsed->wl = 1; }
   210 	action negate_finish { p_parsed->negate = 1; }
   207 
   211 
   208 	action scheme_start  { MARK_S(scheme) }
   212 	action scheme_start  { MARK_S(scheme) }
   209 	action scheme_finish { MARK_E(scheme) }
   213 	action scheme_finish { MARK_E(scheme) }
   210 	action host_start    { MARK_S(host) }
   214 	action host_start    { MARK_S(host) }
   211 	action host_finish   { MARK_E(host) }
   215 	action host_finish   { MARK_E(host) }
   223 	sep       = space+;
   227 	sep       = space+;
   224 	hostname  = host_component ( '.' host_component )* '.'?;
   228 	hostname  = host_component ( '.' host_component )* '.'?;
   225 	ipv4      = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   229 	ipv4      = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   226 	ipv6      = ( xdigit | ':' )+;
   230 	ipv6      = ( xdigit | ':' )+;
   227 
   231 
   228 	whitelist = ( '-' sep )          %wl_finish;
   232 	negate    = ( '-' )                 %negate_finish;
   229 	path_re   = ( any - space )+     >match_start  %match_finish @!match_error;
   233 	path_re   = ( any - space )+        >match_start  %match_finish @!match_error;
   230 
   234 
   231 	redir     = ( digit{3} ':' )     >redir_start  %redir_finish;
   235 	redir     = ( ('301' | '302') ':' ) >redir_start  %redir_finish;
   232 	scheme    = ( alpha{3,5} '://' ) >scheme_start %scheme_finish;
   236 	scheme    = ( alpha{3,5} '://' )    >scheme_start %scheme_finish;
   233 	host      = ( hostname | ipv4 )  >host_start   %host_finish   @!host_error;
   237 	host      = ( hostname | ipv4 )     >host_start   %host_finish   @!host_error;
   234 	port      = ( ':' digit{1,5} )   >port_start   %port_finish;
   238 	port      = ( ':' digit{1,5} )      >port_start   %port_finish;
   235 	path      = path_segment*        >path_start   %path_finish;
   239 	path      = path_segment*           >path_start   %path_finish;
   236 
   240 
   237 	rewrite   = ( sep redir? scheme? host port? path? );
   241 	rewrite   = ( redir? scheme? host port? path? );
   238 
   242 
   239 	main := whitelist? path_re rewrite?;
   243 	main := path_re sep ( rewrite | negate );
   240 }%%
   244 }%%
   241 
   245 
   242 	/* state machine */
   246 	/* state machine */
   243 	%% write exec;
   247 	%% write exec;
   244 
   248 
   262  *
   266  *
   263  */
   267  */
   264 struct db_input *
   268 struct db_input *
   265 parse_dbinput( char *line )
   269 parse_dbinput( char *line )
   266 {
   270 {
   267    	/* machine required vars */
   271 	/* machine required vars */
   268 	unsigned short int cs = 1;
   272 	unsigned short int cs = 1;
   269 	char *p   = line;
   273 	char *p   = line;
   270 	char *pe  = p + strlen(p);
   274 	char *pe  = p + strlen(p);
   271 	char *eof = pe;
   275 	char *eof = pe;
   272 
   276 
   299 	ipv4           = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   303 	ipv4           = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   300 	token          = ( any - space )+;
   304 	token          = ( any - space )+;
   301 	redir          = ( digit{3} ':' );
   305 	redir          = ( digit{3} ':' );
   302 	host           = ( hostname | ipv4 );
   306 	host           = ( hostname | ipv4 );
   303 
   307 
   304 	key = ( host | '*' )                      >key_start %key_finish @!key_error;
   308 	key = ( host | '*' )      >key_start %key_finish @!key_error;
   305 	val = ( (token sep)? token (sep token)? ) >val_start %val_finish @!val_error;
   309 	val = ( token sep token ) >val_start %val_finish @!val_error;
   306 	#           wl       regex   rewrite
   310 	#       regex     rewrite or negate
   307 	
   311 	
   308 	main:= key sep val '\n';
   312 	main:= key sep val '\n';
   309 }%%
   313 }%%
   310 
   314 
   311 	/* state machine */
   315 	/* state machine */
   349 		debug( 5, LOC, "Unable to allocate memory for parsed struct: %s\n", strerror(errno) );
   353 		debug( 5, LOC, "Unable to allocate memory for parsed struct: %s\n", strerror(errno) );
   350 		return( NULL );
   354 		return( NULL );
   351 	}
   355 	}
   352 
   356 
   353 	p_parsed->type      = 0;
   357 	p_parsed->type      = 0;
   354 	p_parsed->wl        = 0;
   358 	p_parsed->negate    = 0;
   355 	p_parsed->path_re   = NULL;
   359 	p_parsed->path_re   = NULL;
   356 	p_parsed->redir     = NULL;
   360 	p_parsed->redir     = NULL;
   357 	p_parsed->scheme    = NULL;
   361 	p_parsed->scheme    = NULL;
   358 	p_parsed->host      = NULL;
   362 	p_parsed->host      = NULL;
   359 	p_parsed->tld       = NULL;
   363 	p_parsed->tld       = NULL;