OSDN Git Service

* Added REQUEST_REQ address to message.
[modchxj/mod_chxj.git] / src / mod_chxj.c
1 /*
2  * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <unistd.h>
18 #include <string.h>
19 #include <limits.h>
20 #include <errno.h>
21
22 #include "httpd.h"
23 #include "http_config.h"
24 #include "http_core.h"
25 #include "http_protocol.h"
26 #include "http_log.h"
27 #include "ap_config.h"
28 #include "apr_strings.h"
29 #include "util_filter.h"
30 #include "apr_buckets.h"
31 #include "apr_lib.h"
32 #include "apr_tables.h"
33 #include "apr_dso.h"
34 #include "apr_general.h"
35 #include "apr_pools.h"
36
37 #include "mod_chxj.h"
38 #include "chxj_encoding.h"
39 #include "qs_ignore_sp.h"
40 #include "qs_log.h"
41 #include "qs_malloc.h"
42 #include "qs_parse_attr.h"
43 #include "qs_parse_file.h"
44 #include "qs_parse_string.h"
45 #include "qs_parse_tag.h"
46 #include "chxj_load_device_data.h"
47 #include "chxj_load_emoji_data.h"
48 #include "chxj_specified_device.h"
49 #include "chxj_tag_util.h"
50 #include "chxj_xhtml_mobile_1_0.h"
51 #include "chxj_hdml.h"
52 #include "chxj_chtml10.h"
53 #include "chxj_chtml20.h"
54 #include "chxj_chtml30.h"
55 #include "chxj_chtml40.h"
56 #include "chxj_chtml50.h"
57 #include "chxj_jhtml.h"
58 #include "chxj_jxhtml.h"
59 #include "chxj_img_conv_format.h"
60 #include "chxj_qr_code.h"
61 #include "chxj_encoding.h"
62 #include "chxj_apply_convrule.h"
63 #include "chxj_cookie.h"
64 #include "chxj_url_encode.h"
65 #include "chxj_str_util.h"
66 #if defined(USE_MYSQL_COOKIE)
67 #  include "chxj_mysql.h"
68 #endif
69 #include "chxj_serf.h"
70
71
72 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
73 #define CHXJ_VERSION        PACKAGE_VERSION
74 #define CHXJ_POST_MAX       (0x1000000)
75
76 converter_t convert_routine[] = {
77   {
78     /* CHXJ_SPEC_UNKNOWN          */
79     .converter = NULL,
80     .encoder  = NULL,
81   },
82   {
83     /* CHXJ_SPEC_Chtml_1_0        */
84     .converter = chxj_convert_chtml10,
85     .encoder  = chxj_encoding,
86   },
87   {
88     /* CHXJ_SPEC_Chtml_2_0        */
89     .converter = chxj_convert_chtml20,
90     .encoder  = chxj_encoding,
91   },
92   {
93     /* CHXJ_SPEC_Chtml_3_0        */
94     .converter = chxj_convert_chtml30,
95     .encoder  = chxj_encoding,
96   },
97   {
98     /* CHXJ_SPEC_Chtml_4_0        */
99     .converter = chxj_convert_chtml40,
100     .encoder  = chxj_encoding,
101   },
102   {
103     /* CHXJ_SPEC_Chtml_5_0        */
104     .converter = chxj_convert_chtml50,
105     .encoder  = chxj_encoding,
106   },
107   {
108     /* CHXJ_SPEC_Chtml_6_0        */
109     .converter = chxj_convert_chtml50,
110     .encoder  = chxj_encoding,
111   },
112   {
113     /* CHXJ_SPEC_Chtml_7_0        */
114     .converter = chxj_convert_chtml50,
115     .encoder  = chxj_encoding,
116   },
117   {
118     /* CHXJ_SPEC_XHtml_Mobile_1_0 */
119     .converter = chxj_convert_xhtml_mobile_1_0,
120     .encoder  = chxj_encoding,
121   },
122   {
123     /* CHXJ_SPEC_Hdml             */
124     .converter = chxj_convert_hdml,
125     .encoder  = chxj_encoding,
126   },
127   {
128     /* CHXJ_SPEC_Jhtml            */
129     .converter = chxj_convert_jhtml,
130     .encoder  = chxj_encoding,
131   },
132   {
133     /* CHXJ_SPEC_Jxhtml            */
134     .converter = chxj_convert_jxhtml,
135     .encoder  = chxj_encoding,
136   },
137   {
138     /* CHXJ_SPEC_HTML             */
139     .converter = NULL,
140     .encoder  = NULL,
141   },
142 };
143
144 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp);
145 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
146 static void s_clear_cookie_header(request_rec *r, device_table *spec);
147
148 /**
149  * Only when User-Agent is specified, the User-Agent header is camouflaged. 
150  *
151  * @param r   [i]
152  */
153 static apr_status_t 
154 chxj_headers_fixup(request_rec *r)
155 {
156   mod_chxj_config*    dconf; 
157   chxjconvrule_entry* entryp;
158   char*               user_agent;
159   device_table*       spec;
160   char                *contentType;
161   char                *contentLength;
162
163   DBG(r, "REQ[%X] start chxj_headers_fixup()", (unsigned int)r);
164   if (r->main) {
165     DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)r);
166     DBG(r, "REQ[%X] end chxj_headers_fixup()",  (unsigned int)r);
167     return DECLINED;
168   }
169
170   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
171
172   user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
173   spec = chxj_specified_device(r, user_agent);
174
175   contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
176   if (contentType
177       && strncasecmp("multipart/form-data", contentType, 19) == 0) {
178     DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)r);
179     DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)r);
180     return DECLINED;
181   }
182   if (r->method_number == M_POST) {
183     if (!apr_table_get(r->headers_in, "X-Chxj-Forward")) {
184       s_clear_cookie_header(r, spec);
185     }
186   }
187   else {
188     s_clear_cookie_header(r, spec);
189   }
190
191   switch(spec->html_spec_type) {
192   case CHXJ_SPEC_Chtml_1_0:
193   case CHXJ_SPEC_Chtml_2_0:
194   case CHXJ_SPEC_Chtml_3_0:
195   case CHXJ_SPEC_Chtml_4_0:
196   case CHXJ_SPEC_Chtml_5_0:
197   case CHXJ_SPEC_Chtml_6_0:
198   case CHXJ_SPEC_Chtml_7_0:
199   case CHXJ_SPEC_XHtml_Mobile_1_0:
200   case CHXJ_SPEC_Hdml:
201   case CHXJ_SPEC_Jhtml:
202   case CHXJ_SPEC_Jxhtml:
203     entryp = chxj_apply_convrule(r, dconf->convrules);
204     if (! entryp) {
205       DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int) r);
206       return DECLINED;
207     }
208     if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
209       DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int) r);
210       return DECLINED;
211     }
212   
213     apr_table_setn(r->headers_in, 
214                    CHXJ_HTTP_USER_AGENT, 
215                    user_agent);
216   
217     if (entryp->user_agent)
218       apr_table_setn(r->headers_in, 
219                      HTTP_USER_AGENT, 
220                      entryp->user_agent);
221
222     chxj_convert_input_header(r,entryp);
223
224     break;
225   
226   default:
227     DBG(r, "REQ[%X] end chxj_headers_fixup() (not mobile)", (unsigned int) r);
228     return DECLINED;
229
230   }
231
232
233   if (r->method_number == M_POST) {
234     if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
235         DBG(r, "REQ[%X] set Input handler old:[%s] proxyreq:[%d] uri:[%s] filename:[%s]", (unsigned int)r, r->handler, r->proxyreq, r->uri, r->filename);
236         r->proxyreq = PROXYREQ_NONE;
237         r->handler = apr_psprintf(r->pool, "chxj-input-handler");
238     }
239     else {
240       char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
241       if (client_ip) {
242         apr_sockaddr_t *address = NULL;
243         apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
244         if (rv != APR_SUCCESS) {
245           char buf[256];
246           ERR(r, "REQ[%X] %s:%d apr_sockaddr_info_get() failed: rv:[%d|%s]", (unsigned int)r, APLOG_MARK, rv, apr_strerror(rv, buf, 256));
247           DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)r);
248           return DECLINED;
249         }
250         char *addr;
251         if (dconf->forward_server_ip) {
252           addr = dconf->forward_server_ip;
253         }
254         else {
255           apr_sockaddr_ip_get(&addr, address);
256         }
257         DBG(r, "REQ[%X] Client IP:[%s] vs Orig Client IP:[%s] vs Server IP:[%s]", (unsigned int)r, r->connection->remote_ip, client_ip, addr);
258         if (strcmp(addr, r->connection->remote_ip) == 0) {
259           r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
260         }
261         if (! apr_table_get(r->headers_in, "Content-Length")) {
262           contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
263           if (contentLength) {
264             apr_table_set(r->headers_in, "Content-Length", contentLength);
265           }
266         }
267       }
268     }
269   }
270
271   DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)r);
272
273   return DECLINED;
274 }
275
276
277 static void
278 s_clear_cookie_header(request_rec *r, device_table *spec)
279 {
280   switch(spec->html_spec_type) {
281   case CHXJ_SPEC_Chtml_1_0:
282   case CHXJ_SPEC_Chtml_2_0:
283   case CHXJ_SPEC_Chtml_3_0:
284   case CHXJ_SPEC_Chtml_4_0:
285   case CHXJ_SPEC_Chtml_5_0:
286   case CHXJ_SPEC_Chtml_6_0:
287   case CHXJ_SPEC_Chtml_7_0:
288   case CHXJ_SPEC_XHtml_Mobile_1_0:
289   case CHXJ_SPEC_Jhtml:
290     apr_table_unset(r->headers_in, "Cookie");
291     break;
292   default:
293     break;
294   }
295 }
296
297
298 /**
299  * It converts it from CHTML into XXML corresponding to each model. 
300  *
301  * @param r   [i]
302  * @param src [i]   It is former HTML character string. 
303  * @param len [i/o] It is length of former HTML character string. 
304  */
305 static char * 
306 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
307 {
308   char                *user_agent;
309   char                *dst;
310   char                *tmp;
311   cookie_t            *cookie;
312   mod_chxj_config     *dconf; 
313   chxjconvrule_entry  *entryp;
314
315   DBG(r,"REQ[%X] start of chxj_convert() input:[%.*s]", (unsigned int)r, (int)*len, *src);
316   dst  = apr_pstrcat(r->pool, (char *)*src, NULL);
317
318   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
319
320
321   entryp = chxj_apply_convrule(r, dconf->convrules);
322
323   if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
324     DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)r);
325     return (char *)*src;
326   }
327
328
329   /*------------------------------------------------------------------------*/
330   /* get UserAgent from http header                                         */
331   /*------------------------------------------------------------------------*/
332   if (entryp->user_agent)
333     user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
334   else
335     user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
336
337   DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)r, user_agent);
338   DBG(r,"REQ[%X] content type is %s", (unsigned int)r, r->content_type);
339
340
341   if (  ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
342     &&  ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
343     DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)r, r->content_type);
344     DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)r);
345     return (char *)*src;
346   }
347
348   if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
349     /* again */
350     spec = chxj_specified_device(r, user_agent);
351   }
352
353   /*
354    * save cookie.
355    */
356   cookie = NULL;
357   if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
358     switch(spec->html_spec_type) {
359     case CHXJ_SPEC_Chtml_1_0:
360     case CHXJ_SPEC_Chtml_2_0:
361     case CHXJ_SPEC_Chtml_3_0:
362     case CHXJ_SPEC_Chtml_4_0:
363     case CHXJ_SPEC_Chtml_5_0:
364     case CHXJ_SPEC_Chtml_6_0:
365     case CHXJ_SPEC_Chtml_7_0:
366     case CHXJ_SPEC_XHtml_Mobile_1_0:
367     case CHXJ_SPEC_Jhtml:
368       cookie = chxj_save_cookie(r);
369       break;
370     default:
371       break;
372     }
373   }
374
375   if (!r->header_only) {
376
377     tmp = NULL;
378     if (convert_routine[spec->html_spec_type].encoder)
379       tmp = convert_routine[spec->html_spec_type].encoder(r, 
380                                                           *src, 
381                                                           (apr_size_t *)len);
382
383     if (convert_routine[spec->html_spec_type].converter) {
384       if (tmp)
385         dst = convert_routine[spec->html_spec_type].converter(r, 
386                                                               spec, 
387                                                               tmp, 
388                                                               *len, 
389                                                               len, 
390                                                               entryp, 
391                                                               cookie);
392       else
393         dst = convert_routine[spec->html_spec_type].converter(r,
394                                                               spec, 
395                                                               tmp, 
396                                                               *len, 
397                                                               len, 
398                                                               entryp, 
399                                                               cookie);
400     }
401   }
402   ap_set_content_length(r, *len);
403
404   if (*len == 0) {
405     dst = apr_psprintf(r->pool, "\n");
406     *len = 1;
407   }
408   dst[*len] = 0;
409   if (cookie) {
410     *cookiep = cookie;
411   }
412
413   DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)r);
414
415   return dst;
416 }
417
418
419 /**
420  * It converts it from HEADER.
421  *
422  * @param r   [i]
423  */
424 static int
425 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp) 
426 {
427
428   char       *buff;
429   char       *buff_pre;
430   apr_size_t urilen;
431   char       *result;
432   char       *pair;
433   char       *name;
434   char       *value;
435   char       *pstate;
436   char       *vstate;
437   cookie_t   *cookie = NULL;
438   int        no_update_flag = 0;
439
440   DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
441
442   if (! r->args) {
443     DBG(r, "REQ[%X] r->args=[null]", (unsigned int)r);
444     DBG(r, "REQ[%X] end   chxj_convert_input_header()", (unsigned int)r);
445     return 0;
446   }
447   urilen = strlen(r->args);
448
449   result = qs_alloc_zero_byte_string(r->pool);
450
451   buff_pre = apr_pstrdup(r->pool, r->args);
452
453   for (;;) {
454     char *pair_sv;
455
456     pair = apr_strtok(buff_pre, "&", &pstate);
457     if (pair == NULL)
458       break;
459
460     buff_pre = NULL;
461
462     pair_sv = apr_pstrdup(r->pool, pair);
463
464     name  = apr_strtok(pair, "=", &vstate);
465     value = apr_strtok(NULL, "=", &vstate);
466     if (! name) continue;
467     if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
468       DBG(r, "found cookie no update parameter");
469       no_update_flag++;
470     }
471   }
472
473   buff = apr_pstrdup(r->pool, r->args);
474   DBG(r, "r->args=[%s]", buff);
475
476   /* _chxj_dmy */
477   /* _chxj_c_ */
478   /* _chxj_r_ */
479   /* _chxj_s_ */
480   for (;;) {
481     char *pair_sv;
482
483     pair = apr_strtok(buff, "&", &pstate);
484     if (pair == NULL)
485       break;
486
487     buff = NULL;
488
489     pair_sv = apr_pstrdup(r->pool, pair);
490
491     name  = apr_strtok(pair, "=", &vstate);
492     value = apr_strtok(NULL, "=", &vstate);
493     if (! name) continue;
494     if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
495       if (strlen(result) != 0) 
496         result = apr_pstrcat(r->pool, result, "&", NULL);
497
498       if (strcasecmp(entryp->encoding, "NONE") != 0) {
499         apr_size_t dlen;
500         char *dvalue;
501         char *dname;
502
503         if (value && *value != 0) {
504           value = chxj_url_decode(r->pool, value);
505           dlen   = strlen(value);
506           DBG(r, "************ before encoding[%s]", value);
507   
508           dvalue = chxj_rencoding(r, value, &dlen);
509           dvalue = chxj_url_encode(r->pool, dvalue);
510   
511           DBG(r, "************ after encoding[%s]", dvalue);
512         }
513         else {
514           dvalue = "";
515         }
516
517         if (name && *name != 0) {
518           name = chxj_url_decode(r->pool, name);
519           dlen = strlen(name);
520           dname = chxj_rencoding(r, name, &dlen);
521           dname = chxj_url_encode(r->pool, dname);
522         }
523         else {
524           dname = "";
525         }
526
527         result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
528       }
529       else {
530         if (strcmp(name, pair_sv) != 0)
531           result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
532         else
533           result = apr_pstrcat(r->pool, result, name, NULL);
534       }
535     }
536     else
537     if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
538       || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
539       || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
540       if (value == NULL)
541         continue;
542
543       if (strlen(value) == 0)
544         continue;
545
546       if (strlen(result) != 0)
547         result = apr_pstrcat(r->pool, result, "&", NULL);
548
549       result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
550     }
551     else
552     if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
553       if (! cookie) {
554         apr_table_unset(r->headers_in, "Cookie");
555         DBG(r, "found cookie parameter[%s]", value);
556         DBG(r, "call start chxj_load_cookie()");
557         cookie_lock_t *lock = chxj_cookie_lock(r);
558         cookie = chxj_load_cookie(r, value);
559         DBG(r, "call end   chxj_load_cookie()");
560         if (! no_update_flag && cookie) {
561           chxj_update_cookie(r, cookie);
562         }
563         chxj_cookie_unlock(r, lock);
564       }
565       if (cookie && cookie->cookie_id) {
566         if (strlen(result) != 0)
567           result = apr_pstrcat(r->pool, result, "&", NULL);
568         result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
569       }
570     }
571     else
572     if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
573       if (strlen(result) != 0)
574         result = apr_pstrcat(r->pool, result, "&", NULL);
575       result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
576     }
577   }
578   apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
579   if (! no_update_flag) {
580     result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
581   }
582   r->args = result;
583
584   DBG(r, "REQ[%X] result r->args=[%s]",               (unsigned int)r, r->args);
585   DBG(r, "REQ[%X] end   chxj_convert_input_header()", (unsigned int)r);
586   return 0;
587 }
588
589
590 /**
591  * It converts it from POSTDATA .
592  *
593  * @param r   [i]
594  * @param src [i]   It is POSTDATA character string.
595  * @param len [i/o] It is length of former HTML character string.
596  */
597 static char *
598 chxj_input_convert(
599   request_rec         *r, 
600   const char          **src, 
601   apr_size_t          *len, 
602   chxjconvrule_entry  *entryp)
603 {
604   char     *pair;
605   char     *name;
606   char     *value;
607   char     *pstate;
608   char     *vstate;
609   char     *s;
610   char     *result;
611   cookie_t *cookie = NULL;
612   char     *buff_pre;
613   int      no_update_flag = 0;
614
615   s        = apr_pstrdup(r->pool, *src);
616   buff_pre = apr_pstrdup(r->pool, *src);
617
618   result = qs_alloc_zero_byte_string(r->pool);
619
620   DBG(r, "BEFORE input convert source = [%s]", s);
621
622   for (;;) {
623     char *pair_sv;
624
625     pair = apr_strtok(buff_pre, "&", &pstate);
626     if (pair == NULL)
627       break;
628
629     buff_pre = NULL;
630
631     pair_sv = apr_pstrdup(r->pool, pair);
632
633     name  = apr_strtok(pair, "=", &vstate);
634     value = apr_strtok(NULL, "=", &vstate);
635     if (! name) continue;
636     if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
637       DBG(r, "found cookie no update parameter");
638       no_update_flag++;
639     }
640   }
641
642   /* _chxj_dmy */
643   /* _chxj_c_ */
644   /* _chxj_r_ */
645   /* _chxj_s_ */
646   for (;;) {
647     pair = apr_strtok(s, "&", &pstate);
648     if (pair == NULL)
649       break;
650     s = NULL;
651
652     name  = apr_strtok(pair, "=", &vstate);
653     value = apr_strtok(NULL, "=", &vstate);
654     if (! name) continue;
655     if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
656       if (strlen(result) != 0) 
657         result = apr_pstrcat(r->pool, result, "&", NULL);
658
659       if (strcasecmp(entryp->encoding, "NONE") != 0) {
660         apr_size_t dlen;
661         char *dvalue;
662         char *dname;
663
664         if (value && *value != 0) {
665           value = chxj_url_decode(r->pool, value);
666           dlen   = strlen(value);
667           DBG(r, "************ before encoding[%s]", value);
668
669           dvalue = chxj_rencoding(r, value, &dlen);
670           dvalue = chxj_url_encode(r->pool, dvalue);
671
672           DBG(r, "************ after encoding[%s]", dvalue);
673         }
674         else {
675           dvalue = "";
676         }
677
678         if (name && *name != 0) {
679           name = chxj_url_decode(r->pool, name);
680           dlen = strlen(name);
681           dname = chxj_rencoding(r, name, &dlen);
682           dname = chxj_url_encode(r->pool, dname);
683         }
684         else {
685           dname = "";
686         }
687
688         result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
689       }
690       else {
691         result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
692       }
693     }
694     else
695     if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
696       || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
697       || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
698       if (value == NULL)
699         continue;
700
701       if (strlen(value) == 0)
702         continue;
703
704       if (strlen(result) != 0)
705         result = apr_pstrcat(r->pool, result, "&", NULL);
706
707       if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
708         apr_size_t dlen;
709         char       *dvalue;
710
711         dlen   = strlen(value);
712         value = chxj_url_decode(r->pool, value);
713         DBG(r, "************ before encoding[%s]", value);
714
715         dvalue = chxj_rencoding(r, value, &dlen);
716         dvalue = chxj_url_encode(r->pool,dvalue);
717
718         DBG(r, "************ after encoding[%s]", dvalue);
719
720         result = apr_pstrcat(r->pool, result, &name[8], "=", dvalue, NULL);
721
722       }
723       else {
724         result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
725       }
726     }
727     else
728     if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
729       if (! cookie) {
730         apr_table_unset(r->headers_in, "Cookie");
731         DBG(r, "found cookie parameter[%s]", value);
732         DBG(r, "call start chxj_load_cookie()");
733         cookie_lock_t *lock = chxj_cookie_lock(r);
734         cookie = chxj_load_cookie(r, value);
735         DBG(r, "call end   chxj_load_cookie()");
736         if (! no_update_flag && cookie) {
737           chxj_update_cookie(r, cookie);
738         }
739         chxj_cookie_unlock(r, lock);
740       }
741       if (cookie && cookie->cookie_id) {
742         if (strlen(result) != 0)
743           result = apr_pstrcat(r->pool, result, "&", NULL);
744         result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
745       }
746     }
747     else
748     if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
749       if (strlen(result) != 0)
750         result = apr_pstrcat(r->pool, result, "&", NULL);
751       result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
752     }
753     else
754     if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX,     sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
755       apr_size_t dlen;
756       char*      dvalue;
757       dlen   = strlen(value);
758       if (dlen && value) {
759         value = chxj_url_decode(r->pool, value);
760         dvalue = chxj_rencoding(r, value, &dlen);
761         dvalue = chxj_url_encode(r->pool,dvalue);
762         if (r->args && strlen(r->args) > 0) {
763           r->args = apr_pstrcat(r->pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
764         }
765         else {
766           r->args = apr_pstrcat(r->pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
767         }
768       }
769     }
770     else
771     if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
772       apr_size_t dlen;
773       char*      dvalue;
774       dlen   = strlen(value);
775       if (dlen && value) {
776         value = chxj_url_decode(r->pool, value);
777         dvalue = chxj_rencoding(r, value, &dlen);
778         dvalue = chxj_url_encode(r->pool,dvalue);
779         if (r->args && strlen(r->args) > 0) {
780           r->args = apr_pstrcat(r->pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
781         }
782         else {
783           r->args = apr_pstrcat(r->pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
784         }
785       }
786     }
787     DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
788   }
789   *len = strlen(result);
790   apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
791   if (! no_update_flag) {
792     result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
793   }
794
795   DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
796   DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
797
798   return result;
799 }
800
801
802 /**
803  * The received data is returned to the filter.
804  *
805  * @param f    [i/o] It is a filter. 
806  * @param data [i]   It is data returned to the filter. 
807  * @param len  [i]   It is length of the data returned to the filter. 
808  */
809 static apr_status_t 
810 pass_data_to_filter(ap_filter_t *f, const char *data, 
811                                         apr_size_t len)
812 {
813   request_rec         *r = f->r;
814   conn_rec            *c = r->connection;
815   apr_status_t        rv;
816   apr_bucket_brigade  *bb;
817   apr_bucket          *b;
818
819   DBG(r, "start pass_data_to_filter()");
820
821   bb = apr_brigade_create(r->pool, c->bucket_alloc);
822   b  = apr_bucket_transient_create(data, len, c->bucket_alloc);
823
824   APR_BRIGADE_INSERT_TAIL(bb, b);
825   b = apr_bucket_eos_create(f->c->bucket_alloc);
826   APR_BRIGADE_INSERT_TAIL(bb, b);
827
828   rv = ap_pass_brigade(f->next, bb);
829   if (rv != APR_SUCCESS) {
830     DBG(r, "ap_pass_brigade()");
831     return rv;
832   }
833
834   DBG(r, "end pass_data_to_filter()");
835
836   return rv;
837 }
838
839
840 /**
841  * It is the main loop of the output filter. 
842  *
843  * @param f   [i/o] It is a filter.
844  * @param bb  [i]   
845  */
846 static apr_status_t 
847 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
848 {
849   request_rec         *r;
850   apr_status_t        rv;
851   apr_bucket          *b;
852   const char          *data;
853   char                *user_agent = NULL;
854   apr_size_t          len;
855   mod_chxj_ctx        *ctx = (mod_chxj_ctx *)f->ctx;
856   cookie_t            *cookie = NULL;
857   mod_chxj_config     *dconf;
858   chxjconvrule_entry  *entryp = NULL;
859   device_table        *spec = NULL;
860   apr_pool_t          *pool;
861
862   DBG(f->r, "start of chxj_output_filter()");
863   r  = f->r;
864   rv = APR_SUCCESS;
865
866   apr_pool_create(&pool, r->pool);
867
868   entryp = ctx->entryp;
869   spec   = ctx->spec;
870   dconf  = chxj_get_module_config(r->per_dir_config, &chxj_module);
871
872   if (r->content_type) {
873     if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
874     &&  ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
875     &&  ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
876     &&  ! (dconf->image == CHXJ_IMG_ON
877           && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
878           && STRNCASEEQ('i','I',"image/",  r->content_type, sizeof("image/") -1)
879           && ( STRCASEEQ('j','J',"jpeg",            &r->content_type[6])         /* JPEG */
880             || STRCASEEQ('j','J',"jp2",             &r->content_type[6])         /* JPEG2000 */
881             || STRCASEEQ('j','J',"jpeg2000",        &r->content_type[6])         /* JPEG2000 */
882             || STRCASEEQ('j','J',"jpeg2000-image",  &r->content_type[6])         /* JPEG2000 */
883             || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6])         /* JPEG2000 */
884             || STRCASEEQ('p','P',"png",             &r->content_type[6])         /* PNG */
885             || STRCASEEQ('x','X',"x-png",           &r->content_type[6])         /* PNG */
886             || STRCASEEQ('g','G',"gif",             &r->content_type[6])))) {     /* GIF */
887       
888       DBG(r, "not convert content-type:[%s] dconf->image:[%d]", r->content_type, dconf->image);
889       if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
890         cookie_lock_t *lock = NULL;
891         DBG(r, "entryp->action == COOKIE_ON_BIT");
892         switch(spec->html_spec_type) {
893         case CHXJ_SPEC_Chtml_1_0:
894         case CHXJ_SPEC_Chtml_2_0:
895         case CHXJ_SPEC_Chtml_3_0:
896         case CHXJ_SPEC_Chtml_4_0:
897         case CHXJ_SPEC_Chtml_5_0:
898         case CHXJ_SPEC_Chtml_6_0:
899         case CHXJ_SPEC_Chtml_7_0:
900         case CHXJ_SPEC_XHtml_Mobile_1_0:
901         case CHXJ_SPEC_Jhtml:
902           lock = chxj_cookie_lock(r);
903           cookie = chxj_save_cookie(r);
904           s_add_cookie_id_if_has_location_header(r, cookie);
905           chxj_cookie_unlock(r, lock);
906           break;
907         default:
908           break;
909         }
910       }
911       if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
912         if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
913           r->status = HTTP_MOVED_TEMPORARILY;
914         }
915       }
916       ap_pass_brigade(f->next, bb);
917       return APR_SUCCESS;
918     }
919   }
920   else {
921     DBG(r, "not convert content-type:[(null)]");
922     ap_pass_brigade(f->next, bb);
923     return APR_SUCCESS;
924   }
925
926
927   for (b = APR_BRIGADE_FIRST(bb);
928        b != APR_BRIGADE_SENTINEL(bb); 
929        b = APR_BUCKET_NEXT(b)) {
930
931     if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
932       DBG(r, "read data[%.*s]",(int)len, data);
933
934       /*--------------------------------------------------------------------*/
935       /* append data                                                        */
936       /*--------------------------------------------------------------------*/
937       char *tmp;
938       DBG(r, "append data start");
939       ctx = (mod_chxj_ctx *)f->ctx;
940
941       if (len > 0) {
942         tmp = apr_palloc(r->pool, ctx->len);
943         memcpy(tmp, ctx->buffer, ctx->len);
944
945         ctx->buffer = apr_palloc(pool, ctx->len + len);
946
947         memcpy(ctx->buffer, tmp, ctx->len);
948         memcpy(&ctx->buffer[ctx->len], data, len);
949
950         ctx->len += len;
951       }
952       DBG(r, "append data end");
953     }
954
955     if (APR_BUCKET_IS_EOS(b)) {
956
957       DBG(r, "eos");
958       /*----------------------------------------------------------------------*/
959       /* End Of File                                                          */
960       /*----------------------------------------------------------------------*/
961       if (ctx) {
962         cookie_lock_t *lock = NULL;
963         ctx = (mod_chxj_ctx *)f->ctx;
964
965         DBG(r, "content_type=[%s]", r->content_type);
966         lock = chxj_cookie_lock(r);
967
968         if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN 
969             && r->content_type 
970             && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
971             ||  STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
972           DBG(r, "detect convert target:[%s]", r->content_type);
973
974           if (ctx->len) {
975             char *tmp;
976
977             tmp = apr_palloc(pool, ctx->len + 1);
978
979             memset(tmp, 0, ctx->len + 1);
980             memcpy(tmp, ctx->buffer, ctx->len);
981
982             ctx->buffer = chxj_convert(r, 
983                                        (const char **)&tmp, 
984                                        (apr_size_t *)&ctx->len,
985                                        spec,
986                                        user_agent, &cookie);
987
988           }
989           else {
990             ctx->buffer = apr_psprintf(r->pool, "\n");
991             ctx->len += 1;
992             ctx->buffer = chxj_convert(r, 
993                                        (const char **)&ctx->buffer, 
994                                        (apr_size_t *)&ctx->len,
995                                        spec,
996                                        user_agent, &cookie);
997
998           }
999         }
1000         if (r->content_type
1001             && *(char *)r->content_type == 't'
1002             && strncmp(r->content_type, "text/xml",   8) == 0) {
1003           DBG(r, "text/XML");
1004
1005           Doc       doc;
1006           Node      *root;
1007           Node      *child;
1008           qr_code_t qrcode;
1009           int       sts;
1010       
1011           memset(&doc,    0, sizeof(Doc));
1012           memset(&qrcode, 0, sizeof(qr_code_t));
1013           doc.r = r;
1014           doc.parse_mode  = PARSE_MODE_CHTML;
1015           qrcode.doc      = &doc;
1016           qrcode.r        = r;
1017       
1018           qs_init_malloc(&doc);
1019       
1020           root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1021
1022           sts = 0;
1023           for (child = qs_get_child_node(&doc,root);
1024                child ;
1025                child = qs_get_next_node(&doc,child)) {
1026             char *name = qs_get_node_name(&doc,child);
1027             if (strcasecmp("qrcode",name) == 0) {
1028               sts++;
1029               break;
1030             }
1031           }
1032           qs_all_free(&doc,QX_LOGMARK);
1033           if (sts) {
1034             r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1035             chxj_qrcode_node_to_qrcode(&qrcode, root);
1036             sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1037             if (sts != OK) {
1038               ERR(r, "qrcode create failed.");
1039               chxj_cookie_unlock(r, lock);
1040               return sts;
1041             }
1042             r->content_type = apr_psprintf(r->pool, "image/jpeg");
1043           }
1044         }
1045
1046         if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN 
1047             && r->content_type 
1048             && ( *r->content_type == 'i' || *r->content_type == 'I')
1049             && dconf->image == CHXJ_IMG_ON
1050             && strncasecmp("image/", r->content_type, 6) == 0
1051             && ( STRCASEEQ('j','J',"jpeg",            &r->content_type[6])         /* JPEG */
1052               || STRCASEEQ('j','J',"jp2",             &r->content_type[6])         /* JPEG2000 */
1053               || STRCASEEQ('j','J',"jpeg2000",        &r->content_type[6])         /* JPEG2000 */
1054               || STRCASEEQ('j','J',"jpeg2000-image",  &r->content_type[6])         /* JPEG2000 */
1055               || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6])         /* JPEG2000 */
1056               || STRCASEEQ('p','P',"png",             &r->content_type[6])         /* PNG */
1057               || STRCASEEQ('x','X',"x-png",           &r->content_type[6])         /* PNG */
1058               || STRCASEEQ('g','G',"gif",             &r->content_type[6]))) {     /* GIF */
1059           if (ctx->len) {
1060             char *tmp;
1061
1062             tmp = apr_palloc(pool, ctx->len + 1);
1063
1064             memset(tmp, 0, ctx->len + 1);
1065             memcpy(tmp, ctx->buffer, ctx->len);
1066             ctx->buffer = 
1067               chxj_convert_image(r, 
1068                                   (const char **)&tmp,
1069                                   (apr_size_t *)&ctx->len);
1070             if (ctx->buffer == NULL) {
1071               ctx->buffer = tmp;
1072             }
1073           }
1074         }
1075
1076         apr_table_unset(r->headers_out, "Content-Length");
1077         apr_table_unset(r->err_headers_out, "Content-Length");
1078         ap_set_content_length(r, (apr_off_t)ctx->len);
1079
1080         
1081         if (ctx->len > 0) {
1082           DBG(r, "call pass_data_to_filter()");
1083           s_add_cookie_id_if_has_location_header(r, cookie);
1084           if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1085             if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1086               r->status = HTTP_MOVED_TEMPORARILY;
1087             }
1088           }
1089           chxj_cookie_unlock(r,lock);
1090           rv = pass_data_to_filter(f, 
1091                                    (const char *)ctx->buffer, 
1092                                    (apr_size_t)ctx->len);
1093         }
1094         else {
1095           chxj_cookie_unlock(r, lock);
1096
1097         }
1098         return rv;
1099       }
1100       else {
1101         DBG(r, " SAVE COOKIE[%x]", entryp->action);
1102
1103         /*
1104          * save cookie.
1105          */
1106         if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1107           cookie_lock_t *lock = NULL;
1108           DBG(r, "entryp->action == COOKIE_ON_BIT");
1109           switch(spec->html_spec_type) {
1110           case CHXJ_SPEC_Chtml_1_0:
1111           case CHXJ_SPEC_Chtml_2_0:
1112           case CHXJ_SPEC_Chtml_3_0:
1113           case CHXJ_SPEC_Chtml_4_0:
1114           case CHXJ_SPEC_Chtml_5_0:
1115           case CHXJ_SPEC_Chtml_6_0:
1116           case CHXJ_SPEC_Chtml_7_0:
1117           case CHXJ_SPEC_XHtml_Mobile_1_0:
1118           case CHXJ_SPEC_Jhtml:
1119             lock = chxj_cookie_lock(r);
1120             cookie = chxj_save_cookie(r);
1121             /*
1122              * Location Header Check to add cookie parameter.
1123              */
1124             s_add_cookie_id_if_has_location_header(r, cookie);
1125             chxj_cookie_unlock(r, lock);
1126             apr_table_unset(r->headers_out, "Set-Cookie");
1127             apr_table_unset(r->err_headers_out, "Set-Cookie");
1128             break;
1129
1130           default:
1131             break;
1132           }
1133         }
1134         if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1135           if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1136             r->status = HTTP_MOVED_TEMPORARILY;
1137           }
1138         }
1139         apr_table_setn(r->headers_out, "Content-Length", "0");
1140         DBG(r, "call pass_data_to_filter()");
1141         rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1142         return rv;
1143       }
1144     }
1145   }
1146   apr_brigade_destroy(bb);
1147
1148   DBG(r, "end of output filter");
1149
1150   return APR_SUCCESS;
1151 }
1152
1153 /**
1154  * Add Cookie_id if it has location header.
1155  */
1156 static void
1157 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1158 {
1159   char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1160   if (! location_header) {
1161     location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1162   }
1163   if (cookie && location_header) {
1164     DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)r, location_header);
1165     location_header = chxj_add_cookie_parameter(r,
1166                                                 location_header,
1167                                                 cookie);
1168     apr_table_unset(r->headers_out, "Location");
1169     apr_table_setn(r->headers_out, "Location", location_header);
1170     DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)r, location_header);
1171     if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1172       r->status = HTTP_MOVED_TEMPORARILY;
1173     }
1174   }
1175 }
1176
1177 /**
1178  * It is the main loop of the input filter handler. 
1179  *
1180  */
1181 static int 
1182 chxj_input_handler(request_rec *r)
1183 {
1184   mod_chxj_config     *dconf;
1185   chxjconvrule_entry  *entryp = NULL;
1186   device_table        *spec   = NULL;
1187   char                *post_data = NULL;
1188   apr_size_t          post_data_len = 0;
1189   char                *response;
1190   char                *user_agent;
1191   apr_pool_t          *pool;
1192   apr_size_t          ii;
1193   
1194   DBG(r, "start of chxj_input_handler()");
1195
1196   if (strcasecmp(r->handler, "chxj-input-handler")) {
1197     DBG(r, "end chxj_input_handler()");
1198     return DECLINED;
1199   }
1200   apr_pool_create(&pool, r->pool);
1201
1202   dconf      = chxj_get_module_config(r->per_dir_config, &chxj_module);
1203   user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
1204   spec       = chxj_specified_device(r, user_agent);
1205   entryp     = chxj_apply_convrule(r, dconf->convrules);
1206
1207   post_data = apr_pstrdup(pool, "");
1208   if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1209     if (ap_should_client_block(r)) {
1210       while (post_data_len < CHXJ_POST_MAX) {
1211 #define BUFSZ (256)
1212         char buffer[BUFSZ];
1213         int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1214         if (read_bytes<=0) {
1215           break;
1216         }
1217         buffer[read_bytes] = '\0';
1218         post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1219         post_data_len += read_bytes;
1220 #undef BUFSZ
1221       }
1222     }
1223   }
1224
1225   /* 
1226    * now convert.
1227    */
1228   if (post_data_len > 0) {
1229     post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp);
1230     DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1231   }
1232
1233   char *url_path;
1234   if (dconf->forward_url_base) {
1235     url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1236   }
1237   else {
1238     url_path = apr_psprintf(pool, "%s://%s:%d%s", chxj_apache_run_http_scheme(r), ap_get_server_name(r), ap_get_server_port(r), r->uri);
1239   }
1240   if (r->args) {
1241     url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1242   }
1243   DBG(r, "==> new url_path:[%s]", url_path);
1244
1245   apr_size_t res_len;
1246   apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1247   apr_table_unset(r->headers_in, "Content-Length");
1248   apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1249   response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len);
1250   DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1251   DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1252   for (ii=0; ii<res_len/64; ii++) {
1253     DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1254   }
1255   DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1256
1257   char *chunked;
1258   if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1259     if (strcasecmp(chunked, "chunked") == 0) {
1260       r->chunked = 1;  
1261       apr_table_unset(r->headers_out, "Transfer-Encoding");
1262     }
1263   }
1264   {
1265     apr_pool_t *wpool;
1266     apr_pool_create(&wpool, r->pool);
1267     apr_bucket_brigade *bb;
1268     apr_bucket *e;
1269     apr_status_t rv;
1270     conn_rec *c = r->connection;
1271     
1272     bb = apr_brigade_create(wpool, c->bucket_alloc);
1273     e  = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1274     APR_BRIGADE_INSERT_TAIL(bb, e);
1275     e = apr_bucket_eos_create(c->bucket_alloc);
1276     APR_BRIGADE_INSERT_TAIL(bb, e);
1277     if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1278       ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1279       return rv;
1280     }
1281     apr_brigade_cleanup(bb);
1282   }
1283
1284   DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1285   return APR_SUCCESS;
1286 }
1287
1288 static mod_chxj_global_config *
1289 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1290 {
1291   mod_chxj_global_config *conf;
1292
1293   SDBG(s, "start chxj_global_config_create()");
1294
1295   /*--------------------------------------------------------------------------*/
1296   /* allocate an own subpool which survives server restarts                   */
1297   /*--------------------------------------------------------------------------*/
1298   conf = (mod_chxj_global_config *)apr_palloc(pool, 
1299                   sizeof(mod_chxj_global_config));
1300 #if 0
1301   conf->cookie_db_lock = NULL;
1302 #endif
1303   SDBG(s, "end   chxj_global_config_create()");
1304
1305   return conf;
1306 }
1307
1308
1309 /**
1310  * initialize chxj module
1311  */
1312 static int 
1313 chxj_init_module(apr_pool_t *p, 
1314                  apr_pool_t *UNUSED(plog), 
1315                  apr_pool_t *UNUSED(ptemp), 
1316                  server_rec *s)
1317 {
1318   void *user_data;
1319   apr_status_t rv;
1320
1321   SDBG(s, "start chxj_init_module()");
1322
1323   apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1324   SDBG(s, " ");
1325   if (user_data == NULL) {
1326     SDBG(s, " ");
1327     /*
1328      * dummy user_data set.
1329      */
1330     apr_pool_userdata_set(
1331       (const void *)(1), 
1332       CHXJ_MOD_CONFIG_KEY, 
1333       apr_pool_cleanup_null, 
1334       s->process->pool);
1335     SDBG(s, "end  chxj_init_module()");
1336     return OK;
1337   }
1338
1339   ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1340
1341   if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL,  APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1342     char errstr[255];
1343     SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1344     return HTTP_INTERNAL_SERVER_ERROR;
1345   }
1346
1347   SDBG(s, "end  chxj_init_module()");
1348
1349   return OK;
1350 }
1351
1352
1353 static void 
1354 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1355 {
1356   apr_status_t rv;
1357   SDBG(s, "start chxj_child_init()");
1358   if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1359     char errstr[255];
1360     SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1361   }
1362   SDBG(s, "end   chxj_child_init()");
1363 }
1364
1365
1366 /**
1367  * A set structure of each server is generated. 
1368  * 
1369  * @param p
1370  * @param s
1371  */
1372 static void *
1373 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1374 {
1375   mod_chxj_global_config *gc;
1376
1377   gc = chxj_global_config_create(p,s);
1378
1379   return gc;
1380 }
1381
1382
1383 static int
1384 chxj_translate_name(request_rec *r)
1385 {
1386   DBG(r, "REQ[%X] =======================================================================", (unsigned int)r);
1387   DBG(r, "REQ[%X] ", (unsigned int)r);
1388   DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1389   DBG(r, "REQ[%X] ", (unsigned int)r);
1390   DBG(r, "REQ[%X] =======================================================================", (unsigned int)r);
1391   return chxj_trans_name(r);
1392 }
1393
1394
1395 static void 
1396 chxj_insert_filter(request_rec *r)
1397 {
1398   char                *user_agent;
1399   device_table        *spec;
1400   mod_chxj_config     *dconf;
1401   chxjconvrule_entry  *entryp;
1402   mod_chxj_ctx        *ctx;
1403   apr_status_t        rv;
1404   char                *contentType;
1405
1406   DBG(r, "start chxj_insert_filter()");
1407
1408   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1409
1410   user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1411
1412   contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1413   if (contentType
1414       && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1415     DBG(r, "detect multipart/form-data ==> no target");
1416     return;
1417   }
1418
1419   spec = chxj_specified_device(r, user_agent);
1420   entryp = chxj_apply_convrule(r, dconf->convrules);
1421   if (!entryp) {
1422     DBG(r, "end chxj_insert_filter()");
1423     return;
1424   }
1425   ctx = apr_palloc(r->pool, sizeof(*ctx));
1426   memset(ctx, 0, sizeof(*ctx));
1427   if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1428     ERR(r, "failed: new pool create. rv:[%d]", rv);
1429     return;
1430   }
1431   ctx->entryp = entryp;
1432   ctx->spec   = spec;
1433   ctx->buffer = apr_palloc(ctx->pool, 1);
1434   ctx->buffer[0] = 0;
1435
1436   if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1437     DBG(r,"EngineOff");
1438     return;
1439   }
1440
1441   switch(spec->html_spec_type) {
1442   case CHXJ_SPEC_Chtml_1_0:
1443   case CHXJ_SPEC_Chtml_2_0:
1444   case CHXJ_SPEC_Chtml_3_0:
1445   case CHXJ_SPEC_Chtml_4_0:
1446   case CHXJ_SPEC_Chtml_5_0:
1447   case CHXJ_SPEC_Chtml_6_0:
1448   case CHXJ_SPEC_Chtml_7_0:
1449   case CHXJ_SPEC_XHtml_Mobile_1_0:
1450   case CHXJ_SPEC_Hdml:
1451   case CHXJ_SPEC_Jhtml:
1452   case CHXJ_SPEC_Jxhtml:
1453     break;
1454
1455   default:
1456     return;
1457   }
1458
1459
1460   if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1461     ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1462     DBG(r, "added Output Filter");
1463   }
1464
1465   DBG(r, "end   chxj_insert_filter()");
1466 }
1467
1468
1469 /**
1470  * The hook is registered.
1471  *
1472  * @param p
1473  */
1474 static void 
1475 chxj_register_hooks(apr_pool_t *UNUSED(p))
1476 {
1477   ap_hook_post_config(chxj_init_module,
1478                       NULL,
1479                       NULL,
1480                       APR_HOOK_REALLY_FIRST);
1481   ap_hook_child_init(chxj_child_init, 
1482                      NULL, 
1483                      NULL, 
1484                      APR_HOOK_REALLY_FIRST);
1485   ap_register_output_filter (
1486                       "chxj_output_filter", 
1487                       chxj_output_filter, 
1488                       NULL, 
1489                       AP_FTYPE_RESOURCE);
1490   ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1491   ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1492   ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1493   ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1494   ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1495   ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1496 }
1497
1498
1499 /**
1500  * A set structure according to the directory is generated. 
1501  *
1502  * @param p
1503  * @param arg
1504  */
1505 static void * 
1506 chxj_create_per_dir_config(apr_pool_t *p, char *arg) 
1507 {
1508   mod_chxj_config *conf;
1509
1510   conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1511   conf->device_data_file = NULL;
1512   conf->devices          = NULL;
1513   conf->emoji_data_file  = NULL;
1514   conf->emoji            = NULL;
1515   conf->emoji_tail       = NULL;
1516   conf->image            = CHXJ_IMG_NONE;
1517   conf->image_cache_dir  = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1518   conf->image_cache_limit = 0;
1519   conf->server_side_encoding = NULL;
1520   conf->cookie_db_dir    = NULL;
1521   conf->cookie_timeout   = 0;
1522   conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1523   conf->cookie_lazy_mode  = 0;
1524 #if defined(USE_MYSQL_COOKIE)
1525   memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1526   conf->mysql.port       = MYSQL_PORT;
1527   conf->mysql.host       = NULL;
1528 #endif
1529 #if defined(USE_MEMCACHE_COOKIE)
1530   memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1531   conf->memcache.host    = NULL;
1532   conf->memcache.port    = 0;
1533 #endif
1534   conf->forward_url_base = NULL;
1535   conf->forward_server_ip = NULL;
1536
1537   if (arg == NULL) {
1538     conf->dir                  = NULL;
1539   }
1540   else {
1541     conf->dir                  = apr_pcalloc(p, strlen(arg)+1);
1542     memset(conf->dir, 0, strlen(arg)+1);
1543     strcpy(conf->dir, arg);
1544   }
1545   conf->convrules   = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1546
1547   /* Default is copyleft */
1548   conf->image_copyright = NULL; 
1549
1550   return conf;
1551 }
1552
1553
1554 /*
1555  *  Merge per-directory CHXJ configurations
1556  */
1557 static void *
1558 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1559 {
1560   mod_chxj_config *base;
1561   mod_chxj_config *add;
1562   mod_chxj_config *mrg;
1563
1564   base = (mod_chxj_config *)basev;
1565   add  = (mod_chxj_config *)addv;
1566   mrg  = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1567
1568   mrg->device_data_file = NULL;
1569   mrg->devices          = NULL;
1570   mrg->emoji_data_file  = NULL;
1571   mrg->image            = CHXJ_IMG_NONE;
1572   mrg->image_cache_dir  = NULL;
1573   mrg->image_copyright  = NULL;
1574   mrg->image_cache_limit  = 0;
1575   mrg->emoji            = NULL;
1576   mrg->emoji_tail       = NULL;
1577   mrg->new_line_type    = NLTYPE_NIL;
1578   mrg->forward_url_base = NULL;
1579   mrg->forward_server_ip = NULL;
1580
1581   mrg->dir = apr_pstrdup(p, add->dir);
1582
1583   if (! add->device_data_file) {
1584     mrg->devices = base->devices;
1585     mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1586   }
1587   else {
1588     mrg->devices = add->devices;
1589     mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1590   }
1591
1592   if (! add->emoji_data_file) {
1593     mrg->emoji = base->emoji;
1594     mrg->emoji_tail = base->emoji_tail;
1595     mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1596   }
1597   else {
1598     mrg->emoji = add->emoji;
1599     mrg->emoji_tail = add->emoji_tail;
1600     mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1601   }
1602
1603   if (add->image == CHXJ_IMG_NONE) {
1604     mrg->image = base->image;
1605   }
1606   else {
1607     mrg->image = add->image;
1608   }
1609
1610   if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1611     mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1612   }
1613   else {
1614     mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1615   }
1616
1617   if (add->image_cache_limit) {
1618     mrg->image_cache_limit = add->image_cache_limit;
1619   }
1620   else {
1621     mrg->image_cache_limit = base->image_cache_limit;
1622   }
1623
1624   if (add->image_copyright) 
1625     mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1626   else
1627     mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1628
1629   if (add->server_side_encoding) {
1630     mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1631   }
1632   else 
1633   if (base->server_side_encoding) {
1634     mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1635   }
1636   else {
1637     mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1638   }
1639
1640   mrg->convrules    = apr_array_append(p, add->convrules, base->convrules);
1641
1642   if (add->cookie_db_dir) {
1643     mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1644   }
1645   else
1646   if (base->cookie_db_dir) {
1647     mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1648   }
1649   else {
1650     mrg->cookie_db_dir = NULL;
1651   }
1652
1653   if (add->cookie_timeout) {
1654     mrg->cookie_timeout   = add->cookie_timeout;
1655   }
1656   else
1657   if (base->cookie_db_dir) {
1658     mrg->cookie_timeout   = base->cookie_timeout;
1659   }
1660   else {
1661     mrg->cookie_timeout   = 0;
1662   }
1663
1664 #if defined(USE_MYSQL_COOKIE)
1665   if (add->mysql.host) {
1666     mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1667   }
1668   else if (base->mysql.host) {
1669     mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1670   }
1671   else {
1672     mrg->mysql.host = NULL;
1673   }
1674   if (add->mysql.port) {
1675     mrg->mysql.port = add->mysql.port;
1676   }
1677   else if (base->mysql.port) {
1678     mrg->mysql.port = base->mysql.port;
1679   }
1680   else {
1681     mrg->mysql.port = 0;
1682   }
1683
1684   if (add->mysql.database) {
1685     mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1686   }
1687   else if (base->mysql.database) {
1688     mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1689   }
1690   else {
1691     mrg->mysql.database = NULL;
1692   }
1693
1694   if (add->mysql.username) {
1695     mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1696   }
1697   else if (base->mysql.username) {
1698     mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1699   }
1700   else {
1701     mrg->mysql.username = NULL;
1702   }
1703
1704   if (add->mysql.password) {
1705     mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1706   }
1707   else if (base->mysql.password) {
1708     mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1709   }
1710   else {
1711     mrg->mysql.password = NULL;
1712   }
1713
1714   if (add->mysql.tablename) {
1715     mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1716   }
1717   else if (base->mysql.tablename) {
1718     mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1719   }
1720   else {
1721     mrg->mysql.tablename = NULL;
1722   }
1723
1724   if (add->mysql.socket_path) {
1725     mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1726   }
1727   else if (base->mysql.socket_path) {
1728     mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1729   }
1730   else {
1731     mrg->mysql.socket_path = NULL;
1732   }
1733
1734   if (add->mysql.charset) {
1735     mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1736   }
1737   else if (base->mysql.charset) {
1738     mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1739   }
1740   else {
1741     mrg->mysql.charset = NULL;
1742   }
1743 #endif
1744 #if defined(USE_MEMCACHE_COOKIE)
1745   if (add->memcache.host) {
1746     mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1747   }
1748   else if (base->memcache.host) {
1749     mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1750   }
1751   else {
1752     mrg->memcache.host = NULL;
1753   }
1754   if (add->memcache.port) {
1755     mrg->memcache.port = add->memcache.port;
1756   }
1757   else if (base->memcache.port) {
1758     mrg->memcache.port = base->memcache.port;
1759   }
1760   else {
1761     mrg->memcache.port = 0;
1762   }
1763 #endif
1764   if (add->cookie_store_type) {
1765     mrg->cookie_store_type = add->cookie_store_type;
1766   }
1767   else if (base->cookie_store_type) {
1768     mrg->cookie_store_type = base->cookie_store_type;
1769   }
1770   else {
1771     mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1772   }
1773   if (add->cookie_lazy_mode) {
1774     mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1775   }
1776   else if (base->cookie_lazy_mode) {
1777     mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1778   }
1779   else {
1780     mrg->cookie_lazy_mode = 0;
1781   }
1782   if (add->new_line_type) {
1783     mrg->new_line_type = add->new_line_type;
1784   }
1785   else if (base->new_line_type) {
1786     mrg->new_line_type = base->new_line_type;
1787   }
1788   else {
1789     mrg->new_line_type = NLTYPE_NIL;
1790   }
1791   return mrg;
1792 }
1793
1794
1795 static int
1796 chxj_command_parse_take5(
1797   const char *arg, 
1798   char       **prm1, 
1799   char       **prm2, 
1800   char       **prm3, 
1801   char       **prm4, 
1802   char       **prm5)
1803 {
1804   int  isquoted;
1805   char *strp;
1806
1807   strp = (char *)arg;
1808
1809   for (;*strp == ' '||*strp == '\t'; strp++) ;
1810
1811   isquoted = 0; 
1812   if (*strp == '"') { 
1813     isquoted = 1;
1814     strp++;
1815   }
1816
1817   *prm1 = strp;
1818
1819   for (; *strp != '\0'; strp++) {
1820     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1821     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1822       strp++;
1823       continue;
1824     }
1825
1826     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1827     ||  (isquoted  && *strp == '"'))
1828       break;
1829   }
1830
1831   if (! *strp) {
1832     *prm2 = strp;
1833     *prm3 = strp;
1834     *prm4 = strp;
1835     *prm5 = strp;
1836     return 1;
1837   }
1838
1839   *strp++ = '\0';
1840
1841   for (;*strp == ' '||*strp == '\t'; strp++) ;
1842
1843   isquoted = 0; 
1844   if (*strp == '"') { 
1845     isquoted = 1;
1846     strp++;
1847   }
1848
1849   *prm2 = strp;
1850   for (; *strp != '\0'; strp++) {
1851     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1852     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1853       strp++;
1854       continue;
1855     }
1856
1857     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1858     ||  (isquoted  && *strp == '"'))
1859       break;
1860   }
1861
1862   if (! *strp) {
1863     *prm3 = strp;
1864     *prm4 = strp;
1865     *prm5 = strp;
1866     return 0;
1867   }
1868
1869   *strp++ = '\0';
1870
1871   for (;*strp == ' '||*strp == '\t'; strp++);
1872
1873   isquoted = 0; 
1874   if (*strp == '"') { 
1875     isquoted = 1;
1876     strp++;
1877   }
1878   *prm3 = strp;
1879   for (; *strp != '\0'; strp++) {
1880     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1881     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1882       strp++;
1883       continue;
1884     }
1885
1886     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1887     ||  (isquoted  && *strp == '"'))
1888       break;
1889   }
1890
1891   if (! *strp) {
1892     *prm4 = strp;
1893     *prm5 = strp;
1894     return 0;
1895   }
1896
1897   *strp++ = '\0';
1898
1899   for (;*strp == ' '||*strp == '\t'; strp++);
1900
1901   isquoted = 0; 
1902   if (*strp == '"') { 
1903     isquoted = 1;
1904     strp++;
1905   }
1906   *prm4 = strp;
1907   for (; *strp != '\0'; strp++) {
1908     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1909     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1910       strp++;
1911       continue;
1912     }
1913
1914     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1915     ||  (isquoted  && *strp == '"'))
1916       break;
1917   }
1918
1919   if (! *strp) {
1920     *prm5 = strp;
1921     return 0;
1922   }
1923
1924   *strp++ = '\0';
1925
1926   for (;*strp == ' '||*strp == '\t'; strp++);
1927
1928   isquoted = 0; 
1929   if (*strp == '"') { 
1930     isquoted = 1;
1931     strp++;
1932   }
1933   *prm5 = strp;
1934   for (; *strp != '\0'; strp++) {
1935     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1936     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1937       strp++;
1938       continue;
1939     }
1940
1941     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1942     ||  (isquoted  && *strp == '"'))
1943       break;
1944   }
1945   *strp = '\0';
1946
1947   return 0;
1948 }
1949
1950
1951 /**
1952  * The device definition file is loaded. 
1953  *
1954  * @param arg     [i]   The name of the device definition file is specified.
1955  * @param mconfig [i/o] The pointer to a set structure is specified. 
1956  * @param parms   [i]   
1957  */
1958 static const char * 
1959 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg) 
1960 {
1961   mod_chxj_config  *conf;
1962   Doc              doc;
1963
1964   doc.r = NULL;
1965
1966   if (strlen(arg) > 256) 
1967     return "mod_chxj: device data filename too long.";
1968
1969   conf = (mod_chxj_config *)mconfig;
1970   conf->device_data_file = apr_pstrdup(parms->pool, arg);
1971
1972   qs_init_malloc(&doc);
1973   qs_init_root_node(&doc);
1974
1975   qs_parse_file((Doc *)&doc, (const char *)arg);
1976   chxj_load_device_data(&doc,parms->pool, conf);
1977   qs_all_free(&doc, QX_LOGMARK);
1978
1979   return NULL;
1980 }
1981
1982
1983 /**
1984  * Device definition information is loaded. 
1985  *
1986  * @param parms 
1987  * @param arg     [i]   The name of the device definition file is specified. 
1988  * @param mconfig [i/o] The pointer to a set structure is specified. 
1989  * @return 
1990  */
1991 static const char * 
1992 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg) 
1993 {
1994   mod_chxj_config *conf;
1995   char            *rtn;
1996   Doc              doc;
1997
1998   doc.r = NULL;
1999
2000
2001   if (strlen(arg) > 256) 
2002     return "mod_chxj: emoji data filename too long.";
2003
2004   conf = (mod_chxj_config *)mconfig;
2005   conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2006   qs_init_malloc(&doc);
2007   qs_init_root_node(&doc);
2008
2009   qs_parse_file((Doc *)&doc, (const char *)arg);
2010
2011   rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2012
2013   qs_all_free(&doc, QX_LOGMARK);
2014
2015
2016   return rtn;
2017 }
2018
2019
2020 static const char * 
2021 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg) 
2022 {
2023   mod_chxj_config *conf;
2024   Doc              doc;
2025
2026   doc.r = NULL;
2027
2028   if (strlen(arg) > 256) 
2029     return "image uri is too long.";
2030
2031   conf = (mod_chxj_config*)mconfig;
2032   if (strcasecmp("ON", arg) == 0) {
2033     conf->image = CHXJ_IMG_ON;
2034   }
2035   else {
2036     conf->image = CHXJ_IMG_OFF;
2037   }
2038
2039   return NULL;
2040 }
2041
2042
2043 static const char * 
2044 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg) 
2045 {
2046   mod_chxj_config *conf;
2047   Doc              doc;
2048
2049   doc.r = NULL;
2050
2051   if (strlen(arg) > 256) 
2052     return "cache dir name is too long.";
2053
2054   conf = (mod_chxj_config *)mconfig;
2055   conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2056
2057   return NULL;
2058 }
2059
2060
2061 static const char * 
2062 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg) 
2063 {
2064   mod_chxj_config *conf;
2065   Doc              doc;
2066
2067   doc.r = NULL;
2068
2069   if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN) 
2070     return "cache size is too long.";
2071
2072   conf = (mod_chxj_config *)mconfig;
2073   errno = 0;
2074   /* 
2075    * I use strtol function because strtoul is not portable function. 
2076    */
2077   conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2078   switch (errno) {
2079   case EINVAL:
2080     return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2081   case ERANGE:
2082     return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2083   default:
2084     break;
2085   }
2086   return NULL;
2087 }
2088
2089
2090 static const char * 
2091 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg) 
2092 {
2093   mod_chxj_config *conf;
2094   Doc              doc;
2095
2096   doc.r = NULL;
2097
2098   if (strlen(arg) > 256) 
2099     return "Copyright Flag is too long.";
2100
2101   conf = (mod_chxj_config *)mconfig;
2102   conf->image_copyright = apr_pstrdup(parms->pool, arg);
2103
2104   return NULL;
2105 }
2106
2107
2108 static const char *
2109 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2110 {
2111   int                 mode;
2112   ap_regex_t          *regexp;
2113   mod_chxj_config     *dconf;
2114   chxjconvrule_entry  *newrule;
2115   char                *prm1;
2116   char                *prm2;
2117   char                *prm3;
2118   char                *prm4;
2119   char                *prm5;
2120   char                *pstate;
2121   char                *action;
2122   char                *pp;
2123
2124   dconf = (mod_chxj_config *)mconfig;
2125
2126   if (strlen(arg) > 4096) 
2127     return "mod_chxj: ChxjConvertRule: is too long.";
2128
2129   dconf = (mod_chxj_config *)mconfig;
2130   if (dconf->convrules == NULL)
2131     dconf->convrules   = apr_array_make(cmd->pool, 
2132                                         2, 
2133                                         sizeof(chxjconvrule_entry));
2134
2135   newrule = apr_array_push(dconf->convrules);
2136
2137   newrule->flags  = 0;
2138   newrule->action = 0;
2139
2140   if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2141     return "ChxjConvertRule: bad argument line";
2142
2143   newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2144
2145   /* Parse action */
2146   for (;;) {
2147     if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2148       break;
2149     prm2 = NULL;
2150     switch(*action) {
2151     case 'e':
2152     case 'E':
2153       if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2154         newrule->action |= CONVRULE_ENGINE_ON_BIT;
2155       }
2156       else
2157       if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2158         newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2159       }
2160       break;
2161
2162     case 'C':
2163     case 'c':
2164       if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2165         newrule->action |= CONVRULE_COOKIE_ON_BIT;
2166       }
2167       break;
2168     default:
2169       break;
2170     }
2171   }
2172   
2173   pp = prm1;
2174   if (*pp == '!') {
2175     newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2176     pp++;
2177   }
2178
2179   mode = AP_REG_EXTENDED;
2180   if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2181     return "RewriteRule: cannot compile regular expression ";
2182
2183   newrule->regexp = regexp;
2184   if (*prm3)
2185     newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2186   else
2187     newrule->encoding = apr_pstrdup(cmd->pool, "none");
2188
2189   newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2190   if (*prm4)
2191     if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2192       newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2193
2194   newrule->user_agent = NULL;
2195   if (*prm5)
2196     newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2197     
2198   return NULL;
2199 }
2200
2201
2202 static const char *
2203 cmd_set_cookie_dir(
2204   cmd_parms   *cmd, 
2205   void        *mconfig, 
2206   const char  *arg)
2207 {
2208   mod_chxj_config *dconf;
2209
2210
2211   if (strlen(arg) > 4096) 
2212     return "mod_chxj: ChxjCookieDir is too long.";
2213
2214   dconf = (mod_chxj_config *)mconfig;
2215
2216   dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2217
2218   return NULL;
2219 }
2220
2221
2222 static const char *
2223 cmd_set_cookie_timeout(
2224   cmd_parms   *UNUSED(cmd), 
2225   void        *mconfig, 
2226   const char  *arg)
2227 {
2228   mod_chxj_config *dconf;
2229
2230   if (strlen(arg) > 4096) 
2231     return "mod_chxj: ChxjCookieTimeout is too long.";
2232
2233   if (chxj_chk_numeric(arg) != 0)
2234     return "mod_chxj: ChxjCookieTimeout is not numeric.";
2235
2236   dconf = (mod_chxj_config *)mconfig;
2237
2238   dconf->cookie_timeout = atoi(arg);
2239
2240   return NULL;
2241 }
2242
2243
2244 #if defined(USE_MYSQL_COOKIE)
2245 static const char *
2246 cmd_set_cookie_mysql_database(
2247   cmd_parms   *cmd, 
2248   void        *mconfig, 
2249   const char  *arg)
2250 {
2251   mod_chxj_config  *dconf;
2252
2253   if (strlen(arg) > 255) 
2254     return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2255
2256   dconf = (mod_chxj_config *)mconfig;
2257
2258   dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2259
2260   return NULL;
2261 }
2262
2263
2264 static const char *
2265 cmd_set_cookie_mysql_username(
2266   cmd_parms   *cmd, 
2267   void        *mconfig, 
2268   const char  *arg)
2269 {
2270   mod_chxj_config  *dconf;
2271
2272   if (strlen(arg) > 255) 
2273     return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2274
2275   dconf = (mod_chxj_config *)mconfig;
2276
2277   dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2278
2279   return NULL;
2280 }
2281
2282
2283 static const char *
2284 cmd_set_cookie_mysql_password(
2285   cmd_parms   *cmd, 
2286   void        *mconfig, 
2287   const char  *arg)
2288 {
2289   mod_chxj_config  *dconf;
2290
2291   if (strlen(arg) > 255) 
2292     return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2293
2294   dconf = (mod_chxj_config *)mconfig;
2295
2296   dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2297
2298   return NULL;
2299 }
2300
2301
2302 static const char *
2303 cmd_set_cookie_mysql_table_name(
2304   cmd_parms   *cmd, 
2305   void        *mconfig, 
2306   const char  *arg)
2307 {
2308   mod_chxj_config  *dconf;
2309
2310   if (strlen(arg) > 255) 
2311     return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2312
2313   dconf = (mod_chxj_config *)mconfig;
2314
2315   dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2316
2317   return NULL;
2318 }
2319
2320 static const char *
2321 cmd_set_cookie_mysql_port(
2322   cmd_parms   *UNUSED(cmd), 
2323   void        *mconfig, 
2324   const char  *arg)
2325 {
2326   mod_chxj_config *dconf;
2327
2328   if (strlen(arg) > 255) 
2329     return "mod_chxj: ChxjCookieMysqlPort is too long.";
2330
2331   dconf = (mod_chxj_config *)mconfig;
2332
2333   if (chxj_chk_numeric(arg) != 0)
2334     return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2335
2336   dconf = (mod_chxj_config *)mconfig;
2337
2338   dconf->mysql.port = chxj_atoi(arg);
2339
2340   return NULL;
2341 }
2342
2343
2344 static const char *
2345 cmd_set_cookie_mysql_host(
2346   cmd_parms   *cmd, 
2347   void        *mconfig, 
2348   const char  *arg)
2349 {
2350   mod_chxj_config  *dconf;
2351
2352   if (strlen(arg) > 255) 
2353     return "mod_chxj: ChxjCookieMysqlHost is too long.";
2354
2355   dconf = (mod_chxj_config *)mconfig;
2356
2357   dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2358
2359   return NULL;
2360 }
2361
2362
2363 static const char *
2364 cmd_set_cookie_mysql_socket_path(
2365   cmd_parms   *cmd, 
2366   void        *mconfig, 
2367   const char  *arg)
2368 {
2369   mod_chxj_config  *dconf;
2370
2371   if (strlen(arg) > 4096) 
2372     return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2373
2374   dconf = (mod_chxj_config *)mconfig;
2375
2376   dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2377
2378   return NULL;
2379 }
2380
2381
2382 static const char *
2383 cmd_set_cookie_mysql_charset(
2384   cmd_parms   *cmd, 
2385   void        *mconfig, 
2386   const char  *arg)
2387 {
2388   mod_chxj_config  *dconf;
2389
2390   if (strlen(arg) > 255) 
2391     return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2392
2393   dconf = (mod_chxj_config *)mconfig;
2394
2395   dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2396
2397   return NULL;
2398 }
2399 #endif
2400 #if defined(USE_MEMCACHE_COOKIE)
2401 static const char *
2402 cmd_set_cookie_memcache_port(
2403   cmd_parms   *UNUSED(cmd), 
2404   void        *mconfig, 
2405   const char  *arg)
2406 {
2407   mod_chxj_config *dconf;
2408
2409   if (strlen(arg) > 255) 
2410     return "mod_chxj: ChxjCookieMemcachePort is too long.";
2411
2412   dconf = (mod_chxj_config *)mconfig;
2413
2414   if (chxj_chk_numeric(arg) != 0)
2415     return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2416
2417   dconf = (mod_chxj_config *)mconfig;
2418
2419   dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2420
2421   return NULL;
2422 }
2423
2424
2425 static const char *
2426 cmd_set_cookie_memcache_host(
2427   cmd_parms   *cmd, 
2428   void        *mconfig, 
2429   const char  *arg)
2430 {
2431   mod_chxj_config  *dconf;
2432
2433   if (strlen(arg) > 255) 
2434     return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2435
2436   dconf = (mod_chxj_config *)mconfig;
2437
2438   dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2439
2440   return NULL;
2441 }
2442 #endif
2443
2444 static const char *
2445 cmd_set_cookie_lazy_mode(
2446   cmd_parms   *UNUSED(cmd), 
2447   void        *mconfig, 
2448   const char  *arg)
2449 {
2450   mod_chxj_config  *dconf;
2451
2452   if (strlen(arg) > 255) 
2453     return "mod_chxj: ChxjCookieLazyMode is too long.";
2454
2455   dconf = (mod_chxj_config *)mconfig;
2456
2457   if (strcasecmp("TRUE",arg) == 0) {
2458     dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2459   }
2460   else {
2461     dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2462   }
2463
2464   return NULL;
2465 }
2466
2467 static const char *
2468 cmd_set_cookie_store_type(
2469   cmd_parms   *UNUSED(cmd), 
2470   void        *mconfig, 
2471   const char  *arg)
2472 {
2473   mod_chxj_config  *dconf;
2474
2475   if (strlen(arg) > 255) 
2476     return "mod_chxj: ChxjCookieStoreType is too long.";
2477
2478   dconf = (mod_chxj_config *)mconfig;
2479
2480   if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2481     dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2482   }
2483   else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2484     dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2485   }
2486   else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2487     dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2488   }
2489   else {
2490     dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2491   }
2492
2493   return NULL;
2494 }
2495
2496 static const char *
2497 cmd_set_forward_url_base(
2498   cmd_parms   *cmd,
2499   void        *mconfig,
2500   const char  *arg)
2501 {
2502  mod_chxj_config *dconf;
2503
2504   if (strlen(arg) > 255)
2505     return "mod_chxj: ChxjForwardUrlBase is too long.";
2506
2507   dconf = (mod_chxj_config *)mconfig;
2508
2509   dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2510
2511   return NULL;
2512 }
2513
2514 static const char *
2515 cmd_set_forward_server_ip(
2516   cmd_parms   *cmd,
2517   void        *mconfig,
2518   const char  *arg)
2519 {
2520   mod_chxj_config *dconf;
2521
2522   if (strlen(arg) > 255)
2523     return "mod_chxj: ChxjForwardServerIp is too long.";
2524
2525   dconf = (mod_chxj_config *)mconfig;
2526
2527   dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2528
2529   return NULL;
2530 }
2531
2532 static const char *
2533 cmd_set_new_line_type(
2534   cmd_parms   *UNUSED(cmd), 
2535   void        *mconfig, 
2536   const char  *arg)
2537 {
2538   mod_chxj_config  *dconf;
2539   if (strlen(arg) > 255)
2540     return "mod_chxj: ChxjNewLineType is too long.";
2541
2542   dconf = (mod_chxj_config *)mconfig;
2543
2544   if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2545     dconf->new_line_type = NLTYPE_CRLF;
2546   }
2547   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2548     dconf->new_line_type = NLTYPE_LF;
2549   }
2550   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2551     dconf->new_line_type = NLTYPE_CR;
2552   }
2553   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2554     dconf->new_line_type = NLTYPE_NONE;
2555   }
2556   else {
2557     return "mod_chxj: invalid value (ChxjNewLineType)";
2558   }
2559   return NULL;
2560 }
2561
2562
2563 static const command_rec cmds[] = {
2564   AP_INIT_TAKE1(
2565     "ChxjLoadDeviceData",
2566     cmd_load_device_data,
2567     NULL,
2568     OR_ALL,
2569     "Load Device Data"),
2570   AP_INIT_TAKE1(
2571     "ChxjLoadEmojiData",
2572     cmd_load_emoji_data,
2573     NULL,
2574     OR_ALL,
2575     "Load Emoji Data"),
2576   AP_INIT_TAKE1(
2577     "ChxjImageEngine",
2578     cmd_set_image_engine,
2579     NULL,
2580     OR_ALL,
2581     "Convert Target URI"),
2582   AP_INIT_TAKE1(
2583     "ChxjImageCacheDir",
2584     cmd_set_image_cache_dir,
2585     NULL,
2586     OR_ALL,
2587     "Image Cache Directory"),
2588   AP_INIT_TAKE1(
2589     "ChxjImageCacheLimit",
2590     cmd_set_image_cache_limit,
2591     NULL,
2592     OR_ALL,
2593     "Image Cache Limit"),
2594   AP_INIT_TAKE1(
2595     "ChxjImageCopyright",
2596     cmd_set_image_copyright,
2597     NULL,
2598     OR_ALL,
2599     "Copyright Flag"),
2600   AP_INIT_RAW_ARGS(
2601     "ChxjConvertRule",
2602     cmd_convert_rule,
2603     NULL, 
2604     OR_FILEINFO,
2605     "an URL-applied regexp-pattern and a substitution URL"),
2606   AP_INIT_TAKE1(
2607     "ChxjCookieDir",
2608     cmd_set_cookie_dir,
2609     NULL,
2610     OR_ALL,
2611     "save cookie.db directory."),
2612   AP_INIT_TAKE1(
2613     "ChxjCookieTimeout",
2614     cmd_set_cookie_timeout,
2615     NULL,
2616     OR_ALL,
2617     "The compulsion time-out time of the cookie is specified. "),
2618   AP_INIT_TAKE1(
2619     "ChxjCookieStoreType",
2620     cmd_set_cookie_store_type,
2621     NULL,
2622     OR_ALL,
2623     "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2624   AP_INIT_TAKE1(
2625     "ChxjCookieLazyMode",
2626     cmd_set_cookie_lazy_mode,
2627     NULL,
2628     OR_ALL,
2629     "OneTimeID is negligently done. (TRUE/FALSE)"),
2630 #if defined(USE_MYSQL_COOKIE)
2631   AP_INIT_TAKE1(
2632     "ChxjCookieMysqlHost",
2633     cmd_set_cookie_mysql_host,
2634     NULL,
2635     OR_ALL,
2636     "The MySQL database host used by saving Cookie"),
2637   AP_INIT_TAKE1(
2638     "ChxjCookieMysqlPort",
2639     cmd_set_cookie_mysql_port,
2640     NULL,
2641     OR_ALL,
2642     "The MySQL database port used by saving Cookie"),
2643   AP_INIT_TAKE1(
2644     "ChxjCookieMysqlDatabase",
2645     cmd_set_cookie_mysql_database,
2646     NULL,
2647     OR_ALL,
2648     "The MySQL database name used by saving Cookie"),
2649   AP_INIT_TAKE1(
2650     "ChxjCookieMysqlUsername",
2651     cmd_set_cookie_mysql_username,
2652     NULL,
2653     OR_ALL,
2654     "The MySQL username used by saving Cookie"),
2655   AP_INIT_TAKE1(
2656     "ChxjCookieMysqlPassword",
2657     cmd_set_cookie_mysql_password,
2658     NULL,
2659     OR_ALL,
2660     "The MySQL password used by saving Cookie"),
2661   AP_INIT_TAKE1(
2662     "ChxjCookieMysqlTableName",
2663     cmd_set_cookie_mysql_table_name,
2664     NULL,
2665     OR_ALL,
2666     "The MySQL table name used by saving Cookie"),
2667   AP_INIT_TAKE1(
2668     "ChxjCookieMysqlSocketPath",
2669     cmd_set_cookie_mysql_socket_path,
2670     NULL,
2671     OR_ALL,
2672     "The MySQL socket path used by saving Cookie"),
2673   AP_INIT_TAKE1(
2674     "ChxjCookieMysqlCharset",
2675     cmd_set_cookie_mysql_charset,
2676     NULL,
2677     OR_ALL,
2678     "The MySQL charset used by saving Cookie"),
2679 #endif
2680 #if defined(USE_MEMCACHE_COOKIE)
2681   AP_INIT_TAKE1(
2682     "ChxjCookieMemcacheHost",
2683     cmd_set_cookie_memcache_host,
2684     NULL,
2685     OR_ALL,
2686     "The Memcached host used by saving Cookie"),
2687   AP_INIT_TAKE1(
2688     "ChxjCookieMemcachePort",
2689     cmd_set_cookie_memcache_port,
2690     NULL,
2691     OR_ALL,
2692     "The Memcached port used by saving Cookie"),
2693 #endif
2694   AP_INIT_TAKE1(
2695     "ChxjNewLineType",
2696     cmd_set_new_line_type,
2697     NULL,
2698     OR_ALL,
2699     "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
2700   AP_INIT_TAKE1(
2701     "ChxjForwardUrlBase",
2702     cmd_set_forward_url_base,
2703     NULL,
2704     OR_ALL,
2705     "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2706   AP_INIT_TAKE1(
2707     "ChxjForwardServerIp",
2708     cmd_set_forward_server_ip,
2709     NULL,
2710     OR_ALL,
2711     "The forward server ip(default: this server ip)"),
2712   {NULL}
2713 };
2714
2715
2716 /*----------------------------------------------------------------------------*/
2717 /* Dispatch list for API hooks                                                */
2718 /*----------------------------------------------------------------------------*/
2719 module AP_MODULE_DECLARE_DATA chxj_module = {
2720   STANDARD20_MODULE_STUFF, 
2721   chxj_create_per_dir_config,          /* create per-dir    config structures */
2722   chxj_merge_per_dir_config,           /* merge  per-dir    config structures */
2723   chxj_config_server_create,           /* create per-server config structures */
2724   NULL,                                /* merge  per-server config structures */
2725   cmds,                                /* table of config file commands       */
2726   chxj_register_hooks                  /* register hooks                      */
2727 };
2728 /*
2729  * vim:ts=2 et
2730  */