OSDN Git Service

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