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 "rewrite-int.h"
34 * This becomes the running context for subsequent calls to
35 * rewrite_parse; it can be altered only by a
36 * rewriteContext config line or by a change in info.
38 struct rewrite_context *__curr_context = NULL;
48 struct rewrite_info *info;
49 struct rewrite_context *context;
52 case REWRITE_MODE_ERR:
54 case REWRITE_MODE_COPY_INPUT:
55 case REWRITE_MODE_USE_DEFAULT:
58 mode = REWRITE_MODE_USE_DEFAULT;
64 * Resets the running context for parsing ...
66 __curr_context = NULL;
68 info = calloc( sizeof( struct rewrite_info ), 1 );
73 info->li_state = REWRITE_DEFAULT;
74 info->li_max_passes = REWRITE_MAX_PASSES;
75 info->li_rewrite_mode = mode;
78 * Add the default (empty) rule
80 context = rewrite_context_create( info, REWRITE_DEFAULT_CONTEXT );
81 if ( context == NULL ) {
86 #ifdef USE_REWRITE_LDAP_PVT_THREADS
87 if ( ldap_pvt_thread_rdwr_init( &info->li_cookies_mutex ) ) {
91 if ( ldap_pvt_thread_rdwr_init( &info->li_params_mutex ) ) {
95 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
101 * Cleans up the info structure
105 struct rewrite_info *info
108 assert( info != NULL );
110 rewrite_session_destroy( info );
112 rewrite_param_destroy( info );
114 #ifdef USE_REWRITE_LDAP_PVT_THREADS
115 ldap_pvt_thread_rdwr_destroy( &info->li_cookies_mutex );
116 ldap_pvt_thread_rdwr_destroy( &info->li_params_mutex );
117 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
119 return REWRITE_SUCCESS;
123 * Rewrites a string according to context.
124 * If the engine is off, OK is returned, but the return string will be NULL.
125 * In case of 'unwilling to perform', UNWILLING is returned, and the
126 * return string will also be null. The same in case of error.
127 * Otherwise, OK is returned, and result will hold a newly allocated string
128 * with the rewriting.
130 * What to do in case of non-existing rewrite context is still an issue.
131 * Four possibilities:
133 * - ok with NULL result,
134 * - ok with copy of string as result,
135 * - use the default rewrite context.
139 struct rewrite_info *info,
140 const char *rewriteContext,
145 return rewrite_session( info, rewriteContext,
146 string, NULL, result );
151 struct rewrite_info *info,
152 const char *rewriteContext,
158 struct rewrite_context *context;
159 struct rewrite_op op = { 0, 0, NULL, NULL, NULL, NULL };
162 assert( info != NULL );
163 assert( rewriteContext != NULL );
164 assert( string != NULL );
165 assert( result != NULL );
168 * cookie can be null; means: don't care about session stuff
172 op.lo_cookie = cookie;
175 * Engine not on means no failure, but explicit no rewriting
177 if ( info->li_state != REWRITE_ON ) {
178 rc = REWRITE_REGEXEC_OK;
183 * Undefined context means no rewriting also
184 * (conservative, are we sure it's what we want?)
186 context = rewrite_context_find( info, rewriteContext );
187 if ( context == NULL ) {
188 switch ( info->li_rewrite_mode ) {
189 case REWRITE_MODE_ERR:
190 rc = REWRITE_REGEXEC_ERR;
192 case REWRITE_MODE_OK:
193 rc = REWRITE_REGEXEC_OK;
195 case REWRITE_MODE_COPY_INPUT:
196 *result = strdup( string );
197 rc = REWRITE_REGEXEC_OK;
199 case REWRITE_MODE_USE_DEFAULT:
200 context = rewrite_context_find( info,
201 REWRITE_DEFAULT_CONTEXT );
206 op.lo_string = strdup( string );
207 if ( op.lo_string == NULL ) {
208 rc = REWRITE_REGEXEC_ERR;
213 * Applies rewrite context
215 rc = rewrite_context_apply(info, &op, context, string, result );
216 assert( op.lo_depth == 0 );
219 free( op.lo_string );
225 case REWRITE_REGEXEC_OK:
226 case REWRITE_REGEXEC_STOP:
228 * If rewrite succeeded return OK regardless of how
229 * the successful rewriting was obtained!
231 rc = REWRITE_REGEXEC_OK;
236 * Internal or forced error, return = NULL; rc already OK.
238 case REWRITE_REGEXEC_UNWILLING:
239 case REWRITE_REGEXEC_ERR:
241 if ( *result != NULL ) {
249 rewrite_var_delete( op.lo_vars );