OSDN Git Service

* merge from 0.8.x branch.
authorkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Fri, 8 Feb 2008 16:24:26 +0000 (16:24 +0000)
committerkonn <konn@1a406e8e-add9-4483-a2c8-d8cac5b7c224>
Fri, 8 Feb 2008 16:24:26 +0000 (16:24 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/modchxj/mod_chxj/trunk@1842 1a406e8e-add9-4483-a2c8-d8cac5b7c224

include/chxj_preg_replace.h [new file with mode: 0644]
src/Makefile.am
src/chxj_img_conv_format.c
src/chxj_preg_replace.c [new file with mode: 0644]

diff --git a/include/chxj_preg_replace.h b/include/chxj_preg_replace.h
new file mode 100644 (file)
index 0000000..eb69f99
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
+ * Copyright (C) 2005 QSDN,Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __CHXJ_PREG_REPLACE_H__
+#define __CHXJ_PREG_REPLACE_H__
+
+#include "mod_chxj.h"
+
+extern ap_regex_t *chxj_compile_for_preg_replace(apr_pool_t *p, const char *pattern);
+extern char *chxj_preg_str_replace(apr_pool_t *p, const char *pattern, const char *replacement, const char *str);
+extern char *chxj_preg_str_replace_all(apr_pool_t *p, const char *pattern, const char *replacement, const char *str);
+extern char *chxj_preg_replace(apr_pool_t *p, ap_regex_t *regexp, const char *replacement, const char *str);
+extern char *chxj_preg_replace_all(apr_pool_t *p, ap_regex_t *regexp, const char *replacement, const char *str);
+
+#endif
index 8c33dca..2e7c2b9 100644 (file)
@@ -29,6 +29,7 @@ libmod_chxj_la_SOURCES = mod_chxj.c \
     chxj_node_exchange.c \
     chxj_url_encode.c \
     chxj_cookie.c \
+    chxj_preg_replace.c \
     chxj_emoji.c
 
 
index ddfa590..85010db 100644 (file)
@@ -23,6 +23,7 @@
 #include "chxj_apply_convrule.h"
 #include "chxj_url_encode.h"
 #include "qs_parse_string.h"
+#include "chxj_preg_replace.h"
 
 #include <limits.h>
 
@@ -161,10 +162,15 @@ static const char* HDML_FAIL_PAGE =
   "  </DISPLAY>\r\n"
   "<HDML>\r\n";
 
+static ap_regex_t *v_docomo_serial_pattern1 = NULL;
+static ap_regex_t *v_docomo_serial_pattern2 = NULL;
+static ap_regex_t *v_docomo_serial_pattern3 = NULL;
+static ap_regex_t *v_softbank_serial_pattern1 = NULL;
+
 /*----------------------------------------------------------------------------*/
 /* Prototype declaration                                                      */
 /*----------------------------------------------------------------------------*/
-static char *s_create_workfile(request_rec *, 
+static char *s_create_workfile_name(request_rec *, 
                                mod_chxj_config *, 
                                const char *,
                                query_string_param_t *);
@@ -389,7 +395,7 @@ s_img_conv_format_from_file(
   /*--------------------------------------------------------------------------*/
   /* Create Workfile Name                                                     */
   /*--------------------------------------------------------------------------*/
-  tmpfile = s_create_workfile(r, conf, user_agent, qsp);
+  tmpfile = s_create_workfile_name(r, conf, user_agent, qsp);
   DBG(r,"workfile=[%s]", tmpfile);
 
   rv = apr_stat(&st, r->filename, APR_FINFO_MIN, r->pool);
@@ -1685,8 +1691,25 @@ s_header_only_cache_file(
 }
 
 
+static void
+s_init_serial_pattern(apr_pool_t *p)
+{
+  if (!v_docomo_serial_pattern1) {
+    v_docomo_serial_pattern1 = chxj_compile_for_preg_replace(p, "/ser[^;\\)]+");
+  }
+  if (!v_docomo_serial_pattern2) {
+    v_docomo_serial_pattern2 = chxj_compile_for_preg_replace(p, ";ser[^;\\)]+");
+  }
+  if (!v_docomo_serial_pattern3) {
+    v_docomo_serial_pattern3 = chxj_compile_for_preg_replace(p, ";icc[^;\\)]+");
+  }
+  if (!v_softbank_serial_pattern1) {
+    v_softbank_serial_pattern1 = chxj_compile_for_preg_replace(p, "/SN[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z] ");
+  }
+}
+
 static char *
-s_create_workfile(
+s_create_workfile_name(
   request_rec *r, 
   mod_chxj_config *conf, 
   const char *user_agent, 
@@ -1697,6 +1720,17 @@ s_create_workfile(
   int len;
   char *w = apr_palloc(r->pool, 256);
   char *fname;
+  char *new_user_agent;
+
+  s_init_serial_pattern(r->pool);
+
+  /* for DoCoMo */
+  new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern1, "", user_agent);
+  new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern2, "", new_user_agent);
+  new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern3, "", new_user_agent);
+
+  /* for SoftBank */
+  new_user_agent = chxj_preg_replace(r->pool, v_softbank_serial_pattern1, " ", new_user_agent);
 
   memset(w, 0, 256);
   switch (qsp->mode) {
diff --git a/src/chxj_preg_replace.c b/src/chxj_preg_replace.c
new file mode 100644 (file)
index 0000000..73a674f
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
+ * Copyright (C) 2005 QSDN,Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <apr.h>
+#include <ap_config.h>
+#include <ap_regex.h>
+#include <apr_strings.h>
+#include <httpd.h>
+
+#include "chxj_preg_replace.h"
+
+static inline char *
+s_init_pattern(apr_pool_t *p, const char *old)
+{
+  return apr_psprintf(p, "^(.*)(%s)(.*)$", old);
+}
+
+
+static inline ap_regex_t *
+s_compile_regex(apr_pool_t *p, const char *pattern)
+{
+  char *new_pat;
+  new_pat = s_init_pattern(p, pattern);
+  return ap_pregcomp(p, new_pat, AP_REG_EXTENDED|AP_REG_ICASE);
+}
+
+ap_regex_t *
+chxj_compile_for_preg_replace(apr_pool_t *p, const char *pattern)
+{
+  return s_compile_regex(p, pattern);
+}
+
+
+static inline char *
+s_one_time_replace(apr_pool_t *p, ap_regex_t *regexp, const char *replacement, const char *str)
+{
+  ap_regmatch_t match[10];
+
+  if (ap_regexec(regexp, str, regexp->re_nsub + 1, match, 0) == 0) {
+    /* Match */
+    char *one = ap_pregsub(p, "$1", str, regexp->re_nsub + 1, match);
+    char *three = ap_pregsub(p, "$3", str, regexp->re_nsub + 1, match);
+    if (strlen(replacement)) {
+      return apr_pstrcat(p, one, replacement, three, NULL);
+    }
+    return apr_pstrcat(p, one, three, NULL);
+  }
+  /* Not Match */
+  return NULL;
+}
+
+
+char *
+chxj_preg_str_replace(apr_pool_t *p, const char *pattern, const char *replacement, const char *str)
+{
+  return chxj_preg_replace(p, s_compile_regex(p, pattern), replacement, str);
+}
+
+
+char *
+chxj_preg_str_replace_all(apr_pool_t *p, const char *pattern, const char *replacement, const char *str)
+{
+  return chxj_preg_replace_all(p, s_compile_regex(p, pattern), replacement, str);
+}
+
+
+char *
+chxj_preg_replace(apr_pool_t *p, ap_regex_t *regexp, const char *replacement, const char *str)
+{
+  char *result;
+
+  result = s_one_time_replace(p, regexp, replacement, str);
+  if (result == NULL)
+    return apr_pstrdup(p, str);
+
+  return result;
+}
+
+
+char *
+chxj_preg_replace_all(apr_pool_t *p, ap_regex_t *regexp, const char *replacement, const char *str)
+{
+  char *result;
+  char *pre_result;
+
+  result = apr_pstrdup(p, str);
+  for (;;) {
+    pre_result = s_one_time_replace(p, regexp, replacement, result);
+    if (pre_result == NULL)
+      break;
+    result = pre_result;
+  }
+
+  return result;
+}
+
+