OSDN Git Service

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