diff Core/HttpServer/HttpToolbox.cpp @ 1447:5ba7471780ae

refactoring: HttpToolbox, DumpJson in Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 01 Jul 2015 17:42:06 +0200
parents 8dc80ba768aa
children 3232f1c995a5
line wrap: on
line diff
--- a/Core/HttpServer/HttpToolbox.cpp	Wed Jul 01 13:16:12 2015 +0200
+++ b/Core/HttpServer/HttpToolbox.cpp	Wed Jul 01 17:42:06 2015 +0200
@@ -194,7 +194,7 @@
   }
 
 
-  bool HttpToolbox::SimpleGet(std::string& output,
+  bool HttpToolbox::SimpleGet(std::string& result,
                               IHttpHandler& handler,
                               const std::string& uri)
   {
@@ -210,7 +210,35 @@
     if (handler.Handle(http, HttpMethod_Get, curi, headers, getArguments, 
                        NULL /* no body for GET */, 0))
     {
-      stream.GetOutput(output);
+      stream.GetOutput(result);
+      return true;
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+
+  static bool SimplePostOrPut(std::string& result,
+                              IHttpHandler& handler,
+                              HttpMethod method,
+                              const std::string& uri,
+                              const char* bodyData,
+                              size_t bodySize)
+  {
+    IHttpHandler::Arguments headers;  // No HTTP header
+    IHttpHandler::GetArguments getArguments;  // No GET argument for POST/PUT
+
+    UriComponents curi;
+    Toolbox::SplitUriComponents(curi, uri);
+
+    StringHttpOutput stream;
+    HttpOutput http(stream, false /* no keep alive */);
+
+    if (handler.Handle(http, method, curi, headers, getArguments, bodyData, bodySize))
+    {
+      stream.GetOutput(result);
       return true;
     }
     else
@@ -219,4 +247,40 @@
     }
   }
 
+
+  bool HttpToolbox::SimplePost(std::string& result,
+                               IHttpHandler& handler,
+                               const std::string& uri,
+                               const char* bodyData,
+                               size_t bodySize)
+  {
+    return SimplePostOrPut(result, handler, HttpMethod_Post, uri, bodyData, bodySize);
+  }
+
+
+  bool HttpToolbox::SimplePut(std::string& result,
+                              IHttpHandler& handler,
+                              const std::string& uri,
+                              const char* bodyData,
+                              size_t bodySize)
+  {
+    return SimplePostOrPut(result, handler, HttpMethod_Put, uri, bodyData, bodySize);
+  }
+
+
+  bool HttpToolbox::SimpleDelete(IHttpHandler& handler,
+                                 const std::string& uri)
+  {
+    UriComponents curi;
+    Toolbox::SplitUriComponents(curi, uri);
+
+    IHttpHandler::Arguments headers;  // No HTTP header
+    IHttpHandler::GetArguments getArguments;  // No GET argument for DELETE
+
+    StringHttpOutput stream;
+    HttpOutput http(stream, false /* no keep alive */);
+
+    return handler.Handle(http, HttpMethod_Delete, curi, headers, getArguments, 
+                          NULL /* no body for DELETE */, 0);
+  }
 }