OSDN Git Service

* Bug fix.
[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)(apr_size_t)r);
164   if (r->main) {
165     DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)(apr_size_t)r);
166     DBG(r, "REQ[%X] end chxj_headers_fixup()",  (unsigned int)(apr_size_t)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)(apr_size_t)r);
179     DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)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)(apr_size_t)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)(apr_size_t)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)(apr_size_t)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)(apr_size_t)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)(apr_size_t)r, APLOG_MARK, rv, apr_strerror(rv, buf, 256));
247           DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)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)(apr_size_t)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)(apr_size_t)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)(apr_size_t)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)(apr_size_t)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)(apr_size_t)r, user_agent);
338   DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)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)(apr_size_t)r, r->content_type);
344     DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)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)(apr_size_t)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)(apr_size_t)r);
444     DBG(r, "REQ[%X] end   chxj_convert_input_header()", (unsigned int)(apr_size_t)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, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
469       no_update_flag++;
470     }
471   }
472
473   buff = apr_pstrdup(r->pool, r->args);
474   DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, 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, "REQ[%X] found cookie parameter[%s]",    (unsigned int)(apr_size_t)r, value);
556         DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
557         cookie_lock_t *lock = chxj_cookie_lock(r);
558         cookie = chxj_load_cookie(r, value);
559         DBG(r, "REQ[%X] call end   chxj_load_cookie()", (unsigned int)(apr_size_t)r);
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)(apr_size_t)r, r->args);
585   DBG(r, "REQ[%X] end   chxj_convert_input_header()", (unsigned int)(apr_size_t)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   apr_size_t ii;
615   apr_size_t ilen = 0;
616   apr_pool_t *pool;
617
618   DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
619
620   if (! *src) {
621     DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
622     return apr_pstrdup(r->pool, "");
623   }
624
625   apr_pool_create(&pool, r->pool);
626
627   s        = apr_pstrdup(pool, *src);
628   ilen     = strlen(s);
629   buff_pre = apr_pstrdup(pool, *src);
630
631   result   = qs_alloc_zero_byte_string(pool);
632
633   DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
634   DBG(r, "REQ[%X] | BEFORE input convert source                                       |", (unsigned int)(apr_size_t)r);
635   DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
636   for (ii=0; ii<ilen-64; ii+=64) {
637     DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
638   }
639   DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
640   DBG(r, "REQ[%X] +--------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
641
642   for (;;) {
643     char *pair_sv;
644
645     pair = apr_strtok(buff_pre, "&", &pstate);
646     if (pair == NULL)
647       break;
648
649     buff_pre = NULL;
650
651     pair_sv = apr_pstrdup(pool, pair);
652
653     name  = apr_strtok(pair, "=", &vstate);
654     value = apr_strtok(NULL, "=", &vstate);
655     if (! name) continue;
656     if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
657       DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
658       no_update_flag++;
659     }
660   }
661
662   /* _chxj_dmy */
663   /* _chxj_c_ */
664   /* _chxj_r_ */
665   /* _chxj_s_ */
666   for (;;) {
667     pair = apr_strtok(s, "&", &pstate);
668     if (pair == NULL)
669       break;
670     s = NULL;
671
672     name  = apr_strtok(pair, "=", &vstate);
673     value = apr_strtok(NULL, "=", &vstate);
674     if (! name) continue;
675     if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
676       if (strlen(result) != 0) 
677         result = apr_pstrcat(pool, result, "&", NULL);
678
679       if (strcasecmp(entryp->encoding, "NONE") != 0) {
680         apr_size_t dlen;
681         char *dvalue;
682         char *dname;
683
684         if (value && *value != 0) {
685           value = chxj_url_decode(pool, value);
686           dlen   = strlen(value);
687           dvalue = chxj_rencoding(r, value, &dlen);
688           dvalue = chxj_url_encode(pool, dvalue);
689         }
690         else {
691           dvalue = "";
692         }
693
694         if (name && *name != 0) {
695           name = chxj_url_decode(pool, name);
696           dlen = strlen(name);
697           dname = chxj_rencoding(r, name, &dlen);
698           dname = chxj_url_encode(pool, dname);
699         }
700         else {
701           dname = "";
702         }
703
704         result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
705       }
706       else {
707         result = apr_pstrcat(pool, result, name, "=", value, NULL);
708       }
709     }
710     else
711     if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
712       || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
713       || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
714       if (value == NULL)
715         continue;
716
717       if (strlen(value) == 0)
718         continue;
719
720       if (strlen(result) != 0)
721         result = apr_pstrcat(pool, result, "&", NULL);
722
723       if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
724         apr_size_t dlen;
725         char       *dvalue;
726
727         dlen   = strlen(value);
728         value = chxj_url_decode(pool, value);
729         dvalue = chxj_rencoding(r, value, &dlen);
730         dvalue = chxj_url_encode(pool,dvalue);
731         result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
732
733       }
734       else {
735         result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
736       }
737     }
738     else
739     if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
740       if (! cookie) {
741         apr_table_unset(r->headers_in, "Cookie");
742         DBG(r, "REQ[%X] found cookie parameter[%s]",    (unsigned int)(apr_size_t)r, value);
743         DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
744         cookie_lock_t *lock = chxj_cookie_lock(r);
745         cookie = chxj_load_cookie(r, value);
746         DBG(r, "REQ[%X] call end   chxj_load_cookie()", (unsigned int)(apr_size_t)r);
747         if (! no_update_flag && cookie) {
748           chxj_update_cookie(r, cookie);
749         }
750         chxj_cookie_unlock(r, lock);
751       }
752       if (cookie && cookie->cookie_id) {
753         if (strlen(result) != 0)
754           result = apr_pstrcat(pool, result, "&", NULL);
755         result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
756       }
757     }
758     else
759     if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
760       if (strlen(result) != 0)
761         result = apr_pstrcat(pool, result, "&", NULL);
762       result = apr_pstrcat(pool, result, name, "=", value, NULL);
763     }
764     else
765     if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX,     sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
766       apr_size_t dlen;
767       char*      dvalue;
768       dlen   = strlen(value);
769       if (dlen && value) {
770         value = chxj_url_decode(pool, value);
771         dvalue = chxj_rencoding(r, value, &dlen);
772         dvalue = chxj_url_encode(pool,dvalue);
773         if (r->args && strlen(r->args) > 0) {
774           r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
775         }
776         else {
777           r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
778         }
779       }
780     }
781     else
782     if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
783       apr_size_t dlen;
784       char*      dvalue;
785       dlen   = strlen(value);
786       if (dlen && value) {
787         value = chxj_url_decode(pool, value);
788         dvalue = chxj_rencoding(r, value, &dlen);
789         dvalue = chxj_url_encode(pool,dvalue);
790         if (r->args && strlen(r->args) > 0) {
791           r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
792         }
793         else {
794           r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
795         }
796       }
797     }
798     DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
799   }
800   *len = strlen(result);
801   apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
802   if (! no_update_flag) {
803     result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
804   }
805
806   DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
807   DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
808
809   return result;
810 }
811
812
813 /**
814  * The received data is returned to the filter.
815  *
816  * @param f    [i/o] It is a filter. 
817  * @param data [i]   It is data returned to the filter. 
818  * @param len  [i]   It is length of the data returned to the filter. 
819  */
820 static apr_status_t 
821 pass_data_to_filter(ap_filter_t *f, const char *data, 
822                                         apr_size_t len)
823 {
824   request_rec         *r = f->r;
825   conn_rec            *c = r->connection;
826   apr_status_t        rv;
827   apr_bucket_brigade  *bb;
828   apr_bucket          *b;
829
830   DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
831
832   bb = apr_brigade_create(r->pool, c->bucket_alloc);
833   b  = apr_bucket_transient_create(data, len, c->bucket_alloc);
834
835   APR_BRIGADE_INSERT_TAIL(bb, b);
836   b = apr_bucket_eos_create(f->c->bucket_alloc);
837   APR_BRIGADE_INSERT_TAIL(bb, b);
838
839   rv = ap_pass_brigade(f->next, bb);
840   if (rv != APR_SUCCESS) {
841     DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
842     return rv;
843   }
844
845   DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
846
847   return rv;
848 }
849
850
851 /**
852  * It is the main loop of the output filter. 
853  *
854  * @param f   [i/o] It is a filter.
855  * @param bb  [i]   
856  */
857 static apr_status_t 
858 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
859 {
860   request_rec         *r;
861   apr_status_t        rv;
862   apr_bucket          *b;
863   const char          *data;
864   char                *user_agent = NULL;
865   apr_size_t          len;
866   mod_chxj_ctx        *ctx = (mod_chxj_ctx *)f->ctx;
867   cookie_t            *cookie = NULL;
868   mod_chxj_config     *dconf;
869   chxjconvrule_entry  *entryp = NULL;
870   device_table        *spec = NULL;
871   apr_pool_t          *pool;
872
873   r  = f->r;
874   DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
875   rv = APR_SUCCESS;
876
877   apr_pool_create(&pool, r->pool);
878
879   entryp = ctx->entryp;
880   spec   = ctx->spec;
881   dconf  = chxj_get_module_config(r->per_dir_config, &chxj_module);
882
883   if (r->content_type) {
884     if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
885     &&  ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
886     &&  ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
887     &&  ! (dconf->image == CHXJ_IMG_ON
888           && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
889           && STRNCASEEQ('i','I',"image/",  r->content_type, sizeof("image/") -1)
890           && ( STRCASEEQ('j','J',"jpeg",            &r->content_type[6])         /* JPEG */
891             || STRCASEEQ('j','J',"jp2",             &r->content_type[6])         /* JPEG2000 */
892             || STRCASEEQ('j','J',"jpeg2000",        &r->content_type[6])         /* JPEG2000 */
893             || STRCASEEQ('j','J',"jpeg2000-image",  &r->content_type[6])         /* JPEG2000 */
894             || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6])         /* JPEG2000 */
895             || STRCASEEQ('p','P',"png",             &r->content_type[6])         /* PNG */
896             || STRCASEEQ('x','X',"x-png",           &r->content_type[6])         /* PNG */
897             || STRCASEEQ('g','G',"gif",             &r->content_type[6])))) {     /* GIF */
898       
899       DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
900       if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
901         cookie_lock_t *lock = NULL;
902         DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
903         switch(spec->html_spec_type) {
904         case CHXJ_SPEC_Chtml_1_0:
905         case CHXJ_SPEC_Chtml_2_0:
906         case CHXJ_SPEC_Chtml_3_0:
907         case CHXJ_SPEC_Chtml_4_0:
908         case CHXJ_SPEC_Chtml_5_0:
909         case CHXJ_SPEC_Chtml_6_0:
910         case CHXJ_SPEC_Chtml_7_0:
911         case CHXJ_SPEC_XHtml_Mobile_1_0:
912         case CHXJ_SPEC_Jhtml:
913           lock = chxj_cookie_lock(r);
914           cookie = chxj_save_cookie(r);
915           s_add_cookie_id_if_has_location_header(r, cookie);
916           chxj_cookie_unlock(r, lock);
917           break;
918         default:
919           break;
920         }
921       }
922       if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
923         if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
924           r->status = HTTP_MOVED_TEMPORARILY;
925         }
926       }
927       ap_pass_brigade(f->next, bb);
928       DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
929       return APR_SUCCESS;
930     }
931   }
932   else {
933     DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
934     ap_pass_brigade(f->next, bb);
935     DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
936     return APR_SUCCESS;
937   }
938
939
940   for (b = APR_BRIGADE_FIRST(bb);
941        b != APR_BRIGADE_SENTINEL(bb); 
942        b = APR_BUCKET_NEXT(b)) {
943
944     if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
945       DBG(r, "REQ[%X] read data[%.*s]",(unsigned int)(apr_size_t)r, (int)len, data);
946
947       /*--------------------------------------------------------------------*/
948       /* append data                                                        */
949       /*--------------------------------------------------------------------*/
950       char *tmp;
951       DBG(r, "append data start");
952       ctx = (mod_chxj_ctx *)f->ctx;
953
954       if (len > 0) {
955         tmp = apr_palloc(r->pool, ctx->len);
956         memcpy(tmp, ctx->buffer, ctx->len);
957
958         ctx->buffer = apr_palloc(pool, ctx->len + len);
959
960         memcpy(ctx->buffer, tmp, ctx->len);
961         memcpy(&ctx->buffer[ctx->len], data, len);
962
963         ctx->len += len;
964       }
965       DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
966     }
967
968     if (APR_BUCKET_IS_EOS(b)) {
969
970       DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
971       /*----------------------------------------------------------------------*/
972       /* End Of File                                                          */
973       /*----------------------------------------------------------------------*/
974       if (ctx) {
975         cookie_lock_t *lock = NULL;
976         ctx = (mod_chxj_ctx *)f->ctx;
977
978         DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
979         lock = chxj_cookie_lock(r);
980
981         if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN 
982             && r->content_type 
983             && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
984             ||  STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
985           DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
986
987           if (ctx->len) {
988             char *tmp;
989
990             tmp = apr_palloc(pool, ctx->len + 1);
991
992             memset(tmp, 0, ctx->len + 1);
993             memcpy(tmp, ctx->buffer, ctx->len);
994
995             ctx->buffer = chxj_convert(r, 
996                                        (const char **)&tmp, 
997                                        (apr_size_t *)&ctx->len,
998                                        spec,
999                                        user_agent, &cookie);
1000
1001           }
1002           else {
1003             ctx->buffer = apr_psprintf(r->pool, "\n");
1004             ctx->len += 1;
1005             ctx->buffer = chxj_convert(r, 
1006                                        (const char **)&ctx->buffer, 
1007                                        (apr_size_t *)&ctx->len,
1008                                        spec,
1009                                        user_agent, &cookie);
1010
1011           }
1012         }
1013         if (r->content_type
1014             && *(char *)r->content_type == 't'
1015             && strncmp(r->content_type, "text/xml",   8) == 0) {
1016           DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1017
1018           Doc       doc;
1019           Node      *root;
1020           Node      *child;
1021           qr_code_t qrcode;
1022           int       sts;
1023       
1024           memset(&doc,    0, sizeof(Doc));
1025           memset(&qrcode, 0, sizeof(qr_code_t));
1026           doc.r = r;
1027           doc.parse_mode  = PARSE_MODE_CHTML;
1028           qrcode.doc      = &doc;
1029           qrcode.r        = r;
1030       
1031           qs_init_malloc(&doc);
1032       
1033           root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1034
1035           sts = 0;
1036           for (child = qs_get_child_node(&doc,root);
1037                child ;
1038                child = qs_get_next_node(&doc,child)) {
1039             char *name = qs_get_node_name(&doc,child);
1040             if (strcasecmp("qrcode",name) == 0) {
1041               sts++;
1042               break;
1043             }
1044           }
1045           qs_all_free(&doc,QX_LOGMARK);
1046           if (sts) {
1047             r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1048             chxj_qrcode_node_to_qrcode(&qrcode, root);
1049             sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1050             if (sts != OK) {
1051               ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1052               chxj_cookie_unlock(r, lock);
1053               DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1054               return sts;
1055             }
1056             r->content_type = apr_psprintf(r->pool, "image/jpeg");
1057           }
1058         }
1059
1060         if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN 
1061             && r->content_type 
1062             && ( *r->content_type == 'i' || *r->content_type == 'I')
1063             && dconf->image == CHXJ_IMG_ON
1064             && strncasecmp("image/", r->content_type, 6) == 0
1065             && ( STRCASEEQ('j','J',"jpeg",            &r->content_type[6])         /* JPEG */
1066               || STRCASEEQ('j','J',"jp2",             &r->content_type[6])         /* JPEG2000 */
1067               || STRCASEEQ('j','J',"jpeg2000",        &r->content_type[6])         /* JPEG2000 */
1068               || STRCASEEQ('j','J',"jpeg2000-image",  &r->content_type[6])         /* JPEG2000 */
1069               || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6])         /* JPEG2000 */
1070               || STRCASEEQ('p','P',"png",             &r->content_type[6])         /* PNG */
1071               || STRCASEEQ('x','X',"x-png",           &r->content_type[6])         /* PNG */
1072               || STRCASEEQ('g','G',"gif",             &r->content_type[6]))) {     /* GIF */
1073           if (ctx->len) {
1074             char *tmp;
1075
1076             tmp = apr_palloc(pool, ctx->len + 1);
1077
1078             memset(tmp, 0, ctx->len + 1);
1079             memcpy(tmp, ctx->buffer, ctx->len);
1080             ctx->buffer = 
1081               chxj_convert_image(r, 
1082                                   (const char **)&tmp,
1083                                   (apr_size_t *)&ctx->len);
1084             if (ctx->buffer == NULL) {
1085               ctx->buffer = tmp;
1086             }
1087           }
1088         }
1089
1090         apr_table_unset(r->headers_out, "Content-Length");
1091         apr_table_unset(r->err_headers_out, "Content-Length");
1092         ap_set_content_length(r, (apr_off_t)ctx->len);
1093
1094         
1095         if (ctx->len > 0) {
1096           DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1097           s_add_cookie_id_if_has_location_header(r, cookie);
1098           if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1099             if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1100               r->status = HTTP_MOVED_TEMPORARILY;
1101             }
1102           }
1103           chxj_cookie_unlock(r,lock);
1104           rv = pass_data_to_filter(f, 
1105                                    (const char *)ctx->buffer, 
1106                                    (apr_size_t)ctx->len);
1107         }
1108         else {
1109           chxj_cookie_unlock(r, lock);
1110
1111         }
1112         DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1113         return rv;
1114       }
1115       else {
1116         DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1117
1118         /*
1119          * save cookie.
1120          */
1121         if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1122           cookie_lock_t *lock = NULL;
1123           DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1124           switch(spec->html_spec_type) {
1125           case CHXJ_SPEC_Chtml_1_0:
1126           case CHXJ_SPEC_Chtml_2_0:
1127           case CHXJ_SPEC_Chtml_3_0:
1128           case CHXJ_SPEC_Chtml_4_0:
1129           case CHXJ_SPEC_Chtml_5_0:
1130           case CHXJ_SPEC_Chtml_6_0:
1131           case CHXJ_SPEC_Chtml_7_0:
1132           case CHXJ_SPEC_XHtml_Mobile_1_0:
1133           case CHXJ_SPEC_Jhtml:
1134             lock = chxj_cookie_lock(r);
1135             cookie = chxj_save_cookie(r);
1136             /*
1137              * Location Header Check to add cookie parameter.
1138              */
1139             s_add_cookie_id_if_has_location_header(r, cookie);
1140             chxj_cookie_unlock(r, lock);
1141             apr_table_unset(r->headers_out, "Set-Cookie");
1142             apr_table_unset(r->err_headers_out, "Set-Cookie");
1143             break;
1144
1145           default:
1146             break;
1147           }
1148         }
1149         if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1150           if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1151             r->status = HTTP_MOVED_TEMPORARILY;
1152           }
1153         }
1154         apr_table_setn(r->headers_out, "Content-Length", "0");
1155         DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1156         rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1157         DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1158         return rv;
1159       }
1160     }
1161   }
1162   apr_brigade_destroy(bb);
1163
1164   DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1165
1166   return APR_SUCCESS;
1167 }
1168
1169 /**
1170  * Add Cookie_id if it has location header.
1171  */
1172 static void
1173 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1174 {
1175   char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1176   if (! location_header) {
1177     location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1178   }
1179   if (cookie && location_header) {
1180     DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1181     location_header = chxj_add_cookie_parameter(r,
1182                                                 location_header,
1183                                                 cookie);
1184     apr_table_unset(r->headers_out, "Location");
1185     apr_table_setn(r->headers_out, "Location", location_header);
1186     DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1187     if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1188       r->status = HTTP_MOVED_TEMPORARILY;
1189     }
1190   }
1191 }
1192
1193 /**
1194  * It is the main loop of the input filter handler. 
1195  *
1196  */
1197 static apr_status_t
1198 chxj_input_handler(request_rec *r)
1199 {
1200   mod_chxj_config     *dconf;
1201   chxjconvrule_entry  *entryp = NULL;
1202   device_table        *spec   = NULL;
1203   char                *post_data = NULL;
1204   apr_size_t          post_data_len = 0;
1205   char                *response;
1206   char                *user_agent;
1207   apr_pool_t          *pool;
1208   apr_size_t          ii;
1209   
1210   DBG(r, "start of chxj_input_handler()");
1211
1212   if (strcasecmp(r->handler, "chxj-input-handler")) {
1213     DBG(r, "end chxj_input_handler()");
1214     return DECLINED;
1215   }
1216   apr_pool_create(&pool, r->pool);
1217
1218   dconf      = chxj_get_module_config(r->per_dir_config, &chxj_module);
1219   user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
1220   spec       = chxj_specified_device(r, user_agent);
1221   entryp     = chxj_apply_convrule(r, dconf->convrules);
1222
1223   post_data = apr_pstrdup(pool, "");
1224   if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1225     if (ap_should_client_block(r)) {
1226       while (post_data_len < CHXJ_POST_MAX) {
1227 #define BUFSZ (256)
1228         char buffer[BUFSZ];
1229         int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1230         if (read_bytes<=0) {
1231           break;
1232         }
1233         buffer[read_bytes] = '\0';
1234         post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1235         post_data_len += read_bytes;
1236 #undef BUFSZ
1237       }
1238     }
1239   }
1240
1241   /* 
1242    * now convert.
1243    */
1244   if (post_data_len > 0) {
1245     post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp);
1246     DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1247   }
1248
1249   char *url_path;
1250   if (dconf->forward_url_base) {
1251     url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1252   }
1253   else {
1254 #if 0
1255     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);
1256 #else
1257     url_path = apr_psprintf(pool, "%s://%s:%d%s", chxj_apache_run_http_scheme(r), "127.0.0.1", ap_get_server_port(r), r->uri);
1258 #endif
1259   }
1260   if (r->args) {
1261     url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1262   }
1263   DBG(r, "==> new url_path:[%s]", url_path);
1264
1265   apr_size_t res_len;
1266   apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1267   apr_table_unset(r->headers_in, "Content-Length");
1268   apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1269   response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len);
1270   DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1271   DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1272   for (ii=0; ii<res_len/64; ii++) {
1273     DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1274   }
1275   DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1276
1277   char *chunked;
1278   if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1279     if (strcasecmp(chunked, "chunked") == 0) {
1280       r->chunked = 1;  
1281       apr_table_unset(r->headers_out, "Transfer-Encoding");
1282     }
1283   }
1284   {
1285     apr_pool_t *wpool;
1286     apr_pool_create(&wpool, r->pool);
1287     apr_bucket_brigade *bb;
1288     apr_bucket *e;
1289     apr_status_t rv;
1290     conn_rec *c = r->connection;
1291     
1292     bb = apr_brigade_create(wpool, c->bucket_alloc);
1293     e  = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1294     APR_BRIGADE_INSERT_TAIL(bb, e);
1295     e = apr_bucket_eos_create(c->bucket_alloc);
1296     APR_BRIGADE_INSERT_TAIL(bb, e);
1297     if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1298       ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1299       return rv;
1300     }
1301     apr_brigade_cleanup(bb);
1302   }
1303
1304   DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1305   return APR_SUCCESS;
1306 }
1307
1308 static mod_chxj_global_config *
1309 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1310 {
1311   mod_chxj_global_config *conf;
1312
1313   SDBG(s, "start chxj_global_config_create()");
1314
1315   /*--------------------------------------------------------------------------*/
1316   /* allocate an own subpool which survives server restarts                   */
1317   /*--------------------------------------------------------------------------*/
1318   conf = (mod_chxj_global_config *)apr_palloc(pool, 
1319                   sizeof(mod_chxj_global_config));
1320 #if 0
1321   conf->cookie_db_lock = NULL;
1322 #endif
1323   SDBG(s, "end   chxj_global_config_create()");
1324
1325   return conf;
1326 }
1327
1328
1329 /**
1330  * initialize chxj module
1331  */
1332 static int 
1333 chxj_init_module(apr_pool_t *p, 
1334                  apr_pool_t *UNUSED(plog), 
1335                  apr_pool_t *UNUSED(ptemp), 
1336                  server_rec *s)
1337 {
1338   void *user_data;
1339   apr_status_t rv;
1340
1341   SDBG(s, "start chxj_init_module()");
1342
1343   apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1344   SDBG(s, " ");
1345   if (user_data == NULL) {
1346     SDBG(s, " ");
1347     /*
1348      * dummy user_data set.
1349      */
1350     apr_pool_userdata_set(
1351       (const void *)(1), 
1352       CHXJ_MOD_CONFIG_KEY, 
1353       apr_pool_cleanup_null, 
1354       s->process->pool);
1355     SDBG(s, "end  chxj_init_module()");
1356     return OK;
1357   }
1358
1359   ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1360
1361   if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL,  APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1362     char errstr[255];
1363     SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1364     return HTTP_INTERNAL_SERVER_ERROR;
1365   }
1366
1367   SDBG(s, "end  chxj_init_module()");
1368
1369   return OK;
1370 }
1371
1372
1373 static void 
1374 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1375 {
1376   apr_status_t rv;
1377   SDBG(s, "start chxj_child_init()");
1378   if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1379     char errstr[255];
1380     SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1381   }
1382   SDBG(s, "end   chxj_child_init()");
1383 }
1384
1385
1386 /**
1387  * A set structure of each server is generated. 
1388  * 
1389  * @param p
1390  * @param s
1391  */
1392 static void *
1393 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1394 {
1395   mod_chxj_global_config *gc;
1396
1397   gc = chxj_global_config_create(p,s);
1398
1399   return gc;
1400 }
1401
1402
1403 static int
1404 chxj_translate_name(request_rec *r)
1405 {
1406   DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1407   DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1408   DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1409   DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1410   DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1411   return chxj_trans_name(r);
1412 }
1413
1414
1415 static void 
1416 chxj_insert_filter(request_rec *r)
1417 {
1418   char                *user_agent;
1419   device_table        *spec;
1420   mod_chxj_config     *dconf;
1421   chxjconvrule_entry  *entryp;
1422   mod_chxj_ctx        *ctx;
1423   apr_status_t        rv;
1424   char                *contentType;
1425
1426   DBG(r, "start chxj_insert_filter()");
1427
1428   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1429
1430   user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1431
1432   contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1433   if (contentType
1434       && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1435     DBG(r, "detect multipart/form-data ==> no target");
1436     return;
1437   }
1438
1439   spec = chxj_specified_device(r, user_agent);
1440   entryp = chxj_apply_convrule(r, dconf->convrules);
1441   if (!entryp) {
1442     DBG(r, "end chxj_insert_filter()");
1443     return;
1444   }
1445   ctx = apr_palloc(r->pool, sizeof(*ctx));
1446   memset(ctx, 0, sizeof(*ctx));
1447   if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1448     ERR(r, "failed: new pool create. rv:[%d]", rv);
1449     return;
1450   }
1451   ctx->entryp = entryp;
1452   ctx->spec   = spec;
1453   ctx->buffer = apr_palloc(ctx->pool, 1);
1454   ctx->buffer[0] = 0;
1455
1456   if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1457     DBG(r,"EngineOff");
1458     return;
1459   }
1460
1461   switch(spec->html_spec_type) {
1462   case CHXJ_SPEC_Chtml_1_0:
1463   case CHXJ_SPEC_Chtml_2_0:
1464   case CHXJ_SPEC_Chtml_3_0:
1465   case CHXJ_SPEC_Chtml_4_0:
1466   case CHXJ_SPEC_Chtml_5_0:
1467   case CHXJ_SPEC_Chtml_6_0:
1468   case CHXJ_SPEC_Chtml_7_0:
1469   case CHXJ_SPEC_XHtml_Mobile_1_0:
1470   case CHXJ_SPEC_Hdml:
1471   case CHXJ_SPEC_Jhtml:
1472   case CHXJ_SPEC_Jxhtml:
1473     break;
1474
1475   default:
1476     return;
1477   }
1478
1479
1480   if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1481     ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1482     DBG(r, "added Output Filter");
1483   }
1484
1485   DBG(r, "end   chxj_insert_filter()");
1486 }
1487
1488
1489 /**
1490  * The hook is registered.
1491  *
1492  * @param p
1493  */
1494 static void 
1495 chxj_register_hooks(apr_pool_t *UNUSED(p))
1496 {
1497   ap_hook_post_config(chxj_init_module,
1498                       NULL,
1499                       NULL,
1500                       APR_HOOK_REALLY_FIRST);
1501   ap_hook_child_init(chxj_child_init, 
1502                      NULL, 
1503                      NULL, 
1504                      APR_HOOK_REALLY_FIRST);
1505   ap_register_output_filter (
1506                       "chxj_output_filter", 
1507                       chxj_output_filter, 
1508                       NULL, 
1509                       AP_FTYPE_RESOURCE);
1510   ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1511   ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1512   ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1513   ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1514   ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1515   ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1516 }
1517
1518
1519 /**
1520  * A set structure according to the directory is generated. 
1521  *
1522  * @param p
1523  * @param arg
1524  */
1525 static void * 
1526 chxj_create_per_dir_config(apr_pool_t *p, char *arg) 
1527 {
1528   mod_chxj_config *conf;
1529
1530   conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1531   conf->device_data_file = NULL;
1532   conf->devices          = NULL;
1533   conf->emoji_data_file  = NULL;
1534   conf->emoji            = NULL;
1535   conf->emoji_tail       = NULL;
1536   conf->image            = CHXJ_IMG_NONE;
1537   conf->image_cache_dir  = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1538   conf->image_cache_limit = 0;
1539   conf->server_side_encoding = NULL;
1540   conf->cookie_db_dir    = NULL;
1541   conf->cookie_timeout   = 0;
1542   conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1543   conf->cookie_lazy_mode  = 0;
1544 #if defined(USE_MYSQL_COOKIE)
1545   memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1546   conf->mysql.port       = MYSQL_PORT;
1547   conf->mysql.host       = NULL;
1548 #endif
1549 #if defined(USE_MEMCACHE_COOKIE)
1550   memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1551   conf->memcache.host    = NULL;
1552   conf->memcache.port    = 0;
1553 #endif
1554   conf->forward_url_base = NULL;
1555   conf->forward_server_ip = NULL;
1556
1557   if (arg == NULL) {
1558     conf->dir                  = NULL;
1559   }
1560   else {
1561     conf->dir                  = apr_pcalloc(p, strlen(arg)+1);
1562     memset(conf->dir, 0, strlen(arg)+1);
1563     strcpy(conf->dir, arg);
1564   }
1565   conf->convrules   = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1566
1567   /* Default is copyleft */
1568   conf->image_copyright = NULL; 
1569
1570   return conf;
1571 }
1572
1573
1574 /*
1575  *  Merge per-directory CHXJ configurations
1576  */
1577 static void *
1578 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1579 {
1580   mod_chxj_config *base;
1581   mod_chxj_config *add;
1582   mod_chxj_config *mrg;
1583
1584   base = (mod_chxj_config *)basev;
1585   add  = (mod_chxj_config *)addv;
1586   mrg  = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1587
1588   mrg->device_data_file = NULL;
1589   mrg->devices          = NULL;
1590   mrg->emoji_data_file  = NULL;
1591   mrg->image            = CHXJ_IMG_NONE;
1592   mrg->image_cache_dir  = NULL;
1593   mrg->image_copyright  = NULL;
1594   mrg->image_cache_limit  = 0;
1595   mrg->emoji            = NULL;
1596   mrg->emoji_tail       = NULL;
1597   mrg->new_line_type    = NLTYPE_NIL;
1598   mrg->forward_url_base = NULL;
1599   mrg->forward_server_ip = NULL;
1600
1601   mrg->dir = apr_pstrdup(p, add->dir);
1602
1603   if (! add->device_data_file) {
1604     mrg->devices = base->devices;
1605     mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1606   }
1607   else {
1608     mrg->devices = add->devices;
1609     mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1610   }
1611
1612   if (! add->emoji_data_file) {
1613     mrg->emoji = base->emoji;
1614     mrg->emoji_tail = base->emoji_tail;
1615     mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1616   }
1617   else {
1618     mrg->emoji = add->emoji;
1619     mrg->emoji_tail = add->emoji_tail;
1620     mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1621   }
1622
1623   if (add->image == CHXJ_IMG_NONE) {
1624     mrg->image = base->image;
1625   }
1626   else {
1627     mrg->image = add->image;
1628   }
1629
1630   if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1631     mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1632   }
1633   else {
1634     mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1635   }
1636
1637   if (add->image_cache_limit) {
1638     mrg->image_cache_limit = add->image_cache_limit;
1639   }
1640   else {
1641     mrg->image_cache_limit = base->image_cache_limit;
1642   }
1643
1644   if (add->image_copyright) 
1645     mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1646   else
1647     mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1648
1649   if (add->server_side_encoding) {
1650     mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1651   }
1652   else 
1653   if (base->server_side_encoding) {
1654     mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1655   }
1656   else {
1657     mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1658   }
1659
1660   mrg->convrules    = apr_array_append(p, add->convrules, base->convrules);
1661
1662   if (add->cookie_db_dir) {
1663     mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1664   }
1665   else
1666   if (base->cookie_db_dir) {
1667     mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1668   }
1669   else {
1670     mrg->cookie_db_dir = NULL;
1671   }
1672
1673   if (add->cookie_timeout) {
1674     mrg->cookie_timeout   = add->cookie_timeout;
1675   }
1676   else
1677   if (base->cookie_db_dir) {
1678     mrg->cookie_timeout   = base->cookie_timeout;
1679   }
1680   else {
1681     mrg->cookie_timeout   = 0;
1682   }
1683
1684 #if defined(USE_MYSQL_COOKIE)
1685   if (add->mysql.host) {
1686     mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1687   }
1688   else if (base->mysql.host) {
1689     mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1690   }
1691   else {
1692     mrg->mysql.host = NULL;
1693   }
1694   if (add->mysql.port) {
1695     mrg->mysql.port = add->mysql.port;
1696   }
1697   else if (base->mysql.port) {
1698     mrg->mysql.port = base->mysql.port;
1699   }
1700   else {
1701     mrg->mysql.port = 0;
1702   }
1703
1704   if (add->mysql.database) {
1705     mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1706   }
1707   else if (base->mysql.database) {
1708     mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1709   }
1710   else {
1711     mrg->mysql.database = NULL;
1712   }
1713
1714   if (add->mysql.username) {
1715     mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1716   }
1717   else if (base->mysql.username) {
1718     mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1719   }
1720   else {
1721     mrg->mysql.username = NULL;
1722   }
1723
1724   if (add->mysql.password) {
1725     mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1726   }
1727   else if (base->mysql.password) {
1728     mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1729   }
1730   else {
1731     mrg->mysql.password = NULL;
1732   }
1733
1734   if (add->mysql.tablename) {
1735     mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1736   }
1737   else if (base->mysql.tablename) {
1738     mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1739   }
1740   else {
1741     mrg->mysql.tablename = NULL;
1742   }
1743
1744   if (add->mysql.socket_path) {
1745     mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1746   }
1747   else if (base->mysql.socket_path) {
1748     mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1749   }
1750   else {
1751     mrg->mysql.socket_path = NULL;
1752   }
1753
1754   if (add->mysql.charset) {
1755     mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1756   }
1757   else if (base->mysql.charset) {
1758     mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1759   }
1760   else {
1761     mrg->mysql.charset = NULL;
1762   }
1763 #endif
1764 #if defined(USE_MEMCACHE_COOKIE)
1765   if (add->memcache.host) {
1766     mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1767   }
1768   else if (base->memcache.host) {
1769     mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1770   }
1771   else {
1772     mrg->memcache.host = NULL;
1773   }
1774   if (add->memcache.port) {
1775     mrg->memcache.port = add->memcache.port;
1776   }
1777   else if (base->memcache.port) {
1778     mrg->memcache.port = base->memcache.port;
1779   }
1780   else {
1781     mrg->memcache.port = 0;
1782   }
1783 #endif
1784   if (add->cookie_store_type) {
1785     mrg->cookie_store_type = add->cookie_store_type;
1786   }
1787   else if (base->cookie_store_type) {
1788     mrg->cookie_store_type = base->cookie_store_type;
1789   }
1790   else {
1791     mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1792   }
1793   if (add->cookie_lazy_mode) {
1794     mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1795   }
1796   else if (base->cookie_lazy_mode) {
1797     mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1798   }
1799   else {
1800     mrg->cookie_lazy_mode = 0;
1801   }
1802   if (add->new_line_type) {
1803     mrg->new_line_type = add->new_line_type;
1804   }
1805   else if (base->new_line_type) {
1806     mrg->new_line_type = base->new_line_type;
1807   }
1808   else {
1809     mrg->new_line_type = NLTYPE_NIL;
1810   }
1811   return mrg;
1812 }
1813
1814
1815 static int
1816 chxj_command_parse_take5(
1817   const char *arg, 
1818   char       **prm1, 
1819   char       **prm2, 
1820   char       **prm3, 
1821   char       **prm4, 
1822   char       **prm5)
1823 {
1824   int  isquoted;
1825   char *strp;
1826
1827   strp = (char *)arg;
1828
1829   for (;*strp == ' '||*strp == '\t'; strp++) ;
1830
1831   isquoted = 0; 
1832   if (*strp == '"') { 
1833     isquoted = 1;
1834     strp++;
1835   }
1836
1837   *prm1 = strp;
1838
1839   for (; *strp != '\0'; strp++) {
1840     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1841     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1842       strp++;
1843       continue;
1844     }
1845
1846     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1847     ||  (isquoted  && *strp == '"'))
1848       break;
1849   }
1850
1851   if (! *strp) {
1852     *prm2 = strp;
1853     *prm3 = strp;
1854     *prm4 = strp;
1855     *prm5 = strp;
1856     return 1;
1857   }
1858
1859   *strp++ = '\0';
1860
1861   for (;*strp == ' '||*strp == '\t'; strp++) ;
1862
1863   isquoted = 0; 
1864   if (*strp == '"') { 
1865     isquoted = 1;
1866     strp++;
1867   }
1868
1869   *prm2 = strp;
1870   for (; *strp != '\0'; strp++) {
1871     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1872     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1873       strp++;
1874       continue;
1875     }
1876
1877     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1878     ||  (isquoted  && *strp == '"'))
1879       break;
1880   }
1881
1882   if (! *strp) {
1883     *prm3 = strp;
1884     *prm4 = strp;
1885     *prm5 = strp;
1886     return 0;
1887   }
1888
1889   *strp++ = '\0';
1890
1891   for (;*strp == ' '||*strp == '\t'; strp++);
1892
1893   isquoted = 0; 
1894   if (*strp == '"') { 
1895     isquoted = 1;
1896     strp++;
1897   }
1898   *prm3 = strp;
1899   for (; *strp != '\0'; strp++) {
1900     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1901     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1902       strp++;
1903       continue;
1904     }
1905
1906     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1907     ||  (isquoted  && *strp == '"'))
1908       break;
1909   }
1910
1911   if (! *strp) {
1912     *prm4 = strp;
1913     *prm5 = strp;
1914     return 0;
1915   }
1916
1917   *strp++ = '\0';
1918
1919   for (;*strp == ' '||*strp == '\t'; strp++);
1920
1921   isquoted = 0; 
1922   if (*strp == '"') { 
1923     isquoted = 1;
1924     strp++;
1925   }
1926   *prm4 = strp;
1927   for (; *strp != '\0'; strp++) {
1928     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1929     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1930       strp++;
1931       continue;
1932     }
1933
1934     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1935     ||  (isquoted  && *strp == '"'))
1936       break;
1937   }
1938
1939   if (! *strp) {
1940     *prm5 = strp;
1941     return 0;
1942   }
1943
1944   *strp++ = '\0';
1945
1946   for (;*strp == ' '||*strp == '\t'; strp++);
1947
1948   isquoted = 0; 
1949   if (*strp == '"') { 
1950     isquoted = 1;
1951     strp++;
1952   }
1953   *prm5 = strp;
1954   for (; *strp != '\0'; strp++) {
1955     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1956     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1957       strp++;
1958       continue;
1959     }
1960
1961     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1962     ||  (isquoted  && *strp == '"'))
1963       break;
1964   }
1965   *strp = '\0';
1966
1967   return 0;
1968 }
1969
1970
1971 /**
1972  * The device definition file is loaded. 
1973  *
1974  * @param arg     [i]   The name of the device definition file is specified.
1975  * @param mconfig [i/o] The pointer to a set structure is specified. 
1976  * @param parms   [i]   
1977  */
1978 static const char * 
1979 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg) 
1980 {
1981   mod_chxj_config  *conf;
1982   Doc              doc;
1983
1984   doc.r = NULL;
1985
1986   if (strlen(arg) > 256) 
1987     return "mod_chxj: device data filename too long.";
1988
1989   conf = (mod_chxj_config *)mconfig;
1990   conf->device_data_file = apr_pstrdup(parms->pool, arg);
1991
1992   qs_init_malloc(&doc);
1993   qs_init_root_node(&doc);
1994
1995   qs_parse_file((Doc *)&doc, (const char *)arg);
1996   chxj_load_device_data(&doc,parms->pool, conf);
1997   qs_all_free(&doc, QX_LOGMARK);
1998
1999   return NULL;
2000 }
2001
2002
2003 /**
2004  * Device definition information is loaded. 
2005  *
2006  * @param parms 
2007  * @param arg     [i]   The name of the device definition file is specified. 
2008  * @param mconfig [i/o] The pointer to a set structure is specified. 
2009  * @return 
2010  */
2011 static const char * 
2012 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg) 
2013 {
2014   mod_chxj_config *conf;
2015   char            *rtn;
2016   Doc              doc;
2017
2018   doc.r = NULL;
2019
2020
2021   if (strlen(arg) > 256) 
2022     return "mod_chxj: emoji data filename too long.";
2023
2024   conf = (mod_chxj_config *)mconfig;
2025   conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2026   qs_init_malloc(&doc);
2027   qs_init_root_node(&doc);
2028
2029   qs_parse_file((Doc *)&doc, (const char *)arg);
2030
2031   rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2032
2033   qs_all_free(&doc, QX_LOGMARK);
2034
2035
2036   return rtn;
2037 }
2038
2039
2040 static const char * 
2041 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg) 
2042 {
2043   mod_chxj_config *conf;
2044   Doc              doc;
2045
2046   doc.r = NULL;
2047
2048   if (strlen(arg) > 256) 
2049     return "image uri is too long.";
2050
2051   conf = (mod_chxj_config*)mconfig;
2052   if (strcasecmp("ON", arg) == 0) {
2053     conf->image = CHXJ_IMG_ON;
2054   }
2055   else {
2056     conf->image = CHXJ_IMG_OFF;
2057   }
2058
2059   return NULL;
2060 }
2061
2062
2063 static const char * 
2064 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg) 
2065 {
2066   mod_chxj_config *conf;
2067   Doc              doc;
2068
2069   doc.r = NULL;
2070
2071   if (strlen(arg) > 256) 
2072     return "cache dir name is too long.";
2073
2074   conf = (mod_chxj_config *)mconfig;
2075   conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2076
2077   return NULL;
2078 }
2079
2080
2081 static const char * 
2082 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg) 
2083 {
2084   mod_chxj_config *conf;
2085   Doc              doc;
2086
2087   doc.r = NULL;
2088
2089   if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN) 
2090     return "cache size is too long.";
2091
2092   conf = (mod_chxj_config *)mconfig;
2093   errno = 0;
2094   /* 
2095    * I use strtol function because strtoul is not portable function. 
2096    */
2097   conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2098   switch (errno) {
2099   case EINVAL:
2100     return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2101   case ERANGE:
2102     return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2103   default:
2104     break;
2105   }
2106   return NULL;
2107 }
2108
2109
2110 static const char * 
2111 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg) 
2112 {
2113   mod_chxj_config *conf;
2114   Doc              doc;
2115
2116   doc.r = NULL;
2117
2118   if (strlen(arg) > 256) 
2119     return "Copyright Flag is too long.";
2120
2121   conf = (mod_chxj_config *)mconfig;
2122   conf->image_copyright = apr_pstrdup(parms->pool, arg);
2123
2124   return NULL;
2125 }
2126
2127
2128 static const char *
2129 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2130 {
2131   int                 mode;
2132   ap_regex_t          *regexp;
2133   mod_chxj_config     *dconf;
2134   chxjconvrule_entry  *newrule;
2135   char                *prm1;
2136   char                *prm2;
2137   char                *prm3;
2138   char                *prm4;
2139   char                *prm5;
2140   char                *pstate;
2141   char                *action;
2142   char                *pp;
2143
2144   dconf = (mod_chxj_config *)mconfig;
2145
2146   if (strlen(arg) > 4096) 
2147     return "mod_chxj: ChxjConvertRule: is too long.";
2148
2149   dconf = (mod_chxj_config *)mconfig;
2150   if (dconf->convrules == NULL)
2151     dconf->convrules   = apr_array_make(cmd->pool, 
2152                                         2, 
2153                                         sizeof(chxjconvrule_entry));
2154
2155   newrule = apr_array_push(dconf->convrules);
2156
2157   newrule->flags  = 0;
2158   newrule->action = 0;
2159
2160   if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2161     return "ChxjConvertRule: bad argument line";
2162
2163   newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2164
2165   /* Parse action */
2166   for (;;) {
2167     if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2168       break;
2169     prm2 = NULL;
2170     switch(*action) {
2171     case 'e':
2172     case 'E':
2173       if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2174         newrule->action |= CONVRULE_ENGINE_ON_BIT;
2175       }
2176       else
2177       if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2178         newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2179       }
2180       break;
2181
2182     case 'C':
2183     case 'c':
2184       if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2185         newrule->action |= CONVRULE_COOKIE_ON_BIT;
2186       }
2187       break;
2188     default:
2189       break;
2190     }
2191   }
2192   
2193   pp = prm1;
2194   if (*pp == '!') {
2195     newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2196     pp++;
2197   }
2198
2199   mode = AP_REG_EXTENDED;
2200   if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2201     return "RewriteRule: cannot compile regular expression ";
2202
2203   newrule->regexp = regexp;
2204   if (*prm3)
2205     newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2206   else
2207     newrule->encoding = apr_pstrdup(cmd->pool, "none");
2208
2209   newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2210   if (*prm4)
2211     if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2212       newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2213
2214   newrule->user_agent = NULL;
2215   if (*prm5)
2216     newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2217     
2218   return NULL;
2219 }
2220
2221
2222 static const char *
2223 cmd_set_cookie_dir(
2224   cmd_parms   *cmd, 
2225   void        *mconfig, 
2226   const char  *arg)
2227 {
2228   mod_chxj_config *dconf;
2229
2230
2231   if (strlen(arg) > 4096) 
2232     return "mod_chxj: ChxjCookieDir is too long.";
2233
2234   dconf = (mod_chxj_config *)mconfig;
2235
2236   dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2237
2238   return NULL;
2239 }
2240
2241
2242 static const char *
2243 cmd_set_cookie_timeout(
2244   cmd_parms   *UNUSED(cmd), 
2245   void        *mconfig, 
2246   const char  *arg)
2247 {
2248   mod_chxj_config *dconf;
2249
2250   if (strlen(arg) > 4096) 
2251     return "mod_chxj: ChxjCookieTimeout is too long.";
2252
2253   if (chxj_chk_numeric(arg) != 0)
2254     return "mod_chxj: ChxjCookieTimeout is not numeric.";
2255
2256   dconf = (mod_chxj_config *)mconfig;
2257
2258   dconf->cookie_timeout = atoi(arg);
2259
2260   return NULL;
2261 }
2262
2263
2264 #if defined(USE_MYSQL_COOKIE)
2265 static const char *
2266 cmd_set_cookie_mysql_database(
2267   cmd_parms   *cmd, 
2268   void        *mconfig, 
2269   const char  *arg)
2270 {
2271   mod_chxj_config  *dconf;
2272
2273   if (strlen(arg) > 255) 
2274     return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2275
2276   dconf = (mod_chxj_config *)mconfig;
2277
2278   dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2279
2280   return NULL;
2281 }
2282
2283
2284 static const char *
2285 cmd_set_cookie_mysql_username(
2286   cmd_parms   *cmd, 
2287   void        *mconfig, 
2288   const char  *arg)
2289 {
2290   mod_chxj_config  *dconf;
2291
2292   if (strlen(arg) > 255) 
2293     return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2294
2295   dconf = (mod_chxj_config *)mconfig;
2296
2297   dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2298
2299   return NULL;
2300 }
2301
2302
2303 static const char *
2304 cmd_set_cookie_mysql_password(
2305   cmd_parms   *cmd, 
2306   void        *mconfig, 
2307   const char  *arg)
2308 {
2309   mod_chxj_config  *dconf;
2310
2311   if (strlen(arg) > 255) 
2312     return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2313
2314   dconf = (mod_chxj_config *)mconfig;
2315
2316   dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2317
2318   return NULL;
2319 }
2320
2321
2322 static const char *
2323 cmd_set_cookie_mysql_table_name(
2324   cmd_parms   *cmd, 
2325   void        *mconfig, 
2326   const char  *arg)
2327 {
2328   mod_chxj_config  *dconf;
2329
2330   if (strlen(arg) > 255) 
2331     return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2332
2333   dconf = (mod_chxj_config *)mconfig;
2334
2335   dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2336
2337   return NULL;
2338 }
2339
2340 static const char *
2341 cmd_set_cookie_mysql_port(
2342   cmd_parms   *UNUSED(cmd), 
2343   void        *mconfig, 
2344   const char  *arg)
2345 {
2346   mod_chxj_config *dconf;
2347
2348   if (strlen(arg) > 255) 
2349     return "mod_chxj: ChxjCookieMysqlPort is too long.";
2350
2351   dconf = (mod_chxj_config *)mconfig;
2352
2353   if (chxj_chk_numeric(arg) != 0)
2354     return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2355
2356   dconf = (mod_chxj_config *)mconfig;
2357
2358   dconf->mysql.port = chxj_atoi(arg);
2359
2360   return NULL;
2361 }
2362
2363
2364 static const char *
2365 cmd_set_cookie_mysql_host(
2366   cmd_parms   *cmd, 
2367   void        *mconfig, 
2368   const char  *arg)
2369 {
2370   mod_chxj_config  *dconf;
2371
2372   if (strlen(arg) > 255) 
2373     return "mod_chxj: ChxjCookieMysqlHost is too long.";
2374
2375   dconf = (mod_chxj_config *)mconfig;
2376
2377   dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2378
2379   return NULL;
2380 }
2381
2382
2383 static const char *
2384 cmd_set_cookie_mysql_socket_path(
2385   cmd_parms   *cmd, 
2386   void        *mconfig, 
2387   const char  *arg)
2388 {
2389   mod_chxj_config  *dconf;
2390
2391   if (strlen(arg) > 4096) 
2392     return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2393
2394   dconf = (mod_chxj_config *)mconfig;
2395
2396   dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2397
2398   return NULL;
2399 }
2400
2401
2402 static const char *
2403 cmd_set_cookie_mysql_charset(
2404   cmd_parms   *cmd, 
2405   void        *mconfig, 
2406   const char  *arg)
2407 {
2408   mod_chxj_config  *dconf;
2409
2410   if (strlen(arg) > 255) 
2411     return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2412
2413   dconf = (mod_chxj_config *)mconfig;
2414
2415   dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2416
2417   return NULL;
2418 }
2419 #endif
2420 #if defined(USE_MEMCACHE_COOKIE)
2421 static const char *
2422 cmd_set_cookie_memcache_port(
2423   cmd_parms   *UNUSED(cmd), 
2424   void        *mconfig, 
2425   const char  *arg)
2426 {
2427   mod_chxj_config *dconf;
2428
2429   if (strlen(arg) > 255) 
2430     return "mod_chxj: ChxjCookieMemcachePort is too long.";
2431
2432   dconf = (mod_chxj_config *)mconfig;
2433
2434   if (chxj_chk_numeric(arg) != 0)
2435     return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2436
2437   dconf = (mod_chxj_config *)mconfig;
2438
2439   dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2440
2441   return NULL;
2442 }
2443
2444
2445 static const char *
2446 cmd_set_cookie_memcache_host(
2447   cmd_parms   *cmd, 
2448   void        *mconfig, 
2449   const char  *arg)
2450 {
2451   mod_chxj_config  *dconf;
2452
2453   if (strlen(arg) > 255) 
2454     return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2455
2456   dconf = (mod_chxj_config *)mconfig;
2457
2458   dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2459
2460   return NULL;
2461 }
2462 #endif
2463
2464 static const char *
2465 cmd_set_cookie_lazy_mode(
2466   cmd_parms   *UNUSED(cmd), 
2467   void        *mconfig, 
2468   const char  *arg)
2469 {
2470   mod_chxj_config  *dconf;
2471
2472   if (strlen(arg) > 255) 
2473     return "mod_chxj: ChxjCookieLazyMode is too long.";
2474
2475   dconf = (mod_chxj_config *)mconfig;
2476
2477   if (strcasecmp("TRUE",arg) == 0) {
2478     dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2479   }
2480   else {
2481     dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2482   }
2483
2484   return NULL;
2485 }
2486
2487 static const char *
2488 cmd_set_cookie_store_type(
2489   cmd_parms   *UNUSED(cmd), 
2490   void        *mconfig, 
2491   const char  *arg)
2492 {
2493   mod_chxj_config  *dconf;
2494
2495   if (strlen(arg) > 255) 
2496     return "mod_chxj: ChxjCookieStoreType is too long.";
2497
2498   dconf = (mod_chxj_config *)mconfig;
2499
2500   if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2501     dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2502   }
2503   else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2504     dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2505   }
2506   else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2507     dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2508   }
2509   else {
2510     dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2511   }
2512
2513   return NULL;
2514 }
2515
2516 static const char *
2517 cmd_set_forward_url_base(
2518   cmd_parms   *cmd,
2519   void        *mconfig,
2520   const char  *arg)
2521 {
2522  mod_chxj_config *dconf;
2523
2524   if (strlen(arg) > 255)
2525     return "mod_chxj: ChxjForwardUrlBase is too long.";
2526
2527   dconf = (mod_chxj_config *)mconfig;
2528
2529   dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2530
2531   return NULL;
2532 }
2533
2534 static const char *
2535 cmd_set_forward_server_ip(
2536   cmd_parms   *cmd,
2537   void        *mconfig,
2538   const char  *arg)
2539 {
2540   mod_chxj_config *dconf;
2541
2542   if (strlen(arg) > 255)
2543     return "mod_chxj: ChxjForwardServerIp is too long.";
2544
2545   dconf = (mod_chxj_config *)mconfig;
2546
2547   dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2548
2549   return NULL;
2550 }
2551
2552 static const char *
2553 cmd_set_new_line_type(
2554   cmd_parms   *UNUSED(cmd), 
2555   void        *mconfig, 
2556   const char  *arg)
2557 {
2558   mod_chxj_config  *dconf;
2559   if (strlen(arg) > 255)
2560     return "mod_chxj: ChxjNewLineType is too long.";
2561
2562   dconf = (mod_chxj_config *)mconfig;
2563
2564   if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2565     dconf->new_line_type = NLTYPE_CRLF;
2566   }
2567   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2568     dconf->new_line_type = NLTYPE_LF;
2569   }
2570   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2571     dconf->new_line_type = NLTYPE_CR;
2572   }
2573   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2574     dconf->new_line_type = NLTYPE_NONE;
2575   }
2576   else {
2577     return "mod_chxj: invalid value (ChxjNewLineType)";
2578   }
2579   return NULL;
2580 }
2581
2582
2583 static const command_rec cmds[] = {
2584   AP_INIT_TAKE1(
2585     "ChxjLoadDeviceData",
2586     cmd_load_device_data,
2587     NULL,
2588     OR_ALL,
2589     "Load Device Data"),
2590   AP_INIT_TAKE1(
2591     "ChxjLoadEmojiData",
2592     cmd_load_emoji_data,
2593     NULL,
2594     OR_ALL,
2595     "Load Emoji Data"),
2596   AP_INIT_TAKE1(
2597     "ChxjImageEngine",
2598     cmd_set_image_engine,
2599     NULL,
2600     OR_ALL,
2601     "Convert Target URI"),
2602   AP_INIT_TAKE1(
2603     "ChxjImageCacheDir",
2604     cmd_set_image_cache_dir,
2605     NULL,
2606     OR_ALL,
2607     "Image Cache Directory"),
2608   AP_INIT_TAKE1(
2609     "ChxjImageCacheLimit",
2610     cmd_set_image_cache_limit,
2611     NULL,
2612     OR_ALL,
2613     "Image Cache Limit"),
2614   AP_INIT_TAKE1(
2615     "ChxjImageCopyright",
2616     cmd_set_image_copyright,
2617     NULL,
2618     OR_ALL,
2619     "Copyright Flag"),
2620   AP_INIT_RAW_ARGS(
2621     "ChxjConvertRule",
2622     cmd_convert_rule,
2623     NULL, 
2624     OR_FILEINFO,
2625     "an URL-applied regexp-pattern and a substitution URL"),
2626   AP_INIT_TAKE1(
2627     "ChxjCookieDir",
2628     cmd_set_cookie_dir,
2629     NULL,
2630     OR_ALL,
2631     "save cookie.db directory."),
2632   AP_INIT_TAKE1(
2633     "ChxjCookieTimeout",
2634     cmd_set_cookie_timeout,
2635     NULL,
2636     OR_ALL,
2637     "The compulsion time-out time of the cookie is specified. "),
2638   AP_INIT_TAKE1(
2639     "ChxjCookieStoreType",
2640     cmd_set_cookie_store_type,
2641     NULL,
2642     OR_ALL,
2643     "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2644   AP_INIT_TAKE1(
2645     "ChxjCookieLazyMode",
2646     cmd_set_cookie_lazy_mode,
2647     NULL,
2648     OR_ALL,
2649     "OneTimeID is negligently done. (TRUE/FALSE)"),
2650 #if defined(USE_MYSQL_COOKIE)
2651   AP_INIT_TAKE1(
2652     "ChxjCookieMysqlHost",
2653     cmd_set_cookie_mysql_host,
2654     NULL,
2655     OR_ALL,
2656     "The MySQL database host used by saving Cookie"),
2657   AP_INIT_TAKE1(
2658     "ChxjCookieMysqlPort",
2659     cmd_set_cookie_mysql_port,
2660     NULL,
2661     OR_ALL,
2662     "The MySQL database port used by saving Cookie"),
2663   AP_INIT_TAKE1(
2664     "ChxjCookieMysqlDatabase",
2665     cmd_set_cookie_mysql_database,
2666     NULL,
2667     OR_ALL,
2668     "The MySQL database name used by saving Cookie"),
2669   AP_INIT_TAKE1(
2670     "ChxjCookieMysqlUsername",
2671     cmd_set_cookie_mysql_username,
2672     NULL,
2673     OR_ALL,
2674     "The MySQL username used by saving Cookie"),
2675   AP_INIT_TAKE1(
2676     "ChxjCookieMysqlPassword",
2677     cmd_set_cookie_mysql_password,
2678     NULL,
2679     OR_ALL,
2680     "The MySQL password used by saving Cookie"),
2681   AP_INIT_TAKE1(
2682     "ChxjCookieMysqlTableName",
2683     cmd_set_cookie_mysql_table_name,
2684     NULL,
2685     OR_ALL,
2686     "The MySQL table name used by saving Cookie"),
2687   AP_INIT_TAKE1(
2688     "ChxjCookieMysqlSocketPath",
2689     cmd_set_cookie_mysql_socket_path,
2690     NULL,
2691     OR_ALL,
2692     "The MySQL socket path used by saving Cookie"),
2693   AP_INIT_TAKE1(
2694     "ChxjCookieMysqlCharset",
2695     cmd_set_cookie_mysql_charset,
2696     NULL,
2697     OR_ALL,
2698     "The MySQL charset used by saving Cookie"),
2699 #endif
2700 #if defined(USE_MEMCACHE_COOKIE)
2701   AP_INIT_TAKE1(
2702     "ChxjCookieMemcacheHost",
2703     cmd_set_cookie_memcache_host,
2704     NULL,
2705     OR_ALL,
2706     "The Memcached host used by saving Cookie"),
2707   AP_INIT_TAKE1(
2708     "ChxjCookieMemcachePort",
2709     cmd_set_cookie_memcache_port,
2710     NULL,
2711     OR_ALL,
2712     "The Memcached port used by saving Cookie"),
2713 #endif
2714   AP_INIT_TAKE1(
2715     "ChxjNewLineType",
2716     cmd_set_new_line_type,
2717     NULL,
2718     OR_ALL,
2719     "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
2720   AP_INIT_TAKE1(
2721     "ChxjForwardUrlBase",
2722     cmd_set_forward_url_base,
2723     NULL,
2724     OR_ALL,
2725     "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2726   AP_INIT_TAKE1(
2727     "ChxjForwardServerIp",
2728     cmd_set_forward_server_ip,
2729     NULL,
2730     OR_ALL,
2731     "The forward server ip(default: this server ip)"),
2732   {NULL}
2733 };
2734
2735
2736 /*----------------------------------------------------------------------------*/
2737 /* Dispatch list for API hooks                                                */
2738 /*----------------------------------------------------------------------------*/
2739 module AP_MODULE_DECLARE_DATA chxj_module = {
2740   STANDARD20_MODULE_STUFF, 
2741   chxj_create_per_dir_config,          /* create per-dir    config structures */
2742   chxj_merge_per_dir_config,           /* merge  per-dir    config structures */
2743   chxj_config_server_create,           /* create per-server config structures */
2744   NULL,                                /* merge  per-server config structures */
2745   cmds,                                /* table of config file commands       */
2746   chxj_register_hooks                  /* register hooks                      */
2747 };
2748 /*
2749  * vim:ts=2 et
2750  */