]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/HTTPRequest.cxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / io / HTTPRequest.cxx
index 6b2a8290a38e36c790d0cdfc64ed8e6612c1bfd2..535ec402da8fa9c043ded3de7f9f7e0d0238fa63 100644 (file)
@@ -4,6 +4,7 @@
 #include <simgear/debug/logstream.hxx>
 #include <simgear/misc/strutils.hxx>
 #include <simgear/props/props_io.hxx>
+#include <simgear/structure/exception.hxx>
 
 namespace simgear
 {
@@ -115,9 +116,7 @@ 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) {
-        SG_LOG(SG_IO, SG_WARN, "HTTP::Request: malformed response start:" << r);
-        setFailure(400, "malformed HTTP response header");
-        return;
+        throw sg_io_exception("bad HTTP response");
     }
     
     _responseVersion = decodeHTTPVersion(parts[0]);
@@ -273,6 +272,16 @@ std::string Request::hostAndPort() const
   return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
 }
 
+//------------------------------------------------------------------------------
+std::string Request::responseMime() const
+{
+  std::string content_type = _responseHeaders.get("content-type");
+  if( content_type.empty() )
+    return "application/octet-stream";
+
+  return content_type.substr(0, content_type.find(';'));
+}
+
 //------------------------------------------------------------------------------
 void Request::setResponseLength(unsigned int l)
 {
@@ -304,22 +313,28 @@ void Request::setReadyState(ReadyState state)
   _ready_state = state;
   if( state == DONE )
   {
+    // Finish C++ part of request to ensure everything is finished (for example
+    // files and streams are closed) before calling any callback (possibly using
+    // such files)
+    onDone();
+    onAlways();
+
     if( _cb_done )
       _cb_done(this);
-    onDone();
   }
   else if( state == FAILED )
   {
+    onFail();
+    onAlways();
+
     if( _cb_fail )
       _cb_fail(this);
-    onFail();
   }
   else
     return;
 
   if( _cb_always )
     _cb_always(this);
-  onAlways();
 }
 
 //------------------------------------------------------------------------------