parser.rl
changeset 22 822094314703
parent 18 d4ce82194b64
child 29 c5d00a24af56
equal deleted inserted replaced
21:3510b50c6694 22:822094314703
   206 	action match_start   { MARK_S(path_re) }
   206 	action match_start   { MARK_S(path_re) }
   207 	action match_finish  { MARK_E(path_re) }
   207 	action match_finish  { MARK_E(path_re) }
   208 	action redir_start   { MARK_S(redir) }
   208 	action redir_start   { MARK_S(redir) }
   209 	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
   210 	action negate_finish { p_parsed->negate = 1; }
   210 	action negate_finish { p_parsed->negate = 1; }
   211 
   211 	action luapath_start { p_parsed->lua = 1; MARK_S(luapath) }
   212 	action scheme_start  { MARK_S(scheme) }
   212 
   213 	action scheme_finish { MARK_E(scheme) }
   213 	action scheme_start   { MARK_S(scheme) }
   214 	action host_start    { MARK_S(host) }
   214 	action scheme_finish  { MARK_E(scheme) }
   215 	action host_finish   { MARK_E(host) }
   215 	action host_start     { MARK_S(host) }
   216 	action port_start    { p_parsed->tokens.port_start = p+1; } # strip leading colon
   216 	action host_finish    { MARK_E(host) }
   217 	action port_finish   { MARK_E(port) }
   217 	action port_start     { p_parsed->tokens.port_start = p+1; } # strip leading colon
   218 	action path_start    { MARK_S(path) }
   218 	action port_finish    { MARK_E(port) }
   219 	action path_finish   { MARK_E(path) }
   219 	action path_start     { MARK_S(path) }
   220 
   220 	action path_finish    { MARK_E(path) }
   221 	action match_error { debug( 3, LOC, "Unable to parse the rule path matcher.\n" ); }
   221 	action luapath_finish { MARK_E(luapath) }
   222 	action host_error  { debug( 3, LOC, "Unable to parse the rule hostname.\n" ); }
   222 
       
   223 	action match_error   { debug( 3, LOC, "Unable to parse the rule path matcher.\n" ); }
       
   224 	action host_error    { debug( 3, LOC, "Unable to parse the rule hostname.\n" ); }
       
   225 	action luapath_error { debug( 3, LOC, "Unable to parse the lua path.\n" ); }
   223 
   226 
   224 	host_component  = alnum | ( alnum [a-zA-Z0-9\-_]* alnum );
   227 	host_component  = alnum | ( alnum [a-zA-Z0-9\-_]* alnum );
   225 	path_segment    = '/' ( any - space )*;
   228 	path_segment    = '/' ( any - space )*;
   226 
   229 
   227 	sep       = space+;
   230 	sep       = space+;
   228 	hostname  = host_component ( '.' host_component )* '.'?;
   231 	hostname  = host_component ( '.' host_component )* '.'?;
   229 	ipv4      = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   232 	ipv4      = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   230 	ipv6      = ( xdigit | ':' )+;
   233 	ipv6      = ( xdigit | ':' )+;
   231 
   234 
   232 	negate    = ( '-' )                 %negate_finish;
   235 	negate    = ( '-' )                 %negate_finish;
   233 	path_re   = ( any - space )+        >match_start  %match_finish @!match_error;
   236 	path_re   = ( any - space )+        >match_start    %match_finish   @!match_error;
       
   237 	luapath   = ( any - space )+        >luapath_start  %luapath_finish @!luapath_error;
   234 
   238 
   235 	redir     = ( ('301' | '302') ':' ) >redir_start  %redir_finish;
   239 	redir     = ( ('301' | '302') ':' ) >redir_start  %redir_finish;
   236 	scheme    = ( alpha{3,5} '://' )    >scheme_start %scheme_finish;
   240 	scheme    = ( alpha{3,5} '://' )    >scheme_start %scheme_finish;
   237 	host      = ( hostname | ipv4 )     >host_start   %host_finish   @!host_error;
   241 	host      = ( hostname | ipv4 )     >host_start   %host_finish   @!host_error;
   238 	port      = ( ':' digit{1,5} )      >port_start   %port_finish;
   242 	port      = ( ':' digit{1,5} )      >port_start   %port_finish;
   239 	path      = path_segment*           >path_start   %path_finish;
   243 	path      = path_segment*           >path_start   %path_finish;
   240 
   244 
   241 	rewrite   = ( redir? scheme? host port? path? );
   245 	rewrite   = ( redir? scheme? host port? path? );
   242 
   246 	luarule   = ( 'lua:' luapath );
   243 	main := path_re sep ( rewrite | negate );
   247 
       
   248 	main := path_re sep ( rewrite | negate | luarule );
   244 }%%
   249 }%%
   245 
   250 
   246 	/* state machine */
   251 	/* state machine */
   247 	%% write exec;
   252 	%% write exec;
   248 
   253 
   300 	sep            = space+;
   305 	sep            = space+;
   301 	host_component = alnum | ( alnum [a-zA-Z0-9\-_]* alnum );
   306 	host_component = alnum | ( alnum [a-zA-Z0-9\-_]* alnum );
   302 	hostname       = host_component ( '.' host_component )* '.'?;
   307 	hostname       = host_component ( '.' host_component )* '.'?;
   303 	ipv4           = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   308 	ipv4           = digit{1,3} '.' digit{1,3} '.' digit{1,3} '.' digit{1,3};
   304 	token          = ( any - space )+;
   309 	token          = ( any - space )+;
   305 	redir          = ( digit{3} ':' );
       
   306 	host           = ( hostname | ipv4 );
   310 	host           = ( hostname | ipv4 );
   307 
   311 
   308 	key = ( host | '*' )      >key_start %key_finish @!key_error;
   312 	key = ( host | '*' )      >key_start %key_finish @!key_error;
   309 	val = ( token sep token ) >val_start %val_finish @!val_error;
   313 	val = ( token sep token ) >val_start %val_finish @!val_error;
   310 	#       regex     rewrite or negate
       
   311 	
   314 	
   312 	main:= key sep val '\n';
   315 	main:= key sep val '\n';
   313 }%%
   316 }%%
   314 
   317 
   315 	/* state machine */
   318 	/* state machine */
   354 		return( NULL );
   357 		return( NULL );
   355 	}
   358 	}
   356 
   359 
   357 	p_parsed->type      = 0;
   360 	p_parsed->type      = 0;
   358 	p_parsed->negate    = 0;
   361 	p_parsed->negate    = 0;
       
   362 	p_parsed->lua       = 0;
   359 	p_parsed->path_re   = NULL;
   363 	p_parsed->path_re   = NULL;
   360 	p_parsed->redir     = NULL;
   364 	p_parsed->redir     = NULL;
   361 	p_parsed->scheme    = NULL;
   365 	p_parsed->scheme    = NULL;
   362 	p_parsed->host      = NULL;
   366 	p_parsed->host      = NULL;
   363 	p_parsed->tld       = NULL;
   367 	p_parsed->tld       = NULL;
   364 	p_parsed->port      = NULL;
   368 	p_parsed->port      = NULL;
   365 	p_parsed->path      = NULL;
   369 	p_parsed->path      = NULL;
   366 	p_parsed->user      = NULL;
   370 	p_parsed->user      = NULL;
   367 	p_parsed->method    = NULL;
   371 	p_parsed->method    = NULL;
   368 	p_parsed->client_ip = NULL;
   372 	p_parsed->client_ip = NULL;
       
   373 	p_parsed->luapath   = NULL;
   369 
   374 
   370 	p_parsed->tokens.path_re_start  = NULL;
   375 	p_parsed->tokens.path_re_start  = NULL;
   371 	p_parsed->tokens.redir_start    = NULL;
   376 	p_parsed->tokens.redir_start    = NULL;
   372 	p_parsed->tokens.scheme_start   = NULL;
   377 	p_parsed->tokens.scheme_start   = NULL;
   373 	p_parsed->tokens.host_start     = NULL;
   378 	p_parsed->tokens.host_start     = NULL;
   374 	p_parsed->tokens.port_start     = NULL;
   379 	p_parsed->tokens.port_start     = NULL;
   375 	p_parsed->tokens.path_start     = NULL;
   380 	p_parsed->tokens.path_start     = NULL;
   376 	p_parsed->tokens.meth_start     = NULL;
   381 	p_parsed->tokens.meth_start     = NULL;
   377 	p_parsed->tokens.c_ip_start     = NULL;
   382 	p_parsed->tokens.c_ip_start     = NULL;
       
   383 	p_parsed->tokens.luapath_start  = NULL;
   378 	p_parsed->tokens.path_re_length = 0;
   384 	p_parsed->tokens.path_re_length = 0;
   379 	p_parsed->tokens.redir_length   = 0;
   385 	p_parsed->tokens.redir_length   = 0;
   380 	p_parsed->tokens.scheme_length  = 0;
   386 	p_parsed->tokens.scheme_length  = 0;
   381 	p_parsed->tokens.host_length    = 0;
   387 	p_parsed->tokens.host_length    = 0;
   382 	p_parsed->tokens.port_length    = 0;
   388 	p_parsed->tokens.port_length    = 0;
   383 	p_parsed->tokens.path_length    = 0;
   389 	p_parsed->tokens.path_length    = 0;
   384 	p_parsed->tokens.meth_length    = 0;
   390 	p_parsed->tokens.meth_length    = 0;
   385 	p_parsed->tokens.c_ip_length    = 0;
   391 	p_parsed->tokens.c_ip_length    = 0;
       
   392 	p_parsed->tokens.luapath_length = 0;
   386 
   393 
   387 	return p_parsed;
   394 	return p_parsed;
   388 }
   395 }
   389 
   396 
   390 
   397 
   409 	}
   416 	}
   410 
   417 
   411 	if ( p_parsed->type == RULE ) {
   418 	if ( p_parsed->type == RULE ) {
   412 		free( p_parsed->path_re );
   419 		free( p_parsed->path_re );
   413 		free( p_parsed->redir );
   420 		free( p_parsed->redir );
       
   421 		free( p_parsed->luapath );
   414 	}
   422 	}
   415 
   423 
   416 	free( p_parsed ), p_parsed = NULL;
   424 	free( p_parsed ), p_parsed = NULL;
   417 
   425 
   418 	return;
   426 	return;
   444 	}
   452 	}
   445 
   453 
   446 	if ( p_parsed->type == RULE ) {
   454 	if ( p_parsed->type == RULE ) {
   447 		p_parsed->path_re = COPY_STR( path_re );
   455 		p_parsed->path_re = COPY_STR( path_re );
   448 		p_parsed->redir   = COPY_STR( redir );
   456 		p_parsed->redir   = COPY_STR( redir );
       
   457 		p_parsed->luapath = COPY_STR( luapath );
   449 	}
   458 	}
   450 
   459 
   451 	return;
   460 	return;
   452 }
   461 }
   453 
   462