]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/HTTPRequest.cxx
Don't use object returned from vector::end()
[simgear.git] / simgear / io / HTTPRequest.cxx
index b9466ebf36d11a3938365f9f035e7830a36add6c..6b06785ad00bf57d79fb12e77b971a9475362138 100644 (file)
@@ -32,6 +32,7 @@ extern const int DEFAULT_HTTP_PORT;
 
 //------------------------------------------------------------------------------
 Request::Request(const std::string& url, const std::string method):
+  _client(0),
   _method(method),
   _url(url),
   _responseVersion(HTTP_VERSION_UNKNOWN),
@@ -39,7 +40,8 @@ Request::Request(const std::string& url, const std::string method):
   _responseLength(0),
   _receivedBodyBytes(0),
   _ready_state(UNSENT),
-  _willClose(false)
+  _willClose(false),
+  _connectionCloseHeader(false)
 {
 
 }
@@ -133,19 +135,28 @@ void Request::responseStart(const std::string& r)
     const int maxSplit = 2; // HTTP/1.1 nnn reason-string
     string_list parts = strutils::split(r, NULL, maxSplit);
     if (parts.size() != 3) {
-        throw sg_io_exception("bad HTTP response");
+        throw sg_io_exception("bad HTTP response:" + r);
     }
-    
+
     _responseVersion = decodeHTTPVersion(parts[0]);
     _responseStatus = strutils::to_int(parts[1]);
     _responseReason = parts[2];
+
+    setReadyState(STATUS_RECEIVED);
 }
 
 //------------------------------------------------------------------------------
 void Request::responseHeader(const std::string& key, const std::string& value)
 {
-  if( key == "connection" )
-    _willClose = (value.find("close") != std::string::npos);
+  if( key == "connection" ) {
+    _connectionCloseHeader = (value.find("close") != std::string::npos);
+      // track willClose seperately because other conditions (abort, for
+      // example) can also set it
+    _willClose = _connectionCloseHeader;
+  } else if (key == "content-length") {
+    int sz = strutils::to_int(value);
+    setResponseLength(sz);
+  }
 
   _responseHeaders[key] = value;
 }
@@ -207,7 +218,7 @@ std::string Request::scheme() const
     if (firstColon > 0) {
         return url().substr(0, firstColon);
     }
-    
+
     return ""; // couldn't parse scheme
 }
 
@@ -219,20 +230,20 @@ std::string Request::path() const
     if (schemeEnd < 0) {
         return ""; // couldn't parse scheme
     }
-    
+
     int hostEnd = u.find('/', schemeEnd + 3);
     if (hostEnd < 0) {
-// couldn't parse host, or URL looks like 'http://foo.com' (no trailing '/') 
-// fixup to root resource path: '/' 
-        return "/"; 
+// 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);
     if (query < 0) {
         // all remainder of URL is path
         return u.substr(hostEnd);
     }
-    
+
     return u.substr(hostEnd, query - hostEnd);
 }
 
@@ -244,7 +255,7 @@ std::string Request::query() const
   if (query < 0) {
     return "";  //no query string found
   }
-  
+
   return u.substr(query);   //includes question mark
 }
 
@@ -319,6 +330,7 @@ unsigned int Request::responseLength() const
 //------------------------------------------------------------------------------
 void Request::setFailure(int code, const std::string& reason)
 {
+    SG_LOG(SG_IO, SG_WARN, "HTTP request: set failure:" << code << " reason " << reason);
   _responseStatus = code;
   _responseReason = reason;
 
@@ -354,23 +366,23 @@ void Request::setReadyState(ReadyState state)
 }
 
 //------------------------------------------------------------------------------
-void Request::abort()
+bool Request::closeAfterComplete() const
 {
-  abort("Request aborted.");
+  // for non HTTP/1.1 connections, assume server closes
+  return _willClose || (_responseVersion != HTTP_1_1);
 }
 
-//----------------------------------------------------------------------------
-void Request::abort(const std::string& reason)
+//------------------------------------------------------------------------------
+
+void Request::setCloseAfterComplete()
 {
-  setFailure(-1, reason);
-  _willClose = true;
+    _willClose = true;
 }
 
 //------------------------------------------------------------------------------
-bool Request::closeAfterComplete() const
+bool Request::serverSupportsPipelining() const
 {
-  // for non HTTP/1.1 connections, assume server closes
-  return _willClose || (_responseVersion != HTTP_1_1);
+    return (_responseVersion == HTTP_1_1) && !_connectionCloseHeader;
 }
 
 //------------------------------------------------------------------------------