]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/HTTPRequest.cxx
Ensure individual log-level setting works.
[simgear.git] / simgear / io / HTTPRequest.cxx
index 84a19c99682e427394bbe0f3f7e96ec3b660a7fe..ce706957b6f45469b4213b9f2c6244f462830fc9 100644 (file)
@@ -18,9 +18,11 @@ extern const int DEFAULT_HTTP_PORT;
 Request::Request(const string& url, const string method) :
     _method(method),
     _url(url),
+    _responseVersion(HTTP_VERSION_UNKNOWN),
     _responseStatus(0),
     _responseLength(0),
-    _receivedBodyBytes(0)
+    _receivedBodyBytes(0),
+    _willClose(false)
 {
     
 }
@@ -52,17 +54,21 @@ void Request::responseStart(const string& r)
     string_list parts = strutils::split(r, NULL, maxSplit);
     if (parts.size() != 3) {
         SG_LOG(SG_IO, SG_WARN, "HTTP::Request: malformed response start:" << r);
-        _responseStatus = 400;
-        _responseReason = "bad HTTP response header";
+        setFailure(400, "malformed HTTP response header");
         return;
     }
     
+    _responseVersion = decodeVersion(parts[0]);    
     _responseStatus = strutils::to_int(parts[1]);
     _responseReason = parts[2];
 }
 
 void Request::responseHeader(const string& key, const string& value)
 {
+    if (key == "connection") {
+        _willClose = (value.find("close") != string::npos);
+    }
+    
     _responseHeaders[key] = value;
 }
 
@@ -106,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);
@@ -119,6 +127,20 @@ string Request::path() const
     return u.substr(hostEnd, query - hostEnd);
 }
 
+
+string Request::query() const
+{
+  string u(url());
+  int query = u.find('?');
+  if (query < 0) {
+    return "";  //no query string found
+  }
+  
+  return u.substr(query);   //includes question mark
+}
+
+
+
 string Request::host() const
 {
     string hp(hostAndPort());
@@ -183,6 +205,37 @@ 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)
+{
+    if (v == "HTTP/1.1") return HTTP_1_1;
+    if (v == "HTTP/1.0") return HTTP_1_0;
+    if (strutils::starts_with(v, "HTTP/0.")) return HTTP_0_x;
+    return HTTP_VERSION_UNKNOWN;
+}
+
+bool Request::closeAfterComplete() const
+{
+// for non HTTP/1.1 connections, assume server closes
+    return _willClose || (_responseVersion != HTTP_1_1);
+}
+  
+int Request::requestBodyLength() const
+{
+  return -1;
+}
+
+std::string Request::requestBodyType() const
+{
+    return "text/plain";
+}
+  
+void Request::getBodyData(char*, int& count) const
+{
+  count = 0;
+  return;
 }
 
 } // of namespace HTTP