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"
32 * Compares two cookies
40 struct rewrite_session *s1, *s2;
42 s1 = ( struct rewrite_session * )c1;
43 s2 = ( struct rewrite_session * )c2;
47 assert( s1->ls_cookie != NULL );
48 assert( s2->ls_cookie != NULL );
50 return ( ( s1->ls_cookie < s2->ls_cookie ) ? -1 :
51 ( ( s1->ls_cookie > s2->ls_cookie ) ? 1 : 0 ) );
63 struct rewrite_session *s1, *s2;
65 s1 = ( struct rewrite_session * )c1;
66 s2 = ( struct rewrite_session * )c2;
70 assert( s1->ls_cookie != NULL );
71 assert( s2->ls_cookie != NULL );
73 return ( ( s1->ls_cookie == s2->ls_cookie ) ? -1 : 0 );
79 struct rewrite_session *
81 struct rewrite_info *info,
85 struct rewrite_session *session;
88 assert( info != NULL );
89 assert( cookie != NULL );
91 session = calloc( sizeof( struct rewrite_session ), 1 );
92 if ( session == NULL ) {
95 session->ls_cookie = ( void * )cookie;
97 #ifdef USE_REWRITE_LDAP_PVT_THREADS
98 if ( ldap_pvt_thread_rdwr_init( &session->ls_vars_mutex ) ) {
102 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
103 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
105 rc = avl_insert( &info->li_cookies, ( caddr_t )session,
106 rewrite_cookie_cmp, rewrite_cookie_dup );
107 info->li_num_cookies++;
109 #ifdef USE_REWRITE_LDAP_PVT_THREADS
110 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
111 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
124 struct rewrite_session *
125 rewrite_session_find(
126 struct rewrite_info *info,
130 struct rewrite_session *session, tmp;
132 assert( info != NULL );
133 assert( cookie != NULL );
135 tmp.ls_cookie = ( void * )cookie;
136 #ifdef USE_REWRITE_LDAP_PVT_THREADS
137 ldap_pvt_thread_rdwr_rlock( &info->li_cookies_mutex );
138 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
139 session = ( struct rewrite_session * )avl_find( info->li_cookies,
140 ( caddr_t )&tmp, rewrite_cookie_cmp );
141 #ifdef USE_REWRITE_LDAP_PVT_THREADS
142 ldap_pvt_thread_rdwr_runlock( &info->li_cookies_mutex );
143 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
150 * Defines and inits a var with session scope
153 rewrite_session_var_set(
154 struct rewrite_info *info,
160 struct rewrite_session *session;
161 struct rewrite_var *var;
163 assert( info != NULL );
164 assert( cookie != NULL );
165 assert( name != NULL );
166 assert( value != NULL );
168 session = rewrite_session_find( info, cookie );
169 if ( session == NULL ) {
170 session = rewrite_session_init( info, cookie );
173 #ifdef USE_REWRITE_LDAP_PVT_THREADS
174 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
175 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
177 var = rewrite_var_find( session->ls_vars, name );
179 assert( var->lv_value.bv_val != NULL );
180 free( var->lv_value.bv_val );
181 var->lv_value.bv_val = strdup( value );
182 var->lv_value.bv_len = strlen( value );
184 var = rewrite_var_insert( &session->ls_vars, name, value );
186 #ifdef USE_REWRITE_LDAP_PVT_THREADS
187 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
188 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
193 #ifdef USE_REWRITE_LDAP_PVT_THREADS
194 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
195 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
197 return REWRITE_SUCCESS;
201 * Gets a var with session scope
204 rewrite_session_var_get(
205 struct rewrite_info *info,
211 struct rewrite_session *session;
212 struct rewrite_var *var;
214 assert( info != NULL );
215 assert( cookie != NULL );
216 assert( name != NULL );
217 assert( value != NULL );
219 value->bv_val = NULL;
222 if ( cookie == NULL ) {
226 session = rewrite_session_find( info, cookie );
227 if ( session == NULL ) {
231 #ifdef USE_REWRITE_LDAP_PVT_THREADS
232 ldap_pvt_thread_rdwr_rlock( &session->ls_vars_mutex );
233 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
235 var = rewrite_var_find( session->ls_vars, name );
238 #ifdef USE_REWRITE_LDAP_PVT_THREADS
239 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
240 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
244 value->bv_val = strdup( var->lv_value.bv_val );
245 value->bv_len = var->lv_value.bv_len;
248 #ifdef USE_REWRITE_LDAP_PVT_THREADS
249 ldap_pvt_thread_rdwr_runlock( &session->ls_vars_mutex );
250 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
252 return REWRITE_SUCCESS;
259 rewrite_session_delete(
260 struct rewrite_info *info,
264 struct rewrite_session *session, tmp;
266 assert( info != NULL );
267 assert( cookie != NULL );
269 tmp.ls_cookie = ( void * )cookie;
271 session = rewrite_session_find( info, cookie );
273 if ( session != NULL ) {
274 #ifdef USE_REWRITE_LDAP_PVT_THREADS
275 ldap_pvt_thread_rdwr_wlock( &session->ls_vars_mutex );
276 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
277 rewrite_var_delete( session->ls_vars );
278 #ifdef USE_REWRITE_LDAP_PVT_THREADS
279 ldap_pvt_thread_rdwr_wunlock( &session->ls_vars_mutex );
280 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
283 #ifdef USE_REWRITE_LDAP_PVT_THREADS
284 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
285 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
287 assert( info->li_num_cookies > 0 );
288 info->li_num_cookies--;
291 * There is nothing to delete in the return value
293 avl_delete( &info->li_cookies, ( caddr_t )&tmp, rewrite_cookie_cmp );
294 #ifdef USE_REWRITE_LDAP_PVT_THREADS
295 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
296 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
298 return REWRITE_SUCCESS;
302 * Destroys the cookie tree
305 rewrite_session_destroy(
306 struct rewrite_info *info
311 assert( info != NULL );
313 #ifdef USE_REWRITE_LDAP_PVT_THREADS
314 ldap_pvt_thread_rdwr_wlock( &info->li_cookies_mutex );
315 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
318 * Should call per-session destruction routine ...
321 count = avl_free( info->li_cookies, NULL );
322 info->li_cookies = NULL;
323 assert( count == info->li_num_cookies );
324 info->li_num_cookies = 0;
326 #ifdef USE_REWRITE_LDAP_PVT_THREADS
327 ldap_pvt_thread_rdwr_wunlock( &info->li_cookies_mutex );
328 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
330 return REWRITE_SUCCESS;