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);
148 * Only when User-Agent is specified, the User-Agent header is camouflaged.
153 chxj_headers_fixup(request_rec *r)
155 mod_chxj_config* dconf;
156 chxjconvrule_entry* entryp;
162 DBG(r, "start chxj_headers_fixup()");
163 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
165 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
166 spec = chxj_specified_device(r, user_agent);
168 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
170 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
171 DBG(r, "detect multipart/form-data ==> no target");
172 DBG(r, "end chxj_headers_fixup()");
176 switch(spec->html_spec_type) {
177 case CHXJ_SPEC_Chtml_1_0:
178 case CHXJ_SPEC_Chtml_2_0:
179 case CHXJ_SPEC_Chtml_3_0:
180 case CHXJ_SPEC_Chtml_4_0:
181 case CHXJ_SPEC_Chtml_5_0:
182 case CHXJ_SPEC_Chtml_6_0:
183 case CHXJ_SPEC_Chtml_7_0:
184 case CHXJ_SPEC_XHtml_Mobile_1_0:
186 case CHXJ_SPEC_Jhtml:
187 case CHXJ_SPEC_Jxhtml:
188 entryp = chxj_apply_convrule(r, dconf->convrules);
190 DBG(r, "end chxj_headers_fixup() no pattern");
193 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
198 apr_table_setn(r->headers_in,
199 CHXJ_HTTP_USER_AGENT,
202 if (entryp->user_agent)
203 apr_table_setn(r->headers_in,
207 chxj_convert_input_header(r,entryp);
217 if (r->method_number == M_POST) {
218 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
219 DBG(r, "set Input handler old:[%s] proxyreq:[%d] uri:[%s] filename:[%s]", r->handler, r->proxyreq, r->uri, r->filename);
220 r->proxyreq = PROXYREQ_NONE;
221 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
224 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
226 apr_sockaddr_t *address = NULL;
227 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
228 if (rv != APR_SUCCESS) {
230 ERR(r, "%s:%d apr_sockaddr_info_get() failed: rv:[%d|%s]", APLOG_MARK, rv, apr_strerror(rv, buf, 256));
231 DBG(r, "end chxj_headers_fixup()");
235 if (dconf->forward_server_ip) {
236 addr = dconf->forward_server_ip;
239 apr_sockaddr_ip_get(&addr, address);
241 DBG(r, "Client IP:[%s] vs Orig Client IP:[%s] vs Server IP:[%s]", r->connection->remote_ip, client_ip, addr);
242 if (strcmp(addr, r->connection->remote_ip) == 0) {
243 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
245 if (! apr_table_get(r->headers_in, "Content-Length")) {
246 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
248 apr_table_set(r->headers_in, "Content-Length", contentLength);
255 DBG(r, "end chxj_headers_fixup()");
261 * It converts it from CHTML into XXML corresponding to each model.
264 * @param src [i] It is former HTML character string.
265 * @param len [i/o] It is length of former HTML character string.
268 chxj_convert(request_rec *r, const char **src, apr_size_t *len, device_table *spec, const char *ua, cookie_t **cookiep)
274 mod_chxj_config *dconf;
275 chxjconvrule_entry *entryp;
277 DBG(r,"start of chxj_convert() input:[%.*s]", (int)*len, *src);
278 dst = apr_pstrcat(r->pool, (char *)*src, NULL);
280 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
283 entryp = chxj_apply_convrule(r, dconf->convrules);
285 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT))
289 /*------------------------------------------------------------------------*/
290 /* get UserAgent from http header */
291 /*------------------------------------------------------------------------*/
292 if (entryp->user_agent)
293 user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
295 user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
297 DBG(r,"User-Agent:[%s]", user_agent);
298 DBG(r,"content type is %s", r->content_type);
301 if (! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
302 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
303 DBG(r,"no convert. content type is %s", r->content_type);
304 DBG(r,"end of chxj_convert()");
308 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
310 spec = chxj_specified_device(r, user_agent);
317 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
318 switch(spec->html_spec_type) {
319 case CHXJ_SPEC_Chtml_1_0:
320 case CHXJ_SPEC_Chtml_2_0:
321 case CHXJ_SPEC_Chtml_3_0:
322 case CHXJ_SPEC_Chtml_4_0:
323 case CHXJ_SPEC_Chtml_5_0:
324 case CHXJ_SPEC_Chtml_6_0:
325 case CHXJ_SPEC_Chtml_7_0:
326 case CHXJ_SPEC_XHtml_Mobile_1_0:
327 case CHXJ_SPEC_Jhtml:
328 cookie = chxj_save_cookie(r);
335 if (!r->header_only) {
338 if (convert_routine[spec->html_spec_type].encoder)
339 tmp = convert_routine[spec->html_spec_type].encoder(r,
343 if (convert_routine[spec->html_spec_type].converter) {
345 dst = convert_routine[spec->html_spec_type].converter(r,
353 dst = convert_routine[spec->html_spec_type].converter(r,
362 ap_set_content_length(r, *len);
365 dst = apr_psprintf(r->pool, "\n");
373 DBG(r, "end of chxj_convert()");
380 * It converts it from HEADER.
385 chxj_convert_input_header(request_rec *r,chxjconvrule_entry *entryp)
398 int no_update_flag = 0;
400 DBG(r, "start chxj_convert_input_header()");
403 DBG(r, "r->args=[null]");
404 DBG(r, "end chxj_convert_input_header()");
407 urilen = strlen(r->args);
409 result = qs_alloc_zero_byte_string(r->pool);
411 buff_pre = apr_pstrdup(r->pool, r->args);
416 pair = apr_strtok(buff_pre, "&", &pstate);
422 pair_sv = apr_pstrdup(r->pool, pair);
424 name = apr_strtok(pair, "=", &vstate);
425 value = apr_strtok(NULL, "=", &vstate);
426 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
427 DBG(r, "found cookie no update parameter");
432 buff = apr_pstrdup(r->pool, r->args);
433 DBG(r, "r->args=[%s]", buff);
442 pair = apr_strtok(buff, "&", &pstate);
448 pair_sv = apr_pstrdup(r->pool, pair);
450 name = apr_strtok(pair, "=", &vstate);
451 value = apr_strtok(NULL, "=", &vstate);
452 if (strncasecmp(name, "_chxj", 5) != 0) {
453 if (strlen(result) != 0)
454 result = apr_pstrcat(r->pool, result, "&", NULL);
456 if (strcasecmp(entryp->encoding, "NONE") != 0) {
461 if (value && *value != 0) {
462 value = chxj_url_decode(r->pool, value);
463 dlen = strlen(value);
464 DBG(r, "************ before encoding[%s]", value);
466 dvalue = chxj_rencoding(r, value, &dlen);
467 dvalue = chxj_url_encode(r->pool, dvalue);
469 DBG(r, "************ after encoding[%s]", dvalue);
475 if (name && *name != 0) {
476 name = chxj_url_decode(r->pool, name);
478 dname = chxj_rencoding(r, name, &dlen);
479 dname = chxj_url_encode(r->pool, dname);
485 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
488 if (strcmp(name, pair_sv) != 0)
489 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
491 result = apr_pstrcat(r->pool, result, name, NULL);
495 if (strncasecmp(name, "_chxj_c_", 8) == 0
496 || strncasecmp(name, "_chxj_r_", 8) == 0
497 || strncasecmp(name, "_chxj_s_", 8) == 0) {
501 if (strlen(value) == 0)
504 if (strlen(result) != 0)
505 result = apr_pstrcat(r->pool, result, "&", NULL);
507 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
510 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0) {
511 DBG(r, "found cookie parameter[%s]", value);
512 DBG(r, "call start chxj_load_cookie()");
514 cookie = chxj_load_cookie(r, value);
515 DBG(r, "call end chxj_load_cookie()");
516 if (! no_update_flag && cookie) {
517 chxj_update_cookie(r, cookie);
519 chxj_cookie_unlock(r);
524 DBG(r, "result r->args=[%s]", r->args);
525 DBG(r, "end chxj_convert_input_header()");
531 * It converts it from POSTDATA .
534 * @param src [i] It is POSTDATA character string.
535 * @param len [i/o] It is length of former HTML character string.
542 chxjconvrule_entry *entryp)
553 int no_update_flag = 0;
555 s = apr_pstrdup(r->pool, *src);
556 buff_pre = apr_pstrdup(r->pool, *src);
558 result = qs_alloc_zero_byte_string(r->pool);
560 DBG(r, "BEFORE input convert source = [%s]", s);
565 pair = apr_strtok(buff_pre, "&", &pstate);
571 pair_sv = apr_pstrdup(r->pool, pair);
573 name = apr_strtok(pair, "=", &vstate);
574 value = apr_strtok(NULL, "=", &vstate);
575 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
576 DBG(r, "found cookie no update parameter");
586 pair = apr_strtok(s, "&", &pstate);
591 name = apr_strtok(pair, "=", &vstate);
592 value = apr_strtok(NULL, "=", &vstate);
593 if (strncasecmp(name, "_chxj", 5) != 0) {
594 if (strlen(result) != 0)
595 result = apr_pstrcat(r->pool, result, "&", NULL);
597 if (strcasecmp(entryp->encoding, "NONE") != 0) {
602 if (value && *value != 0) {
603 value = chxj_url_decode(r->pool, value);
604 dlen = strlen(value);
605 DBG(r, "************ before encoding[%s]", value);
607 dvalue = chxj_rencoding(r, value, &dlen);
608 dvalue = chxj_url_encode(r->pool, dvalue);
610 DBG(r, "************ after encoding[%s]", dvalue);
616 if (name && *name != 0) {
617 name = chxj_url_decode(r->pool, name);
619 dname = chxj_rencoding(r, name, &dlen);
620 dname = chxj_url_encode(r->pool, dname);
626 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
629 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
633 if (strncasecmp(name, "_chxj_c_", 8) == 0
634 || strncasecmp(name, "_chxj_r_", 8) == 0
635 || strncasecmp(name, "_chxj_s_", 8) == 0) {
639 if (strlen(value) == 0)
642 if (strlen(result) != 0)
643 result = apr_pstrcat(r->pool, result, "&", NULL);
645 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
649 dlen = strlen(value);
650 value = chxj_url_decode(r->pool, value);
651 DBG(r, "************ before encoding[%s]", value);
653 dvalue = chxj_rencoding(r, value, &dlen);
654 dvalue = chxj_url_encode(r->pool,dvalue);
656 DBG(r, "************ after encoding[%s]", dvalue);
658 result = apr_pstrcat(r->pool, result, &name[8], "=", dvalue, NULL);
662 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
666 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0) {
667 DBG(r, "found cookie parameter[%s]", value);
668 DBG(r, "call start chxj_load_cookie()");
669 cookie = chxj_load_cookie(r, value);
670 DBG(r, "call end chxj_load_cookie()");
671 if (! no_update_flag && cookie) {
672 chxj_update_cookie(r, cookie);
676 *len = strlen(result);
678 DBG(r, "AFTER input convert result = [%s]", result);
685 * The received data is returned to the filter.
687 * @param f [i/o] It is a filter.
688 * @param data [i] It is data returned to the filter.
689 * @param len [i] It is length of the data returned to the filter.
692 pass_data_to_filter(ap_filter_t *f, const char *data,
695 request_rec *r = f->r;
696 conn_rec *c = r->connection;
698 apr_bucket_brigade *bb;
701 DBG(r, "start pass_data_to_filter()");
703 bb = apr_brigade_create(r->pool, c->bucket_alloc);
704 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
706 APR_BRIGADE_INSERT_TAIL(bb, b);
707 b = apr_bucket_eos_create(f->c->bucket_alloc);
708 APR_BRIGADE_INSERT_TAIL(bb, b);
710 rv = ap_pass_brigade(f->next, bb);
711 if (rv != APR_SUCCESS) {
712 DBG(r, "ap_pass_brigade()");
716 DBG(r, "end pass_data_to_filter()");
723 * It is the main loop of the output filter.
725 * @param f [i/o] It is a filter.
729 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
735 char *user_agent = NULL;
737 mod_chxj_ctx *ctx = (mod_chxj_ctx *)f->ctx;
738 cookie_t *cookie = NULL;
739 mod_chxj_config *dconf;
740 chxjconvrule_entry *entryp = NULL;
741 device_table *spec = NULL;
744 DBG(f->r, "start of chxj_output_filter()");
748 apr_pool_create(&pool, r->pool);
750 entryp = ctx->entryp;
752 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
754 if (r->content_type) {
755 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
756 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
757 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
758 && ! (dconf->image == CHXJ_IMG_ON
759 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
760 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
761 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
762 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
763 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
764 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
765 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
766 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
767 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
769 DBG(r, "not convert content-type:[%s]", r->content_type);
770 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
771 DBG(r, "entryp->action == COOKIE_ON_BIT");
772 switch(spec->html_spec_type) {
773 case CHXJ_SPEC_Chtml_1_0:
774 case CHXJ_SPEC_Chtml_2_0:
775 case CHXJ_SPEC_Chtml_3_0:
776 case CHXJ_SPEC_Chtml_4_0:
777 case CHXJ_SPEC_Chtml_5_0:
778 case CHXJ_SPEC_Chtml_6_0:
779 case CHXJ_SPEC_Chtml_7_0:
780 case CHXJ_SPEC_XHtml_Mobile_1_0:
781 case CHXJ_SPEC_Jhtml:
783 cookie = chxj_save_cookie(r);
784 s_add_cookie_id_if_has_location_header(r, cookie);
785 chxj_cookie_unlock(r);
791 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
792 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
793 r->status = HTTP_MOVED_TEMPORARILY;
796 ap_pass_brigade(f->next, bb);
801 DBG(r, "not convert content-type:[(null)]");
802 ap_pass_brigade(f->next, bb);
807 for (b = APR_BRIGADE_FIRST(bb);
808 b != APR_BRIGADE_SENTINEL(bb);
809 b = APR_BUCKET_NEXT(b)) {
811 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
812 DBG(r, "read data[%.*s]",(int)len, data);
814 /*--------------------------------------------------------------------*/
816 /*--------------------------------------------------------------------*/
818 DBG(r, "append data start");
819 ctx = (mod_chxj_ctx *)f->ctx;
822 tmp = apr_palloc(r->pool, ctx->len);
823 memcpy(tmp, ctx->buffer, ctx->len);
825 ctx->buffer = apr_palloc(pool, ctx->len + len);
827 memcpy(ctx->buffer, tmp, ctx->len);
828 memcpy(&ctx->buffer[ctx->len], data, len);
832 DBG(r, "append data end");
835 if (APR_BUCKET_IS_EOS(b)) {
838 /*----------------------------------------------------------------------*/
840 /*----------------------------------------------------------------------*/
842 ctx = (mod_chxj_ctx *)f->ctx;
844 DBG(r, "content_type=[%s]", r->content_type);
847 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
849 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
850 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
851 DBG(r, "detect convert target:[%s]", r->content_type);
856 tmp = apr_palloc(pool, ctx->len + 1);
858 memset(tmp, 0, ctx->len + 1);
859 memcpy(tmp, ctx->buffer, ctx->len);
861 ctx->buffer = chxj_convert(r,
863 (apr_size_t *)&ctx->len,
865 user_agent, &cookie);
869 ctx->buffer = apr_psprintf(r->pool, "\n");
871 ctx->buffer = chxj_convert(r,
872 (const char **)&ctx->buffer,
873 (apr_size_t *)&ctx->len,
875 user_agent, &cookie);
880 && *(char *)r->content_type == 't'
881 && strncmp(r->content_type, "text/xml", 8) == 0) {
890 memset(&doc, 0, sizeof(Doc));
891 memset(&qrcode, 0, sizeof(qr_code_t));
893 doc.parse_mode = PARSE_MODE_CHTML;
897 qs_init_malloc(&doc);
899 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
902 for (child = qs_get_child_node(&doc,root);
904 child = qs_get_next_node(&doc,child)) {
905 char *name = qs_get_node_name(&doc,child);
906 if (strcasecmp("qrcode",name) == 0) {
911 qs_all_free(&doc,QX_LOGMARK);
913 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
914 chxj_qrcode_node_to_qrcode(&qrcode, root);
915 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
917 ERR(r, "qrcode create failed.");
918 chxj_cookie_unlock(r);
921 r->content_type = apr_psprintf(r->pool, "image/jpeg");
925 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
927 && ( *r->content_type == 'i' || *r->content_type == 'I')
928 && dconf->image == CHXJ_IMG_ON
929 && strncasecmp("image/", r->content_type, 6) == 0
930 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
931 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
932 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
933 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
934 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
935 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
936 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
937 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
941 tmp = apr_palloc(pool, ctx->len + 1);
943 memset(tmp, 0, ctx->len + 1);
944 memcpy(tmp, ctx->buffer, ctx->len);
946 chxj_convert_image(r,
948 (apr_size_t *)&ctx->len);
949 if (ctx->buffer == NULL) {
955 apr_table_unset(r->headers_out, "Content-Length");
956 apr_table_unset(r->err_headers_out, "Content-Length");
957 ap_set_content_length(r, (apr_off_t)ctx->len);
959 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
960 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
961 r->status = HTTP_MOVED_TEMPORARILY;
966 DBG(r, "call pass_data_to_filter()");
967 s_add_cookie_id_if_has_location_header(r, cookie);
968 chxj_cookie_unlock(r);
969 rv = pass_data_to_filter(f,
970 (const char *)ctx->buffer,
971 (apr_size_t)ctx->len);
974 chxj_cookie_unlock(r);
979 DBG(r, " SAVE COOKIE[%x]", entryp->action);
984 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
985 DBG(r, "entryp->action == COOKIE_ON_BIT");
986 switch(spec->html_spec_type) {
987 case CHXJ_SPEC_Chtml_1_0:
988 case CHXJ_SPEC_Chtml_2_0:
989 case CHXJ_SPEC_Chtml_3_0:
990 case CHXJ_SPEC_Chtml_4_0:
991 case CHXJ_SPEC_Chtml_5_0:
992 case CHXJ_SPEC_Chtml_6_0:
993 case CHXJ_SPEC_Chtml_7_0:
994 case CHXJ_SPEC_XHtml_Mobile_1_0:
995 case CHXJ_SPEC_Jhtml:
997 cookie = chxj_save_cookie(r);
999 * Location Header Check to add cookie parameter.
1001 s_add_cookie_id_if_has_location_header(r, cookie);
1002 chxj_cookie_unlock(r);
1009 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
1010 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1011 r->status = HTTP_MOVED_TEMPORARILY;
1014 apr_table_setn(r->headers_out, "Content-Length", "0");
1015 DBG(r, "call pass_data_to_filter()");
1016 rv = pass_data_to_filter(f, (const char *)"", (apr_size_t)0);
1021 apr_brigade_destroy(bb);
1023 DBG(r, "end of output filter");
1029 * Add Cookie_id if it has location header.
1032 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1034 char *location_header = (char *)apr_table_get(r->headers_out, "Location");
1035 if (! location_header) {
1036 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1038 if (cookie && location_header) {
1039 DBG(r, "Location Header=[%s]", location_header);
1040 location_header = chxj_add_cookie_parameter(r,
1043 apr_table_unset(r->headers_out, "Location");
1044 apr_table_setn(r->headers_out, "Location", location_header);
1045 DBG(r, "Location Header=[%s]", location_header);
1046 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1047 r->status = HTTP_MOVED_TEMPORARILY;
1053 * It is the main loop of the input filter handler.
1057 chxj_input_handler(request_rec *r)
1059 mod_chxj_config *dconf;
1060 chxjconvrule_entry *entryp = NULL;
1061 device_table *spec = NULL;
1062 char *post_data = NULL;
1063 apr_size_t post_data_len = 0;
1068 DBG(r, "start of chxj_input_handler()");
1070 if (strcasecmp(r->handler, "chxj-input-handler")) {
1071 DBG(r, "end chxj_input_handler()");
1074 apr_pool_create(&pool, r->pool);
1076 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1077 user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
1078 spec = chxj_specified_device(r, user_agent);
1079 entryp = chxj_apply_convrule(r, dconf->convrules);
1081 post_data = apr_pstrdup(pool, "");
1082 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1083 if (ap_should_client_block(r)) {
1084 while (post_data_len < CHXJ_POST_MAX) {
1087 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1088 if (read_bytes<=0) {
1091 buffer[read_bytes] = '\0';
1092 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1093 post_data_len += read_bytes;
1102 if (post_data_len > 0) {
1103 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp);
1104 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1108 if (dconf->forward_url_base) {
1109 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1112 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);
1115 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1117 DBG(r, "==> new url_path:[%s]", url_path);
1120 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1121 apr_table_unset(r->headers_in, "Content-Length");
1122 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%d", post_data_len));
1123 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len);
1124 DBG(r, "response:[%.*s][%d]", res_len, response, res_len);
1127 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1128 if (strcasecmp(chunked, "chunked") == 0) {
1130 apr_table_unset(r->headers_out, "Transfer-Encoding");
1135 apr_pool_create(&wpool, r->pool);
1136 apr_bucket_brigade *bb;
1139 conn_rec *c = r->connection;
1141 bb = apr_brigade_create(wpool, c->bucket_alloc);
1142 e = apr_bucket_heap_create(response, res_len, NULL, c->bucket_alloc);
1143 APR_BRIGADE_INSERT_TAIL(bb, e);
1144 e = apr_bucket_eos_create(c->bucket_alloc);
1145 APR_BRIGADE_INSERT_TAIL(bb, e);
1146 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1147 ERR(r, "%s:%d failed ap_pass_brigade()", APLOG_MARK);
1150 apr_brigade_cleanup(bb);
1153 DBG(r, "end of chxj_input_handler()");
1157 static mod_chxj_global_config *
1158 chxj_global_config_create(apr_pool_t *pool, server_rec *s)
1160 mod_chxj_global_config *conf;
1162 SDBG(s, "start chxj_global_config_create()");
1164 /*--------------------------------------------------------------------------*/
1165 /* allocate an own subpool which survives server restarts */
1166 /*--------------------------------------------------------------------------*/
1167 conf = (mod_chxj_global_config *)apr_palloc(pool,
1168 sizeof(mod_chxj_global_config));
1170 conf->cookie_db_lock = NULL;
1172 SDBG(s, "end chxj_global_config_create()");
1179 * initialize chxj module
1182 chxj_init_module(apr_pool_t *p,
1183 apr_pool_t *UNUSED(plog),
1184 apr_pool_t *UNUSED(ptemp),
1190 SDBG(s, "start chxj_init_module()");
1192 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1194 if (user_data == NULL) {
1197 * dummy user_data set.
1199 apr_pool_userdata_set(
1201 CHXJ_MOD_CONFIG_KEY,
1202 apr_pool_cleanup_null,
1204 SDBG(s, "end chxj_init_module()");
1208 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1210 if ((rv = apr_proc_mutex_create(&global_cookie_mutex, NULL, APR_LOCK_FCNTL, s->process->pool)) != APR_SUCCESS) {
1212 SERR(s, "%s:%d end chxj_init_module(). mutex create failure.(%d:%s)",APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1213 return HTTP_INTERNAL_SERVER_ERROR;
1216 SDBG(s, "end chxj_init_module()");
1223 chxj_child_init(apr_pool_t *UNUSED(p), server_rec *s)
1226 SDBG(s, "start chxj_child_init()");
1227 if ((rv = apr_proc_mutex_child_init(&global_cookie_mutex, NULL, s->process->pool)) != APR_SUCCESS) {
1229 SERR(s, "%s:%d ERROR end chxj_init_module(). mutex create failure.(%d:%s)", APLOG_MARK, rv,apr_strerror(rv,errstr,255));
1231 SDBG(s, "end chxj_child_init()");
1236 * A set structure of each server is generated.
1242 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1244 mod_chxj_global_config *gc;
1246 gc = chxj_global_config_create(p,s);
1253 chxj_translate_name(request_rec *r)
1255 return chxj_trans_name(r);
1260 chxj_insert_filter(request_rec *r)
1264 mod_chxj_config *dconf;
1265 chxjconvrule_entry *entryp;
1270 DBG(r, "start chxj_insert_filter()");
1272 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1274 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1276 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1278 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1279 DBG(r, "detect multipart/form-data ==> no target");
1283 spec = chxj_specified_device(r, user_agent);
1284 entryp = chxj_apply_convrule(r, dconf->convrules);
1286 DBG(r, "end chxj_insert_filter()");
1289 ctx = apr_palloc(r->pool, sizeof(*ctx));
1290 memset(ctx, 0, sizeof(*ctx));
1291 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1292 ERR(r, "failed: new pool create. rv:[%d]", rv);
1295 ctx->entryp = entryp;
1297 ctx->buffer = apr_palloc(ctx->pool, 1);
1300 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1305 switch(spec->html_spec_type) {
1306 case CHXJ_SPEC_Chtml_1_0:
1307 case CHXJ_SPEC_Chtml_2_0:
1308 case CHXJ_SPEC_Chtml_3_0:
1309 case CHXJ_SPEC_Chtml_4_0:
1310 case CHXJ_SPEC_Chtml_5_0:
1311 case CHXJ_SPEC_Chtml_6_0:
1312 case CHXJ_SPEC_Chtml_7_0:
1313 case CHXJ_SPEC_XHtml_Mobile_1_0:
1314 case CHXJ_SPEC_Hdml:
1315 case CHXJ_SPEC_Jhtml:
1316 case CHXJ_SPEC_Jxhtml:
1324 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1325 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1326 DBG(r, "added Output Filter");
1329 DBG(r, "end chxj_insert_filter()");
1334 * The hook is registered.
1339 chxj_register_hooks(apr_pool_t *UNUSED(p))
1341 ap_hook_post_config(chxj_init_module,
1344 APR_HOOK_REALLY_FIRST);
1345 ap_hook_child_init(chxj_child_init,
1348 APR_HOOK_REALLY_FIRST);
1349 ap_register_output_filter (
1350 "chxj_output_filter",
1354 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1355 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1356 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1357 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1358 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1359 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_FIRST);
1364 * A set structure according to the directory is generated.
1370 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1372 mod_chxj_config *conf;
1374 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1375 conf->device_data_file = NULL;
1376 conf->devices = NULL;
1377 conf->emoji_data_file = NULL;
1379 conf->emoji_tail = NULL;
1380 conf->image = CHXJ_IMG_NONE;
1381 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1382 conf->image_cache_limit = 0;
1383 conf->server_side_encoding = NULL;
1384 conf->cookie_db_dir = NULL;
1385 conf->cookie_timeout = 0;
1386 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1387 conf->cookie_lazy_mode = 0;
1388 #if defined(USE_MYSQL_COOKIE)
1389 memset((void *)&conf->mysql, 0, sizeof(mysql_t));
1390 conf->mysql.port = MYSQL_PORT;
1391 conf->mysql.host = NULL;
1393 #if defined(USE_MEMCACHE_COOKIE)
1394 memset((void *)&conf->memcache, 0, sizeof(memcache_t));
1395 conf->memcache.host = NULL;
1396 conf->memcache.port = 0;
1398 conf->forward_url_base = NULL;
1399 conf->forward_server_ip = NULL;
1405 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1406 memset(conf->dir, 0, strlen(arg)+1);
1407 strcpy(conf->dir, arg);
1409 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1411 /* Default is copyleft */
1412 conf->image_copyright = NULL;
1419 * Merge per-directory CHXJ configurations
1422 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1424 mod_chxj_config *base;
1425 mod_chxj_config *add;
1426 mod_chxj_config *mrg;
1428 base = (mod_chxj_config *)basev;
1429 add = (mod_chxj_config *)addv;
1430 mrg = (mod_chxj_config *)apr_palloc(p, sizeof(mod_chxj_config));
1432 mrg->device_data_file = NULL;
1433 mrg->devices = NULL;
1434 mrg->emoji_data_file = NULL;
1435 mrg->image = CHXJ_IMG_NONE;
1436 mrg->image_cache_dir = NULL;
1437 mrg->image_copyright = NULL;
1438 mrg->image_cache_limit = 0;
1440 mrg->emoji_tail = NULL;
1441 mrg->new_line_type = NLTYPE_NIL;
1442 mrg->forward_url_base = NULL;
1443 mrg->forward_server_ip = NULL;
1445 mrg->dir = apr_pstrdup(p, add->dir);
1447 if (! add->device_data_file) {
1448 mrg->devices = base->devices;
1449 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1452 mrg->devices = add->devices;
1453 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1456 if (! add->emoji_data_file) {
1457 mrg->emoji = base->emoji;
1458 mrg->emoji_tail = base->emoji_tail;
1459 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1462 mrg->emoji = add->emoji;
1463 mrg->emoji_tail = add->emoji_tail;
1464 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1467 if (add->image == CHXJ_IMG_NONE) {
1468 mrg->image = base->image;
1471 mrg->image = add->image;
1474 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1475 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1478 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1481 if (add->image_cache_limit) {
1482 mrg->image_cache_limit = add->image_cache_limit;
1485 mrg->image_cache_limit = base->image_cache_limit;
1488 if (add->image_copyright)
1489 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1491 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1493 if (add->server_side_encoding) {
1494 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1497 if (base->server_side_encoding) {
1498 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1501 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1504 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1506 if (add->cookie_db_dir) {
1507 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1510 if (base->cookie_db_dir) {
1511 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1514 mrg->cookie_db_dir = NULL;
1517 if (add->cookie_timeout) {
1518 mrg->cookie_timeout = add->cookie_timeout;
1521 if (base->cookie_db_dir) {
1522 mrg->cookie_timeout = base->cookie_timeout;
1525 mrg->cookie_timeout = 0;
1528 #if defined(USE_MYSQL_COOKIE)
1529 if (add->mysql.host) {
1530 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1532 else if (base->mysql.host) {
1533 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1536 mrg->mysql.host = NULL;
1538 if (add->mysql.port) {
1539 mrg->mysql.port = add->mysql.port;
1541 else if (base->mysql.port) {
1542 mrg->mysql.port = base->mysql.port;
1545 mrg->mysql.port = 0;
1548 if (add->mysql.database) {
1549 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1551 else if (base->mysql.database) {
1552 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1555 mrg->mysql.database = NULL;
1558 if (add->mysql.username) {
1559 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1561 else if (base->mysql.username) {
1562 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1565 mrg->mysql.username = NULL;
1568 if (add->mysql.password) {
1569 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1571 else if (base->mysql.password) {
1572 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1575 mrg->mysql.password = NULL;
1578 if (add->mysql.tablename) {
1579 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1581 else if (base->mysql.tablename) {
1582 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1585 mrg->mysql.tablename = NULL;
1588 if (add->mysql.socket_path) {
1589 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1591 else if (base->mysql.socket_path) {
1592 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1595 mrg->mysql.socket_path = NULL;
1598 if (add->mysql.charset) {
1599 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1601 else if (base->mysql.charset) {
1602 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1605 mrg->mysql.charset = NULL;
1608 #if defined(USE_MEMCACHE_COOKIE)
1609 if (add->memcache.host) {
1610 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1612 else if (base->memcache.host) {
1613 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1616 mrg->memcache.host = NULL;
1618 if (add->memcache.port) {
1619 mrg->memcache.port = add->memcache.port;
1621 else if (base->memcache.port) {
1622 mrg->memcache.port = base->memcache.port;
1625 mrg->memcache.port = 0;
1628 if (add->cookie_store_type) {
1629 mrg->cookie_store_type = add->cookie_store_type;
1631 else if (base->cookie_store_type) {
1632 mrg->cookie_store_type = base->cookie_store_type;
1635 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1637 if (add->cookie_lazy_mode) {
1638 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1640 else if (base->cookie_lazy_mode) {
1641 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1644 mrg->cookie_lazy_mode = 0;
1646 if (add->new_line_type) {
1647 mrg->new_line_type = add->new_line_type;
1649 else if (base->new_line_type) {
1650 mrg->new_line_type = base->new_line_type;
1653 mrg->new_line_type = NLTYPE_NIL;
1660 chxj_command_parse_take5(
1673 for (;*strp == ' '||*strp == '\t'; strp++) ;
1683 for (; *strp != '\0'; strp++) {
1684 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1685 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1690 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1691 || (isquoted && *strp == '"'))
1705 for (;*strp == ' '||*strp == '\t'; strp++) ;
1714 for (; *strp != '\0'; strp++) {
1715 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1716 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1721 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1722 || (isquoted && *strp == '"'))
1735 for (;*strp == ' '||*strp == '\t'; strp++);
1743 for (; *strp != '\0'; strp++) {
1744 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1745 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1750 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1751 || (isquoted && *strp == '"'))
1763 for (;*strp == ' '||*strp == '\t'; strp++);
1771 for (; *strp != '\0'; strp++) {
1772 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1773 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1778 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1779 || (isquoted && *strp == '"'))
1790 for (;*strp == ' '||*strp == '\t'; strp++);
1798 for (; *strp != '\0'; strp++) {
1799 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1800 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1805 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1806 || (isquoted && *strp == '"'))
1816 * The device definition file is loaded.
1818 * @param arg [i] The name of the device definition file is specified.
1819 * @param mconfig [i/o] The pointer to a set structure is specified.
1823 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char *arg)
1825 mod_chxj_config *conf;
1830 if (strlen(arg) > 256)
1831 return "mod_chxj: device data filename too long.";
1833 conf = (mod_chxj_config *)mconfig;
1834 conf->device_data_file = apr_pstrdup(parms->pool, arg);
1836 qs_init_malloc(&doc);
1837 qs_init_root_node(&doc);
1839 qs_parse_file((Doc *)&doc, (const char *)arg);
1840 chxj_load_device_data(&doc,parms->pool, conf);
1841 qs_all_free(&doc, QX_LOGMARK);
1848 * Device definition information is loaded.
1851 * @param arg [i] The name of the device definition file is specified.
1852 * @param mconfig [i/o] The pointer to a set structure is specified.
1856 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char *arg)
1858 mod_chxj_config *conf;
1865 if (strlen(arg) > 256)
1866 return "mod_chxj: emoji data filename too long.";
1868 conf = (mod_chxj_config *)mconfig;
1869 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
1870 qs_init_malloc(&doc);
1871 qs_init_root_node(&doc);
1873 qs_parse_file((Doc *)&doc, (const char *)arg);
1875 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
1877 qs_all_free(&doc, QX_LOGMARK);
1885 cmd_set_image_engine(cmd_parms * UNUSED(parms), void *mconfig, const char *arg)
1887 mod_chxj_config *conf;
1892 if (strlen(arg) > 256)
1893 return "image uri is too long.";
1895 conf = (mod_chxj_config*)mconfig;
1896 if (strcasecmp("ON", arg) == 0) {
1897 conf->image = CHXJ_IMG_ON;
1900 conf->image = CHXJ_IMG_OFF;
1908 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char *arg)
1910 mod_chxj_config *conf;
1915 if (strlen(arg) > 256)
1916 return "cache dir name is too long.";
1918 conf = (mod_chxj_config *)mconfig;
1919 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
1926 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char *arg)
1928 mod_chxj_config *conf;
1933 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
1934 return "cache size is too long.";
1936 conf = (mod_chxj_config *)mconfig;
1939 * I use strtol function because strtoul is not portable function.
1941 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
1944 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
1946 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
1955 cmd_set_image_copyright(cmd_parms *parms, void *mconfig, const char *arg)
1957 mod_chxj_config *conf;
1962 if (strlen(arg) > 256)
1963 return "Copyright Flag is too long.";
1965 conf = (mod_chxj_config *)mconfig;
1966 conf->image_copyright = apr_pstrdup(parms->pool, arg);
1973 cmd_convert_rule(cmd_parms *cmd, void *mconfig, const char *arg)
1977 mod_chxj_config *dconf;
1978 chxjconvrule_entry *newrule;
1988 dconf = (mod_chxj_config *)mconfig;
1990 if (strlen(arg) > 4096)
1991 return "mod_chxj: ChxjConvertRule: is too long.";
1993 dconf = (mod_chxj_config *)mconfig;
1994 if (dconf->convrules == NULL)
1995 dconf->convrules = apr_array_make(cmd->pool,
1997 sizeof(chxjconvrule_entry));
1999 newrule = apr_array_push(dconf->convrules);
2002 newrule->action = 0;
2004 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
2005 return "ChxjConvertRule: bad argument line";
2007 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
2011 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
2017 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
2018 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2021 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2022 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2028 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2029 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2039 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2043 mode = AP_REG_EXTENDED;
2044 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2045 return "RewriteRule: cannot compile regular expression ";
2047 newrule->regexp = regexp;
2049 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2051 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2053 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2055 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2056 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2058 newrule->user_agent = NULL;
2060 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2072 mod_chxj_config *dconf;
2075 if (strlen(arg) > 4096)
2076 return "mod_chxj: ChxjCookieDir is too long.";
2078 dconf = (mod_chxj_config *)mconfig;
2080 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2087 cmd_set_cookie_timeout(
2088 cmd_parms *UNUSED(cmd),
2092 mod_chxj_config *dconf;
2094 if (strlen(arg) > 4096)
2095 return "mod_chxj: ChxjCookieTimeout is too long.";
2097 if (chxj_chk_numeric(arg) != 0)
2098 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2100 dconf = (mod_chxj_config *)mconfig;
2102 dconf->cookie_timeout = atoi(arg);
2108 #if defined(USE_MYSQL_COOKIE)
2110 cmd_set_cookie_mysql_database(
2115 mod_chxj_config *dconf;
2117 if (strlen(arg) > 255)
2118 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2120 dconf = (mod_chxj_config *)mconfig;
2122 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2129 cmd_set_cookie_mysql_username(
2134 mod_chxj_config *dconf;
2136 if (strlen(arg) > 255)
2137 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2139 dconf = (mod_chxj_config *)mconfig;
2141 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2148 cmd_set_cookie_mysql_password(
2153 mod_chxj_config *dconf;
2155 if (strlen(arg) > 255)
2156 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2158 dconf = (mod_chxj_config *)mconfig;
2160 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2167 cmd_set_cookie_mysql_table_name(
2172 mod_chxj_config *dconf;
2174 if (strlen(arg) > 255)
2175 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2177 dconf = (mod_chxj_config *)mconfig;
2179 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2185 cmd_set_cookie_mysql_port(
2186 cmd_parms *UNUSED(cmd),
2190 mod_chxj_config *dconf;
2192 if (strlen(arg) > 255)
2193 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2195 dconf = (mod_chxj_config *)mconfig;
2197 if (chxj_chk_numeric(arg) != 0)
2198 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2200 dconf = (mod_chxj_config *)mconfig;
2202 dconf->mysql.port = chxj_atoi(arg);
2209 cmd_set_cookie_mysql_host(
2214 mod_chxj_config *dconf;
2216 if (strlen(arg) > 255)
2217 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2219 dconf = (mod_chxj_config *)mconfig;
2221 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2228 cmd_set_cookie_mysql_socket_path(
2233 mod_chxj_config *dconf;
2235 if (strlen(arg) > 4096)
2236 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2238 dconf = (mod_chxj_config *)mconfig;
2240 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2247 cmd_set_cookie_mysql_charset(
2252 mod_chxj_config *dconf;
2254 if (strlen(arg) > 255)
2255 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2257 dconf = (mod_chxj_config *)mconfig;
2259 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2264 #if defined(USE_MEMCACHE_COOKIE)
2266 cmd_set_cookie_memcache_port(
2267 cmd_parms *UNUSED(cmd),
2271 mod_chxj_config *dconf;
2273 if (strlen(arg) > 255)
2274 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2276 dconf = (mod_chxj_config *)mconfig;
2278 if (chxj_chk_numeric(arg) != 0)
2279 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2281 dconf = (mod_chxj_config *)mconfig;
2283 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2290 cmd_set_cookie_memcache_host(
2295 mod_chxj_config *dconf;
2297 if (strlen(arg) > 255)
2298 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2300 dconf = (mod_chxj_config *)mconfig;
2302 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2309 cmd_set_cookie_lazy_mode(
2310 cmd_parms *UNUSED(cmd),
2314 mod_chxj_config *dconf;
2316 if (strlen(arg) > 255)
2317 return "mod_chxj: ChxjCookieLazyMode is too long.";
2319 dconf = (mod_chxj_config *)mconfig;
2321 if (strcasecmp("TRUE",arg) == 0) {
2322 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2325 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2332 cmd_set_cookie_store_type(
2333 cmd_parms *UNUSED(cmd),
2337 mod_chxj_config *dconf;
2339 if (strlen(arg) > 255)
2340 return "mod_chxj: ChxjCookieStoreType is too long.";
2342 dconf = (mod_chxj_config *)mconfig;
2344 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2345 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2347 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2348 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2350 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2351 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2354 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2361 cmd_set_forward_url_base(
2366 mod_chxj_config *dconf;
2368 if (strlen(arg) > 255)
2369 return "mod_chxj: ChxjForwardUrlBase is too long.";
2371 dconf = (mod_chxj_config *)mconfig;
2373 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2379 cmd_set_forward_server_ip(
2384 mod_chxj_config *dconf;
2386 if (strlen(arg) > 255)
2387 return "mod_chxj: ChxjForwardServerIp is too long.";
2389 dconf = (mod_chxj_config *)mconfig;
2391 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2397 cmd_set_new_line_type(
2398 cmd_parms *UNUSED(cmd),
2402 mod_chxj_config *dconf;
2403 if (strlen(arg) > 255)
2404 return "mod_chxj: ChxjNewLineType is too long.";
2406 dconf = (mod_chxj_config *)mconfig;
2408 if (strcasecmp(CHXJ_NEW_LINE_TYPE_CRLF, arg) == 0) {
2409 dconf->new_line_type = NLTYPE_CRLF;
2411 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_LF, arg) == 0) {
2412 dconf->new_line_type = NLTYPE_LF;
2414 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_CR, arg) == 0) {
2415 dconf->new_line_type = NLTYPE_CR;
2417 else if (strcasecmp(CHXJ_NEW_LINE_TYPE_NONE, arg) == 0) {
2418 dconf->new_line_type = NLTYPE_NONE;
2421 return "mod_chxj: invalid value (ChxjNewLineType)";
2427 static const command_rec cmds[] = {
2429 "ChxjLoadDeviceData",
2430 cmd_load_device_data,
2433 "Load Device Data"),
2435 "ChxjLoadEmojiData",
2436 cmd_load_emoji_data,
2442 cmd_set_image_engine,
2445 "Convert Target URI"),
2447 "ChxjImageCacheDir",
2448 cmd_set_image_cache_dir,
2451 "Image Cache Directory"),
2453 "ChxjImageCacheLimit",
2454 cmd_set_image_cache_limit,
2457 "Image Cache Limit"),
2459 "ChxjImageCopyright",
2460 cmd_set_image_copyright,
2469 "an URL-applied regexp-pattern and a substitution URL"),
2475 "save cookie.db directory."),
2477 "ChxjCookieTimeout",
2478 cmd_set_cookie_timeout,
2481 "The compulsion time-out time of the cookie is specified. "),
2483 "ChxjCookieStoreType",
2484 cmd_set_cookie_store_type,
2487 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2489 "ChxjCookieLazyMode",
2490 cmd_set_cookie_lazy_mode,
2493 "OneTimeID is negligently done. (TRUE/FALSE)"),
2494 #if defined(USE_MYSQL_COOKIE)
2496 "ChxjCookieMysqlHost",
2497 cmd_set_cookie_mysql_host,
2500 "The MySQL database host used by saving Cookie"),
2502 "ChxjCookieMysqlPort",
2503 cmd_set_cookie_mysql_port,
2506 "The MySQL database port used by saving Cookie"),
2508 "ChxjCookieMysqlDatabase",
2509 cmd_set_cookie_mysql_database,
2512 "The MySQL database name used by saving Cookie"),
2514 "ChxjCookieMysqlUsername",
2515 cmd_set_cookie_mysql_username,
2518 "The MySQL username used by saving Cookie"),
2520 "ChxjCookieMysqlPassword",
2521 cmd_set_cookie_mysql_password,
2524 "The MySQL password used by saving Cookie"),
2526 "ChxjCookieMysqlTableName",
2527 cmd_set_cookie_mysql_table_name,
2530 "The MySQL table name used by saving Cookie"),
2532 "ChxjCookieMysqlSocketPath",
2533 cmd_set_cookie_mysql_socket_path,
2536 "The MySQL socket path used by saving Cookie"),
2538 "ChxjCookieMysqlCharset",
2539 cmd_set_cookie_mysql_charset,
2542 "The MySQL charset used by saving Cookie"),
2544 #if defined(USE_MEMCACHE_COOKIE)
2546 "ChxjCookieMemcacheHost",
2547 cmd_set_cookie_memcache_host,
2550 "The Memcached host used by saving Cookie"),
2552 "ChxjCookieMemcachePort",
2553 cmd_set_cookie_memcache_port,
2556 "The Memcached port used by saving Cookie"),
2560 cmd_set_new_line_type,
2563 "HTML new line type (NONE|CRLF|LF|CR). default is CRLF"),
2565 "ChxjForwardUrlBase",
2566 cmd_set_forward_url_base,
2569 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2571 "ChxjForwardServerIp",
2572 cmd_set_forward_server_ip,
2575 "The forward server ip(default: this server ip)"),
2580 /*----------------------------------------------------------------------------*/
2581 /* Dispatch list for API hooks */
2582 /*----------------------------------------------------------------------------*/
2583 module AP_MODULE_DECLARE_DATA chxj_module = {
2584 STANDARD20_MODULE_STUFF,
2585 chxj_create_per_dir_config, /* create per-dir config structures */
2586 chxj_merge_per_dir_config, /* merge per-dir config structures */
2587 chxj_config_server_create, /* create per-server config structures */
2588 NULL, /* merge per-server config structures */
2589 cmds, /* table of config file commands */
2590 chxj_register_hooks /* register hooks */