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_jhtml.h"
57 #include "chxj_img_conv_format.h"
58 #include "chxj_qr_code.h"
59 #include "chxj_encoding.h"
60 #include "chxj_apply_convrule.h"
61 #include "chxj_cookie.h"
62 #include "chxj_url_encode.h"
63 #include "chxj_str_util.h"
64 #if defined(USE_MYSQL_COOKIE)
65 # include "chxj_mysql.h"
67 #include "chxj_serf.h"
70 #define CHXJ_VERSION_PREFIX PACKAGE_NAME "/"
71 #define CHXJ_VERSION PACKAGE_VERSION
72 #define CHXJ_POST_MAX (0x1000000)
74 converter_t convert_routine[] = {
76 /* CHXJ_SPEC_UNKNOWN */
81 /* CHXJ_SPEC_Chtml_1_0 */
82 .converter = chxj_exchange_chtml10,
83 .encoder = chxj_encoding,
86 /* CHXJ_SPEC_Chtml_2_0 */
87 .converter = chxj_exchange_chtml20,
88 .encoder = chxj_encoding,
91 /* CHXJ_SPEC_Chtml_3_0 */
92 .converter = chxj_exchange_chtml30,
93 .encoder = chxj_encoding,
96 /* CHXJ_SPEC_Chtml_4_0 */
97 .converter = chxj_exchange_chtml30,
98 .encoder = chxj_encoding,
101 /* CHXJ_SPEC_Chtml_5_0 */
102 .converter = chxj_exchange_chtml30,
103 .encoder = chxj_encoding,
106 /* CHXJ_SPEC_XHtml_Mobile_1_0 */
107 .converter = chxj_exchange_xhtml_mobile_1_0,
108 .encoder = chxj_encoding,
112 .converter = chxj_exchange_hdml,
113 .encoder = chxj_encoding,
116 /* CHXJ_SPEC_Jhtml */
117 .converter = chxj_exchange_jhtml,
118 .encoder = chxj_encoding,
127 static int chxj_convert_input_header(request_rec *r,chxjconvrule_entry* entryp);
128 static void s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie);
131 * Only when User-Agent is specified, the User-Agent header is camouflaged.
136 chxj_headers_fixup(request_rec *r)
138 mod_chxj_config* dconf;
139 chxjconvrule_entry* entryp;
145 DBG(r, "start chxj_headers_fixup()");
146 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
148 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
149 spec = chxj_specified_device(r, user_agent);
151 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
153 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
154 DBG(r, "detect multipart/form-data ==> no target");
155 DBG(r, "end chxj_headers_fixup()");
159 switch(spec->html_spec_type) {
160 case CHXJ_SPEC_Chtml_1_0:
161 case CHXJ_SPEC_Chtml_2_0:
162 case CHXJ_SPEC_Chtml_3_0:
163 case CHXJ_SPEC_Chtml_4_0:
164 case CHXJ_SPEC_Chtml_5_0:
165 case CHXJ_SPEC_XHtml_Mobile_1_0:
167 case CHXJ_SPEC_Jhtml:
168 entryp = chxj_apply_convrule(r, dconf->convrules);
170 DBG(r, "end chxj_headers_fixup() no pattern");
173 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
178 apr_table_setn(r->headers_in,
179 CHXJ_HTTP_USER_AGENT,
182 if (entryp->user_agent)
183 apr_table_setn(r->headers_in,
187 chxj_convert_input_header(r,entryp);
197 if (r->method_number == M_POST) {
198 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
199 DBG(r, "set Input handler old:[%s] proxyreq:[%d] uri:[%s] filename:[%s]", r->handler, r->proxyreq, r->uri, r->filename);
200 r->proxyreq = PROXYREQ_NONE;
201 r->handler = apr_psprintf(r->pool, "chxj-input-handler");
204 char *client_ip = (char *)apr_table_get(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP);
206 apr_sockaddr_t *address = NULL;
207 apr_status_t rv = apr_sockaddr_info_get(&address, ap_get_server_name(r), APR_UNSPEC, ap_get_server_port(r), 0, r->pool);
208 if (rv != APR_SUCCESS) {
210 ERR(r, "%s:%d apr_sockaddr_info_get() failed: rv:[%d|%s]", APLOG_MARK, rv, apr_strerror(rv, buf, 256));
211 DBG(r, "end chxj_headers_fixup()");
215 if (dconf->forward_server_ip) {
216 addr = dconf->forward_server_ip;
219 apr_sockaddr_ip_get(&addr, address);
221 DBG(r, "Client IP:[%s] vs Orig Client IP:[%s] vs Server IP:[%s]", r->connection->remote_ip, client_ip, addr);
222 if (strcmp(addr, r->connection->remote_ip) == 0) {
223 r->connection->remote_ip = apr_pstrdup(r->connection->pool, client_ip);
225 if (! apr_table_get(r->headers_in, "Content-Length")) {
226 contentLength = (char *)apr_table_get(r->headers_in, "X-Chxj-Content-Length");
228 apr_table_set(r->headers_in, "Content-Length", contentLength);
235 DBG(r, "end chxj_headers_fixup()");
242 * It converts it from CHTML into XXML corresponding to each model.
245 * @param src [i] It is former HTML character string.
246 * @param len [i/o] It is length of former HTML character string.
249 chxj_exchange(request_rec *r, const char** src, apr_size_t* len, device_table *spec, const char *ua, cookie_t **cookiep)
255 mod_chxj_config *dconf;
256 chxjconvrule_entry *entryp;
258 DBG(r,"start of chxj_exchange() input:[%.*s]", (int)*len, *src);
259 dst = apr_pstrcat(r->pool, (char*)*src, NULL);
261 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
264 entryp = chxj_apply_convrule(r, dconf->convrules);
266 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT))
270 /*------------------------------------------------------------------------*/
271 /* get UserAgent from http header */
272 /*------------------------------------------------------------------------*/
273 if (entryp->user_agent)
274 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
276 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
278 DBG(r,"User-Agent:[%s]", user_agent);
279 DBG(r,"content type is %s", r->content_type);
282 if (! STRNCASEEQ('t','T', "text/html", r->content_type, sizeof("text/html")-1)
283 && ! STRNCASEEQ('a','A', "application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)) {
284 DBG(r,"no convert. content type is %s", r->content_type);
285 DBG(r,"end of chxj_exchange()");
289 if (ua && user_agent && strcasecmp(user_agent, ua) != 0) {
291 spec = chxj_specified_device(r, user_agent);
298 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
299 switch(spec->html_spec_type) {
300 case CHXJ_SPEC_Chtml_1_0:
301 case CHXJ_SPEC_Chtml_2_0:
302 case CHXJ_SPEC_Chtml_3_0:
303 case CHXJ_SPEC_Chtml_4_0:
304 case CHXJ_SPEC_Chtml_5_0:
305 case CHXJ_SPEC_XHtml_Mobile_1_0:
306 case CHXJ_SPEC_Jhtml:
307 cookie = chxj_save_cookie(r);
313 DBG(r, "save cookie done");
315 if (!r->header_only) {
316 DBG(r, "start do convert");
319 if (convert_routine[spec->html_spec_type].encoder)
320 tmp = convert_routine[spec->html_spec_type].encoder(r,
324 if (convert_routine[spec->html_spec_type].converter) {
326 dst = convert_routine[spec->html_spec_type].converter(r,
335 dst = convert_routine[spec->html_spec_type].converter(r,
344 DBG(r, "end do convert");
348 dst = apr_psprintf(r->pool, "\n");
355 ap_set_content_length(r, *len);
357 DBG(r, "end of chxj_exchange() [%s]", dst);
364 * It converts it from HEADER.
369 chxj_convert_input_header(request_rec *r,chxjconvrule_entry* entryp)
382 int no_update_flag = 0;
384 DBG(r, "start chxj_convert_input_header()");
387 DBG(r, "r->args=[null]");
388 DBG(r, "end chxj_convert_input_header()");
391 urilen = strlen(r->args);
393 result = qs_alloc_zero_byte_string(r);
395 buff_pre = apr_pstrdup(r->pool, r->args);
400 pair = apr_strtok(buff_pre, "&", &pstate);
406 pair_sv = apr_pstrdup(r->pool, pair);
408 name = apr_strtok(pair, "=", &vstate);
409 value = apr_strtok(NULL, "=", &vstate);
410 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
411 DBG(r, "found cookie no update parameter");
416 buff = apr_pstrdup(r->pool, r->args);
417 DBG(r, "r->args=[%s]", buff);
426 pair = apr_strtok(buff, "&", &pstate);
432 pair_sv = apr_pstrdup(r->pool, pair);
434 name = apr_strtok(pair, "=", &vstate);
435 value = apr_strtok(NULL, "=", &vstate);
436 if (strncasecmp(name, "_chxj", 5) != 0) {
437 if (strlen(result) != 0)
438 result = apr_pstrcat(r->pool, result, "&", NULL);
440 if (strcasecmp(entryp->encoding, "NONE") != 0) {
445 if (value && *value != 0) {
446 value = chxj_url_decode(r, value);
447 dlen = strlen(value);
448 DBG(r, "************ before encoding[%s]", value);
450 dvalue = chxj_rencoding(r, value, &dlen);
451 dvalue = chxj_url_encode(r, dvalue);
453 DBG(r, "************ after encoding[%s]", dvalue);
459 if (name && *name != 0) {
460 name = chxj_url_decode(r, name);
462 dname = chxj_rencoding(r, name, &dlen);
463 dname = chxj_url_encode(r, dname);
469 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
472 if (strcmp(name, pair_sv) != 0)
473 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
475 result = apr_pstrcat(r->pool, result, name, NULL);
479 if (strncasecmp(name, "_chxj_c_", 8) == 0
480 || strncasecmp(name, "_chxj_r_", 8) == 0
481 || strncasecmp(name, "_chxj_s_", 8) == 0) {
485 if (strlen(value) == 0)
488 if (strlen(result) != 0)
489 result = apr_pstrcat(r->pool, result, "&", NULL);
491 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
494 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0) {
495 DBG(r, "found cookie parameter[%s]", value);
496 DBG(r, "call start chxj_load_cookie()");
497 cookie = chxj_load_cookie(r, value);
498 DBG(r, "call end chxj_load_cookie()");
499 if (! no_update_flag && cookie) {
500 chxj_update_cookie(r, cookie);
506 DBG(r, "result r->args=[%s]", r->args);
507 DBG(r, "end chxj_convert_input_header()");
513 * It converts it from POSTDATA .
516 * @param src [i] It is POSTDATA character string.
517 * @param len [i/o] It is length of former HTML character string.
524 chxjconvrule_entry* entryp)
535 int no_update_flag = 0;
537 s = apr_pstrdup(r->pool, *src);
538 buff_pre = apr_pstrdup(r->pool, *src);
540 result = qs_alloc_zero_byte_string(r);
542 DBG(r, "BEFORE input convert source = [%s]", s);
547 pair = apr_strtok(buff_pre, "&", &pstate);
553 pair_sv = apr_pstrdup(r->pool, pair);
555 name = apr_strtok(pair, "=", &vstate);
556 value = apr_strtok(NULL, "=", &vstate);
557 if (strcasecmp(name, CHXJ_COOKIE_NOUPDATE_PARAM) == 0) {
558 DBG(r, "found cookie no update parameter");
568 pair = apr_strtok(s, "&", &pstate);
573 name = apr_strtok(pair, "=", &vstate);
574 value = apr_strtok(NULL, "=", &vstate);
575 if (strncasecmp(name, "_chxj", 5) != 0) {
576 if (strlen(result) != 0)
577 result = apr_pstrcat(r->pool, result, "&", NULL);
579 if (strcasecmp(entryp->encoding, "NONE") != 0) {
584 if (value && *value != 0) {
585 value = chxj_url_decode(r, value);
586 dlen = strlen(value);
587 DBG(r, "************ before encoding[%s]", value);
589 dvalue = chxj_rencoding(r, value, &dlen);
590 dvalue = chxj_url_encode(r, dvalue);
592 DBG(r, "************ after encoding[%s]", dvalue);
598 if (name && *name != 0) {
599 name = chxj_url_decode(r, name);
601 dname = chxj_rencoding(r, name, &dlen);
602 dname = chxj_url_encode(r, dname);
608 result = apr_pstrcat(r->pool, result, dname, "=", dvalue, NULL);
611 result = apr_pstrcat(r->pool, result, name, "=", value, NULL);
615 if (strncasecmp(name, "_chxj_c_", 8) == 0
616 || strncasecmp(name, "_chxj_r_", 8) == 0
617 || strncasecmp(name, "_chxj_s_", 8) == 0) {
621 if (strlen(value) == 0)
624 if (strlen(result) != 0)
625 result = apr_pstrcat(r->pool, result, "&", NULL);
627 if (strcasecmp(entryp->encoding, "NONE") != 0 && value && strlen(value)) {
631 dlen = strlen(value);
632 value = chxj_url_decode(r, value);
633 DBG(r, "************ before encoding[%s]", value);
635 dvalue = chxj_rencoding(r, value, &dlen);
636 dvalue = chxj_url_encode(r,dvalue);
638 DBG(r, "************ after encoding[%s]", dvalue);
640 result = apr_pstrcat(r->pool, result, &name[8], "=", dvalue, NULL);
644 result = apr_pstrcat(r->pool, result, &name[8], "=", value, NULL);
648 if (strcasecmp(name, CHXJ_COOKIE_PARAM) == 0) {
649 DBG(r, "found cookie parameter[%s]", value);
650 DBG(r, "call start chxj_load_cookie()");
651 cookie = chxj_load_cookie(r, value);
652 DBG(r, "call end chxj_load_cookie()");
653 if (! no_update_flag && cookie) {
654 chxj_update_cookie(r, cookie);
658 *len = strlen(result);
660 DBG(r, "AFTER input convert result = [%s]", result);
667 * The received data is returned to the filter.
669 * @param f [i/o] It is a filter.
670 * @param data [i] It is data returned to the filter.
671 * @param len [i] It is length of the data returned to the filter.
674 pass_data_to_filter(ap_filter_t *f, const char *data,
677 request_rec* r = f->r;
678 conn_rec* c = r->connection;
680 apr_bucket_brigade* bb;
683 DBG(r, "start pass_data_to_filter()");
685 bb = apr_brigade_create(r->pool, c->bucket_alloc);
686 b = apr_bucket_transient_create(data, len, c->bucket_alloc);
688 APR_BRIGADE_INSERT_TAIL(bb, b);
689 b = apr_bucket_eos_create(f->c->bucket_alloc);
690 APR_BRIGADE_INSERT_TAIL(bb, b);
692 rv = ap_pass_brigade(f->next, bb);
693 if (rv != APR_SUCCESS) {
694 DBG(r, "ap_pass_brigade()");
698 DBG(r, "end pass_data_to_filter()");
705 * It is the main loop of the output filter.
707 * @param f [i/o] It is a filter.
711 chxj_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
717 char* user_agent = NULL;
719 mod_chxj_ctx* ctx = (mod_chxj_ctx *)f->ctx;
720 cookie_t* cookie = NULL;
721 mod_chxj_config* dconf;
722 chxjconvrule_entry *entryp = NULL;
723 device_table *spec = NULL;
728 DBG(f->r, "start of chxj_output_filter()");
733 apr_pool_create(&pool, r->pool);
735 entryp = ctx->entryp;
737 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
740 if (! f->r->chunked) {
741 if ((f->r->proto_num >= 1001)
747 if (r->content_type) {
748 if (! STRNCASEEQ('t','T',"text/html",r->content_type, sizeof("text/html")-1)
749 && ! STRNCASEEQ('t','T',"text/xml", r->content_type, sizeof("text/xml")-1)
750 && ! STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
751 && ! (dconf->image == CHXJ_IMG_ON
752 && STRNCASEEQ('i','I',"image/", r->content_type, sizeof("image/") -1)
753 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
754 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
755 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
756 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
757 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
758 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
759 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
760 || STRCASEEQ('g','G',"gif", &r->content_type[6])))) { /* GIF */
762 DBG(r, "not convert content-type:[%s]", r->content_type);
763 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
764 DBG(r, "entryp->action == COOKIE_ON_BIT");
765 switch(spec->html_spec_type) {
766 case CHXJ_SPEC_Chtml_1_0:
767 case CHXJ_SPEC_Chtml_2_0:
768 case CHXJ_SPEC_Chtml_3_0:
769 case CHXJ_SPEC_Chtml_4_0:
770 case CHXJ_SPEC_Chtml_5_0:
771 case CHXJ_SPEC_XHtml_Mobile_1_0:
772 case CHXJ_SPEC_Jhtml:
773 cookie = chxj_save_cookie(r);
774 s_add_cookie_id_if_has_location_header(r, cookie);
780 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
781 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
782 r->status = HTTP_MOVED_TEMPORARILY;
785 ap_pass_brigade(f->next, bb);
790 DBG(r, "not convert content-type:[(null)]");
791 ap_pass_brigade(f->next, bb);
796 for (b = APR_BRIGADE_FIRST(bb);
797 b != APR_BRIGADE_SENTINEL(bb);
798 b = APR_BUCKET_NEXT(b)) {
800 if (apr_bucket_read(b, &data, &len, APR_BLOCK_READ) == APR_SUCCESS) {
801 DBG(r, "read data[%.*s]",(int)len, data);
803 /*--------------------------------------------------------------------*/
805 /*--------------------------------------------------------------------*/
807 DBG(r, "append data start");
808 ctx = (mod_chxj_ctx*)f->ctx;
811 tmp = apr_palloc(r->pool, ctx->len);
812 memcpy(tmp, ctx->buffer, ctx->len);
814 ctx->buffer = apr_palloc(pool, ctx->len + len);
816 memcpy(ctx->buffer, tmp, ctx->len);
817 memcpy(&ctx->buffer[ctx->len], data, len);
820 DBG(r, "append data end");
824 if (APR_BUCKET_IS_EOS(b)) {
827 /*----------------------------------------------------------------------*/
829 /*----------------------------------------------------------------------*/
832 ctx = (mod_chxj_ctx*)f->ctx;
834 DBG(r, "content_type=[%s]", r->content_type);
836 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
838 && (STRNCASEEQ('a','A',"application/xhtml+xml", r->content_type, sizeof("application/xhtml+xml")-1)
839 || STRNCASEEQ('t','T',"text/html", r->content_type, sizeof("text/html")-1))) {
840 DBG(r, "detect exchange target:[%s]", r->content_type);
845 tmp = apr_palloc(pool, ctx->len + 1);
847 memset(tmp, 0, ctx->len + 1);
848 memcpy(tmp, ctx->buffer, ctx->len);
851 DBG(r, "input data=[%s] len=[%d]", tmp, ctx->len);
854 ctx->buffer = chxj_exchange(r,
856 (apr_size_t*)&ctx->len,
858 user_agent, &cookie);
861 DBG(r, "output data=[%.*s]", ctx->len,ctx->buffer);
865 ctx->buffer = apr_psprintf(r->pool, "\n");
867 ctx->buffer = chxj_exchange(r,
868 (const char**)&ctx->buffer,
869 (apr_size_t*)&ctx->len,
871 user_agent, &cookie);
876 && *(char*)r->content_type == 't'
877 && strncmp(r->content_type, "text/xml", 8) == 0) {
886 memset(&doc, 0, sizeof(Doc));
887 memset(&qrcode, 0, sizeof(qr_code_t));
889 doc.parse_mode = PARSE_MODE_CHTML;
893 qs_init_malloc(&doc);
895 root = qs_parse_string(&doc, ctx->buffer, ctx->len);
898 for (child = qs_get_child_node(&doc,root);
900 child = qs_get_next_node(&doc,child)) {
901 char *name = qs_get_node_name(&doc,child);
902 if (strcasecmp("qrcode",name) == 0) {
907 qs_all_free(&doc,QX_LOGMARK);
909 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
910 chxj_qrcode_node_to_qrcode(&qrcode, root);
911 sts = chxj_qrcode_create_image_data(&qrcode, &ctx->buffer, &ctx->len);
913 ERR(r, "qrcode create failed.");
916 r->content_type = apr_psprintf(r->pool, "image/jpeg");
920 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN
922 && ( *r->content_type == 'i' || *r->content_type == 'I')
923 && dconf->image == CHXJ_IMG_ON
924 && strncasecmp("image/", r->content_type, 6) == 0
925 && ( STRCASEEQ('j','J',"jpeg", &r->content_type[6]) /* JPEG */
926 || STRCASEEQ('j','J',"jp2", &r->content_type[6]) /* JPEG2000 */
927 || STRCASEEQ('j','J',"jpeg2000", &r->content_type[6]) /* JPEG2000 */
928 || STRCASEEQ('j','J',"jpeg2000-image", &r->content_type[6]) /* JPEG2000 */
929 || STRCASEEQ('x','X',"x-jpeg2000-image",&r->content_type[6]) /* JPEG2000 */
930 || STRCASEEQ('p','P',"png", &r->content_type[6]) /* PNG */
931 || STRCASEEQ('x','X',"x-png", &r->content_type[6]) /* PNG */
932 || STRCASEEQ('g','G',"gif", &r->content_type[6]))) { /* GIF */
936 tmp = apr_palloc(pool, ctx->len + 1);
938 memset(tmp, 0, ctx->len + 1);
939 memcpy(tmp, ctx->buffer, ctx->len);
941 chxj_exchange_image(r,
943 (apr_size_t*)&ctx->len);
944 if (ctx->buffer == NULL) {
950 apr_table_unset(r->headers_out, "Content-Length");
951 apr_table_unset(r->err_headers_out, "Content-Length");
952 ap_set_content_length(r, (apr_off_t)ctx->len);
955 DBG(r, "call pass_data_to_filter()");
956 s_add_cookie_id_if_has_location_header(r, cookie);
957 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
958 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
959 r->status = HTTP_MOVED_TEMPORARILY;
962 rv = pass_data_to_filter(f,
963 (const char*)ctx->buffer,
964 (apr_size_t)ctx->len);
969 DBG(r, " SAVE COOKIE[%x]", entryp->action);
974 if (entryp->action & CONVRULE_COOKIE_ON_BIT) {
975 DBG(r, "entryp->action == COOKIE_ON_BIT");
976 switch(spec->html_spec_type) {
977 case CHXJ_SPEC_Chtml_1_0:
978 case CHXJ_SPEC_Chtml_2_0:
979 case CHXJ_SPEC_Chtml_3_0:
980 case CHXJ_SPEC_Chtml_4_0:
981 case CHXJ_SPEC_Chtml_5_0:
982 case CHXJ_SPEC_XHtml_Mobile_1_0:
983 case CHXJ_SPEC_Jhtml:
984 cookie = chxj_save_cookie(r);
986 * Location Header Check to add cookie parameter.
988 s_add_cookie_id_if_has_location_header(r, cookie);
992 if (apr_table_get(r->headers_out, "Location") || apr_table_get(r->err_headers_out, "Location")) {
993 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
994 r->status = HTTP_MOVED_TEMPORARILY;
1000 apr_table_setn(r->headers_out, "Content-Length", "0");
1001 DBG(r, "call pass_data_to_filter()");
1002 rv = pass_data_to_filter(f, (const char*)"", (apr_size_t)0);
1007 apr_brigade_destroy(bb);
1009 DBG(r, "end of output filter");
1015 * Add Cookie_id if it has location header.
1018 s_add_cookie_id_if_has_location_header(request_rec *r, cookie_t *cookie)
1020 char *location_header = (char*)apr_table_get(r->headers_out, "Location");
1021 if (! location_header) {
1022 location_header = (char *)apr_table_get(r->err_headers_out, "Location");
1024 if (cookie && location_header) {
1025 DBG(r, "Location Header=[%s]", location_header);
1026 location_header = chxj_add_cookie_parameter(r,
1029 apr_table_unset(r->headers_out, "Location");
1030 apr_table_add(r->headers_out, apr_pstrdup(r->pool, "Location"), location_header);
1031 DBG(r, "Location Header=[%s]", location_header);
1032 DBG(r, "Status-Code:[%d]", r->status);
1033 if (r->status < HTTP_MULTIPLE_CHOICES || r->status > HTTP_TEMPORARY_REDIRECT) {
1034 r->status = HTTP_MOVED_TEMPORARILY;
1040 * It is the main loop of the input filter handler.
1044 chxj_input_handler(request_rec *r)
1046 mod_chxj_config *dconf;
1047 chxjconvrule_entry *entryp = NULL;
1048 device_table *spec = NULL;
1049 char *post_data = NULL;
1050 apr_size_t post_data_len = 0;
1055 DBG(r, "start of chxj_input_handler()");
1057 if (strcasecmp(r->handler, "chxj-input-handler")) {
1058 DBG(r, "end chxj_input_handler()");
1061 apr_pool_create(&pool, r->pool);
1063 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1064 user_agent = (char*)apr_table_get(r->headers_in, "User-Agent");
1065 spec = chxj_specified_device(r, user_agent);
1066 entryp = chxj_apply_convrule(r, dconf->convrules);
1068 post_data = apr_pstrdup(pool, "");
1069 if (ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK) == OK) {
1070 if (ap_should_client_block(r)) {
1071 while (post_data_len < CHXJ_POST_MAX) {
1074 int read_bytes = ap_get_client_block(r, buffer, BUFSZ-1);
1075 if (read_bytes<=0) {
1078 buffer[read_bytes] = '\0';
1079 post_data = apr_pstrcat(pool, post_data, buffer, NULL);
1080 post_data_len += read_bytes;
1089 if (post_data_len > 0) {
1090 post_data = chxj_input_convert(r, (const char**)&post_data, (apr_size_t*)&post_data_len, entryp);
1091 DBG(r, "(in:exchange)POSTDATA:[%s]", post_data);
1095 if (dconf->forward_url_base) {
1096 url_path = apr_psprintf(pool, "%s%s", dconf->forward_url_base, r->uri);
1099 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);
1102 url_path = apr_pstrcat(pool, url_path, "?", r->args, NULL);
1104 DBG(r, "==> new url_path:[%s]", url_path);
1107 apr_table_setn(r->headers_in, CHXJ_HEADER_ORIG_CLIENT_IP, r->connection->remote_ip);
1108 apr_table_unset(r->headers_in, "Content-Length");
1109 apr_table_setn(r->headers_in, "Content-Length", apr_psprintf(pool, "%d", post_data_len));
1110 response = chxj_serf_post(r, pool, url_path, post_data, post_data_len, 1, &res_len);
1111 DBG(r, "response:[%.*s][%d]", res_len, response, res_len);
1114 if ((chunked = (char *)apr_table_get(r->headers_out, "Transfer-Encoding")) != NULL) {
1115 if (strncasecmp(chunked, "chunked", sizeof("chunked")-1) == 0) {
1117 apr_table_unset(r->headers_out, "Transfer-Encoding");
1119 DBG(r, "TRANSFER-ENCODING:[%s]",chunked);
1123 apr_pool_create(&wpool, r->pool);
1124 apr_bucket_brigade *bb;
1127 conn_rec *c = r->connection;
1129 bb = apr_brigade_create(wpool, c->bucket_alloc);
1130 e = apr_bucket_transient_create(response, res_len, c->bucket_alloc);
1131 APR_BRIGADE_INSERT_TAIL(bb, e);
1132 e = apr_bucket_eos_create(c->bucket_alloc);
1133 APR_BRIGADE_INSERT_TAIL(bb, e);
1134 if ((rv = ap_pass_brigade(r->output_filters, bb)) != APR_SUCCESS) {
1135 ERR(r, "%s:%d failed ap_pass_brigade()", APLOG_MARK);
1138 apr_brigade_cleanup(bb);
1141 DBG(r, "end of chxj_input_handler()");
1145 static mod_chxj_global_config*
1146 chxj_global_config_create(apr_pool_t* pool, server_rec* s)
1148 mod_chxj_global_config* conf;
1150 SDBG(s, "start chxj_global_config_create()");
1152 /*--------------------------------------------------------------------------*/
1153 /* allocate an own subpool which survives server restarts */
1154 /*--------------------------------------------------------------------------*/
1155 conf = (mod_chxj_global_config*)apr_palloc(pool,
1156 sizeof(mod_chxj_global_config));
1158 conf->cookie_db_lock = NULL;
1160 SDBG(s, "end chxj_global_config_create()");
1167 * initialize chxj module
1170 chxj_init_module(apr_pool_t *p,
1171 apr_pool_t* UNUSED(plog),
1172 apr_pool_t* UNUSED(ptemp),
1177 SDBG(s, "start chxj_init_module()");
1179 apr_pool_userdata_get(&user_data, CHXJ_MOD_CONFIG_KEY, s->process->pool);
1181 if (user_data == NULL) {
1184 * dummy user_data set.
1186 apr_pool_userdata_set(
1188 CHXJ_MOD_CONFIG_KEY,
1189 apr_pool_cleanup_null,
1191 SDBG(s, "end chxj_init_module()");
1195 ap_add_version_component(p, CHXJ_VERSION_PREFIX CHXJ_VERSION);
1197 SDBG(s, "end chxj_init_module()");
1204 chxj_child_init(apr_pool_t* UNUSED(p), server_rec *UNUSED(s))
1210 * A set structure of each server is generated.
1216 chxj_config_server_create(apr_pool_t *p, server_rec *s)
1218 mod_chxj_global_config *gc;
1220 gc = chxj_global_config_create(p,s);
1227 chxj_translate_name(request_rec *r)
1229 return chxj_trans_name(r);
1234 chxj_insert_filter(request_rec *r)
1238 mod_chxj_config *dconf;
1239 chxjconvrule_entry *entryp;
1244 DBG(r, "start chxj_insert_filter()");
1246 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
1248 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
1250 contentType = (char *)apr_table_get(r->headers_in, "Content-Type");
1252 && strncasecmp("multipart/form-data", contentType, 19) == 0) {
1253 DBG(r, "detect multipart/form-data ==> no target");
1257 spec = chxj_specified_device(r, user_agent);
1258 entryp = chxj_apply_convrule(r, dconf->convrules);
1260 DBG(r, "end chxj_insert_filter()");
1263 ctx = apr_palloc(r->pool, sizeof(*ctx));
1264 memset(ctx, 0, sizeof(*ctx));
1265 if ((rv = apr_pool_create(&ctx->pool, r->pool)) != APR_SUCCESS) {
1266 ERR(r, "failed: new pool create. rv:[%d]", rv);
1269 ctx->entryp = entryp;
1271 ctx->buffer = apr_palloc(ctx->pool, 1);
1274 if (!entryp || !(entryp->action & CONVRULE_ENGINE_ON_BIT)) {
1279 switch(spec->html_spec_type) {
1280 case CHXJ_SPEC_Chtml_1_0:
1281 case CHXJ_SPEC_Chtml_2_0:
1282 case CHXJ_SPEC_Chtml_3_0:
1283 case CHXJ_SPEC_Chtml_4_0:
1284 case CHXJ_SPEC_Chtml_5_0:
1285 case CHXJ_SPEC_XHtml_Mobile_1_0:
1286 case CHXJ_SPEC_Hdml:
1287 case CHXJ_SPEC_Jhtml:
1295 if (! apr_table_get(r->headers_in, "X-Chxj-Forward")) {
1296 ap_add_output_filter("chxj_output_filter", ctx, r, r->connection);
1297 DBG(r, "added Output Filter");
1300 DBG(r, "end chxj_insert_filter()");
1305 * The hook is registered.
1310 chxj_register_hooks(apr_pool_t* UNUSED(p))
1312 ap_hook_post_config(chxj_init_module,
1315 APR_HOOK_REALLY_FIRST);
1316 ap_hook_child_init(chxj_child_init,
1319 APR_HOOK_REALLY_FIRST);
1320 ap_register_output_filter (
1321 "chxj_output_filter",
1325 ap_hook_insert_filter(chxj_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1326 ap_hook_handler(chxj_img_conv_format_handler, NULL, NULL, APR_HOOK_MIDDLE);
1327 ap_hook_handler(chxj_qr_code_handler, NULL, NULL, APR_HOOK_MIDDLE);
1328 ap_hook_handler(chxj_input_handler, NULL, NULL, APR_HOOK_MIDDLE);
1329 ap_hook_translate_name(chxj_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1330 ap_hook_fixups(chxj_headers_fixup, NULL, NULL, APR_HOOK_LAST);
1335 * A set structure according to the directory is generated.
1341 chxj_create_per_dir_config(apr_pool_t *p, char *arg)
1343 mod_chxj_config* conf;
1345 conf = apr_pcalloc(p, sizeof(mod_chxj_config));
1346 conf->device_data_file = NULL;
1347 conf->devices = NULL;
1348 conf->emoji_data_file = NULL;
1350 conf->emoji_tail = NULL;
1351 conf->image = CHXJ_IMG_NONE;
1352 conf->image_cache_dir = apr_psprintf(p, "%s",DEFAULT_IMAGE_CACHE_DIR);
1353 conf->image_cache_limit = 0;
1354 conf->server_side_encoding = NULL;
1355 conf->cookie_db_dir = NULL;
1356 conf->cookie_timeout = 0;
1357 conf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1358 conf->cookie_lazy_mode = 0;
1359 #if defined(USE_MYSQL_COOKIE)
1360 memset((void*)&conf->mysql, 0, sizeof(mysql_t));
1361 conf->mysql.port = MYSQL_PORT;
1362 conf->mysql.host = NULL;
1364 #if defined(USE_MEMCACHE_COOKIE)
1365 memset((void*)&conf->memcache, 0, sizeof(memcache_t));
1366 conf->memcache.host = NULL;
1367 conf->memcache.port = 0;
1369 conf->forward_url_base = NULL;
1370 conf->forward_server_ip = NULL;
1376 conf->dir = apr_pcalloc(p, strlen(arg)+1);
1377 memset(conf->dir, 0, strlen(arg)+1);
1378 strcpy(conf->dir, arg);
1380 conf->convrules = apr_array_make(p, 2, sizeof(chxjconvrule_entry));
1382 /* Default is copyleft */
1383 conf->image_copyright = NULL;
1390 * Merge per-directory CHXJ configurations
1393 chxj_merge_per_dir_config(apr_pool_t *p, void *basev, void *addv)
1395 mod_chxj_config *base;
1396 mod_chxj_config *add;
1397 mod_chxj_config *mrg;
1399 base = (mod_chxj_config*)basev;
1400 add = (mod_chxj_config*)addv;
1401 mrg = (mod_chxj_config*)apr_palloc(p, sizeof(mod_chxj_config));
1403 mrg->device_data_file = NULL;
1404 mrg->devices = NULL;
1405 mrg->emoji_data_file = NULL;
1406 mrg->image = CHXJ_IMG_NONE;
1407 mrg->image_cache_dir = NULL;
1408 mrg->image_copyright = NULL;
1409 mrg->image_cache_limit = 0;
1411 mrg->emoji_tail = NULL;
1412 mrg->forward_url_base = NULL;
1413 mrg->forward_server_ip = NULL;
1415 mrg->dir = apr_pstrdup(p, add->dir);
1417 if (! add->device_data_file) {
1418 mrg->devices = base->devices;
1419 mrg->device_data_file = apr_pstrdup(p, base->device_data_file);
1422 mrg->devices = add->devices;
1423 mrg->device_data_file = apr_pstrdup(p, add->device_data_file);
1426 if (! add->emoji_data_file) {
1427 mrg->emoji = base->emoji;
1428 mrg->emoji_tail = base->emoji_tail;
1429 mrg->emoji_data_file = apr_pstrdup(p, base->emoji_data_file);
1432 mrg->emoji = add->emoji;
1433 mrg->emoji_tail = add->emoji_tail;
1434 mrg->emoji_data_file = apr_pstrdup(p, add->emoji_data_file);
1437 if (add->image == CHXJ_IMG_NONE) {
1438 mrg->image = base->image;
1441 mrg->image = add->image;
1444 if (strcasecmp(add->image_cache_dir ,DEFAULT_IMAGE_CACHE_DIR)==0) {
1445 mrg->image_cache_dir = apr_pstrdup(p, base->image_cache_dir);
1448 mrg->image_cache_dir = apr_pstrdup(p, add->image_cache_dir);
1451 if (add->image_cache_limit) {
1452 mrg->image_cache_limit = add->image_cache_limit;
1455 mrg->image_cache_limit = base->image_cache_limit;
1458 if (add->image_copyright)
1459 mrg->image_copyright = apr_pstrdup(p, add->image_copyright);
1461 mrg->image_copyright = apr_pstrdup(p, base->image_copyright);
1463 if (add->server_side_encoding) {
1464 mrg->server_side_encoding = apr_pstrdup(p, add->server_side_encoding);
1467 if (base->server_side_encoding) {
1468 mrg->server_side_encoding = apr_pstrdup(p, base->server_side_encoding);
1471 mrg->server_side_encoding = apr_pstrdup(p, DEFAULT_SERVER_SIDE_ENCODING);
1474 mrg->convrules = apr_array_append(p, add->convrules, base->convrules);
1476 if (add->cookie_db_dir) {
1477 mrg->cookie_db_dir = apr_pstrdup(p, add->cookie_db_dir);
1480 if (base->cookie_db_dir) {
1481 mrg->cookie_db_dir = apr_pstrdup(p, base->cookie_db_dir);
1484 mrg->cookie_db_dir = NULL;
1487 if (add->cookie_timeout) {
1488 mrg->cookie_timeout = add->cookie_timeout;
1491 if (base->cookie_db_dir) {
1492 mrg->cookie_timeout = base->cookie_timeout;
1495 mrg->cookie_timeout = 0;
1498 #if defined(USE_MYSQL_COOKIE)
1499 if (add->mysql.host) {
1500 mrg->mysql.host = apr_pstrdup(p, add->mysql.host);
1502 else if (base->mysql.host) {
1503 mrg->mysql.host = apr_pstrdup(p, base->mysql.host);
1506 mrg->mysql.host = NULL;
1508 if (add->mysql.port) {
1509 mrg->mysql.port = add->mysql.port;
1511 else if (base->mysql.port) {
1512 mrg->mysql.port = base->mysql.port;
1515 mrg->mysql.port = 0;
1518 if (add->mysql.database) {
1519 mrg->mysql.database = apr_pstrdup(p, add->mysql.database);
1521 else if (base->mysql.database) {
1522 mrg->mysql.database = apr_pstrdup(p, base->mysql.database);
1525 mrg->mysql.database = NULL;
1528 if (add->mysql.username) {
1529 mrg->mysql.username = apr_pstrdup(p, add->mysql.username);
1531 else if (base->mysql.username) {
1532 mrg->mysql.username = apr_pstrdup(p, base->mysql.username);
1535 mrg->mysql.username = NULL;
1538 if (add->mysql.password) {
1539 mrg->mysql.password = apr_pstrdup(p, add->mysql.password);
1541 else if (base->mysql.password) {
1542 mrg->mysql.password = apr_pstrdup(p, base->mysql.password);
1545 mrg->mysql.password = NULL;
1548 if (add->mysql.tablename) {
1549 mrg->mysql.tablename = apr_pstrdup(p, add->mysql.tablename);
1551 else if (base->mysql.tablename) {
1552 mrg->mysql.tablename = apr_pstrdup(p, base->mysql.tablename);
1555 mrg->mysql.tablename = NULL;
1558 if (add->mysql.socket_path) {
1559 mrg->mysql.socket_path = apr_pstrdup(p, add->mysql.socket_path);
1561 else if (base->mysql.socket_path) {
1562 mrg->mysql.socket_path = apr_pstrdup(p, base->mysql.socket_path);
1565 mrg->mysql.socket_path = NULL;
1568 if (add->mysql.charset) {
1569 mrg->mysql.charset = apr_pstrdup(p, add->mysql.charset);
1571 else if (base->mysql.charset) {
1572 mrg->mysql.charset = apr_pstrdup(p, base->mysql.charset);
1575 mrg->mysql.charset = NULL;
1578 #if defined(USE_MEMCACHE_COOKIE)
1579 if (add->memcache.host) {
1580 mrg->memcache.host = apr_pstrdup(p, add->memcache.host);
1582 else if (base->memcache.host) {
1583 mrg->memcache.host = apr_pstrdup(p, base->memcache.host);
1586 mrg->memcache.host = NULL;
1588 if (add->memcache.port) {
1589 mrg->memcache.port = add->memcache.port;
1591 else if (base->memcache.port) {
1592 mrg->memcache.port = base->memcache.port;
1595 mrg->memcache.port = 0;
1598 if (add->cookie_store_type) {
1599 mrg->cookie_store_type = add->cookie_store_type;
1601 else if (base->cookie_store_type) {
1602 mrg->cookie_store_type = base->cookie_store_type;
1605 mrg->cookie_store_type = COOKIE_STORE_TYPE_NONE;
1607 if (add->cookie_lazy_mode) {
1608 mrg->cookie_lazy_mode = add->cookie_lazy_mode;
1610 else if (base->cookie_lazy_mode) {
1611 mrg->cookie_lazy_mode = base->cookie_lazy_mode;
1614 mrg->cookie_lazy_mode = 0;
1616 if (add->forward_url_base) {
1617 mrg->forward_url_base = apr_pstrdup(p, add->forward_url_base);
1619 else if (base->forward_url_base) {
1620 mrg->forward_url_base = apr_pstrdup(p, base->forward_url_base);
1623 mrg->forward_url_base = NULL;
1625 if (add->forward_server_ip) {
1626 mrg->forward_server_ip = apr_pstrdup(p, add->forward_server_ip);
1628 else if (base->forward_server_ip) {
1629 mrg->forward_server_ip = apr_pstrdup(p, base->forward_server_ip);
1632 mrg->forward_server_ip = NULL;
1639 chxj_command_parse_take5(
1652 for (;*strp == ' '||*strp == '\t'; strp++) ;
1662 for (; *strp != '\0'; strp++) {
1663 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1664 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1669 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1670 || (isquoted && *strp == '"'))
1684 for (;*strp == ' '||*strp == '\t'; strp++) ;
1693 for (; *strp != '\0'; strp++) {
1694 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1695 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1700 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1701 || (isquoted && *strp == '"'))
1714 for (;*strp == ' '||*strp == '\t'; strp++);
1722 for (; *strp != '\0'; strp++) {
1723 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1724 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1729 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1730 || (isquoted && *strp == '"'))
1742 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 == '"'))
1769 for (;*strp == ' '||*strp == '\t'; strp++);
1777 for (; *strp != '\0'; strp++) {
1778 if ((isquoted && (*strp == ' ' || *strp == '\t'))
1779 || (*strp == '\\' && (*(strp+1) == ' ' || *(strp+1) == '\t'))) {
1784 if ((!isquoted && (*strp == ' ' || *strp == '\t'))
1785 || (isquoted && *strp == '"'))
1795 * The device definition file is loaded.
1797 * @param arg [i] The name of the device definition file is specified.
1798 * @param mconfig [i/o] The pointer to a set structure is specified.
1802 cmd_load_device_data(cmd_parms *parms, void *mconfig, const char* arg)
1804 mod_chxj_config* conf;
1809 if (strlen(arg) > 256)
1810 return "mod_chxj: device data filename too long.";
1812 conf = (mod_chxj_config*)mconfig;
1813 conf->device_data_file = apr_pstrdup(parms->pool, arg);
1815 qs_init_malloc(&doc);
1816 qs_init_root_node(&doc);
1818 qs_parse_file((Doc*)&doc, (const char*)arg);
1819 chxj_load_device_data(&doc,parms->pool, conf);
1820 qs_all_free(&doc, QX_LOGMARK);
1827 * Device definition information is loaded.
1830 * @param arg [i] The name of the device definition file is specified.
1831 * @param mconfig [i/o] The pointer to a set structure is specified.
1835 cmd_load_emoji_data(cmd_parms *parms, void *mconfig, const char* arg)
1837 mod_chxj_config* conf;
1844 if (strlen(arg) > 256)
1845 return "mod_chxj: emoji data filename too long.";
1847 conf = (mod_chxj_config*)mconfig;
1848 conf->emoji_data_file = apr_pstrdup(parms->pool, arg);
1849 qs_init_malloc(&doc);
1850 qs_init_root_node(&doc);
1852 qs_parse_file((Doc*)&doc, (const char*)arg);
1854 rtn = chxj_load_emoji_data(&doc,parms->pool, conf);
1856 qs_all_free(&doc, QX_LOGMARK);
1864 cmd_set_image_engine(cmd_parms* UNUSED(parms), void *mconfig, const char* arg)
1866 mod_chxj_config* conf;
1871 if (strlen(arg) > 256)
1872 return "image uri is too long.";
1874 conf = (mod_chxj_config*)mconfig;
1875 if (strcasecmp("ON", arg) == 0) {
1876 conf->image = CHXJ_IMG_ON;
1879 conf->image = CHXJ_IMG_OFF;
1887 cmd_set_image_cache_dir(cmd_parms *parms, void *mconfig, const char* arg)
1889 mod_chxj_config* conf;
1894 if (strlen(arg) > 256)
1895 return "cache dir name is too long.";
1897 conf = (mod_chxj_config*)mconfig;
1898 conf->image_cache_dir = apr_pstrdup(parms->pool, arg);
1905 cmd_set_image_cache_limit(cmd_parms *parms, void *mconfig, const char* arg)
1907 mod_chxj_config* conf;
1912 if (strlen(arg) > IMAGE_CACHE_LIMIT_FMT_LEN)
1913 return "cache size is too long.";
1915 conf = (mod_chxj_config*)mconfig;
1918 * I use strtol function because strtoul is not portable function.
1920 conf->image_cache_limit = (unsigned long)strtol(arg, NULL, 10);
1923 return apr_psprintf(parms->pool, "ChxjImageCacheLimit invalid value [%s] errno:[%d]", arg, errno);
1925 return apr_psprintf(parms->pool, "ChxjImageCacheLimit Out of range [%s] errno:[%d]", arg, errno);
1934 cmd_set_image_copyright(cmd_parms *parms, void* mconfig, const char* arg)
1936 mod_chxj_config* conf;
1941 if (strlen(arg) > 256)
1942 return "Copyright Flag is too long.";
1944 conf = (mod_chxj_config*)mconfig;
1945 conf->image_copyright = apr_pstrdup(parms->pool, arg);
1952 cmd_convert_rule(cmd_parms *cmd, void* mconfig, const char *arg)
1956 mod_chxj_config* dconf;
1957 chxjconvrule_entry* newrule;
1967 dconf = (mod_chxj_config*)mconfig;
1969 if (strlen(arg) > 4096)
1970 return "mod_chxj: ChxjConvertRule: is too long.";
1972 dconf = (mod_chxj_config*)mconfig;
1973 if (dconf->convrules == NULL)
1974 dconf->convrules = apr_array_make(cmd->pool,
1976 sizeof(chxjconvrule_entry));
1978 newrule = apr_array_push(dconf->convrules);
1981 newrule->action = 0;
1983 if (chxj_command_parse_take5(arg, &prm1, &prm2, &prm3, &prm4, &prm5))
1984 return "ChxjConvertRule: bad argument line";
1986 newrule->pattern = apr_pstrdup(cmd->pool, prm1);
1990 if ((action = apr_strtok(prm2, ",", &pstate)) == NULL)
1996 if (strcasecmp(CONVRULE_ENGINE_ON_CMD, action) == 0) {
1997 newrule->action |= CONVRULE_ENGINE_ON_BIT;
2000 if (strcasecmp(CONVRULE_ENGINE_OFF_CMD, action) == 0) {
2001 newrule->action |= CONVRULE_ENGINE_OFF_BIT;
2007 if (strcasecmp(CONVRULE_COOKIE_ON_CMD, action) == 0) {
2008 newrule->action |= CONVRULE_COOKIE_ON_BIT;
2018 newrule->flags |= CONVRULE_FLAG_NOTMATCH;
2022 mode = AP_REG_EXTENDED;
2023 if ((regexp = ap_pregcomp((apr_pool_t *)cmd->pool, (const char *)pp, mode)) == NULL)
2024 return "RewriteRule: cannot compile regular expression ";
2026 newrule->regexp = regexp;
2028 newrule->encoding = apr_pstrdup(cmd->pool, prm3);
2030 newrule->encoding = apr_pstrdup(cmd->pool, "none");
2032 newrule->pc_flag = CONVRULE_PC_FLAG_OFF_BIT;
2034 if (strcasecmp(CONVRULE_PC_FLAG_ON_CMD, prm4) == 0)
2035 newrule->pc_flag = CONVRULE_PC_FLAG_ON_BIT;
2037 newrule->user_agent = NULL;
2039 newrule->user_agent = apr_pstrdup(cmd->pool, prm5);
2051 mod_chxj_config* dconf;
2054 if (strlen(arg) > 4096)
2055 return "mod_chxj: ChxjCookieDir is too long.";
2057 dconf = (mod_chxj_config*)mconfig;
2059 dconf->cookie_db_dir = apr_pstrdup(cmd->pool, arg);
2066 cmd_set_cookie_timeout(
2067 cmd_parms* UNUSED(cmd),
2071 mod_chxj_config* dconf;
2074 if (strlen(arg) > 4096)
2075 return "mod_chxj: ChxjCookieTimeout is too long.";
2077 if (chxj_chk_numeric(arg) != 0)
2078 return "mod_chxj: ChxjCookieTimeout is not numeric.";
2080 dconf = (mod_chxj_config*)mconfig;
2082 dconf->cookie_timeout = atoi(arg);
2088 #if defined(USE_MYSQL_COOKIE)
2090 cmd_set_cookie_mysql_database(
2095 mod_chxj_config *dconf;
2097 if (strlen(arg) > 255)
2098 return "mod_chxj: ChxjCookieMysqlDatabase is too long.";
2100 dconf = (mod_chxj_config *)mconfig;
2102 dconf->mysql.database = apr_pstrdup(cmd->pool, arg);
2109 cmd_set_cookie_mysql_username(
2114 mod_chxj_config *dconf;
2116 if (strlen(arg) > 255)
2117 return "mod_chxj: ChxjCookieMysqlUsername is too long.";
2119 dconf = (mod_chxj_config *)mconfig;
2121 dconf->mysql.username = apr_pstrdup(cmd->pool, arg);
2128 cmd_set_cookie_mysql_password(
2133 mod_chxj_config *dconf;
2135 if (strlen(arg) > 255)
2136 return "mod_chxj: ChxjCookieMysqlPassword is too long.";
2138 dconf = (mod_chxj_config *)mconfig;
2140 dconf->mysql.password = apr_pstrdup(cmd->pool, arg);
2147 cmd_set_cookie_mysql_table_name(
2152 mod_chxj_config *dconf;
2154 if (strlen(arg) > 255)
2155 return "mod_chxj: ChxjCookieMysqlTableName is too long.";
2157 dconf = (mod_chxj_config *)mconfig;
2159 dconf->mysql.tablename = apr_pstrdup(cmd->pool, arg);
2165 cmd_set_cookie_mysql_port(
2166 cmd_parms *UNUSED(cmd),
2170 mod_chxj_config *dconf;
2172 if (strlen(arg) > 255)
2173 return "mod_chxj: ChxjCookieMysqlPort is too long.";
2175 dconf = (mod_chxj_config *)mconfig;
2177 if (chxj_chk_numeric(arg) != 0)
2178 return "mod_chxj: ChxjCookieMysqlPort is not numeric.";
2180 dconf = (mod_chxj_config *)mconfig;
2182 dconf->mysql.port = chxj_atoi(arg);
2189 cmd_set_cookie_mysql_host(
2194 mod_chxj_config *dconf;
2196 if (strlen(arg) > 255)
2197 return "mod_chxj: ChxjCookieMysqlHost is too long.";
2199 dconf = (mod_chxj_config *)mconfig;
2201 dconf->mysql.host = apr_pstrdup(cmd->pool, arg);
2208 cmd_set_cookie_mysql_socket_path(
2213 mod_chxj_config *dconf;
2215 if (strlen(arg) > 4096)
2216 return "mod_chxj: ChxjCookieMysqlSocketPath is too long.";
2218 dconf = (mod_chxj_config *)mconfig;
2220 dconf->mysql.socket_path = apr_pstrdup(cmd->pool, arg);
2227 cmd_set_cookie_mysql_charset(
2232 mod_chxj_config *dconf;
2234 if (strlen(arg) > 255)
2235 return "mod_chxj: ChxjCookieMysqlCharset is too long.";
2237 dconf = (mod_chxj_config *)mconfig;
2239 dconf->mysql.charset = apr_pstrdup(cmd->pool, arg);
2244 #if defined(USE_MEMCACHE_COOKIE)
2246 cmd_set_cookie_memcache_port(
2247 cmd_parms *UNUSED(cmd),
2251 mod_chxj_config *dconf;
2253 if (strlen(arg) > 255)
2254 return "mod_chxj: ChxjCookieMemcachePort is too long.";
2256 dconf = (mod_chxj_config *)mconfig;
2258 if (chxj_chk_numeric(arg) != 0)
2259 return "mod_chxj: ChxjCookieMemcachePort is not numeric.";
2261 dconf = (mod_chxj_config *)mconfig;
2263 dconf->memcache.port = (apr_port_t)chxj_atoi(arg);
2270 cmd_set_cookie_memcache_host(
2275 mod_chxj_config *dconf;
2277 if (strlen(arg) > 255)
2278 return "mod_chxj: ChxjCookieMemcacheHost is too long.";
2280 dconf = (mod_chxj_config *)mconfig;
2282 dconf->memcache.host = apr_pstrdup(cmd->pool, arg);
2289 cmd_set_cookie_lazy_mode(
2290 cmd_parms *UNUSED(cmd),
2294 mod_chxj_config *dconf;
2296 if (strlen(arg) > 255)
2297 return "mod_chxj: ChxjCookieLazyMode is too long.";
2299 dconf = (mod_chxj_config *)mconfig;
2301 if (strcasecmp("TRUE",arg) == 0) {
2302 dconf->cookie_lazy_mode = COOKIE_LAZY_ON;
2305 dconf->cookie_lazy_mode = COOKIE_LAZY_OFF;
2312 cmd_set_cookie_store_type(
2313 cmd_parms *UNUSED(cmd),
2317 mod_chxj_config *dconf;
2319 if (strlen(arg) > 255)
2320 return "mod_chxj: ChxjCookieStoreType is too long.";
2322 dconf = (mod_chxj_config *)mconfig;
2324 if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_DBM, arg) == 0) {
2325 dconf->cookie_store_type = COOKIE_STORE_TYPE_DBM;
2327 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MYSQL, arg) == 0) {
2328 dconf->cookie_store_type = COOKIE_STORE_TYPE_MYSQL;
2330 else if (strcasecmp(CHXJ_COOKIE_STORE_TYPE_MEMCACHE, arg) == 0) {
2331 dconf->cookie_store_type = COOKIE_STORE_TYPE_MEMCACHE;
2334 dconf->cookie_store_type = COOKIE_STORE_TYPE_NONE;
2341 cmd_set_forward_url_base(
2346 mod_chxj_config *dconf;
2348 if (strlen(arg) > 255)
2349 return "mod_chxj: ChxjForwardUrlBase is too long.";
2351 dconf = (mod_chxj_config *)mconfig;
2353 dconf->forward_url_base = apr_pstrdup(cmd->pool, arg);
2359 cmd_set_forward_server_ip(
2364 mod_chxj_config *dconf;
2366 if (strlen(arg) > 255)
2367 return "mod_chxj: ChxjForwardServerIp is too long.";
2369 dconf = (mod_chxj_config *)mconfig;
2371 dconf->forward_server_ip = apr_pstrdup(cmd->pool, arg);
2377 static const command_rec cmds[] = {
2379 "ChxjLoadDeviceData",
2380 cmd_load_device_data,
2383 "Load Device Data"),
2385 "ChxjLoadEmojiData",
2386 cmd_load_emoji_data,
2392 cmd_set_image_engine,
2395 "Convert Target URI"),
2397 "ChxjImageCacheDir",
2398 cmd_set_image_cache_dir,
2401 "Image Cache Directory"),
2403 "ChxjImageCacheLimit",
2404 cmd_set_image_cache_limit,
2407 "Image Cache Limit"),
2409 "ChxjImageCopyright",
2410 cmd_set_image_copyright,
2419 "an URL-applied regexp-pattern and a substitution URL"),
2425 "save cookie.db directory."),
2427 "ChxjCookieTimeout",
2428 cmd_set_cookie_timeout,
2431 "The compulsion time-out time of the cookie is specified. "),
2433 "ChxjCookieStoreType",
2434 cmd_set_cookie_store_type,
2437 "It specifies preserving of the cookie ahead. (DBM/MYSQL/MEMCACHE)"),
2439 "ChxjCookieLazyMode",
2440 cmd_set_cookie_lazy_mode,
2443 "OneTimeID is negligently done. (TRUE/FALSE)"),
2444 #if defined(USE_MYSQL_COOKIE)
2446 "ChxjCookieMysqlHost",
2447 cmd_set_cookie_mysql_host,
2450 "The MySQL database host used by saving Cookie"),
2452 "ChxjCookieMysqlPort",
2453 cmd_set_cookie_mysql_port,
2456 "The MySQL database port used by saving Cookie"),
2458 "ChxjCookieMysqlDatabase",
2459 cmd_set_cookie_mysql_database,
2462 "The MySQL database name used by saving Cookie"),
2464 "ChxjCookieMysqlUsername",
2465 cmd_set_cookie_mysql_username,
2468 "The MySQL username used by saving Cookie"),
2470 "ChxjCookieMysqlPassword",
2471 cmd_set_cookie_mysql_password,
2474 "The MySQL password used by saving Cookie"),
2476 "ChxjCookieMysqlTableName",
2477 cmd_set_cookie_mysql_table_name,
2480 "The MySQL table name used by saving Cookie"),
2482 "ChxjCookieMysqlSocketPath",
2483 cmd_set_cookie_mysql_socket_path,
2486 "The MySQL socket path used by saving Cookie"),
2488 "ChxjCookieMysqlCharset",
2489 cmd_set_cookie_mysql_charset,
2492 "The MySQL charset used by saving Cookie"),
2494 #if defined(USE_MEMCACHE_COOKIE)
2496 "ChxjCookieMemcacheHost",
2497 cmd_set_cookie_memcache_host,
2500 "The Memcached host used by saving Cookie"),
2502 "ChxjCookieMemcachePort",
2503 cmd_set_cookie_memcache_port,
2506 "The Memcached port used by saving Cookie"),
2509 "ChxjForwardUrlBase",
2510 cmd_set_forward_url_base,
2513 "The forward url base(default: {request protocol}://{this server}:{this server port}"),
2515 "ChxjForwardServerIp",
2516 cmd_set_forward_server_ip,
2519 "The forward server ip(default: this server ip)"),
2524 /*----------------------------------------------------------------------------*/
2525 /* Dispatch list for API hooks */
2526 /*----------------------------------------------------------------------------*/
2527 module AP_MODULE_DECLARE_DATA chxj_module = {
2528 STANDARD20_MODULE_STUFF,
2529 chxj_create_per_dir_config, /* create per-dir config structures */
2530 chxj_merge_per_dir_config, /* merge per-dir config structures */
2531 chxj_config_server_create, /* create per-server config structures */
2532 NULL, /* merge per-server config structures */
2533 cmds, /* table of config file commands */
2534 chxj_register_hooks /* register hooks */