OSDN Git Service

change the urlencoding method.
authorvisor <visor@users.sourceforge.jp>
Sat, 16 Nov 2013 06:12:27 +0000 (15:12 +0900)
committervisor <visor@users.sourceforge.jp>
Sat, 16 Nov 2013 06:12:27 +0000 (15:12 +0900)
config.h
lib/util_string.cc
lib/util_url.cc
lib/util_url.h
modules/ml-http.cc

index 4035ea9..f4dc7a4 100644 (file)
--- a/config.h
+++ b/config.h
@@ -28,6 +28,7 @@
 //#define  BOOTSTRAPHACK               1
 //#define  COMPAT_STORAGE_CREATE       1
 //#define  HTTPS_NOCACHE       1
+//#define  QUERYENCODEALT              1
 #define  STANDALONE            1
 
 #endif /* CONFIG_H */
index 5baf957..7bbdaca 100644 (file)
@@ -268,10 +268,21 @@ ustring  percentEncode (uiterator b, uiterator e) {
     return percentEncode (b, e, re);
 }
 
+ustring  percentEncode_path (uiterator b, uiterator e) {
+    static uregex  re ("(\\x00)|([^A-Za-z0-9_/.~-])");
+
+    return percentEncode (b, e, re);
+}
+
 ustring  percentEncode (const ustring& str) {
     return percentEncode (str.begin (), str.end ());
 }
 
+ustring  percentEncode_path (const ustring& str) {
+    return percentEncode_path (str.begin (), str.end ());
+}
+
+#if 0
 ustring  percentEncode_path (uiterator b, uiterator e) {
     uiterator  i;
     ustring  ans;
@@ -293,6 +304,7 @@ ustring  percentEncode_path (uiterator b, uiterator e) {
 ustring  percentEncode_path (const ustring& str) {
     return percentEncode_path (str.begin (), str.end ());
 }
+#endif
 
 ustring  percentDecode (const ustring& str) {
     ustring  ans;
index 8f8157d..687b9dc 100644 (file)
@@ -1,3 +1,4 @@
+#include "config.h"
 #include "util_url.h"
 #include "util_const.h"
 #include "util_string.h"
@@ -125,9 +126,11 @@ bool  checkScheme (ustring& proto) {
     return  usearch (proto, m, re);
 }
 
+#ifdef QUERYENCODEALT
 static ustring  urlencode (uiterator b, uiterator e, const uregex& re) {
     umatch  m;
     ustring  ans;
+    int  c;
 
     while (b < e && usearch (b, e, m, re)) {
        while (b < m[0].first) {
@@ -140,10 +143,11 @@ static ustring  urlencode (uiterator b, uiterator e, const uregex& re) {
        b = m[0].second;
     }
     while (b < e) {
-       if (*b == 0)
+       c = *b ++;
+       if (c == 0)
            ans.append (uUScore);
        else
-           ans.append (percentHEX (*b++));
+           ans.append (percentHEX (c));
     }
 
     return ans;
@@ -158,6 +162,7 @@ ustring  urlencode_path (uiterator b, uiterator e) {
     static uregex  re ("[" CUNRESERVED "/" MSB1CHAR "]+");
     return urlencode (b, e, re);
 }
+#endif
 
 ustring  buildQuery (MNode* query) {
     int  c = 0;
@@ -173,10 +178,18 @@ ustring  buildQuery (MNode* query) {
                ans.append (CharConst ("&"));
            c ++;
            u = to_string (a->car ());
+#ifdef QUERYENCODEALT
            ans.append (urlencode (u.begin (), u.end ()));
+#else
+           ans.append (percentEncode (u.begin (), u.end ()));
+#endif
            ans.append (uEq);
            u = to_string (a->cdr ());
+#ifdef QUERYENCODEALT
            ans.append (urlencode (u.begin (), u.end ()));
+#else
+           ans.append (percentEncode (u.begin (), u.end ()));
+#endif
        } else {
            throw (to_string (a) + uErrorBadValue);
        }
index c680dd3..c915aaa 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef UTIL_URL_H
 #define UTIL_URL_H
 
+#include "config.h"
 #include "ustring.h"
 
 class MNode;
@@ -8,8 +9,10 @@ class MNode;
 bool  checkURL (ustring& url, upair* scheme, upair* delim, upair* user, upair* pass, upair* host, upair* port, upair* rest);
 bool  checkURLSafe (ustring& url);
 bool  checkScheme (ustring& proto);
+#ifdef QUERYENCODEALT
 ustring  urlencode (uiterator b, uiterator e);
 ustring  urlencode_path (uiterator b, uiterator e);
+#endif
 ustring  buildQuery (MNode* query);
 
 #endif /* UTIL_URL_H */
index ebceabb..392dc42 100644 (file)
@@ -1,3 +1,4 @@
+#include "config.h"
 #include "ml-http.h"
 #include "ml.h"
 #include "mlenv.h"
@@ -29,9 +30,17 @@ static void  location_sub (ustring& url, MNode* query, MlEnv* mlenv) {
        if (port.first != port.second) {
            ans.append (uColon).append (port.first, port.second);
        }
+#ifdef QUERYENCODEALT
        ans.append (urlencode_path (rest.first, rest.second));
+#else
+       ans.append (percentEncode_path (rest.first, rest.second));
+#endif
     } else {
+#ifdef QUERYENCODEALT
        ans.assign (urlencode_path (url.begin (), url.end ()));
+#else
+       ans.assign (percentEncode_path (url.begin (), url.end ()));
+#endif
     }
     if (query)
        ans.append (buildQuery (query));
@@ -382,12 +391,20 @@ MNode*  ml_build_url (MNode* cell, MlEnv* mlenv) {
     if (user ()) {
        u = to_string (user ());
        omitCtrl (u);
+#ifdef QUERYENCODEALT
        ans ()->append (urlencode (u.begin (), u.end ()));
+#else
+       ans ()->append (percentEncode (u.begin (), u.end ()));
+#endif
        if (pwd ()) {
            ans ()->append (uColon);
            u = to_string (pwd ());
            omitCtrl (u);
+#ifdef QUERYENCODEALT
            ans ()->append (urlencode (u.begin (), u.end ()));
+#else
+           ans ()->append (percentEncode (u.begin (), u.end ()));
+#endif
        }
        ans ()->append (CharConst ("@"));
     }
@@ -398,7 +415,11 @@ MNode*  ml_build_url (MNode* cell, MlEnv* mlenv) {
     if (path[0] != '/') {
        ans ()->append (uSlash);
     }
+#ifdef QUERYENCODEALT
     ans ()->append (urlencode_path (path.begin (), path.end ()));
+#else
+    ans ()->append (percentEncode_path (path.begin (), path.end ()));
+#endif
     if (query ()) {
        ans ()->append (buildQuery (query ()));
     }
@@ -425,10 +446,18 @@ MNode*  ml_build_url_path (MNode* cell, MlEnv* mlenv) {
        if (! isNil (t ())) {
            u = to_string (t ());
            if (c == 0) {
+#ifdef QUERYENCODEALT
                ans ()->append (urlencode (u.begin (), u.end ()));
+#else
+               ans ()->append (percentEncode (u.begin (), u.end ()));
+#endif
                c ++;
            } else {
+#ifdef QUERYENCODEALT
                ans ()->append (uSlash).append (urlencode (u.begin (), u.end ()));
+#else
+               ans ()->append (uSlash).append (percentEncode (u.begin (), u.end ()));
+#endif
            }
        }
        nextNode (arg);