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);
955 * Add No Cache Header
958 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
960 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
961 apr_table_unset(r->headers_out, "Pragma");
962 apr_table_unset(r->err_headers_out, "Pragma");
963 apr_table_unset(r->headers_out, "Expires");
964 apr_table_unset(r->err_headers_out, "Expires");
965 apr_table_unset(r->headers_out, "Cache-Control");
966 apr_table_unset(r->err_headers_out, "Cache-Control");
968 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
969 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
970 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
976 * It is the main loop of the output filter.
978 * @param f [i/o] It is a filter.
982 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
988 char *user_agent = NULL;
990 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
991 cookie_t *cookie = NULL;
992 mod_chxj_config *dconf;
993 chxjconvrule_entry *entryp = NULL;
994 device_table *spec = NULL;
998 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1001 apr_pool_create(&pool, r->pool);
1003 entryp = ctx->entryp;
1005 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1007 if (r->content_type) {
1008 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1009 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1010 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1011 && ! (dconf->image == CHXJ_IMG_ON
1012 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1013 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1014 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1015 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1016 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1017 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1018 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1019 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1020 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1021 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1022 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1024 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1025 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1026 cookie_lock_t *lock = NULL;
1027 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1028 switch(spec->html_spec_type) {
1029 case CHXJ_SPEC_Chtml_1_0:
1030 case CHXJ_SPEC_Chtml_2_0:
1031 case CHXJ_SPEC_Chtml_3_0:
1032 case CHXJ_SPEC_Chtml_4_0:
1033 case CHXJ_SPEC_Chtml_5_0:
1034 case CHXJ_SPEC_Chtml_6_0:
1035 case CHXJ_SPEC_Chtml_7_0:
1036 case CHXJ_SPEC_XHtml_Mobile_1_0:
1037 case CHXJ_SPEC_Jhtml:
1038 lock = chxj_cookie_lock(r);
1039 cookie = chxj_save_cookie(r);
1040 s_add_cookie_id_if_has_location_header(r, cookie);
1041 chxj_cookie_unlock(r, lock);
1047 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1048 if (! ap_is_HTTP_REDIRECT(r->status)) {
1049 r->status = HTTP_MOVED_TEMPORARILY;
1052 s_add_no_cache_headers(r, entryp);
1053 ap_pass_brigade(f->next, bb);
1054 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1059 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1060 ap_pass_brigade(f->next, bb);
1061 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1066 for (b = APR_BRIGADE_FIRST(bb);
1067 b != APR_BRIGADE_SENTINEL(bb);
1068 b = APR_BUCKET_NEXT(b)) {
1070 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1071 chxj_dump_string(r, APLOG_MARK, "READ Data", data, len);
1073 /*--------------------------------------------------------------------*/
1075 /*--------------------------------------------------------------------*/
1077 DBG(r, "append data start");
1078 ctx = (mod_chxj_ctx *)f->ctx;
1081 tmp = apr_palloc(r->pool, ctx->len);
1082 memcpy(tmp, ctx->buffer, ctx->len);
1084 ctx->buffer = apr_palloc(pool, ctx->len + len);
1086 memcpy(ctx->buffer, tmp, ctx->len);
1087 memcpy(&ctx->buffer[ctx->len], data, len);
1091 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1094 if (APR_BUCKET_IS_EOS(b)) {
1096 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1097 /*----------------------------------------------------------------------*/
1099 /*----------------------------------------------------------------------*/
1101 cookie_lock_t *lock = NULL;
1102 ctx = (mod_chxj_ctx *)f->ctx;
1104 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1105 lock = chxj_cookie_lock(r);
1107 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1109 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1110 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1111 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1116 tmp = apr_palloc(pool, ctx->len + 1);
1118 memset(tmp, 0, ctx->len + 1);
1119 memcpy(tmp, ctx->buffer, ctx->len);
1121 ctx->buffer = chxj_convert(r,
1122 (const char **)&tmp,
1123 (apr_size_t *)&ctx->len,
1125 user_agent, &cookie);
1129 ctx->buffer = apr_psprintf(r->pool, "\n");
1131 ctx->buffer = chxj_convert(r,
1132 (const char **)&ctx->buffer,
1133 (apr_size_t *)&ctx->len,
1135 user_agent, &cookie);
1140 && *(char *)r->content_type == 't'
1141 && strncmp(r->content_type, "text/xml", 8) == 0) {
1142 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1150 memset(&doc, 0, sizeof(Doc));
1151 memset(&qrcode, 0, sizeof(qr_code_t));
1153 doc.parse_mode = PARSE_MODE_CHTML;
1157 qs_init_malloc(&doc);
1159 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1162 for (child = qs_get_child_node(&doc,root);
1164 child = qs_get_next_node(&doc,child)) {
1165 char *name = qs_get_node_name(&doc,child);
1166 if (strcasecmp("qrcode",name) == 0) {
1171 qs_all_free(&doc,QX_LOGMARK);
1173 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1174 chxj_qrcode_node_to_qrcode(&qrcode, root);
1175 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1177 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1178 chxj_cookie_unlock(r, lock);
1179 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1182 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1186 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1188 && ( *r->content_type == 'i' || *r->content_type == 'I')
1189 && dconf->image == CHXJ_IMG_ON
1190 && strncasecmp("image/", r->content_type, 6) == 0
1191 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1192 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1193 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1194 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1195 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1196 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1197 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1198 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1199 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1200 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1204 tmp = apr_palloc(pool, ctx->len + 1);
1206 memset(tmp, 0, ctx->len + 1);
1207 memcpy(tmp, ctx->buffer, ctx->len);
1209 chxj_convert_image(r,
1210 (const char **)&tmp,
1211 (apr_size_t *)&ctx->len);
1212 if (ctx->buffer == NULL) {
1218 apr_table_unset(r->headers_out, "Content-Length");
1219 apr_table_unset(r->err_headers_out, "Content-Length");
1220 ap_set_content_length(r, (apr_off_t)ctx->len);
1224 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1225 s_add_cookie_id_if_has_location_header(r, cookie);
1226 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1227 if (! ap_is_HTTP_REDIRECT(r->status)) {
1228 r->status = HTTP_MOVED_TEMPORARILY;
1231 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1232 ctx->buffer = apr_pstrdup(pool, "");
1234 ap_set_content_length(r, (apr_off_t)ctx->len);
1236 chxj_cookie_unlock(r,lock);
1237 s_add_no_cache_headers(r, entryp);
1238 rv = pass_data_to_filter(f,
1239 (const char *)ctx->buffer,
1240 (apr_size_t)ctx->len);
1243 ctx->buffer = apr_pstrdup(pool, "");
1245 ap_set_content_length(r, (apr_off_t)ctx->len);
1246 chxj_cookie_unlock(r, lock);
1247 s_add_no_cache_headers(r, entryp);
1248 rv = pass_data_to_filter(f,
1249 (const char *)ctx->buffer,
1250 (apr_size_t)ctx->len);
1252 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1256 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1261 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1262 cookie_lock_t *lock = NULL;
1263 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1264 switch(spec->html_spec_type) {
1265 case CHXJ_SPEC_Chtml_1_0:
1266 case CHXJ_SPEC_Chtml_2_0:
1267 case CHXJ_SPEC_Chtml_3_0:
1268 case CHXJ_SPEC_Chtml_4_0:
1269 case CHXJ_SPEC_Chtml_5_0:
1270 case CHXJ_SPEC_Chtml_6_0:
1271 case CHXJ_SPEC_Chtml_7_0:
1272 case CHXJ_SPEC_XHtml_Mobile_1_0:
1273 case CHXJ_SPEC_Jhtml:
1274 lock = chxj_cookie_lock(r);
1275 cookie = chxj_save_cookie(r);
1277 * Location Header Check to add cookie parameter.
1279 s_add_cookie_id_if_has_location_header(r, cookie);
1280 chxj_cookie_unlock(r, lock);
1281 apr_table_unset(r->headers_out, "Set-Cookie");
1282 apr_table_unset(r->err_headers_out, "Set-Cookie");
1289 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1290 if (! ap_is_HTTP_REDIRECT(r->status)) {
1291 r->status = HTTP_MOVED_TEMPORARILY;
1294 apr_table_setn(r->headers_out, "Content-Length", "0");
1295 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1296 s_add_no_cache_headers(r, entryp);
1297 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1298 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1303 apr_brigade_destroy(bb);
1305 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1311 * Add Cookie_id if it has location header.
1314 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1316 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1317 if (! location_header) {
1318 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1320 if (cookie && location_header) {
1321 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1322 location_header = chxj_add_cookie_parameter(r,
1325 apr_table_unset(r->headers_out, "Location");
1326 apr_table_setn(r->headers_out, "Location", location_header);
1327 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1328 if (!ap_is_HTTP_REDIRECT(r->status)) {
1329 r->status = HTTP_MOVED_TEMPORARILY;
1335 * It is the main loop of the input filter handler.
1339 chxj_input_handler(request_rec *r)
1341 mod_chxj_config *dconf;
1342 chxjconvrule_entry *entryp = NULL;
1343 device_table *spec = NULL;
1344 char *post_data = NULL;
1345 apr_size_t post_data_len = 0;
1350 int response_code = 0;
1352 DBG(r, "start of chxj_input_handler()");
1354 if (strcasecmp(r->handler, "chxj-input-handler")) {
1355 DBG(r, "end chxj_input_handler()");
1358 apr_pool_create(&pool, r->pool);
1360 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1361 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1363 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1365 spec = chxj_specified_device(r, user_agent);
1366 entryp = chxj_apply_convrule(r, dconf->convrules);
1368 post_data = apr_pstrdup(pool, "");
1369 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1370 if (ap_should_client_block(r)) {
1371 while (post_data_len < CHXJ_POST_MAX) {
1374 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1375 if (read_bytes<=0) {
1378 buffer[read_bytes] = '\0';
1379 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1380 post_data_len += read_bytes;
1389 if (post_data_len > 0) {
1390 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1391 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1395 if (dconf->forward_url_base) {
1396 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1399 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);
1402 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1404 DBG(r, "==> new url_path:[%s]", url_path);
1407 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1408 apr_table_unset(r->headers_in, "Content-Length");
1409 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1410 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1411 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1412 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1413 for (ii=0; ii<res_len/64; ii++) {
1414 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1416 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1419 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1420 if (strcasecmp(chunked, "chunked") == 0) {
1422 apr_table_unset(r->headers_out, "Transfer-Encoding");
1425 if (ap_is_HTTP_ERROR(response_code)) {
1426 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1427 return response_code;
1431 apr_pool_create(&wpool, r->pool);
1432 apr_bucket_brigade *bb;
1435 conn_rec *c = r->connection;
1437 bb = apr_brigade_create(wpool, c->bucket_alloc);
1438 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1439 APR_BRIGADE_INSERT_TAIL(bb, e);
1440 e = apr_bucket_eos_create(c->bucket_alloc);
1441 APR_BRIGADE_INSERT_TAIL(bb, e);
1442 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1443 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1446 apr_brigade_cleanup(bb);
1449 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1453 static mod_chxj_global_config *
1454 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1456 mod_chxj_global_config *conf;
1458 SDBG(s, "start chxj_global_config_create()");
1460 /*--------------------------------------------------------------------------*/
1461 /* allocate an own subpool which survives server restarts */
1462 /*--------------------------------------------------------------------------*/
1463 conf = (mod_chxj_global_config *)apr_palloc(pool,
1464 sizeof(mod_chxj_global_config));
1466 conf->cookie_db_lock = NULL;
1468 SDBG(s, "end chxj_global_config_create()");
1475 * initialize chxj module
1478 chxj_init_module(apr_pool_t *p,
1479 apr_pool_t *UNUSED(plog),
1480 apr_pool_t *UNUSED(ptemp),
1486 SDBG(s, "start chxj_init_module()");
1488 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1490 if (user_data == NULL) {
1493 * dummy user_data set.
1495 apr_pool_userdata_set(
1497 CHXJ_MOD_CONFIG_KEY,
1498 apr_pool_cleanup_null,
1500 SDBG(s, "end chxj_init_module()");
1504 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1506 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1508 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1509 return HTTP_INTERNAL_SERVER_ERROR;
1512 SDBG(s, "end chxj_init_module()");
1519 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1522 SDBG(s, "start chxj_child_init()");
1523 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1525 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1527 SDBG(s, "end chxj_child_init()");
1532 * A set structure of each server is generated.
1538 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1540 mod_chxj_global_config *gc;
1542 gc = chxj_global_config_create(p,s);
1549 chxj_translate_name(request_rec *r)
1551 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1552 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1553 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1554 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1555 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1556 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1558 return chxj_trans_name(r);
1566 chxj_insert_filter(request_rec *r)
1570 mod_chxj_config *dconf;
1571 chxjconvrule_entry *entryp;
1576 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1578 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1580 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1581 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1583 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1586 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1588 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1589 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1590 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1594 spec = chxj_specified_device(r, user_agent);
1595 entryp = chxj_apply_convrule(r, dconf->convrules);
1597 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1600 ctx = apr_palloc(r->pool, sizeof(*ctx));
1601 memset(ctx, 0, sizeof(*ctx));
1602 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1603 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1604 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1607 ctx->entryp = entryp;
1609 ctx->buffer = apr_palloc(ctx->pool, 1);
1612 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1613 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1614 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1618 switch(spec->html_spec_type) {
1619 case CHXJ_SPEC_Chtml_1_0:
1620 case CHXJ_SPEC_Chtml_2_0:
1621 case CHXJ_SPEC_Chtml_3_0:
1622 case CHXJ_SPEC_Chtml_4_0:
1623 case CHXJ_SPEC_Chtml_5_0:
1624 case CHXJ_SPEC_Chtml_6_0:
1625 case CHXJ_SPEC_Chtml_7_0:
1626 case CHXJ_SPEC_XHtml_Mobile_1_0:
1627 case CHXJ_SPEC_Hdml:
1628 case CHXJ_SPEC_Jhtml:
1629 case CHXJ_SPEC_Jxhtml:
1633 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1638 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1639 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1640 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1643 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1648 * The hook is registered.
1653 chxj_register_hooks(apr_pool_t *UNUSED(p))
1655 ap_hook_post_config(chxj_init_module,
1658 APR_HOOK_REALLY_FIRST);
1659 ap_hook_child_init(chxj_child_init,
1662 APR_HOOK_REALLY_FIRST);
1663 ap_register_output_filter (
1664 "chxj_output_filter",
1668 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1669 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1670 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1671 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1672 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1673 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1678 * A set structure according to the directory is generated.
1684 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1686 mod_chxj_config *conf;
1688 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1689 conf->device_data_file = NULL;
1690 conf->devices = NULL;
1691 conf->emoji_data_file = NULL;
1693 conf->emoji_tail = NULL;
1694 conf->image = CHXJ_IMG_NONE;
1695 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1696 conf->image_cache_limit = 0;
1697 conf->server_side_encoding = NULL;
1698 conf->cookie_db_dir = NULL;
1699 conf->cookie_timeout = 0;
1700 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1701 conf->cookie_lazy_mode = 0;
1702 #if defined(USE_MYSQL_COOKIE)
1703 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1704 conf->mysql.port = MYSQL_PORT;
1705 conf->mysql.host = NULL;
1707 #if defined(USE_MEMCACHE_COOKIE)
1708 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1709 conf->memcache.host = NULL;
1710 conf->memcache.port = 0;
1712 conf->forward_url_base = NULL;
1713 conf->forward_server_ip = NULL;
1714 conf->allowed_cookie_domain = NULL;
1715 conf->post_log = NULL;
1721 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1722 memset(conf->dir, 0, strlen(arg)+1);
1723 strcpy(conf->dir, arg);
1725 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1727 /* Default is copyleft */
1728 conf->image_copyright = NULL;
1735 * Merge per-directory CHXJ configurations
1738 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1740 mod_chxj_config *base;
1741 mod_chxj_config *add;
1742 mod_chxj_config *mrg;
1744 base = (mod_chxj_config *)basev;
1745 add = (mod_chxj_config *)addv;
1746 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1748 mrg->device_data_file = NULL;
1749 mrg->devices = NULL;
1750 mrg->emoji_data_file = NULL;
1751 mrg->image = CHXJ_IMG_NONE;
1752 mrg->image_cache_dir = NULL;
1753 mrg->image_copyright = NULL;
1754 mrg->image_cache_limit = 0;
1756 mrg->emoji_tail = NULL;
1757 mrg->new_line_type = NLTYPE_NIL;
1758 mrg->forward_url_base = NULL;
1759 mrg->forward_server_ip = NULL;
1760 mrg->allowed_cookie_domain = NULL;
1761 mrg->post_log = NULL;
1763 mrg->dir = apr_pstrdup(p, add->dir);
1765 if (! add->device_data_file) {
1766 mrg->devices = base->devices;
1767 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1770 mrg->devices = add->devices;
1771 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1774 if (! add->emoji_data_file) {
1775 mrg->emoji = base->emoji;
1776 mrg->emoji_tail = base->emoji_tail;
1777 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1780 mrg->emoji = add->emoji;
1781 mrg->emoji_tail = add->emoji_tail;
1782 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1785 if (add->image == CHXJ_IMG_NONE) {
1786 mrg->image = base->image;
1789 mrg->image = add->image;
1792 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1793 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1796 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1799 if (add->image_cache_limit) {
1800 mrg->image_cache_limit = add->image_cache_limit;
1803 mrg->image_cache_limit = base->image_cache_limit;
1806 if (add->image_copyright)
1807 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1809 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1811 if (add->server_side_encoding) {
1812 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1815 if (base->server_side_encoding) {
1816 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1819 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1822 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1824 if (add->cookie_db_dir) {
1825 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1828 if (base->cookie_db_dir) {
1829 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1832 mrg->cookie_db_dir = NULL;
1835 if (add->cookie_timeout) {
1836 mrg->cookie_timeout = add->cookie_timeout;
1839 if (base->cookie_db_dir) {
1840 mrg->cookie_timeout = base->cookie_timeout;
1843 mrg->cookie_timeout = 0;
1846 #if defined(USE_MYSQL_COOKIE)
1847 if (add->mysql.host) {
1848 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1850 else if (base->mysql.host) {
1851 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1854 mrg->mysql.host = NULL;
1856 if (add->mysql.port) {
1857 mrg->mysql.port = add->mysql.port;
1859 else if (base->mysql.port) {
1860 mrg->mysql.port = base->mysql.port;
1863 mrg->mysql.port = 0;
1866 if (add->mysql.database) {
1867 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1869 else if (base->mysql.database) {
1870 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1873 mrg->mysql.database = NULL;
1876 if (add->mysql.username) {
1877 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1879 else if (base->mysql.username) {
1880 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1883 mrg->mysql.username = NULL;
1886 if (add->mysql.password) {
1887 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1889 else if (base->mysql.password) {
1890 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1893 mrg->mysql.password = NULL;
1896 if (add->mysql.tablename) {
1897 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1899 else if (base->mysql.tablename) {
1900 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1903 mrg->mysql.tablename = NULL;
1906 if (add->mysql.socket_path) {
1907 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1909 else if (base->mysql.socket_path) {
1910 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1913 mrg->mysql.socket_path = NULL;
1916 if (add->mysql.charset) {
1917 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1919 else if (base->mysql.charset) {
1920 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1923 mrg->mysql.charset = NULL;
1926 #if defined(USE_MEMCACHE_COOKIE)
1927 if (add->memcache.host) {
1928 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1930 else if (base->memcache.host) {
1931 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1934 mrg->memcache.host = NULL;
1936 if (add->memcache.port) {
1937 mrg->memcache.port = add->memcache.port;
1939 else if (base->memcache.port) {
1940 mrg->memcache.port = base->memcache.port;
1943 mrg->memcache.port = 0;
1946 if (add->cookie_store_type) {
1947 mrg->cookie_store_type = add->cookie_store_type;
1949 else if (base->cookie_store_type) {
1950 mrg->cookie_store_type = base->cookie_store_type;
1953 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1955 if (add->cookie_lazy_mode) {
1956 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1958 else if (base->cookie_lazy_mode) {
1959 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1962 mrg->cookie_lazy_mode = 0;
1964 if (add->new_line_type) {
1965 mrg->new_line_type = add->new_line_type;
1967 else if (base->new_line_type) {
1968 mrg->new_line_type = base->new_line_type;
1971 mrg->new_line_type = NLTYPE_NIL;
1974 if (add->forward_url_base) {
1975 mrg->forward_url_base = add->forward_url_base;
1977 else if (base->forward_url_base) {
1978 mrg->forward_url_base = base->forward_url_base;
1981 if (add->allowed_cookie_domain) {
1982 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
1985 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
1987 if (add->post_log) {
1988 mrg->post_log = add->post_log;
1991 mrg->post_log = base->post_log;
1998 chxj_command_parse_take5(
2011 for (;*strp == ' '||*strp == '\t'; strp++) ;
2021 for (; *strp != '\0'; strp++) {
2022 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2023 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2028 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2029 || (isquoted && *strp == '"'))
2043 for (;*strp == ' '||*strp == '\t'; strp++) ;
2052 for (; *strp != '\0'; strp++) {
2053 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2054 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2059 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2060 || (isquoted && *strp == '"'))
2073 for (;*strp == ' '||*strp == '\t'; strp++);
2081 for (; *strp != '\0'; strp++) {
2082 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2083 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2088 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2089 || (isquoted && *strp == '"'))
2101 for (;*strp == ' '||*strp == '\t'; strp++);
2109 for (; *strp != '\0'; strp++) {
2110 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2111 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2116 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2117 || (isquoted && *strp == '"'))
2128 for (;*strp == ' '||*strp == '\t'; strp++);
2136 for (; *strp != '\0'; strp++) {
2137 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2138 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2143 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2144 || (isquoted && *strp == '"'))
2154 * The device definition file is loaded.
2156 * @param arg [i] The name of the device definition file is specified.
2157 * @param mconfig [i/o] The pointer to a set structure is specified.
2161 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2163 mod_chxj_config *conf;
2168 if (strlen(arg) > 256)
2169 return "mod_chxj: device data filename too long.";
2171 conf = (mod_chxj_config *)mconfig;
2172 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2174 qs_init_malloc(&doc);
2175 qs_init_root_node(&doc);
2177 qs_parse_file((Doc *)&doc, (const char *)arg);
2178 chxj_load_device_data(&doc,parms->pool, conf);
2179 qs_all_free(&doc, QX_LOGMARK);
2186 * Device definition information is loaded.
2189 * @param arg [i] The name of the device definition file is specified.
2190 * @param mconfig [i/o] The pointer to a set structure is specified.
2194 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2196 mod_chxj_config *conf;
2203 if (strlen(arg) > 256)
2204 return "mod_chxj: emoji data filename too long.";
2206 conf = (mod_chxj_config *)mconfig;
2207 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2208 qs_init_malloc(&doc);
2209 qs_init_root_node(&doc);
2211 qs_parse_file((Doc *)&doc, (const char *)arg);
2213 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2215 qs_all_free(&doc, QX_LOGMARK);
2223 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2225 mod_chxj_config *conf;
2230 if (strlen(arg) > 256)
2231 return "image uri is too long.";
2233 conf = (mod_chxj_config*)mconfig;
2234 if (strcasecmp("ON", arg) == 0) {
2235 conf->image = CHXJ_IMG_ON;
2238 conf->image = CHXJ_IMG_OFF;
2246 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2248 mod_chxj_config *conf;
2253 if (strlen(arg) > 256)
2254 return "cache dir name is too long.";
2256 conf = (mod_chxj_config *)mconfig;
2257 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2264 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2266 mod_chxj_config *conf;
2271 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2272 return "cache size is too long.";
2274 conf = (mod_chxj_config *)mconfig;
2277 * I use strtol function because strtoul is not portable function.
2279 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2282 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2284 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2293 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2295 mod_chxj_config *conf;
2300 if (strlen(arg) > 256)
2301 return "Copyright Flag is too long.";
2303 conf = (mod_chxj_config *)mconfig;
2304 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2311 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2315 mod_chxj_config *dconf;
2316 chxjconvrule_entry *newrule;
2326 dconf = (mod_chxj_config *)mconfig;
2328 if (strlen(arg) > 4096)
2329 return "mod_chxj: ChxjConvertRule: is too long.";
2331 dconf = (mod_chxj_config *)mconfig;
2332 if (dconf->convrules == NULL)
2333 dconf->convrules = apr_array_make(cmd->pool,
2335 sizeof(chxjconvrule_entry));
2337 newrule = apr_array_push(dconf->convrules);
2340 newrule->action = 0;
2342 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2343 return "ChxjConvertRule: bad argument line";
2345 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2349 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2355 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2356 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2359 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2360 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2363 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2364 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2367 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2368 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2374 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2375 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2377 else if (strcasecmp(CONVRULE_COOKIE_OFF_CMD, action) == 0) {
2378 newrule->action &= (0xffffffff ^ CONVRULE_COOKIE_ON_BIT);
2380 else if (strcasecmp(CONVRULE_CSS_ON_CMD, action) == 0) {
2381 newrule->action |= CONVRULE_CSS_ON_BIT;
2383 else if (strcasecmp(CONVRULE_CSS_OFF_CMD, action) == 0) {
2384 newrule->action &= (0xffffffff ^ CONVRULE_CSS_ON_BIT);
2390 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2391 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2397 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2398 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2404 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2405 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2411 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2412 newrule->action |= CONVRULE_Z2H_ON_BIT;
2415 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2416 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2419 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2420 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2423 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2424 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2427 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2428 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2431 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2432 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2435 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2436 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2439 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2440 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2451 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2455 mode = AP_REG_EXTENDED;
2456 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2457 return "RewriteRule: cannot compile regular expression ";
2459 newrule->regexp = regexp;
2461 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2463 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2465 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2467 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2468 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2470 newrule->user_agent = NULL;
2472 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2484 mod_chxj_config *dconf;
2487 if (strlen(arg) > 4096)
2488 return "mod_chxj: ChxjCookieDir is too long.";
2490 dconf = (mod_chxj_config *)mconfig;
2492 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2499 cmd_set_cookie_timeout(
2500 cmd_parms *UNUSED(cmd),
2504 mod_chxj_config *dconf;
2506 if (strlen(arg) > 4096)
2507 return "mod_chxj: ChxjCookieTimeout is too long.";
2509 if (chxj_chk_numeric(arg) != 0)
2510 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2512 dconf = (mod_chxj_config *)mconfig;
2514 dconf->cookie_timeout = atoi(arg);
2520 #if defined(USE_MYSQL_COOKIE)
2522 cmd_set_cookie_mysql_database(
2527 mod_chxj_config *dconf;
2529 if (strlen(arg) > 255)
2530 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2532 dconf = (mod_chxj_config *)mconfig;
2534 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2541 cmd_set_cookie_mysql_username(
2546 mod_chxj_config *dconf;
2548 if (strlen(arg) > 255)
2549 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2551 dconf = (mod_chxj_config *)mconfig;
2553 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2560 cmd_set_cookie_mysql_password(
2565 mod_chxj_config *dconf;
2567 if (strlen(arg) > 255)
2568 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2570 dconf = (mod_chxj_config *)mconfig;
2572 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2579 cmd_set_cookie_mysql_table_name(
2584 mod_chxj_config *dconf;
2586 if (strlen(arg) > 255)
2587 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2589 dconf = (mod_chxj_config *)mconfig;
2591 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2597 cmd_set_cookie_mysql_port(
2598 cmd_parms *UNUSED(cmd),
2602 mod_chxj_config *dconf;
2604 if (strlen(arg) > 255)
2605 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2607 dconf = (mod_chxj_config *)mconfig;
2609 if (chxj_chk_numeric(arg) != 0)
2610 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2612 dconf = (mod_chxj_config *)mconfig;
2614 dconf->mysql.port = chxj_atoi(arg);
2621 cmd_set_cookie_mysql_host(
2626 mod_chxj_config *dconf;
2628 if (strlen(arg) > 255)
2629 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2631 dconf = (mod_chxj_config *)mconfig;
2633 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2640 cmd_set_cookie_mysql_socket_path(
2645 mod_chxj_config *dconf;
2647 if (strlen(arg) > 4096)
2648 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2650 dconf = (mod_chxj_config *)mconfig;
2652 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2659 cmd_set_cookie_mysql_charset(
2664 mod_chxj_config *dconf;
2666 if (strlen(arg) > 255)
2667 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2669 dconf = (mod_chxj_config *)mconfig;
2671 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2676 #if defined(USE_MEMCACHE_COOKIE)
2678 cmd_set_cookie_memcache_port(
2679 cmd_parms *UNUSED(cmd),
2683 mod_chxj_config *dconf;
2685 if (strlen(arg) > 255)
2686 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2688 dconf = (mod_chxj_config *)mconfig;
2690 if (chxj_chk_numeric(arg) != 0)
2691 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2693 dconf = (mod_chxj_config *)mconfig;
2695 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2702 cmd_set_cookie_memcache_host(
2707 mod_chxj_config *dconf;
2709 if (strlen(arg) > 255)
2710 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2712 dconf = (mod_chxj_config *)mconfig;
2714 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2721 cmd_set_cookie_lazy_mode(
2722 cmd_parms *UNUSED(cmd),
2726 mod_chxj_config *dconf;
2728 if (strlen(arg) > 255)
2729 return "mod_chxj: ChxjCookieLazyMode is too long.";
2731 dconf = (mod_chxj_config *)mconfig;
2733 if (strcasecmp("TRUE",arg) == 0) {
2734 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2737 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2744 cmd_set_cookie_store_type(
2745 cmd_parms *UNUSED(cmd),
2749 mod_chxj_config *dconf;
2751 if (strlen(arg) > 255)
2752 return "mod_chxj: ChxjCookieStoreType is too long.";
2754 dconf = (mod_chxj_config *)mconfig;
2756 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2757 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2759 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2760 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2762 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2763 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2766 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2773 cmd_set_forward_url_base(
2778 mod_chxj_config *dconf;
2780 if (strlen(arg) > 255)
2781 return "mod_chxj: ChxjForwardUrlBase is too long.";
2783 dconf = (mod_chxj_config *)mconfig;
2785 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2791 cmd_set_forward_server_ip(
2796 mod_chxj_config *dconf;
2798 if (strlen(arg) > 255)
2799 return "mod_chxj: ChxjForwardServerIp is too long.";
2801 dconf = (mod_chxj_config *)mconfig;
2803 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2809 cmd_allowed_cookie_domain(
2814 mod_chxj_config *dconf;
2816 if (strlen(arg) > 255)
2817 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2819 dconf = (mod_chxj_config *)mconfig;
2821 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2827 cmd_set_new_line_type(
2828 cmd_parms *UNUSED(cmd),
2832 mod_chxj_config *dconf;
2833 if (strlen(arg) > 255)
2834 return "mod_chxj: ChxjNewLineType is too long.";
2836 dconf = (mod_chxj_config *)mconfig;
2838 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2839 dconf->new_line_type = NLTYPE_CRLF;
2841 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2842 dconf->new_line_type = NLTYPE_LF;
2844 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2845 dconf->new_line_type = NLTYPE_CR;
2847 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2848 dconf->new_line_type = NLTYPE_NONE;
2851 return "mod_chxj: invalid value (ChxjNewLineType)";
2862 mod_chxj_config *dconf;
2863 if (strlen(arg) > 255)
2864 return "mod_chxj: ChxjPostLogEnv is too long.";
2866 dconf = (mod_chxj_config *)mconfig;
2868 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2874 static const command_rec cmds[] = {
2876 "ChxjLoadDeviceData",
2877 cmd_load_device_data,
2880 "Load Device Data"),
2882 "ChxjLoadEmojiData",
2883 cmd_load_emoji_data,
2889 cmd_set_image_engine,
2892 "Convert Target URI"),
2894 "ChxjImageCacheDir",
2895 cmd_set_image_cache_dir,
2898 "Image Cache Directory"),
2900 "ChxjImageCacheLimit",
2901 cmd_set_image_cache_limit,
2904 "Image Cache Limit"),
2906 "ChxjImageCopyright",
2907 cmd_set_image_copyright,
2916 "an URL-applied regexp-pattern and a substitution URL"),
2922 "save cookie.db directory."),
2924 "ChxjCookieTimeout",
2925 cmd_set_cookie_timeout,
2928 "The compulsion time-out time of the cookie is specified. "),
2930 "ChxjCookieStoreType",
2931 cmd_set_cookie_store_type,
2934 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2936 "ChxjCookieLazyMode",
2937 cmd_set_cookie_lazy_mode,
2940 "OneTimeID is negligently done. (TRUE/FALSE)"),
2941 #if defined(USE_MYSQL_COOKIE)
2943 "ChxjCookieMysqlHost",
2944 cmd_set_cookie_mysql_host,
2947 "The MySQL database host used by saving Cookie"),
2949 "ChxjCookieMysqlPort",
2950 cmd_set_cookie_mysql_port,
2953 "The MySQL database port used by saving Cookie"),
2955 "ChxjCookieMysqlDatabase",
2956 cmd_set_cookie_mysql_database,
2959 "The MySQL database name used by saving Cookie"),
2961 "ChxjCookieMysqlUsername",
2962 cmd_set_cookie_mysql_username,
2965 "The MySQL username used by saving Cookie"),
2967 "ChxjCookieMysqlPassword",
2968 cmd_set_cookie_mysql_password,
2971 "The MySQL password used by saving Cookie"),
2973 "ChxjCookieMysqlTableName",
2974 cmd_set_cookie_mysql_table_name,
2977 "The MySQL table name used by saving Cookie"),
2979 "ChxjCookieMysqlSocketPath",
2980 cmd_set_cookie_mysql_socket_path,
2983 "The MySQL socket path used by saving Cookie"),
2985 "ChxjCookieMysqlCharset",
2986 cmd_set_cookie_mysql_charset,
2989 "The MySQL charset used by saving Cookie"),
2991 #if defined(USE_MEMCACHE_COOKIE)
2993 "ChxjCookieMemcacheHost",
2994 cmd_set_cookie_memcache_host,
2997 "The Memcached host used by saving Cookie"),
2999 "ChxjCookieMemcachePort",
3000 cmd_set_cookie_memcache_port,
3003 "The Memcached port used by saving Cookie"),
3007 cmd_set_new_line_type,
3010 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3012 "ChxjForwardUrlBase",
3013 cmd_set_forward_url_base,
3016 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3018 "ChxjForwardServerIp",
3019 cmd_set_forward_server_ip,
3022 "The forward server ip(default: this server ip)"),
3024 "ChxjAllowedCookieDomain",
3025 cmd_allowed_cookie_domain,
3028 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3034 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3039 /*----------------------------------------------------------------------------*/
3040 /* Dispatch list for API hooks */
3041 /*----------------------------------------------------------------------------*/
3042 module AP_MODULE_DECLARE_DATA chxj_module = {
3043 STANDARD20_MODULE_STUFF,
3044 chxj_create_per_dir_config, /* create per-dir config structures */
3045 chxj_merge_per_dir_config, /* merge per-dir config structures */
3046 chxj_config_server_create, /* create per-server config structures */
3047 NULL, /* merge per-server config structures */
3048 cmds, /* table of config file commands */
3049 chxj_register_hooks /* register hooks */