2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
22 #include "rewrite-int.h"
25 * Compares two cookies
33 const struct rewrite_session *s1, *s2;
35 s1 = ( const struct rewrite_session * )c1;
36 s2 = ( const struct rewrite_session * )c2;
40 assert( s1->ls_cookie != NULL );
41 assert( s2->ls_cookie != NULL );
43 return ( ( s1->ls_cookie < s2->ls_cookie ) ? -1 :
44 ( ( s1->ls_cookie > s2->ls_cookie ) ? 1 : 0 ) );
56 struct rewrite_session *s1, *s2;
58 s1 = ( struct rewrite_session * )c1;
59 s2 = ( struct rewrite_session * )c2;
63 assert( s1->ls_cookie != NULL );
64 assert( s2->ls_cookie != NULL );
66 assert( s1->ls_cookie != s2->ls_cookie );
68 return ( ( s1->ls_cookie == s2->ls_cookie ) ? -1 : 0 );
74 struct rewrite_session *
76 struct rewrite_info *info,
80 struct rewrite_session *session, tmp;
83 assert( info != NULL );
84 assert( cookie != NULL );
86 #ifdef USE_REWRITE_LDAP_PVT_THREADS
87 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
88 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
90 tmp.ls_cookie = ( void * )cookie;
91 session = ( struct rewrite_session * )avl_find( info->li_cookies,
92 ( caddr_t )&tmp, rewrite_cookie_cmp );
95 #ifdef USE_REWRITE_LDAP_PVT_THREADS
96 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
97 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
101 session = calloc( sizeof( struct rewrite_session ), 1 );
102 if ( session == NULL ) {
105 session->ls_cookie = ( void * )cookie;
106 session->ls_count = 1;
108 #ifdef USE_REWRITE_LDAP_PVT_THREADS
109 if ( ldap_pvt_thread_mutex_init( &session->ls_mutex ) ) {
113 if ( ldap_pvt_thread_rdwr_init( &session->ls_vars_mutex ) ) {
114 ldap_pvt_thread_mutex_destroy( &session->ls_mutex );
118 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
120 rc = avl_insert( &info->li_cookies, ( caddr_t )session,
121 rewrite_cookie_cmp, rewrite_cookie_dup );
122 info->li_num_cookies++;
124 #ifdef USE_REWRITE_LDAP_PVT_THREADS
125 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
126 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
129 #ifdef USE_REWRITE_LDAP_PVT_THREADS
130 ldap_pvt_thread_rdwr_destroy( &session->ls_vars_mutex );
131 ldap_pvt_thread_mutex_destroy( &session->ls_mutex );
132 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
144 struct rewrite_session *
145 rewrite_session_find(
146 struct rewrite_info *info,
150 struct rewrite_session *session, tmp;
152 assert( info != NULL );
153 assert( cookie != NULL );
155 tmp.ls_cookie = ( void * )cookie;
156 #ifdef USE_REWRITE_LDAP_PVT_THREADS
157 ldap_pvt_thread_rdwr_rlock( &info->li_cookies_mutex );
158 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
159 session = ( struct rewrite_session * )avl_find( info->li_cookies,
160 ( caddr_t )&tmp, rewrite_cookie_cmp );
161 #ifdef USE_REWRITE_LDAP_PVT_THREADS
163 ldap_pvt_thread_mutex_lock( &session->ls_mutex );
165 ldap_pvt_thread_rdwr_runlock( &info->li_cookies_mutex );
166 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
175 rewrite_session_return(
176 struct rewrite_info *info,
177 struct rewrite_session *session
180 assert( session != NULL );
181 ldap_pvt_thread_mutex_unlock( &session->ls_mutex );
185 * Defines and inits a var with session scope
188 rewrite_session_var_set_f(
189 struct rewrite_info *info,
196 struct rewrite_session *session;
197 struct rewrite_var *var;
199 assert( info != NULL );
200 assert( cookie != NULL );
201 assert( name != NULL );
202 assert( value != NULL );
204 session = rewrite_session_find( info, cookie );
205 if ( session == NULL ) {
206 session = rewrite_session_init( info, cookie );
209 #ifdef USE_REWRITE_LDAP_PVT_THREADS
210 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
211 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
213 var = rewrite_var_find( session->ls_vars, name );
215 assert( var->lv_value.bv_val != NULL );
217 (void)rewrite_var_replace( var, value, flags );
220 var = rewrite_var_insert_f( &session->ls_vars, name, value, flags );
222 #ifdef USE_REWRITE_LDAP_PVT_THREADS
223 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
224 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
225 rewrite_session_return( info, session );
230 #ifdef USE_REWRITE_LDAP_PVT_THREADS
231 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
232 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
234 rewrite_session_return( info, session );
236 return REWRITE_SUCCESS;
240 * Gets a var with session scope
243 rewrite_session_var_get(
244 struct rewrite_info *info,
250 struct rewrite_session *session;
251 struct rewrite_var *var;
253 assert( info != NULL );
254 assert( cookie != NULL );
255 assert( name != NULL );
256 assert( value != NULL );
258 value->bv_val = NULL;
261 if ( cookie == NULL ) {
265 session = rewrite_session_find( info, cookie );
266 if ( session == NULL ) {
270 #ifdef USE_REWRITE_LDAP_PVT_THREADS
271 ldap_pvt_thread_rdwr_rlock( &session->ls_vars_mutex );
272 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
274 var = rewrite_var_find( session->ls_vars, name );
277 #ifdef USE_REWRITE_LDAP_PVT_THREADS
278 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
279 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
281 rewrite_session_return( info, session );
285 value->bv_val = strdup( var->lv_value.bv_val );
286 value->bv_len = var->lv_value.bv_len;
289 #ifdef USE_REWRITE_LDAP_PVT_THREADS
290 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
291 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
293 rewrite_session_return( info, session );
295 return REWRITE_SUCCESS;
302 rewrite_session_delete(
303 struct rewrite_info *info,
307 struct rewrite_session *session, tmp;
309 assert( info != NULL );
310 assert( cookie != NULL );
312 tmp.ls_cookie = ( void * )cookie;
314 session = rewrite_session_find( info, cookie );
316 if ( session == NULL ) {
317 return REWRITE_SUCCESS;
320 if ( --session->ls_count > 0 ) {
321 rewrite_session_return( info, session );
322 return REWRITE_SUCCESS;
325 #ifdef USE_REWRITE_LDAP_PVT_THREADS
326 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
327 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
329 rewrite_var_delete( session->ls_vars );
331 #ifdef USE_REWRITE_LDAP_PVT_THREADS
332 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
333 ldap_pvt_thread_rdwr_destroy( &session->ls_vars_mutex );
334 ldap_pvt_thread_mutex_unlock( &session->ls_mutex );
335 ldap_pvt_thread_mutex_destroy( &session->ls_mutex );
336 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
338 #ifdef USE_REWRITE_LDAP_PVT_THREADS
339 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
340 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
342 assert( info->li_num_cookies > 0 );
343 info->li_num_cookies--;
346 * There is nothing to delete in the return value
348 avl_delete( &info->li_cookies, ( caddr_t )&tmp, rewrite_cookie_cmp );
351 #ifdef USE_REWRITE_LDAP_PVT_THREADS
352 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
353 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
355 return REWRITE_SUCCESS;
359 * Destroys the cookie tree
362 rewrite_session_destroy(
363 struct rewrite_info *info
368 assert( info != NULL );
370 #ifdef USE_REWRITE_LDAP_PVT_THREADS
371 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
372 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
375 * Should call per-session destruction routine ...
378 count = avl_free( info->li_cookies, NULL );
379 info->li_cookies = NULL;
382 fprintf( stderr, "count = %d; num_cookies = %d\n",
383 count, info->li_num_cookies );
386 assert( count == info->li_num_cookies );
387 info->li_num_cookies = 0;
389 #ifdef USE_REWRITE_LDAP_PVT_THREADS
390 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
391 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
393 return REWRITE_SUCCESS;