OSDN Git Service

* for KANTAN LOGIN with Cookie
[modchxj/mod_chxj.git] / src / chxj_cookie.c
index e80710c..80eec5f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
+ * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -92,15 +92,16 @@ alloc_cookie_id(request_rec *r)
   memset(cookie_id, 0, APR_MD5_DIGESTSIZE+1);
   apr_base64_encode(cookie_id, (char*)md5_value, APR_MD5_DIGESTSIZE);
 
-  DBG(r, "cookie_id=[%s]", cookie_id);
+  DBG(r,"REQ[%X] cookie_id=[%s]", TO_ADDR(r),cookie_id);
 
   cookie_id = chxj_url_encode(r->pool,cookie_id);
 
-  DBG(r, "cookie_id=[%s]", cookie_id);
+  DBG(r, "REQ[%X] cookie_id=[%s]", TO_ADDR(r),cookie_id);
   return cookie_id;
 }
 
 
+
 /*
  *
  */
@@ -115,6 +116,7 @@ chxj_save_cookie(request_rec *r)
   char                *old_cookie_id;
   char                *store_string;
   mod_chxj_config     *dconf;
+  mod_chxj_req_config *req_conf;
   chxjconvrule_entry  *entryp;
   apr_table_t         *new_cookie_table;
   int                 has_cookie = 0;
@@ -126,7 +128,7 @@ chxj_save_cookie(request_rec *r)
   apr_pool_t          *pool;
 
 
-  DBG(r, "start chxj_save_cookie()");
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
 
   apr_pool_create(&pool, r->pool);
 
@@ -137,13 +139,22 @@ chxj_save_cookie(request_rec *r)
   has_refer = 0;
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
-  entryp = chxj_apply_convrule(r, dconf->convrules);
+  req_conf = chxj_get_req_config(r);
+  /*-------------------------------------------------------------------------*/
+  /* already setup entryp if request_conf->user_agent is not null            */
+  /*-------------------------------------------------------------------------*/
+  if (req_conf->user_agent) {
+    entryp = req_conf->entryp;
+  }
+  else {
+    entryp = chxj_apply_convrule(r, dconf->convrules);
+  }
   if (! entryp) {
-    DBG(r, "end chxj_save_cookie() no pattern");
+    DBG(r,"REQ[%X] end %s() no pattern",TO_ADDR(r),__func__);
     return NULL;
   }
-  if (! (entryp->action & CONVRULE_COOKIE_ON_BIT)) {
-    DBG(r, "end chxj_save_cookie() CookieOff");
+  if (! (entryp->action & CONVRULE_COOKIE_ON_BIT) && ! (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
+    DBG(r,"REQ[%X] end %s() CookieOff",TO_ADDR(r),__func__);
     return NULL;
   }
 
@@ -159,7 +170,7 @@ chxj_save_cookie(request_rec *r)
 
   for (ii=0; ii<headers->nelts; ii++) {
     if (strcasecmp(hentryp[ii].key, "Set-Cookie") == 0) {
-      DBG(r, "REQ[%X] cookie=[%s:%s]", (unsigned int)(apr_size_t)r, hentryp[ii].key, hentryp[ii].val);
+      DBG(r, "REQ[%X] cookie=[%s:%s]", TO_ADDR(r), hentryp[ii].key, hentryp[ii].val);
 
       char* key;
       char* val;
@@ -181,7 +192,7 @@ chxj_save_cookie(request_rec *r)
   }
   for (ii=0; ii<err_headers->nelts; ii++) {
     if (strcasecmp(err_hentryp[ii].key, "Set-Cookie") == 0) {
-      DBG(r, "REQ[%X] cookie=[%s:%s]", (unsigned int)(apr_size_t)r, err_hentryp[ii].key, err_hentryp[ii].val);
+      DBG(r, "REQ[%X] cookie=[%s:%s]", TO_ADDR(r), err_hentryp[ii].key, err_hentryp[ii].val);
 
       char* key;
       char* val;
@@ -201,8 +212,10 @@ chxj_save_cookie(request_rec *r)
       }
     }
   }
+#if 0
   apr_table_unset(r->headers_out, "Set-Cookie");
   apr_table_unset(r->err_headers_out, "Set-Cookie");
+#endif
 
   if (! has_refer) {
     apr_uri_parse(pool,r->uri, &parsed_uri);
@@ -217,7 +230,7 @@ chxj_save_cookie(request_rec *r)
       refer_string = apr_pstrcat(pool, refer_string, "?", r->args, NULL);
     }
     apr_table_setn(new_cookie_table, REFERER_COOKIE_KEY, refer_string);
-    DBG(r, "ADD REFER[%s]", refer_string);
+    DBG(r, "REQ[%X] ADD REFER[%s]", TO_ADDR(r),refer_string);
     has_cookie++;
   }
 
@@ -245,7 +258,7 @@ chxj_save_cookie(request_rec *r)
 
 
   if (! has_cookie) {
-    DBG(r, "REQ[%X] end chxj_save_cookie() (no cookie)", (unsigned int)(apr_size_t)r);
+    DBG(r,"REQ[%X] end %s() (no cookie)",TO_ADDR(r),__func__);
     return NULL;
   }
 
@@ -273,25 +286,28 @@ chxj_save_cookie(request_rec *r)
   }
 
   if (old_cookie_id && IS_COOKIE_LAZY(dconf)) {
-    DBG(r, "REQ[%X] LAZY COOKIE save",(unsigned int)(apr_size_t)r);
+    DBG(r, "REQ[%X] LAZY COOKIE save",TO_ADDR(r));
     cookie->cookie_id = apr_pstrdup(r->pool, old_cookie_id);
   }
   else if (old_cookie_id && apr_table_get(r->headers_in, "X-Chxj-Cookie-No-Update")) {
-    DBG(r, "REQ[%X] NO UPDATE MODE",(unsigned int)(apr_size_t)r);
+    DBG(r, "REQ[%X] NO UPDATE MODE",TO_ADDR(r));
     cookie->cookie_id = apr_pstrdup(r->pool, old_cookie_id);
   }
   else {
     if (old_cookie_id && apr_table_get(r->headers_in, "X-Chxj-Forward")) {
-      DBG(r, "REQ[%X] NO LAZY COOKIE(X-Chxj-Forward)  save",(unsigned int)(apr_size_t)r);
+      DBG(r, "REQ[%X] NO LAZY COOKIE(X-Chxj-Forward)  save",TO_ADDR(r));
       cookie->cookie_id = apr_pstrdup(r->pool, old_cookie_id);
     }
     else {
-      DBG(r, "REQ[%X] NO LAZY COOKIE save",(unsigned int)(apr_size_t)r);
+      DBG(r, "REQ[%X] NO LAZY COOKIE save",TO_ADDR(r));
       cookie->cookie_id = alloc_cookie_id(r);
     }
   }
 
-  DBG(r, "REQ[%X] TYPE:[%d]", (unsigned int)(apr_size_t)r, dconf->cookie_store_type);
+  DBG(r,"REQ[%X] TYPE:[%d]", TO_ADDR(r), dconf->cookie_store_type);
+  DBG(r,"REQ[%X] STORE STRING:=======================================================", TO_ADDR(r));
+  DBG(r,"REQ[%X] [%s]", TO_ADDR(r), store_string);
+  DBG(r,"REQ[%X] =======================================================:STORE STRING", TO_ADDR(r));
 
   {
     int done_proc = 0;
@@ -323,15 +339,17 @@ chxj_save_cookie(request_rec *r)
       }
     }
   }
+#if 0
   apr_table_unset(r->headers_out, "Set-Cookie");
   apr_table_unset(r->err_headers_out, "Set-Cookie");
+#endif
 
   if (cookie) {
     chxj_save_cookie_expire(r, cookie);
   }
 
 on_error:
-  DBG(r, "end chxj_save_cookie()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return cookie;
 }
 
@@ -348,13 +366,15 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
   apr_table_entry_t   *hentryp;
   char                *store_string;
   mod_chxj_config     *dconf;
+  mod_chxj_req_config *req_conf;
   chxjconvrule_entry  *entryp;
   cookie_t            *cookie;
 
 
-  DBG(r, "start chxj_update_cookie()");
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
   if (!old_cookie || ! old_cookie->cookie_headers || ! old_cookie->cookie_id) {
-    DBG(r, "end chxj_update_cookie() (old_cookie is null)");
+    DBG(r,"REQ[%X] old_cookie is null",TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return  NULL;
   }
 
@@ -362,13 +382,24 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
   cookie->cookie_id = NULL;
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
-  entryp = chxj_apply_convrule(r, dconf->convrules);
+  req_conf = chxj_get_req_config(r);
+  /*-------------------------------------------------------------------------*/
+  /* already setup entryp if request_conf->user_agent is not null            */
+  /*-------------------------------------------------------------------------*/
+  if (req_conf->user_agent) {
+    entryp = req_conf->entryp;
+  }
+  else {
+    entryp = chxj_apply_convrule(r, dconf->convrules);
+  }
   if (! entryp) {
-    DBG(r, "end chxj_update_cookie() no pattern");
+    DBG(r,"REQ[%X] no pattern",TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return NULL;
   }
   if (! (entryp->action & CONVRULE_COOKIE_ON_BIT)) {
-    DBG(r, "end chxj_update_cookie() CookieOff");
+    DBG(r,"REQ[%X] CookieOff",TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return NULL;
   }
 
@@ -379,11 +410,11 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
   chxj_delete_cookie_expire(r, old_cookie->cookie_id);
 
   if (IS_COOKIE_LAZY(dconf)) {
-    DBG(r, "LAZY MODE");
+    DBG(r,"REQ[%X] LAZY MODE",TO_ADDR(r));
     cookie->cookie_id = apr_pstrdup(r->pool, old_cookie->cookie_id);
   }
   else {
-    DBG(r, "NO LAZY MODE");
+    DBG(r,"REQ[%X] NO LAZY MODE",TO_ADDR(r));
     cookie->cookie_id = alloc_cookie_id(r);
   }
 
@@ -398,7 +429,7 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
                                "\n",
                                NULL);
 
-    DBG(r, "OLD COOKIE VALUE=[%s][%s]", hentryp[ii].key, hentryp[ii].val);
+    DBG(r,"REQ[%X] OLD COOKIE VALUE=[%s][%s]",TO_ADDR(r), hentryp[ii].key, hentryp[ii].val);
     store_string = apr_pstrcat(r->pool, 
                                store_string, 
                                hentryp[ii].key, 
@@ -412,7 +443,7 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
 #if defined(USE_MYSQL_COOKIE)
     if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
       if (!chxj_update_cookie_mysql(r, dconf, cookie->cookie_id, store_string)) {
-        ERR(r, "failed: chxj_update_cookie_mysql() cookie_id:[%s]", cookie->cookie_id);
+        ERR(r,"REQ[%X] failed: chxj_update_cookie_mysql() cookie_id:[%s]", TO_ADDR(r),cookie->cookie_id);
         goto on_error;
       }
       done_proc = 1;
@@ -422,7 +453,7 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
 #if defined(USE_MEMCACHE_COOKIE)
     if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
       if (! chxj_update_cookie_memcache(r, dconf, cookie->cookie_id, store_string)) {
-        ERR(r, "failed: chxj_update_cookie_memcache() cookie_id:[%s]", cookie->cookie_id);
+        ERR(r, "REQ[%X] failed: chxj_update_cookie_memcache() cookie_id:[%s]", TO_ADDR(r),cookie->cookie_id);
         goto on_error;
       }
       done_proc = 1;
@@ -430,7 +461,7 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
 #endif
     if (!done_proc || IS_COOKIE_STORE_DBM(dconf->cookie_store_type)) {
       if (! chxj_update_cookie_dbm(r, dconf, cookie->cookie_id, store_string)) {
-        ERR(r, "failed: chxj_update_cookie_dbm() cookie_id:[%s]", cookie->cookie_id);
+        ERR(r, "REQ[%X] failed: chxj_update_cookie_dbm() cookie_id:[%s]", TO_ADDR(r),cookie->cookie_id);
         goto on_error;
       }
     }
@@ -442,7 +473,7 @@ chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
 
 
 on_error:
-  DBG(r, "end   chxj_update_cookie()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return cookie;
 }
 
@@ -455,6 +486,7 @@ cookie_t *
 chxj_load_cookie(request_rec *r, char *cookie_id)
 {
   mod_chxj_config         *dconf;
+  mod_chxj_req_config     *req_conf;
   chxjconvrule_entry      *entryp;
   cookie_t                *cookie;
   apr_table_t             *load_cookie_table;
@@ -465,7 +497,8 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
   char                    *pair;
   char                    *header_cookie;
 
-  DBG(r, "start chxj_load_cookie() cookie_id=[%s]", cookie_id);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] cookie_id=[%s]", TO_ADDR(r), cookie_id);
   chxj_cookie_expire_gc(r);
 
   cookie = (cookie_t*)apr_palloc(r->pool, sizeof(cookie_t));
@@ -475,13 +508,24 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
 
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
-  entryp = chxj_apply_convrule(r, dconf->convrules);
+  req_conf = chxj_get_req_config(r);
+  /*-------------------------------------------------------------------------*/
+  /* already setup entryp if request_conf->user_agent is not null            */
+  /*-------------------------------------------------------------------------*/
+  if (req_conf->user_agent) {
+    entryp = req_conf->entryp;
+  }
+  else {
+    entryp = chxj_apply_convrule(r, dconf->convrules);
+  }
   if (! entryp) {
-    DBG(r, "end chxj_load_cookie() no pattern");
+    DBG(r,"REQ[%X] pattern", TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     goto on_error0;
   }
-  if (! (entryp->action & CONVRULE_COOKIE_ON_BIT)) {
-    DBG(r, "end chxj_load_cookie() CookieOff");
+  if (! (entryp->action & CONVRULE_COOKIE_ON_BIT) && ! (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
+    DBG(r,"REQ[%X] CookieOff", TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     goto on_error0;
   }
   load_cookie_table = apr_table_make(r->pool, 0);
@@ -491,7 +535,7 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
 #if defined(USE_MYSQL_COOKIE)
     if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
       if (! (load_string = chxj_load_cookie_mysql(r, dconf, cookie->cookie_id))) {
-        ERR(r, "%s:%d failed: chxj_load_cookie_mysql() cookie_id:[%s]", APLOG_MARK, cookie_id);
+        ERR(r, "REQ[%X] %s:%d failed: chxj_load_cookie_mysql() cookie_id:[%s]", TO_ADDR(r),APLOG_MARK, cookie_id);
         goto on_error0;
       }
       done_proc = 1;
@@ -500,7 +544,7 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
 #if defined(USE_MEMCACHE_COOKIE)
     if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
       if (! (load_string = chxj_load_cookie_memcache(r, dconf, cookie->cookie_id))) {
-        ERR(r, "%s:%d failed: chxj_load_cookie_memcache() cookie_id:[%s]", APLOG_MARK,cookie_id);
+        ERR(r, "REQ[%X] %s:%d failed: chxj_load_cookie_memcache() cookie_id:[%s]", TO_ADDR(r),APLOG_MARK,cookie_id);
         goto on_error0;
       }
       done_proc = 1;
@@ -508,14 +552,14 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
 #endif
     if (!done_proc || IS_COOKIE_STORE_DBM(dconf->cookie_store_type)) {
       if (! (load_string = chxj_load_cookie_dbm(r, dconf, cookie->cookie_id))) {
-        ERR(r, "%s:%d failed: chxj_load_cookie_dbm() cookie_id:[%s]", APLOG_MARK,cookie_id);
+        ERR(r, "REQ[%X] %s:%d failed: chxj_load_cookie_dbm() cookie_id:[%s]", TO_ADDR(r),APLOG_MARK,cookie_id);
         goto on_error0;
       }
     }
   }
 
   if (load_string) {
-    DBG(r, "load_string=[%s]", load_string);
+    DBG(r, "REQ[%X] load_string=[%s]", TO_ADDR(r), load_string);
     header_cookie = apr_palloc(r->pool, 1);
     header_cookie[0] = 0;
     for (;;) {
@@ -525,7 +569,7 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
       load_string = NULL;
       if (!pair) break;
 
-      DBG(r, "Cookie:[%s]", pair);
+      DBG(r,"REQ[%X] Cookie:[%s]", TO_ADDR(r), pair);
 
       tmp_pair = apr_pstrdup(r->pool, pair);
       val = strchr(tmp_pair, '=');
@@ -533,7 +577,7 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
         key = tmp_pair;
         *val++ = 0;
         apr_table_add(load_cookie_table, key, val);
-        DBG(r, "ADD key:[%s] val:[%s]", key, val);
+        DBG(r,"REQ[%X] ADD key:[%s] val:[%s]", TO_ADDR(r), key, val);
       }
       tmp_pair = apr_pstrdup(r->pool, pair);
       tmp_sem = strchr(tmp_pair, ';'); 
@@ -548,7 +592,7 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
       }
     }
     if (strlen(header_cookie)) {
-      DBG(r, "ADD COOKIE to REQUEST HEADER:[%s]", header_cookie);
+      DBG(r,"REQ[%X] ADD COOKIE to REQUEST HEADER:[%s]", TO_ADDR(r), header_cookie);
       apr_table_add(r->headers_in, "Cookie", header_cookie);
     }
   
@@ -566,13 +610,13 @@ chxj_load_cookie(request_rec *r, char *cookie_id)
     apr_table_setn(r->headers_in, "CHXJ_COOKIE_ID", cookie->cookie_id);
   }
 
-  DBG(r, "end   chxj_load_cookie()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return cookie;
 
 
 on_error0:
 
-  DBG(r, "end   chxj_load_cookie()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return NULL;
 }
 
@@ -588,7 +632,8 @@ check_valid_cookie_attribute(request_rec *r, const char *value)
   char *secure_pair;
   char *p;
 
-  DBG(r, "start check_valid_cookie_attribute() value:[%s]", value);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r),value);
 
   domain_pair = path_pair = expire_pair = secure_pair = NULL;
   p = apr_pstrdup(r->pool, value);
@@ -616,29 +661,34 @@ check_valid_cookie_attribute(request_rec *r, const char *value)
 
   if (domain_pair) {
     if (!valid_domain(r, domain_pair)) {
-      DBG(r, "invalid domain. domain_pair:[%s]", domain_pair);
+      DBG(r,"REQ[%X] invalid domain. domain_pair:[%s]", TO_ADDR(r),domain_pair);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return CHXJ_FALSE;
     }
   }
   if (path_pair) {
     if (!valid_path(r, path_pair)) {
-      DBG(r, "invalid path. path_pair:[%s]", path_pair);
+      DBG(r,"REQ[%X] invalid path. path_pair:[%s]", TO_ADDR(r),path_pair);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return CHXJ_FALSE;
     }
   }
   if (expire_pair) {
     if (!valid_expires(r, expire_pair)) {
-      DBG(r, "invalid expire. expire_pair:[%s]", expire_pair);
+      DBG(r,"REQ[%X] invalid expire. expire_pair:[%s]", TO_ADDR(r),expire_pair);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return CHXJ_FALSE;
     }
   }
   if (secure_pair) {
     if (!valid_secure(r, secure_pair)) {
-      DBG(r, "invalid secure. secure_pair:[%s]", secure_pair);
+      DBG(r,"REQ[%X] invalid secure. secure_pair:[%s]", TO_ADDR(r),secure_pair);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return CHXJ_FALSE;
     }
   }
-  DBG(r, "end check_valid_cookie_attribute() value:[%s]", value);
+  DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r),value);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return CHXJ_TRUE;
 }
 
@@ -652,7 +702,8 @@ check_valid_cookie_attribute_expires_only(request_rec *r, const char *value)
   char *expire_pair = NULL;
   char *p;
 
-  DBG(r, "REQ[%X] start check_valid_cookie_attribute_expires_only() value:[%s]", (unsigned int)(apr_size_t)r, value);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r), value);
 
   expire_pair = NULL;
   p = apr_pstrdup(r->pool, value);
@@ -671,11 +722,13 @@ check_valid_cookie_attribute_expires_only(request_rec *r, const char *value)
 
   if (expire_pair) {
     if (!valid_expires(r, expire_pair)) {
-      DBG(r, "REQ[%X] invalid expire. expire_pair:[%s]", (unsigned int)(apr_size_t)r, expire_pair);
+      DBG(r,"REQ[%X] invalid expire. expire_pair:[%s]", TO_ADDR(r), expire_pair);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return CHXJ_FALSE;
     }
   }
-  DBG(r, "REQ[%X] end check_valid_cookie_attribute_expires_only() value:[%s]", (unsigned int)(apr_size_t)r, value);
+  DBG(r,"REQ[%X] value:[%s]",TO_ADDR(r), value);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return CHXJ_TRUE;
 }
 
@@ -688,12 +741,18 @@ valid_domain(request_rec *r, const char *value)
   char *val;
   char *pstat;
   char *p = apr_pstrdup(r->pool, value);
-  const char *host = apr_table_get(r->headers_in, HTTP_HOST);
+  char *host = (char *)apr_table_get(r->headers_in, HTTP_HOST);
 
-  DBG(r, "start valid_domain() value:[%s]", value);
-  DBG(r, "host:[%s]", host);
-  if (!host)
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r), value);
+  if (!host) {
+    DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r), value);
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return CHXJ_TRUE;
+  }
+  DBG(r, "REQ[%X] host:[%s]", TO_ADDR(r), host);
+  host = s_cut_until_end_hostname(r, apr_pstrdup(r->pool, host));
+  DBG(r, "REQ[%X] host:[%s](after s_cut_until_end_hostname())", TO_ADDR(r), host);
 
   name = apr_strtok(p,"=", &pstat);
   name = qs_trim_string(r->pool, name);
@@ -702,11 +761,14 @@ valid_domain(request_rec *r, const char *value)
   len = strlen(host);
   if (len) {
     if (chxj_strcasenrcmp(r->pool, host, val, strlen(val))) {
-      DBG(r, "not match domain. host domain:[%s] vs value:[%s]", host, val);
+      DBG(r,"REQ[%X] not match domain. host domain:[%s] vs value:[%s]", TO_ADDR(r), host, val);
+      DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r), value);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return CHXJ_FALSE;
     }
   }
-  DBG(r, "end valid_domain() value:[%s]", value);
+  DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r), value);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return CHXJ_TRUE;
 }
 
@@ -721,7 +783,8 @@ valid_path(request_rec *r, const char *value)
   char *val;
   char *pstat;
 
-  DBG(r, "start valid_path() unparsed_uri:[%s] value:[%s]", r->unparsed_uri, value);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] unparsed_uri:[%s] value:[%s]", TO_ADDR(r),r->unparsed_uri, value);
   if (chxj_starts_with(r->unparsed_uri, "http://")) {
     uri = strchr(&r->unparsed_uri[sizeof("http://")], '/');
     if (uri != NULL) {
@@ -744,19 +807,21 @@ valid_path(request_rec *r, const char *value)
   if ((tmp = strchr(uri, '?'))) {
     *tmp = '\0';
   }
-  DBG(r, "uri=[%s]", uri);
+  DBG(r,"REQ[%X] uri=[%s]", TO_ADDR(r),uri);
   name = apr_strtok(p, "=", &pstat);
   val = apr_strtok(NULL, "=", &pstat);
   name = qs_trim_string(r->pool, name);
   val = qs_trim_string(r->pool, val);
-  DBG(r, "name=[%s] val=[%s]", name, val);
+  DBG(r,"REQ[%X] name=[%s] val=[%s]", TO_ADDR(r),name, val);
   
-  DBG(r, "val:[%s] vs uri:[%s]", val, uri);
+  DBG(r, "REQ[%X] val:[%s] vs uri:[%s]", TO_ADDR(r),val, uri);
   if (! chxj_starts_with(uri, val)) {
-    DBG(r, "end valid_path() unparsed_uri:[%s] value:[%s] (false)", r->unparsed_uri, value);
+    DBG(r,"REQ[%X] unparsed_uri:[%s] value:[%s] (false)",TO_ADDR(r), r->unparsed_uri, value);
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return CHXJ_FALSE;
   }
-  DBG(r, "end valid_path() unparsed_uri:[%s] value:[%s] (true)", r->unparsed_uri, value);
+  DBG(r,"REQ[%X] unparsed_uri:[%s] value:[%s] (true)", TO_ADDR(r),r->unparsed_uri, value);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return CHXJ_TRUE;
 }
 
@@ -771,18 +836,21 @@ valid_expires(request_rec *r, const char *value)
   apr_time_t expires;
   apr_time_t now;
 
-  DBG(r, "start valid_expire() value:[%s]", value);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] value:[%s]",TO_ADDR(r),value);
   name = apr_strtok(p, "=", &pstat);
   val  = apr_strtok(NULL, "=", &pstat);
-  DBG(r, "name=[%s] val=[%s]", name, val);
+  DBG(r,"REQ[%X] name=[%s] val=[%s]", TO_ADDR(r),name, val);
   now = apr_time_now();
   expires = chxj_parse_cookie_expires(val);
   if (expires < now) {
-    DBG(r, "end valid_expire() value:[%s] (expired)", value);
+    DBG(r,"REQ[%X] value:[%s] (expired)", TO_ADDR(r),value);
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return CHXJ_FALSE;
   }
   
-  DBG(r, "end valid_expire() value:[%s] (non expired)", value);
+  DBG(r,"REQ[%X] value:[%s] (non expired)", TO_ADDR(r),value);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return CHXJ_TRUE;
 }
 
@@ -791,13 +859,16 @@ static int
 valid_secure(request_rec *r, const char *value)
 {
   const char *scheme;
-  DBG(r, "start valid_secure() value:[%s]", value);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] value:[%s]", TO_ADDR(r),value);
   scheme = chxj_apache_run_http_scheme(r);
   if (strcasecmp("https", scheme)) {
-    DBG(r, "end valid_secure() value:[%s] (non secure)", value);
+    DBG(r,"REQ[%X] value:[%s] (non secure)", TO_ADDR(r),value);
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return CHXJ_FALSE;
   }
-  DBG(r, "end valid_secure() value:[%s] (secure)", value);
+  DBG(r,"REQ[%X] value:[%s] (secure)", TO_ADDR(r),value);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return CHXJ_TRUE;
 }
 
@@ -809,7 +880,8 @@ chxj_add_cookie_parameter(request_rec *r, char *value, cookie_t *cookie)
   char *dst;
   char *name = "";
 
-  DBG(r, "start chxj_add_cookie_parameter() cookie_id=[%s]", (cookie) ? cookie->cookie_id : NULL);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] cookie_id=[%s]", TO_ADDR(r), (cookie) ? cookie->cookie_id : NULL);
 
   dst = apr_pstrdup(r->pool, value);
 
@@ -820,7 +892,8 @@ chxj_add_cookie_parameter(request_rec *r, char *value, cookie_t *cookie)
     goto on_error;
 
   if (chxj_cookie_check_host(r, value) != 0) {
-    DBG(r, "end chxj_add_cookie_parameter()(check host)");
+    DBG(r,"REQ[%X] (check host)", TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     goto on_error;
   }
 
@@ -832,40 +905,53 @@ chxj_add_cookie_parameter(request_rec *r, char *value, cookie_t *cookie)
 
   qs = strchr(dst, '?');
   if (qs) {
+    char *sv_qs = qs;
+    qs = chxj_delete_chxj_cc_param(r, ++qs);
+    DBG(r, "REQ[%X] qs:[%s]",TO_ADDR(r), qs);
+    *sv_qs = 0;
+    if (qs && strlen(qs)) {
+      dst = apr_psprintf(r->pool, "%s?%s", dst, qs);
+    }
+  }
+  if (qs) {
     dst = apr_psprintf(r->pool, "%s&%s=%s%s", dst, CHXJ_COOKIE_PARAM, cookie->cookie_id, name);
   }
   else {
     dst = apr_psprintf(r->pool, "%s?%s=%s%s", dst, CHXJ_COOKIE_PARAM, cookie->cookie_id, name);
   }
 
-  DBG(r, "end   chxj_add_cookie_parameter() dst=[%s]", dst);
+  DBG(r,"REQ[%X] dst=[%s]", TO_ADDR(r), dst);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
 
   return dst;
 
 on_error:
-  DBG(r, "end   chxj_add_cookie_parameter() (on_error)");
+  DBG(r,"REQ[%X] (on_error)", TO_ADDR(r));
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return dst;
 }
 
 
 char *
-chxj_add_cookie_no_update_parameter(request_rec *r, char *value)
+chxj_add_cookie_no_update_parameter(request_rec *r, char *value, int xmlflag)
 {
   char *qs;
   char *dst;
   char *name = "";
 
-  DBG(r, "REQ[%X] start chxj_add_cookie_no_update_parameter()", (unsigned int)(apr_size_t)r);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
 
   if (! value || ! *value) {
-    DBG(r, "REQ[%X] end chxj_add_cookie_parameter()(void value)", (unsigned int)(apr_size_t)r);
+    DBG(r,"REQ[%X] (void value)", TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return apr_pstrdup(r->pool, "");
   }
 
   dst = apr_pstrdup(r->pool, value);
 
   if (chxj_cookie_check_host(r, value) != 0) {
-    DBG(r, "REQ[%X] end chxj_add_cookie_parameter()(check host)", (unsigned int)(apr_size_t)r);
+    DBG(r,"REQ[%X] (check host)", TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     goto on_error;
   }
 
@@ -874,12 +960,18 @@ chxj_add_cookie_no_update_parameter(request_rec *r, char *value)
     name = apr_pstrdup(r->pool, qs);
     *qs = 0;
   }
-  dst = apr_psprintf(r->pool, "%s%c%s=true%s", dst, (strchr(dst,'?')) ? '&' : '?',CHXJ_COOKIE_NOUPDATE_PARAM, name);
-  DBG(r, "REQ[%X] end   chxj_add_cookie_no_update_parameter() dst=[%s]", (unsigned int)(apr_size_t)r, dst);
+  char *amp = "&";
+  if (xmlflag) {
+    amp = "&amp;";
+  }
+  dst = apr_psprintf(r->pool, "%s%s%s=true%s", dst, (strchr(dst,'?')) ? amp : "?",CHXJ_COOKIE_NOUPDATE_PARAM, name);
+  DBG(r,"REQ[%X] dst=[%s]", TO_ADDR(r), dst);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return dst;
 
 on_error:
-  DBG(r, "REQ[%X] end   chxj_add_cookie_no_update_parameter() (on_error)", (unsigned int)(apr_size_t)r);
+  DBG(r,"REQ[%X] (on_error)", TO_ADDR(r));
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return dst;
 }
 
@@ -887,23 +979,44 @@ on_error:
 int
 chxj_cookie_check_host(request_rec *r, char *value) 
 {
-  char* hostnm;
+  char *hostnm;
+  mod_chxj_config *dconf;
 
-  DBG(r, "REQ[%X] start chxj_cookie_check_host()", (unsigned int)(apr_size_t)r);
-  DBG(r, "hostname=[%s] vs Location:[%s]", r->hostname, value);
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  DBG(r,"REQ[%X] hostname=[%s] vs Location:[%s]", TO_ADDR(r), r->hostname, value);
+
+  dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
 
   hostnm = s_get_hostname_from_url(r, value);
   if (hostnm) {
-    if (strcasecmp(hostnm, r->hostname) == 0) {
-      DBG(r, "REQ[%X] end chxj_cookie_check_host() (true)", (unsigned int)(apr_size_t)r);
-      return 0;
+    if (dconf->allowed_cookie_domain) {
+      DBG(r, "REQ[%X] allowed_domain[%s] vs Location:[%s]", TO_ADDR(r), dconf->allowed_cookie_domain, value);
+      if (chxj_strcasenrcmp(r->pool, hostnm, dconf->allowed_cookie_domain, strlen(dconf->allowed_cookie_domain))) {
+        DBG(r,"REQ[%X] (false/allowed_domain)", TO_ADDR(r));
+        DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
+        return 1;
+      }
+      else {
+        DBG(r,"REQ[%X] (true/allowed_domain)", TO_ADDR(r));
+        DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
+        return 0;
+      }
     }
     else {
-      DBG(r, "REQ[%X] end chxj_cookie_check_host() (false)", (unsigned int)(apr_size_t)r);
-      return 1;
+      if (strcasecmp(hostnm, r->hostname) == 0) {
+        DBG(r,"REQ[%X] (true)", TO_ADDR(r));
+        DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
+        return 0;
+      }
+      else {
+        DBG(r,"REQ[%X] (false)", TO_ADDR(r));
+        DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
+        return 1;
+      }
     }
   }
-  DBG(r, "REQ[%X] end chxj_cookie_check_host() (true)", (unsigned int)(apr_size_t)r);
+  DBG(r,"REQ[%X] (true)", TO_ADDR(r));
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
   return 0;
 }
 
@@ -948,14 +1061,14 @@ chxj_delete_cookie(request_rec *r, const char *cookie_id)
   int done_proc = 0;
   mod_chxj_config *dconf;
 
-  DBG(r, "start chxj_delete_cookie()");
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
 
 #if defined(USE_MYSQL_COOKIE)
   if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
     if (! chxj_delete_cookie_mysql(r, dconf, cookie_id)) {
-      ERR(r, "failed: chxj_delete_cookie_mysql() cookie_id:[%s]", cookie_id);
-      DBG(r, "end   chxj_delete_cookie()");
+      ERR(r,"REQ[%X] failed: chxj_delete_cookie_mysql() cookie_id:[%s]", TO_ADDR(r),cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -964,8 +1077,8 @@ chxj_delete_cookie(request_rec *r, const char *cookie_id)
 #if defined(USE_MEMCACHE_COOKIE)
   if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
     if (! chxj_delete_cookie_memcache(r, dconf, cookie_id)) {
-      ERR(r, "failed: chxj_delete_cookie_memcache() cookie_id:[%s]", cookie_id);
-      DBG(r, "end   chxj_delete_cookie()");
+      ERR(r,"REQ[%X] failed: chxj_delete_cookie_memcache() cookie_id:[%s]", TO_ADDR(r), cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -973,13 +1086,13 @@ chxj_delete_cookie(request_rec *r, const char *cookie_id)
 #endif
   if (!done_proc || IS_COOKIE_STORE_DBM(dconf->cookie_store_type)) {
     if (! chxj_delete_cookie_dbm(r, dconf, cookie_id)) {
-      ERR(r, "failed: chxj_delete_cookie_dbm() cookie_id:[%s]", cookie_id);
-      DBG(r, "end   chxj_delete_cookie()");
+      ERR(r,"REQ[%X] failed: chxj_delete_cookie_dbm() cookie_id:[%s]", TO_ADDR(r),cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
   }
 
-  DBG(r, "end   chxj_delete_cookie()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
 }
 
 
@@ -992,27 +1105,30 @@ chxj_save_cookie_expire(request_rec *r, cookie_t *cookie)
   int done_proc = 0;
   mod_chxj_config         *dconf;
 
-  DBG(r, "start chxj_save_cookie_expire()");
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
   if (!cookie) {
-    DBG(r, "cookie is NULL");
+    DBG(r,"REQ[%X] cookie is NULL",TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return;
   }
   if (!cookie->cookie_id) {
-    DBG(r, "cookie->cookie_id is NULL");
+    DBG(r,"REQ[%X] cookie->cookie_id is NULL",TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return;
   }
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
   if (!dconf) {
-    DBG(r, "dconf is NULL");
+    DBG(r,"REQ[%X] dconf is NULL",TO_ADDR(r));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return;
   }
 
 #if defined(USE_MYSQL_COOKIE)
   if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
     if (! chxj_save_cookie_expire_mysql(r, dconf, cookie->cookie_id)) {
-      ERR(r, "failed: chxj_save_cookie_expire_mysql() cookie_id:[%s]", cookie->cookie_id);
-      DBG(r, "end   chxj_save_cookie_expire()");
+      ERR(r,"REQ[%X] failed: chxj_save_cookie_expire_mysql() cookie_id:[%s]", TO_ADDR(r),cookie->cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -1021,8 +1137,8 @@ chxj_save_cookie_expire(request_rec *r, cookie_t *cookie)
 #if defined(USE_MEMCACHE_COOKIE)
   if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
     if (! chxj_save_cookie_expire_memcache(r, dconf, cookie->cookie_id)) {
-      ERR(r, "failed: chxj_save_cookie_expire_memcache() cookie_id:[%s]", cookie->cookie_id);
-      DBG(r, "end   chxj_save_cookie_expire()");
+      ERR(r,"REQ[%X] failed: chxj_save_cookie_expire_memcache() cookie_id:[%s]", TO_ADDR(r),cookie->cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -1030,13 +1146,13 @@ chxj_save_cookie_expire(request_rec *r, cookie_t *cookie)
 #endif
   if (!done_proc || IS_COOKIE_STORE_DBM(dconf->cookie_store_type)) {
     if (! chxj_save_cookie_expire_dbm(r, dconf, cookie->cookie_id)) {
-      ERR(r, "failed: chxj_save_cookie_expire_dbm() cookie_id:[%s]", cookie->cookie_id);
-      DBG(r, "end   chxj_save_cookie_expire()");
+      ERR(r,"REQ[%X] failed: chxj_save_cookie_expire_dbm() cookie_id:[%s]", TO_ADDR(r),cookie->cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
   }
 
-  DBG(r, "end   chxj_save_cookie_expire()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
 }
 
 
@@ -1046,14 +1162,15 @@ chxj_delete_cookie_expire(request_rec *r, char *cookie_id)
   int done_proc = 0;
   mod_chxj_config *dconf;
 
-  DBG(r, "start chxj_delete_cookie_expire()");
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
 
 #if defined(USE_MYSQL_COOKIE)
   if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
     if (! chxj_delete_cookie_expire_mysql(r, dconf, cookie_id)) {
-      ERR(r, "failed: chxj_delete_cookie_expire_mysql() cookie_id:[%s]", cookie_id);
+      ERR(r,"REQ[%X] failed: chxj_delete_cookie_expire_mysql() cookie_id:[%s]", TO_ADDR(r),cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -1062,7 +1179,8 @@ chxj_delete_cookie_expire(request_rec *r, char *cookie_id)
 #if defined(USE_MEMCACHE_COOKIE)
   if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
     if (!chxj_delete_cookie_expire_memcache(r, dconf, cookie_id)) {
-      ERR(r, "failed: chxj_delete_cookie_expire_memcache() cookie_id:[%s]", cookie_id);
+      ERR(r,"REQ[%X] failed: chxj_delete_cookie_expire_memcache() cookie_id:[%s]", TO_ADDR(r),cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     } 
     done_proc = 1;
@@ -1070,12 +1188,13 @@ chxj_delete_cookie_expire(request_rec *r, char *cookie_id)
 #endif
   if (!done_proc || IS_COOKIE_STORE_DBM(dconf->cookie_store_type)) {
     if (!chxj_delete_cookie_expire_dbm(r, dconf, cookie_id)) {
-      ERR(r, "failed: chxj_delete_cookie_expire_dbm() cookie_id:[%s]", cookie_id);
+      ERR(r,"REQ[%X] failed: chxj_delete_cookie_expire_dbm() cookie_id:[%s]", TO_ADDR(r),cookie_id);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     } 
   }
 
-  DBG(r, "end   chxj_delete_cookie_expire()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
 }
 
 
@@ -1085,13 +1204,14 @@ chxj_cookie_expire_gc(request_rec *r)
   mod_chxj_config   *dconf;
   int done_proc = 0;
 
-  DBG(r, "start chxj_cookie_expire_gc()");
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
 #if defined(USE_MYSQL_COOKIE)
   if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
     if (! chxj_cookie_expire_gc_mysql(r, dconf)) {
-      ERR(r, "%s:%d end chxj_cookie_expire_gc(): failed: chxj_cookie_expire_gc_mysql()", APLOG_MARK);
+      ERR(r,"REQ[%X] %s:%d end chxj_cookie_expire_gc(): failed: chxj_cookie_expire_gc_mysql()", TO_ADDR(r),APLOG_MARK);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -1100,7 +1220,8 @@ chxj_cookie_expire_gc(request_rec *r)
 #if defined(USE_MEMCACHE_COOKIE)
   if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
     if (! chxj_cookie_expire_gc_memcache(r, dconf)) {
-      ERR(r, "%s:%d end chxj_cookie_expire_gc(): failed: chxj_cookie_expire_gc_memcache()", APLOG_MARK);
+      ERR(r,"REQ[%X] %s:%d end chxj_cookie_expire_gc(): failed: chxj_cookie_expire_gc_memcache()", TO_ADDR(r),APLOG_MARK);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
     done_proc = 1;
@@ -1108,11 +1229,12 @@ chxj_cookie_expire_gc(request_rec *r)
 #endif
   if (!done_proc) {
     if (! chxj_cookie_expire_gc_dbm(r, dconf)) {
-      ERR(r, "%s:%d end chxj_cookie_expire_gc(): failed: chxj_cookie_expire_gc_dbm()", APLOG_MARK);
+      ERR(r,"REQ[%X] %s:%d end chxj_cookie_expire_gc(): failed: chxj_cookie_expire_gc_dbm()", TO_ADDR(r),APLOG_MARK);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return;
     }
   }
-  DBG(r, "end   chxj_cookie_expire_gc()");
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
 }
 
 apr_time_t
@@ -1131,17 +1253,19 @@ __chxj_cookie_lock(request_rec *r, const char *filename, int line)
   int done_proc = 0;
   cookie_lock_t *ret = NULL;
 
-  DBG(r, "start chxj_cookie_lock() call from %s:%d", filename, line);
+  DBG(r,"REQ[%X] start %s() call from %s:%d",TO_ADDR(r),__func__, filename,line);
   if ((rv = apr_proc_mutex_lock(global_cookie_mutex)) != APR_SUCCESS) {
     char errstr[255];
-    ERR(r, "%s:%d apr_proc_mutex_lock failure.(%d:%s)", APLOG_MARK, rv, apr_strerror(rv, errstr, 255));
+    ERR(r,"REQ[%X] %s:%d apr_proc_mutex_lock failure.(%d:%s)", TO_ADDR(r),APLOG_MARK, rv, apr_strerror(rv, errstr, 255));
+    DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
     return NULL;
   }
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
 #if defined(USE_MYSQL_COOKIE)
   if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
     if (! chxj_cookie_lock_mysql(r, dconf)) {
-      ERR(r, "%s:%d end chxj_cookie_lock(): failed: chxj_cookie_lock_mysql()", APLOG_MARK);
+      ERR(r,"REQ[%X] %s:%d end chxj_cookie_lock(): failed: chxj_cookie_lock_mysql()", TO_ADDR(r),APLOG_MARK);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return NULL;
     }
     done_proc = 1;
@@ -1152,7 +1276,8 @@ __chxj_cookie_lock(request_rec *r, const char *filename, int line)
 #if defined(USE_MEMCACHE_COOKIE)
   if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
     if (! chxj_cookie_lock_memcache(r, dconf)) {
-      ERR(r, "%s:%d end chxj_cookie_lock(): failed: chxj_cookie_lock_memcache()", APLOG_MARK);
+      ERR(r,"REQ[%X] %s:%d end chxj_cookie_lock(): failed: chxj_cookie_lock_memcache()", TO_ADDR(r),APLOG_MARK);
+      DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
       return NULL;
     }
     done_proc = 1;
@@ -1162,12 +1287,12 @@ __chxj_cookie_lock(request_rec *r, const char *filename, int line)
 #endif
   if (!done_proc) {
     if (!(ret = chxj_cookie_lock_dbm(r, dconf))) {
-      ERR(r, "%s:%d end chxj_cookie_lock(): failed: chxj_cookie_lock_dbm()", APLOG_MARK);
-      DBG(r, "end chxj_cookie_lock() call from %s:%d", filename, line);
+      ERR(r,"REQ[%X] %s:%d end chxj_cookie_lock(): failed: chxj_cookie_lock_dbm()", TO_ADDR(r),APLOG_MARK);
+      DBG(r,"REQ[%X] end %s() call from %s:%d",TO_ADDR(r),__func__,filename,line);
       return NULL;
     }
   }
-  DBG(r, "REQ:[%X] end chxj_cookie_lock() call from %s:%d", (unsigned int)(apr_size_t)r, filename, line);
+  DBG(r,"REQ[%X] end %s() call from %s:%d",TO_ADDR(r),__func__,filename,line);
   return ret;
 }
 
@@ -1180,13 +1305,13 @@ __chxj_cookie_unlock(request_rec *r, cookie_lock_t *lock, const char *filename,
   apr_status_t rv;
   int rtn = 1;
 
-  DBG(r, "start chxj_cookie_unlock() call from %s:%d", filename, line);
+  DBG(r,"REQ[%X] start %s() call from %s:%d", TO_ADDR(r),__func__,filename, line);
 
   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
 #if defined(USE_MYSQL_COOKIE)
   if (IS_COOKIE_STORE_MYSQL(dconf->cookie_store_type)) {
     if (! chxj_cookie_unlock_mysql(r, dconf)) {
-      ERR(r, "failed: chxj_cookie_unlock_mysql()");
+      ERR(r,"REQ[%X] failed: chxj_cookie_unlock_mysql()",TO_ADDR(r));
       rtn = 0;
       goto end_chxj_cookie_unlock;
     }
@@ -1196,7 +1321,7 @@ __chxj_cookie_unlock(request_rec *r, cookie_lock_t *lock, const char *filename,
 #if defined(USE_MEMCACHE_COOKIE)
   if (IS_COOKIE_STORE_MEMCACHE(dconf->cookie_store_type)) {
     if (! chxj_cookie_unlock_memcache(r, dconf)) {
-      ERR(r, "failed: chxj_cookie_unlock_memcache()");
+      ERR(r,"REQ[%X] failed: chxj_cookie_unlock_memcache()",TO_ADDR(r));
       rtn = 0;
       goto end_chxj_cookie_unlock;
     }
@@ -1205,7 +1330,7 @@ __chxj_cookie_unlock(request_rec *r, cookie_lock_t *lock, const char *filename,
 #endif
   if (!done_proc) {
     if (! chxj_cookie_unlock_dbm(r, lock, dconf)) {
-      ERR(r, "failed: chxj_cookie_unlock_dbm()");
+      ERR(r,"REQ[%X] failed: chxj_cookie_unlock_dbm()",TO_ADDR(r));
       rtn = 0;
       goto end_chxj_cookie_unlock;
     }
@@ -1213,14 +1338,254 @@ __chxj_cookie_unlock(request_rec *r, cookie_lock_t *lock, const char *filename,
 end_chxj_cookie_unlock:
   if ((rv = apr_proc_mutex_unlock(global_cookie_mutex)) != APR_SUCCESS) {
     char errstr[255];
-    ERR(r, "%s:%d apr_proc_mutex_unlock failure.(%d:%s)", APLOG_MARK, rv, apr_strerror(rv, errstr, 255));
-    DBG(r, "end chxj_cookie_unlock() call from %s:%d", filename, line);
+    ERR(r,"REQ[%X] %s:%d apr_proc_mutex_unlock failure.(%d:%s)", TO_ADDR(r),APLOG_MARK, rv, apr_strerror(rv, errstr, 255));
+    DBG(r,"REQ[%X] end %s() call from %s:%d", TO_ADDR(r),__func__,filename, line);
     return 0;
   }
-  DBG(r, "end chxj_cookie_unlock() call from %s:%d", filename, line);
+  DBG(r,"REQ[%X] end %s() call from %s:%d", TO_ADDR(r),__func__,filename, line);
 
   return rtn;
 }
+
+
+#include "chxj_encoding.h"
+char *
+s_convert_a_tag(request_rec *r, const char *s, apr_size_t *len, cookie_t *cookie)
+{
+  apr_pool_t *pool;
+  ap_regex_t *regexp = NULL;
+  int   nowpos = 0;
+  Doc       doc;
+  char *dst = NULL;
+
+  apr_pool_create(&pool, r->pool);
+  regexp = ap_pregcomp(pool, "(<a[^>]*>)", AP_REG_EXTENDED|AP_REG_ICASE);
+  doc.r = r;
+  qs_init_malloc(&doc);
+  qs_init_root_node(&doc);
+
+  while(1) {
+    ap_regmatch_t        match[10];
+    if (ap_regexec((const ap_regex_t *)regexp, &s[nowpos], (apr_size_t)regexp->re_nsub + 1, match, 0) == 0) {
+      apr_size_t plen = match[1].rm_so;
+      if (plen > 0) {
+        char *tmpstr = apr_palloc(pool, plen + 1);
+        memset(tmpstr, 0, plen + 1);
+        memcpy(tmpstr, &s[nowpos], plen);
+        dst = apr_pstrcat(pool, (dst) ? dst : "", tmpstr, NULL);
+      }
+      else {
+        plen = 0;
+      }
+      char *matchstr = ap_pregsub(pool, "$1", &s[nowpos], regexp->re_nsub + 1, match);
+      if (matchstr) {
+        Node *node = qs_parse_tag(&doc, matchstr, strlen(matchstr) - 1);
+        Attr *attr;
+
+        DBG(r,"REQ[%X] matchstr:[%s]", TO_ADDR(r),matchstr);
+        dst = apr_pstrcat(pool, (dst) ? dst : "", "<a ", NULL);
+        for (attr = qs_get_attr(&doc,node);
+             attr;
+             attr = qs_get_next_attr(&doc,attr)) {
+          char *name  = qs_get_attr_name(&doc,attr);
+          char *value = qs_get_attr_value(&doc,attr);
+          DBG(r,"REQ[%X] name:[%s] value=[%s]",TO_ADDR(r), name, value);
+          if (STRCASEEQ('h', 'H', "href", name)
+              && ! chxj_starts_with(value, "mailto:") 
+              && ! chxj_starts_with(value, "tel:")) {
+            char *flgp = strchr(value, '#');
+            char *flgsv = NULL;
+            if (flgp) {
+              int flgl = strlen(flgp);
+              flgsv = apr_palloc(pool, flgl+1);
+              memset(flgsv, 0, flgl + 1);
+              memcpy(flgsv, flgp, flgl);
+              *flgp = 0;
+            }
+            if (strchr(value, '?') != 0) {
+              value = apr_pstrcat(pool, value, "&_chxj_cc=", cookie->cookie_id, NULL);
+            }
+            else {
+              value = apr_pstrcat(pool, value, "?_chxj_cc=", cookie->cookie_id, NULL);
+            }
+            if (flgsv) {
+              value = apr_pstrcat(pool, value, flgsv, NULL);
+            }
+            dst = apr_pstrcat(pool, (dst) ? dst : "", "href='", value, "' ", NULL);
+          }
+          else {
+            dst = apr_pstrcat(pool, (dst) ? dst : "", name, "='", value, "' ", NULL);
+          }
+        }
+        dst = apr_pstrcat(pool, (dst) ? dst : "", ">", NULL);
+        plen += strlen(matchstr);
+      }
+      nowpos += plen;
+    }
+    else {
+      break;
+    }
+  }
+
+  if ((apr_size_t)nowpos < *len) {
+    apr_size_t plen = *len - nowpos;
+    char *tmpstr = apr_palloc(pool, plen + 1);
+    memset(tmpstr, 0, plen+1);
+    memcpy(tmpstr, &s[nowpos], plen);
+    dst = apr_pstrcat(pool, (dst) ? dst : "", tmpstr, NULL);
+  }
+
+  *len = strlen(dst);
+  return dst;
+}
+char *
+s_convert_img_tag(request_rec *r, const char *s, apr_size_t *len, cookie_t *cookie)
+{
+  apr_pool_t *pool;
+  ap_regex_t *regexp = NULL;
+  int   nowpos = 0;
+  Doc       doc;
+  char *dst = NULL;
+
+  apr_pool_create(&pool, r->pool);
+  regexp = ap_pregcomp(pool, "(<img[^>]*>)", AP_REG_EXTENDED|AP_REG_ICASE);
+  doc.r = r;
+  qs_init_malloc(&doc);
+  qs_init_root_node(&doc);
+
+  while(1) {
+    ap_regmatch_t        match[10];
+    if (ap_regexec((const ap_regex_t *)regexp, &s[nowpos], (apr_size_t)regexp->re_nsub + 1, match, 0) == 0) {
+      apr_size_t plen = match[1].rm_so;
+      if (plen > 0) {
+        char *tmpstr = apr_palloc(pool, plen + 1);
+        memset(tmpstr, 0, plen + 1);
+        memcpy(tmpstr, &s[nowpos], plen);
+        dst = apr_pstrcat(pool, (dst) ? dst : "", tmpstr, NULL);
+      }
+      else {
+        plen = 0;
+      }
+      char *matchstr = ap_pregsub(pool, "$1", &s[nowpos], regexp->re_nsub + 1, match);
+      if (matchstr) {
+        Node *node = qs_parse_tag(&doc, matchstr, strlen(matchstr) - 1);
+        Attr *attr;
+
+        DBG(r,"REQ[%X] matchstr:[%s]", TO_ADDR(r),matchstr);
+        dst = apr_pstrcat(pool, (dst) ? dst : "", "<img ", NULL);
+        for (attr = qs_get_attr(&doc,node);
+             attr;
+             attr = qs_get_next_attr(&doc,attr)) {
+          char *name  = qs_get_attr_name(&doc,attr);
+          char *value = qs_get_attr_value(&doc,attr);
+          DBG(r,"REQ[%X] name:[%s] value=[%s]", TO_ADDR(r),name, value);
+          if (STRCASEEQ('s', 'S', "src", name)) {
+            if (strchr(value, '?') != 0) {
+              value = apr_pstrcat(pool, value, "&_chxj_cc=", cookie->cookie_id, "&_chxj_nc=true", NULL);
+            }
+            else {
+              value = apr_pstrcat(pool, value, "?_chxj_cc=", cookie->cookie_id, "&_chxj_nc=true", NULL);
+            }
+            dst = apr_pstrcat(pool, (dst) ? dst : "", "src='", value, "' ", NULL);
+          }
+          else {
+            dst = apr_pstrcat(pool, (dst) ? dst : "", name, "='", value, "' ", NULL);
+          }
+        }
+        dst = apr_pstrcat(pool, (dst) ? dst : "", "/>", NULL);
+        plen += strlen(matchstr);
+      }
+      nowpos += plen;
+    }
+    else {
+      break;
+    }
+  }
+
+  if ((apr_size_t)nowpos < *len) {
+    apr_size_t plen = *len - nowpos;
+    char *tmpstr = apr_palloc(pool, plen + 1);
+    memset(tmpstr, 0, plen+1);
+    memcpy(tmpstr, &s[nowpos], plen);
+    dst = apr_pstrcat(pool, (dst) ? dst : "", tmpstr, NULL);
+  }
+
+  *len = strlen(dst);
+  return dst;
+}
+char *
+s_convert_form_tag(request_rec *r, const char *s, apr_size_t *len, cookie_t *cookie)
+{
+  apr_pool_t *pool;
+  ap_regex_t *regexp = NULL;
+  int   nowpos = 0;
+  Doc       doc;
+  char *dst = NULL;
+  char *cookie_id;
+
+  apr_pool_create(&pool, r->pool);
+  regexp = ap_pregcomp(pool, "(</form>)", AP_REG_EXTENDED|AP_REG_ICASE);
+  doc.r = r;
+  qs_init_malloc(&doc);
+  qs_init_root_node(&doc);
+  cookie_id = chxj_url_decode(pool, cookie->cookie_id);
+
+  while(1) {
+    ap_regmatch_t        match[10];
+    if (ap_regexec((const ap_regex_t *)regexp, &s[nowpos], (apr_size_t)regexp->re_nsub + 1, match, 0) == 0) {
+      apr_size_t plen = match[1].rm_so;
+      if (plen > 0) {
+        char *tmpstr = apr_palloc(pool, plen + 1);
+        memset(tmpstr, 0, plen + 1);
+        memcpy(tmpstr, &s[nowpos], plen);
+        dst = apr_pstrcat(pool, (dst) ? dst : "", tmpstr, NULL);
+      }
+      else {
+        plen = 0;
+      }
+      char *matchstr = ap_pregsub(pool, "$1", &s[nowpos], regexp->re_nsub + 1, match);
+      if (matchstr) {
+        DBG(r,"REQ[%X] matchstr:[%s]",TO_ADDR(r),matchstr);
+        dst = apr_pstrcat(pool, (dst) ? dst : "", "<input type='hidden' name='_chxj_cc' value='", cookie_id, "' />", matchstr, NULL);
+        plen += strlen(matchstr);
+      }
+      nowpos += plen;
+    }
+    else {
+      break;
+    }
+  }
+
+  if ((apr_size_t)nowpos < *len) {
+    apr_size_t plen = *len - nowpos;
+    char *tmpstr = apr_palloc(pool, plen + 1);
+    memset(tmpstr, 0, plen + 1);
+    memcpy(tmpstr, &s[nowpos], plen);
+    dst = apr_pstrcat(pool, (dst) ? dst : "", tmpstr, NULL);
+  }
+
+  *len = strlen(dst);
+  return dst;
+}
+
+char *
+chxj_cookie_only_mode(request_rec *r, const char *src, apr_size_t *len, cookie_t* cookie)
+{
+  char *s;
+  char *result;
+  char *dst = NULL;
+
+  DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
+  s = chxj_encoding(r, src, len);
+
+  dst = s_convert_a_tag(r, s, len, cookie);
+  dst = s_convert_img_tag(r, dst, len, cookie);
+  dst = s_convert_form_tag(r, dst, len, cookie);
+
+  result = chxj_rencoding(r, dst, len, NULL);
+  DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
+  return result;
+}
 /*
  * vim:ts=2 et
  */