OSDN Git Service

* version up for RELEASE
[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   tagCHXJRAW,  
262   tagNOBR,  
263   tagSMALL,  
264   tagSTYLE,  
265   tagSPAN,  
266   tagTEXT,  
267   tagTH,  
268   tagB,
269   tagFIELDSET,
270   tagDT,
271   tagLEGEND,
272   tagLABEL,
273   tagBLOCKQUOTE,
274   tagDIR,
275   tagDL,
276   tagDD,
277   tagMENU,
278   tagPLAINTEXT,
279   tagBLINK,
280   tagMARQUEE,
281   tagNLMARK,      /* New Line Code */
282 } tag_type;
283
284 typedef struct mod_chxj_config mod_chxj_config;
285 typedef struct mod_chxj_req_config_t mod_chxj_req_config;
286
287 #if defined(USE_MYSQL_COOKIE)
288 #  include "chxj_mysql.h"
289 #endif
290 #if defined(USE_MEMCACHE_COOKIE)
291 #  include "chxj_memcache.h"
292 #endif
293
294 /* cookie store type */
295 #define CHXJ_COOKIE_STORE_TYPE_DBM      "dbm"
296 #define CHXJ_COOKIE_STORE_TYPE_MYSQL    "mysql"
297 #define CHXJ_COOKIE_STORE_TYPE_MEMCACHE "memcache"
298 typedef enum {
299   COOKIE_STORE_TYPE_NONE     = 0,
300   COOKIE_STORE_TYPE_DBM      = 1,
301   COOKIE_STORE_TYPE_MYSQL    = 2,
302   COOKIE_STORE_TYPE_MEMCACHE = 3, 
303 } cookie_store_type_t;
304
305 /* new line type */
306 #define CHXJ_NEW_LINE_TYPE_CRLF         "crlf"
307 #define CHXJ_NEW_LINE_TYPE_LF           "lf"
308 #define CHXJ_NEW_LINE_TYPE_CR           "cr"
309 #define CHXJ_NEW_LINE_TYPE_NONE         "none"
310 typedef enum {
311   NLTYPE_NIL  = 0,
312   NLTYPE_CRLF = 1,
313   NLTYPE_LF   = 2,
314   NLTYPE_CR   = 3,
315   NLTYPE_NONE = 4,
316 } chxj_new_line_type_t;
317 #define IS_NLTYPE_CRLF(X)   ((X)->new_line_type == NLTYPE_CRLF)
318 #define IS_NLTYPE_LF(X)     ((X)->new_line_type == NLTYPE_LF)
319 #define IS_NLTYPE_CR(X)     ((X)->new_line_type == NLTYPE_CR)
320 #define IS_NLTYPE_NONE(X)   ((X)->new_line_type == NLTYPE_NONE)
321 #define TO_NLCODE(X)        (IS_NLTYPE_CRLF(X) ? "\r\n" : \
322                              IS_NLTYPE_LF(X)   ? "\n"   : \
323                              IS_NLTYPE_CR(X)   ? "\r"   : \
324                              IS_NLTYPE_NONE(X) ? ""     : "\r\n")
325
326 struct mod_chxj_config {
327   int                   image;
328
329   char                  *device_data_file;
330   char                  *emoji_data_file;
331
332   char                  *image_cache_dir;
333   char                  *image_copyright;
334   unsigned long         image_cache_limit;
335   device_table_list     *devices;
336   emoji_t               *emoji;
337   emoji_t               *emoji_tail;
338   char                  *server_side_encoding;
339
340   char                  *dir; /* for LOG */
341
342   apr_array_header_t    *convrules;
343
344   char                  *cookie_db_dir;
345   long                  cookie_timeout;
346   cookie_store_type_t   cookie_store_type;
347   int                   cookie_lazy_mode;
348   char                  *cookie_dbm_type;
349
350 #if defined(USE_MYSQL_COOKIE)
351   mysql_t               mysql;
352 #endif
353 #if defined(USE_MEMCACHE_COOKIE)
354   memcache_t            memcache;
355 #endif
356   char                  *forward_url_base;  /* use input filter */
357   char                  *forward_server_ip; /* use input filter */
358
359   char                  *allowed_cookie_domain; /* default is null */
360
361   chxj_new_line_type_t  new_line_type;
362
363   char                  *post_log;              /* post log environment name. */
364 };
365
366 struct mod_chxj_req_config_t {
367   char               *user_agent;
368   device_table       *spec;
369   ap_filter_t        *f;
370   chxjconvrule_entry *entryp;
371 };
372
373 #define IS_COOKIE_STORE_DBM(X)      ((X) == COOKIE_STORE_TYPE_DBM)
374 #define IS_COOKIE_STORE_MYSQL(X)    ((X) == COOKIE_STORE_TYPE_MYSQL)
375 #define IS_COOKIE_STORE_MEMCACHE(X) ((X) == COOKIE_STORE_TYPE_MEMCACHE)
376 #define IS_COOKIE_STORE_NONE(X)     ((X) == COOKIE_STORE_TYPE_NONE)
377
378 #define COOKIE_LAZY_ON                (2)
379 #define COOKIE_LAZY_OFF               (1)
380 #define IS_COOKIE_LAZY(X)             ((X)->cookie_lazy_mode == COOKIE_LAZY_ON)
381
382
383 #define CONVRULE_ENGINE_ON_BIT        (0x00000001)
384 #define CONVRULE_ENGINE_OFF_BIT       (0x00000002)
385 #define CONVRULE_COOKIE_ON_BIT        (0x00000004)
386 #define CONVRULE_Z2H_ON_BIT           (0x00000008)
387 #define CONVRULE_Z2H_OFF_BIT          (0x00000010)
388 #define CONVRULE_Z2H_ALPHA_ON_BIT     (0x00000020)
389 #define CONVRULE_Z2H_ALPHA_OFF_BIT    (0x00000040)
390 #define CONVRULE_Z2H_NUM_ON_BIT       (0x00000080)
391 #define CONVRULE_Z2H_NUM_OFF_BIT      (0x00000100)
392 #define CONVRULE_QSCONV_OFF_BIT       (0x00000200)
393 #define CONVRULE_JRCONV_OFF_BIT       (0x00000400)
394 #define CONVRULE_NOCACHE_ON_BIT       (0x00000800)
395 #define CONVRULE_EMOJI_ONLY_BIT       (0x00001000)
396 #define CONVRULE_ENVINFO_ONLY_BIT     (0x00002000)
397 #define CONVRULE_COOKIE_ONLY_BIT      (0x00004000)
398 #define CONVRULE_OVERWRITE_X_CLIENT_TYPE_BIT (0x00008000)
399
400 #define CONVRULE_ENGINE_ON_CMD        "EngineOn"
401 #define CONVRULE_ENGINE_OFF_CMD       "EngineOff"
402 #define CONVRULE_COOKIE_ON_CMD        "CookieOn"
403 #define CONVRULE_COOKIE_OFF_CMD       "CookieOff"
404 #define CONVRULE_Z2H_ON_CMD           "Z2hOn"
405 #define CONVRULE_Z2H_OFF_CMD          "Z2hOff"
406 #define CONVRULE_Z2H_ALPHA_ON_CMD     "Z2hAlphaOn"
407 #define CONVRULE_Z2H_ALPHA_OFF_CMD    "Z2hAlphaOff"
408 #define CONVRULE_Z2H_NUM_ON_CMD       "Z2hNumOn"
409 #define CONVRULE_Z2H_NUM_OFF_CMD      "Z2hNumOff"
410 #define CONVRULE_Z2H_ALL_ON_CMD       "Z2hAllOn"
411 #define CONVRULE_Z2H_ALL_OFF_CMD      "Z2hAllOff"
412 #define CONVRULE_QSCONV_OFF_CMD       "QSConvOff"
413 #define CONVRULE_JRCONV_OFF_CMD       "JRConvOff"
414 #define CONVRULE_NOCACHE_ON_CMD       "NoCacheOn"
415 #define CONVRULE_EMOJI_ONLY_CMD       "EmojiOnly"
416 #define CONVRULE_ENVINFO_ONLY_CMD     "EnvInfoOnly"
417 #define CONVRULE_COOKIE_ONLY_CMD      "CookieOnly"
418 #define CONVRULE_OVERWRITE_X_CLIENT_TYPE_CMD "OverwriteXClientType"
419
420
421 #define CONVRULE_FLAG_NOTMATCH        (0x00000001)
422
423 #define CONVRULE_PC_FLAG_ON_CMD       "PC"
424
425 #define CONVRULE_PC_FLAG_ON_BIT       (0x00000001)
426 #define CONVRULE_PC_FLAG_OFF_BIT      (0x00000002)
427
428
429 typedef struct {
430   apr_global_mutex_t* cookie_db_lock;
431 } mod_chxj_global_config;
432
433 typedef struct {
434   apr_size_t len;
435   chxjconvrule_entry  *entryp;
436   device_table        *spec;
437
438   apr_bucket_brigade *bb;
439   apr_pool_t *pool;
440
441   char* buffer;
442 } mod_chxj_ctx;
443
444 #include "chxj_tag_util.h"
445
446 #define CHXJ_MOD_CONFIG_KEY   "chxj_module_key"
447
448 #define HTTP_USER_AGENT       "User-Agent"
449 #define HTTP_HOST             "Host"
450 #define CHXJ_HTTP_USER_AGENT  "CHXJ_HTTP_USER_AGENT"
451 #define CHXJ_HEADER_ORIG_CLIENT_IP "X-Chxj-Orig-Client-Ip"
452 #define CHXJ_HEADER_ORIG_CLIENT_TYPE "X-Chxj-Orig-Client-Type"
453
454 module AP_MODULE_DECLARE_DATA chxj_module;
455
456 #define CHXJ_IMG_ON     (2)
457 #define CHXJ_IMG_OFF    (1)
458 #define CHXJ_IMG_NONE   (0)
459
460
461 #define DBG(X,args...)  chxj_log_rerror(APLOG_MARK,APLOG_DEBUG,0,(request_rec*)(X),##args)
462 #define SDBG(X,Y)       chxj_log_error(APLOG_MARK,APLOG_DEBUG,0,(X),(Y))
463 #define PDBG(X,args...) chxj_log_perror(APLOG_MARK,APLOG_DEBUG,0,(apr_pool_t *)(X),##args)
464 #define ERR(X,args...)  chxj_log_rerror(APLOG_MARK,APLOG_ERR,0,(X), ##args)
465 #define SERR(X,args...) chxj_log_error(APLOG_MARK,APLOG_ERR,0,(X),##args)
466 #define PERR(X,args...) chxj_log_perror(APLOG_MARK,APLOG_ERR,0,(apr_pool_t *)(X),##args)
467 #define WRN(rec,format,args...)  chxj_log_rerror(APLOG_MARK,APLOG_WARNING,0,(rec),(format), ##args)
468
469 extern tag_handlers chxj_tag_handlers[];
470 extern tag_handler  chtml10_handler[];
471 extern tag_handler  chtml20_handler[];
472 extern tag_handler  chtml30_handler[];
473 extern tag_handler  chtml40_handler[];
474 extern tag_handler  chtml50_handler[];
475 extern tag_handler  xhtml_handler[];
476 extern tag_handler  hdml_handler[];
477 extern tag_handler  jhtml_handler[];
478 extern tag_handler  jxhtml_handler[];
479
480 extern char* chxj_node_convert( 
481   device_table *spec,
482   request_rec  *r,
483   void         *pdoc,
484   Doc          *doc,
485   Node         *node,
486   int          indent
487 );
488 extern char *chxj_node_convert_chxjif_only(
489   request_rec  *r,
490   device_table *spec,
491   const char   *src,
492   apr_size_t   *len
493 );
494
495 extern void chxj_remove_filter(request_rec *r);
496
497 #define IMAGE_CACHE_LIMIT_FMT_LEN  (20)
498
499
500 #if HAVE_MALLOC == 0
501 extern void *rpl_malloc(size_t n);
502 #endif
503
504
505 #define TO_ADDR(X) ((unsigned int)(apr_size_t)(X))
506 #endif
507 /*
508  * vim:ts=2 et
509  */