]> git.mxchange.org Git - simgear.git/commitdiff
Tweak HTTP handling for POST requests.
authorJames Turner <zakalawe@mac.com>
Sat, 17 Nov 2012 18:08:38 +0000 (18:08 +0000)
committerJames Turner <zakalawe@mac.com>
Sat, 17 Nov 2012 18:08:38 +0000 (18:08 +0000)
Tolerate slightly malformed request URLs (no slash following the host specified), and use a better check when POST-ing to decide the request-body data source.

simgear/io/HTTPClient.cxx
simgear/io/HTTPRequest.cxx

index 31ea555a9738ab6a5f373b88176da71a82e0d949..2eff7439ba3e1adaada1edb1c40c2012a89822a5 100644 (file)
@@ -130,7 +130,7 @@ public:
     {
       assert(!sentRequests.empty());
       
-      activeRequest = sentRequests.front();
+      activeRequest = sentRequests.front();      
       activeRequest->responseStart(buffer);
       state = STATE_GETTING_HEADERS;
       buffer.clear();
@@ -169,9 +169,10 @@ public:
      
       Request_ptr r = queuedRequests.front();
       requestBodyBytesToSend = r->requestBodyLength();
-    
+          
       stringstream headerData;
       string path = r->path();
+      assert(!path.empty());
       string query = r->query();
       string bodyData;
       
@@ -179,7 +180,7 @@ public:
           path = r->scheme() + "://" + r->host() + r->path();
       }
 
-      if (r->method() == "POST") {
+      if (r->requestBodyType() == CONTENT_TYPE_URL_ENCODED) {
           headerData << r->method() << " " << path << " HTTP/1.1\r\n";
           bodyData = query.substr(1); // URL-encode, drop the leading '?'
           headerData << "Content-Type:" << CONTENT_TYPE_URL_ENCODED << "\r\n";
@@ -429,7 +430,7 @@ public:
 private:
     bool connectToHost()
     {
-        SG_LOG(SG_IO, SG_INFO, "HTTP connecting to " << host << ":" << port);
+        SG_LOG(SG_IO, SG_DEBUG, "HTTP connecting to " << host << ":" << port);
         
         if (!open()) {
             SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed");
@@ -657,7 +658,6 @@ void Client::update(int waitTimeout)
             _connections.erase(del);
         } else {
             if (it->second->shouldStartNext()) {
-                SG_LOG(SG_IO, SG_INFO, "should start next, hmm");
                 it->second->tryStartNextRequest();
             }
             
index 0e789e5ce3920c9ddbed5ac48afa82b75689981b..ce706957b6f45469b4213b9f2c6244f462830fc9 100644 (file)
@@ -112,8 +112,10 @@ string Request::path() const
     }
     
     int hostEnd = u.find('/', schemeEnd + 3);
-    if (hostEnd < 0) { 
-        return ""; // couldn't parse host
+    if (hostEnd < 0) {
+// couldn't parse host, or URL looks like 'http://foo.com' (no trailing '/') 
+// fixup to root resource path: '/' 
+        return "/"; 
     }
     
     int query = u.find('?', hostEnd + 1);
@@ -203,6 +205,7 @@ void Request::setFailure(int code, const std::string& reason)
 void Request::failed()
 {
     // no-op in base class
+    SG_LOG(SG_IO, SG_INFO, "request failed:" << url());
 }
 
 Request::HTTPVersion Request::decodeVersion(const string& v)