OSDN Git Service

* version up for freeBSD
[modchxj/mod_chxj.git] / src / chxj_chtml10.c
index e604ae5..cc51397 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
+ * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,6 +23,8 @@
 #include "chxj_encoding.h"
 #include "chxj_buffered_write.h"
 #include "chxj_str_util.h"
+#include "chxj_header_inf.h"
+#include "chxj_conv_z2h.h"
 
 #define GET_CHTML10(X) ((chtml10_t *)(X))
 #undef W_L
@@ -288,6 +290,11 @@ tag_handler chtml10_handler[] = {
     s_chtml10_chxjif_tag,
     NULL,
   },
+  /* tagCHXJRAW */
+  {
+    s_chtml10_chxjif_tag,
+    NULL,
+  },
   /* tagNOBR */
   {
     NULL,
@@ -420,7 +427,7 @@ chxj_convert_chtml10(
   dst = NULL;
 
   t = apr_time_now();
-  DBG(r, "start chxj_convert_chtml10() cookie_id=[%s]", (cookie) ? cookie->cookie_id : "");
+  DBG(r, "REQ[%X] start %s() cookie_id=[%s]", TO_ADDR(r),__func__, (cookie) ? cookie->cookie_id : "");
 
   /*--------------------------------------------------------------------------*/
   /* If qrcode xml                                                            */
@@ -428,10 +435,11 @@ chxj_convert_chtml10(
   *dstlen = srclen;
   dst = chxj_qr_code_blob_handler(r, src, (size_t*)dstlen);
   if (dst) {
-    DBG(r,"i found qrcode xml");
+    DBG(r, "REQ[%X] found qrcode xml", TO_ADDR(r));
+    DBG(r, "REQ[%X] end %s()",TO_ADDR(r),__func__);
     return dst;
   }
-  DBG(r,"not found qrcode xml");
+  DBG(r,"REQ[%X] not found qrcode xml", TO_ADDR(r));
 
   /*--------------------------------------------------------------------------*/
   /* The CHTML structure is initialized.                                      */
@@ -440,7 +448,7 @@ chxj_convert_chtml10(
   chtml10.entryp    = entryp;
   chtml10.cookie    = cookie;
 
-  chxj_set_content_type(r, "text/html; charset=Windows-31J");
+  chxj_set_content_type(r, chxj_header_inf_set_content_type(r, "text/html; charset=Windows-31J"));
 
   /*--------------------------------------------------------------------------*/
   /* The character string of the input is analyzed.                           */
@@ -470,6 +478,7 @@ chxj_convert_chtml10(
   qs_all_free(&doc,QX_LOGMARK);
 
   if (!dst) {
+    DBG(r, "REQ[%X] end %s()",TO_ADDR(r),__func__);
     return apr_pstrdup(r->pool,ss);
   }
 
@@ -482,7 +491,8 @@ chxj_convert_chtml10(
   chxj_dump_out("[dst] CHTML -> CHTML1.0", dst, *dstlen);
 #endif
 
-  DBG(r, "end   chxj_convert_chtml10() cookie_id=[%s] time=[%" APR_TIME_T_FMT "]", (cookie) ? cookie->cookie_id : "", apr_time_now() - t);
+  DBG(r, "REQ[%X] cookie_id=[%s] time=[%" APR_TIME_T_FMT "]", TO_ADDR(r),(cookie) ? cookie->cookie_id : "", apr_time_now() - t);
+  DBG(r, "REQ[%X] end %s()",TO_ADDR(r),__func__);
 
   return dst;
 }
@@ -542,7 +552,7 @@ s_chtml10_search_emoji(chtml10_t *chtml10, char *txt, char **rslt)
   r = chtml10->doc->r;
 
   if (!spec) {
-    DBG(r,"spec is NULL");
+    DBG(r,"REQ[%X] spec is NULL", TO_ADDR(r));
   }
 
   for (ee = chtml10->conf->emoji;
@@ -550,7 +560,7 @@ s_chtml10_search_emoji(chtml10_t *chtml10, char *txt, char **rslt)
        ee = ee->next) {
 
     if (!ee->imode) {
-      DBG(r,"emoji->imode is NULL");
+      DBG(r,"REQ[%X] emoji->imode is NULL", TO_ADDR(r));
       continue;
     }
 
@@ -559,20 +569,77 @@ s_chtml10_search_emoji(chtml10_t *chtml10, char *txt, char **rslt)
     &&  strlen(ee->imode->string) > 0
     &&  *ee->imode->string == *txt
     &&  strncasecmp(ee->imode->string, txt, strlen(ee->imode->string)) == 0) {
-      if (!spec || !spec->emoji_type) {
-        *rslt = apr_palloc(r->pool, 3);
-        (*rslt)[0] = ee->imode->hex1byte & 0xff;
-        (*rslt)[1] = ee->imode->hex2byte & 0xff;
-        (*rslt)[2] = 0;
-        return strlen(ee->imode->string);
-      }
-
-      return 0;
+      *rslt = apr_palloc(r->pool, 3);
+      (*rslt)[0] = ee->imode->hex1byte & 0xff;
+      (*rslt)[1] = ee->imode->hex2byte & 0xff;
+      (*rslt)[2] = 0;
+      return strlen(ee->imode->string);
     }
   }
   return 0;
 }
 
+char *
+chxj_chtml10_emoji_only_converter(request_rec *r, device_table *spec, const char *src, apr_size_t len)
+{
+  apr_size_t ii;
+  Doc __doc;
+  Doc *doc;
+  chtml10_t __chtml10;
+  chtml10_t *chtml10;
+  char one_byte[2];
+  char two_byte[3];
+  apr_pool_t *pool;
+
+  chtml10 = &__chtml10;
+  doc     = &__doc;
+
+  DBG(r, "REQ[%X] start %s()", TO_ADDR(r),__func__);
+  memset(doc,     0, sizeof(Doc));
+  memset(chtml10, 0, sizeof(chtml10_t));
+
+  doc->r        = r;
+  chtml10->doc  = doc;
+  chtml10->spec = spec;
+  chtml10->out  = qs_alloc_zero_byte_string(r->pool);
+  chtml10->conf = chxj_get_module_config(r->per_dir_config, &chxj_module);
+  chtml10->doc->parse_mode = PARSE_MODE_CHTML;
+
+  apr_pool_create(&pool, r->pool);
+
+  chxj_buffered_write_init(pool, &doc->buf);
+
+  for (ii=0; ii<len; ii++) {
+    char *out;
+    int   rtn;
+
+    rtn = s_chtml10_search_emoji(chtml10, (char *)&src[ii], &out);
+    if (rtn) {
+      W_V(out);
+      ii+=(rtn - 1);
+      continue;
+    }
+  
+    if (is_sjis_kanji(src[ii])) {
+      two_byte[0] = src[ii+0];
+      two_byte[1] = src[ii+1];
+      two_byte[2] = 0;
+      W_V(two_byte);
+      ii++;
+    }
+    else {
+      one_byte[0] = src[ii+0];
+      one_byte[1] = 0;
+      W_V(one_byte);
+    }
+  }
+
+  chtml10->out = chxj_buffered_write_flush(chtml10->out, &doc->buf);
+
+  DBG(r, "REQ[%X] end %s()", TO_ADDR(r),__func__);
+  return chtml10->out;
+}
+
 
 /**
  * It is a handler who processes the HTML tag.
@@ -1501,7 +1568,9 @@ s_chtml10_start_a_tag(void *pdoc, Node *node)
         /* CHTML1.0                                                           */
         /*--------------------------------------------------------------------*/
         value = chxj_encoding_parameter(r, value);
-        value = chxj_add_cookie_parameter(r, value, chtml10->cookie);
+        if (! chxj_starts_with(value, "mailto:") && ! chxj_starts_with(value, "tel:")) {
+          value = chxj_add_cookie_parameter(r, value, chtml10->cookie);
+        }
         W_L(" href=\"");
         W_V(value);
         W_L("\"");
@@ -1800,12 +1869,13 @@ s_chtml10_start_form_tag(void *pdoc, Node *node)
   request_rec  *r;
   Attr         *attr;
   char         *new_hidden_tag = NULL;
+  char         *attr_value = NULL;
+  char         *attr_method = NULL;
 
   chtml10 = GET_CHTML10(pdoc);
   doc     = chtml10->doc;
   r       = doc->r;
 
-  W_L("<form");
 
   /*--------------------------------------------------------------------------*/
   /* Get Attributes                                                           */
@@ -1822,17 +1892,8 @@ s_chtml10_start_form_tag(void *pdoc, Node *node)
         /*--------------------------------------------------------------------*/
         /* CHTML 1.0                                                          */
         /*--------------------------------------------------------------------*/
-        value = chxj_encoding_parameter(r, value);
-        value = chxj_add_cookie_parameter(r, value, chtml10->cookie);
-        char *q;
-        q = strchr(value, '?');
-        if (q) {
-          new_hidden_tag = chxj_form_action_to_hidden_tag(doc->pool, value, 0);
-          *q = 0;
-        }
-        W_L(" action=\"");
-        W_V(value);
-        W_L("\"");
+        attr_value = chxj_encoding_parameter(r, value);
+        attr_value = chxj_add_cookie_parameter(r, attr_value, chtml10->cookie);
       }
       break;
 
@@ -1842,9 +1903,7 @@ s_chtml10_start_form_tag(void *pdoc, Node *node)
         /*--------------------------------------------------------------------*/
         /* CHTML 1.0                                                          */
         /*--------------------------------------------------------------------*/
-        W_L(" method=\"");
-        W_V(value);
-        W_L("\"");
+        attr_method = apr_pstrdup(doc->pool, value);
       }
       break;
 
@@ -1863,6 +1922,32 @@ s_chtml10_start_form_tag(void *pdoc, Node *node)
     }
   }
 
+  int post_flag = (attr_method && strcasecmp(attr_method, "post") == 0) ? 1 : 0;
+
+  W_L("<form");
+  if (attr_value) {
+    char *q;
+    char *new_query_string = NULL;
+    q = strchr(attr_value, '?');
+    if (q) {
+      new_hidden_tag = chxj_form_action_to_hidden_tag(r, doc->pool, attr_value, 0, post_flag, &new_query_string, CHXJ_TRUE, CHXJ_FALSE, chtml10->entryp);
+      if (new_hidden_tag || new_query_string) {
+        *q = 0;
+      }
+    }
+    W_L(" action=\"");
+    W_V(attr_value);
+    if (new_query_string) {
+      W_L("?");
+      W_V(new_query_string);
+    }
+    W_L("\"");
+  }
+  if (attr_method) {
+    W_L(" method=\"");
+    W_V(attr_method);
+    W_L("\"");
+  }
   W_L(">");
   if (new_hidden_tag) {
     W_V(new_hidden_tag);
@@ -1976,6 +2061,11 @@ s_chtml10_start_input_tag(void *pdoc, Node *node)
   }
 
   if (value && *value != 0) {
+    if (type && (STRCASEEQ('s','S',"submit",type) || STRCASEEQ('r','R',"reset",type))) {
+      apr_size_t value_len = strlen(value);
+      value = chxj_conv_z2h(r, value, &value_len, chtml10->entryp);
+    }
+
     W_L(" value=\"");
     W_V(chxj_add_slash_to_doublequote(doc->pool, value));
     W_L("\"");
@@ -2240,13 +2330,7 @@ s_chtml10_start_img_tag(void *pdoc, Node *node)
 #ifdef IMG_NOT_CONVERT_FILENAME
         value = chxj_encoding_parameter(r, value);
         value = chxj_add_cookie_parameter(r, value, chtml10->cookie);
-        if (value) {
-          value = apr_psprintf(doc->buf.pool, 
-                               "%s%c%s=true", 
-                               value, 
-                               (strchr(value, '?')) ? '&' : '?',
-                               CHXJ_COOKIE_NOUPDATE_PARAM);
-        }
+        value = chxj_add_cookie_no_update_parameter(r, value);
         W_L(" src=\"");
         W_V(value);
         W_L("\"");
@@ -2254,13 +2338,7 @@ s_chtml10_start_img_tag(void *pdoc, Node *node)
         value = chxj_img_conv(r, spec, value);
         value = chxj_encoding_parameter(r, value);
         value = chxj_add_cookie_parameter(r, value, chtml10->cookie);
-        if (value) {
-          value = apr_psprintf(doc->buf.pool,
-                               "%s%c%s=true",
-                               value,
-                               (strchr(value, '?')) ? '&' : '?',
-                               CHXJ_COOKIE_NOUPDATE_PARAM);
-        }
+        value = chxj_add_cookie_no_update_parameter(r, value);
         W_L(" src=\"");
         W_V(value);
         W_L("\"");
@@ -2553,7 +2631,7 @@ s_chtml10_start_option_tag(void *pdoc, Node *child)
     }
   }
 
-  if (value && *value != 0) {
+  if (value) {
     W_L(" value=\"");
     W_V(value);
     W_L("\"");
@@ -2929,6 +3007,7 @@ s_chtml10_text(void *pdoc, Node *child)
   chtml10_t   *chtml10;
   Doc         *doc;
   request_rec *r;
+  apr_size_t  z2h_input_len;
 
   chtml10 = GET_CHTML10(pdoc);
   doc     = chtml10->doc;
@@ -2981,6 +3060,9 @@ s_chtml10_text(void *pdoc, Node *child)
     }
   }
 
+  z2h_input_len = strlen(tdst);
+  tdst = chxj_conv_z2h(r, tdst, &z2h_input_len, chtml10->entryp);
+
   W_V(tdst);
   return chtml10->out;
 }