OSDN Git Service

Merge branch 'branch_0.12.0' into branch_0.12.0-svn
[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   return mrg;
1824 }
1825
1826
1827 static int
1828 chxj_command_parse_take5(
1829   const char *arg, 
1830   char       **prm1, 
1831   char       **prm2, 
1832   char       **prm3, 
1833   char       **prm4, 
1834   char       **prm5)
1835 {
1836   int  isquoted;
1837   char *strp;
1838
1839   strp = (char *)arg;
1840
1841   for (;*strp == ' '||*strp == '\t'; strp++) ;
1842
1843   isquoted = 0; 
1844   if (*strp == '"') { 
1845     isquoted = 1;
1846     strp++;
1847   }
1848
1849   *prm1 = strp;
1850
1851   for (; *strp != '\0'; strp++) {
1852     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1853     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1854       strp++;
1855       continue;
1856     }
1857
1858     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1859     ||  (isquoted  && *strp == '"'))
1860       break;
1861   }
1862
1863   if (! *strp) {
1864     *prm2 = strp;
1865     *prm3 = strp;
1866     *prm4 = strp;
1867     *prm5 = strp;
1868     return 1;
1869   }
1870
1871   *strp++ = '\0';
1872
1873   for (;*strp == ' '||*strp == '\t'; strp++) ;
1874
1875   isquoted = 0; 
1876   if (*strp == '"') { 
1877     isquoted = 1;
1878     strp++;
1879   }
1880
1881   *prm2 = strp;
1882   for (; *strp != '\0'; strp++) {
1883     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1884     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1885       strp++;
1886       continue;
1887     }
1888
1889     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1890     ||  (isquoted  && *strp == '"'))
1891       break;
1892   }
1893
1894   if (! *strp) {
1895     *prm3 = strp;
1896     *prm4 = strp;
1897     *prm5 = strp;
1898     return 0;
1899   }
1900
1901   *strp++ = '\0';
1902
1903   for (;*strp == ' '||*strp == '\t'; strp++);
1904
1905   isquoted = 0; 
1906   if (*strp == '"') { 
1907     isquoted = 1;
1908     strp++;
1909   }
1910   *prm3 = strp;
1911   for (; *strp != '\0'; strp++) {
1912     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1913     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1914       strp++;
1915       continue;
1916     }
1917
1918     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1919     ||  (isquoted  && *strp == '"'))
1920       break;
1921   }
1922
1923   if (! *strp) {
1924     *prm4 = strp;
1925     *prm5 = strp;
1926     return 0;
1927   }
1928
1929   *strp++ = '\0';
1930
1931   for (;*strp == ' '||*strp == '\t'; strp++);
1932
1933   isquoted = 0; 
1934   if (*strp == '"') { 
1935     isquoted = 1;
1936     strp++;
1937   }
1938   *prm4 = strp;
1939   for (; *strp != '\0'; strp++) {
1940     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1941     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1942       strp++;
1943       continue;
1944     }
1945
1946     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1947     ||  (isquoted  && *strp == '"'))
1948       break;
1949   }
1950
1951   if (! *strp) {
1952     *prm5 = strp;
1953     return 0;
1954   }
1955
1956   *strp++ = '\0';
1957
1958   for (;*strp == ' '||*strp == '\t'; strp++);
1959
1960   isquoted = 0; 
1961   if (*strp == '"') { 
1962     isquoted = 1;
1963     strp++;
1964   }
1965   *prm5 = strp;
1966   for (; *strp != '\0'; strp++) {
1967     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1968     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1969       strp++;
1970       continue;
1971     }
1972
1973     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1974     ||  (isquoted  && *strp == '"'))
1975       break;
1976   }
1977   *strp = '\0';
1978
1979   return 0;
1980 }
1981
1982
1983 /**
1984  * The device definition file is loaded. 
1985  *
1986  * @param arg     [i]   The name of the device definition file is specified.
1987  * @param mconfig [i/o] The pointer to a set structure is specified. 
1988  * @param parms   [i]   
1989  */
1990 static const char * 
1991 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg) 
1992 {
1993   mod_chxj_config  *conf;
1994   Doc              doc;
1995
1996   doc.r = NULL;
1997
1998   if (strlen(arg) > 256) 
1999     return "mod_chxj: device data filename too long.";
2000
2001   conf = (mod_chxj_config *)mconfig;
2002   conf->device_data_file = apr_pstrdup(parms->pool, arg);
2003
2004   qs_init_malloc(&doc);
2005   qs_init_root_node(&doc);
2006
2007   qs_parse_file((Doc *)&doc, (const char *)arg);
2008   chxj_load_device_data(&doc,parms->pool, conf);
2009   qs_all_free(&doc, QX_LOGMARK);
2010
2011   return NULL;
2012 }
2013
2014
2015 /**
2016  * Device definition information is loaded. 
2017  *
2018  * @param parms 
2019  * @param arg     [i]   The name of the device definition file is specified. 
2020  * @param mconfig [i/o] The pointer to a set structure is specified. 
2021  * @return 
2022  */
2023 static const char * 
2024 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg) 
2025 {
2026   mod_chxj_config *conf;
2027   char            *rtn;
2028   Doc              doc;
2029
2030   doc.r = NULL;
2031
2032
2033   if (strlen(arg) > 256) 
2034     return "mod_chxj: emoji data filename too long.";
2035
2036   conf = (mod_chxj_config *)mconfig;
2037   conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2038   qs_init_malloc(&doc);
2039   qs_init_root_node(&doc);
2040
2041   qs_parse_file((Doc *)&doc, (const char *)arg);
2042
2043   rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2044
2045   qs_all_free(&doc, QX_LOGMARK);
2046
2047
2048   return rtn;
2049 }
2050
2051
2052 static const char * 
2053 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg) 
2054 {
2055   mod_chxj_config *conf;
2056   Doc              doc;
2057
2058   doc.r = NULL;
2059
2060   if (strlen(arg) > 256) 
2061     return "image uri is too long.";
2062
2063   conf = (mod_chxj_config*)mconfig;
2064   if (strcasecmp("ON", arg) == 0) {
2065     conf->image = CHXJ_IMG_ON;
2066   }
2067   else {
2068     conf->image = CHXJ_IMG_OFF;
2069   }
2070
2071   return NULL;
2072 }
2073
2074
2075 static const char * 
2076 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg) 
2077 {
2078   mod_chxj_config *conf;
2079   Doc              doc;
2080
2081   doc.r = NULL;
2082
2083   if (strlen(arg) > 256) 
2084     return "cache dir name is too long.";
2085
2086   conf = (mod_chxj_config *)mconfig;
2087   conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2088
2089   return NULL;
2090 }
2091
2092
2093 static const char * 
2094 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg) 
2095 {
2096   mod_chxj_config *conf;
2097   Doc              doc;
2098
2099   doc.r = NULL;
2100
2101   if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN) 
2102     return "cache size is too long.";
2103
2104   conf = (mod_chxj_config *)mconfig;
2105   errno = 0;
2106   /* 
2107    * I use strtol function because strtoul is not portable function. 
2108    */
2109   conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2110   switch (errno) {
2111   case EINVAL:
2112     return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2113   case ERANGE:
2114     return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2115   default:
2116     break;
2117   }
2118   return NULL;
2119 }
2120
2121
2122 static const char * 
2123 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg) 
2124 {
2125   mod_chxj_config *conf;
2126   Doc              doc;
2127
2128   doc.r = NULL;
2129
2130   if (strlen(arg) > 256) 
2131     return "Copyright Flag is too long.";
2132
2133   conf = (mod_chxj_config *)mconfig;
2134   conf->image_copyright = apr_pstrdup(parms->pool, arg);
2135
2136   return NULL;
2137 }
2138
2139
2140 static const char *
2141 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2142 {
2143   int                 mode;
2144   ap_regex_t          *regexp;
2145   mod_chxj_config     *dconf;
2146   chxjconvrule_entry  *newrule;
2147   char                *prm1;
2148   char                *prm2;
2149   char                *prm3;
2150   char                *prm4;
2151   char                *prm5;
2152   char                *pstate;
2153   char                *action;
2154   char                *pp;
2155
2156   dconf = (mod_chxj_config *)mconfig;
2157
2158   if (strlen(arg) > 4096) 
2159     return "mod_chxj: ChxjConvertRule: is too long.";
2160
2161   dconf = (mod_chxj_config *)mconfig;
2162   if (dconf->convrules == NULL)
2163     dconf->convrules   = apr_array_make(cmd->pool, 
2164                                         2, 
2165                                         sizeof(chxjconvrule_entry));
2166
2167   newrule = apr_array_push(dconf->convrules);
2168
2169   newrule->flags  = 0;
2170   newrule->action = 0;
2171
2172   if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2173     return "ChxjConvertRule: bad argument line";
2174
2175   newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2176
2177   /* Parse action */
2178   for (;;) {
2179     if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2180       break;
2181     prm2 = NULL;
2182     switch(*action) {
2183     case 'e':
2184     case 'E':
2185       if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2186         newrule->action |= CONVRULE_ENGINE_ON_BIT;
2187       }
2188       else
2189       if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2190         newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2191       }
2192       break;
2193
2194     case 'C':
2195     case 'c':
2196       if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2197         newrule->action |= CONVRULE_COOKIE_ON_BIT;
2198       }
2199       break;
2200
2201     case 'Z':
2202     case 'z':
2203       if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2204         newrule->action |= CONVRULE_Z2H_ON_BIT;
2205       }
2206       else
2207       if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2208         newrule->action |= CONVRULE_Z2H_OFF_BIT;
2209       }
2210       else
2211       if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2212         newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2213       }
2214       else
2215       if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2216         newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2217       }
2218       else
2219       if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2220         newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2221       }
2222       else
2223       if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2224         newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2225       }
2226       else
2227       if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2228         newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2229       }
2230       else
2231       if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2232         newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2233       }
2234       break;
2235
2236     default:
2237       break;
2238     }
2239   }
2240   
2241   pp = prm1;
2242   if (*pp == '!') {
2243     newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2244     pp++;
2245   }
2246
2247   mode = AP_REG_EXTENDED;
2248   if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2249     return "RewriteRule: cannot compile regular expression ";
2250
2251   newrule->regexp = regexp;
2252   if (*prm3)
2253     newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2254   else
2255     newrule->encoding = apr_pstrdup(cmd->pool, "none");
2256
2257   newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2258   if (*prm4)
2259     if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2260       newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2261
2262   newrule->user_agent = NULL;
2263   if (*prm5)
2264     newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2265     
2266   return NULL;
2267 }
2268
2269
2270 static const char *
2271 cmd_set_cookie_dir(
2272   cmd_parms   *cmd, 
2273   void        *mconfig, 
2274   const char  *arg)
2275 {
2276   mod_chxj_config *dconf;
2277
2278
2279   if (strlen(arg) > 4096) 
2280     return "mod_chxj: ChxjCookieDir is too long.";
2281
2282   dconf = (mod_chxj_config *)mconfig;
2283
2284   dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2285
2286   return NULL;
2287 }
2288
2289
2290 static const char *
2291 cmd_set_cookie_timeout(
2292   cmd_parms   *UNUSED(cmd), 
2293   void        *mconfig, 
2294   const char  *arg)
2295 {
2296   mod_chxj_config *dconf;
2297
2298   if (strlen(arg) > 4096) 
2299     return "mod_chxj: ChxjCookieTimeout is too long.";
2300
2301   if (chxj_chk_numeric(arg) != 0)
2302     return "mod_chxj: ChxjCookieTimeout is not numeric.";
2303
2304   dconf = (mod_chxj_config *)mconfig;
2305
2306   dconf->cookie_timeout = atoi(arg);
2307
2308   return NULL;
2309 }
2310
2311
2312 #if defined(USE_MYSQL_COOKIE)
2313 static const char *
2314 cmd_set_cookie_mysql_database(
2315   cmd_parms   *cmd, 
2316   void        *mconfig, 
2317   const char  *arg)
2318 {
2319   mod_chxj_config  *dconf;
2320
2321   if (strlen(arg) > 255) 
2322     return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2323
2324   dconf = (mod_chxj_config *)mconfig;
2325
2326   dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2327
2328   return NULL;
2329 }
2330
2331
2332 static const char *
2333 cmd_set_cookie_mysql_username(
2334   cmd_parms   *cmd, 
2335   void        *mconfig, 
2336   const char  *arg)
2337 {
2338   mod_chxj_config  *dconf;
2339
2340   if (strlen(arg) > 255) 
2341     return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2342
2343   dconf = (mod_chxj_config *)mconfig;
2344
2345   dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2346
2347   return NULL;
2348 }
2349
2350
2351 static const char *
2352 cmd_set_cookie_mysql_password(
2353   cmd_parms   *cmd, 
2354   void        *mconfig, 
2355   const char  *arg)
2356 {
2357   mod_chxj_config  *dconf;
2358
2359   if (strlen(arg) > 255) 
2360     return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2361
2362   dconf = (mod_chxj_config *)mconfig;
2363
2364   dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2365
2366   return NULL;
2367 }
2368
2369
2370 static const char *
2371 cmd_set_cookie_mysql_table_name(
2372   cmd_parms   *cmd, 
2373   void        *mconfig, 
2374   const char  *arg)
2375 {
2376   mod_chxj_config  *dconf;
2377
2378   if (strlen(arg) > 255) 
2379     return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2380
2381   dconf = (mod_chxj_config *)mconfig;
2382
2383   dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2384
2385   return NULL;
2386 }
2387
2388 static const char *
2389 cmd_set_cookie_mysql_port(
2390   cmd_parms   *UNUSED(cmd), 
2391   void        *mconfig, 
2392   const char  *arg)
2393 {
2394   mod_chxj_config *dconf;
2395
2396   if (strlen(arg) > 255) 
2397     return "mod_chxj: ChxjCookieMysqlPort is too long.";
2398
2399   dconf = (mod_chxj_config *)mconfig;
2400
2401   if (chxj_chk_numeric(arg) != 0)
2402     return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2403
2404   dconf = (mod_chxj_config *)mconfig;
2405
2406   dconf->mysql.port = chxj_atoi(arg);
2407
2408   return NULL;
2409 }
2410
2411
2412 static const char *
2413 cmd_set_cookie_mysql_host(
2414   cmd_parms   *cmd, 
2415   void        *mconfig, 
2416   const char  *arg)
2417 {
2418   mod_chxj_config  *dconf;
2419
2420   if (strlen(arg) > 255) 
2421     return "mod_chxj: ChxjCookieMysqlHost is too long.";
2422
2423   dconf = (mod_chxj_config *)mconfig;
2424
2425   dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2426
2427   return NULL;
2428 }
2429
2430
2431 static const char *
2432 cmd_set_cookie_mysql_socket_path(
2433   cmd_parms   *cmd, 
2434   void        *mconfig, 
2435   const char  *arg)
2436 {
2437   mod_chxj_config  *dconf;
2438
2439   if (strlen(arg) > 4096) 
2440     return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2441
2442   dconf = (mod_chxj_config *)mconfig;
2443
2444   dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2445
2446   return NULL;
2447 }
2448
2449
2450 static const char *
2451 cmd_set_cookie_mysql_charset(
2452   cmd_parms   *cmd, 
2453   void        *mconfig, 
2454   const char  *arg)
2455 {
2456   mod_chxj_config  *dconf;
2457
2458   if (strlen(arg) > 255) 
2459     return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2460
2461   dconf = (mod_chxj_config *)mconfig;
2462
2463   dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2464
2465   return NULL;
2466 }
2467 #endif
2468 #if defined(USE_MEMCACHE_COOKIE)
2469 static const char *
2470 cmd_set_cookie_memcache_port(
2471   cmd_parms   *UNUSED(cmd), 
2472   void        *mconfig, 
2473   const char  *arg)
2474 {
2475   mod_chxj_config *dconf;
2476
2477   if (strlen(arg) > 255) 
2478     return "mod_chxj: ChxjCookieMemcachePort is too long.";
2479
2480   dconf = (mod_chxj_config *)mconfig;
2481
2482   if (chxj_chk_numeric(arg) != 0)
2483     return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2484
2485   dconf = (mod_chxj_config *)mconfig;
2486
2487   dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2488
2489   return NULL;
2490 }
2491
2492
2493 static const char *
2494 cmd_set_cookie_memcache_host(
2495   cmd_parms   *cmd, 
2496   void        *mconfig, 
2497   const char  *arg)
2498 {
2499   mod_chxj_config  *dconf;
2500
2501   if (strlen(arg) > 255) 
2502     return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2503
2504   dconf = (mod_chxj_config *)mconfig;
2505
2506   dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2507
2508   return NULL;
2509 }
2510 #endif
2511
2512 static const char *
2513 cmd_set_cookie_lazy_mode(
2514   cmd_parms   *UNUSED(cmd), 
2515   void        *mconfig, 
2516   const char  *arg)
2517 {
2518   mod_chxj_config  *dconf;
2519
2520   if (strlen(arg) > 255) 
2521     return "mod_chxj: ChxjCookieLazyMode is too long.";
2522
2523   dconf = (mod_chxj_config *)mconfig;
2524
2525   if (strcasecmp("TRUE",arg) == 0) {
2526     dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2527   }
2528   else {
2529     dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2530   }
2531
2532   return NULL;
2533 }
2534
2535 static const char *
2536 cmd_set_cookie_store_type(
2537   cmd_parms   *UNUSED(cmd), 
2538   void        *mconfig, 
2539   const char  *arg)
2540 {
2541   mod_chxj_config  *dconf;
2542
2543   if (strlen(arg) > 255) 
2544     return "mod_chxj: ChxjCookieStoreType is too long.";
2545
2546   dconf = (mod_chxj_config *)mconfig;
2547
2548   if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2549     dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2550   }
2551   else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2552     dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2553   }
2554   else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2555     dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2556   }
2557   else {
2558     dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2559   }
2560
2561   return NULL;
2562 }
2563
2564 static const char *
2565 cmd_set_forward_url_base(
2566   cmd_parms   *cmd,
2567   void        *mconfig,
2568   const char  *arg)
2569 {
2570  mod_chxj_config *dconf;
2571
2572   if (strlen(arg) > 255)
2573     return "mod_chxj: ChxjForwardUrlBase is too long.";
2574
2575   dconf = (mod_chxj_config *)mconfig;
2576
2577   dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2578
2579   return NULL;
2580 }
2581
2582 static const char *
2583 cmd_set_forward_server_ip(
2584   cmd_parms   *cmd,
2585   void        *mconfig,
2586   const char  *arg)
2587 {
2588   mod_chxj_config *dconf;
2589
2590   if (strlen(arg) > 255)
2591     return "mod_chxj: ChxjForwardServerIp is too long.";
2592
2593   dconf = (mod_chxj_config *)mconfig;
2594
2595   dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2596
2597   return NULL;
2598 }
2599
2600 static const char *
2601 cmd_set_new_line_type(
2602   cmd_parms   *UNUSED(cmd), 
2603   void        *mconfig, 
2604   const char  *arg)
2605 {
2606   mod_chxj_config  *dconf;
2607   if (strlen(arg) > 255)
2608     return "mod_chxj: ChxjNewLineType is too long.";
2609
2610   dconf = (mod_chxj_config *)mconfig;
2611
2612   if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2613     dconf->new_line_type = NLTYPE_CRLF;
2614   }
2615   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2616     dconf->new_line_type = NLTYPE_LF;
2617   }
2618   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2619     dconf->new_line_type = NLTYPE_CR;
2620   }
2621   else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2622     dconf->new_line_type = NLTYPE_NONE;
2623   }
2624   else {
2625     return "mod_chxj: invalid value (ChxjNewLineType)";
2626   }
2627   return NULL;
2628 }
2629
2630
2631 static const command_rec cmds[] = {
2632   AP_INIT_TAKE1(
2633     "ChxjLoadDeviceData",
2634     cmd_load_device_data,
2635     NULL,
2636     OR_ALL,
2637     "Load Device Data"),
2638   AP_INIT_TAKE1(
2639     "ChxjLoadEmojiData",
2640     cmd_load_emoji_data,
2641     NULL,
2642     OR_ALL,
2643     "Load Emoji Data"),
2644   AP_INIT_TAKE1(
2645     "ChxjImageEngine",
2646     cmd_set_image_engine,
2647     NULL,
2648     OR_ALL,
2649     "Convert Target URI"),
2650   AP_INIT_TAKE1(
2651     "ChxjImageCacheDir",
2652     cmd_set_image_cache_dir,
2653     NULL,
2654     OR_ALL,
2655     "Image Cache Directory"),
2656   AP_INIT_TAKE1(
2657     "ChxjImageCacheLimit",
2658     cmd_set_image_cache_limit,
2659     NULL,
2660     OR_ALL,
2661     "Image Cache Limit"),
2662   AP_INIT_TAKE1(
2663     "ChxjImageCopyright",
2664     cmd_set_image_copyright,
2665     NULL,
2666     OR_ALL,
2667     "Copyright Flag"),
2668   AP_INIT_RAW_ARGS(
2669     "ChxjConvertRule",
2670     cmd_convert_rule,
2671     NULL, 
2672     OR_FILEINFO,
2673     "an URL-applied regexp-pattern and a substitution URL"),
2674   AP_INIT_TAKE1(
2675     "ChxjCookieDir",
2676     cmd_set_cookie_dir,
2677     NULL,
2678     OR_ALL,
2679     "save cookie.db directory."),
2680   AP_INIT_TAKE1(
2681     "ChxjCookieTimeout",
2682     cmd_set_cookie_timeout,
2683     NULL,
2684     OR_ALL,
2685     "The compulsion time-out time of the cookie is specified. "),
2686   AP_INIT_TAKE1(
2687     "ChxjCookieStoreType",
2688     cmd_set_cookie_store_type,
2689     NULL,
2690     OR_ALL,
2691     "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2692   AP_INIT_TAKE1(
2693     "ChxjCookieLazyMode",
2694     cmd_set_cookie_lazy_mode,
2695     NULL,
2696     OR_ALL,
2697     "OneTimeID is negligently done. (TRUE/FALSE)"),
2698 #if defined(USE_MYSQL_COOKIE)
2699   AP_INIT_TAKE1(
2700     "ChxjCookieMysqlHost",
2701     cmd_set_cookie_mysql_host,
2702     NULL,
2703     OR_ALL,
2704     "The MySQL database host used by saving Cookie"),
2705   AP_INIT_TAKE1(
2706     "ChxjCookieMysqlPort",
2707     cmd_set_cookie_mysql_port,
2708     NULL,
2709     OR_ALL,
2710     "The MySQL database port used by saving Cookie"),
2711   AP_INIT_TAKE1(
2712     "ChxjCookieMysqlDatabase",
2713     cmd_set_cookie_mysql_database,
2714     NULL,
2715     OR_ALL,
2716     "The MySQL database name used by saving Cookie"),
2717   AP_INIT_TAKE1(
2718     "ChxjCookieMysqlUsername",
2719     cmd_set_cookie_mysql_username,
2720     NULL,
2721     OR_ALL,
2722     "The MySQL username used by saving Cookie"),
2723   AP_INIT_TAKE1(
2724     "ChxjCookieMysqlPassword",
2725     cmd_set_cookie_mysql_password,
2726     NULL,
2727     OR_ALL,
2728     "The MySQL password used by saving Cookie"),
2729   AP_INIT_TAKE1(
2730     "ChxjCookieMysqlTableName",
2731     cmd_set_cookie_mysql_table_name,
2732     NULL,
2733     OR_ALL,
2734     "The MySQL table name used by saving Cookie"),
2735   AP_INIT_TAKE1(
2736     "ChxjCookieMysqlSocketPath",
2737     cmd_set_cookie_mysql_socket_path,
2738     NULL,
2739     OR_ALL,
2740     "The MySQL socket path used by saving Cookie"),
2741   AP_INIT_TAKE1(
2742     "ChxjCookieMysqlCharset",
2743     cmd_set_cookie_mysql_charset,
2744     NULL,
2745     OR_ALL,
2746     "The MySQL charset used by saving Cookie"),
2747 #endif
2748 #if defined(USE_MEMCACHE_COOKIE)
2749   AP_INIT_TAKE1(
2750     "ChxjCookieMemcacheHost",
2751     cmd_set_cookie_memcache_host,
2752     NULL,
2753     OR_ALL,
2754     "The Memcached host used by saving Cookie"),
2755   AP_INIT_TAKE1(
2756     "ChxjCookieMemcachePort",
2757     cmd_set_cookie_memcache_port,
2758     NULL,
2759     OR_ALL,
2760     "The Memcached port used by saving Cookie"),
2761 #endif
2762   AP_INIT_TAKE1(
2763     "ChxjNewLineType",
2764     cmd_set_new_line_type,
2765     NULL,
2766     OR_ALL,
2767     "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
2768   AP_INIT_TAKE1(
2769     "ChxjForwardUrlBase",
2770     cmd_set_forward_url_base,
2771     NULL,
2772     OR_ALL,
2773     "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2774   AP_INIT_TAKE1(
2775     "ChxjForwardServerIp",
2776     cmd_set_forward_server_ip,
2777     NULL,
2778     OR_ALL,
2779     "The forward server ip(default: this server ip)"),
2780   {NULL}
2781 };
2782
2783
2784 /*----------------------------------------------------------------------------*/
2785 /* Dispatch list for API hooks                                                */
2786 /*----------------------------------------------------------------------------*/
2787 module AP_MODULE_DECLARE_DATA chxj_module = {
2788   STANDARD20_MODULE_STUFF, 
2789   chxj_create_per_dir_config,          /* create per-dir    config structures */
2790   chxj_merge_per_dir_config,           /* merge  per-dir    config structures */
2791   chxj_config_server_create,           /* create per-server config structures */
2792   NULL,                                /* merge  per-server config structures */
2793   cmds,                                /* table of config file commands       */
2794   chxj_register_hooks                  /* register hooks                      */
2795 };
2796 /*
2797  * vim:ts=2 et
2798  */