OSDN Git Service

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