OSDN Git Service

query parameter fix.
authorvisor <visor@users.sourceforge.jp>
Sat, 25 May 2013 15:25:53 +0000 (00:25 +0900)
committervisor <visor@users.sourceforge.jp>
Sat, 25 May 2013 15:25:53 +0000 (00:25 +0900)
lib/http.cc
lib/http.h

index b447971..6b03f65 100644 (file)
@@ -315,12 +315,45 @@ void  HTTPResponse::printMoreHeader (MotorOutput* out, MotorEnv* env) {
 }
 
 #if 0
-void  HTTPSend::setMethod (const ustring& _method, bool _fpost) {
-    static uregex  re ("^(GET|HEAD|POST|OPTIONS|PUT|DELETE|TRACE|PATCH|LINK|UNLINK)$");
-    if (checkRe (_method, re)) {
-       method = _method;
-       fpost = _fpost;
+ustring  HTTPQuery::formString (HTTPSend* o) {
+    MNode*  e;
+    MNode*  a;
+    int  c = 0;
+    ustring  ans;
+
+    e = params ();
+    if (e && e->isCons ()) {
+       if (o) {
+           while (e) {
+               if (a = e->car ()) {
+                   if (c > 0)
+                       ans.append (uAmp);
+                   ans.append (percentEncode (o->cv (to_string (a->car ()))));
+                   if (! isNil (a->cdr ())) {
+                       ans.append (uEq);
+                       ans.append (percentEncode (o->cv (to_string (a->cdr ()))));
+                   }
+                   c ++;
+               }
+               nextNode (e);
+           }
+       } else {
+           while (e) {
+               if (a = e->car ()) {
+                   if (c > 0)
+                       ans.append (uAmp);
+                   ans.append (percentEncode (to_string (a->car ())));
+                   if (! isNil (a->cdr ())) {
+                       ans.append (uEq);
+                       ans.append (percentEncode (to_string (a->cdr ())));
+                   }
+                   c ++;
+               }
+               nextNode (e);
+           }
+       }
     }
+    return ans;
 }
 #endif
 
@@ -461,8 +494,6 @@ ustring  HTTPSend::query (MlEnv* mlenv) {
        assert (0);
     }
     q.append (uSPC).append (path);
-//    if (fpost) {
-//    if (method == M_POST) {
     switch (method) {
     case M_POST:
        if (formData)
@@ -470,21 +501,25 @@ ustring  HTTPSend::query (MlEnv* mlenv) {
        if (getparams ()) {
            q.append (CharConst ("?"));
            appendQueryForm (getparams (), q);
+//         q.append (getparams.formString (this));
        }
        break;
     case M_PUT:
        if (getparams ()) {
            q.append (CharConst ("?"));
            appendQueryForm (getparams (), q);
+//         q.append (getparams.formString (this));
        }
        break;
     default:
        if (getparams ()) {
            q.append (CharConst ("?"));
            appendQueryForm (getparams (), q);
+//         q.append (getparams.formString (this));
        } else if (params ()) {
            q.append (CharConst ("?"));
            appendQueryForm (params (), q);
+//         q.append (params.formString (this));
        }
     }
     q.append (CharConst (" HTTP/1.0" kCRLF));
@@ -508,8 +543,6 @@ ustring  HTTPSend::query (MlEnv* mlenv) {
     for (std::vector<mapelem>::iterator i = header_req.begin (); i < header_req.end (); i ++) {
        makeHeader (q, (*i).first, (*i).second);
     }
-//    if (fpost)
-//    if (method == M_POST)
     switch (method) {
     case M_POST:
        if (formData) {
@@ -555,9 +588,10 @@ void  HTTPSend::writeQueryForm (MNode* e, TcpClient& out) {
                if (c > 0)
                    u.append (uAmp);
                u.append (percentEncode (cv (to_string (a->car ()))));
-               u.append (uEq);
-               out.write (&*u.begin (), u.length ());
-               u.assign (percentEncode (cv (to_string (a->cdr ()))));
+               if (! isNil (a->cdr ())) {
+                   u.append (uEq);
+                   u.append (percentEncode (cv (to_string (a->cdr ()))));
+               }
                out.write (&*u.begin (), u.length ());
                c ++;
            }
@@ -576,8 +610,10 @@ void  HTTPSend::appendQueryForm (MNode* e, ustring& out) {
                if (c > 0)
                    out.append (uAmp);
                out.append (percentEncode (cv (to_string (a->car ()))));
-               out.append (uEq);
-               out.append (percentEncode (cv (to_string (a->cdr ()))));
+               if (! isNil (a->cdr ())) {
+                   out.append (uEq);
+                   out.append (percentEncode (cv (to_string (a->cdr ()))));
+               }
                c ++;
            }
            nextNode (e);
@@ -593,14 +629,13 @@ size_t  HTTPSend::queryFormSize (MNode* e) {
     if (e && e->isCons ()) {
        while (e) {
            if (a = e->car ()) {
-               ustring  u;
                if (c > 0)
-                   u.append (uAmp);
-               u.append (percentEncode (cv (to_string (a->car ()))));
-               u.append (uEq);
-               ans += u.length ();
-               u.assign (percentEncode (cv (to_string (a->cdr ()))));
-               ans += u.length ();
+                   ans += uAmp.length ();
+               ans += percentEncode (cv (to_string (a->car ()))).length ();
+               if (! isNil (a->cdr ())) {
+                   ans += uEq.length ();
+                   ans += percentEncode (cv (to_string (a->cdr ()))).length ();
+               }
                c ++;
            }
            nextNode (e);
index a9d43f7..a4cfa11 100644 (file)
@@ -63,6 +63,24 @@ class  CookieInfo {
     virtual  ~CookieInfo () {};
 };
 
+#if 0
+class  HTTPSend;
+class  HTTPQuery {
+ public:
+    MNodePtr  params;
+
+    HTTPQuery () {};
+    virtual  ~HTTPQuery () {};
+
+    virtual void  assign (MNode* p) {
+       params = p;
+    };
+    virtual ustring  formString (HTTPSend* o = NULL);
+    virtual void  writeForm (TcpClient& out, HTTPSend* o = NULL);
+    virtual void  formSize (HTTPSend* o = NULL);
+};
+#endif
+
 class  HTTPSend {
  public:
     typedef  std::pair<ustring,ustring>  mapelem;
@@ -86,6 +104,8 @@ class  HTTPSend {
     ustring  path;
     MNodePtr  params;
     MNodePtr  getparams;
+//    HTTPQuery  params;
+//    HTTPQuery  getparams;
     ustring  rawquery;
     ustring  rawqueryfile;
     ustring  querytype;