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_jhtml.h"
58 #include "chxj_jxhtml.h"
59 #include "chxj_img_conv_format.h"
60 #include "chxj_qr_code.h"
61 #include "chxj_encoding.h"
62 #include "chxj_apply_convrule.h"
63 #include "chxj_cookie.h"
64 #include "chxj_url_encode.h"
65 #include "chxj_str_util.h"
66 #if defined(USE_MYSQL_COOKIE)
67 # include "chxj_mysql.h"
69 #include "chxj_serf.h"
70 #include "chxj_add_device_env.h"
71 #include "chxj_conv_z2h.h"
72 #include "chxj_header_inf.h"
73 #include "chxj_jreserved_tag.h"
76 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
77 #define CHXJ_VERSION PACKAGE_VERSION
78 #define CHXJ_POST_MAX (0x1000000)
80 converter_t convert_routine[] = {
82 /* CHXJ_SPEC_UNKNOWN */
85 .emoji_only_converter = NULL,
88 /* CHXJ_SPEC_Chtml_1_0 */
89 .converter = chxj_convert_chtml10,
90 .encoder = chxj_encoding,
91 .emoji_only_converter = chxj_chtml10_emoji_only_converter,
94 /* CHXJ_SPEC_Chtml_2_0 */
95 .converter = chxj_convert_chtml20,
96 .encoder = chxj_encoding,
97 .emoji_only_converter = chxj_chtml20_emoji_only_converter,
100 /* CHXJ_SPEC_Chtml_3_0 */
101 .converter = chxj_convert_chtml30,
102 .encoder = chxj_encoding,
103 .emoji_only_converter = chxj_chtml30_emoji_only_converter,
106 /* CHXJ_SPEC_Chtml_4_0 */
107 .converter = chxj_convert_chtml40,
108 .encoder = chxj_encoding,
109 .emoji_only_converter = chxj_chtml40_emoji_only_converter,
112 /* CHXJ_SPEC_Chtml_5_0 */
113 .converter = chxj_convert_chtml50,
114 .encoder = chxj_encoding,
115 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
118 /* CHXJ_SPEC_Chtml_6_0 */
119 .converter = chxj_convert_chtml50,
120 .encoder = chxj_encoding,
121 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
124 /* CHXJ_SPEC_Chtml_7_0 */
125 .converter = chxj_convert_chtml50,
126 .encoder = chxj_encoding,
127 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
130 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
131 .converter = chxj_convert_xhtml_mobile_1_0,
132 .encoder = chxj_encoding,
133 .emoji_only_converter = chxj_xhtml_emoji_only_converter,
137 .converter = chxj_convert_hdml,
138 .encoder = chxj_encoding,
139 .emoji_only_converter = NULL,
142 /* CHXJ_SPEC_Jhtml */
143 .converter = chxj_convert_jhtml,
144 .encoder = chxj_encoding,
145 .emoji_only_converter = chxj_jhtml_emoji_only_converter,
148 /* CHXJ_SPEC_Jxhtml */
149 .converter = chxj_convert_jxhtml,
150 .encoder = chxj_encoding,
151 .emoji_only_converter = chxj_jxhtml_emoji_only_converter,
157 .emoji_only_converter = NULL,
161 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec);
162 static void s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec);
163 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
164 static void s_clear_cookie_header(request_rec *r, device_table *spec);
165 static void s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp);
168 * Only when User-Agent is specified, the User-Agent header is camouflaged.
173 chxj_headers_fixup(request_rec *r)
175 mod_chxj_config* dconf;
176 chxjconvrule_entry* entryp;
182 DBG(r, "REQ[%X] start chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
184 DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)(apr_size_t)r);
185 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
189 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
191 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
192 spec = chxj_specified_device(r, user_agent);
194 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
196 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
197 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)(apr_size_t)r);
198 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
201 if (r->method_number == M_POST) {
202 if (!apr_table_get(r->headers_in, "X-Chxj-Forward")) {
203 s_clear_cookie_header(r, spec);
207 s_clear_cookie_header(r, spec);
210 switch(spec->html_spec_type) {
211 case CHXJ_SPEC_Chtml_1_0:
212 case CHXJ_SPEC_Chtml_2_0:
213 case CHXJ_SPEC_Chtml_3_0:
214 case CHXJ_SPEC_Chtml_4_0:
215 case CHXJ_SPEC_Chtml_5_0:
216 case CHXJ_SPEC_Chtml_6_0:
217 case CHXJ_SPEC_Chtml_7_0:
218 case CHXJ_SPEC_XHtml_Mobile_1_0:
220 case CHXJ_SPEC_Jhtml:
221 case CHXJ_SPEC_Jxhtml:
222 entryp = chxj_apply_convrule(r, dconf->convrules);
224 DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int)(apr_size_t)r);
227 if (!(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
228 DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int)(apr_size_t)r);
231 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
232 DBG(r, "REQ[%X] end chxj_headers_fixup() (emoji only)", (unsigned int)(apr_size_t)r);
236 apr_table_setn(r->headers_in,
237 CHXJ_HTTP_USER_AGENT,
240 if (entryp->user_agent)
241 apr_table_setn(r->headers_in,
245 chxj_convert_input_header(r,entryp, spec);
250 DBG(r, "REQ[%X] end chxj_headers_fixup() (not mobile) spec->device_name[%s]", (unsigned int)(apr_size_t)r, spec->device_name);
256 if (r->method_number == M_POST) {
257 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
258 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);
259 r->proxyreq = PROXYREQ_NONE;
260 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
263 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
265 if (dconf->post_log) {
266 apr_table_setn(r->subprocess_env, dconf->post_log, dconf->post_log);
269 apr_table_setn(r->subprocess_env, "chxj-post-log", "chxj-post-log");
271 apr_sockaddr_t *address = NULL;
272 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
273 if (rv != APR_SUCCESS) {
275 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));
276 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
280 if (dconf->forward_server_ip) {
281 addr = dconf->forward_server_ip;
284 apr_sockaddr_ip_get(&addr, address);
286 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);
287 if (strcmp(addr, r->connection->remote_ip) == 0) {
288 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
290 if (! apr_table_get(r->headers_in, "Content-Length")) {
291 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
293 apr_table_set(r->headers_in, "Content-Length", contentLength);
300 chxj_add_device_env(r, spec);
302 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
309 s_clear_cookie_header(request_rec *r, device_table *spec)
311 switch(spec->html_spec_type) {
312 case CHXJ_SPEC_Chtml_1_0:
313 case CHXJ_SPEC_Chtml_2_0:
314 case CHXJ_SPEC_Chtml_3_0:
315 case CHXJ_SPEC_Chtml_4_0:
316 case CHXJ_SPEC_Chtml_5_0:
317 case CHXJ_SPEC_Chtml_6_0:
318 case CHXJ_SPEC_Chtml_7_0:
319 case CHXJ_SPEC_XHtml_Mobile_1_0:
320 case CHXJ_SPEC_Jhtml:
321 apr_table_unset(r->headers_in, "Cookie");
330 * It converts it from CHTML into XXML corresponding to each model.
333 * @param src [i] It is former HTML character string.
334 * @param len [i/o] It is length of former HTML character string.
337 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
343 mod_chxj_config *dconf;
344 chxjconvrule_entry *entryp;
346 DBG(r,"REQ[%X] start of chxj_convert() input:[%.*s]", (unsigned int)(apr_size_t)r, (int)*len, *src);
347 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
349 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
352 entryp = chxj_apply_convrule(r, dconf->convrules);
354 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
355 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
360 /*------------------------------------------------------------------------*/
361 /* get UserAgent from http header */
362 /*------------------------------------------------------------------------*/
363 if (entryp->user_agent)
364 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
366 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
368 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
369 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
372 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
373 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
374 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
375 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
379 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
381 spec = chxj_specified_device(r, user_agent);
388 if (entryp->action & CONVRULE_COOKIE_ON_BIT
389 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
390 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
391 switch(spec->html_spec_type) {
392 case CHXJ_SPEC_Chtml_1_0:
393 case CHXJ_SPEC_Chtml_2_0:
394 case CHXJ_SPEC_Chtml_3_0:
395 case CHXJ_SPEC_Chtml_4_0:
396 case CHXJ_SPEC_Chtml_5_0:
397 case CHXJ_SPEC_Chtml_6_0:
398 case CHXJ_SPEC_Chtml_7_0:
399 case CHXJ_SPEC_XHtml_Mobile_1_0:
400 case CHXJ_SPEC_Jhtml:
401 cookie = chxj_save_cookie(r);
408 if (!r->header_only) {
411 if (convert_routine[spec->html_spec_type].encoder)
412 tmp = convert_routine[spec->html_spec_type].encoder(r,
416 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
418 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
421 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
423 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
424 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
429 dst = apr_palloc(r->pool, 1);
434 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
437 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
438 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
439 if (convert_routine[spec->html_spec_type].converter) {
441 dst = convert_routine[spec->html_spec_type].converter(r,
449 dst = convert_routine[spec->html_spec_type].converter(r,
458 dst = chxj_conv_z2h(r, dst, len, entryp);
462 ap_set_content_length(r, *len);
465 dst = apr_psprintf(r->pool, "\n");
474 DBG(r, "REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
481 * convert GUID parameter.
485 s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec)
487 DBG(r, "REQ[%X] start s_convert_guid_parameter() param:[%s]", (unsigned int)(apr_size_t)r, param);
488 if (strcasecmp(param, "guid") == 0) {
489 switch(spec->html_spec_type) {
490 case CHXJ_SPEC_XHtml_Mobile_1_0:
492 char *x_up_subno = (char *)apr_table_get(r->headers_in, "x-up-subno");
494 apr_table_setn(r->headers_in, "X-DCMGUID", x_up_subno);
498 case CHXJ_SPEC_Jhtml:
499 case CHXJ_SPEC_Jxhtml:
501 char *x_jphone_uid = (char *)apr_table_get(r->headers_in, "x-jphone-uid");
503 apr_table_setn(r->headers_in, "X-DCMGUID", x_jphone_uid);
511 DBG(r, "REQ[%X] end s_convert_guid_parameter()", (unsigned int)(apr_size_t)r);
515 * It converts it from HEADER.
520 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec)
532 cookie_t *cookie = NULL;
533 int no_update_flag = 0;
535 DBG(r, "REQ[%X] start chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
538 DBG(r, "REQ[%X] r->args=[null]", (unsigned int)(apr_size_t)r);
539 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
542 urilen = strlen(r->args);
544 result = qs_alloc_zero_byte_string(r->pool);
546 buff_pre = apr_pstrdup(r->pool, r->args);
551 pair = apr_strtok(buff_pre, "&", &pstate);
557 pair_sv = apr_pstrdup(r->pool, pair);
559 name = apr_strtok(pair, "=", &vstate);
560 value = apr_strtok(NULL, "=", &vstate);
561 if (! name) continue;
562 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
563 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
568 buff = apr_pstrdup(r->pool, r->args);
569 DBG(r, "REQ[%X] r->args=[%s]", (unsigned int)(apr_size_t)r, buff);
578 pair = apr_strtok(buff, "&", &pstate);
584 pair_sv = apr_pstrdup(r->pool, pair);
586 name = apr_strtok(pair, "=", &vstate);
587 value = apr_strtok(NULL, "=", &vstate);
588 if (! name) continue;
589 name = chxj_safe_to_jreserved_tag(r, name);
590 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
591 if (strlen(result) != 0)
592 result = apr_pstrcat(r->pool, result, "&", NULL);
594 if (strcasecmp(entryp->encoding, "NONE") != 0) {
599 if (value && *value != 0) {
600 value = chxj_url_decode(r->pool, value);
601 dlen = strlen(value);
602 DBG(r, "************ before encoding[%s]", value);
604 dvalue = chxj_rencoding(r, value, &dlen);
605 dvalue = chxj_url_encode(r->pool, dvalue);
607 DBG(r, "************ after encoding[%s]", dvalue);
613 if (name && *name != 0) {
614 name = chxj_url_decode(r->pool, name);
616 dname = chxj_rencoding(r, name, &dlen);
617 dname = chxj_url_encode(r->pool, dname);
622 s_convert_guid_parameter_to_header(r, dname, spec);
623 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
626 s_convert_guid_parameter_to_header(r, name, spec);
627 if (strcmp(name, pair_sv) != 0) {
628 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
631 result = apr_pstrcat(r->pool, result, name, NULL);
636 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
637 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
638 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
642 if (strlen(value) == 0)
645 if (strlen(result) != 0)
646 result = apr_pstrcat(r->pool, result, "&", NULL);
648 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
651 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
653 apr_table_unset(r->headers_in, "Cookie");
654 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
655 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
656 cookie_lock_t *lock = chxj_cookie_lock(r);
657 cookie = chxj_load_cookie(r, value);
658 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
659 if (! no_update_flag && cookie) {
660 cookie = chxj_update_cookie(r, cookie);
662 chxj_cookie_unlock(r, lock);
664 if (cookie && cookie->cookie_id) {
665 if (strlen(result) != 0)
666 result = apr_pstrcat(r->pool, result, "&", NULL);
667 result = apr_pstrcat(r->pool, result, name, "=", cookie->cookie_id, NULL);
671 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
672 if (strlen(result) != 0)
673 result = apr_pstrcat(r->pool, result, "&", NULL);
674 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
677 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
678 if (! no_update_flag) {
679 result = apr_pstrcat(r->pool, result, "&_chxj_nc=true", NULL);
683 DBG(r, "REQ[%X] result r->args=[%s]", (unsigned int)(apr_size_t)r, r->args);
684 DBG(r, "REQ[%X] end chxj_convert_input_header()", (unsigned int)(apr_size_t)r);
690 * It converts it from POSTDATA .
693 * @param src [i] It is POSTDATA character string.
694 * @param len [i/o] It is length of former HTML character string.
701 chxjconvrule_entry *entryp,
711 cookie_t *cookie = NULL;
713 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 DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
734 DBG(r, "REQ[%X] | BEFORE input convert source |", (unsigned int)(apr_size_t)r);
735 DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
736 for (ii=0; ii<ilen-64; ii+=64) {
737 DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
742 if (ilen >= 64 && ((ilen-64) % 64 != 0)) {
743 DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
745 DBG(r, "REQ[%X] +--------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
750 pair = apr_strtok(buff_pre, "&", &pstate);
756 pair_sv = apr_pstrdup(pool, pair);
758 name = apr_strtok(pair, "=", &vstate);
759 value = apr_strtok(NULL, "=", &vstate);
760 if (! name) continue;
761 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
762 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
772 pair = apr_strtok(s, "&", &pstate);
777 name = apr_strtok(pair, "=", &vstate);
778 value = apr_strtok(NULL, "=", &vstate);
779 if (! name) continue;
780 name = chxj_safe_to_jreserved_tag(r, name);
782 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
783 if (strlen(result) != 0)
784 result = apr_pstrcat(pool, result, "&", NULL);
786 if (strcasecmp(entryp->encoding, "NONE") != 0) {
791 if (value && *value != 0) {
792 value = chxj_url_decode(pool, value);
793 dlen = strlen(value);
794 dvalue = chxj_rencoding(r, value, &dlen);
795 dvalue = chxj_url_encode(pool, dvalue);
801 if (name && *name != 0) {
802 name = chxj_url_decode(pool, name);
804 dname = chxj_rencoding(r, name, &dlen);
805 dname = chxj_url_encode(pool, dname);
812 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
815 result = apr_pstrcat(pool, result, name, "=", value, NULL);
819 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
820 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
821 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
825 if (strlen(value) == 0)
828 if (strlen(result) != 0)
829 result = apr_pstrcat(pool, result, "&", NULL);
831 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
835 dlen = strlen(value);
836 value = chxj_url_decode(pool, value);
837 dvalue = chxj_rencoding(r, value, &dlen);
838 dvalue = chxj_url_encode(pool,dvalue);
839 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
843 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
847 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
849 apr_table_unset(r->headers_in, "Cookie");
850 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
851 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
852 cookie_lock_t *lock = chxj_cookie_lock(r);
853 cookie = chxj_load_cookie(r, value);
854 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
855 if (! no_update_flag && cookie) {
856 cookie = chxj_update_cookie(r, cookie);
858 chxj_cookie_unlock(r, lock);
860 if (cookie && cookie->cookie_id) {
861 if (strlen(result) != 0)
862 result = apr_pstrcat(pool, result, "&", NULL);
863 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
867 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
868 if (strlen(result) != 0)
869 result = apr_pstrcat(pool, result, "&", NULL);
870 result = apr_pstrcat(pool, result, name, "=", value, NULL);
873 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
876 dlen = strlen(value);
878 value = chxj_url_decode(pool, value);
879 dvalue = chxj_rencoding(r, value, &dlen);
880 dvalue = chxj_url_encode(pool,dvalue);
881 if (r->args && strlen(r->args) > 0) {
882 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
885 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
887 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
891 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
894 dlen = strlen(value);
896 value = chxj_url_decode(pool, value);
897 dvalue = chxj_rencoding(r, value, &dlen);
898 dvalue = chxj_url_encode(pool,dvalue);
899 if (r->args && strlen(r->args) > 0) {
900 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
903 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
905 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
908 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
910 *len = strlen(result);
911 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
912 if (! no_update_flag) {
913 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
916 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
917 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
924 * The received data is returned to the filter.
926 * @param f [i/o] It is a filter.
927 * @param data [i] It is data returned to the filter.
928 * @param len [i] It is length of the data returned to the filter.
931 pass_data_to_filter(ap_filter_t *f, const char *data,
934 request_rec *r = f->r;
935 conn_rec *c = r->connection;
937 apr_bucket_brigade *bb;
940 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
942 chxj_header_inf_clear(r);
944 bb = apr_brigade_create(r->pool, c->bucket_alloc);
945 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
947 APR_BRIGADE_INSERT_TAIL(bb, b);
948 b = apr_bucket_eos_create(f->c->bucket_alloc);
949 APR_BRIGADE_INSERT_TAIL(bb, b);
951 rv = ap_pass_brigade(f->next, bb);
952 if (rv != APR_SUCCESS) {
953 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
957 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
964 * Add No Cache Header
967 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
969 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
970 apr_table_unset(r->headers_out, "Pragma");
971 apr_table_unset(r->err_headers_out, "Pragma");
972 apr_table_unset(r->headers_out, "Expires");
973 apr_table_unset(r->err_headers_out, "Expires");
974 apr_table_unset(r->headers_out, "Cache-Control");
975 apr_table_unset(r->err_headers_out, "Cache-Control");
977 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
978 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
979 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
985 * It is the main loop of the output filter.
987 * @param f [i/o] It is a filter.
991 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
997 char *user_agent = NULL;
999 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
1000 cookie_t *cookie = NULL;
1001 mod_chxj_config *dconf;
1002 chxjconvrule_entry *entryp = NULL;
1003 device_table *spec = NULL;
1007 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1010 apr_pool_create(&pool, r->pool);
1012 entryp = ctx->entryp;
1014 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1016 if (r->content_type) {
1017 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1018 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1019 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1020 && ! (dconf->image == CHXJ_IMG_ON
1021 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1022 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1023 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1024 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1025 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1026 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1027 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1028 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1029 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1030 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1031 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1033 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1034 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1035 cookie_lock_t *lock = NULL;
1036 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1037 switch(spec->html_spec_type) {
1038 case CHXJ_SPEC_Chtml_1_0:
1039 case CHXJ_SPEC_Chtml_2_0:
1040 case CHXJ_SPEC_Chtml_3_0:
1041 case CHXJ_SPEC_Chtml_4_0:
1042 case CHXJ_SPEC_Chtml_5_0:
1043 case CHXJ_SPEC_Chtml_6_0:
1044 case CHXJ_SPEC_Chtml_7_0:
1045 case CHXJ_SPEC_XHtml_Mobile_1_0:
1046 case CHXJ_SPEC_Jhtml:
1047 lock = chxj_cookie_lock(r);
1048 cookie = chxj_save_cookie(r);
1049 s_add_cookie_id_if_has_location_header(r, cookie);
1050 chxj_cookie_unlock(r, lock);
1056 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1057 if (! ap_is_HTTP_REDIRECT(r->status)) {
1058 r->status = HTTP_MOVED_TEMPORARILY;
1061 s_add_no_cache_headers(r, entryp);
1062 ap_pass_brigade(f->next, bb);
1063 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1068 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1069 ap_pass_brigade(f->next, bb);
1070 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1075 for (b = APR_BRIGADE_FIRST(bb);
1076 b != APR_BRIGADE_SENTINEL(bb);
1077 b = APR_BUCKET_NEXT(b)) {
1079 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1080 DBG(r, "REQ[%X] read data[%.*s]",(unsigned int)(apr_size_t)r, (int)len, data);
1082 /*--------------------------------------------------------------------*/
1084 /*--------------------------------------------------------------------*/
1086 DBG(r, "append data start");
1087 ctx = (mod_chxj_ctx *)f->ctx;
1090 tmp = apr_palloc(r->pool, ctx->len);
1091 memcpy(tmp, ctx->buffer, ctx->len);
1093 ctx->buffer = apr_palloc(pool, ctx->len + len);
1095 memcpy(ctx->buffer, tmp, ctx->len);
1096 memcpy(&ctx->buffer[ctx->len], data, len);
1100 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1103 if (APR_BUCKET_IS_EOS(b)) {
1105 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1106 /*----------------------------------------------------------------------*/
1108 /*----------------------------------------------------------------------*/
1110 cookie_lock_t *lock = NULL;
1111 ctx = (mod_chxj_ctx *)f->ctx;
1113 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1114 lock = chxj_cookie_lock(r);
1116 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1118 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1119 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1120 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1125 tmp = apr_palloc(pool, ctx->len + 1);
1127 memset(tmp, 0, ctx->len + 1);
1128 memcpy(tmp, ctx->buffer, ctx->len);
1130 ctx->buffer = chxj_convert(r,
1131 (const char **)&tmp,
1132 (apr_size_t *)&ctx->len,
1134 user_agent, &cookie);
1138 ctx->buffer = apr_psprintf(r->pool, "\n");
1140 ctx->buffer = chxj_convert(r,
1141 (const char **)&ctx->buffer,
1142 (apr_size_t *)&ctx->len,
1144 user_agent, &cookie);
1149 && *(char *)r->content_type == 't'
1150 && strncmp(r->content_type, "text/xml", 8) == 0) {
1151 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1159 memset(&doc, 0, sizeof(Doc));
1160 memset(&qrcode, 0, sizeof(qr_code_t));
1162 doc.parse_mode = PARSE_MODE_CHTML;
1166 qs_init_malloc(&doc);
1168 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1171 for (child = qs_get_child_node(&doc,root);
1173 child = qs_get_next_node(&doc,child)) {
1174 char *name = qs_get_node_name(&doc,child);
1175 if (strcasecmp("qrcode",name) == 0) {
1180 qs_all_free(&doc,QX_LOGMARK);
1182 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1183 chxj_qrcode_node_to_qrcode(&qrcode, root);
1184 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1186 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1187 chxj_cookie_unlock(r, lock);
1188 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1191 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1195 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1197 && ( *r->content_type == 'i' || *r->content_type == 'I')
1198 && dconf->image == CHXJ_IMG_ON
1199 && strncasecmp("image/", r->content_type, 6) == 0
1200 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1201 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1202 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1203 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1204 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1205 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1206 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1207 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1208 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1209 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1213 tmp = apr_palloc(pool, ctx->len + 1);
1215 memset(tmp, 0, ctx->len + 1);
1216 memcpy(tmp, ctx->buffer, ctx->len);
1218 chxj_convert_image(r,
1219 (const char **)&tmp,
1220 (apr_size_t *)&ctx->len);
1221 if (ctx->buffer == NULL) {
1227 apr_table_unset(r->headers_out, "Content-Length");
1228 apr_table_unset(r->err_headers_out, "Content-Length");
1229 ap_set_content_length(r, (apr_off_t)ctx->len);
1233 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1234 s_add_cookie_id_if_has_location_header(r, cookie);
1235 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1236 if (! ap_is_HTTP_REDIRECT(r->status)) {
1237 r->status = HTTP_MOVED_TEMPORARILY;
1240 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1241 ctx->buffer = apr_pstrdup(pool, "");
1243 ap_set_content_length(r, (apr_off_t)ctx->len);
1245 chxj_cookie_unlock(r,lock);
1246 s_add_no_cache_headers(r, entryp);
1247 rv = pass_data_to_filter(f,
1248 (const char *)ctx->buffer,
1249 (apr_size_t)ctx->len);
1252 ctx->buffer = apr_pstrdup(pool, "");
1254 ap_set_content_length(r, (apr_off_t)ctx->len);
1255 chxj_cookie_unlock(r, lock);
1256 s_add_no_cache_headers(r, entryp);
1257 rv = pass_data_to_filter(f,
1258 (const char *)ctx->buffer,
1259 (apr_size_t)ctx->len);
1261 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1265 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1270 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1271 cookie_lock_t *lock = NULL;
1272 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1273 switch(spec->html_spec_type) {
1274 case CHXJ_SPEC_Chtml_1_0:
1275 case CHXJ_SPEC_Chtml_2_0:
1276 case CHXJ_SPEC_Chtml_3_0:
1277 case CHXJ_SPEC_Chtml_4_0:
1278 case CHXJ_SPEC_Chtml_5_0:
1279 case CHXJ_SPEC_Chtml_6_0:
1280 case CHXJ_SPEC_Chtml_7_0:
1281 case CHXJ_SPEC_XHtml_Mobile_1_0:
1282 case CHXJ_SPEC_Jhtml:
1283 lock = chxj_cookie_lock(r);
1284 cookie = chxj_save_cookie(r);
1286 * Location Header Check to add cookie parameter.
1288 s_add_cookie_id_if_has_location_header(r, cookie);
1289 chxj_cookie_unlock(r, lock);
1290 apr_table_unset(r->headers_out, "Set-Cookie");
1291 apr_table_unset(r->err_headers_out, "Set-Cookie");
1298 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1299 if (! ap_is_HTTP_REDIRECT(r->status)) {
1300 r->status = HTTP_MOVED_TEMPORARILY;
1303 apr_table_setn(r->headers_out, "Content-Length", "0");
1304 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1305 s_add_no_cache_headers(r, entryp);
1306 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1307 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1312 apr_brigade_destroy(bb);
1314 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1320 * Add Cookie_id if it has location header.
1323 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1325 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1326 if (! location_header) {
1327 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1329 if (cookie && location_header) {
1330 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1331 location_header = chxj_add_cookie_parameter(r,
1334 apr_table_unset(r->headers_out, "Location");
1335 apr_table_setn(r->headers_out, "Location", location_header);
1336 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1337 if (!ap_is_HTTP_REDIRECT(r->status)) {
1338 r->status = HTTP_MOVED_TEMPORARILY;
1344 * It is the main loop of the input filter handler.
1348 chxj_input_handler(request_rec *r)
1350 mod_chxj_config *dconf;
1351 chxjconvrule_entry *entryp = NULL;
1352 device_table *spec = NULL;
1353 char *post_data = NULL;
1354 apr_size_t post_data_len = 0;
1359 int response_code = 0;
1361 DBG(r, "start of chxj_input_handler()");
1363 if (strcasecmp(r->handler, "chxj-input-handler")) {
1364 DBG(r, "end chxj_input_handler()");
1367 apr_pool_create(&pool, r->pool);
1369 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1370 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1372 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1374 spec = chxj_specified_device(r, user_agent);
1375 entryp = chxj_apply_convrule(r, dconf->convrules);
1377 post_data = apr_pstrdup(pool, "");
1378 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1379 if (ap_should_client_block(r)) {
1380 while (post_data_len < CHXJ_POST_MAX) {
1383 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1384 if (read_bytes<=0) {
1387 buffer[read_bytes] = '\0';
1388 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1389 post_data_len += read_bytes;
1398 if (post_data_len > 0) {
1399 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1400 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1404 if (dconf->forward_url_base) {
1405 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1408 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);
1411 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1413 DBG(r, "==> new url_path:[%s]", url_path);
1416 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1417 apr_table_unset(r->headers_in, "Content-Length");
1418 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1419 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1420 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1421 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1422 for (ii=0; ii<res_len/64; ii++) {
1423 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1425 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1428 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1429 if (strcasecmp(chunked, "chunked") == 0) {
1431 apr_table_unset(r->headers_out, "Transfer-Encoding");
1434 if (ap_is_HTTP_ERROR(response_code)) {
1435 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1436 return response_code;
1440 apr_pool_create(&wpool, r->pool);
1441 apr_bucket_brigade *bb;
1444 conn_rec *c = r->connection;
1446 bb = apr_brigade_create(wpool, c->bucket_alloc);
1447 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1448 APR_BRIGADE_INSERT_TAIL(bb, e);
1449 e = apr_bucket_eos_create(c->bucket_alloc);
1450 APR_BRIGADE_INSERT_TAIL(bb, e);
1451 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1452 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1455 apr_brigade_cleanup(bb);
1458 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1462 static mod_chxj_global_config *
1463 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1465 mod_chxj_global_config *conf;
1467 SDBG(s, "start chxj_global_config_create()");
1469 /*--------------------------------------------------------------------------*/
1470 /* allocate an own subpool which survives server restarts */
1471 /*--------------------------------------------------------------------------*/
1472 conf = (mod_chxj_global_config *)apr_palloc(pool,
1473 sizeof(mod_chxj_global_config));
1475 conf->cookie_db_lock = NULL;
1477 SDBG(s, "end chxj_global_config_create()");
1484 * initialize chxj module
1487 chxj_init_module(apr_pool_t *p,
1488 apr_pool_t *UNUSED(plog),
1489 apr_pool_t *UNUSED(ptemp),
1495 SDBG(s, "start chxj_init_module()");
1497 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1499 if (user_data == NULL) {
1502 * dummy user_data set.
1504 apr_pool_userdata_set(
1506 CHXJ_MOD_CONFIG_KEY,
1507 apr_pool_cleanup_null,
1509 SDBG(s, "end chxj_init_module()");
1513 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1515 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1517 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1518 return HTTP_INTERNAL_SERVER_ERROR;
1521 SDBG(s, "end chxj_init_module()");
1528 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1531 SDBG(s, "start chxj_child_init()");
1532 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1534 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1536 SDBG(s, "end chxj_child_init()");
1541 * A set structure of each server is generated.
1547 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1549 mod_chxj_global_config *gc;
1551 gc = chxj_global_config_create(p,s);
1558 chxj_translate_name(request_rec *r)
1560 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1561 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1562 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1563 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1564 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1565 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1567 return chxj_trans_name(r);
1575 chxj_insert_filter(request_rec *r)
1579 mod_chxj_config *dconf;
1580 chxjconvrule_entry *entryp;
1585 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1587 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1589 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1590 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1592 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1595 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1597 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1598 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1599 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1603 spec = chxj_specified_device(r, user_agent);
1604 entryp = chxj_apply_convrule(r, dconf->convrules);
1606 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1609 ctx = apr_palloc(r->pool, sizeof(*ctx));
1610 memset(ctx, 0, sizeof(*ctx));
1611 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1612 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1613 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1616 ctx->entryp = entryp;
1618 ctx->buffer = apr_palloc(ctx->pool, 1);
1621 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1622 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1623 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1627 switch(spec->html_spec_type) {
1628 case CHXJ_SPEC_Chtml_1_0:
1629 case CHXJ_SPEC_Chtml_2_0:
1630 case CHXJ_SPEC_Chtml_3_0:
1631 case CHXJ_SPEC_Chtml_4_0:
1632 case CHXJ_SPEC_Chtml_5_0:
1633 case CHXJ_SPEC_Chtml_6_0:
1634 case CHXJ_SPEC_Chtml_7_0:
1635 case CHXJ_SPEC_XHtml_Mobile_1_0:
1636 case CHXJ_SPEC_Hdml:
1637 case CHXJ_SPEC_Jhtml:
1638 case CHXJ_SPEC_Jxhtml:
1642 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1647 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1648 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1649 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1652 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1657 * The hook is registered.
1662 chxj_register_hooks(apr_pool_t *UNUSED(p))
1664 ap_hook_post_config(chxj_init_module,
1667 APR_HOOK_REALLY_FIRST);
1668 ap_hook_child_init(chxj_child_init,
1671 APR_HOOK_REALLY_FIRST);
1672 ap_register_output_filter (
1673 "chxj_output_filter",
1677 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1678 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1679 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1680 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1681 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1682 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1687 * A set structure according to the directory is generated.
1693 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1695 mod_chxj_config *conf;
1697 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1698 conf->device_data_file = NULL;
1699 conf->devices = NULL;
1700 conf->emoji_data_file = NULL;
1702 conf->emoji_tail = NULL;
1703 conf->image = CHXJ_IMG_NONE;
1704 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1705 conf->image_cache_limit = 0;
1706 conf->server_side_encoding = NULL;
1707 conf->cookie_db_dir = NULL;
1708 conf->cookie_timeout = 0;
1709 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1710 conf->cookie_lazy_mode = 0;
1711 #if defined(USE_MYSQL_COOKIE)
1712 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1713 conf->mysql.port = MYSQL_PORT;
1714 conf->mysql.host = NULL;
1716 #if defined(USE_MEMCACHE_COOKIE)
1717 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1718 conf->memcache.host = NULL;
1719 conf->memcache.port = 0;
1721 conf->forward_url_base = NULL;
1722 conf->forward_server_ip = NULL;
1723 conf->allowed_cookie_domain = NULL;
1724 conf->post_log = NULL;
1730 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1731 memset(conf->dir, 0, strlen(arg)+1);
1732 strcpy(conf->dir, arg);
1734 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1736 /* Default is copyleft */
1737 conf->image_copyright = NULL;
1744 * Merge per-directory CHXJ configurations
1747 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1749 mod_chxj_config *base;
1750 mod_chxj_config *add;
1751 mod_chxj_config *mrg;
1753 base = (mod_chxj_config *)basev;
1754 add = (mod_chxj_config *)addv;
1755 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1757 mrg->device_data_file = NULL;
1758 mrg->devices = NULL;
1759 mrg->emoji_data_file = NULL;
1760 mrg->image = CHXJ_IMG_NONE;
1761 mrg->image_cache_dir = NULL;
1762 mrg->image_copyright = NULL;
1763 mrg->image_cache_limit = 0;
1765 mrg->emoji_tail = NULL;
1766 mrg->new_line_type = NLTYPE_NIL;
1767 mrg->forward_url_base = NULL;
1768 mrg->forward_server_ip = NULL;
1769 mrg->allowed_cookie_domain = NULL;
1770 mrg->post_log = NULL;
1772 mrg->dir = apr_pstrdup(p, add->dir);
1774 if (! add->device_data_file) {
1775 mrg->devices = base->devices;
1776 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1779 mrg->devices = add->devices;
1780 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1783 if (! add->emoji_data_file) {
1784 mrg->emoji = base->emoji;
1785 mrg->emoji_tail = base->emoji_tail;
1786 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1789 mrg->emoji = add->emoji;
1790 mrg->emoji_tail = add->emoji_tail;
1791 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1794 if (add->image == CHXJ_IMG_NONE) {
1795 mrg->image = base->image;
1798 mrg->image = add->image;
1801 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1802 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1805 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1808 if (add->image_cache_limit) {
1809 mrg->image_cache_limit = add->image_cache_limit;
1812 mrg->image_cache_limit = base->image_cache_limit;
1815 if (add->image_copyright)
1816 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1818 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1820 if (add->server_side_encoding) {
1821 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1824 if (base->server_side_encoding) {
1825 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1828 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1831 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1833 if (add->cookie_db_dir) {
1834 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1837 if (base->cookie_db_dir) {
1838 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1841 mrg->cookie_db_dir = NULL;
1844 if (add->cookie_timeout) {
1845 mrg->cookie_timeout = add->cookie_timeout;
1848 if (base->cookie_db_dir) {
1849 mrg->cookie_timeout = base->cookie_timeout;
1852 mrg->cookie_timeout = 0;
1855 #if defined(USE_MYSQL_COOKIE)
1856 if (add->mysql.host) {
1857 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1859 else if (base->mysql.host) {
1860 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1863 mrg->mysql.host = NULL;
1865 if (add->mysql.port) {
1866 mrg->mysql.port = add->mysql.port;
1868 else if (base->mysql.port) {
1869 mrg->mysql.port = base->mysql.port;
1872 mrg->mysql.port = 0;
1875 if (add->mysql.database) {
1876 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1878 else if (base->mysql.database) {
1879 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1882 mrg->mysql.database = NULL;
1885 if (add->mysql.username) {
1886 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1888 else if (base->mysql.username) {
1889 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1892 mrg->mysql.username = NULL;
1895 if (add->mysql.password) {
1896 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1898 else if (base->mysql.password) {
1899 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1902 mrg->mysql.password = NULL;
1905 if (add->mysql.tablename) {
1906 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1908 else if (base->mysql.tablename) {
1909 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1912 mrg->mysql.tablename = NULL;
1915 if (add->mysql.socket_path) {
1916 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1918 else if (base->mysql.socket_path) {
1919 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1922 mrg->mysql.socket_path = NULL;
1925 if (add->mysql.charset) {
1926 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1928 else if (base->mysql.charset) {
1929 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1932 mrg->mysql.charset = NULL;
1935 #if defined(USE_MEMCACHE_COOKIE)
1936 if (add->memcache.host) {
1937 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1939 else if (base->memcache.host) {
1940 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1943 mrg->memcache.host = NULL;
1945 if (add->memcache.port) {
1946 mrg->memcache.port = add->memcache.port;
1948 else if (base->memcache.port) {
1949 mrg->memcache.port = base->memcache.port;
1952 mrg->memcache.port = 0;
1955 if (add->cookie_store_type) {
1956 mrg->cookie_store_type = add->cookie_store_type;
1958 else if (base->cookie_store_type) {
1959 mrg->cookie_store_type = base->cookie_store_type;
1962 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1964 if (add->cookie_lazy_mode) {
1965 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1967 else if (base->cookie_lazy_mode) {
1968 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1971 mrg->cookie_lazy_mode = 0;
1973 if (add->new_line_type) {
1974 mrg->new_line_type = add->new_line_type;
1976 else if (base->new_line_type) {
1977 mrg->new_line_type = base->new_line_type;
1980 mrg->new_line_type = NLTYPE_NIL;
1983 if (add->forward_url_base) {
1984 mrg->forward_url_base = add->forward_url_base;
1986 else if (base->forward_url_base) {
1987 mrg->forward_url_base = base->forward_url_base;
1990 if (add->allowed_cookie_domain) {
1991 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
1994 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
1996 if (add->post_log) {
1997 mrg->post_log = add->post_log;
2000 mrg->post_log = base->post_log;
2007 chxj_command_parse_take5(
2020 for (;*strp == ' '||*strp == '\t'; strp++) ;
2030 for (; *strp != '\0'; strp++) {
2031 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2032 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2037 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2038 || (isquoted && *strp == '"'))
2052 for (;*strp == ' '||*strp == '\t'; strp++) ;
2061 for (; *strp != '\0'; strp++) {
2062 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2063 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2068 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2069 || (isquoted && *strp == '"'))
2082 for (;*strp == ' '||*strp == '\t'; strp++);
2090 for (; *strp != '\0'; strp++) {
2091 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2092 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2097 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2098 || (isquoted && *strp == '"'))
2110 for (;*strp == ' '||*strp == '\t'; strp++);
2118 for (; *strp != '\0'; strp++) {
2119 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2120 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2125 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2126 || (isquoted && *strp == '"'))
2137 for (;*strp == ' '||*strp == '\t'; strp++);
2145 for (; *strp != '\0'; strp++) {
2146 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2147 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2152 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2153 || (isquoted && *strp == '"'))
2163 * The device definition file is loaded.
2165 * @param arg [i] The name of the device definition file is specified.
2166 * @param mconfig [i/o] The pointer to a set structure is specified.
2170 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2172 mod_chxj_config *conf;
2177 if (strlen(arg) > 256)
2178 return "mod_chxj: device data filename too long.";
2180 conf = (mod_chxj_config *)mconfig;
2181 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2183 qs_init_malloc(&doc);
2184 qs_init_root_node(&doc);
2186 qs_parse_file((Doc *)&doc, (const char *)arg);
2187 chxj_load_device_data(&doc,parms->pool, conf);
2188 qs_all_free(&doc, QX_LOGMARK);
2195 * Device definition information is loaded.
2198 * @param arg [i] The name of the device definition file is specified.
2199 * @param mconfig [i/o] The pointer to a set structure is specified.
2203 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2205 mod_chxj_config *conf;
2212 if (strlen(arg) > 256)
2213 return "mod_chxj: emoji data filename too long.";
2215 conf = (mod_chxj_config *)mconfig;
2216 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2217 qs_init_malloc(&doc);
2218 qs_init_root_node(&doc);
2220 qs_parse_file((Doc *)&doc, (const char *)arg);
2222 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2224 qs_all_free(&doc, QX_LOGMARK);
2232 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2234 mod_chxj_config *conf;
2239 if (strlen(arg) > 256)
2240 return "image uri is too long.";
2242 conf = (mod_chxj_config*)mconfig;
2243 if (strcasecmp("ON", arg) == 0) {
2244 conf->image = CHXJ_IMG_ON;
2247 conf->image = CHXJ_IMG_OFF;
2255 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2257 mod_chxj_config *conf;
2262 if (strlen(arg) > 256)
2263 return "cache dir name is too long.";
2265 conf = (mod_chxj_config *)mconfig;
2266 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2273 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2275 mod_chxj_config *conf;
2280 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2281 return "cache size is too long.";
2283 conf = (mod_chxj_config *)mconfig;
2286 * I use strtol function because strtoul is not portable function.
2288 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2291 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2293 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2302 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2304 mod_chxj_config *conf;
2309 if (strlen(arg) > 256)
2310 return "Copyright Flag is too long.";
2312 conf = (mod_chxj_config *)mconfig;
2313 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2320 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2324 mod_chxj_config *dconf;
2325 chxjconvrule_entry *newrule;
2335 dconf = (mod_chxj_config *)mconfig;
2337 if (strlen(arg) > 4096)
2338 return "mod_chxj: ChxjConvertRule: is too long.";
2340 dconf = (mod_chxj_config *)mconfig;
2341 if (dconf->convrules == NULL)
2342 dconf->convrules = apr_array_make(cmd->pool,
2344 sizeof(chxjconvrule_entry));
2346 newrule = apr_array_push(dconf->convrules);
2349 newrule->action = 0;
2351 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2352 return "ChxjConvertRule: bad argument line";
2354 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2358 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2364 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2365 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2368 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2369 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2372 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2373 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2376 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2377 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2383 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2384 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2390 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2391 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2397 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2398 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2404 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2405 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2411 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2412 newrule->action |= CONVRULE_Z2H_ON_BIT;
2415 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2416 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2419 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2420 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2423 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2424 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2427 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2428 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2431 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2432 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2435 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2436 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2439 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2440 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2451 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2455 mode = AP_REG_EXTENDED;
2456 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2457 return "RewriteRule: cannot compile regular expression ";
2459 newrule->regexp = regexp;
2461 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2463 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2465 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2467 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2468 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2470 newrule->user_agent = NULL;
2472 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2484 mod_chxj_config *dconf;
2487 if (strlen(arg) > 4096)
2488 return "mod_chxj: ChxjCookieDir is too long.";
2490 dconf = (mod_chxj_config *)mconfig;
2492 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2499 cmd_set_cookie_timeout(
2500 cmd_parms *UNUSED(cmd),
2504 mod_chxj_config *dconf;
2506 if (strlen(arg) > 4096)
2507 return "mod_chxj: ChxjCookieTimeout is too long.";
2509 if (chxj_chk_numeric(arg) != 0)
2510 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2512 dconf = (mod_chxj_config *)mconfig;
2514 dconf->cookie_timeout = atoi(arg);
2520 #if defined(USE_MYSQL_COOKIE)
2522 cmd_set_cookie_mysql_database(
2527 mod_chxj_config *dconf;
2529 if (strlen(arg) > 255)
2530 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2532 dconf = (mod_chxj_config *)mconfig;
2534 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2541 cmd_set_cookie_mysql_username(
2546 mod_chxj_config *dconf;
2548 if (strlen(arg) > 255)
2549 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2551 dconf = (mod_chxj_config *)mconfig;
2553 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2560 cmd_set_cookie_mysql_password(
2565 mod_chxj_config *dconf;
2567 if (strlen(arg) > 255)
2568 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2570 dconf = (mod_chxj_config *)mconfig;
2572 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2579 cmd_set_cookie_mysql_table_name(
2584 mod_chxj_config *dconf;
2586 if (strlen(arg) > 255)
2587 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2589 dconf = (mod_chxj_config *)mconfig;
2591 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2597 cmd_set_cookie_mysql_port(
2598 cmd_parms *UNUSED(cmd),
2602 mod_chxj_config *dconf;
2604 if (strlen(arg) > 255)
2605 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2607 dconf = (mod_chxj_config *)mconfig;
2609 if (chxj_chk_numeric(arg) != 0)
2610 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2612 dconf = (mod_chxj_config *)mconfig;
2614 dconf->mysql.port = chxj_atoi(arg);
2621 cmd_set_cookie_mysql_host(
2626 mod_chxj_config *dconf;
2628 if (strlen(arg) > 255)
2629 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2631 dconf = (mod_chxj_config *)mconfig;
2633 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2640 cmd_set_cookie_mysql_socket_path(
2645 mod_chxj_config *dconf;
2647 if (strlen(arg) > 4096)
2648 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2650 dconf = (mod_chxj_config *)mconfig;
2652 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2659 cmd_set_cookie_mysql_charset(
2664 mod_chxj_config *dconf;
2666 if (strlen(arg) > 255)
2667 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2669 dconf = (mod_chxj_config *)mconfig;
2671 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2676 #if defined(USE_MEMCACHE_COOKIE)
2678 cmd_set_cookie_memcache_port(
2679 cmd_parms *UNUSED(cmd),
2683 mod_chxj_config *dconf;
2685 if (strlen(arg) > 255)
2686 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2688 dconf = (mod_chxj_config *)mconfig;
2690 if (chxj_chk_numeric(arg) != 0)
2691 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2693 dconf = (mod_chxj_config *)mconfig;
2695 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2702 cmd_set_cookie_memcache_host(
2707 mod_chxj_config *dconf;
2709 if (strlen(arg) > 255)
2710 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2712 dconf = (mod_chxj_config *)mconfig;
2714 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2721 cmd_set_cookie_lazy_mode(
2722 cmd_parms *UNUSED(cmd),
2726 mod_chxj_config *dconf;
2728 if (strlen(arg) > 255)
2729 return "mod_chxj: ChxjCookieLazyMode is too long.";
2731 dconf = (mod_chxj_config *)mconfig;
2733 if (strcasecmp("TRUE",arg) == 0) {
2734 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2737 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2744 cmd_set_cookie_store_type(
2745 cmd_parms *UNUSED(cmd),
2749 mod_chxj_config *dconf;
2751 if (strlen(arg) > 255)
2752 return "mod_chxj: ChxjCookieStoreType is too long.";
2754 dconf = (mod_chxj_config *)mconfig;
2756 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2757 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2759 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2760 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2762 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2763 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2766 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2773 cmd_set_forward_url_base(
2778 mod_chxj_config *dconf;
2780 if (strlen(arg) > 255)
2781 return "mod_chxj: ChxjForwardUrlBase is too long.";
2783 dconf = (mod_chxj_config *)mconfig;
2785 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2791 cmd_set_forward_server_ip(
2796 mod_chxj_config *dconf;
2798 if (strlen(arg) > 255)
2799 return "mod_chxj: ChxjForwardServerIp is too long.";
2801 dconf = (mod_chxj_config *)mconfig;
2803 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2809 cmd_allowed_cookie_domain(
2814 mod_chxj_config *dconf;
2816 if (strlen(arg) > 255)
2817 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2819 dconf = (mod_chxj_config *)mconfig;
2821 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2827 cmd_set_new_line_type(
2828 cmd_parms *UNUSED(cmd),
2832 mod_chxj_config *dconf;
2833 if (strlen(arg) > 255)
2834 return "mod_chxj: ChxjNewLineType is too long.";
2836 dconf = (mod_chxj_config *)mconfig;
2838 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2839 dconf->new_line_type = NLTYPE_CRLF;
2841 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2842 dconf->new_line_type = NLTYPE_LF;
2844 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2845 dconf->new_line_type = NLTYPE_CR;
2847 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2848 dconf->new_line_type = NLTYPE_NONE;
2851 return "mod_chxj: invalid value (ChxjNewLineType)";
2862 mod_chxj_config *dconf;
2863 if (strlen(arg) > 255)
2864 return "mod_chxj: ChxjPostLogEnv is too long.";
2866 dconf = (mod_chxj_config *)mconfig;
2868 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2874 static const command_rec cmds[] = {
2876 "ChxjLoadDeviceData",
2877 cmd_load_device_data,
2880 "Load Device Data"),
2882 "ChxjLoadEmojiData",
2883 cmd_load_emoji_data,
2889 cmd_set_image_engine,
2892 "Convert Target URI"),
2894 "ChxjImageCacheDir",
2895 cmd_set_image_cache_dir,
2898 "Image Cache Directory"),
2900 "ChxjImageCacheLimit",
2901 cmd_set_image_cache_limit,
2904 "Image Cache Limit"),
2906 "ChxjImageCopyright",
2907 cmd_set_image_copyright,
2916 "an URL-applied regexp-pattern and a substitution URL"),
2922 "save cookie.db directory."),
2924 "ChxjCookieTimeout",
2925 cmd_set_cookie_timeout,
2928 "The compulsion time-out time of the cookie is specified. "),
2930 "ChxjCookieStoreType",
2931 cmd_set_cookie_store_type,
2934 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2936 "ChxjCookieLazyMode",
2937 cmd_set_cookie_lazy_mode,
2940 "OneTimeID is negligently done. (TRUE/FALSE)"),
2941 #if defined(USE_MYSQL_COOKIE)
2943 "ChxjCookieMysqlHost",
2944 cmd_set_cookie_mysql_host,
2947 "The MySQL database host used by saving Cookie"),
2949 "ChxjCookieMysqlPort",
2950 cmd_set_cookie_mysql_port,
2953 "The MySQL database port used by saving Cookie"),
2955 "ChxjCookieMysqlDatabase",
2956 cmd_set_cookie_mysql_database,
2959 "The MySQL database name used by saving Cookie"),
2961 "ChxjCookieMysqlUsername",
2962 cmd_set_cookie_mysql_username,
2965 "The MySQL username used by saving Cookie"),
2967 "ChxjCookieMysqlPassword",
2968 cmd_set_cookie_mysql_password,
2971 "The MySQL password used by saving Cookie"),
2973 "ChxjCookieMysqlTableName",
2974 cmd_set_cookie_mysql_table_name,
2977 "The MySQL table name used by saving Cookie"),
2979 "ChxjCookieMysqlSocketPath",
2980 cmd_set_cookie_mysql_socket_path,
2983 "The MySQL socket path used by saving Cookie"),
2985 "ChxjCookieMysqlCharset",
2986 cmd_set_cookie_mysql_charset,
2989 "The MySQL charset used by saving Cookie"),
2991 #if defined(USE_MEMCACHE_COOKIE)
2993 "ChxjCookieMemcacheHost",
2994 cmd_set_cookie_memcache_host,
2997 "The Memcached host used by saving Cookie"),
2999 "ChxjCookieMemcachePort",
3000 cmd_set_cookie_memcache_port,
3003 "The Memcached port used by saving Cookie"),
3007 cmd_set_new_line_type,
3010 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3012 "ChxjForwardUrlBase",
3013 cmd_set_forward_url_base,
3016 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3018 "ChxjForwardServerIp",
3019 cmd_set_forward_server_ip,
3022 "The forward server ip(default: this server ip)"),
3024 "ChxjAllowedCookieDomain",
3025 cmd_allowed_cookie_domain,
3028 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3034 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3039 /*----------------------------------------------------------------------------*/
3040 /* Dispatch list for API hooks */
3041 /*----------------------------------------------------------------------------*/
3042 module AP_MODULE_DECLARE_DATA chxj_module = {
3043 STANDARD20_MODULE_STUFF,
3044 chxj_create_per_dir_config, /* create per-dir config structures */
3045 chxj_merge_per_dir_config, /* merge per-dir config structures */
3046 chxj_config_server_create, /* create per-server config structures */
3047 NULL, /* merge per-server config structures */
3048 cmds, /* table of config file commands */
3049 chxj_register_hooks /* register hooks */