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_header_inf.h"
72 #include "chxj_jreserved_tag.h"
75 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
76 #define CHXJ_VERSION PACKAGE_VERSION
77 #define CHXJ_POST_MAX (0x1000000)
79 converter_t convert_routine[] = {
81 /* CHXJ_SPEC_UNKNOWN */
84 .emoji_only_converter = NULL,
87 /* CHXJ_SPEC_Chtml_1_0 */
88 .converter = chxj_convert_chtml10,
89 .encoder = chxj_encoding,
90 .emoji_only_converter = chxj_chtml10_emoji_only_converter,
93 /* CHXJ_SPEC_Chtml_2_0 */
94 .converter = chxj_convert_chtml20,
95 .encoder = chxj_encoding,
96 .emoji_only_converter = chxj_chtml20_emoji_only_converter,
99 /* CHXJ_SPEC_Chtml_3_0 */
100 .converter = chxj_convert_chtml30,
101 .encoder = chxj_encoding,
102 .emoji_only_converter = chxj_chtml30_emoji_only_converter,
105 /* CHXJ_SPEC_Chtml_4_0 */
106 .converter = chxj_convert_chtml40,
107 .encoder = chxj_encoding,
108 .emoji_only_converter = chxj_chtml40_emoji_only_converter,
111 /* CHXJ_SPEC_Chtml_5_0 */
112 .converter = chxj_convert_chtml50,
113 .encoder = chxj_encoding,
114 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
117 /* CHXJ_SPEC_Chtml_6_0 */
118 .converter = chxj_convert_chtml50,
119 .encoder = chxj_encoding,
120 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
123 /* CHXJ_SPEC_Chtml_7_0 */
124 .converter = chxj_convert_chtml50,
125 .encoder = chxj_encoding,
126 .emoji_only_converter = chxj_chtml50_emoji_only_converter,
129 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
130 .converter = chxj_convert_xhtml_mobile_1_0,
131 .encoder = chxj_encoding,
132 .emoji_only_converter = chxj_xhtml_emoji_only_converter,
136 .converter = chxj_convert_hdml,
137 .encoder = chxj_encoding,
138 .emoji_only_converter = NULL,
141 /* CHXJ_SPEC_Jhtml */
142 .converter = chxj_convert_jhtml,
143 .encoder = chxj_encoding,
144 .emoji_only_converter = chxj_jhtml_emoji_only_converter,
147 /* CHXJ_SPEC_Jxhtml */
148 .converter = chxj_convert_jxhtml,
149 .encoder = chxj_encoding,
150 .emoji_only_converter = chxj_jxhtml_emoji_only_converter,
156 .emoji_only_converter = NULL,
160 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp, device_table *spec);
161 static void s_convert_guid_parameter_to_header(request_rec *r, const char *param, device_table *spec);
162 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
163 static void s_clear_cookie_header(request_rec *r, device_table *spec);
164 static void s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp);
167 * Only when User-Agent is specified, the User-Agent header is camouflaged.
172 chxj_headers_fixup(request_rec *r)
174 mod_chxj_config* dconf;
175 chxjconvrule_entry* entryp;
181 DBG(r, "REQ[%X] start chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
183 DBG(r, "REQ[%X] detect internal redirect.", (unsigned int)(apr_size_t)r);
184 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
188 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
190 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
191 spec = chxj_specified_device(r, user_agent);
193 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
195 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
196 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (unsigned int)(apr_size_t)r);
197 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
202 switch(spec->html_spec_type) {
203 case CHXJ_SPEC_Chtml_1_0:
204 case CHXJ_SPEC_Chtml_2_0:
205 case CHXJ_SPEC_Chtml_3_0:
206 case CHXJ_SPEC_Chtml_4_0:
207 case CHXJ_SPEC_Chtml_5_0:
208 case CHXJ_SPEC_Chtml_6_0:
209 case CHXJ_SPEC_Chtml_7_0:
210 case CHXJ_SPEC_XHtml_Mobile_1_0:
212 case CHXJ_SPEC_Jhtml:
213 case CHXJ_SPEC_Jxhtml:
214 entryp = chxj_apply_convrule(r, dconf->convrules);
216 DBG(r, "REQ[%X] end chxj_headers_fixup() (no pattern)", (unsigned int)(apr_size_t)r);
219 if (!(entryp->action & CONVRULE_ENGINE_ON_BIT) && !(entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
220 DBG(r, "REQ[%X] end chxj_headers_fixup() (engine off)", (unsigned int)(apr_size_t)r);
223 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
224 DBG(r, "REQ[%X] end chxj_headers_fixup() (emoji only)", (unsigned int)(apr_size_t)r);
228 apr_table_setn(r->headers_in,
229 CHXJ_HTTP_USER_AGENT,
232 if (entryp->user_agent)
233 apr_table_setn(r->headers_in,
237 * this clear process must do before chxj_convert_input_header function.
239 if ((entryp->action & CONVRULE_COOKIE_ON_BIT) || (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
240 if (r->method_number == M_POST) {
241 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
242 s_clear_cookie_header(r, spec);
246 s_clear_cookie_header(r, spec);
250 chxj_convert_input_header(r,entryp, spec);
255 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);
261 if (r->method_number == M_POST) {
262 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
263 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);
264 r->proxyreq = PROXYREQ_NONE;
265 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
268 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
270 if (dconf->post_log) {
271 apr_table_setn(r->subprocess_env, dconf->post_log, dconf->post_log);
274 apr_table_setn(r->subprocess_env, "chxj-post-log", "chxj-post-log");
276 apr_sockaddr_t *address = NULL;
277 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
278 if (rv != APR_SUCCESS) {
280 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));
281 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
285 if (dconf->forward_server_ip) {
286 addr = dconf->forward_server_ip;
289 apr_sockaddr_ip_get(&addr, address);
291 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);
292 if (strcmp(addr, r->connection->remote_ip) == 0) {
293 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
295 if (! apr_table_get(r->headers_in, "Content-Length")) {
296 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
298 apr_table_set(r->headers_in, "Content-Length", contentLength);
305 chxj_add_device_env(r, spec);
307 DBG(r, "REQ[%X] end chxj_headers_fixup()", (unsigned int)(apr_size_t)r);
314 s_clear_cookie_header(request_rec *r, device_table *spec)
316 DBG(r, "REQ[%X] start s_clear_cookie_header()", TO_ADDR(r));
317 switch(spec->html_spec_type) {
318 case CHXJ_SPEC_Chtml_1_0:
319 case CHXJ_SPEC_Chtml_2_0:
320 case CHXJ_SPEC_Chtml_3_0:
321 case CHXJ_SPEC_Chtml_4_0:
322 case CHXJ_SPEC_Chtml_5_0:
323 case CHXJ_SPEC_Chtml_6_0:
324 case CHXJ_SPEC_Chtml_7_0:
325 case CHXJ_SPEC_XHtml_Mobile_1_0:
326 case CHXJ_SPEC_Jhtml:
327 apr_table_unset(r->headers_in, "Cookie");
332 DBG(r, "REQ[%X] end s_clear_cookie_header()", TO_ADDR(r));
337 * It converts it from CHTML into XXML corresponding to each model.
340 * @param src [i] It is former HTML character string.
341 * @param len [i/o] It is length of former HTML character string.
344 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
350 mod_chxj_config *dconf;
351 chxjconvrule_entry *entryp;
353 DBG(r,"REQ[%X] start of chxj_convert() input:[%.*s]", (unsigned int)(apr_size_t)r, (int)*len, *src);
354 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
356 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
359 entryp = chxj_apply_convrule(r, dconf->convrules);
361 if (!entryp || (!(entryp->action & CONVRULE_ENGINE_ON_BIT) && !(entryp->action & CONVRULE_COOKIE_ONLY_BIT))) {
362 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
367 /*------------------------------------------------------------------------*/
368 /* get UserAgent from http header */
369 /*------------------------------------------------------------------------*/
370 if (entryp->user_agent)
371 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
373 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
375 DBG(r,"REQ[%X] User-Agent:[%s]", (unsigned int)(apr_size_t)r, user_agent);
376 DBG(r,"REQ[%X] content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
379 if ( ! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
380 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
381 DBG(r,"REQ[%X] no convert. content type is %s", (unsigned int)(apr_size_t)r, r->content_type);
382 DBG(r,"REQ[%X] end of chxj_convert()", (unsigned int)(apr_size_t)r);
386 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
388 spec = chxj_specified_device(r, user_agent);
395 if ((entryp->action & CONVRULE_COOKIE_ON_BIT
396 && !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
397 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT))
398 || (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
399 switch(spec->html_spec_type) {
400 case CHXJ_SPEC_Chtml_1_0:
401 case CHXJ_SPEC_Chtml_2_0:
402 case CHXJ_SPEC_Chtml_3_0:
403 case CHXJ_SPEC_Chtml_4_0:
404 case CHXJ_SPEC_Chtml_5_0:
405 case CHXJ_SPEC_Chtml_6_0:
406 case CHXJ_SPEC_Chtml_7_0:
407 case CHXJ_SPEC_XHtml_Mobile_1_0:
408 case CHXJ_SPEC_Jhtml:
409 cookie = chxj_save_cookie(r);
416 if (!r->header_only) {
417 if ((entryp->action & CONVRULE_COOKIE_ONLY_BIT) && cookie) {
418 dst = chxj_cookie_only_mode(r, *src, (apr_size_t *)len, cookie);
422 if (convert_routine[spec->html_spec_type].encoder)
423 tmp = convert_routine[spec->html_spec_type].encoder(r,
427 if (entryp->action & CONVRULE_EMOJI_ONLY_BIT) {
429 tmp = chxj_node_convert_chxjif_only(r, spec, (const char*)tmp, (apr_size_t *)len);
432 tmp = chxj_node_convert_chxjif_only(r, spec, (const char *)*src, (apr_size_t *)len);
434 if (convert_routine[spec->html_spec_type].emoji_only_converter) {
435 dst = convert_routine[spec->html_spec_type].emoji_only_converter(r,spec, tmp,*len);
440 dst = apr_palloc(r->pool, 1);
445 DBG(r, "REQ[%X] end of chxj_convert()(emoji only)", (unsigned int)(apr_size_t)r);
448 if ( !(entryp->action & CONVRULE_EMOJI_ONLY_BIT)
449 && !(entryp->action & CONVRULE_ENVINFO_ONLY_BIT)) {
450 if (convert_routine[spec->html_spec_type].converter) {
452 dst = convert_routine[spec->html_spec_type].converter(r,
460 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;
727 DBG(r, "REQ[%X] start of chxj_input_convert()", (unsigned int)(apr_size_t)r);
730 DBG(r, "REQ[%X] end of chxj_input_convert() (input is null)", (unsigned int)(apr_size_t)r);
731 return apr_pstrdup(r->pool, "");
734 apr_pool_create(&pool, r->pool);
736 s = apr_pstrdup(pool, *src);
738 buff_pre = apr_pstrdup(pool, *src);
740 result = qs_alloc_zero_byte_string(pool);
742 DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
743 DBG(r, "REQ[%X] | BEFORE input convert source |", (unsigned int)(apr_size_t)r);
744 DBG(r, "REQ[%X] +-------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
745 for (ii=0; ii<ilen-64; ii+=64) {
746 DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
751 if (ilen >= 64 && ((ilen-64) % 64 != 0)) {
752 DBG(r, "REQ[%X] | [%-*.*s] |", (unsigned int)(apr_size_t)r, 64, 64, &s[ii]);
754 DBG(r, "REQ[%X] +--------------------------------------------------------------------+", (unsigned int)(apr_size_t)r);
759 pair = apr_strtok(buff_pre, "&", &pstate);
765 pair_sv = apr_pstrdup(pool, pair);
767 name = apr_strtok(pair, "=", &vstate);
768 value = apr_strtok(NULL, "=", &vstate);
769 if (! name) continue;
770 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
771 DBG(r, "REQ[%X] found cookie no update parameter", (unsigned int)(apr_size_t)r);
781 pair = apr_strtok(s, "&", &pstate);
786 name = apr_strtok(pair, "=", &vstate);
787 value = apr_strtok(NULL, "=", &vstate);
788 if (! name) continue;
789 name = chxj_safe_to_jreserved_tag(r, name);
791 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
792 if (strlen(result) != 0)
793 result = apr_pstrcat(pool, result, "&", NULL);
795 if (strcasecmp(entryp->encoding, "NONE") != 0) {
800 if (value && *value != 0) {
801 value = chxj_url_decode(pool, value);
802 dlen = strlen(value);
803 dvalue = chxj_rencoding(r, value, &dlen);
804 dvalue = chxj_url_encode(pool, dvalue);
810 if (name && *name != 0) {
811 name = chxj_url_decode(pool, name);
813 dname = chxj_rencoding(r, name, &dlen);
814 dname = chxj_url_encode(pool, dname);
821 result = apr_pstrcat(pool, result, dname, "=", dvalue, NULL);
824 result = apr_pstrcat(pool, result, name, "=", value, NULL);
828 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
829 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
830 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
834 if (strlen(value) == 0)
837 if (strlen(result) != 0)
838 result = apr_pstrcat(pool, result, "&", NULL);
840 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
844 dlen = strlen(value);
845 value = chxj_url_decode(pool, value);
846 dvalue = chxj_rencoding(r, value, &dlen);
847 dvalue = chxj_url_encode(pool,dvalue);
848 result = apr_pstrcat(pool, result, &name[8], "=", dvalue, NULL);
852 result = apr_pstrcat(pool, result, &name[8], "=", value, NULL);
856 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
858 apr_table_unset(r->headers_in, "Cookie");
859 DBG(r, "REQ[%X] found cookie parameter[%s]", (unsigned int)(apr_size_t)r, value);
860 DBG(r, "REQ[%X] call start chxj_load_cookie()", (unsigned int)(apr_size_t)r);
861 cookie_lock_t *lock = chxj_cookie_lock(r);
862 cookie = chxj_load_cookie(r, value);
863 DBG(r, "REQ[%X] call end chxj_load_cookie()", (unsigned int)(apr_size_t)r);
864 if (! no_update_flag && cookie) {
865 cookie = chxj_update_cookie(r, cookie);
867 chxj_cookie_unlock(r, lock);
869 if (cookie && cookie->cookie_id) {
870 if (strlen(result) != 0)
871 result = apr_pstrcat(pool, result, "&", NULL);
872 result = apr_pstrcat(pool, result, name, "=", cookie->cookie_id, NULL);
876 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
877 if (strlen(result) != 0)
878 result = apr_pstrcat(pool, result, "&", NULL);
879 result = apr_pstrcat(pool, result, name, "=", value, NULL);
882 if ( strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1) == 0) {
885 dlen = strlen(value);
887 value = chxj_url_decode(pool, value);
888 dvalue = chxj_rencoding(r, value, &dlen);
889 dvalue = chxj_url_encode(pool,dvalue);
890 if (r->args && strlen(r->args) > 0) {
891 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
894 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], "=", dvalue, NULL);
896 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX)-1], spec);
900 if (strncasecmp(name, CHXJ_QUERY_STRING_PARAM_PREFIX_ENC, sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1) == 0) {
903 dlen = strlen(value);
905 value = chxj_url_decode(pool, value);
906 dvalue = chxj_rencoding(r, value, &dlen);
907 dvalue = chxj_url_encode(pool,dvalue);
908 if (r->args && strlen(r->args) > 0) {
909 r->args = apr_pstrcat(pool, r->args, "&", &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
912 r->args = apr_pstrcat(pool, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], "=", dvalue, NULL);
914 s_convert_guid_parameter_to_header(r, &name[sizeof(CHXJ_QUERY_STRING_PARAM_PREFIX_ENC)-1], spec);
917 DBG(r, "REQ[%X] ************************ name:[%s]", (unsigned int)(apr_size_t)r, name);
919 *len = strlen(result);
920 apr_table_setn(r->headers_in, "X-Chxj-Cookie-No-Update", "true");
921 if (! no_update_flag) {
922 result = apr_pstrcat(pool, result, "&_chxj_nc=true", NULL);
925 DBG(r, "REQ[%X] AFTER input convert result = [%s]", (unsigned int)(apr_size_t)r, result);
926 DBG(r, "REQ[%X] end chxj_input_convert()", (unsigned int)(apr_size_t)r);
933 * The received data is returned to the filter.
935 * @param f [i/o] It is a filter.
936 * @param data [i] It is data returned to the filter.
937 * @param len [i] It is length of the data returned to the filter.
940 pass_data_to_filter(ap_filter_t *f, const char *data,
943 request_rec *r = f->r;
944 conn_rec *c = r->connection;
946 apr_bucket_brigade *bb;
949 DBG(r, "REQ[%X] start pass_data_to_filter()", (unsigned int)(apr_size_t)r);
951 chxj_header_inf_clear(r);
953 bb = apr_brigade_create(r->pool, c->bucket_alloc);
954 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
956 APR_BRIGADE_INSERT_TAIL(bb, b);
957 b = apr_bucket_eos_create(f->c->bucket_alloc);
958 APR_BRIGADE_INSERT_TAIL(bb, b);
960 rv = ap_pass_brigade(f->next, bb);
961 if (rv != APR_SUCCESS) {
962 DBG(r, "REQ[%X] end pass_data_to_filter() (apr_pass_brigade)", (unsigned int)(apr_size_t)r);
966 DBG(r, "REQ[%X] end pass_data_to_filter()", (unsigned int)(apr_size_t)r);
973 * Add No Cache Header
976 s_add_no_cache_headers(request_rec *r, chxjconvrule_entry *entryp)
978 if (entryp->action & CONVRULE_NOCACHE_ON_BIT) {
979 apr_table_unset(r->headers_out, "Pragma");
980 apr_table_unset(r->err_headers_out, "Pragma");
981 apr_table_unset(r->headers_out, "Expires");
982 apr_table_unset(r->err_headers_out, "Expires");
983 apr_table_unset(r->headers_out, "Cache-Control");
984 apr_table_unset(r->err_headers_out, "Cache-Control");
986 apr_table_setn(r->err_headers_out, "Pragma", "no-cache");
987 apr_table_setn(r->err_headers_out, "Expires", "Thu, 01 Jan 1970 00:00:00 GMT");
988 apr_table_setn(r->err_headers_out, "Cache-Control", "no-cache, no-store");
994 * It is the main loop of the output filter.
996 * @param f [i/o] It is a filter.
1000 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
1006 char *user_agent = NULL;
1008 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
1009 cookie_t *cookie = NULL;
1010 mod_chxj_config *dconf;
1011 chxjconvrule_entry *entryp = NULL;
1012 device_table *spec = NULL;
1016 DBG(f->r, "REQ[%X] start of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1019 apr_pool_create(&pool, r->pool);
1021 entryp = ctx->entryp;
1023 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1026 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1027 DBG(r, "REQ[%X] found Location header", TO_ADDR(r));
1028 if ((entryp->action & CONVRULE_COOKIE_ON_BIT) || (entryp->action & CONVRULE_COOKIE_ONLY_BIT)) {
1029 cookie_lock_t *lock = NULL;
1030 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", TO_ADDR(r));
1031 switch(spec->html_spec_type) {
1032 case CHXJ_SPEC_Chtml_1_0:
1033 case CHXJ_SPEC_Chtml_2_0:
1034 case CHXJ_SPEC_Chtml_3_0:
1035 case CHXJ_SPEC_Chtml_4_0:
1036 case CHXJ_SPEC_Chtml_5_0:
1037 case CHXJ_SPEC_Chtml_6_0:
1038 case CHXJ_SPEC_Chtml_7_0:
1039 case CHXJ_SPEC_XHtml_Mobile_1_0:
1040 case CHXJ_SPEC_Jhtml:
1041 lock = chxj_cookie_lock(r);
1042 cookie = chxj_save_cookie(r);
1043 s_add_cookie_id_if_has_location_header(r, cookie);
1044 chxj_cookie_unlock(r, lock);
1050 if (! ap_is_HTTP_REDIRECT(r->status)) {
1051 r->status = HTTP_MOVED_TEMPORARILY;
1053 s_add_no_cache_headers(r, entryp);
1054 /* must not send body. */
1055 rv = pass_data_to_filter(f, "", 0);
1056 DBG(f->r, "REQ[%X] end of chxj_output_filter()", TO_ADDR(r));
1061 if (r->content_type) {
1062 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
1063 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
1064 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1065 && ! (dconf->image == CHXJ_IMG_ON
1066 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
1067 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
1068 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1069 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1070 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1071 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1072 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1073 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1074 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1075 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1076 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
1078 DBG(r, "REQ[%X] not convert content-type:[%s] dconf->image:[%d]", (unsigned int)(apr_size_t)r, r->content_type, dconf->image);
1079 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1080 cookie_lock_t *lock = NULL;
1081 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1082 switch(spec->html_spec_type) {
1083 case CHXJ_SPEC_Chtml_1_0:
1084 case CHXJ_SPEC_Chtml_2_0:
1085 case CHXJ_SPEC_Chtml_3_0:
1086 case CHXJ_SPEC_Chtml_4_0:
1087 case CHXJ_SPEC_Chtml_5_0:
1088 case CHXJ_SPEC_Chtml_6_0:
1089 case CHXJ_SPEC_Chtml_7_0:
1090 case CHXJ_SPEC_XHtml_Mobile_1_0:
1091 case CHXJ_SPEC_Jhtml:
1092 lock = chxj_cookie_lock(r);
1093 cookie = chxj_save_cookie(r);
1094 s_add_cookie_id_if_has_location_header(r, cookie);
1095 chxj_cookie_unlock(r, lock);
1101 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1102 if (! ap_is_HTTP_REDIRECT(r->status)) {
1103 r->status = HTTP_MOVED_TEMPORARILY;
1106 s_add_no_cache_headers(r, entryp);
1107 ap_pass_brigade(f->next, bb);
1108 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1113 DBG(r, "REQ[%X] not convert content-type:[(null)]", (unsigned int)(apr_size_t)r);
1114 ap_pass_brigade(f->next, bb);
1115 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1120 for (b = APR_BRIGADE_FIRST(bb);
1121 b != APR_BRIGADE_SENTINEL(bb);
1122 b = APR_BUCKET_NEXT(b)) {
1124 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
1125 DBG(r, "REQ[%X] read data[%.*s]",(unsigned int)(apr_size_t)r, (int)len, data);
1127 /*--------------------------------------------------------------------*/
1129 /*--------------------------------------------------------------------*/
1131 DBG(r, "append data start");
1132 ctx = (mod_chxj_ctx *)f->ctx;
1135 tmp = apr_palloc(r->pool, ctx->len);
1136 memcpy(tmp, ctx->buffer, ctx->len);
1138 ctx->buffer = apr_palloc(pool, ctx->len + len);
1140 memcpy(ctx->buffer, tmp, ctx->len);
1141 memcpy(&ctx->buffer[ctx->len], data, len);
1145 DBG(r, "REQ[%X] append data end", (unsigned int)(apr_size_t)r);
1148 if (APR_BUCKET_IS_EOS(b)) {
1150 DBG(r, "REQ[%X] eos", (unsigned int)(apr_size_t)r);
1151 /*----------------------------------------------------------------------*/
1153 /*----------------------------------------------------------------------*/
1155 cookie_lock_t *lock = NULL;
1156 ctx = (mod_chxj_ctx *)f->ctx;
1158 DBG(r, "REQ[%X] content_type=[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1159 lock = chxj_cookie_lock(r);
1161 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1163 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
1164 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
1165 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1170 tmp = apr_palloc(pool, ctx->len + 1);
1172 memset(tmp, 0, ctx->len + 1);
1173 memcpy(tmp, ctx->buffer, ctx->len);
1175 ctx->buffer = chxj_convert(r,
1176 (const char **)&tmp,
1177 (apr_size_t *)&ctx->len,
1179 user_agent, &cookie);
1183 ctx->buffer = apr_psprintf(r->pool, "\n");
1185 ctx->buffer = chxj_convert(r,
1186 (const char **)&ctx->buffer,
1187 (apr_size_t *)&ctx->len,
1189 user_agent, &cookie);
1194 && *(char *)r->content_type == 't'
1195 && strncmp(r->content_type, "text/xml", 8) == 0) {
1196 DBG(r, "REQ[%X] text/XML", (unsigned int)(apr_size_t)r);
1204 memset(&doc, 0, sizeof(Doc));
1205 memset(&qrcode, 0, sizeof(qr_code_t));
1207 doc.parse_mode = PARSE_MODE_CHTML;
1211 qs_init_malloc(&doc);
1213 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
1216 for (child = qs_get_child_node(&doc,root);
1218 child = qs_get_next_node(&doc,child)) {
1219 char *name = qs_get_node_name(&doc,child);
1220 if (strcasecmp("qrcode",name) == 0) {
1225 qs_all_free(&doc,QX_LOGMARK);
1227 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1228 chxj_qrcode_node_to_qrcode(&qrcode, root);
1229 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
1231 ERR(r, "REQ[%X] qrcode create failed.", (unsigned int)(apr_size_t)r);
1232 chxj_cookie_unlock(r, lock);
1233 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1236 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1240 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
1242 && ( *r->content_type == 'i' || *r->content_type == 'I')
1243 && dconf->image == CHXJ_IMG_ON
1244 && strncasecmp("image/", r->content_type, 6) == 0
1245 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
1246 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
1247 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
1248 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
1249 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
1250 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
1251 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
1252 || STRCASEEQ('x','X',"x-ms-bmp", &r->content_type[6]) /* BMP */
1253 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
1254 DBG(r, "REQ[%X] detect convert target:[%s]", (unsigned int)(apr_size_t)r, r->content_type);
1258 tmp = apr_palloc(pool, ctx->len + 1);
1260 memset(tmp, 0, ctx->len + 1);
1261 memcpy(tmp, ctx->buffer, ctx->len);
1263 chxj_convert_image(r,
1264 (const char **)&tmp,
1265 (apr_size_t *)&ctx->len);
1266 if (ctx->buffer == NULL) {
1272 apr_table_unset(r->headers_out, "Content-Length");
1273 apr_table_unset(r->err_headers_out, "Content-Length");
1274 ap_set_content_length(r, (apr_off_t)ctx->len);
1278 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1279 s_add_cookie_id_if_has_location_header(r, cookie);
1280 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1281 if (! ap_is_HTTP_REDIRECT(r->status)) {
1282 r->status = HTTP_MOVED_TEMPORARILY;
1285 if (ctx->len && ap_is_HTTP_REDIRECT(r->status)) {
1286 ctx->buffer = apr_pstrdup(pool, "");
1288 ap_set_content_length(r, (apr_off_t)ctx->len);
1290 chxj_cookie_unlock(r,lock);
1291 s_add_no_cache_headers(r, entryp);
1292 rv = pass_data_to_filter(f,
1293 (const char *)ctx->buffer,
1294 (apr_size_t)ctx->len);
1297 ctx->buffer = apr_pstrdup(pool, "");
1299 ap_set_content_length(r, (apr_off_t)ctx->len);
1300 chxj_cookie_unlock(r, lock);
1301 s_add_no_cache_headers(r, entryp);
1302 rv = pass_data_to_filter(f,
1303 (const char *)ctx->buffer,
1304 (apr_size_t)ctx->len);
1306 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1310 DBG(r, "REQ[%X] SAVE COOKIE[%x]", (unsigned int)(apr_size_t)r, entryp->action);
1315 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1316 cookie_lock_t *lock = NULL;
1317 DBG(r, "REQ[%X] entryp->action == COOKIE_ON_BIT", (unsigned int)(apr_size_t)r);
1318 switch(spec->html_spec_type) {
1319 case CHXJ_SPEC_Chtml_1_0:
1320 case CHXJ_SPEC_Chtml_2_0:
1321 case CHXJ_SPEC_Chtml_3_0:
1322 case CHXJ_SPEC_Chtml_4_0:
1323 case CHXJ_SPEC_Chtml_5_0:
1324 case CHXJ_SPEC_Chtml_6_0:
1325 case CHXJ_SPEC_Chtml_7_0:
1326 case CHXJ_SPEC_XHtml_Mobile_1_0:
1327 case CHXJ_SPEC_Jhtml:
1328 lock = chxj_cookie_lock(r);
1329 cookie = chxj_save_cookie(r);
1331 * Location Header Check to add cookie parameter.
1333 s_add_cookie_id_if_has_location_header(r, cookie);
1334 chxj_cookie_unlock(r, lock);
1335 apr_table_unset(r->headers_out, "Set-Cookie");
1336 apr_table_unset(r->err_headers_out, "Set-Cookie");
1343 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1344 if (! ap_is_HTTP_REDIRECT(r->status)) {
1345 r->status = HTTP_MOVED_TEMPORARILY;
1348 apr_table_setn(r->headers_out, "Content-Length", "0");
1349 DBG(r, "REQ[%X] call pass_data_to_filter()", (unsigned int)(apr_size_t)r);
1350 s_add_no_cache_headers(r, entryp);
1351 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1352 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1357 apr_brigade_destroy(bb);
1359 DBG(f->r, "REQ[%X] end of chxj_output_filter()", (unsigned int)(apr_size_t)r);
1365 * Add Cookie_id if it has location header.
1368 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1370 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1371 if (! location_header) {
1372 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1374 if (cookie && location_header) {
1375 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1376 location_header = chxj_add_cookie_parameter(r,
1379 apr_table_unset(r->headers_out, "Location");
1380 apr_table_setn(r->headers_out, "Location", location_header);
1381 DBG(r, "REQ[%X] Location Header=[%s]", (unsigned int)(apr_size_t)r, location_header);
1382 if (!ap_is_HTTP_REDIRECT(r->status)) {
1383 r->status = HTTP_MOVED_TEMPORARILY;
1389 * It is the main loop of the input filter handler.
1393 chxj_input_handler(request_rec *r)
1395 mod_chxj_config *dconf;
1396 chxjconvrule_entry *entryp = NULL;
1397 device_table *spec = NULL;
1398 char *post_data = NULL;
1399 apr_size_t post_data_len = 0;
1404 int response_code = 0;
1406 DBG(r, "start of chxj_input_handler()");
1408 if (strcasecmp(r->handler, "chxj-input-handler")) {
1409 DBG(r, "end chxj_input_handler()");
1412 apr_pool_create(&pool, r->pool);
1414 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1415 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1417 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1419 spec = chxj_specified_device(r, user_agent);
1420 entryp = chxj_apply_convrule(r, dconf->convrules);
1422 post_data = apr_pstrdup(pool, "");
1423 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1424 if (ap_should_client_block(r)) {
1425 while (post_data_len < CHXJ_POST_MAX) {
1428 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1429 if (read_bytes<=0) {
1432 buffer[read_bytes] = '\0';
1433 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1434 post_data_len += read_bytes;
1443 if (post_data_len > 0) {
1444 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp, spec);
1445 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1449 if (dconf->forward_url_base) {
1450 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1453 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);
1456 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1458 DBG(r, "==> new url_path:[%s]", url_path);
1461 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1462 apr_table_unset(r->headers_in, "Content-Length");
1463 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1464 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len, &response_code);
1465 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1466 DBG(r, "REQ[%X] response length:[%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, res_len);
1467 for (ii=0; ii<res_len/64; ii++) {
1468 DBG(r, "REQ[%X] response:[%.*s]", (unsigned int)(apr_size_t)r, 64, &response[ii*64]);
1470 DBG(r, "REQ[%X] -------------------------------------------------------", (unsigned int)(apr_size_t)r);
1473 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1474 if (strcasecmp(chunked, "chunked") == 0) {
1476 apr_table_unset(r->headers_out, "Transfer-Encoding");
1479 if (ap_is_HTTP_ERROR(response_code)) {
1480 DBG(r, "REQ[%X] end of chxj_input_handler() (HTTP-ERROR received. response code:[%d])", (unsigned int)(apr_size_t)r, response_code);
1481 return response_code;
1485 apr_pool_create(&wpool, r->pool);
1486 apr_bucket_brigade *bb;
1489 conn_rec *c = r->connection;
1491 bb = apr_brigade_create(wpool, c->bucket_alloc);
1492 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1493 APR_BRIGADE_INSERT_TAIL(bb, e);
1494 e = apr_bucket_eos_create(c->bucket_alloc);
1495 APR_BRIGADE_INSERT_TAIL(bb, e);
1496 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1497 ERR(r, "REQ[%X] %s:%d failed ap_pass_brigade()", (unsigned int)(apr_size_t)r, APLOG_MARK);
1500 apr_brigade_cleanup(bb);
1503 DBG(r, "REQ[%X] end of chxj_input_handler()", (unsigned int)(apr_size_t)r);
1507 static mod_chxj_global_config *
1508 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1510 mod_chxj_global_config *conf;
1512 SDBG(s, "start chxj_global_config_create()");
1514 /*--------------------------------------------------------------------------*/
1515 /* allocate an own subpool which survives server restarts */
1516 /*--------------------------------------------------------------------------*/
1517 conf = (mod_chxj_global_config *)apr_palloc(pool,
1518 sizeof(mod_chxj_global_config));
1520 conf->cookie_db_lock = NULL;
1522 SDBG(s, "end chxj_global_config_create()");
1529 * initialize chxj module
1532 chxj_init_module(apr_pool_t *p,
1533 apr_pool_t *UNUSED(plog),
1534 apr_pool_t *UNUSED(ptemp),
1540 SDBG(s, "start chxj_init_module()");
1542 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1544 if (user_data == NULL) {
1547 * dummy user_data set.
1549 apr_pool_userdata_set(
1551 CHXJ_MOD_CONFIG_KEY,
1552 apr_pool_cleanup_null,
1554 SDBG(s, "end chxj_init_module()");
1558 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1560 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1562 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1563 return HTTP_INTERNAL_SERVER_ERROR;
1566 SDBG(s, "end chxj_init_module()");
1573 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1576 SDBG(s, "start chxj_child_init()");
1577 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1579 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1581 SDBG(s, "end chxj_child_init()");
1586 * A set structure of each server is generated.
1592 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1594 mod_chxj_global_config *gc;
1596 gc = chxj_global_config_create(p,s);
1603 chxj_translate_name(request_rec *r)
1605 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1606 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1607 DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
1608 DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
1609 DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
1610 DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
1612 return chxj_trans_name(r);
1620 chxj_insert_filter(request_rec *r)
1624 mod_chxj_config *dconf;
1625 chxjconvrule_entry *entryp;
1630 DBG(r, "REQ[%X] start chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1632 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1634 /* we get User-Agent from CHXJ_HTTP_USER_AGENT header if any */
1635 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
1637 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1640 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1642 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1643 DBG(r, "REQ[%X] detect multipart/form-data ==> no target", (apr_size_t)(unsigned int)r);
1644 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1648 spec = chxj_specified_device(r, user_agent);
1649 entryp = chxj_apply_convrule(r, dconf->convrules);
1651 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1654 ctx = apr_palloc(r->pool, sizeof(*ctx));
1655 memset(ctx, 0, sizeof(*ctx));
1656 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1657 ERR(r, "%s:%d: failed: new pool create. rv:[%d]", __FILE__,__LINE__,rv);
1658 DBG(r, "REQ:[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1661 ctx->entryp = entryp;
1663 ctx->buffer = apr_palloc(ctx->pool, 1);
1666 if (!entryp || (!(entryp->action & CONVRULE_ENGINE_ON_BIT) && !(entryp->action & CONVRULE_COOKIE_ONLY_BIT))) {
1667 DBG(r,"REQ[%X] EngineOff", (unsigned int)(apr_size_t)r);
1668 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1672 switch(spec->html_spec_type) {
1673 case CHXJ_SPEC_Chtml_1_0:
1674 case CHXJ_SPEC_Chtml_2_0:
1675 case CHXJ_SPEC_Chtml_3_0:
1676 case CHXJ_SPEC_Chtml_4_0:
1677 case CHXJ_SPEC_Chtml_5_0:
1678 case CHXJ_SPEC_Chtml_6_0:
1679 case CHXJ_SPEC_Chtml_7_0:
1680 case CHXJ_SPEC_XHtml_Mobile_1_0:
1681 case CHXJ_SPEC_Hdml:
1682 case CHXJ_SPEC_Jhtml:
1683 case CHXJ_SPEC_Jxhtml:
1687 DBG(r, "REQ[%X] end chxj_insert_filter() Unknown spec type(%d).", (unsigned int)(apr_size_t)r, spec->html_spec_type);
1692 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1693 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1694 DBG(r, "REQ[%X] added Output Filter", (unsigned int)(apr_size_t)r);
1697 DBG(r, "REQ[%X] end chxj_insert_filter()", (unsigned int)(apr_size_t)r);
1702 * The hook is registered.
1707 chxj_register_hooks(apr_pool_t *UNUSED(p))
1709 ap_hook_post_config(chxj_init_module,
1712 APR_HOOK_REALLY_FIRST);
1713 ap_hook_child_init(chxj_child_init,
1716 APR_HOOK_REALLY_FIRST);
1717 ap_register_output_filter (
1718 "chxj_output_filter",
1722 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1723 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1724 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1725 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1726 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1727 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1732 * A set structure according to the directory is generated.
1738 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1740 mod_chxj_config *conf;
1742 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1743 conf->device_data_file = NULL;
1744 conf->devices = NULL;
1745 conf->emoji_data_file = NULL;
1747 conf->emoji_tail = NULL;
1748 conf->image = CHXJ_IMG_NONE;
1749 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1750 conf->image_cache_limit = 0;
1751 conf->server_side_encoding = NULL;
1752 conf->cookie_db_dir = NULL;
1753 conf->cookie_timeout = 0;
1754 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1755 conf->cookie_lazy_mode = 0;
1756 conf->cookie_dbm_type = NULL;
1757 #if defined(USE_MYSQL_COOKIE)
1758 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1759 conf->mysql.port = MYSQL_PORT;
1760 conf->mysql.host = NULL;
1762 #if defined(USE_MEMCACHE_COOKIE)
1763 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1764 conf->memcache.host = NULL;
1765 conf->memcache.port = 0;
1767 conf->forward_url_base = NULL;
1768 conf->forward_server_ip = NULL;
1769 conf->allowed_cookie_domain = NULL;
1770 conf->post_log = NULL;
1776 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1777 memset(conf->dir, 0, strlen(arg)+1);
1778 strcpy(conf->dir, arg);
1780 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1782 /* Default is copyleft */
1783 conf->image_copyright = NULL;
1790 * Merge per-directory CHXJ configurations
1793 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1795 mod_chxj_config *base;
1796 mod_chxj_config *add;
1797 mod_chxj_config *mrg;
1799 base = (mod_chxj_config *)basev;
1800 add = (mod_chxj_config *)addv;
1801 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1803 mrg->device_data_file = NULL;
1804 mrg->devices = NULL;
1805 mrg->emoji_data_file = NULL;
1806 mrg->image = CHXJ_IMG_NONE;
1807 mrg->image_cache_dir = NULL;
1808 mrg->image_copyright = NULL;
1809 mrg->image_cache_limit = 0;
1811 mrg->emoji_tail = NULL;
1812 mrg->new_line_type = NLTYPE_NIL;
1813 mrg->forward_url_base = NULL;
1814 mrg->forward_server_ip = NULL;
1815 mrg->allowed_cookie_domain = NULL;
1816 mrg->post_log = NULL;
1817 mrg->cookie_dbm_type = NULL;
1819 mrg->dir = apr_pstrdup(p, add->dir);
1821 if (! add->device_data_file) {
1822 mrg->devices = base->devices;
1823 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1826 mrg->devices = add->devices;
1827 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1830 if (! add->emoji_data_file) {
1831 mrg->emoji = base->emoji;
1832 mrg->emoji_tail = base->emoji_tail;
1833 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1836 mrg->emoji = add->emoji;
1837 mrg->emoji_tail = add->emoji_tail;
1838 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1841 if (add->image == CHXJ_IMG_NONE) {
1842 mrg->image = base->image;
1845 mrg->image = add->image;
1848 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1849 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1852 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1855 if (add->image_cache_limit) {
1856 mrg->image_cache_limit = add->image_cache_limit;
1859 mrg->image_cache_limit = base->image_cache_limit;
1862 if (add->image_copyright)
1863 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1865 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1867 if (add->server_side_encoding) {
1868 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1871 if (base->server_side_encoding) {
1872 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1875 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1878 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1880 if (add->cookie_db_dir) {
1881 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1884 if (base->cookie_db_dir) {
1885 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1888 mrg->cookie_db_dir = NULL;
1891 if (add->cookie_timeout) {
1892 mrg->cookie_timeout = add->cookie_timeout;
1895 if (base->cookie_db_dir) {
1896 mrg->cookie_timeout = base->cookie_timeout;
1899 mrg->cookie_timeout = 0;
1902 #if defined(USE_MYSQL_COOKIE)
1903 if (add->mysql.host) {
1904 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1906 else if (base->mysql.host) {
1907 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1910 mrg->mysql.host = NULL;
1912 if (add->mysql.port) {
1913 mrg->mysql.port = add->mysql.port;
1915 else if (base->mysql.port) {
1916 mrg->mysql.port = base->mysql.port;
1919 mrg->mysql.port = 0;
1922 if (add->mysql.database) {
1923 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1925 else if (base->mysql.database) {
1926 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1929 mrg->mysql.database = NULL;
1932 if (add->mysql.username) {
1933 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1935 else if (base->mysql.username) {
1936 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1939 mrg->mysql.username = NULL;
1942 if (add->mysql.password) {
1943 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1945 else if (base->mysql.password) {
1946 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1949 mrg->mysql.password = NULL;
1952 if (add->mysql.tablename) {
1953 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1955 else if (base->mysql.tablename) {
1956 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1959 mrg->mysql.tablename = NULL;
1962 if (add->mysql.socket_path) {
1963 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1965 else if (base->mysql.socket_path) {
1966 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1969 mrg->mysql.socket_path = NULL;
1972 if (add->mysql.charset) {
1973 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1975 else if (base->mysql.charset) {
1976 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1979 mrg->mysql.charset = NULL;
1982 #if defined(USE_MEMCACHE_COOKIE)
1983 if (add->memcache.host) {
1984 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1986 else if (base->memcache.host) {
1987 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1990 mrg->memcache.host = NULL;
1992 if (add->memcache.port) {
1993 mrg->memcache.port = add->memcache.port;
1995 else if (base->memcache.port) {
1996 mrg->memcache.port = base->memcache.port;
1999 mrg->memcache.port = 0;
2002 if (add->cookie_store_type) {
2003 mrg->cookie_store_type = add->cookie_store_type;
2005 else if (base->cookie_store_type) {
2006 mrg->cookie_store_type = base->cookie_store_type;
2009 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2011 if (add->cookie_lazy_mode) {
2012 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
2014 else if (base->cookie_lazy_mode) {
2015 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
2018 mrg->cookie_lazy_mode = 0;
2020 if (add->new_line_type) {
2021 mrg->new_line_type = add->new_line_type;
2023 else if (base->new_line_type) {
2024 mrg->new_line_type = base->new_line_type;
2027 mrg->new_line_type = NLTYPE_NIL;
2030 if (add->forward_url_base) {
2031 mrg->forward_url_base = add->forward_url_base;
2033 else if (base->forward_url_base) {
2034 mrg->forward_url_base = base->forward_url_base;
2037 if (add->allowed_cookie_domain) {
2038 mrg->allowed_cookie_domain = add->allowed_cookie_domain;
2041 mrg->allowed_cookie_domain = base->allowed_cookie_domain;
2043 if (add->post_log) {
2044 mrg->post_log = add->post_log;
2047 mrg->post_log = base->post_log;
2049 if (add->cookie_dbm_type) {
2050 mrg->cookie_dbm_type = add->cookie_dbm_type;
2053 mrg->cookie_dbm_type = base->cookie_dbm_type;
2060 chxj_command_parse_take5(
2073 for (;*strp == ' '||*strp == '\t'; strp++) ;
2083 for (; *strp != '\0'; strp++) {
2084 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2085 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2090 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2091 || (isquoted && *strp == '"'))
2105 for (;*strp == ' '||*strp == '\t'; strp++) ;
2114 for (; *strp != '\0'; strp++) {
2115 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2116 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2121 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2122 || (isquoted && *strp == '"'))
2135 for (;*strp == ' '||*strp == '\t'; strp++);
2143 for (; *strp != '\0'; strp++) {
2144 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2145 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2150 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2151 || (isquoted && *strp == '"'))
2163 for (;*strp == ' '||*strp == '\t'; strp++);
2171 for (; *strp != '\0'; strp++) {
2172 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2173 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2178 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2179 || (isquoted && *strp == '"'))
2190 for (;*strp == ' '||*strp == '\t'; strp++);
2198 for (; *strp != '\0'; strp++) {
2199 if ((isquoted && (*strp == ' ' || *strp == '\t'))
2200 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
2205 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
2206 || (isquoted && *strp == '"'))
2216 * The device definition file is loaded.
2218 * @param arg [i] The name of the device definition file is specified.
2219 * @param mconfig [i/o] The pointer to a set structure is specified.
2223 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
2225 mod_chxj_config *conf;
2230 if (strlen(arg) > 256)
2231 return "mod_chxj: device data filename too long.";
2233 conf = (mod_chxj_config *)mconfig;
2234 conf->device_data_file = apr_pstrdup(parms->pool, arg);
2236 qs_init_malloc(&doc);
2237 qs_init_root_node(&doc);
2239 qs_parse_file((Doc *)&doc, (const char *)arg);
2240 chxj_load_device_data(&doc,parms->pool, conf);
2241 qs_all_free(&doc, QX_LOGMARK);
2248 * Device definition information is loaded.
2251 * @param arg [i] The name of the device definition file is specified.
2252 * @param mconfig [i/o] The pointer to a set structure is specified.
2256 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
2258 mod_chxj_config *conf;
2265 if (strlen(arg) > 256)
2266 return "mod_chxj: emoji data filename too long.";
2268 conf = (mod_chxj_config *)mconfig;
2269 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
2270 qs_init_malloc(&doc);
2271 qs_init_root_node(&doc);
2273 qs_parse_file((Doc *)&doc, (const char *)arg);
2275 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
2277 qs_all_free(&doc, QX_LOGMARK);
2285 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
2287 mod_chxj_config *conf;
2292 if (strlen(arg) > 256)
2293 return "image uri is too long.";
2295 conf = (mod_chxj_config*)mconfig;
2296 if (strcasecmp("ON", arg) == 0) {
2297 conf->image = CHXJ_IMG_ON;
2300 conf->image = CHXJ_IMG_OFF;
2308 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
2310 mod_chxj_config *conf;
2315 if (strlen(arg) > 256)
2316 return "cache dir name is too long.";
2318 conf = (mod_chxj_config *)mconfig;
2319 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
2326 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
2328 mod_chxj_config *conf;
2333 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
2334 return "cache size is too long.";
2336 conf = (mod_chxj_config *)mconfig;
2339 * I use strtol function because strtoul is not portable function.
2341 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
2344 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
2346 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
2355 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
2357 mod_chxj_config *conf;
2362 if (strlen(arg) > 256)
2363 return "Copyright Flag is too long.";
2365 conf = (mod_chxj_config *)mconfig;
2366 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2373 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2377 mod_chxj_config *dconf;
2378 chxjconvrule_entry *newrule;
2388 dconf = (mod_chxj_config *)mconfig;
2390 if (strlen(arg) > 4096)
2391 return "mod_chxj: ChxjConvertRule: is too long.";
2393 dconf = (mod_chxj_config *)mconfig;
2394 if (dconf->convrules == NULL)
2395 dconf->convrules = apr_array_make(cmd->pool,
2397 sizeof(chxjconvrule_entry));
2399 newrule = apr_array_push(dconf->convrules);
2402 newrule->action = 0;
2404 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2405 return "ChxjConvertRule: bad argument line";
2407 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2411 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2417 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2418 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2421 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2422 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2425 if (strcasecmp(CONVRULE_EMOJI_ONLY_CMD, action) == 0) {
2426 newrule->action |= CONVRULE_EMOJI_ONLY_BIT;
2429 if (strcasecmp(CONVRULE_ENVINFO_ONLY_CMD, action) == 0) {
2430 newrule->action |= CONVRULE_ENVINFO_ONLY_BIT;
2436 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2437 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2439 else if (strcasecmp(CONVRULE_COOKIE_ONLY_CMD, action) == 0) {
2440 newrule->action |= CONVRULE_COOKIE_ONLY_BIT;
2446 if (strcasecmp(CONVRULE_JRCONV_OFF_CMD, action) == 0) {
2447 newrule->action |= CONVRULE_JRCONV_OFF_BIT;
2453 if (strcasecmp(CONVRULE_NOCACHE_ON_CMD, action) == 0) {
2454 newrule->action |= CONVRULE_NOCACHE_ON_BIT;
2460 if (strcasecmp(CONVRULE_QSCONV_OFF_CMD, action) == 0) {
2461 newrule->action |= CONVRULE_QSCONV_OFF_BIT;
2467 if (strcasecmp(CONVRULE_Z2H_ON_CMD, action) == 0) {
2468 newrule->action |= CONVRULE_Z2H_ON_BIT;
2471 if (strcasecmp(CONVRULE_Z2H_OFF_CMD, action) == 0) {
2472 newrule->action |= CONVRULE_Z2H_OFF_BIT;
2475 if (strcasecmp(CONVRULE_Z2H_ALPHA_ON_CMD, action) == 0) {
2476 newrule->action |= CONVRULE_Z2H_ALPHA_ON_BIT;
2479 if (strcasecmp(CONVRULE_Z2H_ALPHA_OFF_CMD, action) == 0) {
2480 newrule->action |= CONVRULE_Z2H_ALPHA_OFF_BIT;
2483 if (strcasecmp(CONVRULE_Z2H_NUM_ON_CMD, action) == 0) {
2484 newrule->action |= CONVRULE_Z2H_NUM_ON_BIT;
2487 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2488 newrule->action |= CONVRULE_Z2H_NUM_OFF_BIT;
2491 if (strcasecmp(CONVRULE_Z2H_ALL_ON_CMD, action) == 0) {
2492 newrule->action |= CONVRULE_Z2H_ON_BIT | CONVRULE_Z2H_ALPHA_ON_BIT | CONVRULE_Z2H_NUM_ON_BIT;
2495 if (strcasecmp(CONVRULE_Z2H_NUM_OFF_CMD, action) == 0) {
2496 newrule->action |= CONVRULE_Z2H_OFF_BIT | CONVRULE_Z2H_ALPHA_OFF_BIT | CONVRULE_Z2H_NUM_OFF_BIT;
2507 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2511 mode = AP_REG_EXTENDED;
2512 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2513 return "RewriteRule: cannot compile regular expression ";
2515 newrule->regexp = regexp;
2517 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2519 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2521 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2523 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2524 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2526 newrule->user_agent = NULL;
2528 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2540 mod_chxj_config *dconf;
2543 if (strlen(arg) > 4096)
2544 return "mod_chxj: ChxjCookieDir is too long.";
2546 dconf = (mod_chxj_config *)mconfig;
2548 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2555 cmd_set_cookie_timeout(
2556 cmd_parms *UNUSED(cmd),
2560 mod_chxj_config *dconf;
2562 if (strlen(arg) > 4096)
2563 return "mod_chxj: ChxjCookieTimeout is too long.";
2565 if (chxj_chk_numeric(arg) != 0)
2566 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2568 dconf = (mod_chxj_config *)mconfig;
2570 dconf->cookie_timeout = atoi(arg);
2576 #if defined(USE_MYSQL_COOKIE)
2578 cmd_set_cookie_mysql_database(
2583 mod_chxj_config *dconf;
2585 if (strlen(arg) > 255)
2586 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2588 dconf = (mod_chxj_config *)mconfig;
2590 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2597 cmd_set_cookie_mysql_username(
2602 mod_chxj_config *dconf;
2604 if (strlen(arg) > 255)
2605 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2607 dconf = (mod_chxj_config *)mconfig;
2609 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2616 cmd_set_cookie_mysql_password(
2621 mod_chxj_config *dconf;
2623 if (strlen(arg) > 255)
2624 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2626 dconf = (mod_chxj_config *)mconfig;
2628 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2635 cmd_set_cookie_mysql_table_name(
2640 mod_chxj_config *dconf;
2642 if (strlen(arg) > 255)
2643 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2645 dconf = (mod_chxj_config *)mconfig;
2647 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2653 cmd_set_cookie_mysql_port(
2654 cmd_parms *UNUSED(cmd),
2658 mod_chxj_config *dconf;
2660 if (strlen(arg) > 255)
2661 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2663 dconf = (mod_chxj_config *)mconfig;
2665 if (chxj_chk_numeric(arg) != 0)
2666 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2668 dconf = (mod_chxj_config *)mconfig;
2670 dconf->mysql.port = chxj_atoi(arg);
2677 cmd_set_cookie_mysql_host(
2682 mod_chxj_config *dconf;
2684 if (strlen(arg) > 255)
2685 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2687 dconf = (mod_chxj_config *)mconfig;
2689 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2696 cmd_set_cookie_mysql_socket_path(
2701 mod_chxj_config *dconf;
2703 if (strlen(arg) > 4096)
2704 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2706 dconf = (mod_chxj_config *)mconfig;
2708 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2715 cmd_set_cookie_mysql_charset(
2720 mod_chxj_config *dconf;
2722 if (strlen(arg) > 255)
2723 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2725 dconf = (mod_chxj_config *)mconfig;
2727 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2732 #if defined(USE_MEMCACHE_COOKIE)
2734 cmd_set_cookie_memcache_port(
2735 cmd_parms *UNUSED(cmd),
2739 mod_chxj_config *dconf;
2741 if (strlen(arg) > 255)
2742 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2744 dconf = (mod_chxj_config *)mconfig;
2746 if (chxj_chk_numeric(arg) != 0)
2747 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2749 dconf = (mod_chxj_config *)mconfig;
2751 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2758 cmd_set_cookie_memcache_host(
2763 mod_chxj_config *dconf;
2765 if (strlen(arg) > 255)
2766 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2768 dconf = (mod_chxj_config *)mconfig;
2770 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2777 cmd_set_cookie_lazy_mode(
2778 cmd_parms *UNUSED(cmd),
2782 mod_chxj_config *dconf;
2784 if (strlen(arg) > 255)
2785 return "mod_chxj: ChxjCookieLazyMode is too long.";
2787 dconf = (mod_chxj_config *)mconfig;
2789 if (strcasecmp("TRUE",arg) == 0) {
2790 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2793 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2800 cmd_set_cookie_store_type(
2801 cmd_parms *UNUSED(cmd),
2805 mod_chxj_config *dconf;
2807 if (strlen(arg) > 255)
2808 return "mod_chxj: ChxjCookieStoreType is too long.";
2810 dconf = (mod_chxj_config *)mconfig;
2812 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2813 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2815 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2816 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2818 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2819 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2822 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2829 cmd_set_forward_url_base(
2834 mod_chxj_config *dconf;
2836 if (strlen(arg) > 255)
2837 return "mod_chxj: ChxjForwardUrlBase is too long.";
2839 dconf = (mod_chxj_config *)mconfig;
2841 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2847 cmd_set_forward_server_ip(
2852 mod_chxj_config *dconf;
2854 if (strlen(arg) > 255)
2855 return "mod_chxj: ChxjForwardServerIp is too long.";
2857 dconf = (mod_chxj_config *)mconfig;
2859 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2865 cmd_allowed_cookie_domain(
2870 mod_chxj_config *dconf;
2872 if (strlen(arg) > 255)
2873 return "mod_chxj: ChxjAllowedCookieDomain is too long.";
2875 dconf = (mod_chxj_config *)mconfig;
2877 dconf->allowed_cookie_domain = apr_pstrdup(cmd->pool, arg);
2883 cmd_set_new_line_type(
2884 cmd_parms *UNUSED(cmd),
2888 mod_chxj_config *dconf;
2889 if (strlen(arg) > 255)
2890 return "mod_chxj: ChxjNewLineType is too long.";
2892 dconf = (mod_chxj_config *)mconfig;
2894 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2895 dconf->new_line_type = NLTYPE_CRLF;
2897 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2898 dconf->new_line_type = NLTYPE_LF;
2900 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2901 dconf->new_line_type = NLTYPE_CR;
2903 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2904 dconf->new_line_type = NLTYPE_NONE;
2907 return "mod_chxj: invalid value (ChxjNewLineType)";
2918 mod_chxj_config *dconf;
2919 if (strlen(arg) > 255)
2920 return "mod_chxj: ChxjPostLogEnv is too long.";
2922 dconf = (mod_chxj_config *)mconfig;
2924 dconf->post_log = apr_pstrdup(cmd->pool, arg);
2930 cmd_cookie_dbm_type(
2935 mod_chxj_config *dconf;
2936 if (strlen(arg) > 255)
2937 return "mod_chxj: ChxjCookieDbmType is too long.";
2939 dconf = (mod_chxj_config *)mconfig;
2941 dconf->cookie_dbm_type = apr_pstrdup(cmd->pool, arg);
2947 static const command_rec cmds[] = {
2949 "ChxjLoadDeviceData",
2950 cmd_load_device_data,
2953 "Load Device Data"),
2955 "ChxjLoadEmojiData",
2956 cmd_load_emoji_data,
2962 cmd_set_image_engine,
2965 "Convert Target URI"),
2967 "ChxjImageCacheDir",
2968 cmd_set_image_cache_dir,
2971 "Image Cache Directory"),
2973 "ChxjImageCacheLimit",
2974 cmd_set_image_cache_limit,
2977 "Image Cache Limit"),
2979 "ChxjImageCopyright",
2980 cmd_set_image_copyright,
2989 "an URL-applied regexp-pattern and a substitution URL"),
2995 "save cookie.db directory."),
2997 "ChxjCookieTimeout",
2998 cmd_set_cookie_timeout,
3001 "The compulsion time-out time of the cookie is specified. "),
3003 "ChxjCookieStoreType",
3004 cmd_set_cookie_store_type,
3007 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
3009 "ChxjCookieLazyMode",
3010 cmd_set_cookie_lazy_mode,
3013 "OneTimeID is negligently done. (TRUE/FALSE)"),
3014 #if defined(USE_MYSQL_COOKIE)
3016 "ChxjCookieMysqlHost",
3017 cmd_set_cookie_mysql_host,
3020 "The MySQL database host used by saving Cookie"),
3022 "ChxjCookieMysqlPort",
3023 cmd_set_cookie_mysql_port,
3026 "The MySQL database port used by saving Cookie"),
3028 "ChxjCookieMysqlDatabase",
3029 cmd_set_cookie_mysql_database,
3032 "The MySQL database name used by saving Cookie"),
3034 "ChxjCookieMysqlUsername",
3035 cmd_set_cookie_mysql_username,
3038 "The MySQL username used by saving Cookie"),
3040 "ChxjCookieMysqlPassword",
3041 cmd_set_cookie_mysql_password,
3044 "The MySQL password used by saving Cookie"),
3046 "ChxjCookieMysqlTableName",
3047 cmd_set_cookie_mysql_table_name,
3050 "The MySQL table name used by saving Cookie"),
3052 "ChxjCookieMysqlSocketPath",
3053 cmd_set_cookie_mysql_socket_path,
3056 "The MySQL socket path used by saving Cookie"),
3058 "ChxjCookieMysqlCharset",
3059 cmd_set_cookie_mysql_charset,
3062 "The MySQL charset used by saving Cookie"),
3064 #if defined(USE_MEMCACHE_COOKIE)
3066 "ChxjCookieMemcacheHost",
3067 cmd_set_cookie_memcache_host,
3070 "The Memcached host used by saving Cookie"),
3072 "ChxjCookieMemcachePort",
3073 cmd_set_cookie_memcache_port,
3076 "The Memcached port used by saving Cookie"),
3080 cmd_set_new_line_type,
3083 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
3085 "ChxjForwardUrlBase",
3086 cmd_set_forward_url_base,
3089 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
3091 "ChxjForwardServerIp",
3092 cmd_set_forward_server_ip,
3095 "The forward server ip(default: this server ip)"),
3097 "ChxjAllowedCookieDomain",
3098 cmd_allowed_cookie_domain,
3101 "Domain that permits parameter addition for cookie besides hostname.(Default:hostname Only)"),
3107 "for CustomLog directive. mod_chxj's internal POST log environment name.(Default:chxj-post-log)"),
3109 "ChxjCookieDbmType",
3110 cmd_cookie_dbm_type,
3113 "Kind of DBM used with Cookie simulator.(default|GDBM|SDBM|DB|NDBM)"),
3114 {NULL,{NULL},NULL,0,0,NULL},
3118 /*----------------------------------------------------------------------------*/
3119 /* Dispatch list for API hooks */
3120 /*----------------------------------------------------------------------------*/
3121 module AP_MODULE_DECLARE_DATA chxj_module = {
3122 STANDARD20_MODULE_STUFF,
3123 chxj_create_per_dir_config, /* create per-dir config structures */
3124 chxj_merge_per_dir_config, /* merge per-dir config structures */
3125 chxj_config_server_create, /* create per-server config structures */
3126 NULL, /* merge per-server config structures */
3127 cmds, /* table of config file commands */
3128 chxj_register_hooks /* register hooks */