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"
30 * Compares two cookies
38 struct rewrite_session *s1, *s2;
40 s1 = ( struct rewrite_session * )c1;
41 s2 = ( struct rewrite_session * )c2;
45 assert( s1->ls_cookie != NULL );
46 assert( s2->ls_cookie != NULL );
48 return ( ( s1->ls_cookie < s2->ls_cookie ) ? -1 :
49 ( ( s1->ls_cookie > s2->ls_cookie ) ? 1 : 0 ) );
61 struct rewrite_session *s1, *s2;
63 s1 = ( struct rewrite_session * )c1;
64 s2 = ( struct rewrite_session * )c2;
68 assert( s1->ls_cookie != NULL );
69 assert( s2->ls_cookie != NULL );
71 return ( ( s1->ls_cookie == s2->ls_cookie ) ? -1 : 0 );
77 struct rewrite_session *
79 struct rewrite_info *info,
83 struct rewrite_session *session;
86 assert( info != NULL );
87 assert( cookie != NULL );
89 session = calloc( sizeof( struct rewrite_session ), 1 );
90 if ( session == NULL ) {
93 session->ls_cookie = ( void * )cookie;
95 #ifdef USE_REWRITE_LDAP_PVT_THREADS
96 if ( ldap_pvt_thread_rdwr_init( &session->ls_vars_mutex ) ) {
100 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
101 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
103 rc = avl_insert( &info->li_cookies, ( caddr_t )session,
104 rewrite_cookie_cmp, rewrite_cookie_dup );
105 info->li_num_cookies++;
107 #ifdef USE_REWRITE_LDAP_PVT_THREADS
108 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
109 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
122 struct rewrite_session *
123 rewrite_session_find(
124 struct rewrite_info *info,
128 struct rewrite_session *session, tmp;
130 assert( info != NULL );
131 assert( cookie != NULL );
133 tmp.ls_cookie = ( void * )cookie;
134 #ifdef USE_REWRITE_LDAP_PVT_THREADS
135 ldap_pvt_thread_rdwr_rlock( &info->li_cookies_mutex );
136 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
137 session = ( struct rewrite_session * )avl_find( info->li_cookies,
138 ( caddr_t )&tmp, rewrite_cookie_cmp );
139 #ifdef USE_REWRITE_LDAP_PVT_THREADS
140 ldap_pvt_thread_rdwr_runlock( &info->li_cookies_mutex );
141 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
148 * Defines and inits a var with session scope
151 rewrite_session_var_set(
152 struct rewrite_info *info,
158 struct rewrite_session *session;
159 struct rewrite_var *var;
161 assert( info != NULL );
162 assert( cookie != NULL );
163 assert( name != NULL );
164 assert( value != NULL );
166 session = rewrite_session_find( info, cookie );
167 if ( session == NULL ) {
168 session = rewrite_session_init( info, cookie );
171 #ifdef USE_REWRITE_LDAP_PVT_THREADS
172 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
173 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
175 var = rewrite_var_find( session->ls_vars, name );
177 assert( var->lv_value.bv_val != NULL );
178 free( var->lv_value.bv_val );
179 var->lv_value.bv_val = strdup( value );
180 var->lv_value.bv_len = strlen( value );
182 var = rewrite_var_insert( &session->ls_vars, name, value );
184 #ifdef USE_REWRITE_LDAP_PVT_THREADS
185 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
186 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
191 #ifdef USE_REWRITE_LDAP_PVT_THREADS
192 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
193 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
195 return REWRITE_SUCCESS;
199 * Gets a var with session scope
202 rewrite_session_var_get(
203 struct rewrite_info *info,
209 struct rewrite_session *session;
210 struct rewrite_var *var;
212 assert( info != NULL );
213 assert( cookie != NULL );
214 assert( name != NULL );
215 assert( value != NULL );
217 value->bv_val = NULL;
220 if ( cookie == NULL ) {
224 session = rewrite_session_find( info, cookie );
225 if ( session == NULL ) {
229 #ifdef USE_REWRITE_LDAP_PVT_THREADS
230 ldap_pvt_thread_rdwr_rlock( &session->ls_vars_mutex );
231 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
233 var = rewrite_var_find( session->ls_vars, name );
236 #ifdef USE_REWRITE_LDAP_PVT_THREADS
237 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
238 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
242 value->bv_val = strdup( var->lv_value.bv_val );
243 value->bv_len = var->lv_value.bv_len;
246 #ifdef USE_REWRITE_LDAP_PVT_THREADS
247 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
248 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
250 return REWRITE_SUCCESS;
257 rewrite_session_delete(
258 struct rewrite_info *info,
262 struct rewrite_session *session, tmp;
264 assert( info != NULL );
265 assert( cookie != NULL );
267 tmp.ls_cookie = ( void * )cookie;
269 session = rewrite_session_find( info, cookie );
271 if ( session != NULL ) {
272 #ifdef USE_REWRITE_LDAP_PVT_THREADS
273 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
274 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
275 rewrite_var_delete( session->ls_vars );
276 #ifdef USE_REWRITE_LDAP_PVT_THREADS
277 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
278 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
281 #ifdef USE_REWRITE_LDAP_PVT_THREADS
282 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
283 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
285 assert( info->li_num_cookies > 0 );
286 info->li_num_cookies--;
289 * There is nothing to delete in the return value
291 avl_delete( &info->li_cookies, ( caddr_t )&tmp, rewrite_cookie_cmp );
292 #ifdef USE_REWRITE_LDAP_PVT_THREADS
293 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
294 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
296 return REWRITE_SUCCESS;
300 * Destroys the cookie tree
303 rewrite_session_destroy(
304 struct rewrite_info *info
309 assert( info != NULL );
311 #ifdef USE_REWRITE_LDAP_PVT_THREADS
312 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
313 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
316 * Should call per-session destruction routine ...
319 count = avl_free( info->li_cookies, NULL );
320 info->li_cookies = NULL;
321 assert( count == info->li_num_cookies );
322 info->li_num_cookies = 0;
324 #ifdef USE_REWRITE_LDAP_PVT_THREADS
325 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
326 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
328 return REWRITE_SUCCESS;