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_ixhtml10.h"
58 #include "chxj_jhtml.h"
59 #include "chxj_jxhtml.h"
60 #include "chxj_img_conv_format.h"
61 #include "chxj_qr_code.h"
62 #include "chxj_encoding.h"
63 #include "chxj_apply_convrule.h"
64 #include "chxj_cookie.h"
65 #include "chxj_url_encode.h"
66 #include "chxj_str_util.h"
67 #include "chxj_dump_string.h"
68 #if defined(USE_MYSQL_COOKIE)
69 # include "chxj_mysql.h"
71 #include "chxj_serf.h"
72 #include "chxj_add_device_env.h"
73 #include "chxj_conv_z2h.h"
74 #include "chxj_header_inf.h"
75 #include "chxj_jreserved_tag.h"
78 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
79 #define CHXJ_VERSION PACKAGE_VERSION
80 #define CHXJ_POST_MAX (0x1000000)
82 converter_t convert_routine[] = {
84 /* CHXJ_SPEC_UNKNOWN */
87 .emoji_only_converter = NULL,
90 /* CHXJ_SPEC_Chtml_1_0 */
91 .converter = chxj_convert_chtml10,
92 .encoder = chxj_encoding,
93 .emoji_only_converter = chxj_chtml10_emoji_only_converter,
96 /* CHXJ_SPEC_Chtml_2_0 */
97 .converter = chxj_convert_chtml20,
98 .encoder = chxj_encoding,
99 .emoji_only_converter = chxj_chtml20_emoji_only_converter,
102 /* CHXJ_SPEC_Chtml_3_0 */
103 .converter = chxj_convert_chtml30,
104 .encoder = chxj_encoding,
105 .emoji_only_converter = chxj_chtml30_emoji_only_converter,
108 /* CHXJ_SPEC_Chtml_4_0 */
109 .converter = chxj_convert_chtml40,
110 .encoder = chxj_encoding,
111 .emoji_only_converter = chxj_chtml40_emoji_only_converter,
114 /* CHXJ_SPEC_Chtml_5_0 */
115 .converter = chxj_convert_chtml50,
116 .encoder = chxj_encoding,
117 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
120 /* CHXJ_SPEC_Chtml_6_0 */
121 .converter = chxj_convert_ixhtml10,
122 .encoder = chxj_encoding,
123 .emoji_only_converter = chxj_chtml50_emoji_only_converter, /* XXX: TODO */
126 /* CHXJ_SPEC_Chtml_7_0 */
127 .converter = chxj_convert_ixhtml10,
128 .encoder = chxj_encoding,
129 .emoji_only_converter = chxj_chtml50_emoji_only_converter, /* XXX: TODO */
132 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
133 .converter = chxj_convert_xhtml_mobile_1_0,
134 .encoder = chxj_encoding,
135 .emoji_only_converter = chxj_xhtml_emoji_only_converter,
139 .converter = chxj_convert_hdml,
140 .encoder = chxj_encoding,
141 .emoji_only_converter = NULL,
144 /* CHXJ_SPEC_Jhtml */
145 .converter = chxj_convert_jhtml,
146 .encoder = chxj_encoding,
147 .emoji_only_converter = chxj_jhtml_emoji_only_converter,
150 /* CHXJ_SPEC_Jxhtml */
151 .converter = chxj_convert_jxhtml,
152 .encoder = chxj_encoding,
153 .emoji_only_converter = chxj_jxhtml_emoji_only_converter,
159 .emoji_only_converter = NULL,
163 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec);
164 static void s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec);
165 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
166 static void s_clear_cookie_header(request_rec *r, device_table *spec);
167 static void s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp);
170 * Only when User-Agent is specified, the User-Agent header is camouflaged.
175 chxj_headers_fixup(request_rec *r)
177 mod_chxj_config* dconf;
178 chxjconvrule_entry* entryp;
184 DBG(r, "REQ[%X] start chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
186 DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)(apr_size_t)r);
187 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
191 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
193 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
194 spec = chxj_specified_device(r, user_agent);
196 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
198 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
199 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)(apr_size_t)r);
200 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
203 if (r->method_number == M_POST) {
204 if (!apr_table_get(r->headers_in, "X-Chxj-Forward")) {
205 s_clear_cookie_header(r, spec);
209 s_clear_cookie_header(r, spec);
212 switch(spec->html_spec_type) {
213 case CHXJ_SPEC_Chtml_1_0:
214 case CHXJ_SPEC_Chtml_2_0:
215 case CHXJ_SPEC_Chtml_3_0:
216 case CHXJ_SPEC_Chtml_4_0:
217 case CHXJ_SPEC_Chtml_5_0:
218 case CHXJ_SPEC_Chtml_6_0:
219 case CHXJ_SPEC_Chtml_7_0:
220 case CHXJ_SPEC_XHtml_Mobile_1_0:
222 case CHXJ_SPEC_Jhtml:
223 case CHXJ_SPEC_Jxhtml:
224 entryp = chxj_apply_convrule(r, dconf->convrules);
226 DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int)(apr_size_t)r);
229 if (!(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
230 DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int)(apr_size_t)r);
233 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
234 DBG(r, "REQ[%X] end chxj_headers_fixup() (emoji only)", (unsigned int)(apr_size_t)r);
238 apr_table_setn(r->headers_in,
239 CHXJ_HTTP_USER_AGENT,
242 if (entryp->user_agent)
243 apr_table_setn(r->headers_in,
247 chxj_convert_input_header(r,entryp, spec);
252 DBG(r, "REQ[%X] end chxj_headers_fixup() (not mobile)", (unsigned int)(apr_size_t)r);
258 if (r->method_number == M_POST) {
259 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
260 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);
261 r->proxyreq = PROXYREQ_NONE;
262 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
265 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
267 apr_sockaddr_t *address = NULL;
268 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
269 if (rv != APR_SUCCESS) {
271 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));
272 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
276 if (dconf->forward_server_ip) {
277 addr = dconf->forward_server_ip;
280 apr_sockaddr_ip_get(&addr, address);
282 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);
283 if (strcmp(addr, r->connection->remote_ip) == 0) {
284 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
286 if (! apr_table_get(r->headers_in, "Content-Length")) {
287 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
289 apr_table_set(r->headers_in, "Content-Length", contentLength);
296 chxj_add_device_env(r, spec);
298 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
305 s_clear_cookie_header(request_rec *r, device_table *spec)
307 switch(spec->html_spec_type) {
308 case CHXJ_SPEC_Chtml_1_0:
309 case CHXJ_SPEC_Chtml_2_0:
310 case CHXJ_SPEC_Chtml_3_0:
311 case CHXJ_SPEC_Chtml_4_0:
312 case CHXJ_SPEC_Chtml_5_0:
313 case CHXJ_SPEC_Chtml_6_0:
314 case CHXJ_SPEC_Chtml_7_0:
315 case CHXJ_SPEC_XHtml_Mobile_1_0:
316 case CHXJ_SPEC_Jhtml:
317 apr_table_unset(r->headers_in, "Cookie");
326 * It converts it from CHTML into XXML corresponding to each model.
329 * @param src [i] It is former HTML character string.
330 * @param len [i/o] It is length of former HTML character string.
333 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
339 mod_chxj_config *dconf;
340 chxjconvrule_entry *entryp;
342 DBG(r,"REQ[%X] start of chxj_convert()", (unsigned int)(apr_size_t)r);
344 chxj_dump_string(r, APLOG_MARK, "INPUT Data", *src, *len);
346 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
348 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
351 entryp = chxj_apply_convrule(r, dconf->convrules);
353 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
354 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
359 /*------------------------------------------------------------------------*/
360 /* get UserAgent from http header */
361 /*------------------------------------------------------------------------*/
362 if (entryp->user_agent)
363 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
365 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
367 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
368 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
371 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
372 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
373 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
374 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
378 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
380 spec = chxj_specified_device(r, user_agent);
387 if (entryp->action & CONVRULE_COOKIE_ON_BIT
388 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
389 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
390 switch(spec->html_spec_type) {
391 case CHXJ_SPEC_Chtml_1_0:
392 case CHXJ_SPEC_Chtml_2_0:
393 case CHXJ_SPEC_Chtml_3_0:
394 case CHXJ_SPEC_Chtml_4_0:
395 case CHXJ_SPEC_Chtml_5_0:
396 case CHXJ_SPEC_Chtml_6_0:
397 case CHXJ_SPEC_Chtml_7_0:
398 case CHXJ_SPEC_XHtml_Mobile_1_0:
399 case CHXJ_SPEC_Jhtml:
400 cookie = chxj_save_cookie(r);
407 if (!r->header_only) {
410 if (convert_routine[spec->html_spec_type].encoder)
411 tmp = convert_routine[spec->html_spec_type].encoder(r,
415 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
417 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
420 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
422 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
423 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
428 dst = apr_palloc(r->pool, 1);
433 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
436 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
437 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
438 if (convert_routine[spec->html_spec_type].converter) {
440 dst = convert_routine[spec->html_spec_type].converter(r,
448 dst = convert_routine[spec->html_spec_type].converter(r,
457 dst = chxj_conv_z2h(r, dst, len, entryp);
461 ap_set_content_length(r, *len);
464 dst = apr_psprintf(r->pool, "\n");
473 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
480 * convert GUID parameter.
484 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
486 if (strcasecmp(param, "guid") == 0) {
487 switch(spec->html_spec_type) {
488 case CHXJ_SPEC_XHtml_Mobile_1_0:
490 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
492 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
496 case CHXJ_SPEC_Jhtml:
497 case CHXJ_SPEC_Jxhtml:
499 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
501 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
512 * It converts it from HEADER.
517 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
529 cookie_t *cookie = NULL;
530 int no_update_flag = 0;
532 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
535 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
536 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
539 urilen = strlen(r->args);
541 result = qs_alloc_zero_byte_string(r->pool);
543 buff_pre = apr_pstrdup(r->pool, r->args);
548 pair = apr_strtok(buff_pre, "&", &pstate);
554 pair_sv = apr_pstrdup(r->pool, pair);
556 name = apr_strtok(pair, "=", &vstate);
557 value = apr_strtok(NULL, "=", &vstate);
558 if (! name) continue;
559 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
560 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
565 buff = apr_pstrdup(r->pool, r->args);
566 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
575 pair = apr_strtok(buff, "&", &pstate);
581 pair_sv = apr_pstrdup(r->pool, pair);
583 name = apr_strtok(pair, "=", &vstate);
584 value = apr_strtok(NULL, "=", &vstate);
585 if (! name) continue;
586 name = chxj_safe_to_jreserved_tag(r, name);
588 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
589 if (strlen(result) != 0)
590 result = apr_pstrcat(r->pool, result, "&", NULL);
592 if (strcasecmp(entryp->encoding, "NONE") != 0) {
597 if (value && *value != 0) {
598 value = chxj_url_decode(r->pool, value);
599 dlen = strlen(value);
600 DBG(r, "************ before encoding[%s]", value);
602 dvalue = chxj_rencoding(r, value, &dlen);
603 dvalue = chxj_url_encode(r->pool, dvalue);
605 DBG(r, "************ after encoding[%s]", dvalue);
611 if (name && *name != 0) {
612 name = chxj_url_decode(r->pool, name);
614 dname = chxj_rencoding(r, name, &dlen);
615 dname = chxj_url_encode(r->pool, dname);
620 s_convert_guid_parameter_to_header(r, dname, spec);
621 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
624 if (strcmp(name, pair_sv) != 0)
625 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
627 result = apr_pstrcat(r->pool, result, name, NULL);
631 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
632 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
633 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
637 if (strlen(value) == 0)
640 if (strlen(result) != 0)
641 result = apr_pstrcat(r->pool, result, "&", NULL);
643 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
646 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
648 apr_table_unset(r->headers_in, "Cookie");
649 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
650 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
651 cookie_lock_t *lock = chxj_cookie_lock(r);
652 cookie = chxj_load_cookie(r, value);
653 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
654 if (! no_update_flag && cookie) {
655 cookie = chxj_update_cookie(r, cookie);
657 chxj_cookie_unlock(r, lock);
659 if (cookie && cookie->cookie_id) {
660 if (strlen(result) != 0)
661 result = apr_pstrcat(r->pool, result, "&", NULL);
662 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
666 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
667 if (strlen(result) != 0)
668 result = apr_pstrcat(r->pool, result, "&", NULL);
669 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
672 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
673 if (! no_update_flag) {
674 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
678 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
679 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
685 * It converts it from POSTDATA .
688 * @param src [i] It is POSTDATA character string.
689 * @param len [i/o] It is length of former HTML character string.
696 chxjconvrule_entry *entryp,
706 cookie_t *cookie = NULL;
708 int no_update_flag = 0;
712 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
715 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
716 return apr_pstrdup(r->pool, "");
719 apr_pool_create(&pool, r->pool);
721 s = apr_pstrdup(pool, *src);
723 buff_pre = apr_pstrdup(pool, *src);
725 result = qs_alloc_zero_byte_string(pool);
727 chxj_dump_string(r, APLOG_MARK, "BEFORE input convert source", s, ilen);
732 pair = apr_strtok(buff_pre, "&", &pstate);
738 pair_sv = apr_pstrdup(pool, pair);
740 name = apr_strtok(pair, "=", &vstate);
741 value = apr_strtok(NULL, "=", &vstate);
742 if (! name) continue;
743 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
744 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
754 pair = apr_strtok(s, "&", &pstate);
759 name = apr_strtok(pair, "=", &vstate);
760 value = apr_strtok(NULL, "=", &vstate);
761 if (! name) continue;
762 name = chxj_safe_to_jreserved_tag(r, name);
764 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
765 if (strlen(result) != 0)
766 result = apr_pstrcat(pool, result, "&", NULL);
768 if (strcasecmp(entryp->encoding, "NONE") != 0) {
773 if (value && *value != 0) {
774 value = chxj_url_decode(pool, value);
775 dlen = strlen(value);
776 dvalue = chxj_rencoding(r, value, &dlen);
777 dvalue = chxj_url_encode(pool, dvalue);
783 if (name && *name != 0) {
784 name = chxj_url_decode(pool, name);
786 dname = chxj_rencoding(r, name, &dlen);
787 dname = chxj_url_encode(pool, dname);
794 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
797 result = apr_pstrcat(pool, result, name, "=", value, NULL);
801 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
802 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
803 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
807 if (strlen(value) == 0)
810 if (strlen(result) != 0)
811 result = apr_pstrcat(pool, result, "&", NULL);
813 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
817 dlen = strlen(value);
818 value = chxj_url_decode(pool, value);
819 dvalue = chxj_rencoding(r, value, &dlen);
820 dvalue = chxj_url_encode(pool,dvalue);
821 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
825 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
829 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
831 apr_table_unset(r->headers_in, "Cookie");
832 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
833 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
834 cookie_lock_t *lock = chxj_cookie_lock(r);
835 cookie = chxj_load_cookie(r, value);
836 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
837 if (! no_update_flag && cookie) {
838 cookie = chxj_update_cookie(r, cookie);
840 chxj_cookie_unlock(r, lock);
842 if (cookie && cookie->cookie_id) {
843 if (strlen(result) != 0)
844 result = apr_pstrcat(pool, result, "&", NULL);
845 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
849 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
850 if (strlen(result) != 0)
851 result = apr_pstrcat(pool, result, "&", NULL);
852 result = apr_pstrcat(pool, result, name, "=", value, NULL);
855 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
858 dlen = strlen(value);
860 value = chxj_url_decode(pool, value);
861 dvalue = chxj_rencoding(r, value, &dlen);
862 dvalue = chxj_url_encode(pool,dvalue);
863 if (r->args && strlen(r->args) > 0) {
864 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
867 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
869 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
873 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
876 dlen = strlen(value);
878 value = chxj_url_decode(pool, value);
879 dvalue = chxj_rencoding(r, value, &dlen);
880 dvalue = chxj_url_encode(pool,dvalue);
881 if (r->args && strlen(r->args) > 0) {
882 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
885 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
887 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
890 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
892 *len = strlen(result);
893 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
894 if (! no_update_flag) {
895 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
898 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
899 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
906 * The received data is returned to the filter.
908 * @param f [i/o] It is a filter.
909 * @param data [i] It is data returned to the filter.
910 * @param len [i] It is length of the data returned to the filter.
913 pass_data_to_filter(ap_filter_t *f, const char *data,
916 request_rec *r = f->r;
917 conn_rec *c = r->connection;
919 apr_bucket_brigade *bb;
922 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
924 chxj_header_inf_clear(r);
926 bb = apr_brigade_create(r->pool, c->bucket_alloc);
927 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
929 APR_BRIGADE_INSERT_TAIL(bb, b);
930 b = apr_bucket_eos_create(f->c->bucket_alloc);
931 APR_BRIGADE_INSERT_TAIL(bb, b);
933 rv = ap_pass_brigade(f->next, bb);
934 if (rv != APR_SUCCESS) {
935 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
939 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
945 * Add No Cache Header
948 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
950 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
951 apr_table_unset(r->headers_out, "Pragma");
952 apr_table_unset(r->err_headers_out, "Pragma");
953 apr_table_unset(r->headers_out, "Expires");
954 apr_table_unset(r->err_headers_out, "Expires");
955 apr_table_unset(r->headers_out, "Cache-Control");
956 apr_table_unset(r->err_headers_out, "Cache-Control");
958 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
959 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
960 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
966 * It is the main loop of the output filter.
968 * @param f [i/o] It is a filter.
972 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
978 char *user_agent = NULL;
980 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
981 cookie_t *cookie = NULL;
982 mod_chxj_config *dconf;
983 chxjconvrule_entry *entryp = NULL;
984 device_table *spec = NULL;
988 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
991 apr_pool_create(&pool, r->pool);
993 entryp = ctx->entryp;
995 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
997 if (r->content_type) {
998 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
999 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1000 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1001 && ! (dconf->image == CHXJ_IMG_ON
1002 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1003 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1004 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1005 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1006 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1007 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1008 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1009 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1010 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1011 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1012 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1014 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1015 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1016 cookie_lock_t *lock = NULL;
1017 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1018 switch(spec->html_spec_type) {
1019 case CHXJ_SPEC_Chtml_1_0:
1020 case CHXJ_SPEC_Chtml_2_0:
1021 case CHXJ_SPEC_Chtml_3_0:
1022 case CHXJ_SPEC_Chtml_4_0:
1023 case CHXJ_SPEC_Chtml_5_0:
1024 case CHXJ_SPEC_Chtml_6_0:
1025 case CHXJ_SPEC_Chtml_7_0:
1026 case CHXJ_SPEC_XHtml_Mobile_1_0:
1027 case CHXJ_SPEC_Jhtml:
1028 lock = chxj_cookie_lock(r);
1029 cookie = chxj_save_cookie(r);
1030 s_add_cookie_id_if_has_location_header(r, cookie);
1031 chxj_cookie_unlock(r, lock);
1037 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1038 if (! ap_is_HTTP_REDIRECT(r->status)) {
1039 r->status = HTTP_MOVED_TEMPORARILY;
1042 s_add_no_cache_headers(r, entryp);
1043 ap_pass_brigade(f->next, bb);
1044 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1049 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1050 ap_pass_brigade(f->next, bb);
1051 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1056 for (b = APR_BRIGADE_FIRST(bb);
1057 b != APR_BRIGADE_SENTINEL(bb);
1058 b = APR_BUCKET_NEXT(b)) {
1060 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1061 chxj_dump_string(r, APLOG_MARK, "READ Data", data, len);
1063 /*--------------------------------------------------------------------*/
1065 /*--------------------------------------------------------------------*/
1067 DBG(r, "append data start");
1068 ctx = (mod_chxj_ctx *)f->ctx;
1071 tmp = apr_palloc(r->pool, ctx->len);
1072 memcpy(tmp, ctx->buffer, ctx->len);
1074 ctx->buffer = apr_palloc(pool, ctx->len + len);
1076 memcpy(ctx->buffer, tmp, ctx->len);
1077 memcpy(&ctx->buffer[ctx->len], data, len);
1081 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1084 if (APR_BUCKET_IS_EOS(b)) {
1086 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1087 /*----------------------------------------------------------------------*/
1089 /*----------------------------------------------------------------------*/
1091 cookie_lock_t *lock = NULL;
1092 ctx = (mod_chxj_ctx *)f->ctx;
1094 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1095 lock = chxj_cookie_lock(r);
1097 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1099 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1100 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1101 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1106 tmp = apr_palloc(pool, ctx->len + 1);
1108 memset(tmp, 0, ctx->len + 1);
1109 memcpy(tmp, ctx->buffer, ctx->len);
1111 ctx->buffer = chxj_convert(r,
1112 (const char **)&tmp,
1113 (apr_size_t *)&ctx->len,
1115 user_agent, &cookie);
1119 ctx->buffer = apr_psprintf(r->pool, "\n");
1121 ctx->buffer = chxj_convert(r,
1122 (const char **)&ctx->buffer,
1123 (apr_size_t *)&ctx->len,
1125 user_agent, &cookie);
1130 && *(char *)r->content_type == 't'
1131 && strncmp(r->content_type, "text/xml", 8) == 0) {
1132 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1140 memset(&doc, 0, sizeof(Doc));
1141 memset(&qrcode, 0, sizeof(qr_code_t));
1143 doc.parse_mode = PARSE_MODE_CHTML;
1147 qs_init_malloc(&doc);
1149 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1152 for (child = qs_get_child_node(&doc,root);
1154 child = qs_get_next_node(&doc,child)) {
1155 char *name = qs_get_node_name(&doc,child);
1156 if (strcasecmp("qrcode",name) == 0) {
1161 qs_all_free(&doc,QX_LOGMARK);
1163 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1164 chxj_qrcode_node_to_qrcode(&qrcode, root);
1165 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1167 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1168 chxj_cookie_unlock(r, lock);
1169 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1172 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1176 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1178 && ( *r->content_type == 'i' || *r->content_type == 'I')
1179 && dconf->image == CHXJ_IMG_ON
1180 && strncasecmp("image/", r->content_type, 6) == 0
1181 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1182 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1183 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1184 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1185 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1186 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1187 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1188 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1189 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1190 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1194 tmp = apr_palloc(pool, ctx->len + 1);
1196 memset(tmp, 0, ctx->len + 1);
1197 memcpy(tmp, ctx->buffer, ctx->len);
1199 chxj_convert_image(r,
1200 (const char **)&tmp,
1201 (apr_size_t *)&ctx->len);
1202 if (ctx->buffer == NULL) {
1208 apr_table_unset(r->headers_out, "Content-Length");
1209 apr_table_unset(r->err_headers_out, "Content-Length");
1210 ap_set_content_length(r, (apr_off_t)ctx->len);
1214 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1215 s_add_cookie_id_if_has_location_header(r, cookie);
1216 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1217 if (! ap_is_HTTP_REDIRECT(r->status)) {
1218 r->status = HTTP_MOVED_TEMPORARILY;
1220 ctx->buffer = apr_pstrdup(pool, "");
1222 ap_set_content_length(r, (apr_off_t)ctx->len);
1224 chxj_cookie_unlock(r,lock);
1225 s_add_no_cache_headers(r, entryp);
1226 rv = pass_data_to_filter(f,
1227 (const char *)ctx->buffer,
1228 (apr_size_t)ctx->len);
1231 ctx->buffer = apr_pstrdup(pool, "");
1233 ap_set_content_length(r, (apr_off_t)ctx->len);
1234 chxj_cookie_unlock(r, lock);
1235 s_add_no_cache_headers(r, entryp);
1236 rv = pass_data_to_filter(f,
1237 (const char *)ctx->buffer,
1238 (apr_size_t)ctx->len);
1240 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1244 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1249 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1250 cookie_lock_t *lock = NULL;
1251 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1252 switch(spec->html_spec_type) {
1253 case CHXJ_SPEC_Chtml_1_0:
1254 case CHXJ_SPEC_Chtml_2_0:
1255 case CHXJ_SPEC_Chtml_3_0:
1256 case CHXJ_SPEC_Chtml_4_0:
1257 case CHXJ_SPEC_Chtml_5_0:
1258 case CHXJ_SPEC_Chtml_6_0:
1259 case CHXJ_SPEC_Chtml_7_0:
1260 case CHXJ_SPEC_XHtml_Mobile_1_0:
1261 case CHXJ_SPEC_Jhtml:
1262 lock = chxj_cookie_lock(r);
1263 cookie = chxj_save_cookie(r);
1265 * Location Header Check to add cookie parameter.
1267 s_add_cookie_id_if_has_location_header(r, cookie);
1268 chxj_cookie_unlock(r, lock);
1269 apr_table_unset(r->headers_out, "Set-Cookie");
1270 apr_table_unset(r->err_headers_out, "Set-Cookie");
1277 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1278 if (! ap_is_HTTP_REDIRECT(r->status)) {
1279 r->status = HTTP_MOVED_TEMPORARILY;
1282 apr_table_setn(r->headers_out, "Content-Length", "0");
1283 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1284 s_add_no_cache_headers(r, entryp);
1285 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1286 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1291 apr_brigade_destroy(bb);
1293 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1299 * Add Cookie_id if it has location header.
1302 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1304 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1305 if (! location_header) {
1306 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1308 if (cookie && location_header) {
1309 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1310 location_header = chxj_add_cookie_parameter(r,
1313 apr_table_unset(r->headers_out, "Location");
1314 apr_table_setn(r->headers_out, "Location", location_header);
1315 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1316 if (!ap_is_HTTP_REDIRECT(r->status)) {
1317 r->status = HTTP_MOVED_TEMPORARILY;
1323 * It is the main loop of the input filter handler.
1327 chxj_input_handler(request_rec *r)
1329 mod_chxj_config *dconf;
1330 chxjconvrule_entry *entryp = NULL;
1331 device_table *spec = NULL;
1332 char *post_data = NULL;
1333 apr_size_t post_data_len = 0;
1338 int response_code = 0;
1340 DBG(r, "start of chxj_input_handler()");
1342 if (strcasecmp(r->handler, "chxj-input-handler")) {
1343 DBG(r, "end chxj_input_handler()");
1346 apr_pool_create(&pool, r->pool);
1348 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1349 user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
1350 spec = chxj_specified_device(r, user_agent);
1351 entryp = chxj_apply_convrule(r, dconf->convrules);
1353 post_data = apr_pstrdup(pool, "");
1354 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1355 if (ap_should_client_block(r)) {
1356 while (post_data_len < CHXJ_POST_MAX) {
1359 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1360 if (read_bytes<=0) {
1363 buffer[read_bytes] = '\0';
1364 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1365 post_data_len += read_bytes;
1374 if (post_data_len > 0) {
1375 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1376 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1380 if (dconf->forward_url_base) {
1381 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1384 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);
1387 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1389 DBG(r, "==> new url_path:[%s]", url_path);
1392 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1393 apr_table_unset(r->headers_in, "Content-Length");
1394 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1395 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1396 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1397 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1398 for (ii=0; ii<res_len/64; ii++) {
1399 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1401 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1404 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1405 if (strcasecmp(chunked, "chunked") == 0) {
1407 apr_table_unset(r->headers_out, "Transfer-Encoding");
1410 if (ap_is_HTTP_ERROR(response_code)) {
1411 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1412 return response_code;
1416 apr_pool_create(&wpool, r->pool);
1417 apr_bucket_brigade *bb;
1420 conn_rec *c = r->connection;
1422 bb = apr_brigade_create(wpool, c->bucket_alloc);
1423 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1424 APR_BRIGADE_INSERT_TAIL(bb, e);
1425 e = apr_bucket_eos_create(c->bucket_alloc);
1426 APR_BRIGADE_INSERT_TAIL(bb, e);
1427 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1428 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1431 apr_brigade_cleanup(bb);
1434 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1438 static mod_chxj_global_config *
1439 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1441 mod_chxj_global_config *conf;
1443 SDBG(s, "start chxj_global_config_create()");
1445 /*--------------------------------------------------------------------------*/
1446 /* allocate an own subpool which survives server restarts */
1447 /*--------------------------------------------------------------------------*/
1448 conf = (mod_chxj_global_config *)apr_palloc(pool,
1449 sizeof(mod_chxj_global_config));
1451 conf->cookie_db_lock = NULL;
1453 SDBG(s, "end chxj_global_config_create()");
1460 * initialize chxj module
1463 chxj_init_module(apr_pool_t *p,
1464 apr_pool_t *UNUSED(plog),
1465 apr_pool_t *UNUSED(ptemp),
1471 SDBG(s, "start chxj_init_module()");
1473 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1475 if (user_data == NULL) {
1478 * dummy user_data set.
1480 apr_pool_userdata_set(
1482 CHXJ_MOD_CONFIG_KEY,
1483 apr_pool_cleanup_null,
1485 SDBG(s, "end chxj_init_module()");
1489 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1491 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1493 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1494 return HTTP_INTERNAL_SERVER_ERROR;
1497 SDBG(s, "end chxj_init_module()");
1504 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1507 SDBG(s, "start chxj_child_init()");
1508 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1510 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1512 SDBG(s, "end chxj_child_init()");
1517 * A set structure of each server is generated.
1523 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1525 mod_chxj_global_config *gc;
1527 gc = chxj_global_config_create(p,s);
1534 chxj_translate_name(request_rec *r)
1536 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1537 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1538 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1539 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1540 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1541 return chxj_trans_name(r);
1546 chxj_insert_filter(request_rec *r)
1550 mod_chxj_config *dconf;
1551 chxjconvrule_entry *entryp;
1556 DBG(r, "start chxj_insert_filter()");
1558 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1560 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1562 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1564 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1565 DBG(r, "detect multipart/form-data ==> no target");
1569 spec = chxj_specified_device(r, user_agent);
1570 entryp = chxj_apply_convrule(r, dconf->convrules);
1572 DBG(r, "end chxj_insert_filter()");
1575 ctx = apr_palloc(r->pool, sizeof(*ctx));
1576 memset(ctx, 0, sizeof(*ctx));
1577 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1578 ERR(r, "failed: new pool create. rv:[%d]", rv);
1581 ctx->entryp = entryp;
1583 ctx->buffer = apr_palloc(ctx->pool, 1);
1586 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1591 switch(spec->html_spec_type) {
1592 case CHXJ_SPEC_Chtml_1_0:
1593 case CHXJ_SPEC_Chtml_2_0:
1594 case CHXJ_SPEC_Chtml_3_0:
1595 case CHXJ_SPEC_Chtml_4_0:
1596 case CHXJ_SPEC_Chtml_5_0:
1597 case CHXJ_SPEC_Chtml_6_0:
1598 case CHXJ_SPEC_Chtml_7_0:
1599 case CHXJ_SPEC_XHtml_Mobile_1_0:
1600 case CHXJ_SPEC_Hdml:
1601 case CHXJ_SPEC_Jhtml:
1602 case CHXJ_SPEC_Jxhtml:
1610 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1611 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1612 DBG(r, "added Output Filter");
1615 DBG(r, "end chxj_insert_filter()");
1620 * The hook is registered.
1625 chxj_register_hooks(apr_pool_t *UNUSED(p))
1627 ap_hook_post_config(chxj_init_module,
1630 APR_HOOK_REALLY_FIRST);
1631 ap_hook_child_init(chxj_child_init,
1634 APR_HOOK_REALLY_FIRST);
1635 ap_register_output_filter (
1636 "chxj_output_filter",
1640 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1641 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1642 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1643 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1644 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1645 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1650 * A set structure according to the directory is generated.
1656 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1658 mod_chxj_config *conf;
1660 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1661 conf->device_data_file = NULL;
1662 conf->devices = NULL;
1663 conf->emoji_data_file = NULL;
1665 conf->emoji_tail = NULL;
1666 conf->image = CHXJ_IMG_NONE;
1667 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1668 conf->image_cache_limit = 0;
1669 conf->server_side_encoding = NULL;
1670 conf->cookie_db_dir = NULL;
1671 conf->cookie_timeout = 0;
1672 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1673 conf->cookie_lazy_mode = 0;
1674 #if defined(USE_MYSQL_COOKIE)
1675 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1676 conf->mysql.port = MYSQL_PORT;
1677 conf->mysql.host = NULL;
1679 #if defined(USE_MEMCACHE_COOKIE)
1680 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1681 conf->memcache.host = NULL;
1682 conf->memcache.port = 0;
1684 conf->forward_url_base = NULL;
1685 conf->forward_server_ip = NULL;
1686 conf->allowed_cookie_domain = NULL;
1692 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1693 memset(conf->dir, 0, strlen(arg)+1);
1694 strcpy(conf->dir, arg);
1696 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1698 /* Default is copyleft */
1699 conf->image_copyright = NULL;
1706 * Merge per-directory CHXJ configurations
1709 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1711 mod_chxj_config *base;
1712 mod_chxj_config *add;
1713 mod_chxj_config *mrg;
1715 base = (mod_chxj_config *)basev;
1716 add = (mod_chxj_config *)addv;
1717 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1719 mrg->device_data_file = NULL;
1720 mrg->devices = NULL;
1721 mrg->emoji_data_file = NULL;
1722 mrg->image = CHXJ_IMG_NONE;
1723 mrg->image_cache_dir = NULL;
1724 mrg->image_copyright = NULL;
1725 mrg->image_cache_limit = 0;
1727 mrg->emoji_tail = NULL;
1728 mrg->new_line_type = NLTYPE_NIL;
1729 mrg->forward_url_base = NULL;
1730 mrg->forward_server_ip = NULL;
1731 mrg->allowed_cookie_domain = NULL;
1733 mrg->dir = apr_pstrdup(p, add->dir);
1735 if (! add->device_data_file) {
1736 mrg->devices = base->devices;
1737 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1740 mrg->devices = add->devices;
1741 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1744 if (! add->emoji_data_file) {
1745 mrg->emoji = base->emoji;
1746 mrg->emoji_tail = base->emoji_tail;
1747 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1750 mrg->emoji = add->emoji;
1751 mrg->emoji_tail = add->emoji_tail;
1752 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1755 if (add->image == CHXJ_IMG_NONE) {
1756 mrg->image = base->image;
1759 mrg->image = add->image;
1762 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1763 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1766 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1769 if (add->image_cache_limit) {
1770 mrg->image_cache_limit = add->image_cache_limit;
1773 mrg->image_cache_limit = base->image_cache_limit;
1776 if (add->image_copyright)
1777 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1779 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1781 if (add->server_side_encoding) {
1782 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1785 if (base->server_side_encoding) {
1786 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1789 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1792 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1794 if (add->cookie_db_dir) {
1795 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1798 if (base->cookie_db_dir) {
1799 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1802 mrg->cookie_db_dir = NULL;
1805 if (add->cookie_timeout) {
1806 mrg->cookie_timeout = add->cookie_timeout;
1809 if (base->cookie_db_dir) {
1810 mrg->cookie_timeout = base->cookie_timeout;
1813 mrg->cookie_timeout = 0;
1816 #if defined(USE_MYSQL_COOKIE)
1817 if (add->mysql.host) {
1818 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1820 else if (base->mysql.host) {
1821 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1824 mrg->mysql.host = NULL;
1826 if (add->mysql.port) {
1827 mrg->mysql.port = add->mysql.port;
1829 else if (base->mysql.port) {
1830 mrg->mysql.port = base->mysql.port;
1833 mrg->mysql.port = 0;
1836 if (add->mysql.database) {
1837 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1839 else if (base->mysql.database) {
1840 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1843 mrg->mysql.database = NULL;
1846 if (add->mysql.username) {
1847 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1849 else if (base->mysql.username) {
1850 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1853 mrg->mysql.username = NULL;
1856 if (add->mysql.password) {
1857 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1859 else if (base->mysql.password) {
1860 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1863 mrg->mysql.password = NULL;
1866 if (add->mysql.tablename) {
1867 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1869 else if (base->mysql.tablename) {
1870 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1873 mrg->mysql.tablename = NULL;
1876 if (add->mysql.socket_path) {
1877 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1879 else if (base->mysql.socket_path) {
1880 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1883 mrg->mysql.socket_path = NULL;
1886 if (add->mysql.charset) {
1887 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1889 else if (base->mysql.charset) {
1890 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1893 mrg->mysql.charset = NULL;
1896 #if defined(USE_MEMCACHE_COOKIE)
1897 if (add->memcache.host) {
1898 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1900 else if (base->memcache.host) {
1901 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1904 mrg->memcache.host = NULL;
1906 if (add->memcache.port) {
1907 mrg->memcache.port = add->memcache.port;
1909 else if (base->memcache.port) {
1910 mrg->memcache.port = base->memcache.port;
1913 mrg->memcache.port = 0;
1916 if (add->cookie_store_type) {
1917 mrg->cookie_store_type = add->cookie_store_type;
1919 else if (base->cookie_store_type) {
1920 mrg->cookie_store_type = base->cookie_store_type;
1923 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1925 if (add->cookie_lazy_mode) {
1926 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1928 else if (base->cookie_lazy_mode) {
1929 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1932 mrg->cookie_lazy_mode = 0;
1934 if (add->new_line_type) {
1935 mrg->new_line_type = add->new_line_type;
1937 else if (base->new_line_type) {
1938 mrg->new_line_type = base->new_line_type;
1941 mrg->new_line_type = NLTYPE_NIL;
1944 if (add->forward_url_base) {
1945 mrg->forward_url_base = add->forward_url_base;
1947 else if (base->forward_url_base) {
1948 mrg->forward_url_base = base->forward_url_base;
1951 if (add->allowed_cookie_domain) {
1952 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
1955 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
1962 chxj_command_parse_take5(
1975 for (;*strp == ' '||*strp == '\t'; strp++) ;
1985 for (; *strp != '\0'; strp++) {
1986 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1987 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1992 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1993 || (isquoted && *strp == '"'))
2007 for (;*strp == ' '||*strp == '\t'; strp++) ;
2016 for (; *strp != '\0'; strp++) {
2017 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2018 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2023 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2024 || (isquoted && *strp == '"'))
2037 for (;*strp == ' '||*strp == '\t'; strp++);
2045 for (; *strp != '\0'; strp++) {
2046 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2047 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2052 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2053 || (isquoted && *strp == '"'))
2065 for (;*strp == ' '||*strp == '\t'; strp++);
2073 for (; *strp != '\0'; strp++) {
2074 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2075 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2080 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2081 || (isquoted && *strp == '"'))
2092 for (;*strp == ' '||*strp == '\t'; strp++);
2100 for (; *strp != '\0'; strp++) {
2101 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2102 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2107 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2108 || (isquoted && *strp == '"'))
2118 * The device definition file is loaded.
2120 * @param arg [i] The name of the device definition file is specified.
2121 * @param mconfig [i/o] The pointer to a set structure is specified.
2125 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2127 mod_chxj_config *conf;
2132 if (strlen(arg) > 256)
2133 return "mod_chxj: device data filename too long.";
2135 conf = (mod_chxj_config *)mconfig;
2136 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2138 qs_init_malloc(&doc);
2139 qs_init_root_node(&doc);
2141 qs_parse_file((Doc *)&doc, (const char *)arg);
2142 chxj_load_device_data(&doc,parms->pool, conf);
2143 qs_all_free(&doc, QX_LOGMARK);
2150 * Device definition information is loaded.
2153 * @param arg [i] The name of the device definition file is specified.
2154 * @param mconfig [i/o] The pointer to a set structure is specified.
2158 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2160 mod_chxj_config *conf;
2167 if (strlen(arg) > 256)
2168 return "mod_chxj: emoji data filename too long.";
2170 conf = (mod_chxj_config *)mconfig;
2171 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2172 qs_init_malloc(&doc);
2173 qs_init_root_node(&doc);
2175 qs_parse_file((Doc *)&doc, (const char *)arg);
2177 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2179 qs_all_free(&doc, QX_LOGMARK);
2187 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2189 mod_chxj_config *conf;
2194 if (strlen(arg) > 256)
2195 return "image uri is too long.";
2197 conf = (mod_chxj_config*)mconfig;
2198 if (strcasecmp("ON", arg) == 0) {
2199 conf->image = CHXJ_IMG_ON;
2202 conf->image = CHXJ_IMG_OFF;
2210 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2212 mod_chxj_config *conf;
2217 if (strlen(arg) > 256)
2218 return "cache dir name is too long.";
2220 conf = (mod_chxj_config *)mconfig;
2221 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2228 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2230 mod_chxj_config *conf;
2235 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2236 return "cache size is too long.";
2238 conf = (mod_chxj_config *)mconfig;
2241 * I use strtol function because strtoul is not portable function.
2243 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2246 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2248 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2257 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2259 mod_chxj_config *conf;
2264 if (strlen(arg) > 256)
2265 return "Copyright Flag is too long.";
2267 conf = (mod_chxj_config *)mconfig;
2268 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2275 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2279 mod_chxj_config *dconf;
2280 chxjconvrule_entry *newrule;
2290 dconf = (mod_chxj_config *)mconfig;
2292 if (strlen(arg) > 4096)
2293 return "mod_chxj: ChxjConvertRule: is too long.";
2295 dconf = (mod_chxj_config *)mconfig;
2296 if (dconf->convrules == NULL)
2297 dconf->convrules = apr_array_make(cmd->pool,
2299 sizeof(chxjconvrule_entry));
2301 newrule = apr_array_push(dconf->convrules);
2304 newrule->action = 0;
2306 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2307 return "ChxjConvertRule: bad argument line";
2309 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2313 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2319 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2320 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2323 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2324 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2327 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2328 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2331 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2332 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2338 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2339 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2341 else if (strcasecmp(CONVRULE_COOKIE_OFF_CMD, action) == 0) {
2342 newrule->action &= (0xffffffff ^ CONVRULE_COOKIE_ON_BIT);
2344 else if (strcasecmp(CONVRULE_CSS_ON_CMD, action) == 0) {
2345 newrule->action |= CONVRULE_CSS_ON_BIT;
2347 else if (strcasecmp(CONVRULE_CSS_OFF_CMD, action) == 0) {
2348 newrule->action &= (0xffffffff ^ CONVRULE_CSS_ON_BIT);
2354 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2355 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2361 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2362 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2368 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2369 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2375 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2376 newrule->action |= CONVRULE_Z2H_ON_BIT;
2379 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2380 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2383 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2384 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2387 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2388 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2391 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2392 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2395 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2396 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2399 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2400 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2403 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2404 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2415 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2419 mode = AP_REG_EXTENDED;
2420 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2421 return "RewriteRule: cannot compile regular expression ";
2423 newrule->regexp = regexp;
2425 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2427 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2429 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2431 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2432 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2434 newrule->user_agent = NULL;
2436 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2448 mod_chxj_config *dconf;
2451 if (strlen(arg) > 4096)
2452 return "mod_chxj: ChxjCookieDir is too long.";
2454 dconf = (mod_chxj_config *)mconfig;
2456 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2463 cmd_set_cookie_timeout(
2464 cmd_parms *UNUSED(cmd),
2468 mod_chxj_config *dconf;
2470 if (strlen(arg) > 4096)
2471 return "mod_chxj: ChxjCookieTimeout is too long.";
2473 if (chxj_chk_numeric(arg) != 0)
2474 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2476 dconf = (mod_chxj_config *)mconfig;
2478 dconf->cookie_timeout = atoi(arg);
2484 #if defined(USE_MYSQL_COOKIE)
2486 cmd_set_cookie_mysql_database(
2491 mod_chxj_config *dconf;
2493 if (strlen(arg) > 255)
2494 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2496 dconf = (mod_chxj_config *)mconfig;
2498 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2505 cmd_set_cookie_mysql_username(
2510 mod_chxj_config *dconf;
2512 if (strlen(arg) > 255)
2513 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2515 dconf = (mod_chxj_config *)mconfig;
2517 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2524 cmd_set_cookie_mysql_password(
2529 mod_chxj_config *dconf;
2531 if (strlen(arg) > 255)
2532 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2534 dconf = (mod_chxj_config *)mconfig;
2536 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2543 cmd_set_cookie_mysql_table_name(
2548 mod_chxj_config *dconf;
2550 if (strlen(arg) > 255)
2551 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2553 dconf = (mod_chxj_config *)mconfig;
2555 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2561 cmd_set_cookie_mysql_port(
2562 cmd_parms *UNUSED(cmd),
2566 mod_chxj_config *dconf;
2568 if (strlen(arg) > 255)
2569 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2571 dconf = (mod_chxj_config *)mconfig;
2573 if (chxj_chk_numeric(arg) != 0)
2574 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2576 dconf = (mod_chxj_config *)mconfig;
2578 dconf->mysql.port = chxj_atoi(arg);
2585 cmd_set_cookie_mysql_host(
2590 mod_chxj_config *dconf;
2592 if (strlen(arg) > 255)
2593 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2595 dconf = (mod_chxj_config *)mconfig;
2597 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2604 cmd_set_cookie_mysql_socket_path(
2609 mod_chxj_config *dconf;
2611 if (strlen(arg) > 4096)
2612 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2614 dconf = (mod_chxj_config *)mconfig;
2616 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2623 cmd_set_cookie_mysql_charset(
2628 mod_chxj_config *dconf;
2630 if (strlen(arg) > 255)
2631 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2633 dconf = (mod_chxj_config *)mconfig;
2635 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2640 #if defined(USE_MEMCACHE_COOKIE)
2642 cmd_set_cookie_memcache_port(
2643 cmd_parms *UNUSED(cmd),
2647 mod_chxj_config *dconf;
2649 if (strlen(arg) > 255)
2650 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2652 dconf = (mod_chxj_config *)mconfig;
2654 if (chxj_chk_numeric(arg) != 0)
2655 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2657 dconf = (mod_chxj_config *)mconfig;
2659 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2666 cmd_set_cookie_memcache_host(
2671 mod_chxj_config *dconf;
2673 if (strlen(arg) > 255)
2674 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2676 dconf = (mod_chxj_config *)mconfig;
2678 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2685 cmd_set_cookie_lazy_mode(
2686 cmd_parms *UNUSED(cmd),
2690 mod_chxj_config *dconf;
2692 if (strlen(arg) > 255)
2693 return "mod_chxj: ChxjCookieLazyMode is too long.";
2695 dconf = (mod_chxj_config *)mconfig;
2697 if (strcasecmp("TRUE",arg) == 0) {
2698 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2701 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2708 cmd_set_cookie_store_type(
2709 cmd_parms *UNUSED(cmd),
2713 mod_chxj_config *dconf;
2715 if (strlen(arg) > 255)
2716 return "mod_chxj: ChxjCookieStoreType is too long.";
2718 dconf = (mod_chxj_config *)mconfig;
2720 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2721 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2723 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2724 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2726 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2727 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2730 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2737 cmd_set_forward_url_base(
2742 mod_chxj_config *dconf;
2744 if (strlen(arg) > 255)
2745 return "mod_chxj: ChxjForwardUrlBase is too long.";
2747 dconf = (mod_chxj_config *)mconfig;
2749 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2755 cmd_set_forward_server_ip(
2760 mod_chxj_config *dconf;
2762 if (strlen(arg) > 255)
2763 return "mod_chxj: ChxjForwardServerIp is too long.";
2765 dconf = (mod_chxj_config *)mconfig;
2767 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2773 cmd_allowed_cookie_domain(
2778 mod_chxj_config *dconf;
2780 if (strlen(arg) > 255)
2781 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2783 dconf = (mod_chxj_config *)mconfig;
2785 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2791 cmd_set_new_line_type(
2792 cmd_parms *UNUSED(cmd),
2796 mod_chxj_config *dconf;
2797 if (strlen(arg) > 255)
2798 return "mod_chxj: ChxjNewLineType is too long.";
2800 dconf = (mod_chxj_config *)mconfig;
2802 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2803 dconf->new_line_type = NLTYPE_CRLF;
2805 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2806 dconf->new_line_type = NLTYPE_LF;
2808 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2809 dconf->new_line_type = NLTYPE_CR;
2811 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2812 dconf->new_line_type = NLTYPE_NONE;
2815 return "mod_chxj: invalid value (ChxjNewLineType)";
2821 static const command_rec cmds[] = {
2823 "ChxjLoadDeviceData",
2824 cmd_load_device_data,
2827 "Load Device Data"),
2829 "ChxjLoadEmojiData",
2830 cmd_load_emoji_data,
2836 cmd_set_image_engine,
2839 "Convert Target URI"),
2841 "ChxjImageCacheDir",
2842 cmd_set_image_cache_dir,
2845 "Image Cache Directory"),
2847 "ChxjImageCacheLimit",
2848 cmd_set_image_cache_limit,
2851 "Image Cache Limit"),
2853 "ChxjImageCopyright",
2854 cmd_set_image_copyright,
2863 "an URL-applied regexp-pattern and a substitution URL"),
2869 "save cookie.db directory."),
2871 "ChxjCookieTimeout",
2872 cmd_set_cookie_timeout,
2875 "The compulsion time-out time of the cookie is specified. "),
2877 "ChxjCookieStoreType",
2878 cmd_set_cookie_store_type,
2881 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2883 "ChxjCookieLazyMode",
2884 cmd_set_cookie_lazy_mode,
2887 "OneTimeID is negligently done. (TRUE/FALSE)"),
2888 #if defined(USE_MYSQL_COOKIE)
2890 "ChxjCookieMysqlHost",
2891 cmd_set_cookie_mysql_host,
2894 "The MySQL database host used by saving Cookie"),
2896 "ChxjCookieMysqlPort",
2897 cmd_set_cookie_mysql_port,
2900 "The MySQL database port used by saving Cookie"),
2902 "ChxjCookieMysqlDatabase",
2903 cmd_set_cookie_mysql_database,
2906 "The MySQL database name used by saving Cookie"),
2908 "ChxjCookieMysqlUsername",
2909 cmd_set_cookie_mysql_username,
2912 "The MySQL username used by saving Cookie"),
2914 "ChxjCookieMysqlPassword",
2915 cmd_set_cookie_mysql_password,
2918 "The MySQL password used by saving Cookie"),
2920 "ChxjCookieMysqlTableName",
2921 cmd_set_cookie_mysql_table_name,
2924 "The MySQL table name used by saving Cookie"),
2926 "ChxjCookieMysqlSocketPath",
2927 cmd_set_cookie_mysql_socket_path,
2930 "The MySQL socket path used by saving Cookie"),
2932 "ChxjCookieMysqlCharset",
2933 cmd_set_cookie_mysql_charset,
2936 "The MySQL charset used by saving Cookie"),
2938 #if defined(USE_MEMCACHE_COOKIE)
2940 "ChxjCookieMemcacheHost",
2941 cmd_set_cookie_memcache_host,
2944 "The Memcached host used by saving Cookie"),
2946 "ChxjCookieMemcachePort",
2947 cmd_set_cookie_memcache_port,
2950 "The Memcached port used by saving Cookie"),
2954 cmd_set_new_line_type,
2957 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
2959 "ChxjForwardUrlBase",
2960 cmd_set_forward_url_base,
2963 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2965 "ChxjForwardServerIp",
2966 cmd_set_forward_server_ip,
2969 "The forward server ip(default: this server ip)"),
2971 "ChxjAllowedCookieDomain",
2972 cmd_allowed_cookie_domain,
2975 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
2980 /*----------------------------------------------------------------------------*/
2981 /* Dispatch list for API hooks */
2982 /*----------------------------------------------------------------------------*/
2983 module AP_MODULE_DECLARE_DATA chxj_module = {
2984 STANDARD20_MODULE_STUFF,
2985 chxj_create_per_dir_config, /* create per-dir config structures */
2986 chxj_merge_per_dir_config, /* merge per-dir config structures */
2987 chxj_config_server_create, /* create per-server config structures */
2988 NULL, /* merge per-server config structures */
2989 cmds, /* table of config file commands */
2990 chxj_register_hooks /* register hooks */