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) spec->device_name[%s] User-Agent:[%s]", (unsigned int)(apr_size_t)r, spec->device_name, user_agent);
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 if (dconf->post_log) {
268 apr_table_setn(r->subprocess_env, dconf->post_log, dconf->post_log);
271 apr_table_setn(r->subprocess_env, "chxj-post-log", "chxj-post-log");
273 apr_sockaddr_t *address = NULL;
274 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
275 if (rv != APR_SUCCESS) {
277 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));
278 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
282 if (dconf->forward_server_ip) {
283 addr = dconf->forward_server_ip;
286 apr_sockaddr_ip_get(&addr, address);
288 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);
289 if (strcmp(addr, r->connection->remote_ip) == 0) {
290 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
292 if (! apr_table_get(r->headers_in, "Content-Length")) {
293 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
295 apr_table_set(r->headers_in, "Content-Length", contentLength);
302 chxj_add_device_env(r, spec);
304 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
311 s_clear_cookie_header(request_rec *r, device_table *spec)
313 switch(spec->html_spec_type) {
314 case CHXJ_SPEC_Chtml_1_0:
315 case CHXJ_SPEC_Chtml_2_0:
316 case CHXJ_SPEC_Chtml_3_0:
317 case CHXJ_SPEC_Chtml_4_0:
318 case CHXJ_SPEC_Chtml_5_0:
319 case CHXJ_SPEC_Chtml_6_0:
320 case CHXJ_SPEC_Chtml_7_0:
321 case CHXJ_SPEC_XHtml_Mobile_1_0:
322 case CHXJ_SPEC_Jhtml:
323 apr_table_unset(r->headers_in, "Cookie");
332 * It converts it from CHTML into XXML corresponding to each model.
335 * @param src [i] It is former HTML character string.
336 * @param len [i/o] It is length of former HTML character string.
339 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
345 mod_chxj_config *dconf;
346 chxjconvrule_entry *entryp;
348 DBG(r,"REQ[%X] start of chxj_convert()", (unsigned int)(apr_size_t)r);
350 chxj_dump_string(r, APLOG_MARK, "INPUT Data", *src, *len);
352 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
354 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
357 entryp = chxj_apply_convrule(r, dconf->convrules);
359 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
360 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
365 /*------------------------------------------------------------------------*/
366 /* get UserAgent from http header */
367 /*------------------------------------------------------------------------*/
368 if (entryp->user_agent)
369 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
371 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
373 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
374 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
377 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
378 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
379 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
380 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
384 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
386 spec = chxj_specified_device(r, user_agent);
393 if (entryp->action & CONVRULE_COOKIE_ON_BIT
394 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
395 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
396 switch(spec->html_spec_type) {
397 case CHXJ_SPEC_Chtml_1_0:
398 case CHXJ_SPEC_Chtml_2_0:
399 case CHXJ_SPEC_Chtml_3_0:
400 case CHXJ_SPEC_Chtml_4_0:
401 case CHXJ_SPEC_Chtml_5_0:
402 case CHXJ_SPEC_Chtml_6_0:
403 case CHXJ_SPEC_Chtml_7_0:
404 case CHXJ_SPEC_XHtml_Mobile_1_0:
405 case CHXJ_SPEC_Jhtml:
406 cookie = chxj_save_cookie(r);
413 if (!r->header_only) {
416 if (convert_routine[spec->html_spec_type].encoder)
417 tmp = convert_routine[spec->html_spec_type].encoder(r,
421 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
423 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
426 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
428 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
429 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
434 dst = apr_palloc(r->pool, 1);
439 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
442 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
443 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
444 if (convert_routine[spec->html_spec_type].converter) {
446 dst = convert_routine[spec->html_spec_type].converter(r,
454 dst = convert_routine[spec->html_spec_type].converter(r,
464 dst = chxj_conv_z2h(r, dst, len, entryp);
469 ap_set_content_length(r, *len);
472 dst = apr_psprintf(r->pool, "\n");
481 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
488 * convert GUID parameter.
492 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
494 DBG(r, "REQ[%X] start s_convert_guid_parameter() param:[%s]", (unsigned int)(apr_size_t)r, param);
495 if (strcasecmp(param, "guid") == 0) {
496 switch(spec->html_spec_type) {
497 case CHXJ_SPEC_XHtml_Mobile_1_0:
499 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
501 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
505 case CHXJ_SPEC_Jhtml:
506 case CHXJ_SPEC_Jxhtml:
508 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
510 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
518 DBG(r, "REQ[%X] end s_convert_guid_parameter()", (unsigned int)(apr_size_t)r);
522 * It converts it from HEADER.
527 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
539 cookie_t *cookie = NULL;
540 int no_update_flag = 0;
542 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
545 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
546 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
549 urilen = strlen(r->args);
551 result = qs_alloc_zero_byte_string(r->pool);
553 buff_pre = apr_pstrdup(r->pool, r->args);
558 pair = apr_strtok(buff_pre, "&", &pstate);
564 pair_sv = apr_pstrdup(r->pool, pair);
566 name = apr_strtok(pair, "=", &vstate);
567 value = apr_strtok(NULL, "=", &vstate);
568 if (! name) continue;
569 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
570 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
575 buff = apr_pstrdup(r->pool, r->args);
576 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
585 pair = apr_strtok(buff, "&", &pstate);
591 pair_sv = apr_pstrdup(r->pool, pair);
593 name = apr_strtok(pair, "=", &vstate);
594 value = apr_strtok(NULL, "=", &vstate);
595 if (! name) continue;
596 name = chxj_safe_to_jreserved_tag(r, name);
597 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
598 if (strlen(result) != 0)
599 result = apr_pstrcat(r->pool, result, "&", NULL);
601 if (strcasecmp(entryp->encoding, "NONE") != 0) {
606 if (value && *value != 0) {
607 value = chxj_url_decode(r->pool, value);
608 dlen = strlen(value);
609 DBG(r, "************ before encoding[%s]", value);
611 dvalue = chxj_rencoding(r, value, &dlen);
612 dvalue = chxj_url_encode(r->pool, dvalue);
614 DBG(r, "************ after encoding[%s]", dvalue);
620 if (name && *name != 0) {
621 name = chxj_url_decode(r->pool, name);
623 dname = chxj_rencoding(r, name, &dlen);
624 dname = chxj_url_encode(r->pool, dname);
629 s_convert_guid_parameter_to_header(r, dname, spec);
630 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
633 s_convert_guid_parameter_to_header(r, name, spec);
634 if (strcmp(name, pair_sv) != 0) {
635 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
638 result = apr_pstrcat(r->pool, result, name, NULL);
643 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
644 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
645 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
649 if (strlen(value) == 0)
652 if (strlen(result) != 0)
653 result = apr_pstrcat(r->pool, result, "&", NULL);
655 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
658 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
660 apr_table_unset(r->headers_in, "Cookie");
661 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
662 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
663 cookie_lock_t *lock = chxj_cookie_lock(r);
664 cookie = chxj_load_cookie(r, value);
665 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
666 if (! no_update_flag && cookie) {
667 cookie = chxj_update_cookie(r, cookie);
669 chxj_cookie_unlock(r, lock);
671 if (cookie && cookie->cookie_id) {
672 if (strlen(result) != 0)
673 result = apr_pstrcat(r->pool, result, "&", NULL);
674 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
678 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
679 if (strlen(result) != 0)
680 result = apr_pstrcat(r->pool, result, "&", NULL);
681 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
684 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
685 if (! no_update_flag) {
686 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
690 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
691 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
697 * It converts it from POSTDATA .
700 * @param src [i] It is POSTDATA character string.
701 * @param len [i/o] It is length of former HTML character string.
708 chxjconvrule_entry *entryp,
718 cookie_t *cookie = NULL;
720 int no_update_flag = 0;
724 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
727 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
728 return apr_pstrdup(r->pool, "");
731 apr_pool_create(&pool, r->pool);
733 s = apr_pstrdup(pool, *src);
735 buff_pre = apr_pstrdup(pool, *src);
737 result = qs_alloc_zero_byte_string(pool);
739 chxj_dump_string(r, APLOG_MARK, "BEFORE input convert source", s, ilen);
744 pair = apr_strtok(buff_pre, "&", &pstate);
750 pair_sv = apr_pstrdup(pool, pair);
752 name = apr_strtok(pair, "=", &vstate);
753 value = apr_strtok(NULL, "=", &vstate);
754 if (! name) continue;
755 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
756 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
766 pair = apr_strtok(s, "&", &pstate);
771 name = apr_strtok(pair, "=", &vstate);
772 value = apr_strtok(NULL, "=", &vstate);
773 if (! name) continue;
774 name = chxj_safe_to_jreserved_tag(r, name);
776 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
777 if (strlen(result) != 0)
778 result = apr_pstrcat(pool, result, "&", NULL);
780 if (strcasecmp(entryp->encoding, "NONE") != 0) {
785 if (value && *value != 0) {
786 value = chxj_url_decode(pool, value);
787 dlen = strlen(value);
788 dvalue = chxj_rencoding(r, value, &dlen);
789 dvalue = chxj_url_encode(pool, dvalue);
795 if (name && *name != 0) {
796 name = chxj_url_decode(pool, name);
798 dname = chxj_rencoding(r, name, &dlen);
799 dname = chxj_url_encode(pool, dname);
806 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
809 result = apr_pstrcat(pool, result, name, "=", value, NULL);
813 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
814 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
815 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
819 if (strlen(value) == 0)
822 if (strlen(result) != 0)
823 result = apr_pstrcat(pool, result, "&", NULL);
825 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
829 dlen = strlen(value);
830 value = chxj_url_decode(pool, value);
831 dvalue = chxj_rencoding(r, value, &dlen);
832 dvalue = chxj_url_encode(pool,dvalue);
833 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
837 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
841 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
843 apr_table_unset(r->headers_in, "Cookie");
844 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
845 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
846 cookie_lock_t *lock = chxj_cookie_lock(r);
847 cookie = chxj_load_cookie(r, value);
848 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
849 if (! no_update_flag && cookie) {
850 cookie = chxj_update_cookie(r, cookie);
852 chxj_cookie_unlock(r, lock);
854 if (cookie && cookie->cookie_id) {
855 if (strlen(result) != 0)
856 result = apr_pstrcat(pool, result, "&", NULL);
857 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
861 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
862 if (strlen(result) != 0)
863 result = apr_pstrcat(pool, result, "&", NULL);
864 result = apr_pstrcat(pool, result, name, "=", value, NULL);
867 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
870 dlen = strlen(value);
872 value = chxj_url_decode(pool, value);
873 dvalue = chxj_rencoding(r, value, &dlen);
874 dvalue = chxj_url_encode(pool,dvalue);
875 if (r->args && strlen(r->args) > 0) {
876 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
879 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
881 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
885 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
888 dlen = strlen(value);
890 value = chxj_url_decode(pool, value);
891 dvalue = chxj_rencoding(r, value, &dlen);
892 dvalue = chxj_url_encode(pool,dvalue);
893 if (r->args && strlen(r->args) > 0) {
894 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
897 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
899 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
902 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
904 *len = strlen(result);
905 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
906 if (! no_update_flag) {
907 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
910 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
911 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
918 * The received data is returned to the filter.
920 * @param f [i/o] It is a filter.
921 * @param data [i] It is data returned to the filter.
922 * @param len [i] It is length of the data returned to the filter.
925 pass_data_to_filter(ap_filter_t *f, const char *data,
928 request_rec *r = f->r;
929 conn_rec *c = r->connection;
931 apr_bucket_brigade *bb;
934 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
936 chxj_header_inf_clear(r);
938 bb = apr_brigade_create(r->pool, c->bucket_alloc);
939 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
941 APR_BRIGADE_INSERT_TAIL(bb, b);
942 b = apr_bucket_eos_create(f->c->bucket_alloc);
943 APR_BRIGADE_INSERT_TAIL(bb, b);
945 rv = ap_pass_brigade(f->next, bb);
946 if (rv != APR_SUCCESS) {
947 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
951 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
957 * Add No Cache Header
960 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
962 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
963 apr_table_unset(r->headers_out, "Pragma");
964 apr_table_unset(r->err_headers_out, "Pragma");
965 apr_table_unset(r->headers_out, "Expires");
966 apr_table_unset(r->err_headers_out, "Expires");
967 apr_table_unset(r->headers_out, "Cache-Control");
968 apr_table_unset(r->err_headers_out, "Cache-Control");
970 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
971 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
972 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
978 * It is the main loop of the output filter.
980 * @param f [i/o] It is a filter.
984 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
990 char *user_agent = NULL;
992 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
993 cookie_t *cookie = NULL;
994 mod_chxj_config *dconf;
995 chxjconvrule_entry *entryp = NULL;
996 device_table *spec = NULL;
1000 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1003 apr_pool_create(&pool, r->pool);
1005 entryp = ctx->entryp;
1007 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1009 if (r->content_type) {
1010 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1011 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1012 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1013 && ! (dconf->image == CHXJ_IMG_ON
1014 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1015 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1016 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1017 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1018 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1019 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1020 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1021 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1022 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1023 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1024 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1026 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1027 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1028 cookie_lock_t *lock = NULL;
1029 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1030 switch(spec->html_spec_type) {
1031 case CHXJ_SPEC_Chtml_1_0:
1032 case CHXJ_SPEC_Chtml_2_0:
1033 case CHXJ_SPEC_Chtml_3_0:
1034 case CHXJ_SPEC_Chtml_4_0:
1035 case CHXJ_SPEC_Chtml_5_0:
1036 case CHXJ_SPEC_Chtml_6_0:
1037 case CHXJ_SPEC_Chtml_7_0:
1038 case CHXJ_SPEC_XHtml_Mobile_1_0:
1039 case CHXJ_SPEC_Jhtml:
1040 lock = chxj_cookie_lock(r);
1041 cookie = chxj_save_cookie(r);
1042 s_add_cookie_id_if_has_location_header(r, cookie);
1043 chxj_cookie_unlock(r, lock);
1049 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1050 if (! ap_is_HTTP_REDIRECT(r->status)) {
1051 r->status = HTTP_MOVED_TEMPORARILY;
1054 s_add_no_cache_headers(r, entryp);
1055 ap_pass_brigade(f->next, bb);
1056 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1061 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1062 ap_pass_brigade(f->next, bb);
1063 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1068 for (b = APR_BRIGADE_FIRST(bb);
1069 b != APR_BRIGADE_SENTINEL(bb);
1070 b = APR_BUCKET_NEXT(b)) {
1072 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1073 chxj_dump_string(r, APLOG_MARK, "READ Data", data, len);
1075 /*--------------------------------------------------------------------*/
1077 /*--------------------------------------------------------------------*/
1079 DBG(r, "append data start");
1080 ctx = (mod_chxj_ctx *)f->ctx;
1083 tmp = apr_palloc(r->pool, ctx->len);
1084 memcpy(tmp, ctx->buffer, ctx->len);
1086 ctx->buffer = apr_palloc(pool, ctx->len + len);
1088 memcpy(ctx->buffer, tmp, ctx->len);
1089 memcpy(&ctx->buffer[ctx->len], data, len);
1093 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1096 if (APR_BUCKET_IS_EOS(b)) {
1098 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1099 /*----------------------------------------------------------------------*/
1101 /*----------------------------------------------------------------------*/
1103 cookie_lock_t *lock = NULL;
1104 ctx = (mod_chxj_ctx *)f->ctx;
1106 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1107 lock = chxj_cookie_lock(r);
1109 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1111 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1112 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1113 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1118 tmp = apr_palloc(pool, ctx->len + 1);
1120 memset(tmp, 0, ctx->len + 1);
1121 memcpy(tmp, ctx->buffer, ctx->len);
1123 ctx->buffer = chxj_convert(r,
1124 (const char **)&tmp,
1125 (apr_size_t *)&ctx->len,
1127 user_agent, &cookie);
1131 ctx->buffer = apr_psprintf(r->pool, "\n");
1133 ctx->buffer = chxj_convert(r,
1134 (const char **)&ctx->buffer,
1135 (apr_size_t *)&ctx->len,
1137 user_agent, &cookie);
1142 && *(char *)r->content_type == 't'
1143 && strncmp(r->content_type, "text/xml", 8) == 0) {
1144 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1152 memset(&doc, 0, sizeof(Doc));
1153 memset(&qrcode, 0, sizeof(qr_code_t));
1155 doc.parse_mode = PARSE_MODE_CHTML;
1159 qs_init_malloc(&doc);
1161 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1164 for (child = qs_get_child_node(&doc,root);
1166 child = qs_get_next_node(&doc,child)) {
1167 char *name = qs_get_node_name(&doc,child);
1168 if (strcasecmp("qrcode",name) == 0) {
1173 qs_all_free(&doc,QX_LOGMARK);
1175 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1176 chxj_qrcode_node_to_qrcode(&qrcode, root);
1177 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1179 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1180 chxj_cookie_unlock(r, lock);
1181 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1184 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1188 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1190 && ( *r->content_type == 'i' || *r->content_type == 'I')
1191 && dconf->image == CHXJ_IMG_ON
1192 && strncasecmp("image/", r->content_type, 6) == 0
1193 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1194 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1195 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1196 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1197 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1198 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1199 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1200 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1201 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1202 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1206 tmp = apr_palloc(pool, ctx->len + 1);
1208 memset(tmp, 0, ctx->len + 1);
1209 memcpy(tmp, ctx->buffer, ctx->len);
1211 chxj_convert_image(r,
1212 (const char **)&tmp,
1213 (apr_size_t *)&ctx->len);
1214 if (ctx->buffer == NULL) {
1220 apr_table_unset(r->headers_out, "Content-Length");
1221 apr_table_unset(r->err_headers_out, "Content-Length");
1222 ap_set_content_length(r, (apr_off_t)ctx->len);
1226 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1227 s_add_cookie_id_if_has_location_header(r, cookie);
1228 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1229 if (! ap_is_HTTP_REDIRECT(r->status)) {
1230 r->status = HTTP_MOVED_TEMPORARILY;
1233 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1234 ctx->buffer = apr_pstrdup(pool, "");
1236 ap_set_content_length(r, (apr_off_t)ctx->len);
1238 chxj_cookie_unlock(r,lock);
1239 s_add_no_cache_headers(r, entryp);
1240 rv = pass_data_to_filter(f,
1241 (const char *)ctx->buffer,
1242 (apr_size_t)ctx->len);
1245 ctx->buffer = apr_pstrdup(pool, "");
1247 ap_set_content_length(r, (apr_off_t)ctx->len);
1248 chxj_cookie_unlock(r, lock);
1249 s_add_no_cache_headers(r, entryp);
1250 rv = pass_data_to_filter(f,
1251 (const char *)ctx->buffer,
1252 (apr_size_t)ctx->len);
1254 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1258 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1263 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1264 cookie_lock_t *lock = NULL;
1265 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1266 switch(spec->html_spec_type) {
1267 case CHXJ_SPEC_Chtml_1_0:
1268 case CHXJ_SPEC_Chtml_2_0:
1269 case CHXJ_SPEC_Chtml_3_0:
1270 case CHXJ_SPEC_Chtml_4_0:
1271 case CHXJ_SPEC_Chtml_5_0:
1272 case CHXJ_SPEC_Chtml_6_0:
1273 case CHXJ_SPEC_Chtml_7_0:
1274 case CHXJ_SPEC_XHtml_Mobile_1_0:
1275 case CHXJ_SPEC_Jhtml:
1276 lock = chxj_cookie_lock(r);
1277 cookie = chxj_save_cookie(r);
1279 * Location Header Check to add cookie parameter.
1281 s_add_cookie_id_if_has_location_header(r, cookie);
1282 chxj_cookie_unlock(r, lock);
1283 apr_table_unset(r->headers_out, "Set-Cookie");
1284 apr_table_unset(r->err_headers_out, "Set-Cookie");
1291 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1292 if (! ap_is_HTTP_REDIRECT(r->status)) {
1293 r->status = HTTP_MOVED_TEMPORARILY;
1296 apr_table_setn(r->headers_out, "Content-Length", "0");
1297 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1298 s_add_no_cache_headers(r, entryp);
1299 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1300 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1305 apr_brigade_destroy(bb);
1307 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1313 * Add Cookie_id if it has location header.
1316 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1318 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1319 if (! location_header) {
1320 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1322 if (cookie && location_header) {
1323 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1324 location_header = chxj_add_cookie_parameter(r,
1327 apr_table_unset(r->headers_out, "Location");
1328 apr_table_setn(r->headers_out, "Location", location_header);
1329 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1330 if (!ap_is_HTTP_REDIRECT(r->status)) {
1331 r->status = HTTP_MOVED_TEMPORARILY;
1337 * It is the main loop of the input filter handler.
1341 chxj_input_handler(request_rec *r)
1343 mod_chxj_config *dconf;
1344 chxjconvrule_entry *entryp = NULL;
1345 device_table *spec = NULL;
1346 char *post_data = NULL;
1347 apr_size_t post_data_len = 0;
1352 int response_code = 0;
1354 DBG(r, "start of chxj_input_handler()");
1356 if (strcasecmp(r->handler, "chxj-input-handler")) {
1357 DBG(r, "end chxj_input_handler()");
1360 apr_pool_create(&pool, r->pool);
1362 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1363 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1365 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1367 spec = chxj_specified_device(r, user_agent);
1368 entryp = chxj_apply_convrule(r, dconf->convrules);
1370 post_data = apr_pstrdup(pool, "");
1371 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1372 if (ap_should_client_block(r)) {
1373 while (post_data_len < CHXJ_POST_MAX) {
1376 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1377 if (read_bytes<=0) {
1380 buffer[read_bytes] = '\0';
1381 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1382 post_data_len += read_bytes;
1391 if (post_data_len > 0) {
1392 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1393 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1397 if (dconf->forward_url_base) {
1398 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1401 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);
1404 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1406 DBG(r, "==> new url_path:[%s]", url_path);
1409 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1410 apr_table_unset(r->headers_in, "Content-Length");
1411 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1412 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1413 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1414 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1415 for (ii=0; ii<res_len/64; ii++) {
1416 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1418 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1421 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1422 if (strcasecmp(chunked, "chunked") == 0) {
1424 apr_table_unset(r->headers_out, "Transfer-Encoding");
1427 if (ap_is_HTTP_ERROR(response_code)) {
1428 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1429 return response_code;
1433 apr_pool_create(&wpool, r->pool);
1434 apr_bucket_brigade *bb;
1437 conn_rec *c = r->connection;
1439 bb = apr_brigade_create(wpool, c->bucket_alloc);
1440 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1441 APR_BRIGADE_INSERT_TAIL(bb, e);
1442 e = apr_bucket_eos_create(c->bucket_alloc);
1443 APR_BRIGADE_INSERT_TAIL(bb, e);
1444 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1445 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1448 apr_brigade_cleanup(bb);
1451 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1455 static mod_chxj_global_config *
1456 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1458 mod_chxj_global_config *conf;
1460 SDBG(s, "start chxj_global_config_create()");
1462 /*--------------------------------------------------------------------------*/
1463 /* allocate an own subpool which survives server restarts */
1464 /*--------------------------------------------------------------------------*/
1465 conf = (mod_chxj_global_config *)apr_palloc(pool,
1466 sizeof(mod_chxj_global_config));
1468 conf->cookie_db_lock = NULL;
1470 SDBG(s, "end chxj_global_config_create()");
1477 * initialize chxj module
1480 chxj_init_module(apr_pool_t *p,
1481 apr_pool_t *UNUSED(plog),
1482 apr_pool_t *UNUSED(ptemp),
1488 SDBG(s, "start chxj_init_module()");
1490 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1492 if (user_data == NULL) {
1495 * dummy user_data set.
1497 apr_pool_userdata_set(
1499 CHXJ_MOD_CONFIG_KEY,
1500 apr_pool_cleanup_null,
1502 SDBG(s, "end chxj_init_module()");
1506 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1508 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1510 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1511 return HTTP_INTERNAL_SERVER_ERROR;
1514 SDBG(s, "end chxj_init_module()");
1521 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1524 SDBG(s, "start chxj_child_init()");
1525 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1527 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1529 SDBG(s, "end chxj_child_init()");
1534 * A set structure of each server is generated.
1540 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1542 mod_chxj_global_config *gc;
1544 gc = chxj_global_config_create(p,s);
1551 chxj_translate_name(request_rec *r)
1553 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1554 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1555 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1556 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1557 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1558 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1560 return chxj_trans_name(r);
1568 chxj_insert_filter(request_rec *r)
1572 mod_chxj_config *dconf;
1573 chxjconvrule_entry *entryp;
1578 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1580 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1582 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1583 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1585 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1588 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1590 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1591 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1592 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1596 spec = chxj_specified_device(r, user_agent);
1597 entryp = chxj_apply_convrule(r, dconf->convrules);
1599 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1602 ctx = apr_palloc(r->pool, sizeof(*ctx));
1603 memset(ctx, 0, sizeof(*ctx));
1604 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1605 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1606 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1609 ctx->entryp = entryp;
1611 ctx->buffer = apr_palloc(ctx->pool, 1);
1614 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1615 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1616 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1620 switch(spec->html_spec_type) {
1621 case CHXJ_SPEC_Chtml_1_0:
1622 case CHXJ_SPEC_Chtml_2_0:
1623 case CHXJ_SPEC_Chtml_3_0:
1624 case CHXJ_SPEC_Chtml_4_0:
1625 case CHXJ_SPEC_Chtml_5_0:
1626 case CHXJ_SPEC_Chtml_6_0:
1627 case CHXJ_SPEC_Chtml_7_0:
1628 case CHXJ_SPEC_XHtml_Mobile_1_0:
1629 case CHXJ_SPEC_Hdml:
1630 case CHXJ_SPEC_Jhtml:
1631 case CHXJ_SPEC_Jxhtml:
1635 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1640 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1641 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1642 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1645 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1650 * The hook is registered.
1655 chxj_register_hooks(apr_pool_t *UNUSED(p))
1657 ap_hook_post_config(chxj_init_module,
1660 APR_HOOK_REALLY_FIRST);
1661 ap_hook_child_init(chxj_child_init,
1664 APR_HOOK_REALLY_FIRST);
1665 ap_register_output_filter (
1666 "chxj_output_filter",
1670 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1671 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1672 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1673 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1674 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1675 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1680 * A set structure according to the directory is generated.
1686 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1688 mod_chxj_config *conf;
1690 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1691 conf->device_data_file = NULL;
1692 conf->devices = NULL;
1693 conf->emoji_data_file = NULL;
1695 conf->emoji_tail = NULL;
1696 conf->image = CHXJ_IMG_NONE;
1697 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1698 conf->image_cache_limit = 0;
1699 conf->server_side_encoding = NULL;
1700 conf->cookie_db_dir = NULL;
1701 conf->cookie_timeout = 0;
1702 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1703 conf->cookie_lazy_mode = 0;
1704 #if defined(USE_MYSQL_COOKIE)
1705 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1706 conf->mysql.port = MYSQL_PORT;
1707 conf->mysql.host = NULL;
1709 #if defined(USE_MEMCACHE_COOKIE)
1710 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1711 conf->memcache.host = NULL;
1712 conf->memcache.port = 0;
1714 conf->forward_url_base = NULL;
1715 conf->forward_server_ip = NULL;
1716 conf->allowed_cookie_domain = NULL;
1717 conf->post_log = NULL;
1723 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1724 memset(conf->dir, 0, strlen(arg)+1);
1725 strcpy(conf->dir, arg);
1727 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1729 /* Default is copyleft */
1730 conf->image_copyright = NULL;
1737 * Merge per-directory CHXJ configurations
1740 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1742 mod_chxj_config *base;
1743 mod_chxj_config *add;
1744 mod_chxj_config *mrg;
1746 base = (mod_chxj_config *)basev;
1747 add = (mod_chxj_config *)addv;
1748 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1750 mrg->device_data_file = NULL;
1751 mrg->devices = NULL;
1752 mrg->emoji_data_file = NULL;
1753 mrg->image = CHXJ_IMG_NONE;
1754 mrg->image_cache_dir = NULL;
1755 mrg->image_copyright = NULL;
1756 mrg->image_cache_limit = 0;
1758 mrg->emoji_tail = NULL;
1759 mrg->new_line_type = NLTYPE_NIL;
1760 mrg->forward_url_base = NULL;
1761 mrg->forward_server_ip = NULL;
1762 mrg->allowed_cookie_domain = NULL;
1763 mrg->post_log = NULL;
1765 mrg->dir = apr_pstrdup(p, add->dir);
1767 if (! add->device_data_file) {
1768 mrg->devices = base->devices;
1769 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1772 mrg->devices = add->devices;
1773 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1776 if (! add->emoji_data_file) {
1777 mrg->emoji = base->emoji;
1778 mrg->emoji_tail = base->emoji_tail;
1779 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1782 mrg->emoji = add->emoji;
1783 mrg->emoji_tail = add->emoji_tail;
1784 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1787 if (add->image == CHXJ_IMG_NONE) {
1788 mrg->image = base->image;
1791 mrg->image = add->image;
1794 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1795 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1798 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1801 if (add->image_cache_limit) {
1802 mrg->image_cache_limit = add->image_cache_limit;
1805 mrg->image_cache_limit = base->image_cache_limit;
1808 if (add->image_copyright)
1809 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1811 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1813 if (add->server_side_encoding) {
1814 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1817 if (base->server_side_encoding) {
1818 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1821 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1824 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1826 if (add->cookie_db_dir) {
1827 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1830 if (base->cookie_db_dir) {
1831 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1834 mrg->cookie_db_dir = NULL;
1837 if (add->cookie_timeout) {
1838 mrg->cookie_timeout = add->cookie_timeout;
1841 if (base->cookie_db_dir) {
1842 mrg->cookie_timeout = base->cookie_timeout;
1845 mrg->cookie_timeout = 0;
1848 #if defined(USE_MYSQL_COOKIE)
1849 if (add->mysql.host) {
1850 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1852 else if (base->mysql.host) {
1853 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1856 mrg->mysql.host = NULL;
1858 if (add->mysql.port) {
1859 mrg->mysql.port = add->mysql.port;
1861 else if (base->mysql.port) {
1862 mrg->mysql.port = base->mysql.port;
1865 mrg->mysql.port = 0;
1868 if (add->mysql.database) {
1869 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1871 else if (base->mysql.database) {
1872 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1875 mrg->mysql.database = NULL;
1878 if (add->mysql.username) {
1879 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1881 else if (base->mysql.username) {
1882 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1885 mrg->mysql.username = NULL;
1888 if (add->mysql.password) {
1889 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1891 else if (base->mysql.password) {
1892 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1895 mrg->mysql.password = NULL;
1898 if (add->mysql.tablename) {
1899 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1901 else if (base->mysql.tablename) {
1902 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1905 mrg->mysql.tablename = NULL;
1908 if (add->mysql.socket_path) {
1909 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1911 else if (base->mysql.socket_path) {
1912 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1915 mrg->mysql.socket_path = NULL;
1918 if (add->mysql.charset) {
1919 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1921 else if (base->mysql.charset) {
1922 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1925 mrg->mysql.charset = NULL;
1928 #if defined(USE_MEMCACHE_COOKIE)
1929 if (add->memcache.host) {
1930 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1932 else if (base->memcache.host) {
1933 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1936 mrg->memcache.host = NULL;
1938 if (add->memcache.port) {
1939 mrg->memcache.port = add->memcache.port;
1941 else if (base->memcache.port) {
1942 mrg->memcache.port = base->memcache.port;
1945 mrg->memcache.port = 0;
1948 if (add->cookie_store_type) {
1949 mrg->cookie_store_type = add->cookie_store_type;
1951 else if (base->cookie_store_type) {
1952 mrg->cookie_store_type = base->cookie_store_type;
1955 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1957 if (add->cookie_lazy_mode) {
1958 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1960 else if (base->cookie_lazy_mode) {
1961 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1964 mrg->cookie_lazy_mode = 0;
1966 if (add->new_line_type) {
1967 mrg->new_line_type = add->new_line_type;
1969 else if (base->new_line_type) {
1970 mrg->new_line_type = base->new_line_type;
1973 mrg->new_line_type = NLTYPE_NIL;
1976 if (add->forward_url_base) {
1977 mrg->forward_url_base = add->forward_url_base;
1979 else if (base->forward_url_base) {
1980 mrg->forward_url_base = base->forward_url_base;
1983 if (add->allowed_cookie_domain) {
1984 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
1987 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
1989 if (add->post_log) {
1990 mrg->post_log = add->post_log;
1993 mrg->post_log = base->post_log;
2000 chxj_command_parse_take5(
2013 for (;*strp == ' '||*strp == '\t'; strp++) ;
2023 for (; *strp != '\0'; strp++) {
2024 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2025 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2030 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2031 || (isquoted && *strp == '"'))
2045 for (;*strp == ' '||*strp == '\t'; strp++) ;
2054 for (; *strp != '\0'; strp++) {
2055 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2056 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2061 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2062 || (isquoted && *strp == '"'))
2075 for (;*strp == ' '||*strp == '\t'; strp++);
2083 for (; *strp != '\0'; strp++) {
2084 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2085 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2090 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2091 || (isquoted && *strp == '"'))
2103 for (;*strp == ' '||*strp == '\t'; strp++);
2111 for (; *strp != '\0'; strp++) {
2112 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2113 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2118 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2119 || (isquoted && *strp == '"'))
2130 for (;*strp == ' '||*strp == '\t'; strp++);
2138 for (; *strp != '\0'; strp++) {
2139 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2140 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2145 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2146 || (isquoted && *strp == '"'))
2156 * The device definition file is loaded.
2158 * @param arg [i] The name of the device definition file is specified.
2159 * @param mconfig [i/o] The pointer to a set structure is specified.
2163 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2165 mod_chxj_config *conf;
2170 if (strlen(arg) > 256)
2171 return "mod_chxj: device data filename too long.";
2173 conf = (mod_chxj_config *)mconfig;
2174 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2176 qs_init_malloc(&doc);
2177 qs_init_root_node(&doc);
2179 qs_parse_file((Doc *)&doc, (const char *)arg);
2180 chxj_load_device_data(&doc,parms->pool, conf);
2181 qs_all_free(&doc, QX_LOGMARK);
2188 * Device definition information is loaded.
2191 * @param arg [i] The name of the device definition file is specified.
2192 * @param mconfig [i/o] The pointer to a set structure is specified.
2196 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2198 mod_chxj_config *conf;
2205 if (strlen(arg) > 256)
2206 return "mod_chxj: emoji data filename too long.";
2208 conf = (mod_chxj_config *)mconfig;
2209 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2210 qs_init_malloc(&doc);
2211 qs_init_root_node(&doc);
2213 qs_parse_file((Doc *)&doc, (const char *)arg);
2215 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2217 qs_all_free(&doc, QX_LOGMARK);
2225 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2227 mod_chxj_config *conf;
2232 if (strlen(arg) > 256)
2233 return "image uri is too long.";
2235 conf = (mod_chxj_config*)mconfig;
2236 if (strcasecmp("ON", arg) == 0) {
2237 conf->image = CHXJ_IMG_ON;
2240 conf->image = CHXJ_IMG_OFF;
2248 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2250 mod_chxj_config *conf;
2255 if (strlen(arg) > 256)
2256 return "cache dir name is too long.";
2258 conf = (mod_chxj_config *)mconfig;
2259 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2266 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2268 mod_chxj_config *conf;
2273 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2274 return "cache size is too long.";
2276 conf = (mod_chxj_config *)mconfig;
2279 * I use strtol function because strtoul is not portable function.
2281 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2284 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2286 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2295 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2297 mod_chxj_config *conf;
2302 if (strlen(arg) > 256)
2303 return "Copyright Flag is too long.";
2305 conf = (mod_chxj_config *)mconfig;
2306 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2313 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2317 mod_chxj_config *dconf;
2318 chxjconvrule_entry *newrule;
2328 dconf = (mod_chxj_config *)mconfig;
2330 if (strlen(arg) > 4096)
2331 return "mod_chxj: ChxjConvertRule: is too long.";
2333 dconf = (mod_chxj_config *)mconfig;
2334 if (dconf->convrules == NULL)
2335 dconf->convrules = apr_array_make(cmd->pool,
2337 sizeof(chxjconvrule_entry));
2339 newrule = apr_array_push(dconf->convrules);
2342 newrule->action = 0;
2344 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2345 return "ChxjConvertRule: bad argument line";
2347 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2351 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2357 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2358 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2361 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2362 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2365 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2366 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2369 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2370 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2376 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2377 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2379 else if (strcasecmp(CONVRULE_COOKIE_OFF_CMD, action) == 0) {
2380 newrule->action &= (0xffffffff ^ CONVRULE_COOKIE_ON_BIT);
2382 else if (strcasecmp(CONVRULE_CSS_ON_CMD, action) == 0) {
2383 newrule->action |= CONVRULE_CSS_ON_BIT;
2385 else if (strcasecmp(CONVRULE_CSS_OFF_CMD, action) == 0) {
2386 newrule->action &= (0xffffffff ^ CONVRULE_CSS_ON_BIT);
2392 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2393 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2399 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2400 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2406 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2407 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2413 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2414 newrule->action |= CONVRULE_Z2H_ON_BIT;
2417 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2418 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2421 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2422 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2425 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2426 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2429 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2430 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2433 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2434 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2437 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2438 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2441 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2442 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2453 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2457 mode = AP_REG_EXTENDED;
2458 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2459 return "RewriteRule: cannot compile regular expression ";
2461 newrule->regexp = regexp;
2463 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2465 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2467 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2469 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2470 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2472 newrule->user_agent = NULL;
2474 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2486 mod_chxj_config *dconf;
2489 if (strlen(arg) > 4096)
2490 return "mod_chxj: ChxjCookieDir is too long.";
2492 dconf = (mod_chxj_config *)mconfig;
2494 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2501 cmd_set_cookie_timeout(
2502 cmd_parms *UNUSED(cmd),
2506 mod_chxj_config *dconf;
2508 if (strlen(arg) > 4096)
2509 return "mod_chxj: ChxjCookieTimeout is too long.";
2511 if (chxj_chk_numeric(arg) != 0)
2512 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2514 dconf = (mod_chxj_config *)mconfig;
2516 dconf->cookie_timeout = atoi(arg);
2522 #if defined(USE_MYSQL_COOKIE)
2524 cmd_set_cookie_mysql_database(
2529 mod_chxj_config *dconf;
2531 if (strlen(arg) > 255)
2532 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2534 dconf = (mod_chxj_config *)mconfig;
2536 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2543 cmd_set_cookie_mysql_username(
2548 mod_chxj_config *dconf;
2550 if (strlen(arg) > 255)
2551 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2553 dconf = (mod_chxj_config *)mconfig;
2555 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2562 cmd_set_cookie_mysql_password(
2567 mod_chxj_config *dconf;
2569 if (strlen(arg) > 255)
2570 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2572 dconf = (mod_chxj_config *)mconfig;
2574 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2581 cmd_set_cookie_mysql_table_name(
2586 mod_chxj_config *dconf;
2588 if (strlen(arg) > 255)
2589 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2591 dconf = (mod_chxj_config *)mconfig;
2593 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2599 cmd_set_cookie_mysql_port(
2600 cmd_parms *UNUSED(cmd),
2604 mod_chxj_config *dconf;
2606 if (strlen(arg) > 255)
2607 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2609 dconf = (mod_chxj_config *)mconfig;
2611 if (chxj_chk_numeric(arg) != 0)
2612 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2614 dconf = (mod_chxj_config *)mconfig;
2616 dconf->mysql.port = chxj_atoi(arg);
2623 cmd_set_cookie_mysql_host(
2628 mod_chxj_config *dconf;
2630 if (strlen(arg) > 255)
2631 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2633 dconf = (mod_chxj_config *)mconfig;
2635 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2642 cmd_set_cookie_mysql_socket_path(
2647 mod_chxj_config *dconf;
2649 if (strlen(arg) > 4096)
2650 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2652 dconf = (mod_chxj_config *)mconfig;
2654 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2661 cmd_set_cookie_mysql_charset(
2666 mod_chxj_config *dconf;
2668 if (strlen(arg) > 255)
2669 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2671 dconf = (mod_chxj_config *)mconfig;
2673 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2678 #if defined(USE_MEMCACHE_COOKIE)
2680 cmd_set_cookie_memcache_port(
2681 cmd_parms *UNUSED(cmd),
2685 mod_chxj_config *dconf;
2687 if (strlen(arg) > 255)
2688 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2690 dconf = (mod_chxj_config *)mconfig;
2692 if (chxj_chk_numeric(arg) != 0)
2693 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2695 dconf = (mod_chxj_config *)mconfig;
2697 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2704 cmd_set_cookie_memcache_host(
2709 mod_chxj_config *dconf;
2711 if (strlen(arg) > 255)
2712 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2714 dconf = (mod_chxj_config *)mconfig;
2716 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2723 cmd_set_cookie_lazy_mode(
2724 cmd_parms *UNUSED(cmd),
2728 mod_chxj_config *dconf;
2730 if (strlen(arg) > 255)
2731 return "mod_chxj: ChxjCookieLazyMode is too long.";
2733 dconf = (mod_chxj_config *)mconfig;
2735 if (strcasecmp("TRUE",arg) == 0) {
2736 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2739 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2746 cmd_set_cookie_store_type(
2747 cmd_parms *UNUSED(cmd),
2751 mod_chxj_config *dconf;
2753 if (strlen(arg) > 255)
2754 return "mod_chxj: ChxjCookieStoreType is too long.";
2756 dconf = (mod_chxj_config *)mconfig;
2758 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2759 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2761 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2762 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2764 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2765 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2768 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2775 cmd_set_forward_url_base(
2780 mod_chxj_config *dconf;
2782 if (strlen(arg) > 255)
2783 return "mod_chxj: ChxjForwardUrlBase is too long.";
2785 dconf = (mod_chxj_config *)mconfig;
2787 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2793 cmd_set_forward_server_ip(
2798 mod_chxj_config *dconf;
2800 if (strlen(arg) > 255)
2801 return "mod_chxj: ChxjForwardServerIp is too long.";
2803 dconf = (mod_chxj_config *)mconfig;
2805 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2811 cmd_allowed_cookie_domain(
2816 mod_chxj_config *dconf;
2818 if (strlen(arg) > 255)
2819 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2821 dconf = (mod_chxj_config *)mconfig;
2823 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2829 cmd_set_new_line_type(
2830 cmd_parms *UNUSED(cmd),
2834 mod_chxj_config *dconf;
2835 if (strlen(arg) > 255)
2836 return "mod_chxj: ChxjNewLineType is too long.";
2838 dconf = (mod_chxj_config *)mconfig;
2840 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2841 dconf->new_line_type = NLTYPE_CRLF;
2843 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2844 dconf->new_line_type = NLTYPE_LF;
2846 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2847 dconf->new_line_type = NLTYPE_CR;
2849 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2850 dconf->new_line_type = NLTYPE_NONE;
2853 return "mod_chxj: invalid value (ChxjNewLineType)";
2864 mod_chxj_config *dconf;
2865 if (strlen(arg) > 255)
2866 return "mod_chxj: ChxjPostLogEnv is too long.";
2868 dconf = (mod_chxj_config *)mconfig;
2870 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2876 static const command_rec cmds[] = {
2878 "ChxjLoadDeviceData",
2879 cmd_load_device_data,
2882 "Load Device Data"),
2884 "ChxjLoadEmojiData",
2885 cmd_load_emoji_data,
2891 cmd_set_image_engine,
2894 "Convert Target URI"),
2896 "ChxjImageCacheDir",
2897 cmd_set_image_cache_dir,
2900 "Image Cache Directory"),
2902 "ChxjImageCacheLimit",
2903 cmd_set_image_cache_limit,
2906 "Image Cache Limit"),
2908 "ChxjImageCopyright",
2909 cmd_set_image_copyright,
2918 "an URL-applied regexp-pattern and a substitution URL"),
2924 "save cookie.db directory."),
2926 "ChxjCookieTimeout",
2927 cmd_set_cookie_timeout,
2930 "The compulsion time-out time of the cookie is specified. "),
2932 "ChxjCookieStoreType",
2933 cmd_set_cookie_store_type,
2936 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2938 "ChxjCookieLazyMode",
2939 cmd_set_cookie_lazy_mode,
2942 "OneTimeID is negligently done. (TRUE/FALSE)"),
2943 #if defined(USE_MYSQL_COOKIE)
2945 "ChxjCookieMysqlHost",
2946 cmd_set_cookie_mysql_host,
2949 "The MySQL database host used by saving Cookie"),
2951 "ChxjCookieMysqlPort",
2952 cmd_set_cookie_mysql_port,
2955 "The MySQL database port used by saving Cookie"),
2957 "ChxjCookieMysqlDatabase",
2958 cmd_set_cookie_mysql_database,
2961 "The MySQL database name used by saving Cookie"),
2963 "ChxjCookieMysqlUsername",
2964 cmd_set_cookie_mysql_username,
2967 "The MySQL username used by saving Cookie"),
2969 "ChxjCookieMysqlPassword",
2970 cmd_set_cookie_mysql_password,
2973 "The MySQL password used by saving Cookie"),
2975 "ChxjCookieMysqlTableName",
2976 cmd_set_cookie_mysql_table_name,
2979 "The MySQL table name used by saving Cookie"),
2981 "ChxjCookieMysqlSocketPath",
2982 cmd_set_cookie_mysql_socket_path,
2985 "The MySQL socket path used by saving Cookie"),
2987 "ChxjCookieMysqlCharset",
2988 cmd_set_cookie_mysql_charset,
2991 "The MySQL charset used by saving Cookie"),
2993 #if defined(USE_MEMCACHE_COOKIE)
2995 "ChxjCookieMemcacheHost",
2996 cmd_set_cookie_memcache_host,
2999 "The Memcached host used by saving Cookie"),
3001 "ChxjCookieMemcachePort",
3002 cmd_set_cookie_memcache_port,
3005 "The Memcached port used by saving Cookie"),
3009 cmd_set_new_line_type,
3012 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3014 "ChxjForwardUrlBase",
3015 cmd_set_forward_url_base,
3018 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3020 "ChxjForwardServerIp",
3021 cmd_set_forward_server_ip,
3024 "The forward server ip(default: this server ip)"),
3026 "ChxjAllowedCookieDomain",
3027 cmd_allowed_cookie_domain,
3030 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3036 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3041 /*----------------------------------------------------------------------------*/
3042 /* Dispatch list for API hooks */
3043 /*----------------------------------------------------------------------------*/
3044 module AP_MODULE_DECLARE_DATA chxj_module = {
3045 STANDARD20_MODULE_STUFF,
3046 chxj_create_per_dir_config, /* create per-dir config structures */
3047 chxj_merge_per_dir_config, /* merge per-dir config structures */
3048 chxj_config_server_create, /* create per-server config structures */
3049 NULL, /* merge per-server config structures */
3050 cmds, /* table of config file commands */
3051 chxj_register_hooks /* register hooks */