OSDN Git Service

* change writting.
[modchxj/mod_chxj.git] / src / mod_chxj.c
1 /*
2  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
3  * Copyright (C) 2005 Atsushi Konno 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
20 #include "httpd.h"
21 #include "http_config.h"
22 #include "http_core.h"
23 #include "http_protocol.h"
24 #include "http_log.h"
25 #include "ap_config.h"
26 #include "apr_strings.h"
27 #include "util_filter.h"
28 #include "apr_buckets.h"
29 #include "apr_lib.h"
30 #include "apr_tables.h"
31 #include "apr_dso.h"
32 #include "apr_general.h"
33 #include "apr_pools.h"
34
35 #include "mod_chxj.h"
36 #include "chxj_encoding.h"
37 #include "qs_ignore_sp.h"
38 #include "qs_log.h"
39 #include "qs_malloc.h"
40 #include "qs_parse_attr.h"
41 #include "qs_parse_file.h"
42 #include "qs_parse_string.h"
43 #include "qs_parse_tag.h"
44 #include "chxj_load_device_data.h"
45 #include "chxj_load_emoji_data.h"
46 #include "chxj_specified_device.h"
47 #include "chxj_tag_util.h"
48 #include "chxj_xhtml_mobile_1_0.h"
49 #include "chxj_hdml.h"
50 #include "chxj_chtml10.h"
51 #include "chxj_chtml20.h"
52 #include "chxj_chtml30.h"
53 #include "chxj_jhtml.h"
54
55 #include "chxj_img_conv_format.h"
56 #include "chxj_qr_code.h"
57 #include "chxj_encoding.h"
58 #include "chxj_apply_convrule.h"
59 #include "chxj_cookie.h"
60 #include "chxj_url_encode.h"
61
62
63 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
64 #define CHXJ_VERSION        PACKAGE_VERSION
65
66 converter_t convert_routine[] = {
67   {
68     /* CHXJ_SPEC_UNKNOWN          */
69     .converter = NULL,
70     .encoder  = NULL,
71   },
72   {
73     /* CHXJ_SPEC_Chtml_1_0        */
74     .converter = chxj_exchange_chtml10,
75     .encoder  = chxj_encoding,
76   },
77   {
78     /* CHXJ_SPEC_Chtml_2_0        */
79     .converter = chxj_exchange_chtml20,
80     .encoder  = chxj_encoding,
81   },
82   {
83     /* CHXJ_SPEC_Chtml_3_0        */
84     .converter = chxj_exchange_chtml30,
85     .encoder  = chxj_encoding,
86   },
87   {
88     /* CHXJ_SPEC_Chtml_4_0        */
89     .converter = chxj_exchange_chtml30,
90     .encoder  = chxj_encoding,
91   },
92   {
93     /* CHXJ_SPEC_Chtml_5_0        */
94     .converter = chxj_exchange_chtml30,
95     .encoder  = chxj_encoding,
96   },
97   {
98     /* CHXJ_SPEC_XHtml_Mobile_1_0 */
99     .converter = chxj_exchange_xhtml_mobile_1_0,
100     .encoder  = chxj_encoding,
101   },
102   {
103     /* CHXJ_SPEC_Hdml             */
104     .converter = chxj_exchange_hdml,
105     .encoder  = chxj_encoding,
106   },
107   {
108     /* CHXJ_SPEC_Jhtml            */
109     .converter = chxj_exchange_jhtml,
110     .encoder  = chxj_encoding,
111   },
112   {
113     /* CHXJ_SPEC_HTML             */
114     .converter = NULL,
115     .encoder  = NULL,
116   },
117   {
118     NULL
119   }
120 };
121
122 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry* entryp);
123
124 /**
125  * Only when User-Agent is specified, the User-Agent header is camouflaged. 
126  *
127  * @param r   [i]
128  */
129 static apr_status_t 
130 chxj_headers_fixup(request_rec *r)
131 {
132   mod_chxj_config*    dconf; 
133   chxjconvrule_entry* entryp;
134   char*               user_agent;
135   device_table*       spec;
136
137   DBG(r, "start chxj_headers_fixup()");
138   dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
139
140   user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
141   spec = chxj_specified_device(r, user_agent);
142
143   switch(spec->html_spec_type) {
144   case CHXJ_SPEC_Chtml_1_0:
145   case CHXJ_SPEC_Chtml_2_0:
146   case CHXJ_SPEC_Chtml_3_0:
147   case CHXJ_SPEC_Chtml_4_0:
148   case CHXJ_SPEC_Chtml_5_0:
149   case CHXJ_SPEC_XHtml_Mobile_1_0:
150   case CHXJ_SPEC_Hdml:
151   case CHXJ_SPEC_Jhtml:
152     entryp = chxj_apply_convrule(r, dconf->convrules);
153     if (! entryp) {
154       DBG(r, "end chxj_headers_fixup() no pattern");
155       return DECLINED;
156     }
157   
158     apr_table_setn(r->headers_in, 
159                    CHXJ_HTTP_USER_AGENT, 
160                    user_agent);
161   
162     if (entryp->user_agent)
163       apr_table_setn(r->headers_in, 
164                      HTTP_USER_AGENT, 
165                      entryp->user_agent);
166
167     chxj_convert_input_header(r,entryp);
168
169     break;
170   
171   default:
172     break;
173
174   }
175
176   DBG(r, "end chxj_headers_fixup()");
177
178   return DECLINED;
179 }
180
181
182 /**
183  * It converts it from CHTML into XXML corresponding to each model. 
184  *
185  * @param r   [i]
186  * @param src [i]   It is former HTML character string. 
187  * @param len [i/o] It is length of former HTML character string. 
188  */
189 static char* 
190 chxj_exchange(request_rec *r, const char** src, apr_size_t* len)
191 {
192   char*               user_agent;
193   char*               dst;
194   char*               tmp;
195   char*               cookie_id;
196   mod_chxj_config*    dconf; 
197   chxjconvrule_entry* entryp;
198
199   dst  = apr_pstrcat(r->pool, (char*)*src, NULL);
200
201   dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
202
203
204   entryp = chxj_apply_convrule(r, dconf->convrules);
205
206   if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT))
207     return (char*)*src;
208
209
210   /*------------------------------------------------------------------------*/
211   /* get UserAgent from http header                                         */
212   /*------------------------------------------------------------------------*/
213   if (entryp->user_agent)
214     user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
215   else
216     user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
217
218   DBG1(r,"User-Agent:[%s]", user_agent);
219   DBG(r, "start chxj_exchange()");
220   DBG1(r,"content type is %s", r->content_type);
221
222
223   if (*(char*)r->content_type == 't' 
224   && strncmp(r->content_type, "text/html",   9) != 0) {
225     DBG1(r,"content type is %s", r->content_type);
226     return (char*)*src;
227   }
228
229   /*
230    * save cookie.
231    */
232   cookie_id = NULL;
233   if (entryp->action & CONVRULE_COOKIE_ON_BIT)
234     cookie_id = chxj_save_cookie(r);
235
236   if (!r->header_only) {
237     device_table* spec = chxj_specified_device(r, user_agent);
238
239     tmp = NULL;
240     if (convert_routine[spec->html_spec_type].encoder)
241       tmp = convert_routine[spec->html_spec_type].encoder(r, 
242                                                           *src, 
243                                                           (apr_size_t*)len);
244
245     if (convert_routine[spec->html_spec_type].converter) {
246       if (tmp)
247         dst = convert_routine[spec->html_spec_type].converter(r, 
248                                                               spec, 
249                                                               tmp, 
250                                                               *len, 
251                                                               len, 
252                                                               entryp, 
253                                                               cookie_id);
254       else
255         dst = convert_routine[spec->html_spec_type].converter(r,
256                                                               spec, 
257                                                               tmp, 
258                                                               *len, 
259                                                               len, 
260                                                               entryp, 
261                                                               cookie_id);
262     }
263   }
264
265   if (*len == 0) {
266     dst = apr_psprintf(r->pool, "\n");
267     *len = 1;
268   }
269   dst[*len] = 0;
270
271   DBG(r, "end chxj_exchange()");
272
273   return dst;
274 }
275
276
277 /**
278  * It converts it from HEADER.
279  *
280  * @param r   [i]
281  */
282 static int
283 chxj_convert_input_header(request_rec *r,chxjconvrule_entry* entryp) 
284 {
285
286   char*      buff;
287   apr_size_t urilen;
288   char*      result;
289   char*      pair;
290   char*      name;
291   char*      value;
292   char*      pstate;
293   char*      vstate;
294
295   DBG(r, "start chxj_convert_input_header()");
296
297   if (! r->args) {
298     DBG(r, "r->args=[null]");
299     DBG(r, "end   chxj_convert_input_header()");
300     return 0;
301   }
302   urilen = strlen(r->args);
303
304   result = qs_alloc_zero_byte_string(r);
305
306   buff = apr_pstrdup(r->pool, r->args);
307   DBG1(r, "r->args=[%s]", buff);
308
309   /* _chxj_dmy */
310   /* _chxj_c_ */
311   /* _chxj_r_ */
312   /* _chxj_s_ */
313   for (;;) {
314
315     pair = apr_strtok(buff, "&", &pstate);
316     if (pair == NULL)
317       break;
318
319     buff = NULL;
320
321     name  = apr_strtok(pair, "=", &vstate);
322     value = apr_strtok(NULL, "=", &vstate);
323     if (strncasecmp(name, "_chxj", 5) != 0) {
324       if (strlen(result) != 0) 
325         result = apr_pstrcat(r->pool, result, "&", NULL);
326
327       if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
328         apr_size_t dlen;
329         char* dvalue;
330
331         dlen   = strlen(value);
332         value = chxj_url_decode(r, value);
333         DBG1(r, "************ before encoding[%s]", value);
334
335         dvalue = chxj_rencoding(r, value, &dlen);
336         dvalue = chxj_url_encode(r, dvalue);
337
338         DBG1(r, "************ after encoding[%s]", dvalue);
339
340         result = apr_pstrcat(r->pool, result, name, "=", dvalue, NULL);
341       }
342       else {
343         if (strcmp(name, pair) != 0)
344           result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
345         else
346           result = apr_pstrcat(r->pool, result, name, NULL);
347       }
348     }
349     else
350     if (strncasecmp(name, "_chxj_c_", 8) == 0 
351     ||  strncasecmp(name, "_chxj_r_", 8) == 0
352     ||  strncasecmp(name, "_chxj_s_", 8) == 0) {
353       if (value == NULL)
354         continue;
355
356       if (strlen(value) == 0)
357         continue;
358
359       if (strlen(result) != 0)
360         result = apr_pstrcat(r->pool, result, "&", NULL);
361
362       result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
363     }
364     else
365     if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0) {
366       DBG1(r, "found cookie parameter[%s]", value);
367       chxj_load_cookie(r, value);
368     }
369   }
370   r->args = result;
371
372   DBG1(r, "result r->args=[%s]", r->args);
373   DBG(r, "end   chxj_convert_input_header()");
374   return 0;
375 }
376
377
378 /**
379  * It converts it from POSTDATA .
380  *
381  * @param r   [i]
382  * @param src [i]   It is POSTDATA character string.
383  * @param len [i/o] It is length of former HTML character string.
384  */
385 static char*
386 chxj_input_convert(
387   request_rec*        r, 
388   const char**        src, 
389   apr_size_t*         len, 
390   chxjconvrule_entry* entryp)
391 {
392   char* pair;
393   char* name;
394   char* value;
395   char* pstate;
396   char* vstate;
397   char* s;
398   char* result;
399
400   s = apr_pstrdup(r->pool, *src);
401
402   result = qs_alloc_zero_byte_string(r);
403
404   DBG1(r, "BEFORE input convert source = [%s]", s);
405
406   /* _chxj_dmy */
407   /* _chxj_c_ */
408   /* _chxj_r_ */
409   /* _chxj_s_ */
410   for (;;) {
411     pair = apr_strtok(s, "&", &pstate);
412     if (pair == NULL)
413       break;
414     s = NULL;
415
416     name  = apr_strtok(pair, "=", &vstate);
417     value = apr_strtok(NULL, "=", &vstate);
418     if (strncasecmp(name, "_chxj", 5) != 0) {
419       if (strlen(result) != 0) 
420         result = apr_pstrcat(r->pool, result, "&", NULL);
421
422       if (strcasecmp(entryp->encoding, "NONE") != 0 
423       &&  value && strlen(value)) {
424         apr_size_t dlen;
425         char*      dvalue;
426
427         dlen   = strlen(value);
428         value = chxj_url_decode(r, value);
429         DBG1(r, "************ before encoding[%s]", value);
430
431         dvalue = chxj_rencoding(r, value, &dlen);
432         dvalue = chxj_url_encode(r,dvalue);
433
434         DBG1(r, "************ after encoding[%s]", dvalue);
435
436         result = apr_pstrcat(r->pool, result, name, "=", dvalue, NULL);
437
438       }
439       else {
440         result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
441       }
442     }
443     else
444     if (strncasecmp(name, "_chxj_c_", 8) == 0 
445     ||  strncasecmp(name, "_chxj_r_", 8) == 0
446     ||  strncasecmp(name, "_chxj_s_", 8) == 0) {
447       if (value == NULL)
448         continue;
449
450       if (strlen(value) == 0)
451         continue;
452
453       if (strlen(result) != 0)
454         result = apr_pstrcat(r->pool, result, "&", NULL);
455
456       if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
457         apr_size_t dlen;
458         char*      dvalue;
459
460         dlen   = strlen(value);
461         value = chxj_url_decode(r, value);
462         DBG1(r, "************ before encoding[%s]", value);
463
464         dvalue = chxj_rencoding(r, value, &dlen);
465         dvalue = chxj_url_encode(r,dvalue);
466
467         DBG1(r, "************ after encoding[%s]", dvalue);
468
469         result = apr_pstrcat(r->pool, result, &name[8], "=", dvalue, NULL);
470
471       }
472       else {
473         result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
474       }
475     }
476     else
477     if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0) {
478       DBG1(r, "found cookie parameter[%s]", value);
479       chxj_load_cookie(r, value);
480     }
481   }
482   *len = strlen(result);
483
484   DBG1(r, "AFTER input convert result = [%s]", result);
485
486   return result;
487 }
488
489
490 /**
491  * The received data is returned to the filter.
492  *
493  * @param f    [i/o] It is a filter. 
494  * @param data [i]   It is data returned to the filter. 
495  * @param len  [i]   It is length of the data returned to the filter. 
496  */
497 static apr_status_t 
498 pass_data_to_filter(ap_filter_t *f, const char *data, 
499                                         apr_size_t len)
500 {
501   request_rec*        r = f->r;
502   conn_rec*           c = r->connection;
503   apr_status_t        rv;
504   apr_bucket_brigade* bb;
505   apr_bucket*         b;
506
507   DBG(r, "start pass_data_to_filter()");
508
509   bb = apr_brigade_create(r->pool, c->bucket_alloc);
510   b  = apr_bucket_transient_create(data, len, c->bucket_alloc);
511
512   APR_BRIGADE_INSERT_TAIL(bb, b);
513   b = apr_bucket_eos_create(f->c->bucket_alloc);
514   APR_BRIGADE_INSERT_TAIL(bb, b);
515
516   rv = ap_pass_brigade(f->next, bb);
517   if (rv != APR_SUCCESS) {
518     DBG(r, "ap_pass_brigade()");
519     return rv;
520   }
521
522   DBG(r, "end pass_data_to_filter()");
523
524   return rv;
525 }
526
527
528 /**
529  * It is the main loop of the output filter. 
530  *
531  * @param f   [i/o] It is a filter.
532  * @param bb  [i]   
533  */
534 static apr_status_t 
535 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
536 {
537   request_rec*        r;
538   apr_status_t        rv;
539   apr_bucket*         b;
540   const char*         data;
541   char*               contentLength;
542   apr_size_t          len;
543   mod_chxj_ctx*       ctx;
544   char*               cookie_id;
545   char*               location_header;
546   mod_chxj_config*    dconf;
547   chxjconvrule_entry* entryp;
548
549
550
551   DBG(r, "start of chxj_output_filter()");
552
553   r  = f->r;
554   rv = APR_SUCCESS;
555
556   if (!f->ctx) {
557     if ((f->r->proto_num >= 1001) 
558     &&  !f->r->main 
559     &&  !f->r->prev) 
560       f->r->chunked = 1;
561   }
562
563   dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
564   entryp = chxj_apply_convrule(r, dconf->convrules);
565
566
567   for (b = APR_BRIGADE_FIRST(bb);
568        b != APR_BRIGADE_SENTINEL(bb); 
569        b = APR_BUCKET_NEXT(b)) {
570     if (APR_BUCKET_IS_EOS(b)) {
571       DBG(r, "eos");
572       /*----------------------------------------------------------------------*/
573       /* End Of File                                                          */
574       /*----------------------------------------------------------------------*/
575       if (f->ctx) {
576         ctx = (mod_chxj_ctx*)f->ctx;
577         DBG1(r, "content_type=[%s]", r->content_type);
578         if (r->content_type 
579         && *(char*)r->content_type == 't' 
580         && strncmp(r->content_type, "text/html",   9) == 0) {
581           if (ctx->len) {
582             char* tmp = apr_palloc(r->pool, ctx->len + 1);
583             memset(tmp, 0, ctx->len + 1);
584             memcpy(tmp, ctx->buffer, ctx->len);
585
586             DBG2(r, "input data=[%s] len=[%d]", tmp, ctx->len);
587
588             ctx->buffer = chxj_exchange(r, (const char**)&tmp, (apr_size_t*)&ctx->len);
589
590             DBG2(r, "output data=[%.*s]", ctx->len,ctx->buffer);
591           }
592           else {
593             ctx->buffer = apr_psprintf(r->pool, "\n");
594             ctx->len += 1;
595             ctx->buffer = chxj_exchange(r, 
596                                         (const char**)&ctx->buffer, 
597                                         (apr_size_t*)&ctx->len);
598
599           }
600         }
601
602         if (r->content_type 
603         && *(char*)r->content_type == 'i' 
604         && strncmp(r->content_type, "image/", 6) == 0) {
605           if (ctx->len) {
606             char* tmp = apr_palloc(r->pool, ctx->len + 1);
607             memset(tmp, 0, ctx->len + 1);
608             memcpy(tmp, ctx->buffer, ctx->len);
609
610             DBG1(r, "input data=[%s]", tmp);
611
612             ctx->buffer = 
613               chxj_exchange_image(r, (const char**)&tmp,(apr_size_t*)&ctx->len);
614             if (ctx->buffer == NULL) {
615               ctx->buffer = tmp;
616             }
617
618             DBG2(r, "output data=[%.*s]", ctx->len,ctx->buffer);
619           }
620         }
621
622         contentLength = apr_psprintf(r->pool, "%d", ctx->len);
623         apr_table_setn(r->headers_out, "Content-Length", contentLength);
624         
625         if (ctx->len > 0) {
626           rv = pass_data_to_filter(f, 
627                                    (const char*)ctx->buffer, 
628                                    (apr_size_t)ctx->len);
629         }
630         f->ctx = NULL;
631         DBG(r, " ");
632         return rv;
633       }
634       else {
635         DBG1(r, " SAVE COOKIE[%x]", entryp->action);
636         /*
637          * save cookie.
638          */
639         if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
640           DBG(r, "entryp->action == COOKIE_ON_BIT");
641
642           cookie_id = chxj_save_cookie(r);
643
644           /*
645            * Location Header Check to add cookie parameter.
646            */
647           location_header = (char*)apr_table_get(r->headers_out, "Location");
648           if (location_header) {
649             DBG1(r, "Location Header=[%s]", location_header);
650             location_header = chxj_add_cookie_parameter(r,
651                                                         location_header,
652                                                         cookie_id);
653             apr_table_setn(r->headers_out, "Location", location_header);
654             DBG1(r, "Location Header=[%s]", location_header);
655           }
656         }
657
658         apr_table_setn(r->headers_out, "Content-Length", "0");
659         rv = pass_data_to_filter(f, (const char*)"", (apr_size_t)0);
660         DBG(r, " ");
661         return rv;
662       }
663     }
664     else
665     if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
666       DBG2(r, "read data[%.*s]",len, data);
667
668       if (f->ctx == NULL) {
669         /*--------------------------------------------------------------------*/
670         /* Start                                                              */
671         /*--------------------------------------------------------------------*/
672         DBG(r, "new context");
673         ctx = (mod_chxj_ctx*)apr_palloc(r->pool, sizeof(mod_chxj_ctx));
674         if (len > 0) {
675           ctx->buffer = apr_palloc(r->pool, len);
676           memcpy(ctx->buffer, data, len);
677         }
678         else {
679           ctx->buffer = apr_palloc(r->pool, 1);
680           ctx->buffer = '\0';
681         }
682         ctx->len = len;
683         f->ctx = (void*)ctx;
684       }
685       else {
686         /*--------------------------------------------------------------------*/
687         /* append data                                                        */
688         /*--------------------------------------------------------------------*/
689         char* tmp;
690         DBG(r, "append data start");
691         ctx = (mod_chxj_ctx*)f->ctx;
692
693         if (len > 0) {
694           tmp = apr_palloc(r->pool, ctx->len);
695           memcpy(tmp, ctx->buffer, ctx->len);
696
697           ctx->buffer = apr_palloc(r->pool, ctx->len + len);
698
699           memcpy(ctx->buffer, tmp, ctx->len);
700           memcpy(&ctx->buffer[ctx->len], data, len);
701
702           ctx->len += len;
703         }
704         DBG(r, "append data end");
705       }
706     }
707   }
708   apr_brigade_destroy(bb);
709
710   DBG(r, "end of output filter");
711
712   return APR_SUCCESS;
713 }
714
715 /**
716  * It is the main loop of the input filter. 
717  *
718  * @param f         [i/o] It is a filter.
719  * @param bb        [i]   brigade
720  * @param mode      [i]   mode
721  * @param block     [i]   block
722  * @param readbytes [i]   readbyte
723  */
724 static apr_status_t 
725 chxj_input_filter(ap_filter_t*        f, 
726                  apr_bucket_brigade*  bb,
727                  ap_input_mode_t      mode, 
728                  apr_read_type_e      block,
729                  apr_off_t            readbytes)
730 {
731   request_rec*        r = f->r;
732   apr_status_t        rv;
733   conn_rec*           c = r->connection;
734   apr_bucket*         b;
735   /*--------------------------------------------------------------------------*/
736   /* It is the brigade area for output                                        */
737   /*--------------------------------------------------------------------------*/
738   apr_bucket_brigade* ibb;            
739   /*--------------------------------------------------------------------------*/
740   /* It is the brigade area for input                                         */
741   /*--------------------------------------------------------------------------*/
742   apr_bucket_brigade* obb;
743   apr_size_t          len;
744   apr_bucket*         tmp_heap;
745   apr_bucket*         eos;
746   const char*         data;
747   char*               data_bucket;
748   char*               data_brigade;
749   char*               content_type;
750   device_table*       spec ;
751   char*               user_agent;
752   mod_chxj_config*    dconf;
753   chxjconvrule_entry* entryp;
754
755   DBG(r, "start of chxj_input_filter()");
756
757   data_brigade = qs_alloc_zero_byte_string(r);
758
759
760   ibb = apr_brigade_create(r->pool, c->bucket_alloc);
761   obb = apr_brigade_create(r->pool, c->bucket_alloc);
762
763   content_type = (char*)apr_table_get(r->headers_in, "Content-Type");
764   if (content_type 
765   && strncasecmp("multipart/form-data", content_type, 19) == 0) {
766
767     DBG(r, "detect multipart/form-data");
768     ap_remove_input_filter(f);
769
770     return ap_get_brigade(f->next, bb, mode, block, readbytes);
771   }
772
773   dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
774
775   entryp = chxj_apply_convrule(r, dconf->convrules);
776   if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
777     DBG(r,"EngineOff");
778
779     ap_remove_input_filter(f);
780     return ap_get_brigade(f->next, bb, mode, block, readbytes);
781   }
782
783   user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
784   spec = chxj_specified_device(r, user_agent);
785
786   switch(spec->html_spec_type) {
787   case CHXJ_SPEC_Chtml_1_0:
788   case CHXJ_SPEC_Chtml_2_0:
789   case CHXJ_SPEC_Chtml_3_0:
790   case CHXJ_SPEC_Chtml_4_0:
791   case CHXJ_SPEC_Chtml_5_0:
792   case CHXJ_SPEC_XHtml_Mobile_1_0:
793   case CHXJ_SPEC_Hdml:
794   case CHXJ_SPEC_Jhtml:
795     break;
796
797   default:
798     ap_remove_input_filter(f);
799     return ap_get_brigade(f->next, bb, mode, block, readbytes);
800   }
801
802
803   rv = ap_get_brigade(f->next, ibb, mode, block, readbytes);
804   if (rv != APR_SUCCESS) {
805     DBG(r, "ap_get_brigade() failed");
806     return rv;
807   }
808
809   APR_BRIGADE_FOREACH(b, ibb) {
810     rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
811     if (rv != APR_SUCCESS) {
812       DBG(r, "apr_bucket_read() failed");
813       return rv;
814     }
815
816     if (data != NULL) {
817       data_bucket = apr_palloc(r->pool, len+1);
818       memset((void*)data_bucket, 0, len+1);
819       memcpy(data_bucket, data, len);
820       DBG1(r, "(in)POSTDATA:[%s]", data_bucket);
821   
822       data_brigade = apr_pstrcat(r->pool, data_brigade, data_bucket, NULL);
823     }
824
825     if (APR_BUCKET_IS_EOS(b)) {
826       break;
827     }
828   }
829   apr_brigade_cleanup(ibb);
830
831
832   len = strlen(data_brigade);
833   if (len == 0) {
834     DBG(r,"data_brigade length is 0");
835     DBG(r,"end of chxj_input_filter()");
836     ap_remove_input_filter(f);
837     return ap_get_brigade(f->next, bb, mode, block, readbytes);
838   }
839   data_brigade = chxj_input_convert(
840     r, 
841     (const char**)&data_brigade, 
842     (apr_size_t*)&len,
843     entryp
844     );
845
846   if (len > 0) {
847     DBG1(r, "(in:exchange)POSTDATA:[%s]", data_brigade);
848
849     obb = apr_brigade_create(r->pool, c->bucket_alloc);
850
851     tmp_heap = apr_bucket_heap_create(data_brigade, 
852                                       len, 
853                                       NULL, 
854                                       f->c->bucket_alloc);
855     eos      = apr_bucket_eos_create(f->c->bucket_alloc);
856
857     APR_BRIGADE_INSERT_TAIL(obb, tmp_heap);
858     APR_BRIGADE_INSERT_TAIL(obb, eos);
859     APR_BRIGADE_CONCAT(bb, obb);
860   }
861
862   DBG(r, "end of chxj_input_filter()");
863
864   return APR_SUCCESS;
865 }
866
867
868 static mod_chxj_global_config*
869 chxj_global_config_create(apr_pool_t* pool, server_rec* s)
870 {
871   mod_chxj_global_config* conf;
872
873   SDBG(s, "start chxj_global_config_create()");
874
875   /*--------------------------------------------------------------------------*/
876   /* allocate an own subpool which survives server restarts                   */
877   /*--------------------------------------------------------------------------*/
878   conf = (mod_chxj_global_config*)apr_palloc(pool, 
879                   sizeof(mod_chxj_global_config));
880 #if 0
881   conf->cookie_db_lock = NULL;
882 #endif
883   SDBG(s, "end   chxj_global_config_create()");
884
885   return conf;
886 }
887
888 /**
889  * initialize chxj module
890  */
891 static int 
892 chxj_init_module(apr_pool_t *p, 
893                   apr_pool_t *plog, 
894                   apr_pool_t *ptemp, 
895                   server_rec *s)
896 {
897   mod_chxj_global_config* conf;
898   void *user_data;
899
900   SDBG(s, "start chxj_init_module()");
901
902   apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
903   SDBG(s, " ");
904   if (user_data == NULL) {
905     SDBG(s, " ");
906     /*
907      * dummy user_data set.
908      */
909     apr_pool_userdata_set(
910       (const void *)(1), 
911       CHXJ_MOD_CONFIG_KEY, 
912       apr_pool_cleanup_null, 
913       s->process->pool);
914     SDBG(s, "end  chxj_init_module()");
915     return OK;
916   }
917
918   ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
919
920
921 #if 0
922   conf = (mod_chxj_global_config *)ap_get_module_config(s->module_config, 
923                                                         &chxj_module);
924
925   if (apr_global_mutex_create(&(conf->cookie_db_lock), 
926                               NULL, APR_LOCK_DEFAULT, p) != APR_SUCCESS) {
927     SERR(s, "end  chxj_init_module()");
928     return HTTP_INTERNAL_SERVER_ERROR;
929   }
930
931 #ifdef AP_NEED_SET_MUTEX_PERMS
932   if (unixd_set_global_mutex_perms(conf->cookie_db_lock) != APR_SUCCESS) {
933     SERR(s, "end  chxj_init_module()");
934     return HTTP_INTERNAL_SERVER_ERROR;
935   }
936 #endif
937 #endif
938
939
940   SDBG(s, "end  chxj_init_module()");
941
942   return OK;
943 }
944
945 static void 
946 chxj_child_init(apr_pool_t *p, server_rec *s)
947 {
948 #if 0
949   mod_chxj_global_config* conf;
950 #endif
951
952   SDBG(s, "start chxj_child_init()");
953
954 #if 0
955   conf = (mod_chxj_global_config*)ap_get_module_config(s->module_config, 
956                                                        &chxj_module);
957
958   if (apr_global_mutex_child_init(&conf->cookie_db_lock, NULL, p) 
959   != APR_SUCCESS) {
960     SERR(s, "Can't attach global mutex.");
961     return;
962   }
963 #endif
964
965   SDBG(s, "end   chxj_child_init()");
966 }
967
968
969 /**
970  * A set structure of each server is generated. 
971  * 
972  * @param p
973  * @param s
974  */
975 static void*
976 chxj_config_server_create(apr_pool_t *p, server_rec *s)
977 {
978   mod_chxj_global_config *gc;
979
980   gc = chxj_global_config_create(p,s);
981
982   return gc;
983 }
984
985
986 static int
987 chxj_translate_name(request_rec *r)
988 {
989   return chxj_trans_name(r);
990 }
991
992 /**
993  * The hook is registered.
994  *
995  * @param p
996  */
997 static void 
998 chxj_register_hooks(apr_pool_t *p)
999 {
1000   ap_hook_post_config(chxj_init_module,
1001                       NULL,
1002                       NULL,
1003                       APR_HOOK_REALLY_FIRST);
1004   ap_hook_child_init(chxj_child_init, 
1005                      NULL, 
1006                      NULL, 
1007                      APR_HOOK_REALLY_FIRST);
1008   ap_register_output_filter (
1009                       "chxj_output_filter", 
1010                       chxj_output_filter, 
1011                       NULL, 
1012                       AP_FTYPE_RESOURCE);
1013   ap_register_input_filter(
1014                       "chxj_input_filter", 
1015                       chxj_input_filter, 
1016                       NULL, 
1017                       AP_FTYPE_RESOURCE);
1018   ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1019   ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1020   ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1021   ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_LAST);
1022 }
1023
1024 /**
1025  * A set structure according to the directory is generated. 
1026  *
1027  * @param p
1028  * @param arg
1029  */
1030 static void* 
1031 chxj_create_per_dir_config(apr_pool_t *p, char *arg) 
1032 {
1033   mod_chxj_config* conf;
1034
1035   conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1036   conf->device_data_file = NULL;
1037   conf->devices          = NULL;
1038   conf->emoji_data_file  = NULL;
1039   conf->emoji            = NULL;
1040   conf->emoji_tail       = NULL;
1041   conf->image            = CHXJ_IMG_OFF;
1042   conf->image_cache_dir  = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1043   conf->server_side_encoding = NULL;
1044   if (arg == NULL) {
1045     conf->dir                  = NULL;
1046   }
1047   else {
1048     conf->dir                  = apr_pcalloc(p, strlen(arg)+1);
1049     memset(conf->dir, 0, strlen(arg)+1);
1050     strcpy(conf->dir, arg);
1051   }
1052   conf->convrules   = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1053
1054   /* Default is copyleft */
1055   conf->image_copyright = NULL; 
1056
1057   return conf;
1058 }
1059 /*
1060  *  Merge per-directory CHXJ configurations
1061  */
1062 static void*
1063 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1064 {
1065   mod_chxj_config *base = (mod_chxj_config*)basev;
1066   mod_chxj_config *add  = (mod_chxj_config*)addv;
1067   mod_chxj_config *mrg  = (mod_chxj_config*)apr_palloc(p, sizeof(mod_chxj_config));
1068
1069   mrg->device_data_file = NULL;
1070   mrg->devices          = NULL;
1071   mrg->emoji_data_file  = NULL;
1072   mrg->image            = CHXJ_IMG_OFF;
1073   mrg->image_cache_dir  = NULL;
1074   mrg->image_copyright  = NULL;
1075   mrg->emoji            = NULL;
1076   mrg->emoji_tail       = NULL;
1077
1078   mrg->dir = apr_pstrdup(p, add->dir);
1079
1080   if (! add->device_data_file) {
1081     mrg->devices = base->devices;
1082     mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1083   }
1084   else {
1085     mrg->devices = add->devices;
1086     mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1087   }
1088
1089   if (! add->emoji_data_file) {
1090     mrg->emoji = base->emoji;
1091     mrg->emoji_tail = base->emoji_tail;
1092     mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1093   }
1094   else {
1095     mrg->emoji = add->emoji;
1096     mrg->emoji_tail = add->emoji_tail;
1097     mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1098   }
1099
1100   if (add->image == CHXJ_IMG_OFF) 
1101     mrg->image = base->image;
1102   else 
1103     mrg->image = add->image;
1104
1105
1106   if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) 
1107     mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1108   else 
1109     mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1110
1111   if (add->image_copyright) 
1112     mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1113   else
1114     mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1115
1116   if (add->server_side_encoding) {
1117     mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1118   }
1119   else 
1120   if (base->server_side_encoding) {
1121     mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1122   }
1123   else {
1124     mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1125   }
1126
1127   mrg->convrules    = apr_array_append(p, add->convrules, base->convrules);
1128
1129   return mrg;
1130 }
1131
1132
1133 static int
1134 chxj_command_parse_take5(
1135   const char* arg, 
1136   char** prm1, 
1137   char** prm2, 
1138   char** prm3, 
1139   char** prm4, 
1140   char** prm5)
1141 {
1142   int isquoted;
1143   char* strp;
1144
1145   strp = (char*)arg;
1146
1147   for (;*strp == ' '||*strp == '\t'; strp++) ;
1148
1149   isquoted = 0; 
1150   if (*strp == '"') { 
1151     isquoted = 1;
1152     strp++;
1153   }
1154
1155   *prm1 = strp;
1156
1157   for (; *strp != '\0'; strp++) {
1158     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1159     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1160       strp++;
1161       continue;
1162     }
1163
1164     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1165     ||  (isquoted  && *strp == '"'))
1166       break;
1167   }
1168
1169   if (! *strp) {
1170     *prm2 = strp;
1171     *prm3 = strp;
1172     *prm4 = strp;
1173     *prm5 = strp;
1174     return 1;
1175   }
1176
1177   *strp++ = '\0';
1178
1179   for (;*strp == ' '||*strp == '\t'; strp++) ;
1180
1181   isquoted = 0; 
1182   if (*strp == '"') { 
1183     isquoted = 1;
1184     strp++;
1185   }
1186
1187   *prm2 = strp;
1188   for (; *strp != '\0'; strp++) {
1189     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1190     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1191       strp++;
1192       continue;
1193     }
1194
1195     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1196     ||  (isquoted  && *strp == '"'))
1197       break;
1198   }
1199
1200   if (! *strp) {
1201     *prm3 = strp;
1202     *prm4 = strp;
1203     *prm5 = strp;
1204     return 0;
1205   }
1206
1207   *strp++ = '\0';
1208
1209   for (;*strp == ' '||*strp == '\t'; strp++);
1210
1211   isquoted = 0; 
1212   if (*strp == '"') { 
1213     isquoted = 1;
1214     strp++;
1215   }
1216   *prm3 = strp;
1217   for (; *strp != '\0'; strp++) {
1218     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1219     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1220       strp++;
1221       continue;
1222     }
1223
1224     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1225     ||  (isquoted  && *strp == '"'))
1226       break;
1227   }
1228
1229   if (! *strp) {
1230     *prm4 = strp;
1231     *prm5 = strp;
1232     return 0;
1233   }
1234
1235   *strp++ = '\0';
1236
1237   for (;*strp == ' '||*strp == '\t'; strp++);
1238
1239   isquoted = 0; 
1240   if (*strp == '"') { 
1241     isquoted = 1;
1242     strp++;
1243   }
1244   *prm4 = strp;
1245   for (; *strp != '\0'; strp++) {
1246     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1247     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1248       strp++;
1249       continue;
1250     }
1251
1252     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1253     ||  (isquoted  && *strp == '"'))
1254       break;
1255   }
1256
1257   if (! *strp) {
1258     *prm5 = strp;
1259     return 0;
1260   }
1261
1262   *strp++ = '\0';
1263
1264   for (;*strp == ' '||*strp == '\t'; strp++);
1265
1266   isquoted = 0; 
1267   if (*strp == '"') { 
1268     isquoted = 1;
1269     strp++;
1270   }
1271   *prm5 = strp;
1272   for (; *strp != '\0'; strp++) {
1273     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1274     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1275       strp++;
1276       continue;
1277     }
1278
1279     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1280     ||  (isquoted  && *strp == '"'))
1281       break;
1282   }
1283   *strp = '\0';
1284
1285   return 0;
1286 }
1287
1288 /**
1289  * The device definition file is loaded. 
1290  *
1291  * @param arg     [i]   The name of the device definition file is specified.
1292  * @param mconfig [i/o] The pointer to a set structure is specified. 
1293  * @param parms   [i]   
1294  */
1295 static const char* 
1296 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char* arg) 
1297 {
1298   mod_chxj_config* conf;
1299   Doc doc;
1300   doc.r = NULL;
1301
1302   if (strlen(arg) > 256) 
1303     return "device data filename too long.";
1304
1305   conf = (mod_chxj_config*)mconfig;
1306   conf->device_data_file = apr_pstrdup(parms->pool, arg);
1307
1308   qs_init_malloc(&doc);
1309   qs_init_root_node(&doc);
1310
1311   qs_parse_file((Doc*)&doc, (const char*)arg);
1312   chxj_load_device_data(&doc,parms->pool, conf);
1313   qs_all_free(&doc, QX_LOGMARK);
1314
1315   return NULL;
1316 }
1317
1318 /**
1319  * Device definition information is loaded. 
1320  *
1321  * @param parms 
1322  * @param arg     [i]   The name of the device definition file is specified. 
1323  * @param mconfig [i/o] The pointer to a set structure is specified. 
1324  * @return 
1325  */
1326 static const char* 
1327 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char* arg) 
1328 {
1329   mod_chxj_config* conf;
1330   char* rtn;
1331   Doc doc;
1332   doc.r = NULL;
1333
1334
1335   if (strlen(arg) > 256) 
1336     return "emoji data filename too long.";
1337
1338   conf = (mod_chxj_config*)mconfig;
1339   conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
1340   qs_init_malloc(&doc);
1341   qs_init_root_node(&doc);
1342
1343   qs_parse_file((Doc*)&doc, (const char*)arg);
1344   rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
1345   qs_all_free(&doc, QX_LOGMARK);
1346
1347
1348   return rtn;
1349 }
1350
1351 static const char* 
1352 cmd_set_image_engine(cmd_parms *parms, void *mconfig, const char* arg) 
1353 {
1354   mod_chxj_config* conf;
1355   Doc doc;
1356
1357
1358   doc.r = NULL;
1359
1360   if (strlen(arg) > 256) 
1361     return "image uri is too long.";
1362
1363   conf = (mod_chxj_config*)mconfig;
1364   if (strcasecmp("ON", arg) == 0)
1365     conf->image = CHXJ_IMG_ON;
1366   else
1367     conf->image = CHXJ_IMG_OFF;
1368
1369
1370   return NULL;
1371 }
1372
1373 static const char* 
1374 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char* arg) 
1375 {
1376   mod_chxj_config* conf;
1377   Doc doc;
1378   doc.r = NULL;
1379
1380   if (strlen(arg) > 256) 
1381     return "cache dir name is too long.";
1382
1383   conf = (mod_chxj_config*)mconfig;
1384   conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
1385
1386   return NULL;
1387 }
1388
1389 static const char* 
1390 cmd_set_image_copyright(cmd_parms *parms, void* mconfig, const char* arg) 
1391 {
1392   mod_chxj_config* conf;
1393   Doc doc;
1394
1395   doc.r = NULL;
1396   if (strlen(arg) > 256) 
1397     return "Copyright Flag is too long.";
1398
1399   conf = (mod_chxj_config*)mconfig;
1400   conf->image_copyright = apr_pstrdup(parms->pool, arg);
1401
1402   return NULL;
1403 }
1404
1405 static const char*
1406 cmd_convert_rule(cmd_parms *cmd, void* mconfig, const char *arg)
1407 {
1408   mod_chxj_config* dconf;
1409   char* prm1;
1410   char* prm2;
1411   char* prm3;
1412   char* prm4;
1413   char* prm5;
1414   int mode;
1415   char* pstate;
1416   char* action;
1417   char* pp;
1418   ap_regex_t *regexp;
1419   chxjconvrule_entry* newrule;
1420
1421   dconf = (mod_chxj_config*)mconfig;
1422
1423   if (strlen(arg) > 4096) 
1424     return "ChxjConvertRule: is too long.";
1425
1426   dconf = (mod_chxj_config*)mconfig;
1427   if (dconf->convrules == NULL)
1428     dconf->convrules   = apr_array_make(cmd->pool, 2, sizeof(chxjconvrule_entry));
1429
1430
1431   newrule = apr_array_push(dconf->convrules);
1432
1433   newrule->flags  = 0;
1434   newrule->action = 0;
1435
1436   if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
1437     return "ChxjConvertRule: bad argument line";
1438
1439   newrule->pattern = apr_pstrdup(cmd->pool, prm1);
1440
1441   /* Parse action */
1442   for (;;) {
1443     if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
1444       break;
1445     prm2 = NULL;
1446     switch(*action) {
1447     case 'e':
1448     case 'E':
1449       if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
1450         newrule->action |= CONVRULE_ENGINE_ON_BIT;
1451       }
1452       else
1453       if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
1454         newrule->action |= CONVRULE_ENGINE_OFF_BIT;
1455       }
1456       break;
1457
1458     case 'C':
1459     case 'c':
1460       if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
1461         newrule->action |= CONVRULE_COOKIE_ON_BIT;
1462       }
1463       break;
1464
1465     default:
1466       break;
1467     }
1468   }
1469   
1470   pp = prm1;
1471   if (*pp == '!') {
1472     newrule->flags |= CONVRULE_FLAG_NOTMATCH;
1473     pp++;
1474   }
1475
1476   mode = AP_REG_EXTENDED;
1477   if ((regexp = ap_pregcomp(cmd->pool, pp, mode)) == NULL) {
1478     return "RewriteRule: cannot compile regular expression ";
1479   }
1480
1481   newrule->regexp = regexp;
1482   if (*prm3)
1483     newrule->encoding = apr_pstrdup(cmd->pool, prm3);
1484   else
1485     newrule->encoding = apr_pstrdup(cmd->pool, "none");
1486
1487   newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
1488   if (*prm4)
1489     if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
1490       newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
1491
1492   newrule->user_agent = NULL;
1493   if (*prm5)
1494     newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
1495     
1496   return NULL;
1497 }
1498
1499
1500 static const command_rec cmds[] = {
1501   AP_INIT_TAKE1(
1502     "ChxjLoadDeviceData",
1503     cmd_load_device_data,
1504     NULL,
1505     OR_ALL,
1506     "Load Device Data"),
1507   AP_INIT_TAKE1(
1508     "ChxjLoadEmojiData",
1509     cmd_load_emoji_data,
1510     NULL,
1511     OR_ALL,
1512     "Load Emoji Data"),
1513   AP_INIT_TAKE1(
1514     "ChxjImageEngine",
1515     cmd_set_image_engine,
1516     NULL,
1517     OR_ALL,
1518     "Convert Target URI"),
1519   AP_INIT_TAKE1(
1520     "ChxjImageCacheDir",
1521     cmd_set_image_cache_dir,
1522     NULL,
1523     OR_ALL,
1524     "Image Cache Directory"),
1525   AP_INIT_TAKE1(
1526     "ChxjImageCopyright",
1527     cmd_set_image_copyright,
1528     NULL,
1529     OR_ALL,
1530     "Copyright Flag"),
1531   AP_INIT_RAW_ARGS(
1532     "ChxjConvertRule",
1533     cmd_convert_rule,
1534     NULL, 
1535     OR_FILEINFO,
1536     "an URL-applied regexp-pattern and a substitution URL"),
1537   { NULL },
1538 };
1539
1540
1541 /*----------------------------------------------------------------------------*/
1542 /* Dispatch list for API hooks                                                */
1543 /*----------------------------------------------------------------------------*/
1544 module AP_MODULE_DECLARE_DATA chxj_module = 
1545 {
1546   STANDARD20_MODULE_STUFF, 
1547   chxj_create_per_dir_config,          /* create per-dir    config structures */
1548   chxj_merge_per_dir_config,           /* merge  per-dir    config structures */
1549   chxj_config_server_create,           /* create per-server config structures */
1550   NULL,                                /* merge  per-server config structures */
1551   cmds,                                /* table of config file commands       */
1552   chxj_register_hooks                  /* register hooks                      */
1553 };
1554 /*
1555  * vim:ts=2 et
1556  */