2 * Copyright (C) 2005-2008 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"
72 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
73 #define CHXJ_VERSION PACKAGE_VERSION
74 #define CHXJ_POST_MAX (0x1000000)
76 converter_t convert_routine[] = {
78 /* CHXJ_SPEC_UNKNOWN */
83 /* CHXJ_SPEC_Chtml_1_0 */
84 .converter = chxj_convert_chtml10,
85 .encoder = chxj_encoding,
88 /* CHXJ_SPEC_Chtml_2_0 */
89 .converter = chxj_convert_chtml20,
90 .encoder = chxj_encoding,
93 /* CHXJ_SPEC_Chtml_3_0 */
94 .converter = chxj_convert_chtml30,
95 .encoder = chxj_encoding,
98 /* CHXJ_SPEC_Chtml_4_0 */
99 .converter = chxj_convert_chtml40,
100 .encoder = chxj_encoding,
103 /* CHXJ_SPEC_Chtml_5_0 */
104 .converter = chxj_convert_chtml50,
105 .encoder = chxj_encoding,
108 /* CHXJ_SPEC_Chtml_6_0 */
109 .converter = chxj_convert_chtml50,
110 .encoder = chxj_encoding,
113 /* CHXJ_SPEC_Chtml_7_0 */
114 .converter = chxj_convert_chtml50,
115 .encoder = chxj_encoding,
118 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
119 .converter = chxj_convert_xhtml_mobile_1_0,
120 .encoder = chxj_encoding,
124 .converter = chxj_convert_hdml,
125 .encoder = chxj_encoding,
128 /* CHXJ_SPEC_Jhtml */
129 .converter = chxj_convert_jhtml,
130 .encoder = chxj_encoding,
133 /* CHXJ_SPEC_Jxhtml */
134 .converter = chxj_convert_jxhtml,
135 .encoder = chxj_encoding,
144 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp);
145 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
146 static void s_clear_cookie_header(request_rec *r, device_table *spec);
149 * Only when User-Agent is specified, the User-Agent header is camouflaged.
154 chxj_headers_fixup(request_rec *r)
156 mod_chxj_config* dconf;
157 chxjconvrule_entry* entryp;
163 DBG(r, "start chxj_headers_fixup()");
164 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
166 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
167 spec = chxj_specified_device(r, user_agent);
169 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
171 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
172 DBG(r, "detect multipart/form-data ==> no target");
173 DBG(r, "end chxj_headers_fixup()");
176 if (r->method_number == M_POST) {
177 if (!apr_table_get(r->headers_in, "X-Chxj-Forward")) {
178 s_clear_cookie_header(r, spec);
182 s_clear_cookie_header(r, spec);
185 switch(spec->html_spec_type) {
186 case CHXJ_SPEC_Chtml_1_0:
187 case CHXJ_SPEC_Chtml_2_0:
188 case CHXJ_SPEC_Chtml_3_0:
189 case CHXJ_SPEC_Chtml_4_0:
190 case CHXJ_SPEC_Chtml_5_0:
191 case CHXJ_SPEC_Chtml_6_0:
192 case CHXJ_SPEC_Chtml_7_0:
193 case CHXJ_SPEC_XHtml_Mobile_1_0:
195 case CHXJ_SPEC_Jhtml:
196 case CHXJ_SPEC_Jxhtml:
197 entryp = chxj_apply_convrule(r, dconf->convrules);
199 DBG(r, "end chxj_headers_fixup() no pattern");
202 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
207 apr_table_setn(r->headers_in,
208 CHXJ_HTTP_USER_AGENT,
211 if (entryp->user_agent)
212 apr_table_setn(r->headers_in,
216 chxj_convert_input_header(r,entryp);
226 if (r->method_number == M_POST) {
227 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
228 DBG(r, "set Input handler old:[%s] proxyreq:[%d] uri:[%s] filename:[%s]", r->handler, r->proxyreq, r->uri, r->filename);
229 r->proxyreq = PROXYREQ_NONE;
230 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
233 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
235 apr_sockaddr_t *address = NULL;
236 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
237 if (rv != APR_SUCCESS) {
239 ERR(r, "%s:%d apr_sockaddr_info_get() failed: rv:[%d|%s]", APLOG_MARK, rv, apr_strerror(rv, buf, 256));
240 DBG(r, "end chxj_headers_fixup()");
244 if (dconf->forward_server_ip) {
245 addr = dconf->forward_server_ip;
248 apr_sockaddr_ip_get(&addr, address);
250 DBG(r, "Client IP:[%s] vs Orig Client IP:[%s] vs Server IP:[%s]", r->connection->remote_ip, client_ip, addr);
251 if (strcmp(addr, r->connection->remote_ip) == 0) {
252 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
254 if (! apr_table_get(r->headers_in, "Content-Length")) {
255 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
257 apr_table_set(r->headers_in, "Content-Length", contentLength);
264 DBG(r, "end chxj_headers_fixup()");
271 s_clear_cookie_header(request_rec *r, device_table *spec)
273 switch(spec->html_spec_type) {
274 case CHXJ_SPEC_Chtml_1_0:
275 case CHXJ_SPEC_Chtml_2_0:
276 case CHXJ_SPEC_Chtml_3_0:
277 case CHXJ_SPEC_Chtml_4_0:
278 case CHXJ_SPEC_Chtml_5_0:
279 case CHXJ_SPEC_Chtml_6_0:
280 case CHXJ_SPEC_Chtml_7_0:
281 case CHXJ_SPEC_XHtml_Mobile_1_0:
282 case CHXJ_SPEC_Jhtml:
283 apr_table_unset(r->headers_in, "Cookie");
292 * It converts it from CHTML into XXML corresponding to each model.
295 * @param src [i] It is former HTML character string.
296 * @param len [i/o] It is length of former HTML character string.
299 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
305 mod_chxj_config *dconf;
306 chxjconvrule_entry *entryp;
308 DBG(r,"start of chxj_convert() input:[%.*s]", (int)*len, *src);
309 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
311 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
314 entryp = chxj_apply_convrule(r, dconf->convrules);
316 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT))
320 /*------------------------------------------------------------------------*/
321 /* get UserAgent from http header */
322 /*------------------------------------------------------------------------*/
323 if (entryp->user_agent)
324 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
326 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
328 DBG(r,"User-Agent:[%s]", user_agent);
329 DBG(r,"content type is %s", r->content_type);
332 if (! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
333 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
334 DBG(r,"no convert. content type is %s", r->content_type);
335 DBG(r,"end of chxj_convert()");
339 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
341 spec = chxj_specified_device(r, user_agent);
348 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
349 switch(spec->html_spec_type) {
350 case CHXJ_SPEC_Chtml_1_0:
351 case CHXJ_SPEC_Chtml_2_0:
352 case CHXJ_SPEC_Chtml_3_0:
353 case CHXJ_SPEC_Chtml_4_0:
354 case CHXJ_SPEC_Chtml_5_0:
355 case CHXJ_SPEC_Chtml_6_0:
356 case CHXJ_SPEC_Chtml_7_0:
357 case CHXJ_SPEC_XHtml_Mobile_1_0:
358 case CHXJ_SPEC_Jhtml:
359 cookie = chxj_save_cookie(r);
366 if (!r->header_only) {
369 if (convert_routine[spec->html_spec_type].encoder)
370 tmp = convert_routine[spec->html_spec_type].encoder(r,
374 if (convert_routine[spec->html_spec_type].converter) {
376 dst = convert_routine[spec->html_spec_type].converter(r,
384 dst = convert_routine[spec->html_spec_type].converter(r,
393 ap_set_content_length(r, *len);
396 dst = apr_psprintf(r->pool, "\n");
404 DBG(r, "end of chxj_convert()");
411 * It converts it from HEADER.
416 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp)
429 int no_update_flag = 0;
431 DBG(r, "start chxj_convert_input_header()");
434 DBG(r, "r->args=[null]");
435 DBG(r, "end chxj_convert_input_header()");
438 urilen = strlen(r->args);
440 result = qs_alloc_zero_byte_string(r->pool);
442 buff_pre = apr_pstrdup(r->pool, r->args);
447 pair = apr_strtok(buff_pre, "&", &pstate);
453 pair_sv = apr_pstrdup(r->pool, pair);
455 name = apr_strtok(pair, "=", &vstate);
456 value = apr_strtok(NULL, "=", &vstate);
457 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
458 DBG(r, "found cookie no update parameter");
463 buff = apr_pstrdup(r->pool, r->args);
464 DBG(r, "r->args=[%s]", buff);
473 pair = apr_strtok(buff, "&", &pstate);
479 pair_sv = apr_pstrdup(r->pool, pair);
481 name = apr_strtok(pair, "=", &vstate);
482 value = apr_strtok(NULL, "=", &vstate);
483 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
484 if (strlen(result) != 0)
485 result = apr_pstrcat(r->pool, result, "&", NULL);
487 if (strcasecmp(entryp->encoding, "NONE") != 0) {
492 if (value && *value != 0) {
493 value = chxj_url_decode(r->pool, value);
494 dlen = strlen(value);
495 DBG(r, "************ before encoding[%s]", value);
497 dvalue = chxj_rencoding(r, value, &dlen);
498 dvalue = chxj_url_encode(r->pool, dvalue);
500 DBG(r, "************ after encoding[%s]", dvalue);
506 if (name && *name != 0) {
507 name = chxj_url_decode(r->pool, name);
509 dname = chxj_rencoding(r, name, &dlen);
510 dname = chxj_url_encode(r->pool, dname);
516 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
519 if (strcmp(name, pair_sv) != 0)
520 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
522 result = apr_pstrcat(r->pool, result, name, NULL);
526 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
527 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
528 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
532 if (strlen(value) == 0)
535 if (strlen(result) != 0)
536 result = apr_pstrcat(r->pool, result, "&", NULL);
538 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
541 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
542 apr_table_unset(r->headers_in, "Cookie");
543 DBG(r, "found cookie parameter[%s]", value);
544 DBG(r, "call start chxj_load_cookie()");
546 cookie = chxj_load_cookie(r, value);
547 DBG(r, "call end chxj_load_cookie()");
548 if (! no_update_flag && cookie) {
549 chxj_update_cookie(r, cookie);
551 chxj_cookie_unlock(r);
556 DBG(r, "result r->args=[%s]", r->args);
557 DBG(r, "end chxj_convert_input_header()");
563 * It converts it from POSTDATA .
566 * @param src [i] It is POSTDATA character string.
567 * @param len [i/o] It is length of former HTML character string.
574 chxjconvrule_entry *entryp)
585 int no_update_flag = 0;
587 s = apr_pstrdup(r->pool, *src);
588 buff_pre = apr_pstrdup(r->pool, *src);
590 result = qs_alloc_zero_byte_string(r->pool);
592 DBG(r, "BEFORE input convert source = [%s]", s);
597 pair = apr_strtok(buff_pre, "&", &pstate);
603 pair_sv = apr_pstrdup(r->pool, pair);
605 name = apr_strtok(pair, "=", &vstate);
606 value = apr_strtok(NULL, "=", &vstate);
607 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0 || strcasecmp(name, chxj_url_encode(r->pool, CHXJ_COOKIE_NOUPDATE_PARAM)) == 0) {
608 DBG(r, "found cookie no update parameter");
618 pair = apr_strtok(s, "&", &pstate);
623 name = apr_strtok(pair, "=", &vstate);
624 value = apr_strtok(NULL, "=", &vstate);
625 if (strncasecmp(name, "_chxj", 5) != 0 && strncasecmp(name, "%5Fchxj", sizeof("%5Fchxj")-1) != 0) {
626 if (strlen(result) != 0)
627 result = apr_pstrcat(r->pool, result, "&", NULL);
629 if (strcasecmp(entryp->encoding, "NONE") != 0) {
634 if (value && *value != 0) {
635 value = chxj_url_decode(r->pool, value);
636 dlen = strlen(value);
637 DBG(r, "************ before encoding[%s]", value);
639 dvalue = chxj_rencoding(r, value, &dlen);
640 dvalue = chxj_url_encode(r->pool, dvalue);
642 DBG(r, "************ after encoding[%s]", dvalue);
648 if (name && *name != 0) {
649 name = chxj_url_decode(r->pool, name);
651 dname = chxj_rencoding(r, name, &dlen);
652 dname = chxj_url_encode(r->pool, dname);
658 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
661 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
665 if ( (strncasecmp(name, "_chxj_c_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fc%5F", sizeof("%5Fchxj%5Fc%5F")-1) == 0)
666 || (strncasecmp(name, "_chxj_r_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fr%5F", sizeof("%5Fchxj%5Fr%5F")-1) == 0)
667 || (strncasecmp(name, "_chxj_s_", 8) == 0 || strncasecmp(name, "%5Fchxj%5Fs%5F", sizeof("%5Fchxj%5Fs%5F")-1) == 0)) {
671 if (strlen(value) == 0)
674 if (strlen(result) != 0)
675 result = apr_pstrcat(r->pool, result, "&", NULL);
677 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
681 dlen = strlen(value);
682 value = chxj_url_decode(r->pool, value);
683 DBG(r, "************ before encoding[%s]", value);
685 dvalue = chxj_rencoding(r, value, &dlen);
686 dvalue = chxj_url_encode(r->pool,dvalue);
688 DBG(r, "************ after encoding[%s]", dvalue);
690 result = apr_pstrcat(r->pool, result, &name[8], "=", dvalue, NULL);
694 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
698 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0 || strcasecmp(name, "%5Fchxj%5Fcc") == 0) {
699 apr_table_unset(r->headers_in, "Cookie");
700 DBG(r, "found cookie parameter[%s]", value);
701 DBG(r, "call start chxj_load_cookie()");
702 cookie = chxj_load_cookie(r, value);
703 DBG(r, "call end chxj_load_cookie()");
704 if (! no_update_flag && cookie) {
705 chxj_update_cookie(r, cookie);
709 *len = strlen(result);
711 DBG(r, "AFTER input convert result = [%s]", result);
718 * The received data is returned to the filter.
720 * @param f [i/o] It is a filter.
721 * @param data [i] It is data returned to the filter.
722 * @param len [i] It is length of the data returned to the filter.
725 pass_data_to_filter(ap_filter_t *f, const char *data,
728 request_rec *r = f->r;
729 conn_rec *c = r->connection;
731 apr_bucket_brigade *bb;
734 DBG(r, "start pass_data_to_filter()");
736 bb = apr_brigade_create(r->pool, c->bucket_alloc);
737 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
739 APR_BRIGADE_INSERT_TAIL(bb, b);
740 b = apr_bucket_eos_create(f->c->bucket_alloc);
741 APR_BRIGADE_INSERT_TAIL(bb, b);
743 rv = ap_pass_brigade(f->next, bb);
744 if (rv != APR_SUCCESS) {
745 DBG(r, "ap_pass_brigade()");
749 DBG(r, "end pass_data_to_filter()");
756 * It is the main loop of the output filter.
758 * @param f [i/o] It is a filter.
762 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
768 char *user_agent = NULL;
770 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
771 cookie_t *cookie = NULL;
772 mod_chxj_config *dconf;
773 chxjconvrule_entry *entryp = NULL;
774 device_table *spec = NULL;
777 DBG(f->r, "start of chxj_output_filter()");
781 apr_pool_create(&pool, r->pool);
783 entryp = ctx->entryp;
785 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
787 if (r->content_type) {
788 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
789 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
790 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
791 && ! (dconf->image == CHXJ_IMG_ON
792 && ! apr_table_get(r->headers_in, "CHXJ_IMG_CONV")
793 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
794 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
795 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
796 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
797 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
798 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
799 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
800 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
801 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
803 DBG(r, "not convert content-type:[%s] dconf->image:[%d]", r->content_type, dconf->image);
804 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
805 DBG(r, "entryp->action == COOKIE_ON_BIT");
806 switch(spec->html_spec_type) {
807 case CHXJ_SPEC_Chtml_1_0:
808 case CHXJ_SPEC_Chtml_2_0:
809 case CHXJ_SPEC_Chtml_3_0:
810 case CHXJ_SPEC_Chtml_4_0:
811 case CHXJ_SPEC_Chtml_5_0:
812 case CHXJ_SPEC_Chtml_6_0:
813 case CHXJ_SPEC_Chtml_7_0:
814 case CHXJ_SPEC_XHtml_Mobile_1_0:
815 case CHXJ_SPEC_Jhtml:
817 cookie = chxj_save_cookie(r);
818 s_add_cookie_id_if_has_location_header(r, cookie);
819 chxj_cookie_unlock(r);
825 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
826 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
827 r->status = HTTP_MOVED_TEMPORARILY;
830 ap_pass_brigade(f->next, bb);
835 DBG(r, "not convert content-type:[(null)]");
836 ap_pass_brigade(f->next, bb);
841 for (b = APR_BRIGADE_FIRST(bb);
842 b != APR_BRIGADE_SENTINEL(bb);
843 b = APR_BUCKET_NEXT(b)) {
845 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
846 DBG(r, "read data[%.*s]",(int)len, data);
848 /*--------------------------------------------------------------------*/
850 /*--------------------------------------------------------------------*/
852 DBG(r, "append data start");
853 ctx = (mod_chxj_ctx *)f->ctx;
856 tmp = apr_palloc(r->pool, ctx->len);
857 memcpy(tmp, ctx->buffer, ctx->len);
859 ctx->buffer = apr_palloc(pool, ctx->len + len);
861 memcpy(ctx->buffer, tmp, ctx->len);
862 memcpy(&ctx->buffer[ctx->len], data, len);
866 DBG(r, "append data end");
869 if (APR_BUCKET_IS_EOS(b)) {
872 /*----------------------------------------------------------------------*/
874 /*----------------------------------------------------------------------*/
876 ctx = (mod_chxj_ctx *)f->ctx;
878 DBG(r, "content_type=[%s]", r->content_type);
881 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
883 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
884 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
885 DBG(r, "detect convert target:[%s]", r->content_type);
890 tmp = apr_palloc(pool, ctx->len + 1);
892 memset(tmp, 0, ctx->len + 1);
893 memcpy(tmp, ctx->buffer, ctx->len);
895 ctx->buffer = chxj_convert(r,
897 (apr_size_t *)&ctx->len,
899 user_agent, &cookie);
903 ctx->buffer = apr_psprintf(r->pool, "\n");
905 ctx->buffer = chxj_convert(r,
906 (const char **)&ctx->buffer,
907 (apr_size_t *)&ctx->len,
909 user_agent, &cookie);
914 && *(char *)r->content_type == 't'
915 && strncmp(r->content_type, "text/xml", 8) == 0) {
924 memset(&doc, 0, sizeof(Doc));
925 memset(&qrcode, 0, sizeof(qr_code_t));
927 doc.parse_mode = PARSE_MODE_CHTML;
931 qs_init_malloc(&doc);
933 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
936 for (child = qs_get_child_node(&doc,root);
938 child = qs_get_next_node(&doc,child)) {
939 char *name = qs_get_node_name(&doc,child);
940 if (strcasecmp("qrcode",name) == 0) {
945 qs_all_free(&doc,QX_LOGMARK);
947 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
948 chxj_qrcode_node_to_qrcode(&qrcode, root);
949 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
951 ERR(r, "qrcode create failed.");
952 chxj_cookie_unlock(r);
955 r->content_type = apr_psprintf(r->pool, "image/jpeg");
959 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
961 && ( *r->content_type == 'i' || *r->content_type == 'I')
962 && dconf->image == CHXJ_IMG_ON
963 && strncasecmp("image/", r->content_type, 6) == 0
964 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
965 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
966 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
967 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
968 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
969 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
970 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
971 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
975 tmp = apr_palloc(pool, ctx->len + 1);
977 memset(tmp, 0, ctx->len + 1);
978 memcpy(tmp, ctx->buffer, ctx->len);
980 chxj_convert_image(r,
982 (apr_size_t *)&ctx->len);
983 if (ctx->buffer == NULL) {
989 apr_table_unset(r->headers_out, "Content-Length");
990 apr_table_unset(r->err_headers_out, "Content-Length");
991 ap_set_content_length(r, (apr_off_t)ctx->len);
993 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
994 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
995 r->status = HTTP_MOVED_TEMPORARILY;
1000 DBG(r, "call pass_data_to_filter()");
1001 s_add_cookie_id_if_has_location_header(r, cookie);
1002 chxj_cookie_unlock(r);
1003 rv = pass_data_to_filter(f,
1004 (const char *)ctx->buffer,
1005 (apr_size_t)ctx->len);
1008 chxj_cookie_unlock(r);
1013 DBG(r, " SAVE COOKIE[%x]", entryp->action);
1018 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
1019 DBG(r, "entryp->action == COOKIE_ON_BIT");
1020 switch(spec->html_spec_type) {
1021 case CHXJ_SPEC_Chtml_1_0:
1022 case CHXJ_SPEC_Chtml_2_0:
1023 case CHXJ_SPEC_Chtml_3_0:
1024 case CHXJ_SPEC_Chtml_4_0:
1025 case CHXJ_SPEC_Chtml_5_0:
1026 case CHXJ_SPEC_Chtml_6_0:
1027 case CHXJ_SPEC_Chtml_7_0:
1028 case CHXJ_SPEC_XHtml_Mobile_1_0:
1029 case CHXJ_SPEC_Jhtml:
1030 chxj_cookie_lock(r);
1031 cookie = chxj_save_cookie(r);
1033 * Location Header Check to add cookie parameter.
1035 s_add_cookie_id_if_has_location_header(r, cookie);
1036 chxj_cookie_unlock(r);
1037 apr_table_unset(r->headers_out, "Set-Cookie");
1038 apr_table_unset(r->err_headers_out, "Set-Cookie");
1045 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1046 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1047 r->status = HTTP_MOVED_TEMPORARILY;
1050 apr_table_setn(r->headers_out, "Content-Length", "0");
1051 DBG(r, "call pass_data_to_filter()");
1052 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1057 apr_brigade_destroy(bb);
1059 DBG(r, "end of output filter");
1065 * Add Cookie_id if it has location header.
1068 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1070 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1071 if (! location_header) {
1072 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1074 if (cookie && location_header) {
1075 DBG(r, "Location Header=[%s]", location_header);
1076 location_header = chxj_add_cookie_parameter(r,
1079 apr_table_unset(r->headers_out, "Location");
1080 apr_table_setn(r->headers_out, "Location", location_header);
1081 DBG(r, "Location Header=[%s]", location_header);
1082 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1083 r->status = HTTP_MOVED_TEMPORARILY;
1089 * It is the main loop of the input filter handler.
1093 chxj_input_handler(request_rec *r)
1095 mod_chxj_config *dconf;
1096 chxjconvrule_entry *entryp = NULL;
1097 device_table *spec = NULL;
1098 char *post_data = NULL;
1099 apr_size_t post_data_len = 0;
1104 DBG(r, "start of chxj_input_handler()");
1106 if (strcasecmp(r->handler, "chxj-input-handler")) {
1107 DBG(r, "end chxj_input_handler()");
1110 apr_pool_create(&pool, r->pool);
1112 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1113 user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
1114 spec = chxj_specified_device(r, user_agent);
1115 entryp = chxj_apply_convrule(r, dconf->convrules);
1117 post_data = apr_pstrdup(pool, "");
1118 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1119 if (ap_should_client_block(r)) {
1120 while (post_data_len < CHXJ_POST_MAX) {
1123 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1124 if (read_bytes<=0) {
1127 buffer[read_bytes] = '\0';
1128 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1129 post_data_len += read_bytes;
1138 if (post_data_len > 0) {
1139 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp);
1140 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1144 if (dconf->forward_url_base) {
1145 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1148 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);
1151 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1153 DBG(r, "==> new url_path:[%s]", url_path);
1156 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1157 apr_table_unset(r->headers_in, "Content-Length");
1158 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%" APR_SIZE_T_FMT, post_data_len));
1159 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len);
1160 DBG(r, "REQ[%X] response:[%.*s][%" APR_SIZE_T_FMT "]", (unsigned int)(apr_size_t)r, (unsigned int)res_len, response, res_len);
1163 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1164 if (strcasecmp(chunked, "chunked") == 0) {
1166 apr_table_unset(r->headers_out, "Transfer-Encoding");
1171 apr_pool_create(&wpool, r->pool);
1172 apr_bucket_brigade *bb;
1175 conn_rec *c = r->connection;
1177 bb = apr_brigade_create(wpool, c->bucket_alloc);
1178 e = apr_bucket_heap_create(response, res_len, NULL, c->bucket_alloc);
1179 APR_BRIGADE_INSERT_TAIL(bb, e);
1180 e = apr_bucket_eos_create(c->bucket_alloc);
1181 APR_BRIGADE_INSERT_TAIL(bb, e);
1182 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1183 ERR(r, "%s:%d failed ap_pass_brigade()", APLOG_MARK);
1186 apr_brigade_cleanup(bb);
1189 DBG(r, "end of chxj_input_handler()");
1193 static mod_chxj_global_config *
1194 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1196 mod_chxj_global_config *conf;
1198 SDBG(s, "start chxj_global_config_create()");
1200 /*--------------------------------------------------------------------------*/
1201 /* allocate an own subpool which survives server restarts */
1202 /*--------------------------------------------------------------------------*/
1203 conf = (mod_chxj_global_config *)apr_palloc(pool,
1204 sizeof(mod_chxj_global_config));
1206 conf->cookie_db_lock = NULL;
1208 SDBG(s, "end chxj_global_config_create()");
1215 * initialize chxj module
1218 chxj_init_module(apr_pool_t *p,
1219 apr_pool_t *UNUSED(plog),
1220 apr_pool_t *UNUSED(ptemp),
1226 SDBG(s, "start chxj_init_module()");
1228 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1230 if (user_data == NULL) {
1233 * dummy user_data set.
1235 apr_pool_userdata_set(
1237 CHXJ_MOD_CONFIG_KEY,
1238 apr_pool_cleanup_null,
1240 SDBG(s, "end chxj_init_module()");
1244 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1246 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1248 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1249 return HTTP_INTERNAL_SERVER_ERROR;
1252 SDBG(s, "end chxj_init_module()");
1259 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1262 SDBG(s, "start chxj_child_init()");
1263 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1265 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1267 SDBG(s, "end chxj_child_init()");
1272 * A set structure of each server is generated.
1278 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1280 mod_chxj_global_config *gc;
1282 gc = chxj_global_config_create(p,s);
1289 chxj_translate_name(request_rec *r)
1291 return chxj_trans_name(r);
1296 chxj_insert_filter(request_rec *r)
1300 mod_chxj_config *dconf;
1301 chxjconvrule_entry *entryp;
1306 DBG(r, "start chxj_insert_filter()");
1308 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1310 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1312 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1314 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1315 DBG(r, "detect multipart/form-data ==> no target");
1319 spec = chxj_specified_device(r, user_agent);
1320 entryp = chxj_apply_convrule(r, dconf->convrules);
1322 DBG(r, "end chxj_insert_filter()");
1325 ctx = apr_palloc(r->pool, sizeof(*ctx));
1326 memset(ctx, 0, sizeof(*ctx));
1327 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1328 ERR(r, "failed: new pool create. rv:[%d]", rv);
1331 ctx->entryp = entryp;
1333 ctx->buffer = apr_palloc(ctx->pool, 1);
1336 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1341 switch(spec->html_spec_type) {
1342 case CHXJ_SPEC_Chtml_1_0:
1343 case CHXJ_SPEC_Chtml_2_0:
1344 case CHXJ_SPEC_Chtml_3_0:
1345 case CHXJ_SPEC_Chtml_4_0:
1346 case CHXJ_SPEC_Chtml_5_0:
1347 case CHXJ_SPEC_Chtml_6_0:
1348 case CHXJ_SPEC_Chtml_7_0:
1349 case CHXJ_SPEC_XHtml_Mobile_1_0:
1350 case CHXJ_SPEC_Hdml:
1351 case CHXJ_SPEC_Jhtml:
1352 case CHXJ_SPEC_Jxhtml:
1360 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1361 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1362 DBG(r, "added Output Filter");
1365 DBG(r, "end chxj_insert_filter()");
1370 * The hook is registered.
1375 chxj_register_hooks(apr_pool_t *UNUSED(p))
1377 ap_hook_post_config(chxj_init_module,
1380 APR_HOOK_REALLY_FIRST);
1381 ap_hook_child_init(chxj_child_init,
1384 APR_HOOK_REALLY_FIRST);
1385 ap_register_output_filter (
1386 "chxj_output_filter",
1390 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1391 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1392 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1393 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1394 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1395 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1400 * A set structure according to the directory is generated.
1406 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1408 mod_chxj_config *conf;
1410 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1411 conf->device_data_file = NULL;
1412 conf->devices = NULL;
1413 conf->emoji_data_file = NULL;
1415 conf->emoji_tail = NULL;
1416 conf->image = CHXJ_IMG_NONE;
1417 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1418 conf->image_cache_limit = 0;
1419 conf->server_side_encoding = NULL;
1420 conf->cookie_db_dir = NULL;
1421 conf->cookie_timeout = 0;
1422 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1423 conf->cookie_lazy_mode = 0;
1424 #if defined(USE_MYSQL_COOKIE)
1425 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1426 conf->mysql.port = MYSQL_PORT;
1427 conf->mysql.host = NULL;
1429 #if defined(USE_MEMCACHE_COOKIE)
1430 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1431 conf->memcache.host = NULL;
1432 conf->memcache.port = 0;
1434 conf->forward_url_base = NULL;
1435 conf->forward_server_ip = NULL;
1441 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1442 memset(conf->dir, 0, strlen(arg)+1);
1443 strcpy(conf->dir, arg);
1445 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1447 /* Default is copyleft */
1448 conf->image_copyright = NULL;
1455 * Merge per-directory CHXJ configurations
1458 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1460 mod_chxj_config *base;
1461 mod_chxj_config *add;
1462 mod_chxj_config *mrg;
1464 base = (mod_chxj_config *)basev;
1465 add = (mod_chxj_config *)addv;
1466 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1468 mrg->device_data_file = NULL;
1469 mrg->devices = NULL;
1470 mrg->emoji_data_file = NULL;
1471 mrg->image = CHXJ_IMG_NONE;
1472 mrg->image_cache_dir = NULL;
1473 mrg->image_copyright = NULL;
1474 mrg->image_cache_limit = 0;
1476 mrg->emoji_tail = NULL;
1477 mrg->new_line_type = NLTYPE_NIL;
1478 mrg->forward_url_base = NULL;
1479 mrg->forward_server_ip = NULL;
1481 mrg->dir = apr_pstrdup(p, add->dir);
1483 if (! add->device_data_file) {
1484 mrg->devices = base->devices;
1485 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1488 mrg->devices = add->devices;
1489 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1492 if (! add->emoji_data_file) {
1493 mrg->emoji = base->emoji;
1494 mrg->emoji_tail = base->emoji_tail;
1495 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1498 mrg->emoji = add->emoji;
1499 mrg->emoji_tail = add->emoji_tail;
1500 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1503 if (add->image == CHXJ_IMG_NONE) {
1504 mrg->image = base->image;
1507 mrg->image = add->image;
1510 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1511 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1514 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1517 if (add->image_cache_limit) {
1518 mrg->image_cache_limit = add->image_cache_limit;
1521 mrg->image_cache_limit = base->image_cache_limit;
1524 if (add->image_copyright)
1525 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1527 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1529 if (add->server_side_encoding) {
1530 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1533 if (base->server_side_encoding) {
1534 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1537 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1540 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1542 if (add->cookie_db_dir) {
1543 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1546 if (base->cookie_db_dir) {
1547 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1550 mrg->cookie_db_dir = NULL;
1553 if (add->cookie_timeout) {
1554 mrg->cookie_timeout = add->cookie_timeout;
1557 if (base->cookie_db_dir) {
1558 mrg->cookie_timeout = base->cookie_timeout;
1561 mrg->cookie_timeout = 0;
1564 #if defined(USE_MYSQL_COOKIE)
1565 if (add->mysql.host) {
1566 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1568 else if (base->mysql.host) {
1569 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1572 mrg->mysql.host = NULL;
1574 if (add->mysql.port) {
1575 mrg->mysql.port = add->mysql.port;
1577 else if (base->mysql.port) {
1578 mrg->mysql.port = base->mysql.port;
1581 mrg->mysql.port = 0;
1584 if (add->mysql.database) {
1585 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1587 else if (base->mysql.database) {
1588 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1591 mrg->mysql.database = NULL;
1594 if (add->mysql.username) {
1595 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1597 else if (base->mysql.username) {
1598 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1601 mrg->mysql.username = NULL;
1604 if (add->mysql.password) {
1605 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1607 else if (base->mysql.password) {
1608 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1611 mrg->mysql.password = NULL;
1614 if (add->mysql.tablename) {
1615 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1617 else if (base->mysql.tablename) {
1618 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1621 mrg->mysql.tablename = NULL;
1624 if (add->mysql.socket_path) {
1625 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1627 else if (base->mysql.socket_path) {
1628 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1631 mrg->mysql.socket_path = NULL;
1634 if (add->mysql.charset) {
1635 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1637 else if (base->mysql.charset) {
1638 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1641 mrg->mysql.charset = NULL;
1644 #if defined(USE_MEMCACHE_COOKIE)
1645 if (add->memcache.host) {
1646 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1648 else if (base->memcache.host) {
1649 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1652 mrg->memcache.host = NULL;
1654 if (add->memcache.port) {
1655 mrg->memcache.port = add->memcache.port;
1657 else if (base->memcache.port) {
1658 mrg->memcache.port = base->memcache.port;
1661 mrg->memcache.port = 0;
1664 if (add->cookie_store_type) {
1665 mrg->cookie_store_type = add->cookie_store_type;
1667 else if (base->cookie_store_type) {
1668 mrg->cookie_store_type = base->cookie_store_type;
1671 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1673 if (add->cookie_lazy_mode) {
1674 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1676 else if (base->cookie_lazy_mode) {
1677 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1680 mrg->cookie_lazy_mode = 0;
1682 if (add->new_line_type) {
1683 mrg->new_line_type = add->new_line_type;
1685 else if (base->new_line_type) {
1686 mrg->new_line_type = base->new_line_type;
1689 mrg->new_line_type = NLTYPE_NIL;
1696 chxj_command_parse_take5(
1709 for (;*strp == ' '||*strp == '\t'; strp++) ;
1719 for (; *strp != '\0'; strp++) {
1720 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1721 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1726 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1727 || (isquoted && *strp == '"'))
1741 for (;*strp == ' '||*strp == '\t'; strp++) ;
1750 for (; *strp != '\0'; strp++) {
1751 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1752 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1757 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1758 || (isquoted && *strp == '"'))
1771 for (;*strp == ' '||*strp == '\t'; strp++);
1779 for (; *strp != '\0'; strp++) {
1780 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1781 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1786 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1787 || (isquoted && *strp == '"'))
1799 for (;*strp == ' '||*strp == '\t'; strp++);
1807 for (; *strp != '\0'; strp++) {
1808 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1809 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1814 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1815 || (isquoted && *strp == '"'))
1826 for (;*strp == ' '||*strp == '\t'; strp++);
1834 for (; *strp != '\0'; strp++) {
1835 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1836 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1841 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1842 || (isquoted && *strp == '"'))
1852 * The device definition file is loaded.
1854 * @param arg [i] The name of the device definition file is specified.
1855 * @param mconfig [i/o] The pointer to a set structure is specified.
1859 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
1861 mod_chxj_config *conf;
1866 if (strlen(arg) > 256)
1867 return "mod_chxj: device data filename too long.";
1869 conf = (mod_chxj_config *)mconfig;
1870 conf->device_data_file = apr_pstrdup(parms->pool, arg);
1872 qs_init_malloc(&doc);
1873 qs_init_root_node(&doc);
1875 qs_parse_file((Doc *)&doc, (const char *)arg);
1876 chxj_load_device_data(&doc,parms->pool, conf);
1877 qs_all_free(&doc, QX_LOGMARK);
1884 * Device definition information is loaded.
1887 * @param arg [i] The name of the device definition file is specified.
1888 * @param mconfig [i/o] The pointer to a set structure is specified.
1892 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
1894 mod_chxj_config *conf;
1901 if (strlen(arg) > 256)
1902 return "mod_chxj: emoji data filename too long.";
1904 conf = (mod_chxj_config *)mconfig;
1905 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
1906 qs_init_malloc(&doc);
1907 qs_init_root_node(&doc);
1909 qs_parse_file((Doc *)&doc, (const char *)arg);
1911 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
1913 qs_all_free(&doc, QX_LOGMARK);
1921 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
1923 mod_chxj_config *conf;
1928 if (strlen(arg) > 256)
1929 return "image uri is too long.";
1931 conf = (mod_chxj_config*)mconfig;
1932 if (strcasecmp("ON", arg) == 0) {
1933 conf->image = CHXJ_IMG_ON;
1936 conf->image = CHXJ_IMG_OFF;
1944 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
1946 mod_chxj_config *conf;
1951 if (strlen(arg) > 256)
1952 return "cache dir name is too long.";
1954 conf = (mod_chxj_config *)mconfig;
1955 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
1962 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
1964 mod_chxj_config *conf;
1969 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
1970 return "cache size is too long.";
1972 conf = (mod_chxj_config *)mconfig;
1975 * I use strtol function because strtoul is not portable function.
1977 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
1980 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
1982 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
1991 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
1993 mod_chxj_config *conf;
1998 if (strlen(arg) > 256)
1999 return "Copyright Flag is too long.";
2001 conf = (mod_chxj_config *)mconfig;
2002 conf->image_copyright = apr_pstrdup(parms->pool, arg);
2009 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
2013 mod_chxj_config *dconf;
2014 chxjconvrule_entry *newrule;
2024 dconf = (mod_chxj_config *)mconfig;
2026 if (strlen(arg) > 4096)
2027 return "mod_chxj: ChxjConvertRule: is too long.";
2029 dconf = (mod_chxj_config *)mconfig;
2030 if (dconf->convrules == NULL)
2031 dconf->convrules = apr_array_make(cmd->pool,
2033 sizeof(chxjconvrule_entry));
2035 newrule = apr_array_push(dconf->convrules);
2038 newrule->action = 0;
2040 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2041 return "ChxjConvertRule: bad argument line";
2043 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2047 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2053 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2054 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2057 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2058 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2064 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2065 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2075 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2079 mode = AP_REG_EXTENDED;
2080 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2081 return "RewriteRule: cannot compile regular expression ";
2083 newrule->regexp = regexp;
2085 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2087 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2089 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2091 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2092 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2094 newrule->user_agent = NULL;
2096 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2108 mod_chxj_config *dconf;
2111 if (strlen(arg) > 4096)
2112 return "mod_chxj: ChxjCookieDir is too long.";
2114 dconf = (mod_chxj_config *)mconfig;
2116 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2123 cmd_set_cookie_timeout(
2124 cmd_parms *UNUSED(cmd),
2128 mod_chxj_config *dconf;
2130 if (strlen(arg) > 4096)
2131 return "mod_chxj: ChxjCookieTimeout is too long.";
2133 if (chxj_chk_numeric(arg) != 0)
2134 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2136 dconf = (mod_chxj_config *)mconfig;
2138 dconf->cookie_timeout = atoi(arg);
2144 #if defined(USE_MYSQL_COOKIE)
2146 cmd_set_cookie_mysql_database(
2151 mod_chxj_config *dconf;
2153 if (strlen(arg) > 255)
2154 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2156 dconf = (mod_chxj_config *)mconfig;
2158 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2165 cmd_set_cookie_mysql_username(
2170 mod_chxj_config *dconf;
2172 if (strlen(arg) > 255)
2173 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2175 dconf = (mod_chxj_config *)mconfig;
2177 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2184 cmd_set_cookie_mysql_password(
2189 mod_chxj_config *dconf;
2191 if (strlen(arg) > 255)
2192 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2194 dconf = (mod_chxj_config *)mconfig;
2196 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2203 cmd_set_cookie_mysql_table_name(
2208 mod_chxj_config *dconf;
2210 if (strlen(arg) > 255)
2211 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2213 dconf = (mod_chxj_config *)mconfig;
2215 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2221 cmd_set_cookie_mysql_port(
2222 cmd_parms *UNUSED(cmd),
2226 mod_chxj_config *dconf;
2228 if (strlen(arg) > 255)
2229 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2231 dconf = (mod_chxj_config *)mconfig;
2233 if (chxj_chk_numeric(arg) != 0)
2234 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2236 dconf = (mod_chxj_config *)mconfig;
2238 dconf->mysql.port = chxj_atoi(arg);
2245 cmd_set_cookie_mysql_host(
2250 mod_chxj_config *dconf;
2252 if (strlen(arg) > 255)
2253 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2255 dconf = (mod_chxj_config *)mconfig;
2257 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2264 cmd_set_cookie_mysql_socket_path(
2269 mod_chxj_config *dconf;
2271 if (strlen(arg) > 4096)
2272 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2274 dconf = (mod_chxj_config *)mconfig;
2276 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2283 cmd_set_cookie_mysql_charset(
2288 mod_chxj_config *dconf;
2290 if (strlen(arg) > 255)
2291 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2293 dconf = (mod_chxj_config *)mconfig;
2295 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2300 #if defined(USE_MEMCACHE_COOKIE)
2302 cmd_set_cookie_memcache_port(
2303 cmd_parms *UNUSED(cmd),
2307 mod_chxj_config *dconf;
2309 if (strlen(arg) > 255)
2310 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2312 dconf = (mod_chxj_config *)mconfig;
2314 if (chxj_chk_numeric(arg) != 0)
2315 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2317 dconf = (mod_chxj_config *)mconfig;
2319 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2326 cmd_set_cookie_memcache_host(
2331 mod_chxj_config *dconf;
2333 if (strlen(arg) > 255)
2334 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2336 dconf = (mod_chxj_config *)mconfig;
2338 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2345 cmd_set_cookie_lazy_mode(
2346 cmd_parms *UNUSED(cmd),
2350 mod_chxj_config *dconf;
2352 if (strlen(arg) > 255)
2353 return "mod_chxj: ChxjCookieLazyMode is too long.";
2355 dconf = (mod_chxj_config *)mconfig;
2357 if (strcasecmp("TRUE",arg) == 0) {
2358 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2361 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2368 cmd_set_cookie_store_type(
2369 cmd_parms *UNUSED(cmd),
2373 mod_chxj_config *dconf;
2375 if (strlen(arg) > 255)
2376 return "mod_chxj: ChxjCookieStoreType is too long.";
2378 dconf = (mod_chxj_config *)mconfig;
2380 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2381 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2383 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2384 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2386 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2387 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2390 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2397 cmd_set_forward_url_base(
2402 mod_chxj_config *dconf;
2404 if (strlen(arg) > 255)
2405 return "mod_chxj: ChxjForwardUrlBase is too long.";
2407 dconf = (mod_chxj_config *)mconfig;
2409 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2415 cmd_set_forward_server_ip(
2420 mod_chxj_config *dconf;
2422 if (strlen(arg) > 255)
2423 return "mod_chxj: ChxjForwardServerIp is too long.";
2425 dconf = (mod_chxj_config *)mconfig;
2427 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2433 cmd_set_new_line_type(
2434 cmd_parms *UNUSED(cmd),
2438 mod_chxj_config *dconf;
2439 if (strlen(arg) > 255)
2440 return "mod_chxj: ChxjNewLineType is too long.";
2442 dconf = (mod_chxj_config *)mconfig;
2444 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2445 dconf->new_line_type = NLTYPE_CRLF;
2447 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2448 dconf->new_line_type = NLTYPE_LF;
2450 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2451 dconf->new_line_type = NLTYPE_CR;
2453 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2454 dconf->new_line_type = NLTYPE_NONE;
2457 return "mod_chxj: invalid value (ChxjNewLineType)";
2463 static const command_rec cmds[] = {
2465 "ChxjLoadDeviceData",
2466 cmd_load_device_data,
2469 "Load Device Data"),
2471 "ChxjLoadEmojiData",
2472 cmd_load_emoji_data,
2478 cmd_set_image_engine,
2481 "Convert Target URI"),
2483 "ChxjImageCacheDir",
2484 cmd_set_image_cache_dir,
2487 "Image Cache Directory"),
2489 "ChxjImageCacheLimit",
2490 cmd_set_image_cache_limit,
2493 "Image Cache Limit"),
2495 "ChxjImageCopyright",
2496 cmd_set_image_copyright,
2505 "an URL-applied regexp-pattern and a substitution URL"),
2511 "save cookie.db directory."),
2513 "ChxjCookieTimeout",
2514 cmd_set_cookie_timeout,
2517 "The compulsion time-out time of the cookie is specified. "),
2519 "ChxjCookieStoreType",
2520 cmd_set_cookie_store_type,
2523 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2525 "ChxjCookieLazyMode",
2526 cmd_set_cookie_lazy_mode,
2529 "OneTimeID is negligently done. (TRUE/FALSE)"),
2530 #if defined(USE_MYSQL_COOKIE)
2532 "ChxjCookieMysqlHost",
2533 cmd_set_cookie_mysql_host,
2536 "The MySQL database host used by saving Cookie"),
2538 "ChxjCookieMysqlPort",
2539 cmd_set_cookie_mysql_port,
2542 "The MySQL database port used by saving Cookie"),
2544 "ChxjCookieMysqlDatabase",
2545 cmd_set_cookie_mysql_database,
2548 "The MySQL database name used by saving Cookie"),
2550 "ChxjCookieMysqlUsername",
2551 cmd_set_cookie_mysql_username,
2554 "The MySQL username used by saving Cookie"),
2556 "ChxjCookieMysqlPassword",
2557 cmd_set_cookie_mysql_password,
2560 "The MySQL password used by saving Cookie"),
2562 "ChxjCookieMysqlTableName",
2563 cmd_set_cookie_mysql_table_name,
2566 "The MySQL table name used by saving Cookie"),
2568 "ChxjCookieMysqlSocketPath",
2569 cmd_set_cookie_mysql_socket_path,
2572 "The MySQL socket path used by saving Cookie"),
2574 "ChxjCookieMysqlCharset",
2575 cmd_set_cookie_mysql_charset,
2578 "The MySQL charset used by saving Cookie"),
2580 #if defined(USE_MEMCACHE_COOKIE)
2582 "ChxjCookieMemcacheHost",
2583 cmd_set_cookie_memcache_host,
2586 "The Memcached host used by saving Cookie"),
2588 "ChxjCookieMemcachePort",
2589 cmd_set_cookie_memcache_port,
2592 "The Memcached port used by saving Cookie"),
2596 cmd_set_new_line_type,
2599 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
2601 "ChxjForwardUrlBase",
2602 cmd_set_forward_url_base,
2605 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2607 "ChxjForwardServerIp",
2608 cmd_set_forward_server_ip,
2611 "The forward server ip(default: this server ip)"),
2616 /*----------------------------------------------------------------------------*/
2617 /* Dispatch list for API hooks */
2618 /*----------------------------------------------------------------------------*/
2619 module AP_MODULE_DECLARE_DATA chxj_module = {
2620 STANDARD20_MODULE_STUFF,
2621 chxj_create_per_dir_config, /* create per-dir config structures */
2622 chxj_merge_per_dir_config, /* merge per-dir config structures */
2623 chxj_config_server_create, /* create per-server config structures */
2624 NULL, /* merge per-server config structures */
2625 cmds, /* table of config file commands */
2626 chxj_register_hooks /* register hooks */