OSDN Git Service

fix http-get function bug.
authorvisor <visor@users.sourceforge.jp>
Tue, 24 May 2011 15:48:03 +0000 (00:48 +0900)
committervisor <visor@users.sourceforge.jp>
Tue, 24 May 2011 15:48:03 +0000 (00:48 +0900)
lib/http.cc
lib/http.h
modules/ml-http.cc

index 0bab613..a273e8b 100644 (file)
@@ -328,8 +328,8 @@ int  HTTPSend::post (TcpClient& client, TcpBuf& buf, ustring& ans) {
 }
 
 void  HTTPSend::makeHeader (ustring& q, const ustring& key, const ustring& val) {
-    static uregex  re1 ("[^a-zA-Z_0-9]");
-    static uregex  re2 ("[\\x00-\\x1f;]");
+    static uregex  re1 ("[\\x00-\\x1f: ]");
+    static uregex  re2 ("[\\x00-\\x1f]");
 
     if (! checkRe (key, re1) && ! checkRe (val, re2) && key.length () + val.length () < 2048) {
        q.append (key).append (CharConst (": ")).append (val).append (uCRLF);
@@ -341,10 +341,15 @@ ustring  HTTPSend::query () {
 
     if (fpost) {
        q.assign (CharConst ("POST ")).append (path);
+       if (getparams.length () > 0) {
+           q.append (CharConst ("?")).append (getparams);
+       }
        q.append (CharConst (" HTTP/1.0\r\n"));
     } else {
        q.assign (CharConst ("GET ")).append (path);
-       if (params.length () > 0) {
+       if (getparams.length () > 0) {
+           q.append (CharConst ("?")).append (getparams);
+       } else if (params.length () > 0) {
            q.append (CharConst ("?")).append (params);
        }
        q.append (CharConst (" HTTP/1.0\r\n"));
@@ -402,6 +407,7 @@ int  HTTPSend::readReplyHead (TcpClient& client, TcpBuf& buf) {
     while (rc = buf.getln (client, line)) {
        if (line.empty ())
            break;
+       line = omitCtrl (fixUTF8 (line));
        b = line.begin ();
        e = line.end ();
        if (splitChar (b, e, ':', m1)) {
@@ -437,7 +443,7 @@ void  HTTPSend::readReplyBody (TcpClient& client, TcpBuf& buf, ustring& ans) {
     std::stringstream  ostr;
     MotorOutputOStream  out (&ostr);
     readReplyBody (client, buf, &out);
-    ans = ostr.str ();
+    ans = fixUTF8 (ostr.str ());
 }
 
 const ustring*  HTTPSend::findHeader (const ustring& name) {
index dc3b888..3f1e5d7 100644 (file)
@@ -70,6 +70,7 @@ class  HTTPSend {
     ustring  reqhost;
     ustring  path;
     ustring  params;
+    ustring  getparams;
     ustring  id;
     ustring  pw;
     ustring  cookie;
index a62cb5f..0626c74 100644 (file)
@@ -98,21 +98,27 @@ static void  url_sub (const ustring& url, HTTPSend& http) {
     }
 }
 
-static void  request_query (MNode* query, HTTPSend& http) {
+static void  request_query (MNode* query, HTTPSend& http, bool getmethod = false) {
     MNode*  a;
+    ustring* pp;
 
+    if (getmethod) {
+       pp = &http.getparams;
+    } else {
+       pp = &http.params;
+    }
     if (query && query->isCons ()) {
        while (query) {
            if (a = query->car ()) {
-               if (http.params.size () > 0)
-                   http.params.append (uAmp);
+               if (pp->size () > 0)
+                   pp->append (uAmp);
                if (a->isCons ()) {
-                   http.params.append (urlencode (to_string (a->car ()))).append (uEq).append (urlencode (to_string (a->cdr ())));
+                   pp->append (urlencode (to_string (a->car ()))).append (uEq).append (urlencode (to_string (a->cdr ())));
                } else {
-                   http.params.append (urlencode (to_string (a))).append (uEq);
+                   pp->append (urlencode (to_string (a))).append (uEq);
                    nextNode (query);
                    if (query)
-                       http.params.append (urlencode (to_string (query->car ())));
+                       pp->append (urlencode (to_string (query->car ())));
                }
            }
            nextNode (query);
@@ -319,6 +325,7 @@ MNode*  ml_http_get (MNode* cell, MlEnv* mlenv) {
     MLHttpGet  obj (mlenv);
     ustring  url;
     MNodePtr  query;
+    MNodePtr  getquery;
     MNodePtr  cookie;
     MNodePtr  headerquery;
 
@@ -339,6 +346,7 @@ MNode*  ml_http_get (MNode* cell, MlEnv* mlenv) {
        {CharConst ("proxy-password"), false},
        {CharConst ("proxy-pw"), false},
        {CharConst ("header"), false},
+       {CharConst ("get-query"), false},
        {NULL, 0, 0}
     };
 
@@ -376,12 +384,16 @@ MNode*  ml_http_get (MNode* cell, MlEnv* mlenv) {
     if (keywords[11]) {                // header
        headerquery = eval (keywords[11], mlenv);
     }
+    if (keywords[12]) {                // get-query
+       getquery = eval (keywords[12], mlenv);
+    }
     
     url_sub (url, obj.http);
     if (obj.http.proto.empty ()) {
        throw (obj.http.path + ": bad URL.");
     }
     request_query (query (), obj.http);
+    request_query (getquery (), obj.http, true);
     request_cookie (cookie (), obj.http);
     request_headerquery (headerquery (), obj.http);