]> git.mxchange.org Git - flightgear.git/commitdiff
Support request body on xmlhttprequest.
authorJames Turner <zakalawe@mac.com>
Sat, 17 Nov 2012 18:10:15 +0000 (18:10 +0000)
committerJames Turner <zakalawe@mac.com>
Sat, 17 Nov 2012 18:10:15 +0000 (18:10 +0000)
Allow XML property data to be submitted with a request; changes the request method from GET to POST. Supply nodes via the 'body' argument to the command.

src/Main/fg_commands.cxx

index ac17b7eaaa4c3a55816e7b7a5e07046ba5926f15..21657fbc729e85f2af864b9ace420f940da3bcec 100644 (file)
@@ -1280,10 +1280,15 @@ public:
     SGPropertyNode_ptr _failed;
     SGPropertyNode_ptr _target;
     string propsData;
+    mutable string _requestBody;
+    int _requestBodyLength;
+    string _method;
     
-    RemoteXMLRequest(const std::string& url, SGPropertyNode* targetNode) : 
+    RemoteXMLRequest(const std::string& url, SGPropertyNode* targetNode) :
         simgear::HTTP::Request(url),
-        _target(targetNode)
+        _target(targetNode),
+        _requestBodyLength(-1),
+        _method("GET")
     {
     }
     
@@ -1301,7 +1306,39 @@ public:
     {
         _failed = p;
     }
+  
+    void setRequestData(const SGPropertyNode* body)
+    {
+        _method = "POST";
+        std::stringstream buf;
+        writeProperties(buf, body, true);
+        _requestBody = buf.str();
+        _requestBodyLength = _requestBody.size();
+    }
+    
+    virtual std::string method() const
+    {
+        return _method;
+    }
 protected:
+    virtual int requestBodyLength() const
+    {
+        return _requestBodyLength;
+    }
+    
+    virtual void getBodyData(char* s, int& count) const
+    {
+        int toRead = std::min(count, (int) _requestBody.size());
+        memcpy(s, _requestBody.c_str(), toRead);
+        count = toRead;
+        _requestBody = _requestBody.substr(count);
+    }
+    
+    virtual std::string requestBodyType() const
+    {
+        return "application/xml";
+    }
+    
     virtual void gotBodyData(const char* s, int n)
     {
         propsData += string(s, n);
@@ -1309,6 +1346,8 @@ protected:
     
     virtual void responseComplete()
     {
+        simgear::HTTP::Request::responseComplete();
+        
         int response = responseCode();
         bool failed = false;
         if (response == 200) {
@@ -1346,6 +1385,9 @@ do_load_xml_from_url(const SGPropertyNode * arg)
     
     RemoteXMLRequest* req = new RemoteXMLRequest(url, targetnode);
     
+    if (arg->hasChild("body"))
+        req->setRequestData(arg->getChild("body"));
+    
 // connect up optional reporting properties
     if (arg->hasValue("complete")) 
         req->setCompletionProp(fgGetNode(arg->getStringValue("complete"), true));