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]", (unsigned int)(apr_size_t)r, spec->device_name);
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,
463 dst = chxj_conv_z2h(r, dst, len, entryp);
467 ap_set_content_length(r, *len);
470 dst = apr_psprintf(r->pool, "\n");
479 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
486 * convert GUID parameter.
490 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
492 DBG(r, "REQ[%X] start s_convert_guid_parameter() param:[%s]", (unsigned int)(apr_size_t)r, param);
493 if (strcasecmp(param, "guid") == 0) {
494 switch(spec->html_spec_type) {
495 case CHXJ_SPEC_XHtml_Mobile_1_0:
497 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
499 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
503 case CHXJ_SPEC_Jhtml:
504 case CHXJ_SPEC_Jxhtml:
506 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
508 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
516 DBG(r, "REQ[%X] end s_convert_guid_parameter()", (unsigned int)(apr_size_t)r);
520 * It converts it from HEADER.
525 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
537 cookie_t *cookie = NULL;
538 int no_update_flag = 0;
540 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
543 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
544 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
547 urilen = strlen(r->args);
549 result = qs_alloc_zero_byte_string(r->pool);
551 buff_pre = apr_pstrdup(r->pool, r->args);
556 pair = apr_strtok(buff_pre, "&", &pstate);
562 pair_sv = apr_pstrdup(r->pool, pair);
564 name = apr_strtok(pair, "=", &vstate);
565 value = apr_strtok(NULL, "=", &vstate);
566 if (! name) continue;
567 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
568 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
573 buff = apr_pstrdup(r->pool, r->args);
574 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
583 pair = apr_strtok(buff, "&", &pstate);
589 pair_sv = apr_pstrdup(r->pool, pair);
591 name = apr_strtok(pair, "=", &vstate);
592 value = apr_strtok(NULL, "=", &vstate);
593 if (! name) continue;
594 name = chxj_safe_to_jreserved_tag(r, name);
595 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
596 if (strlen(result) != 0)
597 result = apr_pstrcat(r->pool, result, "&", NULL);
599 if (strcasecmp(entryp->encoding, "NONE") != 0) {
604 if (value && *value != 0) {
605 value = chxj_url_decode(r->pool, value);
606 dlen = strlen(value);
607 DBG(r, "************ before encoding[%s]", value);
609 dvalue = chxj_rencoding(r, value, &dlen);
610 dvalue = chxj_url_encode(r->pool, dvalue);
612 DBG(r, "************ after encoding[%s]", dvalue);
618 if (name && *name != 0) {
619 name = chxj_url_decode(r->pool, name);
621 dname = chxj_rencoding(r, name, &dlen);
622 dname = chxj_url_encode(r->pool, dname);
627 s_convert_guid_parameter_to_header(r, dname, spec);
628 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
631 s_convert_guid_parameter_to_header(r, name, spec);
632 if (strcmp(name, pair_sv) != 0) {
633 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
636 result = apr_pstrcat(r->pool, result, name, NULL);
641 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
642 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
643 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
647 if (strlen(value) == 0)
650 if (strlen(result) != 0)
651 result = apr_pstrcat(r->pool, result, "&", NULL);
653 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
656 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
658 apr_table_unset(r->headers_in, "Cookie");
659 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
660 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
661 cookie_lock_t *lock = chxj_cookie_lock(r);
662 cookie = chxj_load_cookie(r, value);
663 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
664 if (! no_update_flag && cookie) {
665 cookie = chxj_update_cookie(r, cookie);
667 chxj_cookie_unlock(r, lock);
669 if (cookie && cookie->cookie_id) {
670 if (strlen(result) != 0)
671 result = apr_pstrcat(r->pool, result, "&", NULL);
672 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
676 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
677 if (strlen(result) != 0)
678 result = apr_pstrcat(r->pool, result, "&", NULL);
679 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
682 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
683 if (! no_update_flag) {
684 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
688 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
689 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
695 * It converts it from POSTDATA .
698 * @param src [i] It is POSTDATA character string.
699 * @param len [i/o] It is length of former HTML character string.
706 chxjconvrule_entry *entryp,
716 cookie_t *cookie = NULL;
718 int no_update_flag = 0;
722 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
725 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
726 return apr_pstrdup(r->pool, "");
729 apr_pool_create(&pool, r->pool);
731 s = apr_pstrdup(pool, *src);
733 buff_pre = apr_pstrdup(pool, *src);
735 result = qs_alloc_zero_byte_string(pool);
737 chxj_dump_string(r, APLOG_MARK, "BEFORE input convert source", s, ilen);
742 pair = apr_strtok(buff_pre, "&", &pstate);
748 pair_sv = apr_pstrdup(pool, pair);
750 name = apr_strtok(pair, "=", &vstate);
751 value = apr_strtok(NULL, "=", &vstate);
752 if (! name) continue;
753 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
754 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
764 pair = apr_strtok(s, "&", &pstate);
769 name = apr_strtok(pair, "=", &vstate);
770 value = apr_strtok(NULL, "=", &vstate);
771 if (! name) continue;
772 name = chxj_safe_to_jreserved_tag(r, name);
774 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
775 if (strlen(result) != 0)
776 result = apr_pstrcat(pool, result, "&", NULL);
778 if (strcasecmp(entryp->encoding, "NONE") != 0) {
783 if (value && *value != 0) {
784 value = chxj_url_decode(pool, value);
785 dlen = strlen(value);
786 dvalue = chxj_rencoding(r, value, &dlen);
787 dvalue = chxj_url_encode(pool, dvalue);
793 if (name && *name != 0) {
794 name = chxj_url_decode(pool, name);
796 dname = chxj_rencoding(r, name, &dlen);
797 dname = chxj_url_encode(pool, dname);
804 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
807 result = apr_pstrcat(pool, result, name, "=", value, NULL);
811 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
812 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
813 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
817 if (strlen(value) == 0)
820 if (strlen(result) != 0)
821 result = apr_pstrcat(pool, result, "&", NULL);
823 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
827 dlen = strlen(value);
828 value = chxj_url_decode(pool, value);
829 dvalue = chxj_rencoding(r, value, &dlen);
830 dvalue = chxj_url_encode(pool,dvalue);
831 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
835 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
839 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
841 apr_table_unset(r->headers_in, "Cookie");
842 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
843 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
844 cookie_lock_t *lock = chxj_cookie_lock(r);
845 cookie = chxj_load_cookie(r, value);
846 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
847 if (! no_update_flag && cookie) {
848 cookie = chxj_update_cookie(r, cookie);
850 chxj_cookie_unlock(r, lock);
852 if (cookie && cookie->cookie_id) {
853 if (strlen(result) != 0)
854 result = apr_pstrcat(pool, result, "&", NULL);
855 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
859 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
860 if (strlen(result) != 0)
861 result = apr_pstrcat(pool, result, "&", NULL);
862 result = apr_pstrcat(pool, result, name, "=", value, NULL);
865 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
868 dlen = strlen(value);
870 value = chxj_url_decode(pool, value);
871 dvalue = chxj_rencoding(r, value, &dlen);
872 dvalue = chxj_url_encode(pool,dvalue);
873 if (r->args && strlen(r->args) > 0) {
874 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
877 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
879 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
883 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
886 dlen = strlen(value);
888 value = chxj_url_decode(pool, value);
889 dvalue = chxj_rencoding(r, value, &dlen);
890 dvalue = chxj_url_encode(pool,dvalue);
891 if (r->args && strlen(r->args) > 0) {
892 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
895 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
897 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
900 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
902 *len = strlen(result);
903 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
904 if (! no_update_flag) {
905 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
908 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
909 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
916 * The received data is returned to the filter.
918 * @param f [i/o] It is a filter.
919 * @param data [i] It is data returned to the filter.
920 * @param len [i] It is length of the data returned to the filter.
923 pass_data_to_filter(ap_filter_t *f, const char *data,
926 request_rec *r = f->r;
927 conn_rec *c = r->connection;
929 apr_bucket_brigade *bb;
932 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
934 chxj_header_inf_clear(r);
936 bb = apr_brigade_create(r->pool, c->bucket_alloc);
937 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
939 APR_BRIGADE_INSERT_TAIL(bb, b);
940 b = apr_bucket_eos_create(f->c->bucket_alloc);
941 APR_BRIGADE_INSERT_TAIL(bb, b);
943 rv = ap_pass_brigade(f->next, bb);
944 if (rv != APR_SUCCESS) {
945 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
949 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
956 * Add No Cache Header
959 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
961 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
962 apr_table_unset(r->headers_out, "Pragma");
963 apr_table_unset(r->err_headers_out, "Pragma");
964 apr_table_unset(r->headers_out, "Expires");
965 apr_table_unset(r->err_headers_out, "Expires");
966 apr_table_unset(r->headers_out, "Cache-Control");
967 apr_table_unset(r->err_headers_out, "Cache-Control");
969 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
970 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
971 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
977 * It is the main loop of the output filter.
979 * @param f [i/o] It is a filter.
983 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
989 char *user_agent = NULL;
991 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
992 cookie_t *cookie = NULL;
993 mod_chxj_config *dconf;
994 chxjconvrule_entry *entryp = NULL;
995 device_table *spec = NULL;
999 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1002 apr_pool_create(&pool, r->pool);
1004 entryp = ctx->entryp;
1006 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1008 if (r->content_type) {
1009 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1010 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1011 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1012 && ! (dconf->image == CHXJ_IMG_ON
1013 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1014 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1015 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1016 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1017 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1018 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1019 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1020 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1021 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1022 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1023 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1025 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1026 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1027 cookie_lock_t *lock = NULL;
1028 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1029 switch(spec->html_spec_type) {
1030 case CHXJ_SPEC_Chtml_1_0:
1031 case CHXJ_SPEC_Chtml_2_0:
1032 case CHXJ_SPEC_Chtml_3_0:
1033 case CHXJ_SPEC_Chtml_4_0:
1034 case CHXJ_SPEC_Chtml_5_0:
1035 case CHXJ_SPEC_Chtml_6_0:
1036 case CHXJ_SPEC_Chtml_7_0:
1037 case CHXJ_SPEC_XHtml_Mobile_1_0:
1038 case CHXJ_SPEC_Jhtml:
1039 lock = chxj_cookie_lock(r);
1040 cookie = chxj_save_cookie(r);
1041 s_add_cookie_id_if_has_location_header(r, cookie);
1042 chxj_cookie_unlock(r, lock);
1048 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1049 if (! ap_is_HTTP_REDIRECT(r->status)) {
1050 r->status = HTTP_MOVED_TEMPORARILY;
1053 s_add_no_cache_headers(r, entryp);
1054 ap_pass_brigade(f->next, bb);
1055 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1060 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1061 ap_pass_brigade(f->next, bb);
1062 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1067 for (b = APR_BRIGADE_FIRST(bb);
1068 b != APR_BRIGADE_SENTINEL(bb);
1069 b = APR_BUCKET_NEXT(b)) {
1071 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1072 chxj_dump_string(r, APLOG_MARK, "READ Data", data, len);
1074 /*--------------------------------------------------------------------*/
1076 /*--------------------------------------------------------------------*/
1078 DBG(r, "append data start");
1079 ctx = (mod_chxj_ctx *)f->ctx;
1082 tmp = apr_palloc(r->pool, ctx->len);
1083 memcpy(tmp, ctx->buffer, ctx->len);
1085 ctx->buffer = apr_palloc(pool, ctx->len + len);
1087 memcpy(ctx->buffer, tmp, ctx->len);
1088 memcpy(&ctx->buffer[ctx->len], data, len);
1092 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1095 if (APR_BUCKET_IS_EOS(b)) {
1097 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1098 /*----------------------------------------------------------------------*/
1100 /*----------------------------------------------------------------------*/
1102 cookie_lock_t *lock = NULL;
1103 ctx = (mod_chxj_ctx *)f->ctx;
1105 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1106 lock = chxj_cookie_lock(r);
1108 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1110 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1111 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1112 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1117 tmp = apr_palloc(pool, ctx->len + 1);
1119 memset(tmp, 0, ctx->len + 1);
1120 memcpy(tmp, ctx->buffer, ctx->len);
1122 ctx->buffer = chxj_convert(r,
1123 (const char **)&tmp,
1124 (apr_size_t *)&ctx->len,
1126 user_agent, &cookie);
1130 ctx->buffer = apr_psprintf(r->pool, "\n");
1132 ctx->buffer = chxj_convert(r,
1133 (const char **)&ctx->buffer,
1134 (apr_size_t *)&ctx->len,
1136 user_agent, &cookie);
1141 && *(char *)r->content_type == 't'
1142 && strncmp(r->content_type, "text/xml", 8) == 0) {
1143 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1151 memset(&doc, 0, sizeof(Doc));
1152 memset(&qrcode, 0, sizeof(qr_code_t));
1154 doc.parse_mode = PARSE_MODE_CHTML;
1158 qs_init_malloc(&doc);
1160 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1163 for (child = qs_get_child_node(&doc,root);
1165 child = qs_get_next_node(&doc,child)) {
1166 char *name = qs_get_node_name(&doc,child);
1167 if (strcasecmp("qrcode",name) == 0) {
1172 qs_all_free(&doc,QX_LOGMARK);
1174 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1175 chxj_qrcode_node_to_qrcode(&qrcode, root);
1176 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1178 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1179 chxj_cookie_unlock(r, lock);
1180 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1183 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1187 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1189 && ( *r->content_type == 'i' || *r->content_type == 'I')
1190 && dconf->image == CHXJ_IMG_ON
1191 && strncasecmp("image/", r->content_type, 6) == 0
1192 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1193 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1194 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1195 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1196 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1197 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1198 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1199 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1200 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1201 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1205 tmp = apr_palloc(pool, ctx->len + 1);
1207 memset(tmp, 0, ctx->len + 1);
1208 memcpy(tmp, ctx->buffer, ctx->len);
1210 chxj_convert_image(r,
1211 (const char **)&tmp,
1212 (apr_size_t *)&ctx->len);
1213 if (ctx->buffer == NULL) {
1219 apr_table_unset(r->headers_out, "Content-Length");
1220 apr_table_unset(r->err_headers_out, "Content-Length");
1221 ap_set_content_length(r, (apr_off_t)ctx->len);
1225 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1226 s_add_cookie_id_if_has_location_header(r, cookie);
1227 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1228 if (! ap_is_HTTP_REDIRECT(r->status)) {
1229 r->status = HTTP_MOVED_TEMPORARILY;
1232 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1233 ctx->buffer = apr_pstrdup(pool, "");
1235 ap_set_content_length(r, (apr_off_t)ctx->len);
1237 chxj_cookie_unlock(r,lock);
1238 s_add_no_cache_headers(r, entryp);
1239 rv = pass_data_to_filter(f,
1240 (const char *)ctx->buffer,
1241 (apr_size_t)ctx->len);
1244 ctx->buffer = apr_pstrdup(pool, "");
1246 ap_set_content_length(r, (apr_off_t)ctx->len);
1247 chxj_cookie_unlock(r, lock);
1248 s_add_no_cache_headers(r, entryp);
1249 rv = pass_data_to_filter(f,
1250 (const char *)ctx->buffer,
1251 (apr_size_t)ctx->len);
1253 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1257 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1262 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1263 cookie_lock_t *lock = NULL;
1264 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1265 switch(spec->html_spec_type) {
1266 case CHXJ_SPEC_Chtml_1_0:
1267 case CHXJ_SPEC_Chtml_2_0:
1268 case CHXJ_SPEC_Chtml_3_0:
1269 case CHXJ_SPEC_Chtml_4_0:
1270 case CHXJ_SPEC_Chtml_5_0:
1271 case CHXJ_SPEC_Chtml_6_0:
1272 case CHXJ_SPEC_Chtml_7_0:
1273 case CHXJ_SPEC_XHtml_Mobile_1_0:
1274 case CHXJ_SPEC_Jhtml:
1275 lock = chxj_cookie_lock(r);
1276 cookie = chxj_save_cookie(r);
1278 * Location Header Check to add cookie parameter.
1280 s_add_cookie_id_if_has_location_header(r, cookie);
1281 chxj_cookie_unlock(r, lock);
1282 apr_table_unset(r->headers_out, "Set-Cookie");
1283 apr_table_unset(r->err_headers_out, "Set-Cookie");
1290 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1291 if (! ap_is_HTTP_REDIRECT(r->status)) {
1292 r->status = HTTP_MOVED_TEMPORARILY;
1295 apr_table_setn(r->headers_out, "Content-Length", "0");
1296 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1297 s_add_no_cache_headers(r, entryp);
1298 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1299 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1304 apr_brigade_destroy(bb);
1306 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1312 * Add Cookie_id if it has location header.
1315 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1317 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1318 if (! location_header) {
1319 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1321 if (cookie && location_header) {
1322 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1323 location_header = chxj_add_cookie_parameter(r,
1326 apr_table_unset(r->headers_out, "Location");
1327 apr_table_setn(r->headers_out, "Location", location_header);
1328 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1329 if (!ap_is_HTTP_REDIRECT(r->status)) {
1330 r->status = HTTP_MOVED_TEMPORARILY;
1336 * It is the main loop of the input filter handler.
1340 chxj_input_handler(request_rec *r)
1342 mod_chxj_config *dconf;
1343 chxjconvrule_entry *entryp = NULL;
1344 device_table *spec = NULL;
1345 char *post_data = NULL;
1346 apr_size_t post_data_len = 0;
1351 int response_code = 0;
1353 DBG(r, "start of chxj_input_handler()");
1355 if (strcasecmp(r->handler, "chxj-input-handler")) {
1356 DBG(r, "end chxj_input_handler()");
1359 apr_pool_create(&pool, r->pool);
1361 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1362 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1364 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1366 spec = chxj_specified_device(r, user_agent);
1367 entryp = chxj_apply_convrule(r, dconf->convrules);
1369 post_data = apr_pstrdup(pool, "");
1370 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1371 if (ap_should_client_block(r)) {
1372 while (post_data_len < CHXJ_POST_MAX) {
1375 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1376 if (read_bytes<=0) {
1379 buffer[read_bytes] = '\0';
1380 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1381 post_data_len += read_bytes;
1390 if (post_data_len > 0) {
1391 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1392 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1396 if (dconf->forward_url_base) {
1397 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1400 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);
1403 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1405 DBG(r, "==> new url_path:[%s]", url_path);
1408 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1409 apr_table_unset(r->headers_in, "Content-Length");
1410 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1411 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1412 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1413 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1414 for (ii=0; ii<res_len/64; ii++) {
1415 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1417 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1420 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1421 if (strcasecmp(chunked, "chunked") == 0) {
1423 apr_table_unset(r->headers_out, "Transfer-Encoding");
1426 if (ap_is_HTTP_ERROR(response_code)) {
1427 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1428 return response_code;
1432 apr_pool_create(&wpool, r->pool);
1433 apr_bucket_brigade *bb;
1436 conn_rec *c = r->connection;
1438 bb = apr_brigade_create(wpool, c->bucket_alloc);
1439 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1440 APR_BRIGADE_INSERT_TAIL(bb, e);
1441 e = apr_bucket_eos_create(c->bucket_alloc);
1442 APR_BRIGADE_INSERT_TAIL(bb, e);
1443 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1444 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1447 apr_brigade_cleanup(bb);
1450 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1454 static mod_chxj_global_config *
1455 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1457 mod_chxj_global_config *conf;
1459 SDBG(s, "start chxj_global_config_create()");
1461 /*--------------------------------------------------------------------------*/
1462 /* allocate an own subpool which survives server restarts */
1463 /*--------------------------------------------------------------------------*/
1464 conf = (mod_chxj_global_config *)apr_palloc(pool,
1465 sizeof(mod_chxj_global_config));
1467 conf->cookie_db_lock = NULL;
1469 SDBG(s, "end chxj_global_config_create()");
1476 * initialize chxj module
1479 chxj_init_module(apr_pool_t *p,
1480 apr_pool_t *UNUSED(plog),
1481 apr_pool_t *UNUSED(ptemp),
1487 SDBG(s, "start chxj_init_module()");
1489 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1491 if (user_data == NULL) {
1494 * dummy user_data set.
1496 apr_pool_userdata_set(
1498 CHXJ_MOD_CONFIG_KEY,
1499 apr_pool_cleanup_null,
1501 SDBG(s, "end chxj_init_module()");
1505 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1507 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1509 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1510 return HTTP_INTERNAL_SERVER_ERROR;
1513 SDBG(s, "end chxj_init_module()");
1520 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1523 SDBG(s, "start chxj_child_init()");
1524 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1526 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1528 SDBG(s, "end chxj_child_init()");
1533 * A set structure of each server is generated.
1539 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1541 mod_chxj_global_config *gc;
1543 gc = chxj_global_config_create(p,s);
1550 chxj_translate_name(request_rec *r)
1552 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1553 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1554 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1555 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1556 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1557 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1559 return chxj_trans_name(r);
1567 chxj_insert_filter(request_rec *r)
1571 mod_chxj_config *dconf;
1572 chxjconvrule_entry *entryp;
1577 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1579 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1581 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1582 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1584 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1587 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1589 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1590 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1591 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1595 spec = chxj_specified_device(r, user_agent);
1596 entryp = chxj_apply_convrule(r, dconf->convrules);
1598 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1601 ctx = apr_palloc(r->pool, sizeof(*ctx));
1602 memset(ctx, 0, sizeof(*ctx));
1603 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1604 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1605 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1608 ctx->entryp = entryp;
1610 ctx->buffer = apr_palloc(ctx->pool, 1);
1613 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1614 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1615 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1619 switch(spec->html_spec_type) {
1620 case CHXJ_SPEC_Chtml_1_0:
1621 case CHXJ_SPEC_Chtml_2_0:
1622 case CHXJ_SPEC_Chtml_3_0:
1623 case CHXJ_SPEC_Chtml_4_0:
1624 case CHXJ_SPEC_Chtml_5_0:
1625 case CHXJ_SPEC_Chtml_6_0:
1626 case CHXJ_SPEC_Chtml_7_0:
1627 case CHXJ_SPEC_XHtml_Mobile_1_0:
1628 case CHXJ_SPEC_Hdml:
1629 case CHXJ_SPEC_Jhtml:
1630 case CHXJ_SPEC_Jxhtml:
1634 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1639 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1640 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1641 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1644 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1649 * The hook is registered.
1654 chxj_register_hooks(apr_pool_t *UNUSED(p))
1656 ap_hook_post_config(chxj_init_module,
1659 APR_HOOK_REALLY_FIRST);
1660 ap_hook_child_init(chxj_child_init,
1663 APR_HOOK_REALLY_FIRST);
1664 ap_register_output_filter (
1665 "chxj_output_filter",
1669 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1670 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1671 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1672 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1673 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1674 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1679 * A set structure according to the directory is generated.
1685 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1687 mod_chxj_config *conf;
1689 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1690 conf->device_data_file = NULL;
1691 conf->devices = NULL;
1692 conf->emoji_data_file = NULL;
1694 conf->emoji_tail = NULL;
1695 conf->image = CHXJ_IMG_NONE;
1696 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1697 conf->image_cache_limit = 0;
1698 conf->server_side_encoding = NULL;
1699 conf->cookie_db_dir = NULL;
1700 conf->cookie_timeout = 0;
1701 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1702 conf->cookie_lazy_mode = 0;
1703 #if defined(USE_MYSQL_COOKIE)
1704 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1705 conf->mysql.port = MYSQL_PORT;
1706 conf->mysql.host = NULL;
1708 #if defined(USE_MEMCACHE_COOKIE)
1709 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1710 conf->memcache.host = NULL;
1711 conf->memcache.port = 0;
1713 conf->forward_url_base = NULL;
1714 conf->forward_server_ip = NULL;
1715 conf->allowed_cookie_domain = NULL;
1716 conf->post_log = NULL;
1722 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1723 memset(conf->dir, 0, strlen(arg)+1);
1724 strcpy(conf->dir, arg);
1726 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1728 /* Default is copyleft */
1729 conf->image_copyright = NULL;
1736 * Merge per-directory CHXJ configurations
1739 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1741 mod_chxj_config *base;
1742 mod_chxj_config *add;
1743 mod_chxj_config *mrg;
1745 base = (mod_chxj_config *)basev;
1746 add = (mod_chxj_config *)addv;
1747 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1749 mrg->device_data_file = NULL;
1750 mrg->devices = NULL;
1751 mrg->emoji_data_file = NULL;
1752 mrg->image = CHXJ_IMG_NONE;
1753 mrg->image_cache_dir = NULL;
1754 mrg->image_copyright = NULL;
1755 mrg->image_cache_limit = 0;
1757 mrg->emoji_tail = NULL;
1758 mrg->new_line_type = NLTYPE_NIL;
1759 mrg->forward_url_base = NULL;
1760 mrg->forward_server_ip = NULL;
1761 mrg->allowed_cookie_domain = NULL;
1762 mrg->post_log = NULL;
1764 mrg->dir = apr_pstrdup(p, add->dir);
1766 if (! add->device_data_file) {
1767 mrg->devices = base->devices;
1768 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1771 mrg->devices = add->devices;
1772 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1775 if (! add->emoji_data_file) {
1776 mrg->emoji = base->emoji;
1777 mrg->emoji_tail = base->emoji_tail;
1778 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1781 mrg->emoji = add->emoji;
1782 mrg->emoji_tail = add->emoji_tail;
1783 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1786 if (add->image == CHXJ_IMG_NONE) {
1787 mrg->image = base->image;
1790 mrg->image = add->image;
1793 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1794 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1797 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1800 if (add->image_cache_limit) {
1801 mrg->image_cache_limit = add->image_cache_limit;
1804 mrg->image_cache_limit = base->image_cache_limit;
1807 if (add->image_copyright)
1808 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1810 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1812 if (add->server_side_encoding) {
1813 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1816 if (base->server_side_encoding) {
1817 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1820 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1823 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1825 if (add->cookie_db_dir) {
1826 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1829 if (base->cookie_db_dir) {
1830 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1833 mrg->cookie_db_dir = NULL;
1836 if (add->cookie_timeout) {
1837 mrg->cookie_timeout = add->cookie_timeout;
1840 if (base->cookie_db_dir) {
1841 mrg->cookie_timeout = base->cookie_timeout;
1844 mrg->cookie_timeout = 0;
1847 #if defined(USE_MYSQL_COOKIE)
1848 if (add->mysql.host) {
1849 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1851 else if (base->mysql.host) {
1852 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1855 mrg->mysql.host = NULL;
1857 if (add->mysql.port) {
1858 mrg->mysql.port = add->mysql.port;
1860 else if (base->mysql.port) {
1861 mrg->mysql.port = base->mysql.port;
1864 mrg->mysql.port = 0;
1867 if (add->mysql.database) {
1868 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1870 else if (base->mysql.database) {
1871 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1874 mrg->mysql.database = NULL;
1877 if (add->mysql.username) {
1878 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1880 else if (base->mysql.username) {
1881 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1884 mrg->mysql.username = NULL;
1887 if (add->mysql.password) {
1888 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1890 else if (base->mysql.password) {
1891 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1894 mrg->mysql.password = NULL;
1897 if (add->mysql.tablename) {
1898 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1900 else if (base->mysql.tablename) {
1901 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1904 mrg->mysql.tablename = NULL;
1907 if (add->mysql.socket_path) {
1908 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1910 else if (base->mysql.socket_path) {
1911 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1914 mrg->mysql.socket_path = NULL;
1917 if (add->mysql.charset) {
1918 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1920 else if (base->mysql.charset) {
1921 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1924 mrg->mysql.charset = NULL;
1927 #if defined(USE_MEMCACHE_COOKIE)
1928 if (add->memcache.host) {
1929 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1931 else if (base->memcache.host) {
1932 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1935 mrg->memcache.host = NULL;
1937 if (add->memcache.port) {
1938 mrg->memcache.port = add->memcache.port;
1940 else if (base->memcache.port) {
1941 mrg->memcache.port = base->memcache.port;
1944 mrg->memcache.port = 0;
1947 if (add->cookie_store_type) {
1948 mrg->cookie_store_type = add->cookie_store_type;
1950 else if (base->cookie_store_type) {
1951 mrg->cookie_store_type = base->cookie_store_type;
1954 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1956 if (add->cookie_lazy_mode) {
1957 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1959 else if (base->cookie_lazy_mode) {
1960 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1963 mrg->cookie_lazy_mode = 0;
1965 if (add->new_line_type) {
1966 mrg->new_line_type = add->new_line_type;
1968 else if (base->new_line_type) {
1969 mrg->new_line_type = base->new_line_type;
1972 mrg->new_line_type = NLTYPE_NIL;
1975 if (add->forward_url_base) {
1976 mrg->forward_url_base = add->forward_url_base;
1978 else if (base->forward_url_base) {
1979 mrg->forward_url_base = base->forward_url_base;
1982 if (add->allowed_cookie_domain) {
1983 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
1986 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
1988 if (add->post_log) {
1989 mrg->post_log = add->post_log;
1992 mrg->post_log = base->post_log;
1999 chxj_command_parse_take5(
2012 for (;*strp == ' '||*strp == '\t'; strp++) ;
2022 for (; *strp != '\0'; strp++) {
2023 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2024 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2029 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2030 || (isquoted && *strp == '"'))
2044 for (;*strp == ' '||*strp == '\t'; strp++) ;
2053 for (; *strp != '\0'; strp++) {
2054 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2055 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2060 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2061 || (isquoted && *strp == '"'))
2074 for (;*strp == ' '||*strp == '\t'; strp++);
2082 for (; *strp != '\0'; strp++) {
2083 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2084 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2089 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2090 || (isquoted && *strp == '"'))
2102 for (;*strp == ' '||*strp == '\t'; strp++);
2110 for (; *strp != '\0'; strp++) {
2111 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2112 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2117 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2118 || (isquoted && *strp == '"'))
2129 for (;*strp == ' '||*strp == '\t'; strp++);
2137 for (; *strp != '\0'; strp++) {
2138 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2139 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2144 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2145 || (isquoted && *strp == '"'))
2155 * The device definition file is loaded.
2157 * @param arg [i] The name of the device definition file is specified.
2158 * @param mconfig [i/o] The pointer to a set structure is specified.
2162 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2164 mod_chxj_config *conf;
2169 if (strlen(arg) > 256)
2170 return "mod_chxj: device data filename too long.";
2172 conf = (mod_chxj_config *)mconfig;
2173 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2175 qs_init_malloc(&doc);
2176 qs_init_root_node(&doc);
2178 qs_parse_file((Doc *)&doc, (const char *)arg);
2179 chxj_load_device_data(&doc,parms->pool, conf);
2180 qs_all_free(&doc, QX_LOGMARK);
2187 * Device definition information is loaded.
2190 * @param arg [i] The name of the device definition file is specified.
2191 * @param mconfig [i/o] The pointer to a set structure is specified.
2195 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2197 mod_chxj_config *conf;
2204 if (strlen(arg) > 256)
2205 return "mod_chxj: emoji data filename too long.";
2207 conf = (mod_chxj_config *)mconfig;
2208 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2209 qs_init_malloc(&doc);
2210 qs_init_root_node(&doc);
2212 qs_parse_file((Doc *)&doc, (const char *)arg);
2214 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2216 qs_all_free(&doc, QX_LOGMARK);
2224 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2226 mod_chxj_config *conf;
2231 if (strlen(arg) > 256)
2232 return "image uri is too long.";
2234 conf = (mod_chxj_config*)mconfig;
2235 if (strcasecmp("ON", arg) == 0) {
2236 conf->image = CHXJ_IMG_ON;
2239 conf->image = CHXJ_IMG_OFF;
2247 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2249 mod_chxj_config *conf;
2254 if (strlen(arg) > 256)
2255 return "cache dir name is too long.";
2257 conf = (mod_chxj_config *)mconfig;
2258 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2265 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2267 mod_chxj_config *conf;
2272 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2273 return "cache size is too long.";
2275 conf = (mod_chxj_config *)mconfig;
2278 * I use strtol function because strtoul is not portable function.
2280 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2283 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2285 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2294 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2296 mod_chxj_config *conf;
2301 if (strlen(arg) > 256)
2302 return "Copyright Flag is too long.";
2304 conf = (mod_chxj_config *)mconfig;
2305 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2312 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2316 mod_chxj_config *dconf;
2317 chxjconvrule_entry *newrule;
2327 dconf = (mod_chxj_config *)mconfig;
2329 if (strlen(arg) > 4096)
2330 return "mod_chxj: ChxjConvertRule: is too long.";
2332 dconf = (mod_chxj_config *)mconfig;
2333 if (dconf->convrules == NULL)
2334 dconf->convrules = apr_array_make(cmd->pool,
2336 sizeof(chxjconvrule_entry));
2338 newrule = apr_array_push(dconf->convrules);
2341 newrule->action = 0;
2343 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2344 return "ChxjConvertRule: bad argument line";
2346 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2350 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2356 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2357 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2360 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2361 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2364 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2365 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2368 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2369 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2375 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2376 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2378 else if (strcasecmp(CONVRULE_COOKIE_OFF_CMD, action) == 0) {
2379 newrule->action &= (0xffffffff ^ CONVRULE_COOKIE_ON_BIT);
2381 else if (strcasecmp(CONVRULE_CSS_ON_CMD, action) == 0) {
2382 newrule->action |= CONVRULE_CSS_ON_BIT;
2384 else if (strcasecmp(CONVRULE_CSS_OFF_CMD, action) == 0) {
2385 newrule->action &= (0xffffffff ^ CONVRULE_CSS_ON_BIT);
2391 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2392 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2398 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2399 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2405 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2406 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2412 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2413 newrule->action |= CONVRULE_Z2H_ON_BIT;
2416 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2417 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2420 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2421 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2424 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2425 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2428 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2429 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2432 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2433 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2436 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2437 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2440 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2441 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2452 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2456 mode = AP_REG_EXTENDED;
2457 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2458 return "RewriteRule: cannot compile regular expression ";
2460 newrule->regexp = regexp;
2462 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2464 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2466 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2468 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2469 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2471 newrule->user_agent = NULL;
2473 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2485 mod_chxj_config *dconf;
2488 if (strlen(arg) > 4096)
2489 return "mod_chxj: ChxjCookieDir is too long.";
2491 dconf = (mod_chxj_config *)mconfig;
2493 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2500 cmd_set_cookie_timeout(
2501 cmd_parms *UNUSED(cmd),
2505 mod_chxj_config *dconf;
2507 if (strlen(arg) > 4096)
2508 return "mod_chxj: ChxjCookieTimeout is too long.";
2510 if (chxj_chk_numeric(arg) != 0)
2511 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2513 dconf = (mod_chxj_config *)mconfig;
2515 dconf->cookie_timeout = atoi(arg);
2521 #if defined(USE_MYSQL_COOKIE)
2523 cmd_set_cookie_mysql_database(
2528 mod_chxj_config *dconf;
2530 if (strlen(arg) > 255)
2531 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2533 dconf = (mod_chxj_config *)mconfig;
2535 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2542 cmd_set_cookie_mysql_username(
2547 mod_chxj_config *dconf;
2549 if (strlen(arg) > 255)
2550 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2552 dconf = (mod_chxj_config *)mconfig;
2554 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2561 cmd_set_cookie_mysql_password(
2566 mod_chxj_config *dconf;
2568 if (strlen(arg) > 255)
2569 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2571 dconf = (mod_chxj_config *)mconfig;
2573 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2580 cmd_set_cookie_mysql_table_name(
2585 mod_chxj_config *dconf;
2587 if (strlen(arg) > 255)
2588 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2590 dconf = (mod_chxj_config *)mconfig;
2592 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2598 cmd_set_cookie_mysql_port(
2599 cmd_parms *UNUSED(cmd),
2603 mod_chxj_config *dconf;
2605 if (strlen(arg) > 255)
2606 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2608 dconf = (mod_chxj_config *)mconfig;
2610 if (chxj_chk_numeric(arg) != 0)
2611 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2613 dconf = (mod_chxj_config *)mconfig;
2615 dconf->mysql.port = chxj_atoi(arg);
2622 cmd_set_cookie_mysql_host(
2627 mod_chxj_config *dconf;
2629 if (strlen(arg) > 255)
2630 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2632 dconf = (mod_chxj_config *)mconfig;
2634 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2641 cmd_set_cookie_mysql_socket_path(
2646 mod_chxj_config *dconf;
2648 if (strlen(arg) > 4096)
2649 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2651 dconf = (mod_chxj_config *)mconfig;
2653 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2660 cmd_set_cookie_mysql_charset(
2665 mod_chxj_config *dconf;
2667 if (strlen(arg) > 255)
2668 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2670 dconf = (mod_chxj_config *)mconfig;
2672 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2677 #if defined(USE_MEMCACHE_COOKIE)
2679 cmd_set_cookie_memcache_port(
2680 cmd_parms *UNUSED(cmd),
2684 mod_chxj_config *dconf;
2686 if (strlen(arg) > 255)
2687 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2689 dconf = (mod_chxj_config *)mconfig;
2691 if (chxj_chk_numeric(arg) != 0)
2692 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2694 dconf = (mod_chxj_config *)mconfig;
2696 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2703 cmd_set_cookie_memcache_host(
2708 mod_chxj_config *dconf;
2710 if (strlen(arg) > 255)
2711 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2713 dconf = (mod_chxj_config *)mconfig;
2715 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2722 cmd_set_cookie_lazy_mode(
2723 cmd_parms *UNUSED(cmd),
2727 mod_chxj_config *dconf;
2729 if (strlen(arg) > 255)
2730 return "mod_chxj: ChxjCookieLazyMode is too long.";
2732 dconf = (mod_chxj_config *)mconfig;
2734 if (strcasecmp("TRUE",arg) == 0) {
2735 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2738 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2745 cmd_set_cookie_store_type(
2746 cmd_parms *UNUSED(cmd),
2750 mod_chxj_config *dconf;
2752 if (strlen(arg) > 255)
2753 return "mod_chxj: ChxjCookieStoreType is too long.";
2755 dconf = (mod_chxj_config *)mconfig;
2757 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2758 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2760 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2761 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2763 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2764 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2767 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2774 cmd_set_forward_url_base(
2779 mod_chxj_config *dconf;
2781 if (strlen(arg) > 255)
2782 return "mod_chxj: ChxjForwardUrlBase is too long.";
2784 dconf = (mod_chxj_config *)mconfig;
2786 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2792 cmd_set_forward_server_ip(
2797 mod_chxj_config *dconf;
2799 if (strlen(arg) > 255)
2800 return "mod_chxj: ChxjForwardServerIp is too long.";
2802 dconf = (mod_chxj_config *)mconfig;
2804 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2810 cmd_allowed_cookie_domain(
2815 mod_chxj_config *dconf;
2817 if (strlen(arg) > 255)
2818 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2820 dconf = (mod_chxj_config *)mconfig;
2822 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2828 cmd_set_new_line_type(
2829 cmd_parms *UNUSED(cmd),
2833 mod_chxj_config *dconf;
2834 if (strlen(arg) > 255)
2835 return "mod_chxj: ChxjNewLineType is too long.";
2837 dconf = (mod_chxj_config *)mconfig;
2839 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2840 dconf->new_line_type = NLTYPE_CRLF;
2842 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2843 dconf->new_line_type = NLTYPE_LF;
2845 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2846 dconf->new_line_type = NLTYPE_CR;
2848 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2849 dconf->new_line_type = NLTYPE_NONE;
2852 return "mod_chxj: invalid value (ChxjNewLineType)";
2863 mod_chxj_config *dconf;
2864 if (strlen(arg) > 255)
2865 return "mod_chxj: ChxjPostLogEnv is too long.";
2867 dconf = (mod_chxj_config *)mconfig;
2869 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2875 static const command_rec cmds[] = {
2877 "ChxjLoadDeviceData",
2878 cmd_load_device_data,
2881 "Load Device Data"),
2883 "ChxjLoadEmojiData",
2884 cmd_load_emoji_data,
2890 cmd_set_image_engine,
2893 "Convert Target URI"),
2895 "ChxjImageCacheDir",
2896 cmd_set_image_cache_dir,
2899 "Image Cache Directory"),
2901 "ChxjImageCacheLimit",
2902 cmd_set_image_cache_limit,
2905 "Image Cache Limit"),
2907 "ChxjImageCopyright",
2908 cmd_set_image_copyright,
2917 "an URL-applied regexp-pattern and a substitution URL"),
2923 "save cookie.db directory."),
2925 "ChxjCookieTimeout",
2926 cmd_set_cookie_timeout,
2929 "The compulsion time-out time of the cookie is specified. "),
2931 "ChxjCookieStoreType",
2932 cmd_set_cookie_store_type,
2935 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2937 "ChxjCookieLazyMode",
2938 cmd_set_cookie_lazy_mode,
2941 "OneTimeID is negligently done. (TRUE/FALSE)"),
2942 #if defined(USE_MYSQL_COOKIE)
2944 "ChxjCookieMysqlHost",
2945 cmd_set_cookie_mysql_host,
2948 "The MySQL database host used by saving Cookie"),
2950 "ChxjCookieMysqlPort",
2951 cmd_set_cookie_mysql_port,
2954 "The MySQL database port used by saving Cookie"),
2956 "ChxjCookieMysqlDatabase",
2957 cmd_set_cookie_mysql_database,
2960 "The MySQL database name used by saving Cookie"),
2962 "ChxjCookieMysqlUsername",
2963 cmd_set_cookie_mysql_username,
2966 "The MySQL username used by saving Cookie"),
2968 "ChxjCookieMysqlPassword",
2969 cmd_set_cookie_mysql_password,
2972 "The MySQL password used by saving Cookie"),
2974 "ChxjCookieMysqlTableName",
2975 cmd_set_cookie_mysql_table_name,
2978 "The MySQL table name used by saving Cookie"),
2980 "ChxjCookieMysqlSocketPath",
2981 cmd_set_cookie_mysql_socket_path,
2984 "The MySQL socket path used by saving Cookie"),
2986 "ChxjCookieMysqlCharset",
2987 cmd_set_cookie_mysql_charset,
2990 "The MySQL charset used by saving Cookie"),
2992 #if defined(USE_MEMCACHE_COOKIE)
2994 "ChxjCookieMemcacheHost",
2995 cmd_set_cookie_memcache_host,
2998 "The Memcached host used by saving Cookie"),
3000 "ChxjCookieMemcachePort",
3001 cmd_set_cookie_memcache_port,
3004 "The Memcached port used by saving Cookie"),
3008 cmd_set_new_line_type,
3011 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3013 "ChxjForwardUrlBase",
3014 cmd_set_forward_url_base,
3017 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3019 "ChxjForwardServerIp",
3020 cmd_set_forward_server_ip,
3023 "The forward server ip(default: this server ip)"),
3025 "ChxjAllowedCookieDomain",
3026 cmd_allowed_cookie_domain,
3029 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3035 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3040 /*----------------------------------------------------------------------------*/
3041 /* Dispatch list for API hooks */
3042 /*----------------------------------------------------------------------------*/
3043 module AP_MODULE_DECLARE_DATA chxj_module = {
3044 STANDARD20_MODULE_STUFF,
3045 chxj_create_per_dir_config, /* create per-dir config structures */
3046 chxj_merge_per_dir_config, /* merge per-dir config structures */
3047 chxj_config_server_create, /* create per-server config structures */
3048 NULL, /* merge per-server config structures */
3049 cmds, /* table of config file commands */
3050 chxj_register_hooks /* register hooks */