OSDN Git Service

* Added New Features.
[modchxj/mod_chxj.git] / include / mod_chxj.h
1 /*
2  * Copyright (C) 2005-2009 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef __MOD_CHXJ_H__
18 #define __MOD_CHXJ_H__
19
20 #define CHXJ_TRUE    (1)
21 #define CHXJ_FALSE   (0)
22
23 /* #define DEBUG_FD(X,args...) {FILE*fp=fopen("/tmp/error.log", "ab");fprintf(fp, X, ##args);fflush(fp);fclose(fp);} */
24
25 #ifdef UNUSED
26 #elif defined(__GNUC__)
27 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
28 #elif defined(__LCLINT__)
29 # define UNUSED(x) /*@unused@*/ x
30 #else
31 # define UNUSED(x) x
32 #endif
33
34
35 #include <string.h>
36
37 #include "chxj_apache.h"
38
39 #include "apr_strings.h"
40 #include "apr_buckets.h"
41 #include "apr_lib.h"
42 #include "apr_tables.h"
43 #include "apr_dso.h"
44 #include "apr_general.h"
45 #include "apr_pools.h"
46
47 #if defined(AP_NEED_SET_MUTEX_PERMS)
48 #  include "unixd.h"
49 #endif
50 #if !defined(AP_NEED_SET_MUTEX_PERMS)
51 #  if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
52 #    define AP_NEED_SET_MUTEX_PERMS
53 #  endif
54 #endif
55
56 #define STRCASEEQ(a,b,c,d) \
57   ((((a) == *(d))|| ((b) == *(d))) && strcasecmp((c),(d)) == 0)
58 #define STRNCASEEQ(a,b,c,d,e) \
59   ((((a) == *(d))|| ((b) == *(d))) && strncasecmp((c),(d),(e)) == 0)
60
61 #define CHXJ_QUERY_STRING_PARAM_PREFIX "_chxj_qs_"
62 #define CHXJ_QUERY_STRING_PARAM_PREFIX_ENC "%5Fchxj%5Fqs%5F"
63
64
65 #include "qs_ignore_sp.h"
66 #include "qs_log.h"
67 #include "qs_malloc.h"
68 #include "qs_parse_attr.h"
69 #include "qs_parse_file.h"
70 #include "qs_parse_string.h"
71 #include "qs_parse_tag.h"
72
73 #if defined(HAVE_AP_REGEX_H) && HAVE_AP_REGEX_H == 1
74 #  include "ap_regex.h"
75 #else
76 #  include "pcreposix.h"
77 #  include "pcre.h"
78 #  if !defined(AP_REG_ASSERT)
79 #    define AP_REG_ASSERT   REG_ASSERT
80 #  endif
81 #  if !defined(AP_REG_BADBR)
82 #    define AP_REG_BADBR    REG_BADBR
83 #  endif
84 #  if !defined(AP_REG_BADPAT)
85 #    define AP_REG_BADPAT   REG_BADPAT
86 #  endif
87 #  if !defined(AP_REG_BADRPT)
88 #    define AP_REG_BADRPT   REG_BADRPT
89 #  endif
90 #  if !defined(AP_REG_EBRACE)
91 #    define AP_REG_EBRACE   REG_EBRACE
92 #  endif
93 #  if !defined(AP_REG_EBRACK)
94 #    define AP_REG_EBRACK   REG_EBRACK
95 #  endif
96 #  if !defined(AP_REG_ECOLLATE)
97 #    define AP_REG_ECOLLATE REG_ECOLLATE
98 #  endif
99 #  if !defined(AP_REG_ECTYPE)
100 #    define AP_REG_ECTYPE   REG_ECTYPE
101 #  endif
102 #  if !defined(AP_REG_EESCAPE)
103 #    define AP_REG_EESCAPE  REG_EESCAPE
104 #  endif
105 #  if !defined(AP_REG_EMPTY)
106 #    define AP_REG_EMPTY    REG_EMPTY
107 #  endif
108 #  if !defined(AP_REG_EPAREN)
109 #    define AP_REG_EPAREN   REG_EPAREN
110 #  endif
111 #  if !defined(AP_REG_ERANGE)
112 #    define AP_REG_ERANGE   REG_ERANGE
113 #  endif
114 #  if !defined(AP_REG_ESIZE)
115 #    define AP_REG_ESIZE    REG_ESIZE
116 #  endif
117 #  if !defined(AP_REG_ESPACE)
118 #    define AP_REG_ESPACE   REG_ESPACE
119 #  endif
120 #  if !defined(AP_REG_ESUBREG)
121 #    define AP_REG_ESUBREG  REG_ESUBREG
122 #  endif
123 #  if !defined(AP_REG_INVARG)
124 #    define AP_REG_INVARG   REG_INVARG
125 #  endif
126 #  if !defined(AP_REG_NOMATCH)
127 #    define AP_REG_NOMATCH  REG_NOMATCH
128 #  endif
129
130 #  if !defined(AP_REG_ICASE)
131 #    define AP_REG_ICASE    REG_ICASE
132 #  endif
133 #  if !defined(AP_REG_NEWLINE)
134 #    define AP_REG_NEWLINE  REG_NEWLINE
135 #  endif
136 #  if !defined(AP_REG_NOTBOL)
137 #    define AP_REG_NOTBOL   REG_NOTBOL
138 #  endif
139 #  if !defined(AP_REG_NOTEOL)
140 #    define AP_REG_NOTEOL   REG_NOTEOL
141 #  endif
142
143 #  if !defined(AP_REG_EXTENDED)
144 #    define AP_REG_EXTENDED REG_EXTENDED
145 #  endif
146 #  if !defined(AP_REG_NOSUB)
147 #    define AP_REG_NOSUB    REG_NOSUB
148 #  endif
149
150 #  define ap_regex_t      regex_t
151 #  define ap_regmatch_t   regmatch_t
152 #endif
153
154
155
156
157 #ifdef HAVE_APR_GLOBAL_MUTEX_H
158 #  include "apr_global_mutex.h"
159 #endif
160
161 #ifdef HAVE_APR_SHM_H
162 #  include "apr_shm.h"
163 #endif
164
165
166 typedef struct imode_emoji_t imode_emoji_t;
167
168 struct imode_emoji_t {
169   char  hex1byte;
170   char  hex2byte;
171   char* string;
172   char *description;
173 };
174
175 typedef struct ezweb_emoji_t ezweb_emoji_t;
176
177 struct ezweb_emoji_t {
178   char* typeA;
179   char* typeB;
180   char* typeC;
181   char* typeD;
182 };
183
184 typedef struct jphone_emoji_t jphone_emoji_t;
185
186 struct jphone_emoji_t {
187   char* string;
188 };
189
190 typedef struct emoji_t emoji_t;
191
192 struct emoji_t {
193   struct emoji_t*  next;
194   int              no;
195   imode_emoji_t*   imode;
196   ezweb_emoji_t*   ezweb;
197   jphone_emoji_t*  jphone;
198 };
199
200 typedef struct chxjconvrule_entry chxjconvrule_entry;
201
202 struct chxjconvrule_entry {
203   char         *pattern;
204   ap_regex_t   *regexp;
205   int          flags;
206   int          action;
207   char         *encoding;
208   int          pc_flag;
209   char         *user_agent;
210 };
211
212 typedef struct tag_handler tag_handler;
213
214 struct tag_handler {
215   char* (*start_tag_handler)(void* doc, Node* node); 
216   char* (*end_tag_handler)(void* doc, Node* node); 
217 };
218
219 #include "chxj_specified_device.h"
220
221 typedef struct {
222   spec_type    type; 
223   tag_handler* handler;
224 } tag_handlers;
225
226 typedef enum {
227   tagHTML = 0,
228   tagMETA,  
229   tagTEXTAREA,
230   tagP,
231   tagPRE,
232   tagUL,
233   tagLI,
234   tagOL,
235   tagH1,
236   tagH2,
237   tagH3,
238   tagH4,
239   tagH5,
240   tagH6,
241   tagHEAD,
242   tagTITLE,
243   tagBASE,
244   tagBODY,
245   tagA,
246   tagBR,
247   tagTABLE,
248   tagTR,
249   tagTD,
250   tagTBODY,
251   tagFONT,
252   tagFORM,
253   tagINPUT,
254   tagCENTER,
255   tagHR,
256   tagIMG,
257   tagSELECT,
258   tagOPTION,
259   tagDIV,
260   tagCHXJIF,  
261   tagNOBR,  
262   tagSMALL,  
263   tagSTYLE,  
264   tagSPAN,  
265   tagTEXT,  
266   tagTH,  
267   tagB,
268   tagFIELDSET,
269   tagDT,
270   tagLEGEND,
271   tagLABEL,
272   tagBLOCKQUOTE,
273   tagDIR,
274   tagDL,
275   tagDD,
276   tagMENU,
277   tagPLAINTEXT,
278   tagBLINK,
279   tagMARQUEE,
280   tagNLMARK,      /* New Line Code */
281 } tag_type;
282
283 typedef struct mod_chxj_config mod_chxj_config;
284
285 #if defined(USE_MYSQL_COOKIE)
286 #  include "chxj_mysql.h"
287 #endif
288 #if defined(USE_MEMCACHE_COOKIE)
289 #  include "chxj_memcache.h"
290 #endif
291
292 /* cookie store type */
293 #define CHXJ_COOKIE_STORE_TYPE_DBM      "dbm"
294 #define CHXJ_COOKIE_STORE_TYPE_MYSQL    "mysql"
295 #define CHXJ_COOKIE_STORE_TYPE_MEMCACHE "memcache"
296 typedef enum {
297   COOKIE_STORE_TYPE_NONE     = 0,
298   COOKIE_STORE_TYPE_DBM      = 1,
299   COOKIE_STORE_TYPE_MYSQL    = 2,
300   COOKIE_STORE_TYPE_MEMCACHE = 3, 
301 } cookie_store_type_t;
302
303 /* new line type */
304 #define CHXJ_NEW_LINE_TYPE_CRLF         "crlf"
305 #define CHXJ_NEW_LINE_TYPE_LF           "lf"
306 #define CHXJ_NEW_LINE_TYPE_CR           "cr"
307 #define CHXJ_NEW_LINE_TYPE_NONE         "none"
308 typedef enum {
309   NLTYPE_NIL  = 0,
310   NLTYPE_CRLF = 1,
311   NLTYPE_LF   = 2,
312   NLTYPE_CR   = 3,
313   NLTYPE_NONE = 4,
314 } chxj_new_line_type_t;
315 #define IS_NLTYPE_CRLF(X)   ((X)->new_line_type == NLTYPE_CRLF)
316 #define IS_NLTYPE_LF(X)     ((X)->new_line_type == NLTYPE_LF)
317 #define IS_NLTYPE_CR(X)     ((X)->new_line_type == NLTYPE_CR)
318 #define IS_NLTYPE_NONE(X)   ((X)->new_line_type == NLTYPE_NONE)
319 #define TO_NLCODE(X)        (IS_NLTYPE_CRLF(X) ? "\r\n" : \
320                              IS_NLTYPE_LF(X)   ? "\n"   : \
321                              IS_NLTYPE_CR(X)   ? "\r"   : \
322                              IS_NLTYPE_NONE(X) ? ""     : "\r\n")
323
324 struct mod_chxj_config {
325   int                   image;
326
327   char                  *device_data_file;
328   char                  *emoji_data_file;
329
330   char                  *image_cache_dir;
331   char                  *image_copyright;
332   unsigned long         image_cache_limit;
333   device_table_list     *devices;
334   emoji_t               *emoji;
335   emoji_t               *emoji_tail;
336   char                  *server_side_encoding;
337
338   char                  *dir; /* for LOG */
339
340   apr_array_header_t    *convrules;
341
342   char                  *cookie_db_dir;
343   long                  cookie_timeout;
344   cookie_store_type_t   cookie_store_type;
345   int                   cookie_lazy_mode;
346
347 #if defined(USE_MYSQL_COOKIE)
348   mysql_t               mysql;
349 #endif
350 #if defined(USE_MEMCACHE_COOKIE)
351   memcache_t            memcache;
352 #endif
353   char                  *forward_url_base;  /* use input filter */
354   char                  *forward_server_ip; /* use input filter */
355
356   char                  *allowed_cookie_domain; /* default is null */
357
358   chxj_new_line_type_t  new_line_type;
359 };
360
361 #define IS_COOKIE_STORE_DBM(X)      ((X) == COOKIE_STORE_TYPE_DBM)
362 #define IS_COOKIE_STORE_MYSQL(X)    ((X) == COOKIE_STORE_TYPE_MYSQL)
363 #define IS_COOKIE_STORE_MEMCACHE(X) ((X) == COOKIE_STORE_TYPE_MEMCACHE)
364 #define IS_COOKIE_STORE_NONE(X)     ((X) == COOKIE_STORE_TYPE_NONE)
365
366 #define COOKIE_LAZY_ON                (2)
367 #define COOKIE_LAZY_OFF               (1)
368 #define IS_COOKIE_LAZY(X)             ((X)->cookie_lazy_mode == COOKIE_LAZY_ON)
369
370
371 #define CONVRULE_ENGINE_ON_BIT        (0x00000001)
372 #define CONVRULE_ENGINE_OFF_BIT       (0x00000002)
373 #define CONVRULE_COOKIE_ON_BIT        (0x00000004)
374 #define CONVRULE_Z2H_ON_BIT           (0x00000008)
375 #define CONVRULE_Z2H_OFF_BIT          (0x00000010)
376 #define CONVRULE_Z2H_ALPHA_ON_BIT     (0x00000020)
377 #define CONVRULE_Z2H_ALPHA_OFF_BIT    (0x00000040)
378 #define CONVRULE_Z2H_NUM_ON_BIT       (0x00000080)
379 #define CONVRULE_Z2H_NUM_OFF_BIT      (0x00000100)
380 #define CONVRULE_QSCONV_OFF_BIT       (0x00000200)
381 #define CONVRULE_JRCONV_OFF_BIT       (0x00000400)
382 #define CONVRULE_NOCACHE_ON_BIT       (0x00000800)
383 #define CONVRULE_EMOJI_ONLY_BIT       (0x00001000)
384 #define CONVRULE_ENVINFO_ONLY_BIT     (0x00002000)
385
386 #define CONVRULE_ENGINE_ON_CMD        "EngineOn"
387 #define CONVRULE_ENGINE_OFF_CMD       "EngineOff"
388 #define CONVRULE_COOKIE_ON_CMD        "CookieOn"
389 #define CONVRULE_COOKIE_OFF_CMD       "CookieOff"
390 #define CONVRULE_Z2H_ON_CMD           "Z2hOn"
391 #define CONVRULE_Z2H_OFF_CMD          "Z2hOff"
392 #define CONVRULE_Z2H_ALPHA_ON_CMD     "Z2hAlphaOn"
393 #define CONVRULE_Z2H_ALPHA_OFF_CMD    "Z2hAlphaOff"
394 #define CONVRULE_Z2H_NUM_ON_CMD       "Z2hNumOn"
395 #define CONVRULE_Z2H_NUM_OFF_CMD      "Z2hNumOff"
396 #define CONVRULE_Z2H_ALL_ON_CMD       "Z2hAllOn"
397 #define CONVRULE_Z2H_ALL_OFF_CMD      "Z2hAllOff"
398 #define CONVRULE_QSCONV_OFF_CMD       "QSConvOff"
399 #define CONVRULE_JRCONV_OFF_CMD       "JRConvOff"
400 #define CONVRULE_NOCACHE_ON_CMD       "NoCacheOn"
401 #define CONVRULE_EMOJI_ONLY_CMD       "EmojiOnly"
402 #define CONVRULE_ENVINFO_ONLY_CMD     "EnvInfoOnly"
403
404
405 #define CONVRULE_FLAG_NOTMATCH        (0x00000001)
406
407 #define CONVRULE_PC_FLAG_ON_CMD       "PC"
408
409 #define CONVRULE_PC_FLAG_ON_BIT       (0x00000001)
410 #define CONVRULE_PC_FLAG_OFF_BIT      (0x00000002)
411
412
413 typedef struct {
414   apr_global_mutex_t* cookie_db_lock;
415 } mod_chxj_global_config;
416
417 typedef struct {
418   apr_size_t len;
419   chxjconvrule_entry  *entryp;
420   device_table        *spec;
421
422   apr_bucket_brigade *bb;
423   apr_pool_t *pool;
424
425   char* buffer;
426 } mod_chxj_ctx;
427
428 #include "chxj_tag_util.h"
429
430 #define CHXJ_MOD_CONFIG_KEY   "chxj_module_key"
431
432 #define HTTP_USER_AGENT       "User-Agent"
433 #define HTTP_HOST             "Host"
434 #define CHXJ_HTTP_USER_AGENT  "CHXJ_HTTP_USER_AGENT"
435 #define CHXJ_HEADER_ORIG_CLIENT_IP "X-Chxj-Orig-Client-Ip"
436
437 module AP_MODULE_DECLARE_DATA chxj_module;
438
439 #define CHXJ_IMG_ON     (2)
440 #define CHXJ_IMG_OFF    (1)
441 #define CHXJ_IMG_NONE   (0)
442
443
444 #define DBG(X,args...)  chxj_log_rerror(APLOG_MARK,APLOG_DEBUG,0,(request_rec*)(X),##args)
445 #define SDBG(X,Y)       chxj_log_error(APLOG_MARK,APLOG_DEBUG,0,(X),(Y))
446 #define PDBG(X,args...) chxj_log_perror(APLOG_MARK,APLOG_DEBUG,0,(apr_pool_t *)(X),##args)
447 #define ERR(X,args...)  chxj_log_rerror(APLOG_MARK,APLOG_ERR,0,(X), ##args)
448 #define SERR(X,args...) chxj_log_error(APLOG_MARK,APLOG_ERR,0,(X),##args)
449 #define PERR(X,args...) chxj_log_perror(APLOG_MARK,APLOG_ERR,0,(apr_pool_t *)(X),##args)
450 #define WRN(rec,format,args...)  chxj_log_rerror(APLOG_MARK,APLOG_WARNING,0,(rec),(format), ##args)
451
452 extern tag_handlers chxj_tag_handlers[];
453 extern tag_handler  chtml10_handler[];
454 extern tag_handler  chtml20_handler[];
455 extern tag_handler  chtml30_handler[];
456 extern tag_handler  chtml40_handler[];
457 extern tag_handler  chtml50_handler[];
458 extern tag_handler  xhtml_handler[];
459 extern tag_handler  hdml_handler[];
460 extern tag_handler  jhtml_handler[];
461 extern tag_handler  jxhtml_handler[];
462
463 extern char* chxj_node_convert( 
464   device_table *spec,
465   request_rec  *r,
466   void         *pdoc,
467   Doc          *doc,
468   Node         *node,
469   int          indent
470 );
471 extern char *chxj_node_convert_chxjif_only(
472   request_rec  *r,
473   device_table *spec,
474   const char   *src,
475   apr_size_t   *len
476 );
477
478 #define IMAGE_CACHE_LIMIT_FMT_LEN  (20)
479
480
481 #if HAVE_MALLOC == 0
482 extern void *rpl_malloc(size_t n);
483 #endif
484
485 #endif
486 /*
487  * vim:ts=2 et
488  */