OSDN Git Service

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