]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/HTTPRequest.hxx
Fix VS2010 lack of fminf
[simgear.git] / simgear / io / HTTPRequest.hxx
index 89a953bc6a64dd0514275c3414b3585b6fa337e7..2365b85526895dc096d6e9343e55bab851b1d9a8 100644 (file)
@@ -1,14 +1,34 @@
+///@file
+//
+// Copyright (C) 2011  James Turner <zakalawe@mac.com>
+// Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU Library General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
+
 #ifndef SG_HTTP_REQUEST_HXX
 #define SG_HTTP_REQUEST_HXX
 
 #include <map>
 
+#include <simgear/structure/function_list.hxx>
 #include <simgear/structure/map.hxx>
 #include <simgear/structure/SGReferenced.hxx>
 #include <simgear/structure/SGSharedPtr.hxx>
 #include <simgear/math/sg_types.hxx>
 
-#include <boost/function.hpp>
+#include <boost/bind.hpp>
 
 class SGPropertyNode;
 
@@ -17,6 +37,9 @@ namespace simgear
 namespace HTTP
 {
 
+/**
+ * Base class for HTTP request (and answer).
+ */
 class Request:
   public SGReferenced
 {
@@ -46,15 +69,21 @@ public:
                                             { return _request_headers.get(key); }
 
     /**
-     * Set the handler to be called when the request successfully completes.
+     * Add a handler to be called when the request successfully completes.
      *
      * @note If the request is already complete, the handler is called
      *       immediately.
      */
     Request* done(const Callback& cb);
 
+    template<class C>
+    Request* done(C* instance, void (C::*mem_func)(Request*))
+    {
+      return done(boost::bind(mem_func, instance, _1));
+    }
+
     /**
-     * Set the handler to be called when the request completes or aborts with an
+     * Add a handler to be called when the request completes or aborts with an
      * error.
      *
      * @note If the request has already failed, the handler is called
@@ -62,15 +91,27 @@ public:
      */
     Request* fail(const Callback& cb);
 
+    template<class C>
+    Request* fail(C* instance, void (C::*mem_func)(Request*))
+    {
+      return fail(boost::bind(mem_func, instance, _1));
+    }
+
     /**
-     * Set the handler to be called when the request either successfully
-     * completes or fails.
+     * Add a handler to be called when the request either successfully completes
+     * or fails.
      *
      * @note If the request is already complete or has already failed, the
      *       handler is called immediately.
      */
     Request* always(const Callback& cb);
 
+    template<class C>
+    Request* always(C* instance, void (C::*mem_func)(Request*))
+    {
+      return always(boost::bind(mem_func, instance, _1));
+    }
+
     /**
      * Set the data for the body of the request. The request is automatically
      * send using the POST method.
@@ -99,6 +140,8 @@ public:
     StringMap const& responseHeaders() const
         { return _responseHeaders; }
 
+    std::string responseMime() const;
+
     virtual int responseCode() const
         { return _responseStatus; }
         
@@ -177,6 +220,8 @@ protected:
     virtual void onFail();
     virtual void onAlways();
 
+    void setFailure(int code, const std::string& reason);
+
 private:
     friend class Client;
     friend class Connection;
@@ -186,7 +231,6 @@ private:
     Request& operator=(const Request&); // = delete;
 
     void processBodyBytes(const char* s, int n);
-    void setFailure(int code, const std::string& reason);
     void setReadyState(ReadyState state);
 
     std::string   _method;
@@ -202,9 +246,9 @@ private:
     unsigned int  _responseLength;
     unsigned int  _receivedBodyBytes;
 
-    Callback      _cb_done,
-                  _cb_fail,
-                  _cb_always;
+    function_list<Callback> _cb_done,
+                            _cb_fail,
+                            _cb_always;
 
     ReadyState    _ready_state;
     bool          _willClose;