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);
204 switch(spec->html_spec_type) {
205 case CHXJ_SPEC_Chtml_1_0:
206 case CHXJ_SPEC_Chtml_2_0:
207 case CHXJ_SPEC_Chtml_3_0:
208 case CHXJ_SPEC_Chtml_4_0:
209 case CHXJ_SPEC_Chtml_5_0:
210 case CHXJ_SPEC_Chtml_6_0:
211 case CHXJ_SPEC_Chtml_7_0:
212 case CHXJ_SPEC_XHtml_Mobile_1_0:
214 case CHXJ_SPEC_Jhtml:
215 case CHXJ_SPEC_Jxhtml:
216 entryp = chxj_apply_convrule(r, dconf->convrules);
218 DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int)(apr_size_t)r);
221 if (!(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
222 DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int)(apr_size_t)r);
225 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
226 DBG(r, "REQ[%X] end chxj_headers_fixup() (emoji only)", (unsigned int)(apr_size_t)r);
230 apr_table_setn(r->headers_in,
231 CHXJ_HTTP_USER_AGENT,
234 if (entryp->user_agent)
235 apr_table_setn(r->headers_in,
239 * this clear process must do before chxj_convert_input_header function.
241 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
242 if (r->method_number == M_POST) {
243 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
244 s_clear_cookie_header(r, spec);
248 s_clear_cookie_header(r, spec);
252 chxj_convert_input_header(r,entryp, spec);
257 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);
263 if (r->method_number == M_POST) {
264 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
265 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);
266 r->proxyreq = PROXYREQ_NONE;
267 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
270 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
272 if (dconf->post_log) {
273 apr_table_setn(r->subprocess_env, dconf->post_log, dconf->post_log);
276 apr_table_setn(r->subprocess_env, "chxj-post-log", "chxj-post-log");
278 apr_sockaddr_t *address = NULL;
279 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
280 if (rv != APR_SUCCESS) {
282 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));
283 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
287 if (dconf->forward_server_ip) {
288 addr = dconf->forward_server_ip;
291 apr_sockaddr_ip_get(&addr, address);
293 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);
294 if (strcmp(addr, r->connection->remote_ip) == 0) {
295 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
297 if (! apr_table_get(r->headers_in, "Content-Length")) {
298 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
300 apr_table_set(r->headers_in, "Content-Length", contentLength);
307 chxj_add_device_env(r, spec);
309 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
316 s_clear_cookie_header(request_rec *r, device_table *spec)
318 DBG(r, "REQ[%X] start s_clear_cookie_header()", TO_ADDR(r));
319 switch(spec->html_spec_type) {
320 case CHXJ_SPEC_Chtml_1_0:
321 case CHXJ_SPEC_Chtml_2_0:
322 case CHXJ_SPEC_Chtml_3_0:
323 case CHXJ_SPEC_Chtml_4_0:
324 case CHXJ_SPEC_Chtml_5_0:
325 case CHXJ_SPEC_Chtml_6_0:
326 case CHXJ_SPEC_Chtml_7_0:
327 case CHXJ_SPEC_XHtml_Mobile_1_0:
328 case CHXJ_SPEC_Jhtml:
329 apr_table_unset(r->headers_in, "Cookie");
334 DBG(r, "REQ[%X] end s_clear_cookie_header()", TO_ADDR(r));
339 * It converts it from CHTML into XXML corresponding to each model.
342 * @param src [i] It is former HTML character string.
343 * @param len [i/o] It is length of former HTML character string.
346 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
352 mod_chxj_config *dconf;
353 chxjconvrule_entry *entryp;
355 DBG(r,"REQ[%X] start of chxj_convert()", (unsigned int)(apr_size_t)r);
357 chxj_dump_string(r, APLOG_MARK, "INPUT Data", *src, *len);
359 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
361 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
364 entryp = chxj_apply_convrule(r, dconf->convrules);
366 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
367 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
372 /*------------------------------------------------------------------------*/
373 /* get UserAgent from http header */
374 /*------------------------------------------------------------------------*/
375 if (entryp->user_agent)
376 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
378 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
380 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
381 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
384 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
385 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
386 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
387 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
391 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
393 spec = chxj_specified_device(r, user_agent);
400 if (entryp->action & CONVRULE_COOKIE_ON_BIT
401 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
402 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
403 switch(spec->html_spec_type) {
404 case CHXJ_SPEC_Chtml_1_0:
405 case CHXJ_SPEC_Chtml_2_0:
406 case CHXJ_SPEC_Chtml_3_0:
407 case CHXJ_SPEC_Chtml_4_0:
408 case CHXJ_SPEC_Chtml_5_0:
409 case CHXJ_SPEC_Chtml_6_0:
410 case CHXJ_SPEC_Chtml_7_0:
411 case CHXJ_SPEC_XHtml_Mobile_1_0:
412 case CHXJ_SPEC_Jhtml:
413 cookie = chxj_save_cookie(r);
420 if (!r->header_only) {
423 if (convert_routine[spec->html_spec_type].encoder)
424 tmp = convert_routine[spec->html_spec_type].encoder(r,
428 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
430 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
433 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
435 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
436 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
441 dst = apr_palloc(r->pool, 1);
446 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
449 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
450 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
451 if (convert_routine[spec->html_spec_type].converter) {
453 dst = convert_routine[spec->html_spec_type].converter(r,
461 dst = convert_routine[spec->html_spec_type].converter(r,
471 ap_set_content_length(r, *len);
474 dst = apr_psprintf(r->pool, "\n");
483 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
490 * convert GUID parameter.
494 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
496 DBG(r, "REQ[%X] start s_convert_guid_parameter() param:[%s]", (unsigned int)(apr_size_t)r, param);
497 if (strcasecmp(param, "guid") == 0) {
498 switch(spec->html_spec_type) {
499 case CHXJ_SPEC_XHtml_Mobile_1_0:
501 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
503 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
507 case CHXJ_SPEC_Jhtml:
508 case CHXJ_SPEC_Jxhtml:
510 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
512 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
520 DBG(r, "REQ[%X] end s_convert_guid_parameter()", (unsigned int)(apr_size_t)r);
524 * It converts it from HEADER.
529 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
541 cookie_t *cookie = NULL;
542 int no_update_flag = 0;
544 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
547 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
548 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
551 urilen = strlen(r->args);
553 result = qs_alloc_zero_byte_string(r->pool);
555 buff_pre = apr_pstrdup(r->pool, r->args);
560 pair = apr_strtok(buff_pre, "&", &pstate);
566 pair_sv = apr_pstrdup(r->pool, pair);
568 name = apr_strtok(pair, "=", &vstate);
569 value = apr_strtok(NULL, "=", &vstate);
570 if (! name) continue;
571 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
572 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
577 buff = apr_pstrdup(r->pool, r->args);
578 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
587 pair = apr_strtok(buff, "&", &pstate);
593 pair_sv = apr_pstrdup(r->pool, pair);
595 name = apr_strtok(pair, "=", &vstate);
596 value = apr_strtok(NULL, "=", &vstate);
597 if (! name) continue;
598 name = chxj_safe_to_jreserved_tag(r, name);
599 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
600 if (strlen(result) != 0)
601 result = apr_pstrcat(r->pool, result, "&", NULL);
603 if (strcasecmp(entryp->encoding, "NONE") != 0) {
608 if (value && *value != 0) {
609 value = chxj_url_decode(r->pool, value);
610 dlen = strlen(value);
611 DBG(r, "************ before encoding[%s]", value);
613 dvalue = chxj_rencoding(r, value, &dlen);
614 dvalue = chxj_url_encode(r->pool, dvalue);
616 DBG(r, "************ after encoding[%s]", dvalue);
622 if (name && *name != 0) {
623 name = chxj_url_decode(r->pool, name);
625 dname = chxj_rencoding(r, name, &dlen);
626 dname = chxj_url_encode(r->pool, dname);
631 s_convert_guid_parameter_to_header(r, dname, spec);
632 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
635 s_convert_guid_parameter_to_header(r, name, spec);
636 if (strcmp(name, pair_sv) != 0) {
637 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
640 result = apr_pstrcat(r->pool, result, name, NULL);
645 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
646 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
647 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
651 if (strlen(value) == 0)
654 if (strlen(result) != 0)
655 result = apr_pstrcat(r->pool, result, "&", NULL);
657 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
660 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
662 apr_table_unset(r->headers_in, "Cookie");
663 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
664 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
665 cookie_lock_t *lock = chxj_cookie_lock(r);
666 cookie = chxj_load_cookie(r, value);
667 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
668 if (! no_update_flag && cookie) {
669 cookie = chxj_update_cookie(r, cookie);
671 chxj_cookie_unlock(r, lock);
673 if (cookie && cookie->cookie_id) {
674 if (strlen(result) != 0)
675 result = apr_pstrcat(r->pool, result, "&", NULL);
676 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
680 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
681 if (strlen(result) != 0)
682 result = apr_pstrcat(r->pool, result, "&", NULL);
683 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
686 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
687 if (! no_update_flag) {
688 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
692 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
693 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
699 * It converts it from POSTDATA .
702 * @param src [i] It is POSTDATA character string.
703 * @param len [i/o] It is length of former HTML character string.
710 chxjconvrule_entry *entryp,
720 cookie_t *cookie = NULL;
722 int no_update_flag = 0;
726 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
729 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
730 return apr_pstrdup(r->pool, "");
733 apr_pool_create(&pool, r->pool);
735 s = apr_pstrdup(pool, *src);
737 buff_pre = apr_pstrdup(pool, *src);
739 result = qs_alloc_zero_byte_string(pool);
741 chxj_dump_string(r, APLOG_MARK, "BEFORE input convert source", s, ilen);
746 pair = apr_strtok(buff_pre, "&", &pstate);
752 pair_sv = apr_pstrdup(pool, pair);
754 name = apr_strtok(pair, "=", &vstate);
755 value = apr_strtok(NULL, "=", &vstate);
756 if (! name) continue;
757 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
758 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
768 pair = apr_strtok(s, "&", &pstate);
773 name = apr_strtok(pair, "=", &vstate);
774 value = apr_strtok(NULL, "=", &vstate);
775 if (! name) continue;
776 name = chxj_safe_to_jreserved_tag(r, name);
778 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
779 if (strlen(result) != 0)
780 result = apr_pstrcat(pool, result, "&", NULL);
782 if (strcasecmp(entryp->encoding, "NONE") != 0) {
787 if (value && *value != 0) {
788 value = chxj_url_decode(pool, value);
789 dlen = strlen(value);
790 dvalue = chxj_rencoding(r, value, &dlen);
791 dvalue = chxj_url_encode(pool, dvalue);
797 if (name && *name != 0) {
798 name = chxj_url_decode(pool, name);
800 dname = chxj_rencoding(r, name, &dlen);
801 dname = chxj_url_encode(pool, dname);
808 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
811 result = apr_pstrcat(pool, result, name, "=", value, NULL);
815 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
816 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
817 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
821 if (strlen(value) == 0)
824 if (strlen(result) != 0)
825 result = apr_pstrcat(pool, result, "&", NULL);
827 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
831 dlen = strlen(value);
832 value = chxj_url_decode(pool, value);
833 dvalue = chxj_rencoding(r, value, &dlen);
834 dvalue = chxj_url_encode(pool,dvalue);
835 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
839 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
843 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
845 apr_table_unset(r->headers_in, "Cookie");
846 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
847 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
848 cookie_lock_t *lock = chxj_cookie_lock(r);
849 cookie = chxj_load_cookie(r, value);
850 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
851 if (! no_update_flag && cookie) {
852 cookie = chxj_update_cookie(r, cookie);
854 chxj_cookie_unlock(r, lock);
856 if (cookie && cookie->cookie_id) {
857 if (strlen(result) != 0)
858 result = apr_pstrcat(pool, result, "&", NULL);
859 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
863 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
864 if (strlen(result) != 0)
865 result = apr_pstrcat(pool, result, "&", NULL);
866 result = apr_pstrcat(pool, result, name, "=", value, NULL);
869 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
872 dlen = strlen(value);
874 value = chxj_url_decode(pool, value);
875 dvalue = chxj_rencoding(r, value, &dlen);
876 dvalue = chxj_url_encode(pool,dvalue);
877 if (r->args && strlen(r->args) > 0) {
878 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
881 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
883 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
887 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
890 dlen = strlen(value);
892 value = chxj_url_decode(pool, value);
893 dvalue = chxj_rencoding(r, value, &dlen);
894 dvalue = chxj_url_encode(pool,dvalue);
895 if (r->args && strlen(r->args) > 0) {
896 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
899 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
901 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
904 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
906 *len = strlen(result);
907 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
908 if (! no_update_flag) {
909 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
912 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
913 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
920 * The received data is returned to the filter.
922 * @param f [i/o] It is a filter.
923 * @param data [i] It is data returned to the filter.
924 * @param len [i] It is length of the data returned to the filter.
927 pass_data_to_filter(ap_filter_t *f, const char *data,
930 request_rec *r = f->r;
931 conn_rec *c = r->connection;
933 apr_bucket_brigade *bb;
936 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
938 chxj_header_inf_clear(r);
940 bb = apr_brigade_create(r->pool, c->bucket_alloc);
941 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
943 APR_BRIGADE_INSERT_TAIL(bb, b);
944 b = apr_bucket_eos_create(f->c->bucket_alloc);
945 APR_BRIGADE_INSERT_TAIL(bb, b);
947 rv = ap_pass_brigade(f->next, bb);
948 if (rv != APR_SUCCESS) {
949 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
953 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
959 * Add No Cache Header
962 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
964 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
965 apr_table_unset(r->headers_out, "Pragma");
966 apr_table_unset(r->err_headers_out, "Pragma");
967 apr_table_unset(r->headers_out, "Expires");
968 apr_table_unset(r->err_headers_out, "Expires");
969 apr_table_unset(r->headers_out, "Cache-Control");
970 apr_table_unset(r->err_headers_out, "Cache-Control");
972 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
973 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
974 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
980 * It is the main loop of the output filter.
982 * @param f [i/o] It is a filter.
986 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
992 char *user_agent = NULL;
994 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
995 cookie_t *cookie = NULL;
996 mod_chxj_config *dconf;
997 chxjconvrule_entry *entryp = NULL;
998 device_table *spec = NULL;
1002 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1005 apr_pool_create(&pool, r->pool);
1007 entryp = ctx->entryp;
1009 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1012 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1013 DBG(r, "REQ[%X] found Location header", TO_ADDR(r));
1014 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1015 cookie_lock_t *lock = NULL;
1016 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", TO_ADDR(r));
1017 switch(spec->html_spec_type) {
1018 case CHXJ_SPEC_Chtml_1_0:
1019 case CHXJ_SPEC_Chtml_2_0:
1020 case CHXJ_SPEC_Chtml_3_0:
1021 case CHXJ_SPEC_Chtml_4_0:
1022 case CHXJ_SPEC_Chtml_5_0:
1023 case CHXJ_SPEC_Chtml_6_0:
1024 case CHXJ_SPEC_Chtml_7_0:
1025 case CHXJ_SPEC_XHtml_Mobile_1_0:
1026 case CHXJ_SPEC_Jhtml:
1027 lock = chxj_cookie_lock(r);
1028 cookie = chxj_save_cookie(r);
1029 s_add_cookie_id_if_has_location_header(r, cookie);
1030 chxj_cookie_unlock(r, lock);
1036 if (! ap_is_HTTP_REDIRECT(r->status)) {
1037 r->status = HTTP_MOVED_TEMPORARILY;
1039 s_add_no_cache_headers(r, entryp);
1040 /* must not send body. */
1041 rv = pass_data_to_filter(f, "", 0);
1042 DBG(f->r, "REQ[%X] end of chxj_output_filter()", TO_ADDR(r));
1047 if (r->content_type) {
1048 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1049 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1050 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1051 && ! (dconf->image == CHXJ_IMG_ON
1052 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1053 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1054 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1055 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1056 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1057 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1058 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1059 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1060 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1061 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1062 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1064 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1065 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1066 cookie_lock_t *lock = NULL;
1067 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1068 switch(spec->html_spec_type) {
1069 case CHXJ_SPEC_Chtml_1_0:
1070 case CHXJ_SPEC_Chtml_2_0:
1071 case CHXJ_SPEC_Chtml_3_0:
1072 case CHXJ_SPEC_Chtml_4_0:
1073 case CHXJ_SPEC_Chtml_5_0:
1074 case CHXJ_SPEC_Chtml_6_0:
1075 case CHXJ_SPEC_Chtml_7_0:
1076 case CHXJ_SPEC_XHtml_Mobile_1_0:
1077 case CHXJ_SPEC_Jhtml:
1078 lock = chxj_cookie_lock(r);
1079 cookie = chxj_save_cookie(r);
1080 s_add_cookie_id_if_has_location_header(r, cookie);
1081 chxj_cookie_unlock(r, lock);
1087 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1088 if (! ap_is_HTTP_REDIRECT(r->status)) {
1089 r->status = HTTP_MOVED_TEMPORARILY;
1092 s_add_no_cache_headers(r, entryp);
1093 ap_pass_brigade(f->next, bb);
1094 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1099 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1100 ap_pass_brigade(f->next, bb);
1101 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1106 for (b = APR_BRIGADE_FIRST(bb);
1107 b != APR_BRIGADE_SENTINEL(bb);
1108 b = APR_BUCKET_NEXT(b)) {
1110 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1111 chxj_dump_string(r, APLOG_MARK, "READ Data", data, len);
1113 /*--------------------------------------------------------------------*/
1115 /*--------------------------------------------------------------------*/
1117 DBG(r, "append data start");
1118 ctx = (mod_chxj_ctx *)f->ctx;
1121 tmp = apr_palloc(r->pool, ctx->len);
1122 memcpy(tmp, ctx->buffer, ctx->len);
1124 ctx->buffer = apr_palloc(pool, ctx->len + len);
1126 memcpy(ctx->buffer, tmp, ctx->len);
1127 memcpy(&ctx->buffer[ctx->len], data, len);
1131 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1134 if (APR_BUCKET_IS_EOS(b)) {
1136 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1137 /*----------------------------------------------------------------------*/
1139 /*----------------------------------------------------------------------*/
1141 cookie_lock_t *lock = NULL;
1142 ctx = (mod_chxj_ctx *)f->ctx;
1144 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1145 lock = chxj_cookie_lock(r);
1147 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1149 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1150 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1151 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1156 tmp = apr_palloc(pool, ctx->len + 1);
1158 memset(tmp, 0, ctx->len + 1);
1159 memcpy(tmp, ctx->buffer, ctx->len);
1161 ctx->buffer = chxj_convert(r,
1162 (const char **)&tmp,
1163 (apr_size_t *)&ctx->len,
1165 user_agent, &cookie);
1169 ctx->buffer = apr_psprintf(r->pool, "\n");
1171 ctx->buffer = chxj_convert(r,
1172 (const char **)&ctx->buffer,
1173 (apr_size_t *)&ctx->len,
1175 user_agent, &cookie);
1180 && *(char *)r->content_type == 't'
1181 && strncmp(r->content_type, "text/xml", 8) == 0) {
1182 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1190 memset(&doc, 0, sizeof(Doc));
1191 memset(&qrcode, 0, sizeof(qr_code_t));
1193 doc.parse_mode = PARSE_MODE_CHTML;
1197 qs_init_malloc(&doc);
1199 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1202 for (child = qs_get_child_node(&doc,root);
1204 child = qs_get_next_node(&doc,child)) {
1205 char *name = qs_get_node_name(&doc,child);
1206 if (strcasecmp("qrcode",name) == 0) {
1211 qs_all_free(&doc,QX_LOGMARK);
1213 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1214 chxj_qrcode_node_to_qrcode(&qrcode, root);
1215 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1217 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1218 chxj_cookie_unlock(r, lock);
1219 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1222 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1226 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1228 && ( *r->content_type == 'i' || *r->content_type == 'I')
1229 && dconf->image == CHXJ_IMG_ON
1230 && strncasecmp("image/", r->content_type, 6) == 0
1231 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1232 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1233 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1234 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1235 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1236 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1237 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1238 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1239 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1240 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1244 tmp = apr_palloc(pool, ctx->len + 1);
1246 memset(tmp, 0, ctx->len + 1);
1247 memcpy(tmp, ctx->buffer, ctx->len);
1249 chxj_convert_image(r,
1250 (const char **)&tmp,
1251 (apr_size_t *)&ctx->len);
1252 if (ctx->buffer == NULL) {
1258 apr_table_unset(r->headers_out, "Content-Length");
1259 apr_table_unset(r->err_headers_out, "Content-Length");
1260 ap_set_content_length(r, (apr_off_t)ctx->len);
1264 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1265 s_add_cookie_id_if_has_location_header(r, cookie);
1266 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1267 if (! ap_is_HTTP_REDIRECT(r->status)) {
1268 r->status = HTTP_MOVED_TEMPORARILY;
1271 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1272 ctx->buffer = apr_pstrdup(pool, "");
1274 ap_set_content_length(r, (apr_off_t)ctx->len);
1276 chxj_cookie_unlock(r,lock);
1277 s_add_no_cache_headers(r, entryp);
1278 rv = pass_data_to_filter(f,
1279 (const char *)ctx->buffer,
1280 (apr_size_t)ctx->len);
1283 ctx->buffer = apr_pstrdup(pool, "");
1285 ap_set_content_length(r, (apr_off_t)ctx->len);
1286 chxj_cookie_unlock(r, lock);
1287 s_add_no_cache_headers(r, entryp);
1288 rv = pass_data_to_filter(f,
1289 (const char *)ctx->buffer,
1290 (apr_size_t)ctx->len);
1292 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1296 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1301 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1302 cookie_lock_t *lock = NULL;
1303 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1304 switch(spec->html_spec_type) {
1305 case CHXJ_SPEC_Chtml_1_0:
1306 case CHXJ_SPEC_Chtml_2_0:
1307 case CHXJ_SPEC_Chtml_3_0:
1308 case CHXJ_SPEC_Chtml_4_0:
1309 case CHXJ_SPEC_Chtml_5_0:
1310 case CHXJ_SPEC_Chtml_6_0:
1311 case CHXJ_SPEC_Chtml_7_0:
1312 case CHXJ_SPEC_XHtml_Mobile_1_0:
1313 case CHXJ_SPEC_Jhtml:
1314 lock = chxj_cookie_lock(r);
1315 cookie = chxj_save_cookie(r);
1317 * Location Header Check to add cookie parameter.
1319 s_add_cookie_id_if_has_location_header(r, cookie);
1320 chxj_cookie_unlock(r, lock);
1321 apr_table_unset(r->headers_out, "Set-Cookie");
1322 apr_table_unset(r->err_headers_out, "Set-Cookie");
1329 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1330 if (! ap_is_HTTP_REDIRECT(r->status)) {
1331 r->status = HTTP_MOVED_TEMPORARILY;
1334 apr_table_setn(r->headers_out, "Content-Length", "0");
1335 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1336 s_add_no_cache_headers(r, entryp);
1337 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1338 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1343 apr_brigade_destroy(bb);
1345 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1351 * Add Cookie_id if it has location header.
1354 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1356 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1357 if (! location_header) {
1358 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1360 if (cookie && location_header) {
1361 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1362 location_header = chxj_add_cookie_parameter(r,
1365 apr_table_unset(r->headers_out, "Location");
1366 apr_table_setn(r->headers_out, "Location", location_header);
1367 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1368 if (!ap_is_HTTP_REDIRECT(r->status)) {
1369 r->status = HTTP_MOVED_TEMPORARILY;
1375 * It is the main loop of the input filter handler.
1379 chxj_input_handler(request_rec *r)
1381 mod_chxj_config *dconf;
1382 chxjconvrule_entry *entryp = NULL;
1383 device_table *spec = NULL;
1384 char *post_data = NULL;
1385 apr_size_t post_data_len = 0;
1390 int response_code = 0;
1392 DBG(r, "start of chxj_input_handler()");
1394 if (strcasecmp(r->handler, "chxj-input-handler")) {
1395 DBG(r, "end chxj_input_handler()");
1398 apr_pool_create(&pool, r->pool);
1400 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1401 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1403 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1405 spec = chxj_specified_device(r, user_agent);
1406 entryp = chxj_apply_convrule(r, dconf->convrules);
1408 post_data = apr_pstrdup(pool, "");
1409 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1410 if (ap_should_client_block(r)) {
1411 while (post_data_len < CHXJ_POST_MAX) {
1414 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1415 if (read_bytes<=0) {
1418 buffer[read_bytes] = '\0';
1419 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1420 post_data_len += read_bytes;
1429 if (post_data_len > 0) {
1430 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1431 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1435 if (dconf->forward_url_base) {
1436 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1439 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);
1442 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1444 DBG(r, "==> new url_path:[%s]", url_path);
1447 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1448 apr_table_unset(r->headers_in, "Content-Length");
1449 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1450 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1451 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1452 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1453 for (ii=0; ii<res_len/64; ii++) {
1454 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1456 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1459 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1460 if (strcasecmp(chunked, "chunked") == 0) {
1462 apr_table_unset(r->headers_out, "Transfer-Encoding");
1465 if (ap_is_HTTP_ERROR(response_code)) {
1466 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1467 return response_code;
1471 apr_pool_create(&wpool, r->pool);
1472 apr_bucket_brigade *bb;
1475 conn_rec *c = r->connection;
1477 bb = apr_brigade_create(wpool, c->bucket_alloc);
1478 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1479 APR_BRIGADE_INSERT_TAIL(bb, e);
1480 e = apr_bucket_eos_create(c->bucket_alloc);
1481 APR_BRIGADE_INSERT_TAIL(bb, e);
1482 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1483 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1486 apr_brigade_cleanup(bb);
1489 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1493 static mod_chxj_global_config *
1494 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1496 mod_chxj_global_config *conf;
1498 SDBG(s, "start chxj_global_config_create()");
1500 /*--------------------------------------------------------------------------*/
1501 /* allocate an own subpool which survives server restarts */
1502 /*--------------------------------------------------------------------------*/
1503 conf = (mod_chxj_global_config *)apr_palloc(pool,
1504 sizeof(mod_chxj_global_config));
1506 conf->cookie_db_lock = NULL;
1508 SDBG(s, "end chxj_global_config_create()");
1515 * initialize chxj module
1518 chxj_init_module(apr_pool_t *p,
1519 apr_pool_t *UNUSED(plog),
1520 apr_pool_t *UNUSED(ptemp),
1526 SDBG(s, "start chxj_init_module()");
1528 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1530 if (user_data == NULL) {
1533 * dummy user_data set.
1535 apr_pool_userdata_set(
1537 CHXJ_MOD_CONFIG_KEY,
1538 apr_pool_cleanup_null,
1540 SDBG(s, "end chxj_init_module()");
1544 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1546 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1548 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1549 return HTTP_INTERNAL_SERVER_ERROR;
1552 SDBG(s, "end chxj_init_module()");
1559 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1562 SDBG(s, "start chxj_child_init()");
1563 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1565 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1567 SDBG(s, "end chxj_child_init()");
1572 * A set structure of each server is generated.
1578 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1580 mod_chxj_global_config *gc;
1582 gc = chxj_global_config_create(p,s);
1589 chxj_translate_name(request_rec *r)
1591 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1592 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1593 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1594 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1595 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1596 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1598 return chxj_trans_name(r);
1606 chxj_insert_filter(request_rec *r)
1610 mod_chxj_config *dconf;
1611 chxjconvrule_entry *entryp;
1616 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1618 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1620 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1621 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1623 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1626 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1628 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1629 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1630 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1634 spec = chxj_specified_device(r, user_agent);
1635 entryp = chxj_apply_convrule(r, dconf->convrules);
1637 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1640 ctx = apr_palloc(r->pool, sizeof(*ctx));
1641 memset(ctx, 0, sizeof(*ctx));
1642 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1643 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1644 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1647 ctx->entryp = entryp;
1649 ctx->buffer = apr_palloc(ctx->pool, 1);
1652 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1653 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1654 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1658 switch(spec->html_spec_type) {
1659 case CHXJ_SPEC_Chtml_1_0:
1660 case CHXJ_SPEC_Chtml_2_0:
1661 case CHXJ_SPEC_Chtml_3_0:
1662 case CHXJ_SPEC_Chtml_4_0:
1663 case CHXJ_SPEC_Chtml_5_0:
1664 case CHXJ_SPEC_Chtml_6_0:
1665 case CHXJ_SPEC_Chtml_7_0:
1666 case CHXJ_SPEC_XHtml_Mobile_1_0:
1667 case CHXJ_SPEC_Hdml:
1668 case CHXJ_SPEC_Jhtml:
1669 case CHXJ_SPEC_Jxhtml:
1673 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1678 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1679 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1680 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1683 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1688 * The hook is registered.
1693 chxj_register_hooks(apr_pool_t *UNUSED(p))
1695 ap_hook_post_config(chxj_init_module,
1698 APR_HOOK_REALLY_FIRST);
1699 ap_hook_child_init(chxj_child_init,
1702 APR_HOOK_REALLY_FIRST);
1703 ap_register_output_filter (
1704 "chxj_output_filter",
1708 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1709 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1710 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1711 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1712 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1713 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1718 * A set structure according to the directory is generated.
1724 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1726 mod_chxj_config *conf;
1728 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1729 conf->device_data_file = NULL;
1730 conf->devices = NULL;
1731 conf->emoji_data_file = NULL;
1733 conf->emoji_tail = NULL;
1734 conf->image = CHXJ_IMG_NONE;
1735 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1736 conf->image_cache_limit = 0;
1737 conf->server_side_encoding = NULL;
1738 conf->cookie_db_dir = NULL;
1739 conf->cookie_timeout = 0;
1740 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1741 conf->cookie_lazy_mode = 0;
1742 conf->cookie_dbm_type = NULL;
1743 #if defined(USE_MYSQL_COOKIE)
1744 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1745 conf->mysql.port = MYSQL_PORT;
1746 conf->mysql.host = NULL;
1748 #if defined(USE_MEMCACHE_COOKIE)
1749 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1750 conf->memcache.host = NULL;
1751 conf->memcache.port = 0;
1753 conf->forward_url_base = NULL;
1754 conf->forward_server_ip = NULL;
1755 conf->allowed_cookie_domain = NULL;
1756 conf->post_log = NULL;
1762 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1763 memset(conf->dir, 0, strlen(arg)+1);
1764 strcpy(conf->dir, arg);
1766 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1768 /* Default is copyleft */
1769 conf->image_copyright = NULL;
1776 * Merge per-directory CHXJ configurations
1779 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1781 mod_chxj_config *base;
1782 mod_chxj_config *add;
1783 mod_chxj_config *mrg;
1785 base = (mod_chxj_config *)basev;
1786 add = (mod_chxj_config *)addv;
1787 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1789 mrg->device_data_file = NULL;
1790 mrg->devices = NULL;
1791 mrg->emoji_data_file = NULL;
1792 mrg->image = CHXJ_IMG_NONE;
1793 mrg->image_cache_dir = NULL;
1794 mrg->image_copyright = NULL;
1795 mrg->image_cache_limit = 0;
1797 mrg->emoji_tail = NULL;
1798 mrg->new_line_type = NLTYPE_NIL;
1799 mrg->forward_url_base = NULL;
1800 mrg->forward_server_ip = NULL;
1801 mrg->allowed_cookie_domain = NULL;
1802 mrg->post_log = NULL;
1803 mrg->cookie_dbm_type = NULL;
1805 mrg->dir = apr_pstrdup(p, add->dir);
1807 if (! add->device_data_file) {
1808 mrg->devices = base->devices;
1809 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1812 mrg->devices = add->devices;
1813 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1816 if (! add->emoji_data_file) {
1817 mrg->emoji = base->emoji;
1818 mrg->emoji_tail = base->emoji_tail;
1819 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1822 mrg->emoji = add->emoji;
1823 mrg->emoji_tail = add->emoji_tail;
1824 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1827 if (add->image == CHXJ_IMG_NONE) {
1828 mrg->image = base->image;
1831 mrg->image = add->image;
1834 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1835 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1838 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1841 if (add->image_cache_limit) {
1842 mrg->image_cache_limit = add->image_cache_limit;
1845 mrg->image_cache_limit = base->image_cache_limit;
1848 if (add->image_copyright)
1849 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1851 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1853 if (add->server_side_encoding) {
1854 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1857 if (base->server_side_encoding) {
1858 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1861 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1864 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1866 if (add->cookie_db_dir) {
1867 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1870 if (base->cookie_db_dir) {
1871 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1874 mrg->cookie_db_dir = NULL;
1877 if (add->cookie_timeout) {
1878 mrg->cookie_timeout = add->cookie_timeout;
1881 if (base->cookie_db_dir) {
1882 mrg->cookie_timeout = base->cookie_timeout;
1885 mrg->cookie_timeout = 0;
1888 #if defined(USE_MYSQL_COOKIE)
1889 if (add->mysql.host) {
1890 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1892 else if (base->mysql.host) {
1893 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1896 mrg->mysql.host = NULL;
1898 if (add->mysql.port) {
1899 mrg->mysql.port = add->mysql.port;
1901 else if (base->mysql.port) {
1902 mrg->mysql.port = base->mysql.port;
1905 mrg->mysql.port = 0;
1908 if (add->mysql.database) {
1909 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1911 else if (base->mysql.database) {
1912 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1915 mrg->mysql.database = NULL;
1918 if (add->mysql.username) {
1919 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1921 else if (base->mysql.username) {
1922 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1925 mrg->mysql.username = NULL;
1928 if (add->mysql.password) {
1929 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1931 else if (base->mysql.password) {
1932 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1935 mrg->mysql.password = NULL;
1938 if (add->mysql.tablename) {
1939 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1941 else if (base->mysql.tablename) {
1942 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1945 mrg->mysql.tablename = NULL;
1948 if (add->mysql.socket_path) {
1949 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1951 else if (base->mysql.socket_path) {
1952 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1955 mrg->mysql.socket_path = NULL;
1958 if (add->mysql.charset) {
1959 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1961 else if (base->mysql.charset) {
1962 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1965 mrg->mysql.charset = NULL;
1968 #if defined(USE_MEMCACHE_COOKIE)
1969 if (add->memcache.host) {
1970 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1972 else if (base->memcache.host) {
1973 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1976 mrg->memcache.host = NULL;
1978 if (add->memcache.port) {
1979 mrg->memcache.port = add->memcache.port;
1981 else if (base->memcache.port) {
1982 mrg->memcache.port = base->memcache.port;
1985 mrg->memcache.port = 0;
1988 if (add->cookie_store_type) {
1989 mrg->cookie_store_type = add->cookie_store_type;
1991 else if (base->cookie_store_type) {
1992 mrg->cookie_store_type = base->cookie_store_type;
1995 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1997 if (add->cookie_lazy_mode) {
1998 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
2000 else if (base->cookie_lazy_mode) {
2001 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
2004 mrg->cookie_lazy_mode = 0;
2006 if (add->new_line_type) {
2007 mrg->new_line_type = add->new_line_type;
2009 else if (base->new_line_type) {
2010 mrg->new_line_type = base->new_line_type;
2013 mrg->new_line_type = NLTYPE_NIL;
2016 if (add->forward_url_base) {
2017 mrg->forward_url_base = add->forward_url_base;
2019 else if (base->forward_url_base) {
2020 mrg->forward_url_base = base->forward_url_base;
2023 if (add->allowed_cookie_domain) {
2024 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
2027 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
2029 if (add->post_log) {
2030 mrg->post_log = add->post_log;
2033 mrg->post_log = base->post_log;
2035 if (add->cookie_dbm_type) {
2036 mrg->cookie_dbm_type = add->cookie_dbm_type;
2039 mrg->cookie_dbm_type = base->cookie_dbm_type;
2046 chxj_command_parse_take5(
2059 for (;*strp == ' '||*strp == '\t'; strp++) ;
2069 for (; *strp != '\0'; strp++) {
2070 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2071 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2076 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2077 || (isquoted && *strp == '"'))
2091 for (;*strp == ' '||*strp == '\t'; strp++) ;
2100 for (; *strp != '\0'; strp++) {
2101 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2102 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2107 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2108 || (isquoted && *strp == '"'))
2121 for (;*strp == ' '||*strp == '\t'; strp++);
2129 for (; *strp != '\0'; strp++) {
2130 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2131 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2136 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2137 || (isquoted && *strp == '"'))
2149 for (;*strp == ' '||*strp == '\t'; strp++);
2157 for (; *strp != '\0'; strp++) {
2158 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2159 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2164 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2165 || (isquoted && *strp == '"'))
2176 for (;*strp == ' '||*strp == '\t'; strp++);
2184 for (; *strp != '\0'; strp++) {
2185 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2186 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2191 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2192 || (isquoted && *strp == '"'))
2202 * The device definition file is loaded.
2204 * @param arg [i] The name of the device definition file is specified.
2205 * @param mconfig [i/o] The pointer to a set structure is specified.
2209 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2211 mod_chxj_config *conf;
2216 if (strlen(arg) > 256)
2217 return "mod_chxj: device data filename too long.";
2219 conf = (mod_chxj_config *)mconfig;
2220 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2222 qs_init_malloc(&doc);
2223 qs_init_root_node(&doc);
2225 qs_parse_file((Doc *)&doc, (const char *)arg);
2226 chxj_load_device_data(&doc,parms->pool, conf);
2227 qs_all_free(&doc, QX_LOGMARK);
2234 * Device definition information is loaded.
2237 * @param arg [i] The name of the device definition file is specified.
2238 * @param mconfig [i/o] The pointer to a set structure is specified.
2242 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2244 mod_chxj_config *conf;
2251 if (strlen(arg) > 256)
2252 return "mod_chxj: emoji data filename too long.";
2254 conf = (mod_chxj_config *)mconfig;
2255 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2256 qs_init_malloc(&doc);
2257 qs_init_root_node(&doc);
2259 qs_parse_file((Doc *)&doc, (const char *)arg);
2261 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2263 qs_all_free(&doc, QX_LOGMARK);
2271 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2273 mod_chxj_config *conf;
2278 if (strlen(arg) > 256)
2279 return "image uri is too long.";
2281 conf = (mod_chxj_config*)mconfig;
2282 if (strcasecmp("ON", arg) == 0) {
2283 conf->image = CHXJ_IMG_ON;
2286 conf->image = CHXJ_IMG_OFF;
2294 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2296 mod_chxj_config *conf;
2301 if (strlen(arg) > 256)
2302 return "cache dir name is too long.";
2304 conf = (mod_chxj_config *)mconfig;
2305 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2312 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2314 mod_chxj_config *conf;
2319 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2320 return "cache size is too long.";
2322 conf = (mod_chxj_config *)mconfig;
2325 * I use strtol function because strtoul is not portable function.
2327 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2330 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2332 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2341 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2343 mod_chxj_config *conf;
2348 if (strlen(arg) > 256)
2349 return "Copyright Flag is too long.";
2351 conf = (mod_chxj_config *)mconfig;
2352 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2359 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2363 mod_chxj_config *dconf;
2364 chxjconvrule_entry *newrule;
2374 dconf = (mod_chxj_config *)mconfig;
2376 if (strlen(arg) > 4096)
2377 return "mod_chxj: ChxjConvertRule: is too long.";
2379 dconf = (mod_chxj_config *)mconfig;
2380 if (dconf->convrules == NULL)
2381 dconf->convrules = apr_array_make(cmd->pool,
2383 sizeof(chxjconvrule_entry));
2385 newrule = apr_array_push(dconf->convrules);
2388 newrule->action = 0;
2390 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2391 return "ChxjConvertRule: bad argument line";
2393 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2397 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2403 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2404 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2407 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2408 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2411 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2412 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2415 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2416 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2422 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2423 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2425 else if (strcasecmp(CONVRULE_COOKIE_OFF_CMD, action) == 0) {
2426 newrule->action &= (0xffffffff ^ CONVRULE_COOKIE_ON_BIT);
2428 else if (strcasecmp(CONVRULE_CSS_ON_CMD, action) == 0) {
2429 newrule->action |= CONVRULE_CSS_ON_BIT;
2431 else if (strcasecmp(CONVRULE_CSS_OFF_CMD, action) == 0) {
2432 newrule->action &= (0xffffffff ^ CONVRULE_CSS_ON_BIT);
2438 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2439 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2445 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2446 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2452 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2453 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2459 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2460 newrule->action |= CONVRULE_Z2H_ON_BIT;
2463 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2464 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2467 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2468 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2471 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2472 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2475 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2476 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2479 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2480 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2483 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2484 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2487 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2488 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2499 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2503 mode = AP_REG_EXTENDED;
2504 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2505 return "RewriteRule: cannot compile regular expression ";
2507 newrule->regexp = regexp;
2509 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2511 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2513 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2515 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2516 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2518 newrule->user_agent = NULL;
2520 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2532 mod_chxj_config *dconf;
2535 if (strlen(arg) > 4096)
2536 return "mod_chxj: ChxjCookieDir is too long.";
2538 dconf = (mod_chxj_config *)mconfig;
2540 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2547 cmd_set_cookie_timeout(
2548 cmd_parms *UNUSED(cmd),
2552 mod_chxj_config *dconf;
2554 if (strlen(arg) > 4096)
2555 return "mod_chxj: ChxjCookieTimeout is too long.";
2557 if (chxj_chk_numeric(arg) != 0)
2558 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2560 dconf = (mod_chxj_config *)mconfig;
2562 dconf->cookie_timeout = atoi(arg);
2568 #if defined(USE_MYSQL_COOKIE)
2570 cmd_set_cookie_mysql_database(
2575 mod_chxj_config *dconf;
2577 if (strlen(arg) > 255)
2578 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2580 dconf = (mod_chxj_config *)mconfig;
2582 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2589 cmd_set_cookie_mysql_username(
2594 mod_chxj_config *dconf;
2596 if (strlen(arg) > 255)
2597 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2599 dconf = (mod_chxj_config *)mconfig;
2601 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2608 cmd_set_cookie_mysql_password(
2613 mod_chxj_config *dconf;
2615 if (strlen(arg) > 255)
2616 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2618 dconf = (mod_chxj_config *)mconfig;
2620 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2627 cmd_set_cookie_mysql_table_name(
2632 mod_chxj_config *dconf;
2634 if (strlen(arg) > 255)
2635 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2637 dconf = (mod_chxj_config *)mconfig;
2639 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2645 cmd_set_cookie_mysql_port(
2646 cmd_parms *UNUSED(cmd),
2650 mod_chxj_config *dconf;
2652 if (strlen(arg) > 255)
2653 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2655 dconf = (mod_chxj_config *)mconfig;
2657 if (chxj_chk_numeric(arg) != 0)
2658 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2660 dconf = (mod_chxj_config *)mconfig;
2662 dconf->mysql.port = chxj_atoi(arg);
2669 cmd_set_cookie_mysql_host(
2674 mod_chxj_config *dconf;
2676 if (strlen(arg) > 255)
2677 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2679 dconf = (mod_chxj_config *)mconfig;
2681 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2688 cmd_set_cookie_mysql_socket_path(
2693 mod_chxj_config *dconf;
2695 if (strlen(arg) > 4096)
2696 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2698 dconf = (mod_chxj_config *)mconfig;
2700 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2707 cmd_set_cookie_mysql_charset(
2712 mod_chxj_config *dconf;
2714 if (strlen(arg) > 255)
2715 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2717 dconf = (mod_chxj_config *)mconfig;
2719 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2724 #if defined(USE_MEMCACHE_COOKIE)
2726 cmd_set_cookie_memcache_port(
2727 cmd_parms *UNUSED(cmd),
2731 mod_chxj_config *dconf;
2733 if (strlen(arg) > 255)
2734 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2736 dconf = (mod_chxj_config *)mconfig;
2738 if (chxj_chk_numeric(arg) != 0)
2739 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2741 dconf = (mod_chxj_config *)mconfig;
2743 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2750 cmd_set_cookie_memcache_host(
2755 mod_chxj_config *dconf;
2757 if (strlen(arg) > 255)
2758 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2760 dconf = (mod_chxj_config *)mconfig;
2762 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2769 cmd_set_cookie_lazy_mode(
2770 cmd_parms *UNUSED(cmd),
2774 mod_chxj_config *dconf;
2776 if (strlen(arg) > 255)
2777 return "mod_chxj: ChxjCookieLazyMode is too long.";
2779 dconf = (mod_chxj_config *)mconfig;
2781 if (strcasecmp("TRUE",arg) == 0) {
2782 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2785 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2792 cmd_set_cookie_store_type(
2793 cmd_parms *UNUSED(cmd),
2797 mod_chxj_config *dconf;
2799 if (strlen(arg) > 255)
2800 return "mod_chxj: ChxjCookieStoreType is too long.";
2802 dconf = (mod_chxj_config *)mconfig;
2804 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2805 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2807 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2808 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2810 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2811 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2814 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2821 cmd_set_forward_url_base(
2826 mod_chxj_config *dconf;
2828 if (strlen(arg) > 255)
2829 return "mod_chxj: ChxjForwardUrlBase is too long.";
2831 dconf = (mod_chxj_config *)mconfig;
2833 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2839 cmd_set_forward_server_ip(
2844 mod_chxj_config *dconf;
2846 if (strlen(arg) > 255)
2847 return "mod_chxj: ChxjForwardServerIp is too long.";
2849 dconf = (mod_chxj_config *)mconfig;
2851 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2857 cmd_allowed_cookie_domain(
2862 mod_chxj_config *dconf;
2864 if (strlen(arg) > 255)
2865 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2867 dconf = (mod_chxj_config *)mconfig;
2869 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2875 cmd_set_new_line_type(
2876 cmd_parms *UNUSED(cmd),
2880 mod_chxj_config *dconf;
2881 if (strlen(arg) > 255)
2882 return "mod_chxj: ChxjNewLineType is too long.";
2884 dconf = (mod_chxj_config *)mconfig;
2886 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2887 dconf->new_line_type = NLTYPE_CRLF;
2889 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2890 dconf->new_line_type = NLTYPE_LF;
2892 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2893 dconf->new_line_type = NLTYPE_CR;
2895 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2896 dconf->new_line_type = NLTYPE_NONE;
2899 return "mod_chxj: invalid value (ChxjNewLineType)";
2910 mod_chxj_config *dconf;
2911 if (strlen(arg) > 255)
2912 return "mod_chxj: ChxjPostLogEnv is too long.";
2914 dconf = (mod_chxj_config *)mconfig;
2916 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2922 cmd_cookie_dbm_type(
2927 mod_chxj_config *dconf;
2928 if (strlen(arg) > 255)
2929 return "mod_chxj: ChxjCookieDbmType is too long.";
2931 dconf = (mod_chxj_config *)mconfig;
2933 dconf->cookie_dbm_type = apr_pstrdup(cmd->pool, arg);
2939 static const command_rec cmds[] = {
2941 "ChxjLoadDeviceData",
2942 cmd_load_device_data,
2945 "Load Device Data"),
2947 "ChxjLoadEmojiData",
2948 cmd_load_emoji_data,
2954 cmd_set_image_engine,
2957 "Convert Target URI"),
2959 "ChxjImageCacheDir",
2960 cmd_set_image_cache_dir,
2963 "Image Cache Directory"),
2965 "ChxjImageCacheLimit",
2966 cmd_set_image_cache_limit,
2969 "Image Cache Limit"),
2971 "ChxjImageCopyright",
2972 cmd_set_image_copyright,
2981 "an URL-applied regexp-pattern and a substitution URL"),
2987 "save cookie.db directory."),
2989 "ChxjCookieTimeout",
2990 cmd_set_cookie_timeout,
2993 "The compulsion time-out time of the cookie is specified. "),
2995 "ChxjCookieStoreType",
2996 cmd_set_cookie_store_type,
2999 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
3001 "ChxjCookieLazyMode",
3002 cmd_set_cookie_lazy_mode,
3005 "OneTimeID is negligently done. (TRUE/FALSE)"),
3006 #if defined(USE_MYSQL_COOKIE)
3008 "ChxjCookieMysqlHost",
3009 cmd_set_cookie_mysql_host,
3012 "The MySQL database host used by saving Cookie"),
3014 "ChxjCookieMysqlPort",
3015 cmd_set_cookie_mysql_port,
3018 "The MySQL database port used by saving Cookie"),
3020 "ChxjCookieMysqlDatabase",
3021 cmd_set_cookie_mysql_database,
3024 "The MySQL database name used by saving Cookie"),
3026 "ChxjCookieMysqlUsername",
3027 cmd_set_cookie_mysql_username,
3030 "The MySQL username used by saving Cookie"),
3032 "ChxjCookieMysqlPassword",
3033 cmd_set_cookie_mysql_password,
3036 "The MySQL password used by saving Cookie"),
3038 "ChxjCookieMysqlTableName",
3039 cmd_set_cookie_mysql_table_name,
3042 "The MySQL table name used by saving Cookie"),
3044 "ChxjCookieMysqlSocketPath",
3045 cmd_set_cookie_mysql_socket_path,
3048 "The MySQL socket path used by saving Cookie"),
3050 "ChxjCookieMysqlCharset",
3051 cmd_set_cookie_mysql_charset,
3054 "The MySQL charset used by saving Cookie"),
3056 #if defined(USE_MEMCACHE_COOKIE)
3058 "ChxjCookieMemcacheHost",
3059 cmd_set_cookie_memcache_host,
3062 "The Memcached host used by saving Cookie"),
3064 "ChxjCookieMemcachePort",
3065 cmd_set_cookie_memcache_port,
3068 "The Memcached port used by saving Cookie"),
3072 cmd_set_new_line_type,
3075 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3077 "ChxjForwardUrlBase",
3078 cmd_set_forward_url_base,
3081 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3083 "ChxjForwardServerIp",
3084 cmd_set_forward_server_ip,
3087 "The forward server ip(default: this server ip)"),
3089 "ChxjAllowedCookieDomain",
3090 cmd_allowed_cookie_domain,
3093 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3099 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3101 "ChxjCookieDbmType",
3102 cmd_cookie_dbm_type,
3105 "Kind of DBM used with Cookie simulator.(default|GDBM|SDBM|DB|NDBM)"),
3106 {NULL,{NULL},NULL,0,0,NULL},
3110 /*----------------------------------------------------------------------------*/
3111 /* Dispatch list for API hooks */
3112 /*----------------------------------------------------------------------------*/
3113 module AP_MODULE_DECLARE_DATA chxj_module = {
3114 STANDARD20_MODULE_STUFF,
3115 chxj_create_per_dir_config, /* create per-dir config structures */
3116 chxj_merge_per_dir_config, /* merge per-dir config structures */
3117 chxj_config_server_create, /* create per-server config structures */
3118 NULL, /* merge per-server config structures */
3119 cmds, /* table of config file commands */
3120 chxj_register_hooks /* register hooks */