1 /******************************************************************************
3 * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
6 * Permission is granted to anyone to use this software for any purpose
7 * on any computer system, and to alter it and redistribute it, subject
8 * to the following restrictions:
10 * 1. The author is not responsible for the consequences of use of this
11 * software, no matter how awful, even if they arise from flaws in it.
13 * 2. The origin of this software must not be misrepresented, either by
14 * explicit claim or by omission. Since few users ever read sources,
15 * credits should appear in the documentation.
17 * 3. Altered versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software. Since few users
19 * ever read sources, credits should appear in the documentation.
21 * 4. This notice may not be removed or altered.
23 ******************************************************************************/
27 #include <ac/string.h>
29 #include "rewrite-int.h"
36 * This becomes the running context for subsequent calls to
37 * rewrite_parse; it can be altered only by a
38 * rewriteContext config line or by a change in info.
40 struct rewrite_context *__curr_context = NULL;
50 struct rewrite_info *info;
51 struct rewrite_context *context;
54 case REWRITE_MODE_ERR:
56 case REWRITE_MODE_COPY_INPUT:
57 case REWRITE_MODE_USE_DEFAULT:
60 mode = REWRITE_MODE_USE_DEFAULT;
66 * Resets the running context for parsing ...
68 __curr_context = NULL;
70 info = calloc( sizeof( struct rewrite_info ), 1 );
75 info->li_state = REWRITE_DEFAULT;
76 info->li_max_passes = REWRITE_MAX_PASSES;
77 info->li_rewrite_mode = mode;
80 * Add the default (empty) rule
82 context = rewrite_context_create( info, REWRITE_DEFAULT_CONTEXT );
83 if ( context == NULL ) {
88 #ifdef USE_REWRITE_LDAP_PVT_THREADS
89 if ( ldap_pvt_thread_rdwr_init( &info->li_cookies_mutex ) ) {
93 if ( ldap_pvt_thread_rdwr_init( &info->li_params_mutex ) ) {
97 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
103 * Cleans up the info structure
107 struct rewrite_info *info
110 assert( info != NULL );
112 rewrite_session_destroy( info );
114 rewrite_param_destroy( info );
116 #ifdef USE_REWRITE_LDAP_PVT_THREADS
117 ldap_pvt_thread_rdwr_destroy( &info->li_cookies_mutex );
118 ldap_pvt_thread_rdwr_destroy( &info->li_params_mutex );
119 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
121 return REWRITE_SUCCESS;
125 * Rewrites a string according to context.
126 * If the engine is off, OK is returned, but the return string will be NULL.
127 * In case of 'unwilling to perform', UNWILLING is returned, and the
128 * return string will also be null. The same in case of error.
129 * Otherwise, OK is returned, and result will hold a newly allocated string
130 * with the rewriting.
132 * What to do in case of non-existing rewrite context is still an issue.
133 * Four possibilities:
135 * - ok with NULL result,
136 * - ok with copy of string as result,
137 * - use the default rewrite context.
141 struct rewrite_info *info,
142 const char *rewriteContext,
147 return rewrite_session( info, rewriteContext,
148 string, NULL, result );
153 struct rewrite_info *info,
154 const char *rewriteContext,
160 struct rewrite_context *context;
161 struct rewrite_op op = { 0, 0, NULL, NULL, NULL, NULL };
164 assert( info != NULL );
165 assert( rewriteContext != NULL );
166 assert( string != NULL );
167 assert( result != NULL );
170 * cookie can be null; means: don't care about session stuff
174 op.lo_cookie = cookie;
177 * Engine not on means no failure, but explicit no rewriting
179 if ( info->li_state != REWRITE_ON ) {
180 rc = REWRITE_REGEXEC_OK;
185 * Undefined context means no rewriting also
186 * (conservative, are we sure it's what we want?)
188 context = rewrite_context_find( info, rewriteContext );
189 if ( context == NULL ) {
190 switch ( info->li_rewrite_mode ) {
191 case REWRITE_MODE_ERR:
192 rc = REWRITE_REGEXEC_ERR;
194 case REWRITE_MODE_OK:
195 rc = REWRITE_REGEXEC_OK;
197 case REWRITE_MODE_COPY_INPUT:
198 *result = strdup( string );
199 rc = REWRITE_REGEXEC_OK;
201 case REWRITE_MODE_USE_DEFAULT:
202 context = rewrite_context_find( info,
203 REWRITE_DEFAULT_CONTEXT );
208 op.lo_string = strdup( string );
209 if ( op.lo_string == NULL ) {
210 rc = REWRITE_REGEXEC_ERR;
215 * Applies rewrite context
217 rc = rewrite_context_apply(info, &op, context, string, result );
218 assert( op.lo_depth == 0 );
221 free( op.lo_string );
227 case REWRITE_REGEXEC_OK:
228 case REWRITE_REGEXEC_STOP:
230 * If rewrite succeeded return OK regardless of how
231 * the successful rewriting was obtained!
233 rc = REWRITE_REGEXEC_OK;
238 * Internal or forced error, return = NULL; rc already OK.
240 case REWRITE_REGEXEC_UNWILLING:
241 case REWRITE_REGEXEC_ERR:
243 if ( *result != NULL ) {
251 rewrite_var_delete( op.lo_vars );