2 * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
3 * Copyright (C) 2005 QSDN,Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __MOD_CHXJ_H__
18 #define __MOD_CHXJ_H__
21 #define CHXJ_FALSE (0)
23 /* #define DEBUG_FD(X,args...) {FILE*fp=fopen("/tmp/error.log", "ab");fprintf(fp, X, ##args);fflush(fp);fclose(fp);} */
26 #elif defined(__GNUC__)
27 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
28 #elif defined(__LCLINT__)
29 # define UNUSED(x) /*@unused@*/ x
37 #include "chxj_apache.h"
39 #include "apr_strings.h"
40 #include "apr_buckets.h"
42 #include "apr_tables.h"
44 #include "apr_general.h"
45 #include "apr_pools.h"
48 #if defined(AP_NEED_SET_MUTEX_PERMS)
51 #if !defined(AP_NEED_SET_MUTEX_PERMS)
52 # if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
53 # define AP_NEED_SET_MUTEX_PERMS
57 #define STRCASEEQ(a,b,c,d) \
58 ((((a) == *(d))|| ((b) == *(d))) && strcasecmp((c),(d)) == 0)
59 #define STRNCASEEQ(a,b,c,d,e) \
60 ((((a) == *(d))|| ((b) == *(d))) && strncasecmp((c),(d),(e)) == 0)
62 #define CHXJ_QUERY_STRING_PARAM_PREFIX "_chxj_qs_"
63 #define CHXJ_QUERY_STRING_PARAM_PREFIX_ENC "%5Fchxj%5Fqs%5F"
66 #include "qs_ignore_sp.h"
68 #include "qs_malloc.h"
69 #include "qs_parse_attr.h"
70 #include "qs_parse_file.h"
71 #include "qs_parse_string.h"
72 #include "qs_parse_tag.h"
74 #if defined(HAVE_AP_REGEX_H) && HAVE_AP_REGEX_H == 1
75 # include "ap_regex.h"
77 # include "pcreposix.h"
79 # if !defined(AP_REG_ASSERT)
80 # define AP_REG_ASSERT REG_ASSERT
82 # if !defined(AP_REG_BADBR)
83 # define AP_REG_BADBR REG_BADBR
85 # if !defined(AP_REG_BADPAT)
86 # define AP_REG_BADPAT REG_BADPAT
88 # if !defined(AP_REG_BADRPT)
89 # define AP_REG_BADRPT REG_BADRPT
91 # if !defined(AP_REG_EBRACE)
92 # define AP_REG_EBRACE REG_EBRACE
94 # if !defined(AP_REG_EBRACK)
95 # define AP_REG_EBRACK REG_EBRACK
97 # if !defined(AP_REG_ECOLLATE)
98 # define AP_REG_ECOLLATE REG_ECOLLATE
100 # if !defined(AP_REG_ECTYPE)
101 # define AP_REG_ECTYPE REG_ECTYPE
103 # if !defined(AP_REG_EESCAPE)
104 # define AP_REG_EESCAPE REG_EESCAPE
106 # if !defined(AP_REG_EMPTY)
107 # define AP_REG_EMPTY REG_EMPTY
109 # if !defined(AP_REG_EPAREN)
110 # define AP_REG_EPAREN REG_EPAREN
112 # if !defined(AP_REG_ERANGE)
113 # define AP_REG_ERANGE REG_ERANGE
115 # if !defined(AP_REG_ESIZE)
116 # define AP_REG_ESIZE REG_ESIZE
118 # if !defined(AP_REG_ESPACE)
119 # define AP_REG_ESPACE REG_ESPACE
121 # if !defined(AP_REG_ESUBREG)
122 # define AP_REG_ESUBREG REG_ESUBREG
124 # if !defined(AP_REG_INVARG)
125 # define AP_REG_INVARG REG_INVARG
127 # if !defined(AP_REG_NOMATCH)
128 # define AP_REG_NOMATCH REG_NOMATCH
131 # if !defined(AP_REG_ICASE)
132 # define AP_REG_ICASE REG_ICASE
134 # if !defined(AP_REG_NEWLINE)
135 # define AP_REG_NEWLINE REG_NEWLINE
137 # if !defined(AP_REG_NOTBOL)
138 # define AP_REG_NOTBOL REG_NOTBOL
140 # if !defined(AP_REG_NOTEOL)
141 # define AP_REG_NOTEOL REG_NOTEOL
144 # if !defined(AP_REG_EXTENDED)
145 # define AP_REG_EXTENDED REG_EXTENDED
147 # if !defined(AP_REG_NOSUB)
148 # define AP_REG_NOSUB REG_NOSUB
151 # define ap_regex_t regex_t
152 # define ap_regmatch_t regmatch_t
158 #ifdef HAVE_APR_GLOBAL_MUTEX_H
159 # include "apr_global_mutex.h"
162 #ifdef HAVE_APR_SHM_H
163 # include "apr_shm.h"
167 typedef struct imode_emoji_t imode_emoji_t;
169 struct imode_emoji_t {
177 typedef struct ezweb_emoji_t ezweb_emoji_t;
179 struct ezweb_emoji_t {
186 typedef struct jphone_emoji_t jphone_emoji_t;
188 struct jphone_emoji_t {
192 typedef struct iphone_emoji_t iphone_emoji_t;
194 struct iphone_emoji_t {
198 typedef struct android_emoji_t android_emoji_t;
200 struct android_emoji_t {
204 typedef struct emoji_t emoji_t;
207 struct emoji_t* next;
209 imode_emoji_t* imode;
210 ezweb_emoji_t* ezweb;
211 jphone_emoji_t* jphone;
212 iphone_emoji_t* iphone;
213 android_emoji_t* android;
216 typedef struct chxjconvrule_entry chxjconvrule_entry;
218 struct chxjconvrule_entry {
228 typedef struct tag_handler tag_handler;
231 char* (*start_tag_handler)(void* doc, Node* node);
232 char* (*end_tag_handler)(void* doc, Node* node);
235 #include "chxj_specified_device.h"
239 tag_handler* handler;
298 tagNLMARK, /* New Line Code */
304 typedef struct mod_chxj_config mod_chxj_config;
305 typedef struct mod_chxj_req_config_t mod_chxj_req_config;
307 #if defined(USE_MYSQL_COOKIE)
308 # include "chxj_mysql.h"
310 #if defined(USE_MEMCACHE_COOKIE)
311 # include "chxj_memcache.h"
314 /* cookie store type */
315 #define CHXJ_COOKIE_STORE_TYPE_DBM "dbm"
316 #define CHXJ_COOKIE_STORE_TYPE_MYSQL "mysql"
317 #define CHXJ_COOKIE_STORE_TYPE_MEMCACHE "memcache"
319 COOKIE_STORE_TYPE_NONE = 0,
320 COOKIE_STORE_TYPE_DBM = 1,
321 COOKIE_STORE_TYPE_MYSQL = 2,
322 COOKIE_STORE_TYPE_MEMCACHE = 3,
323 } cookie_store_type_t;
326 #define CHXJ_NEW_LINE_TYPE_CRLF "crlf"
327 #define CHXJ_NEW_LINE_TYPE_LF "lf"
328 #define CHXJ_NEW_LINE_TYPE_CR "cr"
329 #define CHXJ_NEW_LINE_TYPE_NONE "none"
336 } chxj_new_line_type_t;
337 #define IS_NLTYPE_CRLF(X) ((X)->new_line_type == NLTYPE_CRLF)
338 #define IS_NLTYPE_LF(X) ((X)->new_line_type == NLTYPE_LF)
339 #define IS_NLTYPE_CR(X) ((X)->new_line_type == NLTYPE_CR)
340 #define IS_NLTYPE_NONE(X) ((X)->new_line_type == NLTYPE_NONE)
341 #define TO_NLCODE(X) (IS_NLTYPE_CRLF(X) ? "\r\n" : \
342 IS_NLTYPE_LF(X) ? "\n" : \
343 IS_NLTYPE_CR(X) ? "\r" : \
344 IS_NLTYPE_NONE(X) ? "" : "\r\n")
346 struct mod_chxj_config {
349 char *device_data_file;
350 char *emoji_data_file;
352 char *image_cache_dir;
353 char *image_copyright;
354 unsigned long image_cache_limit;
355 device_table_list *devices;
358 int imode_emoji_color;
359 char *server_side_encoding;
361 char *dir; /* for LOG */
363 apr_array_header_t *convrules;
367 cookie_store_type_t cookie_store_type;
368 int cookie_lazy_mode;
369 char *cookie_dbm_type;
371 int detect_device_type; /* XML|TSV */
373 #if defined(USE_MYSQL_COOKIE)
376 #if defined(USE_MEMCACHE_COOKIE)
379 char *forward_url_base; /* use input filter */
380 char *forward_server_ip; /* use input filter */
382 char *allowed_cookie_domain; /* default is null */
384 chxj_new_line_type_t new_line_type;
386 char *post_log; /* post log environment name. */
388 apr_array_header_t *device_keys; /* TSV header array */
389 apr_hash_t *device_hash; /* TSV device data hash table */
392 char *image_rewrite_url;
393 int image_rewrite_mode;
396 char *emoji_image_url;
399 int use_google_analytics;
400 char *google_analytics_account;
401 char *google_analytics_target; /* i.e. /ga.pl */
402 char *google_analytics_debug; /* debug filename */
405 struct mod_chxj_req_config_t {
409 chxjconvrule_entry *entryp;
412 #define IS_COOKIE_STORE_DBM(X) ((X) == COOKIE_STORE_TYPE_DBM)
413 #define IS_COOKIE_STORE_MYSQL(X) ((X) == COOKIE_STORE_TYPE_MYSQL)
414 #define IS_COOKIE_STORE_MEMCACHE(X) ((X) == COOKIE_STORE_TYPE_MEMCACHE)
415 #define IS_COOKIE_STORE_NONE(X) ((X) == COOKIE_STORE_TYPE_NONE)
417 #define COOKIE_LAZY_ON (2)
418 #define COOKIE_LAZY_OFF (1)
419 #define IS_COOKIE_LAZY(X) ((X)->cookie_lazy_mode == COOKIE_LAZY_ON)
422 #define CONVRULE_ENGINE_ON_BIT (0x00000001)
423 #define CONVRULE_ENGINE_OFF_BIT (0x00000002)
424 #define CONVRULE_COOKIE_ON_BIT (0x00000004)
425 #define CONVRULE_CSS_ON_BIT (0x10000000)
426 #define CONVRULE_Z2H_ON_BIT (0x00000008)
427 #define CONVRULE_Z2H_OFF_BIT (0x00000010)
428 #define CONVRULE_Z2H_ALPHA_ON_BIT (0x00000020)
429 #define CONVRULE_Z2H_ALPHA_OFF_BIT (0x00000040)
430 #define CONVRULE_Z2H_NUM_ON_BIT (0x00000080)
431 #define CONVRULE_Z2H_NUM_OFF_BIT (0x00000100)
432 #define CONVRULE_QSCONV_OFF_BIT (0x00000200)
433 #define CONVRULE_JRCONV_OFF_BIT (0x00000400)
434 #define CONVRULE_NOCACHE_ON_BIT (0x00000800)
435 #define CONVRULE_EMOJI_ONLY_BIT (0x00001000)
436 #define CONVRULE_ENVINFO_ONLY_BIT (0x00002000)
437 #define CONVRULE_COOKIE_ONLY_BIT (0x00004000)
438 #define CONVRULE_OVERWRITE_X_CLIENT_TYPE_BIT (0x00008000)
440 #define CONVRULE_ENGINE_ON_CMD "EngineOn"
441 #define CONVRULE_ENGINE_OFF_CMD "EngineOff"
442 #define CONVRULE_COOKIE_ON_CMD "CookieOn"
443 #define CONVRULE_COOKIE_OFF_CMD "CookieOff"
444 #define CONVRULE_CSS_ON_CMD "CssOn"
445 #define CONVRULE_CSS_OFF_CMD "CssOff"
446 #define CONVRULE_Z2H_ON_CMD "Z2hOn"
447 #define CONVRULE_Z2H_OFF_CMD "Z2hOff"
448 #define CONVRULE_Z2H_ALPHA_ON_CMD "Z2hAlphaOn"
449 #define CONVRULE_Z2H_ALPHA_OFF_CMD "Z2hAlphaOff"
450 #define CONVRULE_Z2H_NUM_ON_CMD "Z2hNumOn"
451 #define CONVRULE_Z2H_NUM_OFF_CMD "Z2hNumOff"
452 #define CONVRULE_Z2H_ALL_ON_CMD "Z2hAllOn"
453 #define CONVRULE_Z2H_ALL_OFF_CMD "Z2hAllOff"
454 #define CONVRULE_QSCONV_OFF_CMD "QSConvOff"
455 #define CONVRULE_JRCONV_OFF_CMD "JRConvOff"
456 #define CONVRULE_NOCACHE_ON_CMD "NoCacheOn"
457 #define CONVRULE_EMOJI_ONLY_CMD "EmojiOnly"
458 #define CONVRULE_ENVINFO_ONLY_CMD "EnvInfoOnly"
459 #define CONVRULE_COOKIE_ONLY_CMD "CookieOnly"
460 #define CONVRULE_OVERWRITE_X_CLIENT_TYPE_CMD "OverwriteXClientType"
463 #define CONVRULE_FLAG_NOTMATCH (0x00000001)
465 #define CONVRULE_PC_FLAG_ON_CMD "PC"
467 #define CONVRULE_PC_FLAG_ON_BIT (0x00000001)
468 #define CONVRULE_PC_FLAG_OFF_BIT (0x00000002)
471 #define IS_CSS_ON(X) ((X)->action & CONVRULE_CSS_ON_BIT)
474 apr_global_mutex_t* cookie_db_lock;
475 } mod_chxj_global_config;
479 chxjconvrule_entry *entryp;
482 apr_bucket_brigade *bb;
488 #include "chxj_tag_util.h"
490 #define CHXJ_MOD_CONFIG_KEY "chxj_module_key"
492 #define HTTP_USER_AGENT "User-Agent"
493 #define HTTP_HOST "Host"
494 #define CHXJ_HTTP_USER_AGENT "CHXJ_HTTP_USER_AGENT"
495 #define CHXJ_HEADER_ORIG_CLIENT_IP "X-Chxj-Orig-Client-Ip"
496 #define CHXJ_HEADER_ORIG_CLIENT_TYPE "X-Chxj-Orig-Client-Type"
498 module AP_MODULE_DECLARE_DATA chxj_module;
500 #define CHXJ_IMG_ON (2)
501 #define CHXJ_IMG_OFF (1)
502 #define CHXJ_IMG_NONE (0)
504 #define CHXJ_IMODE_EMOJI_COLOR_ON (3)
505 #define CHXJ_IMODE_EMOJI_COLOR_AUTO (2)
506 #define CHXJ_IMODE_EMOJI_COLOR_OFF (1)
507 #define CHXJ_IMODE_EMOJI_COLOR_NONE (0)
509 #define CHXJ_ADD_DETECT_DEVICE_TYPE_TSV (1)
510 #define CHXJ_ADD_DETECT_DEVICE_TYPE_NONE (0)
512 #define CHXJ_PROVIDER_UNKNOWN (0)
513 #define CHXJ_PROVIDER_DOCOMO (1)
514 #define CHXJ_PROVIDER_AU (2)
515 #define CHXJ_PROVIDER_SOFTBANK (3)
517 #define CHXJ_IMG_REWRITE_ON (2)
518 #define CHXJ_IMG_REWRITE_OFF (1)
519 #define CHXJ_IMG_REWRITE_NONE (0)
521 #define CHXJ_IMG_REWRITE_MODE_ALL (3)
522 #define CHXJ_IMG_REWRITE_MODE_USER (2)
523 #define CHXJ_IMG_REWRITE_MODE_TAG (1)
524 #define CHXJ_IMG_REWRITE_MODE_NONE (0)
526 #define CHXJ_IMG_REWRITE_URL_STRING "_x-chxj_imgrewrite=on"
527 #define CHXJ_IMG_X_HTTP_IMAGE_FILENAME "X-Chxj-Image-Filename"
528 #define CHXJ_IMG_X_HTTP_IMAGE_TYPE "X-Chxj-Image-Type"
530 #define DBG(X,args...) chxj_log_rerror(APLOG_MARK,APLOG_DEBUG,0,(request_rec*)(X),##args)
531 #define SDBG(X,Y) chxj_log_error(APLOG_MARK,APLOG_DEBUG,0,(X),(Y))
532 #define PDBG(X,args...) chxj_log_perror(APLOG_MARK,APLOG_DEBUG,0,(apr_pool_t *)(X),##args)
533 #define ERR(X,args...) chxj_log_rerror(APLOG_MARK,APLOG_ERR,0,(X), ##args)
534 #define SERR(X,args...) chxj_log_error(APLOG_MARK,APLOG_ERR,0,(X),##args)
535 #define PERR(X,args...) chxj_log_perror(APLOG_MARK,APLOG_ERR,0,(apr_pool_t *)(X),##args)
536 #define WRN(rec,format,args...) chxj_log_rerror(APLOG_MARK,APLOG_WARNING,0,(rec),(format), ##args)
538 extern tag_handlers chxj_tag_handlers[];
539 extern tag_handler chtml10_handler[];
540 extern tag_handler chtml20_handler[];
541 extern tag_handler chtml30_handler[];
542 extern tag_handler chtml40_handler[];
543 extern tag_handler chtml50_handler[];
544 extern tag_handler ixhtml10_handler[];
545 extern tag_handler xhtml_handler[];
546 extern tag_handler hdml_handler[];
547 extern tag_handler jhtml_handler[];
548 extern tag_handler jxhtml_handler[];
549 extern tag_handler iphone_handler[];
550 extern tag_handler android_handler[];
552 extern char* chxj_node_convert(
560 extern char *chxj_node_convert_chxjif_only(
566 extern void chxj_remove_filter(request_rec *r);
567 extern mod_chxj_req_config *chxj_get_req_config(request_rec *r);
569 #define IMAGE_CACHE_LIMIT_FMT_LEN (20)
573 extern void *rpl_malloc(size_t n);
577 #define TO_ADDR(X) ((unsigned int)(apr_size_t)(X))
578 #define ADJUST_WIDTH_FOR_ANDROID (40)
580 #if APR_MAJOR_VERSION < 1
581 #ifndef APR_FOPEN_READ
582 #define APR_FOPEN_READ APR_READ
584 #ifndef APR_FOPEN_BUFFERED
585 #define APR_FOPEN_BUFFERED APR_BUFFERED
587 #ifndef APR_FOPEN_BINARY
588 #define APR_FOPEN_BINARY APR_BINARY
590 #ifndef APR_FOPEN_CREATE
591 #define APR_FOPEN_CREATE APR_CREATE
593 #ifndef APR_FOPEN_WRITE
594 #define APR_FOPEN_WRITE APR_WRITE