Skip to content
Snippets Groups Projects
Commit 8b797f70 authored by Pierangelo Masarati's avatar Pierangelo Masarati
Browse files

allow user-defined return codes from rewriteRules

parent 3ddfddb1
No related branches found
No related tags found
No related merge requests found
......@@ -72,7 +72,8 @@
#define REWRITE_REGEXEC_OK 0x0000
#define REWRITE_REGEXEC_ERR 0x0001
#define REWRITE_REGEXEC_STOP 0x0002
#define REWRITE_REGEXEC_UNWILLING 0x0004
#define REWRITE_REGEXEC_UNWILLING 0x0003
#define REWRITE_REGEXEC_USER 0x0004 /* and above ... */
/*
* Rewrite variable flags
......@@ -93,6 +94,7 @@
* referenced string is available for
* the entire life scope of the variable.
*/
#define REWRITE_VAR_NONE 0x0000
#define REWRITE_VAR_INSERT 0x0001
#define REWRITE_VAR_UPDATE 0x0002
#define REWRITE_VAR_COPY_NAME 0x0004
......
......@@ -357,6 +357,15 @@ rewrite_context_apply(
goto rc_end_of_context;
}
break;
/*
* This ends the rewrite context
* and returns a user-defined
* error code
*/
case REWRITE_ACTION_USER:
return_code = ((int *)action->la_args)[ 0 ];
goto rc_end_of_context;
default:
/* ... */
......@@ -385,10 +394,17 @@ rewrite_context_apply(
* This will instruct the server to return
* an `unwilling to perform' error message
*/
case REWRITE_REGEXEC_UNWILLING:
case REWRITE_REGEXEC_UNWILLING:
return_code = REWRITE_REGEXEC_UNWILLING;
goto rc_end_of_context;
/*
* A user-defined error code has propagated ...
*/
default:
assert( rc >= REWRITE_REGEXEC_USER );
goto rc_end_of_context;
}
rc_continue:; /* sent here by actions that require to continue */
......
......@@ -71,6 +71,7 @@
#define REWRITE_FLAG_STOP '@'
#define REWRITE_FLAG_UNWILLING '#'
#define REWRITE_FLAG_GOTO 'G' /* requires an arg */
#define REWRITE_FLAG_USER 'U' /* requires an arg */
#define REWRITE_FLAG_IGNORE_ERR 'I'
/*
......@@ -97,6 +98,7 @@ struct rewrite_action {
#define REWRITE_ACTION_UNWILLING 0x0002
#define REWRITE_ACTION_GOTO 0x0003
#define REWRITE_ACTION_IGNORE_ERR 0x0004
#define REWRITE_ACTION_USER 0x0005
int la_type;
void *la_args;
};
......
......@@ -62,6 +62,8 @@ apply(
rewriteContext != NULL;
rewriteContext = sep,
sep ? sep = strchr( rewriteContext, ',' ) : NULL ) {
char *errmsg = "";
if ( sep != NULL ) {
sep[ 0 ] = '\0';
sep++;
......@@ -70,8 +72,35 @@ apply(
rc = rewrite_session( info, rewriteContext, string,
cookie, &result );
fprintf( stdout, "%s -> %s\n", string,
( result ? result : "unwilling to perform" ) );
switch ( rc ) {
case REWRITE_REGEXEC_OK:
errmsg = "ok";
break;
case REWRITE_REGEXEC_ERR:
errmsg = "error";
break;
case REWRITE_REGEXEC_STOP:
errmsg = "stop";
break;
case REWRITE_REGEXEC_UNWILLING:
errmsg = "unwilling to perform";
break;
default:
if (rc >= REWRITE_REGEXEC_USER) {
errmsg = "user-defined";
} else {
errmsg = "unknown";
}
break;
}
fprintf( stdout, "%s -> %s [%d:%s]\n", string,
( result ? result : "(null)" ),
rc, errmsg );
if ( result == NULL ) {
break;
}
......
......@@ -50,17 +50,17 @@ append_rule(
*/
static int
append_action(
struct rewrite_action *base,
struct rewrite_action **pbase,
struct rewrite_action *action
)
{
struct rewrite_action *a;
struct rewrite_action **pa;
assert( base != NULL );
assert( pbase != NULL );
assert( action != NULL );
for ( a = base; a->la_next != NULL; a = a->la_next );
a->la_next = action;
for ( pa = pbase; *pa != NULL; pa = &(*pa)->la_next );
*pa = action;
return REWRITE_SUCCESS;
}
......@@ -79,7 +79,8 @@ destroy_action(
/* do something */
switch ( action->la_type ) {
case REWRITE_FLAG_GOTO: {
case REWRITE_FLAG_GOTO:
case REWRITE_FLAG_USER: {
int *pi = (int *)action->la_args;
if ( pi ) {
......@@ -208,11 +209,16 @@ rewrite_rule_compile(
action->la_type = REWRITE_ACTION_UNWILLING;
break;
case REWRITE_FLAG_GOTO: { /* 'G' */
case REWRITE_FLAG_GOTO: /* 'G' */
/*
* After applying rule, jump N rules
*/
case REWRITE_FLAG_USER: { /* 'U' */
/*
* After applying rule, return user-defined
* error code
*/
char buf[16], *q;
size_t l;
int *d;
......@@ -228,7 +234,7 @@ rewrite_rule_compile(
return REWRITE_ERR;
}
l = q - p + 2;
l = q - p + 1;
if ( l >= sizeof( buf ) ) {
/* XXX Need to free stuff */
return REWRITE_ERR;
......@@ -248,7 +254,19 @@ rewrite_rule_compile(
/* cleanup ... */
return REWRITE_ERR;
}
action->la_type = REWRITE_ACTION_GOTO;
switch ( p[ 0 ] ) {
case REWRITE_FLAG_GOTO:
action->la_type = REWRITE_ACTION_GOTO;
break;
case REWRITE_FLAG_USER:
action->la_type = REWRITE_ACTION_USER;
break;
default:
assert(0);
}
action->la_args = (void *)d;
p = q; /* p is incremented by the for ... */
......@@ -283,11 +301,7 @@ rewrite_rule_compile(
* Stupid way to append to a list ...
*/
if ( action != NULL ) {
if ( first_action == NULL ) {
first_action = action;
} else {
append_action( first_action, action );
}
append_action( &first_action, action );
action = NULL;
}
}
......
......@@ -27,7 +27,7 @@
struct rewrite_subst *
rewrite_subst_compile(
struct rewrite_info *info,
const char *result
const char *str
)
{
size_t subs_len;
......@@ -36,11 +36,16 @@ rewrite_subst_compile(
struct rewrite_subst *s = NULL;
const char *begin, *p;
char *result, *begin, *p;
int nsub = 0, l;
assert( info != NULL );
assert( result != NULL );
assert( str != NULL );
result = strdup( str );
if ( result == NULL ) {
return NULL;
}
/*
* Take care of substitution string
......@@ -63,8 +68,7 @@ rewrite_subst_compile(
tmps = ( struct berval * )realloc( subs,
sizeof( struct berval )*( nsub + 1 ) );
if ( tmps == NULL ) {
/* FIXME: cleanup */
return NULL;
goto cleanup;
}
subs = tmps;
......@@ -78,7 +82,7 @@ rewrite_subst_compile(
subs[ nsub ].bv_len = l;
subs[ nsub ].bv_val = malloc( l + 1 );
if ( subs[ nsub ].bv_val == NULL ) {
return NULL;
goto cleanup;
}
AC_MEMCPY( subs[ nsub ].bv_val, begin, l );
subs[ nsub ].bv_val[ l ] = '\0';
......@@ -101,8 +105,7 @@ rewrite_subst_compile(
tmpsm = ( struct rewrite_submatch * )realloc( submatch,
sizeof( struct rewrite_submatch )*( nsub + 1 ) );
if ( tmpsm == NULL ) {
/* cleanup */
return NULL;
goto cleanup;
}
submatch = tmpsm;
submatch[ nsub ].ls_submatch = d;
......@@ -124,10 +127,9 @@ rewrite_subst_compile(
REWRITE_SUBMATCH_XMAP;
map = rewrite_xmap_parse( info,
p + 3, &begin );
p + 3, (const char **)&begin );
if ( map == NULL ) {
/* cleanup */
return NULL;
goto cleanup;
}
submatch[ nsub ].ls_map = map;
p = begin - 1;
......@@ -140,10 +142,10 @@ rewrite_subst_compile(
struct rewrite_map *map;
struct rewrite_submatch *tmpsm;
map = rewrite_map_parse( info, p + 2, &begin );
map = rewrite_map_parse( info, p + 2,
(const char **)&begin );
if ( map == NULL ) {
/* cleanup */
return NULL;
goto cleanup;
}
p = begin - 1;
......@@ -153,8 +155,7 @@ rewrite_subst_compile(
tmpsm = ( struct rewrite_submatch * )realloc( submatch,
sizeof( struct rewrite_submatch )*( nsub + 1 ) );
if ( tmpsm == NULL ) {
/* cleanup */
return NULL;
goto cleanup;
}
submatch = tmpsm;
submatch[ nsub ].ls_type =
......@@ -169,7 +170,7 @@ rewrite_subst_compile(
continue;
} else {
return NULL;
goto cleanup;
}
nsub++;
......@@ -184,7 +185,7 @@ rewrite_subst_compile(
* XXX need to free the value subst stuff!
*/
free( subs );
return NULL;
goto cleanup;
}
subs = tmps;
l = p - begin;
......@@ -201,8 +202,7 @@ rewrite_subst_compile(
s = calloc( sizeof( struct rewrite_subst ), 1 );
if ( s == NULL ) {
/* cleanup */
return NULL;
goto cleanup;
}
s->lt_subs_len = subs_len;
......@@ -210,6 +210,9 @@ rewrite_subst_compile(
s->lt_num_submatch = nsub;
s->lt_submatch = submatch;
cleanup:;
free( result );
return s;
}
......@@ -331,7 +334,6 @@ rewrite_subst_apply(
}
if ( rc != REWRITE_SUCCESS ) {
rc = REWRITE_REGEXEC_ERR;
goto cleanup;
}
break;
......@@ -382,7 +384,7 @@ rewrite_subst_apply(
rc = REWRITE_ERR;
break;
}
if ( rc != REWRITE_SUCCESS ) {
rc = REWRITE_REGEXEC_ERR;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment