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_header_inf.h"
74 #include "chxj_jreserved_tag.h"
77 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
78 #define CHXJ_VERSION PACKAGE_VERSION
79 #define CHXJ_POST_MAX (0x1000000)
81 converter_t convert_routine[] = {
83 /* CHXJ_SPEC_UNKNOWN */
86 .emoji_only_converter = NULL,
89 /* CHXJ_SPEC_Chtml_1_0 */
90 .converter = chxj_convert_chtml10,
91 .encoder = chxj_encoding,
92 .emoji_only_converter = chxj_chtml10_emoji_only_converter,
95 /* CHXJ_SPEC_Chtml_2_0 */
96 .converter = chxj_convert_chtml20,
97 .encoder = chxj_encoding,
98 .emoji_only_converter = chxj_chtml20_emoji_only_converter,
101 /* CHXJ_SPEC_Chtml_3_0 */
102 .converter = chxj_convert_chtml30,
103 .encoder = chxj_encoding,
104 .emoji_only_converter = chxj_chtml30_emoji_only_converter,
107 /* CHXJ_SPEC_Chtml_4_0 */
108 .converter = chxj_convert_chtml40,
109 .encoder = chxj_encoding,
110 .emoji_only_converter = chxj_chtml40_emoji_only_converter,
113 /* CHXJ_SPEC_Chtml_5_0 */
114 .converter = chxj_convert_chtml50,
115 .encoder = chxj_encoding,
116 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
119 /* CHXJ_SPEC_Chtml_6_0 */
120 .converter = chxj_convert_ixhtml10,
121 .encoder = chxj_encoding,
122 .emoji_only_converter = chxj_chtml50_emoji_only_converter, /* XXX: TODO */
125 /* CHXJ_SPEC_Chtml_7_0 */
126 .converter = chxj_convert_ixhtml10,
127 .encoder = chxj_encoding,
128 .emoji_only_converter = chxj_chtml50_emoji_only_converter, /* XXX: TODO */
131 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
132 .converter = chxj_convert_xhtml_mobile_1_0,
133 .encoder = chxj_encoding,
134 .emoji_only_converter = chxj_xhtml_emoji_only_converter,
138 .converter = chxj_convert_hdml,
139 .encoder = chxj_encoding,
140 .emoji_only_converter = NULL,
143 /* CHXJ_SPEC_Jhtml */
144 .converter = chxj_convert_jhtml,
145 .encoder = chxj_encoding,
146 .emoji_only_converter = chxj_jhtml_emoji_only_converter,
149 /* CHXJ_SPEC_Jxhtml */
150 .converter = chxj_convert_jxhtml,
151 .encoder = chxj_encoding,
152 .emoji_only_converter = chxj_jxhtml_emoji_only_converter,
158 .emoji_only_converter = NULL,
162 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec);
163 static void s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec);
164 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
165 static void s_clear_cookie_header(request_rec *r, device_table *spec);
166 static void s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp);
169 * Only when User-Agent is specified, the User-Agent header is camouflaged.
174 chxj_headers_fixup(request_rec *r)
176 mod_chxj_config* dconf;
177 chxjconvrule_entry* entryp;
183 DBG(r, "REQ[%X] start chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
185 DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)(apr_size_t)r);
186 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
190 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
192 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
193 spec = chxj_specified_device(r, user_agent);
195 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
197 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
198 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)(apr_size_t)r);
199 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
202 if (r->method_number == M_POST) {
203 if (!apr_table_get(r->headers_in, "X-Chxj-Forward")) {
204 s_clear_cookie_header(r, spec);
208 s_clear_cookie_header(r, spec);
211 switch(spec->html_spec_type) {
212 case CHXJ_SPEC_Chtml_1_0:
213 case CHXJ_SPEC_Chtml_2_0:
214 case CHXJ_SPEC_Chtml_3_0:
215 case CHXJ_SPEC_Chtml_4_0:
216 case CHXJ_SPEC_Chtml_5_0:
217 case CHXJ_SPEC_Chtml_6_0:
218 case CHXJ_SPEC_Chtml_7_0:
219 case CHXJ_SPEC_XHtml_Mobile_1_0:
221 case CHXJ_SPEC_Jhtml:
222 case CHXJ_SPEC_Jxhtml:
223 entryp = chxj_apply_convrule(r, dconf->convrules);
225 DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int)(apr_size_t)r);
228 if (!(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
229 DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int)(apr_size_t)r);
232 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
233 DBG(r, "REQ[%X] end chxj_headers_fixup() (emoji only)", (unsigned int)(apr_size_t)r);
237 apr_table_setn(r->headers_in,
238 CHXJ_HTTP_USER_AGENT,
241 if (entryp->user_agent)
242 apr_table_setn(r->headers_in,
246 chxj_convert_input_header(r,entryp, spec);
251 DBG(r, "REQ[%X] end chxj_headers_fixup() (not mobile) spec->device_name[%s] User-Agent:[%s]", (unsigned int)(apr_size_t)r, spec->device_name, user_agent);
257 if (r->method_number == M_POST) {
258 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
259 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);
260 r->proxyreq = PROXYREQ_NONE;
261 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
264 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
266 if (dconf->post_log) {
267 apr_table_setn(r->subprocess_env, dconf->post_log, dconf->post_log);
270 apr_table_setn(r->subprocess_env, "chxj-post-log", "chxj-post-log");
272 apr_sockaddr_t *address = NULL;
273 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
274 if (rv != APR_SUCCESS) {
276 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));
277 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
281 if (dconf->forward_server_ip) {
282 addr = dconf->forward_server_ip;
285 apr_sockaddr_ip_get(&addr, address);
287 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);
288 if (strcmp(addr, r->connection->remote_ip) == 0) {
289 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
291 if (! apr_table_get(r->headers_in, "Content-Length")) {
292 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
294 apr_table_set(r->headers_in, "Content-Length", contentLength);
301 chxj_add_device_env(r, spec);
303 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
310 s_clear_cookie_header(request_rec *r, device_table *spec)
312 switch(spec->html_spec_type) {
313 case CHXJ_SPEC_Chtml_1_0:
314 case CHXJ_SPEC_Chtml_2_0:
315 case CHXJ_SPEC_Chtml_3_0:
316 case CHXJ_SPEC_Chtml_4_0:
317 case CHXJ_SPEC_Chtml_5_0:
318 case CHXJ_SPEC_Chtml_6_0:
319 case CHXJ_SPEC_Chtml_7_0:
320 case CHXJ_SPEC_XHtml_Mobile_1_0:
321 case CHXJ_SPEC_Jhtml:
322 apr_table_unset(r->headers_in, "Cookie");
331 * It converts it from CHTML into XXML corresponding to each model.
334 * @param src [i] It is former HTML character string.
335 * @param len [i/o] It is length of former HTML character string.
338 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
344 mod_chxj_config *dconf;
345 chxjconvrule_entry *entryp;
347 DBG(r,"REQ[%X] start of chxj_convert()", (unsigned int)(apr_size_t)r);
349 chxj_dump_string(r, APLOG_MARK, "INPUT Data", *src, *len);
351 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
353 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
356 entryp = chxj_apply_convrule(r, dconf->convrules);
358 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
359 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
364 /*------------------------------------------------------------------------*/
365 /* get UserAgent from http header */
366 /*------------------------------------------------------------------------*/
367 if (entryp->user_agent)
368 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
370 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
372 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
373 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
376 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
377 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
378 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
379 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
383 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
385 spec = chxj_specified_device(r, user_agent);
392 if (entryp->action & CONVRULE_COOKIE_ON_BIT
393 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
394 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
395 switch(spec->html_spec_type) {
396 case CHXJ_SPEC_Chtml_1_0:
397 case CHXJ_SPEC_Chtml_2_0:
398 case CHXJ_SPEC_Chtml_3_0:
399 case CHXJ_SPEC_Chtml_4_0:
400 case CHXJ_SPEC_Chtml_5_0:
401 case CHXJ_SPEC_Chtml_6_0:
402 case CHXJ_SPEC_Chtml_7_0:
403 case CHXJ_SPEC_XHtml_Mobile_1_0:
404 case CHXJ_SPEC_Jhtml:
405 cookie = chxj_save_cookie(r);
412 if (!r->header_only) {
415 if (convert_routine[spec->html_spec_type].encoder)
416 tmp = convert_routine[spec->html_spec_type].encoder(r,
420 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
422 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
425 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
427 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
428 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
433 dst = apr_palloc(r->pool, 1);
438 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
441 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
442 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
443 if (convert_routine[spec->html_spec_type].converter) {
445 dst = convert_routine[spec->html_spec_type].converter(r,
453 dst = convert_routine[spec->html_spec_type].converter(r,
463 ap_set_content_length(r, *len);
466 dst = apr_psprintf(r->pool, "\n");
475 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
482 * convert GUID parameter.
486 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
488 DBG(r, "REQ[%X] start s_convert_guid_parameter() param:[%s]", (unsigned int)(apr_size_t)r, param);
489 if (strcasecmp(param, "guid") == 0) {
490 switch(spec->html_spec_type) {
491 case CHXJ_SPEC_XHtml_Mobile_1_0:
493 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
495 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
499 case CHXJ_SPEC_Jhtml:
500 case CHXJ_SPEC_Jxhtml:
502 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
504 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
512 DBG(r, "REQ[%X] end s_convert_guid_parameter()", (unsigned int)(apr_size_t)r);
516 * It converts it from HEADER.
521 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
533 cookie_t *cookie = NULL;
534 int no_update_flag = 0;
536 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
539 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
540 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
543 urilen = strlen(r->args);
545 result = qs_alloc_zero_byte_string(r->pool);
547 buff_pre = apr_pstrdup(r->pool, r->args);
552 pair = apr_strtok(buff_pre, "&", &pstate);
558 pair_sv = apr_pstrdup(r->pool, pair);
560 name = apr_strtok(pair, "=", &vstate);
561 value = apr_strtok(NULL, "=", &vstate);
562 if (! name) continue;
563 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
564 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
569 buff = apr_pstrdup(r->pool, r->args);
570 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
579 pair = apr_strtok(buff, "&", &pstate);
585 pair_sv = apr_pstrdup(r->pool, pair);
587 name = apr_strtok(pair, "=", &vstate);
588 value = apr_strtok(NULL, "=", &vstate);
589 if (! name) continue;
590 name = chxj_safe_to_jreserved_tag(r, name);
591 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
592 if (strlen(result) != 0)
593 result = apr_pstrcat(r->pool, result, "&", NULL);
595 if (strcasecmp(entryp->encoding, "NONE") != 0) {
600 if (value && *value != 0) {
601 value = chxj_url_decode(r->pool, value);
602 dlen = strlen(value);
603 DBG(r, "************ before encoding[%s]", value);
605 dvalue = chxj_rencoding(r, value, &dlen);
606 dvalue = chxj_url_encode(r->pool, dvalue);
608 DBG(r, "************ after encoding[%s]", dvalue);
614 if (name && *name != 0) {
615 name = chxj_url_decode(r->pool, name);
617 dname = chxj_rencoding(r, name, &dlen);
618 dname = chxj_url_encode(r->pool, dname);
623 s_convert_guid_parameter_to_header(r, dname, spec);
624 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
627 s_convert_guid_parameter_to_header(r, name, spec);
628 if (strcmp(name, pair_sv) != 0) {
629 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
632 result = apr_pstrcat(r->pool, result, name, NULL);
637 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
638 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
639 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
643 if (strlen(value) == 0)
646 if (strlen(result) != 0)
647 result = apr_pstrcat(r->pool, result, "&", NULL);
649 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
652 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
654 apr_table_unset(r->headers_in, "Cookie");
655 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
656 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
657 cookie_lock_t *lock = chxj_cookie_lock(r);
658 cookie = chxj_load_cookie(r, value);
659 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
660 if (! no_update_flag && cookie) {
661 cookie = chxj_update_cookie(r, cookie);
663 chxj_cookie_unlock(r, lock);
665 if (cookie && cookie->cookie_id) {
666 if (strlen(result) != 0)
667 result = apr_pstrcat(r->pool, result, "&", NULL);
668 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
672 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
673 if (strlen(result) != 0)
674 result = apr_pstrcat(r->pool, result, "&", NULL);
675 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
678 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
679 if (! no_update_flag) {
680 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
684 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
685 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
691 * It converts it from POSTDATA .
694 * @param src [i] It is POSTDATA character string.
695 * @param len [i/o] It is length of former HTML character string.
702 chxjconvrule_entry *entryp,
712 cookie_t *cookie = NULL;
714 int no_update_flag = 0;
718 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
721 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
722 return apr_pstrdup(r->pool, "");
725 apr_pool_create(&pool, r->pool);
727 s = apr_pstrdup(pool, *src);
729 buff_pre = apr_pstrdup(pool, *src);
731 result = qs_alloc_zero_byte_string(pool);
733 chxj_dump_string(r, APLOG_MARK, "BEFORE input convert source", s, ilen);
738 pair = apr_strtok(buff_pre, "&", &pstate);
744 pair_sv = apr_pstrdup(pool, pair);
746 name = apr_strtok(pair, "=", &vstate);
747 value = apr_strtok(NULL, "=", &vstate);
748 if (! name) continue;
749 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
750 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
760 pair = apr_strtok(s, "&", &pstate);
765 name = apr_strtok(pair, "=", &vstate);
766 value = apr_strtok(NULL, "=", &vstate);
767 if (! name) continue;
768 name = chxj_safe_to_jreserved_tag(r, name);
770 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
771 if (strlen(result) != 0)
772 result = apr_pstrcat(pool, result, "&", NULL);
774 if (strcasecmp(entryp->encoding, "NONE") != 0) {
779 if (value && *value != 0) {
780 value = chxj_url_decode(pool, value);
781 dlen = strlen(value);
782 dvalue = chxj_rencoding(r, value, &dlen);
783 dvalue = chxj_url_encode(pool, dvalue);
789 if (name && *name != 0) {
790 name = chxj_url_decode(pool, name);
792 dname = chxj_rencoding(r, name, &dlen);
793 dname = chxj_url_encode(pool, dname);
800 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
803 result = apr_pstrcat(pool, result, name, "=", value, NULL);
807 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
808 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
809 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
813 if (strlen(value) == 0)
816 if (strlen(result) != 0)
817 result = apr_pstrcat(pool, result, "&", NULL);
819 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
823 dlen = strlen(value);
824 value = chxj_url_decode(pool, value);
825 dvalue = chxj_rencoding(r, value, &dlen);
826 dvalue = chxj_url_encode(pool,dvalue);
827 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
831 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
835 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
837 apr_table_unset(r->headers_in, "Cookie");
838 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
839 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
840 cookie_lock_t *lock = chxj_cookie_lock(r);
841 cookie = chxj_load_cookie(r, value);
842 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
843 if (! no_update_flag && cookie) {
844 cookie = chxj_update_cookie(r, cookie);
846 chxj_cookie_unlock(r, lock);
848 if (cookie && cookie->cookie_id) {
849 if (strlen(result) != 0)
850 result = apr_pstrcat(pool, result, "&", NULL);
851 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
855 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
856 if (strlen(result) != 0)
857 result = apr_pstrcat(pool, result, "&", NULL);
858 result = apr_pstrcat(pool, result, name, "=", value, NULL);
861 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
864 dlen = strlen(value);
866 value = chxj_url_decode(pool, value);
867 dvalue = chxj_rencoding(r, value, &dlen);
868 dvalue = chxj_url_encode(pool,dvalue);
869 if (r->args && strlen(r->args) > 0) {
870 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
873 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
875 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
879 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
882 dlen = strlen(value);
884 value = chxj_url_decode(pool, value);
885 dvalue = chxj_rencoding(r, value, &dlen);
886 dvalue = chxj_url_encode(pool,dvalue);
887 if (r->args && strlen(r->args) > 0) {
888 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
891 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
893 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
896 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
898 *len = strlen(result);
899 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
900 if (! no_update_flag) {
901 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
904 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
905 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
912 * The received data is returned to the filter.
914 * @param f [i/o] It is a filter.
915 * @param data [i] It is data returned to the filter.
916 * @param len [i] It is length of the data returned to the filter.
919 pass_data_to_filter(ap_filter_t *f, const char *data,
922 request_rec *r = f->r;
923 conn_rec *c = r->connection;
925 apr_bucket_brigade *bb;
928 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
930 chxj_header_inf_clear(r);
932 bb = apr_brigade_create(r->pool, c->bucket_alloc);
933 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
935 APR_BRIGADE_INSERT_TAIL(bb, b);
936 b = apr_bucket_eos_create(f->c->bucket_alloc);
937 APR_BRIGADE_INSERT_TAIL(bb, b);
939 rv = ap_pass_brigade(f->next, bb);
940 if (rv != APR_SUCCESS) {
941 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
945 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
951 * Add No Cache Header
954 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
956 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
957 apr_table_unset(r->headers_out, "Pragma");
958 apr_table_unset(r->err_headers_out, "Pragma");
959 apr_table_unset(r->headers_out, "Expires");
960 apr_table_unset(r->err_headers_out, "Expires");
961 apr_table_unset(r->headers_out, "Cache-Control");
962 apr_table_unset(r->err_headers_out, "Cache-Control");
964 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
965 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
966 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
972 * It is the main loop of the output filter.
974 * @param f [i/o] It is a filter.
978 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
984 char *user_agent = NULL;
986 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
987 cookie_t *cookie = NULL;
988 mod_chxj_config *dconf;
989 chxjconvrule_entry *entryp = NULL;
990 device_table *spec = NULL;
994 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
997 apr_pool_create(&pool, r->pool);
999 entryp = ctx->entryp;
1001 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1003 if (r->content_type) {
1004 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1005 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1006 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1007 && ! (dconf->image == CHXJ_IMG_ON
1008 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1009 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1010 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1011 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1012 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1013 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1014 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1015 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1016 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1017 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1018 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1020 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1021 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1022 cookie_lock_t *lock = NULL;
1023 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1024 switch(spec->html_spec_type) {
1025 case CHXJ_SPEC_Chtml_1_0:
1026 case CHXJ_SPEC_Chtml_2_0:
1027 case CHXJ_SPEC_Chtml_3_0:
1028 case CHXJ_SPEC_Chtml_4_0:
1029 case CHXJ_SPEC_Chtml_5_0:
1030 case CHXJ_SPEC_Chtml_6_0:
1031 case CHXJ_SPEC_Chtml_7_0:
1032 case CHXJ_SPEC_XHtml_Mobile_1_0:
1033 case CHXJ_SPEC_Jhtml:
1034 lock = chxj_cookie_lock(r);
1035 cookie = chxj_save_cookie(r);
1036 s_add_cookie_id_if_has_location_header(r, cookie);
1037 chxj_cookie_unlock(r, lock);
1043 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1044 if (! ap_is_HTTP_REDIRECT(r->status)) {
1045 r->status = HTTP_MOVED_TEMPORARILY;
1048 s_add_no_cache_headers(r, entryp);
1049 ap_pass_brigade(f->next, bb);
1050 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1055 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1056 ap_pass_brigade(f->next, bb);
1057 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1062 for (b = APR_BRIGADE_FIRST(bb);
1063 b != APR_BRIGADE_SENTINEL(bb);
1064 b = APR_BUCKET_NEXT(b)) {
1066 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1067 chxj_dump_string(r, APLOG_MARK, "READ Data", data, len);
1069 /*--------------------------------------------------------------------*/
1071 /*--------------------------------------------------------------------*/
1073 DBG(r, "append data start");
1074 ctx = (mod_chxj_ctx *)f->ctx;
1077 tmp = apr_palloc(r->pool, ctx->len);
1078 memcpy(tmp, ctx->buffer, ctx->len);
1080 ctx->buffer = apr_palloc(pool, ctx->len + len);
1082 memcpy(ctx->buffer, tmp, ctx->len);
1083 memcpy(&ctx->buffer[ctx->len], data, len);
1087 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1090 if (APR_BUCKET_IS_EOS(b)) {
1092 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1093 /*----------------------------------------------------------------------*/
1095 /*----------------------------------------------------------------------*/
1097 cookie_lock_t *lock = NULL;
1098 ctx = (mod_chxj_ctx *)f->ctx;
1100 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1101 lock = chxj_cookie_lock(r);
1103 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1105 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1106 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1107 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1112 tmp = apr_palloc(pool, ctx->len + 1);
1114 memset(tmp, 0, ctx->len + 1);
1115 memcpy(tmp, ctx->buffer, ctx->len);
1117 ctx->buffer = chxj_convert(r,
1118 (const char **)&tmp,
1119 (apr_size_t *)&ctx->len,
1121 user_agent, &cookie);
1125 ctx->buffer = apr_psprintf(r->pool, "\n");
1127 ctx->buffer = chxj_convert(r,
1128 (const char **)&ctx->buffer,
1129 (apr_size_t *)&ctx->len,
1131 user_agent, &cookie);
1136 && *(char *)r->content_type == 't'
1137 && strncmp(r->content_type, "text/xml", 8) == 0) {
1138 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1146 memset(&doc, 0, sizeof(Doc));
1147 memset(&qrcode, 0, sizeof(qr_code_t));
1149 doc.parse_mode = PARSE_MODE_CHTML;
1153 qs_init_malloc(&doc);
1155 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1158 for (child = qs_get_child_node(&doc,root);
1160 child = qs_get_next_node(&doc,child)) {
1161 char *name = qs_get_node_name(&doc,child);
1162 if (strcasecmp("qrcode",name) == 0) {
1167 qs_all_free(&doc,QX_LOGMARK);
1169 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1170 chxj_qrcode_node_to_qrcode(&qrcode, root);
1171 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1173 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1174 chxj_cookie_unlock(r, lock);
1175 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1178 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1182 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1184 && ( *r->content_type == 'i' || *r->content_type == 'I')
1185 && dconf->image == CHXJ_IMG_ON
1186 && strncasecmp("image/", r->content_type, 6) == 0
1187 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1188 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1189 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1190 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1191 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1192 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1193 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1194 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1195 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1196 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1200 tmp = apr_palloc(pool, ctx->len + 1);
1202 memset(tmp, 0, ctx->len + 1);
1203 memcpy(tmp, ctx->buffer, ctx->len);
1205 chxj_convert_image(r,
1206 (const char **)&tmp,
1207 (apr_size_t *)&ctx->len);
1208 if (ctx->buffer == NULL) {
1214 apr_table_unset(r->headers_out, "Content-Length");
1215 apr_table_unset(r->err_headers_out, "Content-Length");
1216 ap_set_content_length(r, (apr_off_t)ctx->len);
1220 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1221 s_add_cookie_id_if_has_location_header(r, cookie);
1222 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1223 if (! ap_is_HTTP_REDIRECT(r->status)) {
1224 r->status = HTTP_MOVED_TEMPORARILY;
1227 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1228 ctx->buffer = apr_pstrdup(pool, "");
1230 ap_set_content_length(r, (apr_off_t)ctx->len);
1232 chxj_cookie_unlock(r,lock);
1233 s_add_no_cache_headers(r, entryp);
1234 rv = pass_data_to_filter(f,
1235 (const char *)ctx->buffer,
1236 (apr_size_t)ctx->len);
1239 ctx->buffer = apr_pstrdup(pool, "");
1241 ap_set_content_length(r, (apr_off_t)ctx->len);
1242 chxj_cookie_unlock(r, lock);
1243 s_add_no_cache_headers(r, entryp);
1244 rv = pass_data_to_filter(f,
1245 (const char *)ctx->buffer,
1246 (apr_size_t)ctx->len);
1248 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1252 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1257 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1258 cookie_lock_t *lock = NULL;
1259 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1260 switch(spec->html_spec_type) {
1261 case CHXJ_SPEC_Chtml_1_0:
1262 case CHXJ_SPEC_Chtml_2_0:
1263 case CHXJ_SPEC_Chtml_3_0:
1264 case CHXJ_SPEC_Chtml_4_0:
1265 case CHXJ_SPEC_Chtml_5_0:
1266 case CHXJ_SPEC_Chtml_6_0:
1267 case CHXJ_SPEC_Chtml_7_0:
1268 case CHXJ_SPEC_XHtml_Mobile_1_0:
1269 case CHXJ_SPEC_Jhtml:
1270 lock = chxj_cookie_lock(r);
1271 cookie = chxj_save_cookie(r);
1273 * Location Header Check to add cookie parameter.
1275 s_add_cookie_id_if_has_location_header(r, cookie);
1276 chxj_cookie_unlock(r, lock);
1277 apr_table_unset(r->headers_out, "Set-Cookie");
1278 apr_table_unset(r->err_headers_out, "Set-Cookie");
1285 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1286 if (! ap_is_HTTP_REDIRECT(r->status)) {
1287 r->status = HTTP_MOVED_TEMPORARILY;
1290 apr_table_setn(r->headers_out, "Content-Length", "0");
1291 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1292 s_add_no_cache_headers(r, entryp);
1293 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1294 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1299 apr_brigade_destroy(bb);
1301 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1307 * Add Cookie_id if it has location header.
1310 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1312 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1313 if (! location_header) {
1314 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1316 if (cookie && location_header) {
1317 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1318 location_header = chxj_add_cookie_parameter(r,
1321 apr_table_unset(r->headers_out, "Location");
1322 apr_table_setn(r->headers_out, "Location", location_header);
1323 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1324 if (!ap_is_HTTP_REDIRECT(r->status)) {
1325 r->status = HTTP_MOVED_TEMPORARILY;
1331 * It is the main loop of the input filter handler.
1335 chxj_input_handler(request_rec *r)
1337 mod_chxj_config *dconf;
1338 chxjconvrule_entry *entryp = NULL;
1339 device_table *spec = NULL;
1340 char *post_data = NULL;
1341 apr_size_t post_data_len = 0;
1346 int response_code = 0;
1348 DBG(r, "start of chxj_input_handler()");
1350 if (strcasecmp(r->handler, "chxj-input-handler")) {
1351 DBG(r, "end chxj_input_handler()");
1354 apr_pool_create(&pool, r->pool);
1356 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1357 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1359 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1361 spec = chxj_specified_device(r, user_agent);
1362 entryp = chxj_apply_convrule(r, dconf->convrules);
1364 post_data = apr_pstrdup(pool, "");
1365 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1366 if (ap_should_client_block(r)) {
1367 while (post_data_len < CHXJ_POST_MAX) {
1370 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1371 if (read_bytes<=0) {
1374 buffer[read_bytes] = '\0';
1375 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1376 post_data_len += read_bytes;
1385 if (post_data_len > 0) {
1386 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1387 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1391 if (dconf->forward_url_base) {
1392 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1395 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);
1398 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1400 DBG(r, "==> new url_path:[%s]", url_path);
1403 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1404 apr_table_unset(r->headers_in, "Content-Length");
1405 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1406 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1407 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1408 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1409 for (ii=0; ii<res_len/64; ii++) {
1410 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1412 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1415 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1416 if (strcasecmp(chunked, "chunked") == 0) {
1418 apr_table_unset(r->headers_out, "Transfer-Encoding");
1421 if (ap_is_HTTP_ERROR(response_code)) {
1422 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1423 return response_code;
1427 apr_pool_create(&wpool, r->pool);
1428 apr_bucket_brigade *bb;
1431 conn_rec *c = r->connection;
1433 bb = apr_brigade_create(wpool, c->bucket_alloc);
1434 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1435 APR_BRIGADE_INSERT_TAIL(bb, e);
1436 e = apr_bucket_eos_create(c->bucket_alloc);
1437 APR_BRIGADE_INSERT_TAIL(bb, e);
1438 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1439 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1442 apr_brigade_cleanup(bb);
1445 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1449 static mod_chxj_global_config *
1450 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1452 mod_chxj_global_config *conf;
1454 SDBG(s, "start chxj_global_config_create()");
1456 /*--------------------------------------------------------------------------*/
1457 /* allocate an own subpool which survives server restarts */
1458 /*--------------------------------------------------------------------------*/
1459 conf = (mod_chxj_global_config *)apr_palloc(pool,
1460 sizeof(mod_chxj_global_config));
1462 conf->cookie_db_lock = NULL;
1464 SDBG(s, "end chxj_global_config_create()");
1471 * initialize chxj module
1474 chxj_init_module(apr_pool_t *p,
1475 apr_pool_t *UNUSED(plog),
1476 apr_pool_t *UNUSED(ptemp),
1482 SDBG(s, "start chxj_init_module()");
1484 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1486 if (user_data == NULL) {
1489 * dummy user_data set.
1491 apr_pool_userdata_set(
1493 CHXJ_MOD_CONFIG_KEY,
1494 apr_pool_cleanup_null,
1496 SDBG(s, "end chxj_init_module()");
1500 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1502 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1504 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1505 return HTTP_INTERNAL_SERVER_ERROR;
1508 SDBG(s, "end chxj_init_module()");
1515 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1518 SDBG(s, "start chxj_child_init()");
1519 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1521 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1523 SDBG(s, "end chxj_child_init()");
1528 * A set structure of each server is generated.
1534 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1536 mod_chxj_global_config *gc;
1538 gc = chxj_global_config_create(p,s);
1545 chxj_translate_name(request_rec *r)
1547 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1548 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1549 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1550 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1551 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1552 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1554 return chxj_trans_name(r);
1562 chxj_insert_filter(request_rec *r)
1566 mod_chxj_config *dconf;
1567 chxjconvrule_entry *entryp;
1572 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1574 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1576 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1577 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1579 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1582 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1584 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1585 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1586 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1590 spec = chxj_specified_device(r, user_agent);
1591 entryp = chxj_apply_convrule(r, dconf->convrules);
1593 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1596 ctx = apr_palloc(r->pool, sizeof(*ctx));
1597 memset(ctx, 0, sizeof(*ctx));
1598 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1599 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1600 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1603 ctx->entryp = entryp;
1605 ctx->buffer = apr_palloc(ctx->pool, 1);
1608 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1609 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1610 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1614 switch(spec->html_spec_type) {
1615 case CHXJ_SPEC_Chtml_1_0:
1616 case CHXJ_SPEC_Chtml_2_0:
1617 case CHXJ_SPEC_Chtml_3_0:
1618 case CHXJ_SPEC_Chtml_4_0:
1619 case CHXJ_SPEC_Chtml_5_0:
1620 case CHXJ_SPEC_Chtml_6_0:
1621 case CHXJ_SPEC_Chtml_7_0:
1622 case CHXJ_SPEC_XHtml_Mobile_1_0:
1623 case CHXJ_SPEC_Hdml:
1624 case CHXJ_SPEC_Jhtml:
1625 case CHXJ_SPEC_Jxhtml:
1629 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1634 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1635 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1636 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1639 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1644 * The hook is registered.
1649 chxj_register_hooks(apr_pool_t *UNUSED(p))
1651 ap_hook_post_config(chxj_init_module,
1654 APR_HOOK_REALLY_FIRST);
1655 ap_hook_child_init(chxj_child_init,
1658 APR_HOOK_REALLY_FIRST);
1659 ap_register_output_filter (
1660 "chxj_output_filter",
1664 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1665 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1666 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1667 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1668 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1669 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1674 * A set structure according to the directory is generated.
1680 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1682 mod_chxj_config *conf;
1684 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1685 conf->device_data_file = NULL;
1686 conf->devices = NULL;
1687 conf->emoji_data_file = NULL;
1689 conf->emoji_tail = NULL;
1690 conf->image = CHXJ_IMG_NONE;
1691 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1692 conf->image_cache_limit = 0;
1693 conf->server_side_encoding = NULL;
1694 conf->cookie_db_dir = NULL;
1695 conf->cookie_timeout = 0;
1696 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1697 conf->cookie_lazy_mode = 0;
1698 #if defined(USE_MYSQL_COOKIE)
1699 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1700 conf->mysql.port = MYSQL_PORT;
1701 conf->mysql.host = NULL;
1703 #if defined(USE_MEMCACHE_COOKIE)
1704 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1705 conf->memcache.host = NULL;
1706 conf->memcache.port = 0;
1708 conf->forward_url_base = NULL;
1709 conf->forward_server_ip = NULL;
1710 conf->allowed_cookie_domain = NULL;
1711 conf->post_log = NULL;
1717 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1718 memset(conf->dir, 0, strlen(arg)+1);
1719 strcpy(conf->dir, arg);
1721 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1723 /* Default is copyleft */
1724 conf->image_copyright = NULL;
1731 * Merge per-directory CHXJ configurations
1734 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1736 mod_chxj_config *base;
1737 mod_chxj_config *add;
1738 mod_chxj_config *mrg;
1740 base = (mod_chxj_config *)basev;
1741 add = (mod_chxj_config *)addv;
1742 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1744 mrg->device_data_file = NULL;
1745 mrg->devices = NULL;
1746 mrg->emoji_data_file = NULL;
1747 mrg->image = CHXJ_IMG_NONE;
1748 mrg->image_cache_dir = NULL;
1749 mrg->image_copyright = NULL;
1750 mrg->image_cache_limit = 0;
1752 mrg->emoji_tail = NULL;
1753 mrg->new_line_type = NLTYPE_NIL;
1754 mrg->forward_url_base = NULL;
1755 mrg->forward_server_ip = NULL;
1756 mrg->allowed_cookie_domain = NULL;
1757 mrg->post_log = NULL;
1759 mrg->dir = apr_pstrdup(p, add->dir);
1761 if (! add->device_data_file) {
1762 mrg->devices = base->devices;
1763 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1766 mrg->devices = add->devices;
1767 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1770 if (! add->emoji_data_file) {
1771 mrg->emoji = base->emoji;
1772 mrg->emoji_tail = base->emoji_tail;
1773 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1776 mrg->emoji = add->emoji;
1777 mrg->emoji_tail = add->emoji_tail;
1778 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1781 if (add->image == CHXJ_IMG_NONE) {
1782 mrg->image = base->image;
1785 mrg->image = add->image;
1788 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1789 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1792 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1795 if (add->image_cache_limit) {
1796 mrg->image_cache_limit = add->image_cache_limit;
1799 mrg->image_cache_limit = base->image_cache_limit;
1802 if (add->image_copyright)
1803 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1805 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1807 if (add->server_side_encoding) {
1808 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1811 if (base->server_side_encoding) {
1812 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1815 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1818 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1820 if (add->cookie_db_dir) {
1821 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1824 if (base->cookie_db_dir) {
1825 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1828 mrg->cookie_db_dir = NULL;
1831 if (add->cookie_timeout) {
1832 mrg->cookie_timeout = add->cookie_timeout;
1835 if (base->cookie_db_dir) {
1836 mrg->cookie_timeout = base->cookie_timeout;
1839 mrg->cookie_timeout = 0;
1842 #if defined(USE_MYSQL_COOKIE)
1843 if (add->mysql.host) {
1844 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1846 else if (base->mysql.host) {
1847 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1850 mrg->mysql.host = NULL;
1852 if (add->mysql.port) {
1853 mrg->mysql.port = add->mysql.port;
1855 else if (base->mysql.port) {
1856 mrg->mysql.port = base->mysql.port;
1859 mrg->mysql.port = 0;
1862 if (add->mysql.database) {
1863 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1865 else if (base->mysql.database) {
1866 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1869 mrg->mysql.database = NULL;
1872 if (add->mysql.username) {
1873 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1875 else if (base->mysql.username) {
1876 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1879 mrg->mysql.username = NULL;
1882 if (add->mysql.password) {
1883 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1885 else if (base->mysql.password) {
1886 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1889 mrg->mysql.password = NULL;
1892 if (add->mysql.tablename) {
1893 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1895 else if (base->mysql.tablename) {
1896 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1899 mrg->mysql.tablename = NULL;
1902 if (add->mysql.socket_path) {
1903 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1905 else if (base->mysql.socket_path) {
1906 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1909 mrg->mysql.socket_path = NULL;
1912 if (add->mysql.charset) {
1913 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1915 else if (base->mysql.charset) {
1916 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1919 mrg->mysql.charset = NULL;
1922 #if defined(USE_MEMCACHE_COOKIE)
1923 if (add->memcache.host) {
1924 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1926 else if (base->memcache.host) {
1927 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1930 mrg->memcache.host = NULL;
1932 if (add->memcache.port) {
1933 mrg->memcache.port = add->memcache.port;
1935 else if (base->memcache.port) {
1936 mrg->memcache.port = base->memcache.port;
1939 mrg->memcache.port = 0;
1942 if (add->cookie_store_type) {
1943 mrg->cookie_store_type = add->cookie_store_type;
1945 else if (base->cookie_store_type) {
1946 mrg->cookie_store_type = base->cookie_store_type;
1949 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1951 if (add->cookie_lazy_mode) {
1952 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1954 else if (base->cookie_lazy_mode) {
1955 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1958 mrg->cookie_lazy_mode = 0;
1960 if (add->new_line_type) {
1961 mrg->new_line_type = add->new_line_type;
1963 else if (base->new_line_type) {
1964 mrg->new_line_type = base->new_line_type;
1967 mrg->new_line_type = NLTYPE_NIL;
1970 if (add->forward_url_base) {
1971 mrg->forward_url_base = add->forward_url_base;
1973 else if (base->forward_url_base) {
1974 mrg->forward_url_base = base->forward_url_base;
1977 if (add->allowed_cookie_domain) {
1978 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
1981 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
1983 if (add->post_log) {
1984 mrg->post_log = add->post_log;
1987 mrg->post_log = base->post_log;
1994 chxj_command_parse_take5(
2007 for (;*strp == ' '||*strp == '\t'; strp++) ;
2017 for (; *strp != '\0'; strp++) {
2018 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2019 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2024 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2025 || (isquoted && *strp == '"'))
2039 for (;*strp == ' '||*strp == '\t'; strp++) ;
2048 for (; *strp != '\0'; strp++) {
2049 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2050 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2055 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2056 || (isquoted && *strp == '"'))
2069 for (;*strp == ' '||*strp == '\t'; strp++);
2077 for (; *strp != '\0'; strp++) {
2078 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2079 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2084 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2085 || (isquoted && *strp == '"'))
2097 for (;*strp == ' '||*strp == '\t'; strp++);
2105 for (; *strp != '\0'; strp++) {
2106 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2107 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2112 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2113 || (isquoted && *strp == '"'))
2124 for (;*strp == ' '||*strp == '\t'; strp++);
2132 for (; *strp != '\0'; strp++) {
2133 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2134 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2139 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2140 || (isquoted && *strp == '"'))
2150 * The device definition file is loaded.
2152 * @param arg [i] The name of the device definition file is specified.
2153 * @param mconfig [i/o] The pointer to a set structure is specified.
2157 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2159 mod_chxj_config *conf;
2164 if (strlen(arg) > 256)
2165 return "mod_chxj: device data filename too long.";
2167 conf = (mod_chxj_config *)mconfig;
2168 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2170 qs_init_malloc(&doc);
2171 qs_init_root_node(&doc);
2173 qs_parse_file((Doc *)&doc, (const char *)arg);
2174 chxj_load_device_data(&doc,parms->pool, conf);
2175 qs_all_free(&doc, QX_LOGMARK);
2182 * Device definition information is loaded.
2185 * @param arg [i] The name of the device definition file is specified.
2186 * @param mconfig [i/o] The pointer to a set structure is specified.
2190 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2192 mod_chxj_config *conf;
2199 if (strlen(arg) > 256)
2200 return "mod_chxj: emoji data filename too long.";
2202 conf = (mod_chxj_config *)mconfig;
2203 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2204 qs_init_malloc(&doc);
2205 qs_init_root_node(&doc);
2207 qs_parse_file((Doc *)&doc, (const char *)arg);
2209 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2211 qs_all_free(&doc, QX_LOGMARK);
2219 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2221 mod_chxj_config *conf;
2226 if (strlen(arg) > 256)
2227 return "image uri is too long.";
2229 conf = (mod_chxj_config*)mconfig;
2230 if (strcasecmp("ON", arg) == 0) {
2231 conf->image = CHXJ_IMG_ON;
2234 conf->image = CHXJ_IMG_OFF;
2242 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2244 mod_chxj_config *conf;
2249 if (strlen(arg) > 256)
2250 return "cache dir name is too long.";
2252 conf = (mod_chxj_config *)mconfig;
2253 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2260 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2262 mod_chxj_config *conf;
2267 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2268 return "cache size is too long.";
2270 conf = (mod_chxj_config *)mconfig;
2273 * I use strtol function because strtoul is not portable function.
2275 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2278 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2280 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2289 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2291 mod_chxj_config *conf;
2296 if (strlen(arg) > 256)
2297 return "Copyright Flag is too long.";
2299 conf = (mod_chxj_config *)mconfig;
2300 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2307 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2311 mod_chxj_config *dconf;
2312 chxjconvrule_entry *newrule;
2322 dconf = (mod_chxj_config *)mconfig;
2324 if (strlen(arg) > 4096)
2325 return "mod_chxj: ChxjConvertRule: is too long.";
2327 dconf = (mod_chxj_config *)mconfig;
2328 if (dconf->convrules == NULL)
2329 dconf->convrules = apr_array_make(cmd->pool,
2331 sizeof(chxjconvrule_entry));
2333 newrule = apr_array_push(dconf->convrules);
2336 newrule->action = 0;
2338 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2339 return "ChxjConvertRule: bad argument line";
2341 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2345 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2351 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2352 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2355 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2356 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2359 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2360 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2363 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2364 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2370 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2371 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2373 else if (strcasecmp(CONVRULE_COOKIE_OFF_CMD, action) == 0) {
2374 newrule->action &= (0xffffffff ^ CONVRULE_COOKIE_ON_BIT);
2376 else if (strcasecmp(CONVRULE_CSS_ON_CMD, action) == 0) {
2377 newrule->action |= CONVRULE_CSS_ON_BIT;
2379 else if (strcasecmp(CONVRULE_CSS_OFF_CMD, action) == 0) {
2380 newrule->action &= (0xffffffff ^ CONVRULE_CSS_ON_BIT);
2386 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2387 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2393 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2394 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2400 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2401 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2407 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2408 newrule->action |= CONVRULE_Z2H_ON_BIT;
2411 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2412 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2415 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2416 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2419 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2420 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2423 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2424 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2427 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2428 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2431 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2432 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2435 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2436 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2447 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2451 mode = AP_REG_EXTENDED;
2452 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2453 return "RewriteRule: cannot compile regular expression ";
2455 newrule->regexp = regexp;
2457 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2459 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2461 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2463 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2464 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2466 newrule->user_agent = NULL;
2468 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2480 mod_chxj_config *dconf;
2483 if (strlen(arg) > 4096)
2484 return "mod_chxj: ChxjCookieDir is too long.";
2486 dconf = (mod_chxj_config *)mconfig;
2488 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2495 cmd_set_cookie_timeout(
2496 cmd_parms *UNUSED(cmd),
2500 mod_chxj_config *dconf;
2502 if (strlen(arg) > 4096)
2503 return "mod_chxj: ChxjCookieTimeout is too long.";
2505 if (chxj_chk_numeric(arg) != 0)
2506 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2508 dconf = (mod_chxj_config *)mconfig;
2510 dconf->cookie_timeout = atoi(arg);
2516 #if defined(USE_MYSQL_COOKIE)
2518 cmd_set_cookie_mysql_database(
2523 mod_chxj_config *dconf;
2525 if (strlen(arg) > 255)
2526 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2528 dconf = (mod_chxj_config *)mconfig;
2530 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2537 cmd_set_cookie_mysql_username(
2542 mod_chxj_config *dconf;
2544 if (strlen(arg) > 255)
2545 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2547 dconf = (mod_chxj_config *)mconfig;
2549 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2556 cmd_set_cookie_mysql_password(
2561 mod_chxj_config *dconf;
2563 if (strlen(arg) > 255)
2564 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2566 dconf = (mod_chxj_config *)mconfig;
2568 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2575 cmd_set_cookie_mysql_table_name(
2580 mod_chxj_config *dconf;
2582 if (strlen(arg) > 255)
2583 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2585 dconf = (mod_chxj_config *)mconfig;
2587 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2593 cmd_set_cookie_mysql_port(
2594 cmd_parms *UNUSED(cmd),
2598 mod_chxj_config *dconf;
2600 if (strlen(arg) > 255)
2601 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2603 dconf = (mod_chxj_config *)mconfig;
2605 if (chxj_chk_numeric(arg) != 0)
2606 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2608 dconf = (mod_chxj_config *)mconfig;
2610 dconf->mysql.port = chxj_atoi(arg);
2617 cmd_set_cookie_mysql_host(
2622 mod_chxj_config *dconf;
2624 if (strlen(arg) > 255)
2625 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2627 dconf = (mod_chxj_config *)mconfig;
2629 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2636 cmd_set_cookie_mysql_socket_path(
2641 mod_chxj_config *dconf;
2643 if (strlen(arg) > 4096)
2644 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2646 dconf = (mod_chxj_config *)mconfig;
2648 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2655 cmd_set_cookie_mysql_charset(
2660 mod_chxj_config *dconf;
2662 if (strlen(arg) > 255)
2663 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2665 dconf = (mod_chxj_config *)mconfig;
2667 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2672 #if defined(USE_MEMCACHE_COOKIE)
2674 cmd_set_cookie_memcache_port(
2675 cmd_parms *UNUSED(cmd),
2679 mod_chxj_config *dconf;
2681 if (strlen(arg) > 255)
2682 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2684 dconf = (mod_chxj_config *)mconfig;
2686 if (chxj_chk_numeric(arg) != 0)
2687 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2689 dconf = (mod_chxj_config *)mconfig;
2691 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2698 cmd_set_cookie_memcache_host(
2703 mod_chxj_config *dconf;
2705 if (strlen(arg) > 255)
2706 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2708 dconf = (mod_chxj_config *)mconfig;
2710 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2717 cmd_set_cookie_lazy_mode(
2718 cmd_parms *UNUSED(cmd),
2722 mod_chxj_config *dconf;
2724 if (strlen(arg) > 255)
2725 return "mod_chxj: ChxjCookieLazyMode is too long.";
2727 dconf = (mod_chxj_config *)mconfig;
2729 if (strcasecmp("TRUE",arg) == 0) {
2730 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2733 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2740 cmd_set_cookie_store_type(
2741 cmd_parms *UNUSED(cmd),
2745 mod_chxj_config *dconf;
2747 if (strlen(arg) > 255)
2748 return "mod_chxj: ChxjCookieStoreType is too long.";
2750 dconf = (mod_chxj_config *)mconfig;
2752 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2753 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2755 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2756 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2758 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2759 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2762 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2769 cmd_set_forward_url_base(
2774 mod_chxj_config *dconf;
2776 if (strlen(arg) > 255)
2777 return "mod_chxj: ChxjForwardUrlBase is too long.";
2779 dconf = (mod_chxj_config *)mconfig;
2781 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2787 cmd_set_forward_server_ip(
2792 mod_chxj_config *dconf;
2794 if (strlen(arg) > 255)
2795 return "mod_chxj: ChxjForwardServerIp is too long.";
2797 dconf = (mod_chxj_config *)mconfig;
2799 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2805 cmd_allowed_cookie_domain(
2810 mod_chxj_config *dconf;
2812 if (strlen(arg) > 255)
2813 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2815 dconf = (mod_chxj_config *)mconfig;
2817 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2823 cmd_set_new_line_type(
2824 cmd_parms *UNUSED(cmd),
2828 mod_chxj_config *dconf;
2829 if (strlen(arg) > 255)
2830 return "mod_chxj: ChxjNewLineType is too long.";
2832 dconf = (mod_chxj_config *)mconfig;
2834 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2835 dconf->new_line_type = NLTYPE_CRLF;
2837 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2838 dconf->new_line_type = NLTYPE_LF;
2840 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2841 dconf->new_line_type = NLTYPE_CR;
2843 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2844 dconf->new_line_type = NLTYPE_NONE;
2847 return "mod_chxj: invalid value (ChxjNewLineType)";
2858 mod_chxj_config *dconf;
2859 if (strlen(arg) > 255)
2860 return "mod_chxj: ChxjPostLogEnv is too long.";
2862 dconf = (mod_chxj_config *)mconfig;
2864 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2870 static const command_rec cmds[] = {
2872 "ChxjLoadDeviceData",
2873 cmd_load_device_data,
2876 "Load Device Data"),
2878 "ChxjLoadEmojiData",
2879 cmd_load_emoji_data,
2885 cmd_set_image_engine,
2888 "Convert Target URI"),
2890 "ChxjImageCacheDir",
2891 cmd_set_image_cache_dir,
2894 "Image Cache Directory"),
2896 "ChxjImageCacheLimit",
2897 cmd_set_image_cache_limit,
2900 "Image Cache Limit"),
2902 "ChxjImageCopyright",
2903 cmd_set_image_copyright,
2912 "an URL-applied regexp-pattern and a substitution URL"),
2918 "save cookie.db directory."),
2920 "ChxjCookieTimeout",
2921 cmd_set_cookie_timeout,
2924 "The compulsion time-out time of the cookie is specified. "),
2926 "ChxjCookieStoreType",
2927 cmd_set_cookie_store_type,
2930 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2932 "ChxjCookieLazyMode",
2933 cmd_set_cookie_lazy_mode,
2936 "OneTimeID is negligently done. (TRUE/FALSE)"),
2937 #if defined(USE_MYSQL_COOKIE)
2939 "ChxjCookieMysqlHost",
2940 cmd_set_cookie_mysql_host,
2943 "The MySQL database host used by saving Cookie"),
2945 "ChxjCookieMysqlPort",
2946 cmd_set_cookie_mysql_port,
2949 "The MySQL database port used by saving Cookie"),
2951 "ChxjCookieMysqlDatabase",
2952 cmd_set_cookie_mysql_database,
2955 "The MySQL database name used by saving Cookie"),
2957 "ChxjCookieMysqlUsername",
2958 cmd_set_cookie_mysql_username,
2961 "The MySQL username used by saving Cookie"),
2963 "ChxjCookieMysqlPassword",
2964 cmd_set_cookie_mysql_password,
2967 "The MySQL password used by saving Cookie"),
2969 "ChxjCookieMysqlTableName",
2970 cmd_set_cookie_mysql_table_name,
2973 "The MySQL table name used by saving Cookie"),
2975 "ChxjCookieMysqlSocketPath",
2976 cmd_set_cookie_mysql_socket_path,
2979 "The MySQL socket path used by saving Cookie"),
2981 "ChxjCookieMysqlCharset",
2982 cmd_set_cookie_mysql_charset,
2985 "The MySQL charset used by saving Cookie"),
2987 #if defined(USE_MEMCACHE_COOKIE)
2989 "ChxjCookieMemcacheHost",
2990 cmd_set_cookie_memcache_host,
2993 "The Memcached host used by saving Cookie"),
2995 "ChxjCookieMemcachePort",
2996 cmd_set_cookie_memcache_port,
2999 "The Memcached port used by saving Cookie"),
3003 cmd_set_new_line_type,
3006 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3008 "ChxjForwardUrlBase",
3009 cmd_set_forward_url_base,
3012 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3014 "ChxjForwardServerIp",
3015 cmd_set_forward_server_ip,
3018 "The forward server ip(default: this server ip)"),
3020 "ChxjAllowedCookieDomain",
3021 cmd_allowed_cookie_domain,
3024 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3030 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3035 /*----------------------------------------------------------------------------*/
3036 /* Dispatch list for API hooks */
3037 /*----------------------------------------------------------------------------*/
3038 module AP_MODULE_DECLARE_DATA chxj_module = {
3039 STANDARD20_MODULE_STUFF,
3040 chxj_create_per_dir_config, /* create per-dir config structures */
3041 chxj_merge_per_dir_config, /* merge per-dir config structures */
3042 chxj_config_server_create, /* create per-server config structures */
3043 NULL, /* merge per-server config structures */
3044 cmds, /* table of config file commands */
3045 chxj_register_hooks /* register hooks */