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(f->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
571     if (APR_BUCKET_IS_EOS(b)) {
572
573       DBG(r, "eos");
574       /*----------------------------------------------------------------------*/
575       /* End Of File                                                          */
576       /*----------------------------------------------------------------------*/
577       if (f->ctx) {
578
579         ctx = (mod_chxj_ctx*)f->ctx;
580
581         DBG1(r, "content_type=[%s]", r->content_type);
582
583         if (r->content_type 
584         && *(char*)r->content_type == 't' 
585         && strncmp(r->content_type, "text/html",   9) == 0) {
586
587           if (ctx->len) {
588             char* tmp;
589
590             tmp = apr_palloc(r->pool, ctx->len + 1);
591
592             memset(tmp, 0, ctx->len + 1);
593             memcpy(tmp, ctx->buffer, ctx->len);
594
595             DBG2(r, "input data=[%s] len=[%d]", tmp, ctx->len);
596
597             ctx->buffer = chxj_exchange(r, (const char**)&tmp, (apr_size_t*)&ctx->len);
598
599             DBG2(r, "output data=[%.*s]", ctx->len,ctx->buffer);
600           }
601           else {
602             ctx->buffer = apr_psprintf(r->pool, "\n");
603             ctx->len += 1;
604             ctx->buffer = chxj_exchange(r, 
605                                         (const char**)&ctx->buffer, 
606                                         (apr_size_t*)&ctx->len);
607
608           }
609         }
610
611         if (r->content_type 
612         && *(char*)r->content_type == 'i' 
613         && strncmp(r->content_type, "image/", 6) == 0) {
614           if (ctx->len) {
615             char* tmp = apr_palloc(r->pool, ctx->len + 1);
616             memset(tmp, 0, ctx->len + 1);
617             memcpy(tmp, ctx->buffer, ctx->len);
618
619             DBG1(r, "input data=[%s]", tmp);
620
621             ctx->buffer = 
622               chxj_exchange_image(r, (const char**)&tmp,(apr_size_t*)&ctx->len);
623             if (ctx->buffer == NULL) {
624               ctx->buffer = tmp;
625             }
626
627             DBG2(r, "output data=[%.*s]", ctx->len,ctx->buffer);
628           }
629         }
630
631         contentLength = apr_psprintf(r->pool, "%d", ctx->len);
632         apr_table_setn(r->headers_out, "Content-Length", contentLength);
633         
634         if (ctx->len > 0) {
635           rv = pass_data_to_filter(f, 
636                                    (const char*)ctx->buffer, 
637                                    (apr_size_t)ctx->len);
638         }
639         f->ctx = NULL;
640         DBG(r, " ");
641         return rv;
642       }
643       else {
644         DBG1(r, " SAVE COOKIE[%x]", entryp->action);
645         /*
646          * save cookie.
647          */
648         if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
649           DBG(r, "entryp->action == COOKIE_ON_BIT");
650
651           cookie_id = chxj_save_cookie(r);
652
653           /*
654            * Location Header Check to add cookie parameter.
655            */
656           location_header = (char*)apr_table_get(r->headers_out, "Location");
657           if (location_header) {
658             DBG1(r, "Location Header=[%s]", location_header);
659             location_header = chxj_add_cookie_parameter(r,
660                                                         location_header,
661                                                         cookie_id);
662             apr_table_setn(r->headers_out, "Location", location_header);
663             DBG1(r, "Location Header=[%s]", location_header);
664           }
665         }
666
667         apr_table_setn(r->headers_out, "Content-Length", "0");
668         rv = pass_data_to_filter(f, (const char*)"", (apr_size_t)0);
669         DBG(r, " ");
670         return rv;
671       }
672     }
673     else
674     if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
675       DBG2(r, "read data[%.*s]",len, data);
676
677       if (f->ctx == NULL) {
678         /*--------------------------------------------------------------------*/
679         /* Start                                                              */
680         /*--------------------------------------------------------------------*/
681         DBG(r, "new context");
682         ctx = (mod_chxj_ctx*)apr_palloc(r->pool, sizeof(mod_chxj_ctx));
683         if (len > 0) {
684           ctx->buffer = apr_palloc(r->pool, len);
685           memcpy(ctx->buffer, data, len);
686         }
687         else {
688           ctx->buffer = apr_palloc(r->pool, 1);
689           ctx->buffer = '\0';
690         }
691         ctx->len = len;
692         f->ctx = (void*)ctx;
693       }
694       else {
695         /*--------------------------------------------------------------------*/
696         /* append data                                                        */
697         /*--------------------------------------------------------------------*/
698         char* tmp;
699         DBG(r, "append data start");
700         ctx = (mod_chxj_ctx*)f->ctx;
701
702         if (len > 0) {
703           tmp = apr_palloc(r->pool, ctx->len);
704           memcpy(tmp, ctx->buffer, ctx->len);
705
706           ctx->buffer = apr_palloc(r->pool, ctx->len + len);
707
708           memcpy(ctx->buffer, tmp, ctx->len);
709           memcpy(&ctx->buffer[ctx->len], data, len);
710
711           ctx->len += len;
712         }
713         DBG(r, "append data end");
714       }
715     }
716   }
717   apr_brigade_destroy(bb);
718
719   DBG(r, "end of output filter");
720
721   return APR_SUCCESS;
722 }
723
724 /**
725  * It is the main loop of the input filter. 
726  *
727  * @param f         [i/o] It is a filter.
728  * @param bb        [i]   brigade
729  * @param mode      [i]   mode
730  * @param block     [i]   block
731  * @param readbytes [i]   readbyte
732  */
733 static apr_status_t 
734 chxj_input_filter(ap_filter_t*        f, 
735                  apr_bucket_brigade*  bb,
736                  ap_input_mode_t      mode, 
737                  apr_read_type_e      block,
738                  apr_off_t            readbytes)
739 {
740   request_rec*        r = f->r;
741   apr_status_t        rv;
742   conn_rec*           c = r->connection;
743   apr_bucket*         b;
744   /*--------------------------------------------------------------------------*/
745   /* It is the brigade area for output                                        */
746   /*--------------------------------------------------------------------------*/
747   apr_bucket_brigade* ibb;            
748   /*--------------------------------------------------------------------------*/
749   /* It is the brigade area for input                                         */
750   /*--------------------------------------------------------------------------*/
751   apr_bucket_brigade* obb;
752   apr_size_t          len;
753   apr_bucket*         tmp_heap;
754   apr_bucket*         eos;
755   const char*         data;
756   char*               data_bucket;
757   char*               data_brigade;
758   char*               content_type;
759   device_table*       spec ;
760   char*               user_agent;
761   mod_chxj_config*    dconf;
762   chxjconvrule_entry* entryp;
763
764   DBG(r, "start of chxj_input_filter()");
765
766   data_brigade = qs_alloc_zero_byte_string(r);
767
768
769   ibb = apr_brigade_create(r->pool, c->bucket_alloc);
770   obb = apr_brigade_create(r->pool, c->bucket_alloc);
771
772   content_type = (char*)apr_table_get(r->headers_in, "Content-Type");
773   if (content_type 
774   && strncasecmp("multipart/form-data", content_type, 19) == 0) {
775
776     DBG(r, "detect multipart/form-data");
777     ap_remove_input_filter(f);
778
779     return ap_get_brigade(f->next, bb, mode, block, readbytes);
780   }
781
782   dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
783
784   entryp = chxj_apply_convrule(r, dconf->convrules);
785   if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
786     DBG(r,"EngineOff");
787
788     ap_remove_input_filter(f);
789     return ap_get_brigade(f->next, bb, mode, block, readbytes);
790   }
791
792   user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
793   spec = chxj_specified_device(r, user_agent);
794
795   switch(spec->html_spec_type) {
796   case CHXJ_SPEC_Chtml_1_0:
797   case CHXJ_SPEC_Chtml_2_0:
798   case CHXJ_SPEC_Chtml_3_0:
799   case CHXJ_SPEC_Chtml_4_0:
800   case CHXJ_SPEC_Chtml_5_0:
801   case CHXJ_SPEC_XHtml_Mobile_1_0:
802   case CHXJ_SPEC_Hdml:
803   case CHXJ_SPEC_Jhtml:
804     break;
805
806   default:
807     ap_remove_input_filter(f);
808     return ap_get_brigade(f->next, bb, mode, block, readbytes);
809   }
810
811
812   rv = ap_get_brigade(f->next, ibb, mode, block, readbytes);
813   if (rv != APR_SUCCESS) {
814     DBG(r, "ap_get_brigade() failed");
815     return rv;
816   }
817
818   APR_BRIGADE_FOREACH(b, ibb) {
819     rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
820     if (rv != APR_SUCCESS) {
821       DBG(r, "apr_bucket_read() failed");
822       return rv;
823     }
824
825     if (data != NULL) {
826       data_bucket = apr_palloc(r->pool, len+1);
827       memset((void*)data_bucket, 0, len+1);
828       memcpy(data_bucket, data, len);
829       DBG1(r, "(in)POSTDATA:[%s]", data_bucket);
830   
831       data_brigade = apr_pstrcat(r->pool, data_brigade, data_bucket, NULL);
832     }
833
834     if (APR_BUCKET_IS_EOS(b)) {
835       break;
836     }
837   }
838   apr_brigade_cleanup(ibb);
839
840
841   len = strlen(data_brigade);
842   if (len == 0) {
843     DBG(r,"data_brigade length is 0");
844     DBG(r,"end of chxj_input_filter()");
845     ap_remove_input_filter(f);
846     return ap_get_brigade(f->next, bb, mode, block, readbytes);
847   }
848   data_brigade = chxj_input_convert(
849     r, 
850     (const char**)&data_brigade, 
851     (apr_size_t*)&len,
852     entryp
853     );
854
855   if (len > 0) {
856     DBG1(r, "(in:exchange)POSTDATA:[%s]", data_brigade);
857
858     obb = apr_brigade_create(r->pool, c->bucket_alloc);
859
860     tmp_heap = apr_bucket_heap_create(data_brigade, 
861                                       len, 
862                                       NULL, 
863                                       f->c->bucket_alloc);
864     eos      = apr_bucket_eos_create(f->c->bucket_alloc);
865
866     APR_BRIGADE_INSERT_TAIL(obb, tmp_heap);
867     APR_BRIGADE_INSERT_TAIL(obb, eos);
868     APR_BRIGADE_CONCAT(bb, obb);
869   }
870
871   DBG(r, "end of chxj_input_filter()");
872
873   return APR_SUCCESS;
874 }
875
876
877 static mod_chxj_global_config*
878 chxj_global_config_create(apr_pool_t* pool, server_rec* s)
879 {
880   mod_chxj_global_config* conf;
881
882   SDBG(s, "start chxj_global_config_create()");
883
884   /*--------------------------------------------------------------------------*/
885   /* allocate an own subpool which survives server restarts                   */
886   /*--------------------------------------------------------------------------*/
887   conf = (mod_chxj_global_config*)apr_palloc(pool, 
888                   sizeof(mod_chxj_global_config));
889 #if 0
890   conf->cookie_db_lock = NULL;
891 #endif
892   SDBG(s, "end   chxj_global_config_create()");
893
894   return conf;
895 }
896
897 /**
898  * initialize chxj module
899  */
900 static int 
901 chxj_init_module(apr_pool_t *p, 
902                   apr_pool_t *plog, 
903                   apr_pool_t *ptemp, 
904                   server_rec *s)
905 {
906 #if 0
907   mod_chxj_global_config* conf;
908 #endif
909   void *user_data;
910
911   SDBG(s, "start chxj_init_module()");
912
913   apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
914   SDBG(s, " ");
915   if (user_data == NULL) {
916     SDBG(s, " ");
917     /*
918      * dummy user_data set.
919      */
920     apr_pool_userdata_set(
921       (const void *)(1), 
922       CHXJ_MOD_CONFIG_KEY, 
923       apr_pool_cleanup_null, 
924       s->process->pool);
925     SDBG(s, "end  chxj_init_module()");
926     return OK;
927   }
928
929   ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
930
931
932 #if 0
933   conf = (mod_chxj_global_config *)ap_get_module_config(s->module_config, 
934                                                         &chxj_module);
935
936   if (apr_global_mutex_create(&(conf->cookie_db_lock), 
937                               NULL, APR_LOCK_DEFAULT, p) != APR_SUCCESS) {
938     SERR(s, "end  chxj_init_module()");
939     return HTTP_INTERNAL_SERVER_ERROR;
940   }
941
942 #ifdef AP_NEED_SET_MUTEX_PERMS
943   if (unixd_set_global_mutex_perms(conf->cookie_db_lock) != APR_SUCCESS) {
944     SERR(s, "end  chxj_init_module()");
945     return HTTP_INTERNAL_SERVER_ERROR;
946   }
947 #endif
948 #endif
949
950
951   SDBG(s, "end  chxj_init_module()");
952
953   return OK;
954 }
955
956 static void 
957 chxj_child_init(apr_pool_t *p, server_rec *s)
958 {
959 #if 0
960   mod_chxj_global_config* conf;
961 #endif
962
963   SDBG(s, "start chxj_child_init()");
964
965 #if 0
966   conf = (mod_chxj_global_config*)ap_get_module_config(s->module_config, 
967                                                        &chxj_module);
968
969   if (apr_global_mutex_child_init(&conf->cookie_db_lock, NULL, p) 
970   != APR_SUCCESS) {
971     SERR(s, "Can't attach global mutex.");
972     return;
973   }
974 #endif
975
976   SDBG(s, "end   chxj_child_init()");
977 }
978
979
980 /**
981  * A set structure of each server is generated. 
982  * 
983  * @param p
984  * @param s
985  */
986 static void*
987 chxj_config_server_create(apr_pool_t *p, server_rec *s)
988 {
989   mod_chxj_global_config *gc;
990
991   gc = chxj_global_config_create(p,s);
992
993   return gc;
994 }
995
996
997 static int
998 chxj_translate_name(request_rec *r)
999 {
1000   return chxj_trans_name(r);
1001 }
1002
1003
1004 /**
1005  * The hook is registered.
1006  *
1007  * @param p
1008  */
1009 static void 
1010 chxj_register_hooks(apr_pool_t *p)
1011 {
1012   ap_hook_post_config(chxj_init_module,
1013                       NULL,
1014                       NULL,
1015                       APR_HOOK_REALLY_FIRST);
1016   ap_hook_child_init(chxj_child_init, 
1017                      NULL, 
1018                      NULL, 
1019                      APR_HOOK_REALLY_FIRST);
1020   ap_register_output_filter (
1021                       "chxj_output_filter", 
1022                       chxj_output_filter, 
1023                       NULL, 
1024                       AP_FTYPE_RESOURCE);
1025   ap_register_input_filter(
1026                       "chxj_input_filter", 
1027                       chxj_input_filter, 
1028                       NULL, 
1029                       AP_FTYPE_RESOURCE);
1030   ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1031   ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1032   ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1033   ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_LAST);
1034 }
1035
1036
1037 /**
1038  * A set structure according to the directory is generated. 
1039  *
1040  * @param p
1041  * @param arg
1042  */
1043 static void* 
1044 chxj_create_per_dir_config(apr_pool_t *p, char *arg) 
1045 {
1046   mod_chxj_config* conf;
1047
1048   conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1049   conf->device_data_file = NULL;
1050   conf->devices          = NULL;
1051   conf->emoji_data_file  = NULL;
1052   conf->emoji            = NULL;
1053   conf->emoji_tail       = NULL;
1054   conf->image            = CHXJ_IMG_OFF;
1055   conf->image_cache_dir  = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1056   conf->server_side_encoding = NULL;
1057   if (arg == NULL) {
1058     conf->dir                  = NULL;
1059   }
1060   else {
1061     conf->dir                  = apr_pcalloc(p, strlen(arg)+1);
1062     memset(conf->dir, 0, strlen(arg)+1);
1063     strcpy(conf->dir, arg);
1064   }
1065   conf->convrules   = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1066
1067   /* Default is copyleft */
1068   conf->image_copyright = NULL; 
1069
1070   return conf;
1071 }
1072
1073
1074 /*
1075  *  Merge per-directory CHXJ configurations
1076  */
1077 static void*
1078 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1079 {
1080   mod_chxj_config *base;
1081   mod_chxj_config *add;
1082   mod_chxj_config *mrg;
1083
1084   base = (mod_chxj_config*)basev;
1085   add  = (mod_chxj_config*)addv;
1086   mrg  = (mod_chxj_config*)apr_palloc(p, sizeof(mod_chxj_config));
1087
1088   mrg->device_data_file = NULL;
1089   mrg->devices          = NULL;
1090   mrg->emoji_data_file  = NULL;
1091   mrg->image            = CHXJ_IMG_OFF;
1092   mrg->image_cache_dir  = NULL;
1093   mrg->image_copyright  = NULL;
1094   mrg->emoji            = NULL;
1095   mrg->emoji_tail       = NULL;
1096
1097   mrg->dir = apr_pstrdup(p, add->dir);
1098
1099   if (! add->device_data_file) {
1100     mrg->devices = base->devices;
1101     mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1102   }
1103   else {
1104     mrg->devices = add->devices;
1105     mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1106   }
1107
1108   if (! add->emoji_data_file) {
1109     mrg->emoji = base->emoji;
1110     mrg->emoji_tail = base->emoji_tail;
1111     mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1112   }
1113   else {
1114     mrg->emoji = add->emoji;
1115     mrg->emoji_tail = add->emoji_tail;
1116     mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1117   }
1118
1119   if (add->image == CHXJ_IMG_OFF) 
1120     mrg->image = base->image;
1121   else 
1122     mrg->image = add->image;
1123
1124
1125   if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) 
1126     mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1127   else 
1128     mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1129
1130   if (add->image_copyright) 
1131     mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1132   else
1133     mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1134
1135   if (add->server_side_encoding) {
1136     mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1137   }
1138   else 
1139   if (base->server_side_encoding) {
1140     mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1141   }
1142   else {
1143     mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1144   }
1145
1146   mrg->convrules    = apr_array_append(p, add->convrules, base->convrules);
1147
1148   return mrg;
1149 }
1150
1151
1152 static int
1153 chxj_command_parse_take5(
1154   const char* arg, 
1155   char** prm1, 
1156   char** prm2, 
1157   char** prm3, 
1158   char** prm4, 
1159   char** prm5)
1160 {
1161   int isquoted;
1162   char* strp;
1163
1164   strp = (char*)arg;
1165
1166   for (;*strp == ' '||*strp == '\t'; strp++) ;
1167
1168   isquoted = 0; 
1169   if (*strp == '"') { 
1170     isquoted = 1;
1171     strp++;
1172   }
1173
1174   *prm1 = strp;
1175
1176   for (; *strp != '\0'; strp++) {
1177     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1178     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1179       strp++;
1180       continue;
1181     }
1182
1183     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1184     ||  (isquoted  && *strp == '"'))
1185       break;
1186   }
1187
1188   if (! *strp) {
1189     *prm2 = strp;
1190     *prm3 = strp;
1191     *prm4 = strp;
1192     *prm5 = strp;
1193     return 1;
1194   }
1195
1196   *strp++ = '\0';
1197
1198   for (;*strp == ' '||*strp == '\t'; strp++) ;
1199
1200   isquoted = 0; 
1201   if (*strp == '"') { 
1202     isquoted = 1;
1203     strp++;
1204   }
1205
1206   *prm2 = strp;
1207   for (; *strp != '\0'; strp++) {
1208     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1209     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1210       strp++;
1211       continue;
1212     }
1213
1214     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1215     ||  (isquoted  && *strp == '"'))
1216       break;
1217   }
1218
1219   if (! *strp) {
1220     *prm3 = strp;
1221     *prm4 = strp;
1222     *prm5 = strp;
1223     return 0;
1224   }
1225
1226   *strp++ = '\0';
1227
1228   for (;*strp == ' '||*strp == '\t'; strp++);
1229
1230   isquoted = 0; 
1231   if (*strp == '"') { 
1232     isquoted = 1;
1233     strp++;
1234   }
1235   *prm3 = strp;
1236   for (; *strp != '\0'; strp++) {
1237     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1238     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1239       strp++;
1240       continue;
1241     }
1242
1243     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1244     ||  (isquoted  && *strp == '"'))
1245       break;
1246   }
1247
1248   if (! *strp) {
1249     *prm4 = strp;
1250     *prm5 = strp;
1251     return 0;
1252   }
1253
1254   *strp++ = '\0';
1255
1256   for (;*strp == ' '||*strp == '\t'; strp++);
1257
1258   isquoted = 0; 
1259   if (*strp == '"') { 
1260     isquoted = 1;
1261     strp++;
1262   }
1263   *prm4 = strp;
1264   for (; *strp != '\0'; strp++) {
1265     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1266     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1267       strp++;
1268       continue;
1269     }
1270
1271     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1272     ||  (isquoted  && *strp == '"'))
1273       break;
1274   }
1275
1276   if (! *strp) {
1277     *prm5 = strp;
1278     return 0;
1279   }
1280
1281   *strp++ = '\0';
1282
1283   for (;*strp == ' '||*strp == '\t'; strp++);
1284
1285   isquoted = 0; 
1286   if (*strp == '"') { 
1287     isquoted = 1;
1288     strp++;
1289   }
1290   *prm5 = strp;
1291   for (; *strp != '\0'; strp++) {
1292     if ((isquoted && (*strp == ' ' || *strp == '\t'))
1293     ||  (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1294       strp++;
1295       continue;
1296     }
1297
1298     if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1299     ||  (isquoted  && *strp == '"'))
1300       break;
1301   }
1302   *strp = '\0';
1303
1304   return 0;
1305 }
1306
1307
1308 /**
1309  * The device definition file is loaded. 
1310  *
1311  * @param arg     [i]   The name of the device definition file is specified.
1312  * @param mconfig [i/o] The pointer to a set structure is specified. 
1313  * @param parms   [i]   
1314  */
1315 static const char* 
1316 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char* arg) 
1317 {
1318   mod_chxj_config* conf;
1319   Doc              doc;
1320
1321   doc.r = NULL;
1322
1323   if (strlen(arg) > 256) 
1324     return "device data filename too long.";
1325
1326   conf = (mod_chxj_config*)mconfig;
1327   conf->device_data_file = apr_pstrdup(parms->pool, arg);
1328
1329   qs_init_malloc(&doc);
1330   qs_init_root_node(&doc);
1331
1332   qs_parse_file((Doc*)&doc, (const char*)arg);
1333   chxj_load_device_data(&doc,parms->pool, conf);
1334   qs_all_free(&doc, QX_LOGMARK);
1335
1336   return NULL;
1337 }
1338
1339
1340 /**
1341  * Device definition information is loaded. 
1342  *
1343  * @param parms 
1344  * @param arg     [i]   The name of the device definition file is specified. 
1345  * @param mconfig [i/o] The pointer to a set structure is specified. 
1346  * @return 
1347  */
1348 static const char* 
1349 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char* arg) 
1350 {
1351   mod_chxj_config* conf;
1352   char*            rtn;
1353   Doc              doc;
1354
1355   doc.r = NULL;
1356
1357
1358   if (strlen(arg) > 256) 
1359     return "emoji data filename too long.";
1360
1361   conf = (mod_chxj_config*)mconfig;
1362   conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
1363   qs_init_malloc(&doc);
1364   qs_init_root_node(&doc);
1365
1366   qs_parse_file((Doc*)&doc, (const char*)arg);
1367
1368   rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
1369
1370   qs_all_free(&doc, QX_LOGMARK);
1371
1372
1373   return rtn;
1374 }
1375
1376
1377 static const char* 
1378 cmd_set_image_engine(cmd_parms *parms, void *mconfig, const char* arg) 
1379 {
1380   mod_chxj_config* conf;
1381   Doc              doc;
1382
1383   doc.r = NULL;
1384
1385   if (strlen(arg) > 256) 
1386     return "image uri is too long.";
1387
1388   conf = (mod_chxj_config*)mconfig;
1389   if (strcasecmp("ON", arg) == 0)
1390     conf->image = CHXJ_IMG_ON;
1391   else
1392     conf->image = CHXJ_IMG_OFF;
1393
1394   return NULL;
1395 }
1396
1397
1398 static const char* 
1399 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char* arg) 
1400 {
1401   mod_chxj_config* conf;
1402   Doc              doc;
1403
1404   doc.r = NULL;
1405
1406   if (strlen(arg) > 256) 
1407     return "cache dir name is too long.";
1408
1409   conf = (mod_chxj_config*)mconfig;
1410   conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
1411
1412   return NULL;
1413 }
1414
1415
1416 static const char* 
1417 cmd_set_image_copyright(cmd_parms *parms, void* mconfig, const char* arg) 
1418 {
1419   mod_chxj_config* conf;
1420   Doc              doc;
1421
1422   doc.r = NULL;
1423
1424   if (strlen(arg) > 256) 
1425     return "Copyright Flag is too long.";
1426
1427   conf = (mod_chxj_config*)mconfig;
1428   conf->image_copyright = apr_pstrdup(parms->pool, arg);
1429
1430   return NULL;
1431 }
1432
1433
1434 static const char*
1435 cmd_convert_rule(cmd_parms *cmd, void* mconfig, const char *arg)
1436 {
1437   int                 mode;
1438   ap_regex_t*         regexp;
1439   mod_chxj_config*    dconf;
1440   chxjconvrule_entry* newrule;
1441   char*               prm1;
1442   char*               prm2;
1443   char*               prm3;
1444   char*               prm4;
1445   char*               prm5;
1446   char*               pstate;
1447   char*               action;
1448   char*               pp;
1449
1450   dconf = (mod_chxj_config*)mconfig;
1451
1452   if (strlen(arg) > 4096) 
1453     return "ChxjConvertRule: is too long.";
1454
1455   dconf = (mod_chxj_config*)mconfig;
1456   if (dconf->convrules == NULL)
1457     dconf->convrules   = apr_array_make(cmd->pool, 
1458                                         2, 
1459                                         sizeof(chxjconvrule_entry));
1460
1461   newrule = apr_array_push(dconf->convrules);
1462
1463   newrule->flags  = 0;
1464   newrule->action = 0;
1465
1466   if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
1467     return "ChxjConvertRule: bad argument line";
1468
1469   newrule->pattern = apr_pstrdup(cmd->pool, prm1);
1470
1471   /* Parse action */
1472   for (;;) {
1473     if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
1474       break;
1475     prm2 = NULL;
1476     switch(*action) {
1477     case 'e':
1478     case 'E':
1479       if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
1480         newrule->action |= CONVRULE_ENGINE_ON_BIT;
1481       }
1482       else
1483       if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
1484         newrule->action |= CONVRULE_ENGINE_OFF_BIT;
1485       }
1486       break;
1487
1488     case 'C':
1489     case 'c':
1490       if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
1491         newrule->action |= CONVRULE_COOKIE_ON_BIT;
1492       }
1493       break;
1494     default:
1495       break;
1496     }
1497   }
1498   
1499   pp = prm1;
1500   if (*pp == '!') {
1501     newrule->flags |= CONVRULE_FLAG_NOTMATCH;
1502     pp++;
1503   }
1504
1505   mode = AP_REG_EXTENDED;
1506   if ((regexp = ap_pregcomp(cmd->pool, pp, mode)) == NULL)
1507     return "RewriteRule: cannot compile regular expression ";
1508
1509   newrule->regexp = regexp;
1510   if (*prm3)
1511     newrule->encoding = apr_pstrdup(cmd->pool, prm3);
1512   else
1513     newrule->encoding = apr_pstrdup(cmd->pool, "none");
1514
1515   newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
1516   if (*prm4)
1517     if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
1518       newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
1519
1520   newrule->user_agent = NULL;
1521   if (*prm5)
1522     newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
1523     
1524   return NULL;
1525 }
1526
1527
1528 static const command_rec cmds[] = {
1529   AP_INIT_TAKE1(
1530     "ChxjLoadDeviceData",
1531     cmd_load_device_data,
1532     NULL,
1533     OR_ALL,
1534     "Load Device Data"),
1535   AP_INIT_TAKE1(
1536     "ChxjLoadEmojiData",
1537     cmd_load_emoji_data,
1538     NULL,
1539     OR_ALL,
1540     "Load Emoji Data"),
1541   AP_INIT_TAKE1(
1542     "ChxjImageEngine",
1543     cmd_set_image_engine,
1544     NULL,
1545     OR_ALL,
1546     "Convert Target URI"),
1547   AP_INIT_TAKE1(
1548     "ChxjImageCacheDir",
1549     cmd_set_image_cache_dir,
1550     NULL,
1551     OR_ALL,
1552     "Image Cache Directory"),
1553   AP_INIT_TAKE1(
1554     "ChxjImageCopyright",
1555     cmd_set_image_copyright,
1556     NULL,
1557     OR_ALL,
1558     "Copyright Flag"),
1559   AP_INIT_RAW_ARGS(
1560     "ChxjConvertRule",
1561     cmd_convert_rule,
1562     NULL, 
1563     OR_FILEINFO,
1564     "an URL-applied regexp-pattern and a substitution URL"),
1565   { NULL },
1566 };
1567
1568
1569 /*----------------------------------------------------------------------------*/
1570 /* Dispatch list for API hooks                                                */
1571 /*----------------------------------------------------------------------------*/
1572 module AP_MODULE_DECLARE_DATA chxj_module = {
1573   STANDARD20_MODULE_STUFF, 
1574   chxj_create_per_dir_config,          /* create per-dir    config structures */
1575   chxj_merge_per_dir_config,           /* merge  per-dir    config structures */
1576   chxj_config_server_create,           /* create per-server config structures */
1577   NULL,                                /* merge  per-server config structures */
1578   cmds,                                /* table of config file commands       */
1579   chxj_register_hooks                  /* register hooks                      */
1580 };
1581 /*
1582  * vim:ts=2 et
1583  */