mod_session.c revision 5ae15cd9d22fb3bdfd2eb0b9761c4ef07fbf2f96
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* Licensed to the Apache Software Foundation (ASF) under one or more
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * contributor license agreements. See the NOTICE file distributed with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * this work for additional information regarding copyright ownership.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * The ASF licenses this file to You under the Apache License, Version 2.0
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * (the "License"); you may not use this file except in compliance with
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * the License. You may obtain a copy of the License at
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Unless required by applicable law or agreed to in writing, software
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * distributed under the License is distributed on an "AS IS" BASIS,
2e545ce2450a9953665f701bb05350f0d3f26275nd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * limitations under the License.
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluemAPR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ap, SESSION, int, session_load,
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem (request_rec * r, session_rec ** z), (r, z), DECLINED)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPR_IMPLEMENT_EXTERNAL_HOOK_RUN_FIRST(ap, SESSION, int, session_save,
3f08db06526d6901aa08c110b5bc7dde6bc39905ndAPR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ap, SESSION, int, session_encode,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd (request_rec * r, session_rec * z), (r, z), OK, DECLINED)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPR_IMPLEMENT_EXTERNAL_HOOK_RUN_ALL(ap, SESSION, int, session_decode,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd (request_rec * r, session_rec * z), (r, z), OK, DECLINED)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int session_identity_encode(request_rec * r, session_rec * z);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int session_identity_decode(request_rec * r, session_rec * z);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Should the session be included within this URL.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * This function tests whether a session is valid for this URL. It uses the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * include and exclude arrays to determine whether they should be included.
71fccc298df6a1540d408151a26aa22beed55d0bndstatic int session_included(request_rec * r, session_dir_conf * conf)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd const char **includes = (const char **) conf->includes->elts;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd const char **excludes = (const char **) conf->excludes->elts;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd for (i = 0; !included && i < conf->includes->nelts; i++) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd const char *include = includes[i];
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (strncmp(r->parsed_uri.path, include, strlen(include))) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd for (i = 0; included && i < conf->includes->nelts; i++) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (strncmp(r->parsed_uri.path, exclude, strlen(exclude))) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Load the session.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * If the session doesn't exist, a blank one will be created.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param r The request
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param z A pointer to where the session will be written.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int ap_session_load(request_rec * r, session_rec ** z)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd session_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* is the session enabled? */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* should the session be loaded at all? */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* load the session from the session hook */
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "session is enabled but no session modules have been configured, "
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "error while loading the session, "
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* found a session that hasn't expired? */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* no luck, create a blank session */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd zz = (session_rec *) apr_pcalloc(r->pool, sizeof(session_rec));
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd zz->uuid = (apr_uuid_t *) apr_pcalloc(zz->pool, sizeof(apr_uuid_t));
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "error while decoding the session, "
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* make sure the expiry is set, if present */
b3c7a2279fa7a45f5807d9a404760b9b3760df50nd * Save the session.
316f02e3836836c82e19019ff23f90a7ebc65289nilgun * In most implementations the session is only saved if the dirty flag is
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * true. This prevents the session being saved unnecessarily.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param r The request
22d5d84393d960a2027f472036f3fee15d7dbce9nd * @param z A pointer to where the session will be written.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int ap_session_save(request_rec * r, session_rec * z)
623eebe956d9c2d6d073ed3eae855b56030b40e9noodl session_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
909ce17e2bd0faef7b1c294f2307f009793fd493nd /* sanity checks, should we try save at all? */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, SESSION_PREFIX
cd34a6fbf0a2619544a72eadb73f309370bf6682wrowe "attempt made to save the session twice, "
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "attempt made to save a session when the session had already expired, "
08cf4a15275e4cb65a424b3a1db5410bfb51085cjim /* reset the expiry back to maxage, if the expiry is present */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* encode the session */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "error while encoding the session, "
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* try the save */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "session is enabled but no session modules have been configured, "
5effc8b39fae5cd169d17f342bfc265705840014rbowen ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, SESSION_PREFIX
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "error while saving the session, "
c819c19c2f1ffbf3a3f12a4070cc6c3f4ea2a6f2sf * Get a particular value from the session.
d2b809e5d72658bff23819d8b77f20e4939af541nd * @param r The current request.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param z The current session. If this value is NULL, the session will be
a4687128d43515b5791a6efc6ba55314478f9552jim * looked up in the request, created if necessary, and saved to the request
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param key The key to get.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param value The buffer to write the value to.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic void ap_session_get(request_rec * r, session_rec * z, const char *key, const char **value)
a43bfa789f4e52dde53ae8e53fa0427b5c1cf977nd if (z && z->entries) {
4126704c4950bfd46d32ad54e3b106ac6d868a73sf * Set a particular value to the session.
0a69d9bb491d9810892a9949c01403a1de3c7ac2nd * Using this method ensures that the dirty flag is set correctly, so that
245eb3a84b8bc2b350c3a39ba2599444d443b50ajim * the session can be saved efficiently.
4ed26c413f67a5aae20b95909828f30bb5dc2286poirier * @param r The current request.
6d20aeba2c4aa0938bc6e0659d13adc7670ff421poirier * @param z The current session. If this value is NULL, the session will be
5f4e50966b2b9b58436a1651cbe588d1b595657ewrowe * looked up in the request, created if necessary, and saved to the request
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi * @param key The key to set. The existing key value will be replaced.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * @param value The value to set.
f772e8f448c223e5ea306f1bf92d97d968f972d5jimstatic void ap_session_set(request_rec * r, session_rec * z,
4044e4b6cb07cf7fa8e90676fafffe543c1d439bjimstatic int identity_count(int *count, const char *key, const char *val)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int identity_concat(char *buffer, const char *key, const char *val)
71fccc298df6a1540d408151a26aa22beed55d0bnd * Default identity encoding for the session.
71fccc298df6a1540d408151a26aa22beed55d0bnd * By default, the name value pairs in the session are URLEncoded, separated
71fccc298df6a1540d408151a26aa22beed55d0bnd * by equals, and then in turn separated by ampersand, in the format of an
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * html form.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * This was chosen to make it easy for external code to unpack a session,
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * should there be a need to do so.
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi * @param r The request pointer.
c04f76acce77126cf88b09350e56ea8c6b4a064enilgun * @param z A pointer to where the session will be written.
63f06dce77bb2d9b1c5aa5deeb47a1069987fd1endstatic int session_identity_encode(request_rec * r, session_rec * z)
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh char *expiry = apr_psprintf(r->pool, "%" APR_INT64_T_FMT, z->expiry);
727872d18412fc021f03969b8641810d8896820bhumbedooh apr_table_do((int (*) (void *, const char *, const char *))
0d0ba3a410038e179b695446bb149cce6264e0abnd apr_table_do((int (*) (void *, const char *, const char *))
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh * Default identity decoding for the session.
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen * By default, the name value pairs in the session are URLEncoded, separated
0d0ba3a410038e179b695446bb149cce6264e0abnd * by equals, and then in turn separated by ampersand, in the format of an
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd * html form.
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd * This was chosen to make it easy for external code to unpack a session,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * should there be a need to do so.
if (!z->encoded) {
return OK;
return OK;
ap_session_load(r, &z);
if (!z || z->written) {
r = r->next;
if (!override) {
if (override) {
session_identity_decode(r, z);
ap_session_save(r, z);
r = r->next;
ap_session_load(r, &z);
session_identity_encode(r, z);
if (z->encoded) {
return OK;
return (void *) new;
return new;
return NULL;
return NULL;
return NULL;
return NULL;
*new = f;
return NULL;
*new = f;
return NULL;
{NULL}