2 * Copyright (C) 2005-2009 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.
23 #include "http_config.h"
24 #include "http_core.h"
25 #include "http_protocol.h"
27 #include "ap_config.h"
28 #include "apr_strings.h"
29 #include "util_filter.h"
30 #include "apr_buckets.h"
32 #include "apr_tables.h"
34 #include "apr_general.h"
35 #include "apr_pools.h"
38 #include "chxj_encoding.h"
39 #include "qs_ignore_sp.h"
41 #include "qs_malloc.h"
42 #include "qs_parse_attr.h"
43 #include "qs_parse_file.h"
44 #include "qs_parse_string.h"
45 #include "qs_parse_tag.h"
46 #include "chxj_load_device_data.h"
47 #include "chxj_load_emoji_data.h"
48 #include "chxj_specified_device.h"
49 #include "chxj_tag_util.h"
50 #include "chxj_xhtml_mobile_1_0.h"
51 #include "chxj_hdml.h"
52 #include "chxj_chtml10.h"
53 #include "chxj_chtml20.h"
54 #include "chxj_chtml30.h"
55 #include "chxj_chtml40.h"
56 #include "chxj_chtml50.h"
57 #include "chxj_jhtml.h"
58 #include "chxj_jxhtml.h"
59 #include "chxj_img_conv_format.h"
60 #include "chxj_qr_code.h"
61 #include "chxj_encoding.h"
62 #include "chxj_apply_convrule.h"
63 #include "chxj_cookie.h"
64 #include "chxj_url_encode.h"
65 #include "chxj_str_util.h"
66 #if defined(USE_MYSQL_COOKIE)
67 # include "chxj_mysql.h"
69 #include "chxj_serf.h"
70 #include "chxj_add_device_env.h"
71 #include "chxj_header_inf.h"
72 #include "chxj_jreserved_tag.h"
75 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
76 #define CHXJ_VERSION PACKAGE_VERSION
77 #define CHXJ_POST_MAX (0x1000000)
79 converter_t convert_routine[] = {
81 /* CHXJ_SPEC_UNKNOWN */
84 .emoji_only_converter = NULL,
87 /* CHXJ_SPEC_Chtml_1_0 */
88 .converter = chxj_convert_chtml10,
89 .encoder = chxj_encoding,
90 .emoji_only_converter = chxj_chtml10_emoji_only_converter,
93 /* CHXJ_SPEC_Chtml_2_0 */
94 .converter = chxj_convert_chtml20,
95 .encoder = chxj_encoding,
96 .emoji_only_converter = chxj_chtml20_emoji_only_converter,
99 /* CHXJ_SPEC_Chtml_3_0 */
100 .converter = chxj_convert_chtml30,
101 .encoder = chxj_encoding,
102 .emoji_only_converter = chxj_chtml30_emoji_only_converter,
105 /* CHXJ_SPEC_Chtml_4_0 */
106 .converter = chxj_convert_chtml40,
107 .encoder = chxj_encoding,
108 .emoji_only_converter = chxj_chtml40_emoji_only_converter,
111 /* CHXJ_SPEC_Chtml_5_0 */
112 .converter = chxj_convert_chtml50,
113 .encoder = chxj_encoding,
114 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
117 /* CHXJ_SPEC_Chtml_6_0 */
118 .converter = chxj_convert_chtml50,
119 .encoder = chxj_encoding,
120 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
123 /* CHXJ_SPEC_Chtml_7_0 */
124 .converter = chxj_convert_chtml50,
125 .encoder = chxj_encoding,
126 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
129 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
130 .converter = chxj_convert_xhtml_mobile_1_0,
131 .encoder = chxj_encoding,
132 .emoji_only_converter = chxj_xhtml_emoji_only_converter,
136 .converter = chxj_convert_hdml,
137 .encoder = chxj_encoding,
138 .emoji_only_converter = NULL,
141 /* CHXJ_SPEC_Jhtml */
142 .converter = chxj_convert_jhtml,
143 .encoder = chxj_encoding,
144 .emoji_only_converter = chxj_jhtml_emoji_only_converter,
147 /* CHXJ_SPEC_Jxhtml */
148 .converter = chxj_convert_jxhtml,
149 .encoder = chxj_encoding,
150 .emoji_only_converter = chxj_jxhtml_emoji_only_converter,
156 .emoji_only_converter = NULL,
160 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec);
161 static void s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec);
162 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
163 static void s_clear_cookie_header(request_rec *r, device_table *spec);
164 static void s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp);
167 * Only when User-Agent is specified, the User-Agent header is camouflaged.
172 chxj_headers_fixup(request_rec *r)
174 mod_chxj_config* dconf;
175 chxjconvrule_entry* entryp;
181 DBG(r, "REQ[%X] start chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
183 DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)(apr_size_t)r);
184 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
188 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
190 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
191 spec = chxj_specified_device(r, user_agent);
193 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
195 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
196 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)(apr_size_t)r);
197 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
202 switch(spec->html_spec_type) {
203 case CHXJ_SPEC_Chtml_1_0:
204 case CHXJ_SPEC_Chtml_2_0:
205 case CHXJ_SPEC_Chtml_3_0:
206 case CHXJ_SPEC_Chtml_4_0:
207 case CHXJ_SPEC_Chtml_5_0:
208 case CHXJ_SPEC_Chtml_6_0:
209 case CHXJ_SPEC_Chtml_7_0:
210 case CHXJ_SPEC_XHtml_Mobile_1_0:
212 case CHXJ_SPEC_Jhtml:
213 case CHXJ_SPEC_Jxhtml:
214 entryp = chxj_apply_convrule(r, dconf->convrules);
216 DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int)(apr_size_t)r);
219 if (!(entryp->action & CONVRULE_ENGINE_ON_BIT) && !(entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
220 DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int)(apr_size_t)r);
223 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
224 DBG(r, "REQ[%X] end chxj_headers_fixup() (emoji only)", (unsigned int)(apr_size_t)r);
228 apr_table_setn(r->headers_in,
229 CHXJ_HTTP_USER_AGENT,
232 if (entryp->user_agent)
233 apr_table_setn(r->headers_in,
237 * this clear process must do before chxj_convert_input_header function.
239 if ((entryp->action & CONVRULE_COOKIE_ON_BIT) || (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
240 if (r->method_number == M_POST) {
241 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
242 s_clear_cookie_header(r, spec);
246 s_clear_cookie_header(r, spec);
250 chxj_convert_input_header(r,entryp, spec);
255 DBG(r, "REQ[%X] end chxj_headers_fixup() (not mobile) spec->device_name[%s] User-Agent:[%s]", (unsigned int)(apr_size_t)r, spec->device_name, user_agent);
261 if (r->method_number == M_POST) {
262 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
263 DBG(r, "REQ[%X] set Input handler old:[%s] proxyreq:[%d] uri:[%s] filename:[%s]", (unsigned int)(apr_size_t)r, r->handler, r->proxyreq, r->uri, r->filename);
264 r->proxyreq = PROXYREQ_NONE;
265 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
268 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
270 if (dconf->post_log) {
271 apr_table_setn(r->subprocess_env, dconf->post_log, dconf->post_log);
274 apr_table_setn(r->subprocess_env, "chxj-post-log", "chxj-post-log");
276 apr_sockaddr_t *address = NULL;
277 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
278 if (rv != APR_SUCCESS) {
280 ERR(r, "REQ[%X] %s:%d apr_sockaddr_info_get() failed: rv:[%d|%s]", (unsigned int)(apr_size_t)r, APLOG_MARK, rv, apr_strerror(rv, buf, 256));
281 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
285 if (dconf->forward_server_ip) {
286 addr = dconf->forward_server_ip;
289 apr_sockaddr_ip_get(&addr, address);
291 DBG(r, "REQ[%X] Client IP:[%s] vs Orig Client IP:[%s] vs Server IP:[%s]", (unsigned int)(apr_size_t)r, r->connection->remote_ip, client_ip, addr);
292 if (strcmp(addr, r->connection->remote_ip) == 0) {
293 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
294 /* For mod_cidr_lookup */
295 if (entryp->action & CONVRULE_OVERWRITE_X_CLIENT_TYPE_BIT) {
296 char *client_type = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_TYPE);
297 DBG(r, "REQ[%X] Overwrite X-Client-Type to [%s]", (unsigned int)(apr_size_t)r, client_type);
298 apr_table_setn(r->subprocess_env, "X_CLIENT_TYPE", client_type);
299 apr_table_setn(r->headers_in, "X-Client-Type", client_type);
302 if (! apr_table_get(r->headers_in, "Content-Length")) {
303 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
305 apr_table_set(r->headers_in, "Content-Length", contentLength);
312 chxj_add_device_env(r, spec);
314 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
321 s_clear_cookie_header(request_rec *r, device_table *spec)
323 DBG(r, "REQ[%X] start s_clear_cookie_header()", TO_ADDR(r));
324 switch(spec->html_spec_type) {
325 case CHXJ_SPEC_Chtml_1_0:
326 case CHXJ_SPEC_Chtml_2_0:
327 case CHXJ_SPEC_Chtml_3_0:
328 case CHXJ_SPEC_Chtml_4_0:
329 case CHXJ_SPEC_Chtml_5_0:
330 case CHXJ_SPEC_Chtml_6_0:
331 case CHXJ_SPEC_Chtml_7_0:
332 case CHXJ_SPEC_XHtml_Mobile_1_0:
333 case CHXJ_SPEC_Jhtml:
334 case CHXJ_SPEC_Jxhtml:
335 apr_table_unset(r->headers_in, "Cookie");
340 DBG(r, "REQ[%X] end s_clear_cookie_header()", TO_ADDR(r));
345 * It converts it from CHTML into XXML corresponding to each model.
348 * @param src [i] It is former HTML character string.
349 * @param len [i/o] It is length of former HTML character string.
352 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
358 mod_chxj_config *dconf;
359 chxjconvrule_entry *entryp;
361 DBG(r,"REQ[%X] start of chxj_convert() input:[%.*s]", (unsigned int)(apr_size_t)r, (int)*len, *src);
362 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
364 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
367 entryp = chxj_apply_convrule(r, dconf->convrules);
369 if (!entryp || (!(entryp->action & CONVRULE_ENGINE_ON_BIT) && !(entryp->action & CONVRULE_COOKIE_ONLY_BIT))) {
370 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
375 /*------------------------------------------------------------------------*/
376 /* get UserAgent from http header */
377 /*------------------------------------------------------------------------*/
378 if (entryp->user_agent)
379 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
381 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
383 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
384 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
387 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
388 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
389 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
390 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
394 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
396 spec = chxj_specified_device(r, user_agent);
403 if ((entryp->action & CONVRULE_COOKIE_ON_BIT
404 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
405 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT))
406 || (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
407 switch(spec->html_spec_type) {
408 case CHXJ_SPEC_Chtml_1_0:
409 case CHXJ_SPEC_Chtml_2_0:
410 case CHXJ_SPEC_Chtml_3_0:
411 case CHXJ_SPEC_Chtml_4_0:
412 case CHXJ_SPEC_Chtml_5_0:
413 case CHXJ_SPEC_Chtml_6_0:
414 case CHXJ_SPEC_Chtml_7_0:
415 case CHXJ_SPEC_XHtml_Mobile_1_0:
416 case CHXJ_SPEC_Jhtml:
417 case CHXJ_SPEC_Jxhtml:
418 cookie = chxj_save_cookie(r);
425 if (!r->header_only) {
426 if ((entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
428 dst = chxj_cookie_only_mode(r, *src, (apr_size_t *)len, cookie);
436 if (convert_routine[spec->html_spec_type].encoder)
437 tmp = convert_routine[spec->html_spec_type].encoder(r,
441 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
443 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
446 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
448 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
449 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
454 dst = apr_palloc(r->pool, 1);
459 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
462 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
463 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
464 if (convert_routine[spec->html_spec_type].converter) {
466 dst = convert_routine[spec->html_spec_type].converter(r,
474 dst = convert_routine[spec->html_spec_type].converter(r,
485 ap_set_content_length(r, *len);
488 dst = apr_psprintf(r->pool, "\n");
497 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
504 * convert GUID parameter.
508 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
510 DBG(r, "REQ[%X] start s_convert_guid_parameter() param:[%s]", (unsigned int)(apr_size_t)r, param);
511 if (strcasecmp(param, "guid") == 0) {
512 switch(spec->html_spec_type) {
513 case CHXJ_SPEC_XHtml_Mobile_1_0:
515 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
517 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
521 case CHXJ_SPEC_Jhtml:
522 case CHXJ_SPEC_Jxhtml:
524 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
526 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
534 DBG(r, "REQ[%X] end s_convert_guid_parameter()", (unsigned int)(apr_size_t)r);
538 * It converts it from HEADER.
543 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
555 cookie_t *cookie = NULL;
556 int no_update_flag = 0;
558 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
561 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
562 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
565 urilen = strlen(r->args);
567 result = qs_alloc_zero_byte_string(r->pool);
569 buff_pre = apr_pstrdup(r->pool, r->args);
574 pair = apr_strtok(buff_pre, "&", &pstate);
580 pair_sv = apr_pstrdup(r->pool, pair);
582 name = apr_strtok(pair, "=", &vstate);
583 value = apr_strtok(NULL, "=", &vstate);
584 if (! name) continue;
585 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
586 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
591 buff = apr_pstrdup(r->pool, r->args);
592 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
601 pair = apr_strtok(buff, "&", &pstate);
607 pair_sv = apr_pstrdup(r->pool, pair);
609 name = apr_strtok(pair, "=", &vstate);
610 value = apr_strtok(NULL, "=", &vstate);
611 if (! name) continue;
612 name = chxj_safe_to_jreserved_tag(r, name);
613 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
614 if (strlen(result) != 0)
615 result = apr_pstrcat(r->pool, result, "&", NULL);
617 if (strcasecmp(entryp->encoding, "NONE") != 0) {
622 if (value && *value != 0) {
623 value = chxj_url_decode(r->pool, value);
624 dlen = strlen(value);
625 DBG(r, "************ before encoding[%s]", value);
627 dvalue = chxj_rencoding(r, value, &dlen);
628 dvalue = chxj_url_encode(r->pool, dvalue);
630 DBG(r, "************ after encoding[%s]", dvalue);
636 if (name && *name != 0) {
637 name = chxj_url_decode(r->pool, name);
639 dname = chxj_rencoding(r, name, &dlen);
640 dname = chxj_url_encode(r->pool, dname);
645 s_convert_guid_parameter_to_header(r, dname, spec);
646 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
649 s_convert_guid_parameter_to_header(r, name, spec);
650 if (strcmp(name, pair_sv) != 0) {
651 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
654 result = apr_pstrcat(r->pool, result, name, NULL);
659 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
660 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
661 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
665 if (strlen(value) == 0)
668 if (strlen(result) != 0)
669 result = apr_pstrcat(r->pool, result, "&", NULL);
671 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
674 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
676 apr_table_unset(r->headers_in, "Cookie");
677 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
678 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
679 cookie_lock_t *lock = chxj_cookie_lock(r);
680 cookie = chxj_load_cookie(r, value);
681 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
682 if (! no_update_flag && cookie) {
683 cookie = chxj_update_cookie(r, cookie);
685 chxj_cookie_unlock(r, lock);
687 if (cookie && cookie->cookie_id) {
688 if (strlen(result) != 0)
689 result = apr_pstrcat(r->pool, result, "&", NULL);
690 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
694 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
695 if (strlen(result) != 0)
696 result = apr_pstrcat(r->pool, result, "&", NULL);
697 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
700 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
701 if (! no_update_flag) {
702 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
706 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
707 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
713 * It converts it from POSTDATA .
716 * @param src [i] It is POSTDATA character string.
717 * @param len [i/o] It is length of former HTML character string.
724 chxjconvrule_entry *entryp,
734 cookie_t *cookie = NULL;
736 int no_update_flag = 0;
741 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
744 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
745 return apr_pstrdup(r->pool, "");
748 apr_pool_create(&pool, r->pool);
750 s = apr_pstrdup(pool, *src);
752 buff_pre = apr_pstrdup(pool, *src);
754 result = qs_alloc_zero_byte_string(pool);
756 DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
757 DBG(r, "REQ[%X] | BEFORE input convert source |", (unsigned int)(apr_size_t)r);
758 DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
759 for (ii=0; ii<ilen-64; ii+=64) {
760 DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
765 if (ilen >= 64 && ((ilen-64) % 64 != 0)) {
766 DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
768 DBG(r, "REQ[%X] +--------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
773 pair = apr_strtok(buff_pre, "&", &pstate);
779 pair_sv = apr_pstrdup(pool, pair);
781 name = apr_strtok(pair, "=", &vstate);
782 value = apr_strtok(NULL, "=", &vstate);
783 if (! name) continue;
784 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
785 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
795 pair = apr_strtok(s, "&", &pstate);
800 name = apr_strtok(pair, "=", &vstate);
801 value = apr_strtok(NULL, "=", &vstate);
802 if (! name) continue;
803 name = chxj_safe_to_jreserved_tag(r, name);
805 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
806 if (strlen(result) != 0)
807 result = apr_pstrcat(pool, result, "&", NULL);
809 if (strcasecmp(entryp->encoding, "NONE") != 0) {
814 if (value && *value != 0) {
815 value = chxj_url_decode(pool, value);
816 dlen = strlen(value);
817 dvalue = chxj_rencoding(r, value, &dlen);
818 dvalue = chxj_url_encode(pool, dvalue);
824 if (name && *name != 0) {
825 name = chxj_url_decode(pool, name);
827 dname = chxj_rencoding(r, name, &dlen);
828 dname = chxj_url_encode(pool, dname);
835 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
838 result = apr_pstrcat(pool, result, name, "=", value, NULL);
842 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
843 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
844 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
848 if (strlen(value) == 0)
851 if (strlen(result) != 0)
852 result = apr_pstrcat(pool, result, "&", NULL);
854 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
858 dlen = strlen(value);
859 value = chxj_url_decode(pool, value);
860 dvalue = chxj_rencoding(r, value, &dlen);
861 dvalue = chxj_url_encode(pool,dvalue);
862 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
866 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
870 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
872 apr_table_unset(r->headers_in, "Cookie");
873 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
874 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
875 cookie_lock_t *lock = chxj_cookie_lock(r);
876 cookie = chxj_load_cookie(r, value);
877 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
878 if (! no_update_flag && cookie) {
879 cookie = chxj_update_cookie(r, cookie);
881 chxj_cookie_unlock(r, lock);
883 if (cookie && cookie->cookie_id) {
884 if (strlen(result) != 0)
885 result = apr_pstrcat(pool, result, "&", NULL);
886 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
890 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
891 if (strlen(result) != 0)
892 result = apr_pstrcat(pool, result, "&", NULL);
893 result = apr_pstrcat(pool, result, name, "=", value, NULL);
896 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
899 dlen = strlen(value);
901 value = chxj_url_decode(pool, value);
902 dvalue = chxj_rencoding(r, value, &dlen);
903 dvalue = chxj_url_encode(pool,dvalue);
904 if (r->args && strlen(r->args) > 0) {
905 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
908 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
910 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
914 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
917 dlen = strlen(value);
919 value = chxj_url_decode(pool, value);
920 dvalue = chxj_rencoding(r, value, &dlen);
921 dvalue = chxj_url_encode(pool,dvalue);
922 if (r->args && strlen(r->args) > 0) {
923 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
926 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
928 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
931 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
933 *len = strlen(result);
934 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
935 if (! no_update_flag) {
936 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
939 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
940 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
947 * The received data is returned to the filter.
949 * @param f [i/o] It is a filter.
950 * @param data [i] It is data returned to the filter.
951 * @param len [i] It is length of the data returned to the filter.
954 pass_data_to_filter(ap_filter_t *f, const char *data,
957 request_rec *r = f->r;
958 conn_rec *c = r->connection;
960 apr_bucket_brigade *bb;
963 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
965 chxj_header_inf_clear(r);
967 bb = apr_brigade_create(r->pool, c->bucket_alloc);
968 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
970 APR_BRIGADE_INSERT_TAIL(bb, b);
971 b = apr_bucket_eos_create(f->c->bucket_alloc);
972 APR_BRIGADE_INSERT_TAIL(bb, b);
974 rv = ap_pass_brigade(f->next, bb);
975 if (rv != APR_SUCCESS) {
976 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
980 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
987 * Add No Cache Header
990 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
992 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
993 apr_table_unset(r->headers_out, "Pragma");
994 apr_table_unset(r->err_headers_out, "Pragma");
995 apr_table_unset(r->headers_out, "Expires");
996 apr_table_unset(r->err_headers_out, "Expires");
997 apr_table_unset(r->headers_out, "Cache-Control");
998 apr_table_unset(r->err_headers_out, "Cache-Control");
1000 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
1001 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
1002 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
1008 * It is the main loop of the output filter.
1010 * @param f [i/o] It is a filter.
1014 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
1020 char *user_agent = NULL;
1022 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
1023 cookie_t *cookie = NULL;
1024 mod_chxj_config *dconf;
1025 chxjconvrule_entry *entryp = NULL;
1026 device_table *spec = NULL;
1030 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1033 apr_pool_create(&pool, r->pool);
1035 entryp = ctx->entryp;
1037 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1040 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1041 DBG(r, "REQ[%X] found Location header", TO_ADDR(r));
1042 if ((entryp->action & CONVRULE_COOKIE_ON_BIT) || (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
1043 cookie_lock_t *lock = NULL;
1044 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", TO_ADDR(r));
1045 switch(spec->html_spec_type) {
1046 case CHXJ_SPEC_Chtml_1_0:
1047 case CHXJ_SPEC_Chtml_2_0:
1048 case CHXJ_SPEC_Chtml_3_0:
1049 case CHXJ_SPEC_Chtml_4_0:
1050 case CHXJ_SPEC_Chtml_5_0:
1051 case CHXJ_SPEC_Chtml_6_0:
1052 case CHXJ_SPEC_Chtml_7_0:
1053 case CHXJ_SPEC_XHtml_Mobile_1_0:
1054 case CHXJ_SPEC_Jhtml:
1055 case CHXJ_SPEC_Jxhtml:
1056 lock = chxj_cookie_lock(r);
1057 cookie = chxj_save_cookie(r);
1058 s_add_cookie_id_if_has_location_header(r, cookie);
1059 chxj_cookie_unlock(r, lock);
1065 if (! ap_is_HTTP_REDIRECT(r->status)) {
1066 r->status = HTTP_MOVED_TEMPORARILY;
1068 s_add_no_cache_headers(r, entryp);
1069 /* must not send body. */
1070 rv = pass_data_to_filter(f, "", 0);
1071 DBG(f->r, "REQ[%X] end of chxj_output_filter()", TO_ADDR(r));
1076 if (r->content_type) {
1077 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1078 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1079 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1080 && ! (dconf->image == CHXJ_IMG_ON
1081 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1082 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1083 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1084 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1085 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1086 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1087 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1088 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1089 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1090 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1091 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1093 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1094 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1095 cookie_lock_t *lock = NULL;
1096 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1097 switch(spec->html_spec_type) {
1098 case CHXJ_SPEC_Chtml_1_0:
1099 case CHXJ_SPEC_Chtml_2_0:
1100 case CHXJ_SPEC_Chtml_3_0:
1101 case CHXJ_SPEC_Chtml_4_0:
1102 case CHXJ_SPEC_Chtml_5_0:
1103 case CHXJ_SPEC_Chtml_6_0:
1104 case CHXJ_SPEC_Chtml_7_0:
1105 case CHXJ_SPEC_XHtml_Mobile_1_0:
1106 case CHXJ_SPEC_Jhtml:
1107 case CHXJ_SPEC_Jxhtml:
1108 lock = chxj_cookie_lock(r);
1109 cookie = chxj_save_cookie(r);
1110 s_add_cookie_id_if_has_location_header(r, cookie);
1111 chxj_cookie_unlock(r, lock);
1117 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1118 if (! ap_is_HTTP_REDIRECT(r->status)) {
1119 r->status = HTTP_MOVED_TEMPORARILY;
1122 s_add_no_cache_headers(r, entryp);
1123 ap_pass_brigade(f->next, bb);
1124 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1129 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1130 ap_pass_brigade(f->next, bb);
1131 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1136 for (b = APR_BRIGADE_FIRST(bb);
1137 b != APR_BRIGADE_SENTINEL(bb);
1138 b = APR_BUCKET_NEXT(b)) {
1140 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1141 DBG(r, "REQ[%X] read data[%.*s]",(unsigned int)(apr_size_t)r, (int)len, data);
1143 /*--------------------------------------------------------------------*/
1145 /*--------------------------------------------------------------------*/
1147 DBG(r, "append data start");
1148 ctx = (mod_chxj_ctx *)f->ctx;
1151 tmp = apr_palloc(r->pool, ctx->len);
1152 memcpy(tmp, ctx->buffer, ctx->len);
1154 ctx->buffer = apr_palloc(pool, ctx->len + len);
1156 memcpy(ctx->buffer, tmp, ctx->len);
1157 memcpy(&ctx->buffer[ctx->len], data, len);
1161 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1164 if (APR_BUCKET_IS_EOS(b)) {
1166 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1167 /*----------------------------------------------------------------------*/
1169 /*----------------------------------------------------------------------*/
1171 cookie_lock_t *lock = NULL;
1172 ctx = (mod_chxj_ctx *)f->ctx;
1174 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1175 lock = chxj_cookie_lock(r);
1177 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1179 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1180 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1181 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1186 tmp = apr_palloc(pool, ctx->len + 1);
1188 memset(tmp, 0, ctx->len + 1);
1189 memcpy(tmp, ctx->buffer, ctx->len);
1191 ctx->buffer = chxj_convert(r,
1192 (const char **)&tmp,
1193 (apr_size_t *)&ctx->len,
1195 user_agent, &cookie);
1199 ctx->buffer = apr_psprintf(r->pool, "\n");
1201 ctx->buffer = chxj_convert(r,
1202 (const char **)&ctx->buffer,
1203 (apr_size_t *)&ctx->len,
1205 user_agent, &cookie);
1210 && *(char *)r->content_type == 't'
1211 && strncmp(r->content_type, "text/xml", 8) == 0) {
1212 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1220 memset(&doc, 0, sizeof(Doc));
1221 memset(&qrcode, 0, sizeof(qr_code_t));
1223 doc.parse_mode = PARSE_MODE_CHTML;
1227 qs_init_malloc(&doc);
1229 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1232 for (child = qs_get_child_node(&doc,root);
1234 child = qs_get_next_node(&doc,child)) {
1235 char *name = qs_get_node_name(&doc,child);
1236 if (strcasecmp("qrcode",name) == 0) {
1241 qs_all_free(&doc,QX_LOGMARK);
1243 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1244 chxj_qrcode_node_to_qrcode(&qrcode, root);
1245 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1247 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1248 chxj_cookie_unlock(r, lock);
1249 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1252 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1256 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1258 && ( *r->content_type == 'i' || *r->content_type == 'I')
1259 && dconf->image == CHXJ_IMG_ON
1260 && strncasecmp("image/", r->content_type, 6) == 0
1261 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1262 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1263 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1264 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1265 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1266 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1267 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1268 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1269 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1270 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1274 DBG(r, "REQ[%X] ctx->len[%d]", (unsigned int)(apr_size_t)r, ctx->len);
1276 tmp = apr_palloc(pool, ctx->len + 1);
1277 memset(tmp, 0, ctx->len + 1);
1278 memcpy(tmp, ctx->buffer, ctx->len);
1280 chxj_convert_image(r,
1281 (const char **)&tmp,
1282 (apr_size_t *)&ctx->len);
1286 apr_table_unset(r->headers_out, "Content-Length");
1287 apr_table_unset(r->err_headers_out, "Content-Length");
1288 ap_set_content_length(r, (apr_off_t)ctx->len);
1292 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1293 s_add_cookie_id_if_has_location_header(r, cookie);
1294 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1295 if (! ap_is_HTTP_REDIRECT(r->status)) {
1296 r->status = HTTP_MOVED_TEMPORARILY;
1299 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1300 ctx->buffer = apr_pstrdup(pool, "");
1302 ap_set_content_length(r, (apr_off_t)ctx->len);
1304 chxj_cookie_unlock(r,lock);
1305 s_add_no_cache_headers(r, entryp);
1306 rv = pass_data_to_filter(f,
1307 (const char *)ctx->buffer,
1308 (apr_size_t)ctx->len);
1311 ctx->buffer = apr_pstrdup(pool, "");
1313 ap_set_content_length(r, (apr_off_t)ctx->len);
1314 chxj_cookie_unlock(r, lock);
1315 s_add_no_cache_headers(r, entryp);
1316 rv = pass_data_to_filter(f,
1317 (const char *)ctx->buffer,
1318 (apr_size_t)ctx->len);
1320 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1324 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1329 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1330 cookie_lock_t *lock = NULL;
1331 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1332 switch(spec->html_spec_type) {
1333 case CHXJ_SPEC_Chtml_1_0:
1334 case CHXJ_SPEC_Chtml_2_0:
1335 case CHXJ_SPEC_Chtml_3_0:
1336 case CHXJ_SPEC_Chtml_4_0:
1337 case CHXJ_SPEC_Chtml_5_0:
1338 case CHXJ_SPEC_Chtml_6_0:
1339 case CHXJ_SPEC_Chtml_7_0:
1340 case CHXJ_SPEC_XHtml_Mobile_1_0:
1341 case CHXJ_SPEC_Jhtml:
1342 case CHXJ_SPEC_Jxhtml:
1343 lock = chxj_cookie_lock(r);
1344 cookie = chxj_save_cookie(r);
1346 * Location Header Check to add cookie parameter.
1348 s_add_cookie_id_if_has_location_header(r, cookie);
1349 chxj_cookie_unlock(r, lock);
1350 apr_table_unset(r->headers_out, "Set-Cookie");
1351 apr_table_unset(r->err_headers_out, "Set-Cookie");
1358 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1359 if (! ap_is_HTTP_REDIRECT(r->status)) {
1360 r->status = HTTP_MOVED_TEMPORARILY;
1363 apr_table_setn(r->headers_out, "Content-Length", "0");
1364 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1365 s_add_no_cache_headers(r, entryp);
1366 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1367 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1372 apr_brigade_destroy(bb);
1374 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1380 * Add Cookie_id if it has location header.
1383 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1385 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1386 if (! location_header) {
1387 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1389 if (cookie && location_header) {
1390 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1391 location_header = chxj_add_cookie_parameter(r,
1394 apr_table_unset(r->headers_out, "Location");
1395 apr_table_setn(r->headers_out, "Location", location_header);
1396 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1397 if (!ap_is_HTTP_REDIRECT(r->status)) {
1398 r->status = HTTP_MOVED_TEMPORARILY;
1404 * It is the main loop of the input filter handler.
1408 chxj_input_handler(request_rec *r)
1410 mod_chxj_config *dconf;
1411 chxjconvrule_entry *entryp = NULL;
1412 device_table *spec = NULL;
1413 char *post_data = NULL;
1414 apr_size_t post_data_len = 0;
1419 int response_code = 0;
1421 DBG(r, "start of chxj_input_handler()");
1423 if (strcasecmp(r->handler, "chxj-input-handler")) {
1424 DBG(r, "end chxj_input_handler()");
1427 apr_pool_create(&pool, r->pool);
1429 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1430 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1432 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1434 spec = chxj_specified_device(r, user_agent);
1435 entryp = chxj_apply_convrule(r, dconf->convrules);
1437 post_data = apr_pstrdup(pool, "");
1438 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1439 if (ap_should_client_block(r)) {
1440 while (post_data_len < CHXJ_POST_MAX) {
1443 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1444 if (read_bytes<=0) {
1447 buffer[read_bytes] = '\0';
1448 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1449 post_data_len += read_bytes;
1458 if (post_data_len > 0) {
1459 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1460 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1464 if (dconf->forward_url_base) {
1465 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1468 url_path = apr_psprintf(pool, "%s://%s:%d%s", chxj_apache_run_http_scheme(r), ap_get_server_name(r), ap_get_server_port(r), r->uri);
1471 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1473 DBG(r, "==> new url_path:[%s]", url_path);
1476 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1477 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_TYPE, apr_table_get(r->headers_in, "X-Client-Type")); /* for mod_cidr_lookup */
1478 apr_table_unset(r->headers_in, "Content-Length");
1479 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1480 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1482 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1483 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1484 for (ii=0; ii<res_len/64; ii++) {
1485 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1487 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1491 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1492 if (strcasecmp(chunked, "chunked") == 0) {
1494 apr_table_unset(r->headers_out, "Transfer-Encoding");
1497 if (ap_is_HTTP_ERROR(response_code)) {
1498 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1499 return response_code;
1503 apr_pool_create(&wpool, r->pool);
1504 apr_bucket_brigade *bb;
1507 conn_rec *c = r->connection;
1509 bb = apr_brigade_create(wpool, c->bucket_alloc);
1510 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1511 APR_BRIGADE_INSERT_TAIL(bb, e);
1512 e = apr_bucket_eos_create(c->bucket_alloc);
1513 APR_BRIGADE_INSERT_TAIL(bb, e);
1514 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1515 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1518 apr_brigade_cleanup(bb);
1521 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1525 static mod_chxj_global_config *
1526 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1528 mod_chxj_global_config *conf;
1530 SDBG(s, "start chxj_global_config_create()");
1532 /*--------------------------------------------------------------------------*/
1533 /* allocate an own subpool which survives server restarts */
1534 /*--------------------------------------------------------------------------*/
1535 conf = (mod_chxj_global_config *)apr_palloc(pool,
1536 sizeof(mod_chxj_global_config));
1538 conf->cookie_db_lock = NULL;
1540 SDBG(s, "end chxj_global_config_create()");
1547 * initialize chxj module
1550 chxj_init_module(apr_pool_t *p,
1551 apr_pool_t *UNUSED(plog),
1552 apr_pool_t *UNUSED(ptemp),
1558 SDBG(s, "start chxj_init_module()");
1560 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1562 if (user_data == NULL) {
1565 * dummy user_data set.
1567 apr_pool_userdata_set(
1569 CHXJ_MOD_CONFIG_KEY,
1570 apr_pool_cleanup_null,
1572 SDBG(s, "end chxj_init_module()");
1576 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1578 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1580 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1581 return HTTP_INTERNAL_SERVER_ERROR;
1584 SDBG(s, "end chxj_init_module()");
1591 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1594 SDBG(s, "start chxj_child_init()");
1595 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1597 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1599 SDBG(s, "end chxj_child_init()");
1604 * A set structure of each server is generated.
1610 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1612 mod_chxj_global_config *gc;
1614 gc = chxj_global_config_create(p,s);
1621 chxj_translate_name(request_rec *r)
1623 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1624 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1625 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1626 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1627 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1628 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1630 return chxj_trans_name(r);
1638 chxj_insert_filter(request_rec *r)
1642 mod_chxj_config *dconf;
1643 chxjconvrule_entry *entryp;
1648 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1650 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1652 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1653 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1655 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1658 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1660 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1661 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)(apr_size_t)r);
1662 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1666 spec = chxj_specified_device(r, user_agent);
1667 entryp = chxj_apply_convrule(r, dconf->convrules);
1669 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1672 ctx = apr_palloc(r->pool, sizeof(*ctx));
1673 memset(ctx, 0, sizeof(*ctx));
1674 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1675 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1676 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1679 ctx->entryp = entryp;
1681 ctx->buffer = apr_palloc(ctx->pool, 1);
1684 if (!entryp || (!(entryp->action & CONVRULE_ENGINE_ON_BIT) && !(entryp->action & CONVRULE_COOKIE_ONLY_BIT))) {
1685 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1686 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1690 switch(spec->html_spec_type) {
1691 case CHXJ_SPEC_Chtml_1_0:
1692 case CHXJ_SPEC_Chtml_2_0:
1693 case CHXJ_SPEC_Chtml_3_0:
1694 case CHXJ_SPEC_Chtml_4_0:
1695 case CHXJ_SPEC_Chtml_5_0:
1696 case CHXJ_SPEC_Chtml_6_0:
1697 case CHXJ_SPEC_Chtml_7_0:
1698 case CHXJ_SPEC_XHtml_Mobile_1_0:
1699 case CHXJ_SPEC_Hdml:
1700 case CHXJ_SPEC_Jhtml:
1701 case CHXJ_SPEC_Jxhtml:
1705 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1710 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1711 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1712 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1715 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1720 * The hook is registered.
1725 chxj_register_hooks(apr_pool_t *UNUSED(p))
1727 ap_hook_post_config(chxj_init_module,
1730 APR_HOOK_REALLY_FIRST);
1731 ap_hook_child_init(chxj_child_init,
1734 APR_HOOK_REALLY_FIRST);
1735 ap_register_output_filter (
1736 "chxj_output_filter",
1740 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1741 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1742 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1743 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1744 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1745 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1750 * A set structure according to the directory is generated.
1756 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1758 mod_chxj_config *conf;
1760 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1761 conf->device_data_file = NULL;
1762 conf->devices = NULL;
1763 conf->emoji_data_file = NULL;
1765 conf->emoji_tail = NULL;
1766 conf->image = CHXJ_IMG_NONE;
1767 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1768 conf->image_cache_limit = 0;
1769 conf->server_side_encoding = NULL;
1770 conf->cookie_db_dir = NULL;
1771 conf->cookie_timeout = 0;
1772 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1773 conf->cookie_lazy_mode = 0;
1774 conf->cookie_dbm_type = NULL;
1775 #if defined(USE_MYSQL_COOKIE)
1776 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1777 conf->mysql.port = MYSQL_PORT;
1778 conf->mysql.host = NULL;
1780 #if defined(USE_MEMCACHE_COOKIE)
1781 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1782 conf->memcache.host = NULL;
1783 conf->memcache.port = 0;
1785 conf->forward_url_base = NULL;
1786 conf->forward_server_ip = NULL;
1787 conf->allowed_cookie_domain = NULL;
1788 conf->post_log = NULL;
1794 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1795 memset(conf->dir, 0, strlen(arg)+1);
1796 strcpy(conf->dir, arg);
1798 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1800 /* Default is copyleft */
1801 conf->image_copyright = NULL;
1808 * Merge per-directory CHXJ configurations
1811 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1813 mod_chxj_config *base;
1814 mod_chxj_config *add;
1815 mod_chxj_config *mrg;
1817 base = (mod_chxj_config *)basev;
1818 add = (mod_chxj_config *)addv;
1819 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1821 mrg->device_data_file = NULL;
1822 mrg->devices = NULL;
1823 mrg->emoji_data_file = NULL;
1824 mrg->image = CHXJ_IMG_NONE;
1825 mrg->image_cache_dir = NULL;
1826 mrg->image_copyright = NULL;
1827 mrg->image_cache_limit = 0;
1829 mrg->emoji_tail = NULL;
1830 mrg->new_line_type = NLTYPE_NIL;
1831 mrg->forward_url_base = NULL;
1832 mrg->forward_server_ip = NULL;
1833 mrg->allowed_cookie_domain = NULL;
1834 mrg->post_log = NULL;
1835 mrg->cookie_dbm_type = NULL;
1837 mrg->dir = apr_pstrdup(p, add->dir);
1839 if (! add->device_data_file) {
1840 mrg->devices = base->devices;
1841 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1844 mrg->devices = add->devices;
1845 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1848 if (! add->emoji_data_file) {
1849 mrg->emoji = base->emoji;
1850 mrg->emoji_tail = base->emoji_tail;
1851 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1854 mrg->emoji = add->emoji;
1855 mrg->emoji_tail = add->emoji_tail;
1856 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1859 if (add->image == CHXJ_IMG_NONE) {
1860 mrg->image = base->image;
1863 mrg->image = add->image;
1866 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1867 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1870 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1873 if (add->image_cache_limit) {
1874 mrg->image_cache_limit = add->image_cache_limit;
1877 mrg->image_cache_limit = base->image_cache_limit;
1880 if (add->image_copyright)
1881 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1883 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1885 if (add->server_side_encoding) {
1886 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1889 if (base->server_side_encoding) {
1890 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1893 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1896 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1898 if (add->cookie_db_dir) {
1899 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1902 if (base->cookie_db_dir) {
1903 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1906 mrg->cookie_db_dir = NULL;
1909 if (add->cookie_timeout) {
1910 mrg->cookie_timeout = add->cookie_timeout;
1913 if (base->cookie_db_dir) {
1914 mrg->cookie_timeout = base->cookie_timeout;
1917 mrg->cookie_timeout = 0;
1920 #if defined(USE_MYSQL_COOKIE)
1921 if (add->mysql.host) {
1922 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1924 else if (base->mysql.host) {
1925 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1928 mrg->mysql.host = NULL;
1930 if (add->mysql.port) {
1931 mrg->mysql.port = add->mysql.port;
1933 else if (base->mysql.port) {
1934 mrg->mysql.port = base->mysql.port;
1937 mrg->mysql.port = 0;
1940 if (add->mysql.database) {
1941 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1943 else if (base->mysql.database) {
1944 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1947 mrg->mysql.database = NULL;
1950 if (add->mysql.username) {
1951 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1953 else if (base->mysql.username) {
1954 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1957 mrg->mysql.username = NULL;
1960 if (add->mysql.password) {
1961 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1963 else if (base->mysql.password) {
1964 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1967 mrg->mysql.password = NULL;
1970 if (add->mysql.tablename) {
1971 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1973 else if (base->mysql.tablename) {
1974 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1977 mrg->mysql.tablename = NULL;
1980 if (add->mysql.socket_path) {
1981 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1983 else if (base->mysql.socket_path) {
1984 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1987 mrg->mysql.socket_path = NULL;
1990 if (add->mysql.charset) {
1991 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1993 else if (base->mysql.charset) {
1994 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1997 mrg->mysql.charset = NULL;
2000 #if defined(USE_MEMCACHE_COOKIE)
2001 if (add->memcache.host) {
2002 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
2004 else if (base->memcache.host) {
2005 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
2008 mrg->memcache.host = NULL;
2010 if (add->memcache.port) {
2011 mrg->memcache.port = add->memcache.port;
2013 else if (base->memcache.port) {
2014 mrg->memcache.port = base->memcache.port;
2017 mrg->memcache.port = 0;
2020 if (add->cookie_store_type) {
2021 mrg->cookie_store_type = add->cookie_store_type;
2023 else if (base->cookie_store_type) {
2024 mrg->cookie_store_type = base->cookie_store_type;
2027 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2029 if (add->cookie_lazy_mode) {
2030 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
2032 else if (base->cookie_lazy_mode) {
2033 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
2036 mrg->cookie_lazy_mode = 0;
2038 if (add->new_line_type) {
2039 mrg->new_line_type = add->new_line_type;
2041 else if (base->new_line_type) {
2042 mrg->new_line_type = base->new_line_type;
2045 mrg->new_line_type = NLTYPE_NIL;
2048 if (add->forward_url_base) {
2049 mrg->forward_url_base = add->forward_url_base;
2051 else if (base->forward_url_base) {
2052 mrg->forward_url_base = base->forward_url_base;
2055 if (add->allowed_cookie_domain) {
2056 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
2059 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
2061 if (add->post_log) {
2062 mrg->post_log = add->post_log;
2065 mrg->post_log = base->post_log;
2067 if (add->cookie_dbm_type) {
2068 mrg->cookie_dbm_type = add->cookie_dbm_type;
2071 mrg->cookie_dbm_type = base->cookie_dbm_type;
2078 chxj_command_parse_take5(
2091 for (;*strp == ' '||*strp == '\t'; strp++) ;
2101 for (; *strp != '\0'; strp++) {
2102 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2103 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2108 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2109 || (isquoted && *strp == '"'))
2123 for (;*strp == ' '||*strp == '\t'; strp++) ;
2132 for (; *strp != '\0'; strp++) {
2133 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2134 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2139 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2140 || (isquoted && *strp == '"'))
2153 for (;*strp == ' '||*strp == '\t'; strp++);
2161 for (; *strp != '\0'; strp++) {
2162 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2163 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2168 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2169 || (isquoted && *strp == '"'))
2181 for (;*strp == ' '||*strp == '\t'; strp++);
2189 for (; *strp != '\0'; strp++) {
2190 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2191 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2196 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2197 || (isquoted && *strp == '"'))
2208 for (;*strp == ' '||*strp == '\t'; strp++);
2216 for (; *strp != '\0'; strp++) {
2217 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2218 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2223 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2224 || (isquoted && *strp == '"'))
2234 * The device definition file is loaded.
2236 * @param arg [i] The name of the device definition file is specified.
2237 * @param mconfig [i/o] The pointer to a set structure is specified.
2241 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2243 mod_chxj_config *conf;
2248 if (strlen(arg) > 256)
2249 return "mod_chxj: device data filename too long.";
2251 conf = (mod_chxj_config *)mconfig;
2252 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2254 qs_init_malloc(&doc);
2255 qs_init_root_node(&doc);
2257 qs_parse_file((Doc *)&doc, (const char *)arg);
2258 chxj_load_device_data(&doc,parms->pool, conf);
2259 qs_all_free(&doc, QX_LOGMARK);
2266 * Device definition information is loaded.
2269 * @param arg [i] The name of the device definition file is specified.
2270 * @param mconfig [i/o] The pointer to a set structure is specified.
2274 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2276 mod_chxj_config *conf;
2283 if (strlen(arg) > 256)
2284 return "mod_chxj: emoji data filename too long.";
2286 conf = (mod_chxj_config *)mconfig;
2287 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2288 qs_init_malloc(&doc);
2289 qs_init_root_node(&doc);
2291 qs_parse_file((Doc *)&doc, (const char *)arg);
2293 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2295 qs_all_free(&doc, QX_LOGMARK);
2303 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2305 mod_chxj_config *conf;
2310 if (strlen(arg) > 256)
2311 return "image uri is too long.";
2313 conf = (mod_chxj_config*)mconfig;
2314 if (strcasecmp("ON", arg) == 0) {
2315 conf->image = CHXJ_IMG_ON;
2318 conf->image = CHXJ_IMG_OFF;
2326 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2328 mod_chxj_config *conf;
2333 if (strlen(arg) > 256)
2334 return "cache dir name is too long.";
2336 conf = (mod_chxj_config *)mconfig;
2337 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2344 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2346 mod_chxj_config *conf;
2351 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2352 return "cache size is too long.";
2354 conf = (mod_chxj_config *)mconfig;
2357 * I use strtol function because strtoul is not portable function.
2359 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2362 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2364 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2373 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2375 mod_chxj_config *conf;
2380 if (strlen(arg) > 256)
2381 return "Copyright Flag is too long.";
2383 conf = (mod_chxj_config *)mconfig;
2384 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2391 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2395 mod_chxj_config *dconf;
2396 chxjconvrule_entry *newrule;
2406 dconf = (mod_chxj_config *)mconfig;
2408 if (strlen(arg) > 4096)
2409 return "mod_chxj: ChxjConvertRule: is too long.";
2411 dconf = (mod_chxj_config *)mconfig;
2412 if (dconf->convrules == NULL)
2413 dconf->convrules = apr_array_make(cmd->pool,
2415 sizeof(chxjconvrule_entry));
2417 newrule = apr_array_push(dconf->convrules);
2420 newrule->action = 0;
2422 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2423 return "ChxjConvertRule: bad argument line";
2425 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2429 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2435 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2436 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2439 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2440 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2443 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2444 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2447 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2448 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2454 if (strcasecmp(CONVRULE_OVERWRITE_X_CLIENT_TYPE_CMD, action) == 0) {
2455 newrule->action |= CONVRULE_OVERWRITE_X_CLIENT_TYPE_BIT;
2461 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2462 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2464 else if (strcasecmp(CONVRULE_COOKIE_ONLY_CMD, action) == 0) {
2465 newrule->action |= CONVRULE_COOKIE_ONLY_BIT;
2471 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2472 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2478 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2479 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2485 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2486 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2492 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2493 newrule->action |= CONVRULE_Z2H_ON_BIT;
2496 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2497 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2500 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2501 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2504 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2505 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2508 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2509 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2512 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2513 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2516 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2517 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2520 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2521 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2532 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2536 mode = AP_REG_EXTENDED;
2537 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2538 return "RewriteRule: cannot compile regular expression ";
2540 newrule->regexp = regexp;
2542 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2544 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2546 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2548 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2549 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2551 newrule->user_agent = NULL;
2553 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2565 mod_chxj_config *dconf;
2568 if (strlen(arg) > 4096)
2569 return "mod_chxj: ChxjCookieDir is too long.";
2571 dconf = (mod_chxj_config *)mconfig;
2573 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2580 cmd_set_cookie_timeout(
2581 cmd_parms *UNUSED(cmd),
2585 mod_chxj_config *dconf;
2587 if (strlen(arg) > 4096)
2588 return "mod_chxj: ChxjCookieTimeout is too long.";
2590 if (chxj_chk_numeric(arg) != 0)
2591 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2593 dconf = (mod_chxj_config *)mconfig;
2595 dconf->cookie_timeout = atoi(arg);
2601 #if defined(USE_MYSQL_COOKIE)
2603 cmd_set_cookie_mysql_database(
2608 mod_chxj_config *dconf;
2610 if (strlen(arg) > 255)
2611 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2613 dconf = (mod_chxj_config *)mconfig;
2615 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2622 cmd_set_cookie_mysql_username(
2627 mod_chxj_config *dconf;
2629 if (strlen(arg) > 255)
2630 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2632 dconf = (mod_chxj_config *)mconfig;
2634 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2641 cmd_set_cookie_mysql_password(
2646 mod_chxj_config *dconf;
2648 if (strlen(arg) > 255)
2649 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2651 dconf = (mod_chxj_config *)mconfig;
2653 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2660 cmd_set_cookie_mysql_table_name(
2665 mod_chxj_config *dconf;
2667 if (strlen(arg) > 255)
2668 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2670 dconf = (mod_chxj_config *)mconfig;
2672 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2678 cmd_set_cookie_mysql_port(
2679 cmd_parms *UNUSED(cmd),
2683 mod_chxj_config *dconf;
2685 if (strlen(arg) > 255)
2686 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2688 dconf = (mod_chxj_config *)mconfig;
2690 if (chxj_chk_numeric(arg) != 0)
2691 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2693 dconf = (mod_chxj_config *)mconfig;
2695 dconf->mysql.port = chxj_atoi(arg);
2702 cmd_set_cookie_mysql_host(
2707 mod_chxj_config *dconf;
2709 if (strlen(arg) > 255)
2710 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2712 dconf = (mod_chxj_config *)mconfig;
2714 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2721 cmd_set_cookie_mysql_socket_path(
2726 mod_chxj_config *dconf;
2728 if (strlen(arg) > 4096)
2729 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2731 dconf = (mod_chxj_config *)mconfig;
2733 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2740 cmd_set_cookie_mysql_charset(
2745 mod_chxj_config *dconf;
2747 if (strlen(arg) > 255)
2748 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2750 dconf = (mod_chxj_config *)mconfig;
2752 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2757 #if defined(USE_MEMCACHE_COOKIE)
2759 cmd_set_cookie_memcache_port(
2760 cmd_parms *UNUSED(cmd),
2764 mod_chxj_config *dconf;
2766 if (strlen(arg) > 255)
2767 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2769 dconf = (mod_chxj_config *)mconfig;
2771 if (chxj_chk_numeric(arg) != 0)
2772 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2774 dconf = (mod_chxj_config *)mconfig;
2776 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2783 cmd_set_cookie_memcache_host(
2788 mod_chxj_config *dconf;
2790 if (strlen(arg) > 255)
2791 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2793 dconf = (mod_chxj_config *)mconfig;
2795 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2802 cmd_set_cookie_lazy_mode(
2803 cmd_parms *UNUSED(cmd),
2807 mod_chxj_config *dconf;
2809 if (strlen(arg) > 255)
2810 return "mod_chxj: ChxjCookieLazyMode is too long.";
2812 dconf = (mod_chxj_config *)mconfig;
2814 if (strcasecmp("TRUE",arg) == 0) {
2815 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2818 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2825 cmd_set_cookie_store_type(
2826 cmd_parms *UNUSED(cmd),
2830 mod_chxj_config *dconf;
2832 if (strlen(arg) > 255)
2833 return "mod_chxj: ChxjCookieStoreType is too long.";
2835 dconf = (mod_chxj_config *)mconfig;
2837 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2838 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2840 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2841 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2843 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2844 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2847 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2854 cmd_set_forward_url_base(
2859 mod_chxj_config *dconf;
2861 if (strlen(arg) > 255)
2862 return "mod_chxj: ChxjForwardUrlBase is too long.";
2864 dconf = (mod_chxj_config *)mconfig;
2866 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2872 cmd_set_forward_server_ip(
2877 mod_chxj_config *dconf;
2879 if (strlen(arg) > 255)
2880 return "mod_chxj: ChxjForwardServerIp is too long.";
2882 dconf = (mod_chxj_config *)mconfig;
2884 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2890 cmd_allowed_cookie_domain(
2895 mod_chxj_config *dconf;
2897 if (strlen(arg) > 255)
2898 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2900 dconf = (mod_chxj_config *)mconfig;
2902 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2908 cmd_set_new_line_type(
2909 cmd_parms *UNUSED(cmd),
2913 mod_chxj_config *dconf;
2914 if (strlen(arg) > 255)
2915 return "mod_chxj: ChxjNewLineType is too long.";
2917 dconf = (mod_chxj_config *)mconfig;
2919 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2920 dconf->new_line_type = NLTYPE_CRLF;
2922 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2923 dconf->new_line_type = NLTYPE_LF;
2925 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2926 dconf->new_line_type = NLTYPE_CR;
2928 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2929 dconf->new_line_type = NLTYPE_NONE;
2932 return "mod_chxj: invalid value (ChxjNewLineType)";
2943 mod_chxj_config *dconf;
2944 if (strlen(arg) > 255)
2945 return "mod_chxj: ChxjPostLogEnv is too long.";
2947 dconf = (mod_chxj_config *)mconfig;
2949 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2955 cmd_cookie_dbm_type(
2960 mod_chxj_config *dconf;
2961 if (strlen(arg) > 255)
2962 return "mod_chxj: ChxjCookieDbmType is too long.";
2964 dconf = (mod_chxj_config *)mconfig;
2966 dconf->cookie_dbm_type = apr_pstrdup(cmd->pool, arg);
2972 static const command_rec cmds[] = {
2974 "ChxjLoadDeviceData",
2975 cmd_load_device_data,
2978 "Load Device Data"),
2980 "ChxjLoadEmojiData",
2981 cmd_load_emoji_data,
2987 cmd_set_image_engine,
2990 "Convert Target URI"),
2992 "ChxjImageCacheDir",
2993 cmd_set_image_cache_dir,
2996 "Image Cache Directory"),
2998 "ChxjImageCacheLimit",
2999 cmd_set_image_cache_limit,
3002 "Image Cache Limit"),
3004 "ChxjImageCopyright",
3005 cmd_set_image_copyright,
3014 "an URL-applied regexp-pattern and a substitution URL"),
3020 "save cookie.db directory."),
3022 "ChxjCookieTimeout",
3023 cmd_set_cookie_timeout,
3026 "The compulsion time-out time of the cookie is specified. "),
3028 "ChxjCookieStoreType",
3029 cmd_set_cookie_store_type,
3032 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
3034 "ChxjCookieLazyMode",
3035 cmd_set_cookie_lazy_mode,
3038 "OneTimeID is negligently done. (TRUE/FALSE)"),
3039 #if defined(USE_MYSQL_COOKIE)
3041 "ChxjCookieMysqlHost",
3042 cmd_set_cookie_mysql_host,
3045 "The MySQL database host used by saving Cookie"),
3047 "ChxjCookieMysqlPort",
3048 cmd_set_cookie_mysql_port,
3051 "The MySQL database port used by saving Cookie"),
3053 "ChxjCookieMysqlDatabase",
3054 cmd_set_cookie_mysql_database,
3057 "The MySQL database name used by saving Cookie"),
3059 "ChxjCookieMysqlUsername",
3060 cmd_set_cookie_mysql_username,
3063 "The MySQL username used by saving Cookie"),
3065 "ChxjCookieMysqlPassword",
3066 cmd_set_cookie_mysql_password,
3069 "The MySQL password used by saving Cookie"),
3071 "ChxjCookieMysqlTableName",
3072 cmd_set_cookie_mysql_table_name,
3075 "The MySQL table name used by saving Cookie"),
3077 "ChxjCookieMysqlSocketPath",
3078 cmd_set_cookie_mysql_socket_path,
3081 "The MySQL socket path used by saving Cookie"),
3083 "ChxjCookieMysqlCharset",
3084 cmd_set_cookie_mysql_charset,
3087 "The MySQL charset used by saving Cookie"),
3089 #if defined(USE_MEMCACHE_COOKIE)
3091 "ChxjCookieMemcacheHost",
3092 cmd_set_cookie_memcache_host,
3095 "The Memcached host used by saving Cookie"),
3097 "ChxjCookieMemcachePort",
3098 cmd_set_cookie_memcache_port,
3101 "The Memcached port used by saving Cookie"),
3105 cmd_set_new_line_type,
3108 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3110 "ChxjForwardUrlBase",
3111 cmd_set_forward_url_base,
3114 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3116 "ChxjForwardServerIp",
3117 cmd_set_forward_server_ip,
3120 "The forward server ip(default: this server ip)"),
3122 "ChxjAllowedCookieDomain",
3123 cmd_allowed_cookie_domain,
3126 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3132 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3134 "ChxjCookieDbmType",
3135 cmd_cookie_dbm_type,
3138 "Kind of DBM used with Cookie simulator.(default|GDBM|SDBM|DB|NDBM)"),
3139 {NULL,{NULL},NULL,0,0,NULL},
3143 /*----------------------------------------------------------------------------*/
3144 /* Dispatch list for API hooks */
3145 /*----------------------------------------------------------------------------*/
3146 module AP_MODULE_DECLARE_DATA chxj_module = {
3147 STANDARD20_MODULE_STUFF,
3148 chxj_create_per_dir_config, /* create per-dir config structures */
3149 chxj_merge_per_dir_config, /* merge per-dir config structures */
3150 chxj_config_server_create, /* create per-server config structures */
3151 NULL, /* merge per-server config structures */
3152 cmds, /* table of config file commands */
3153 chxj_register_hooks /* register hooks */