From 7951ded3ecf36ef26adab3202af74c90ec870dd4 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 19 Sep 2015 13:01:40 +0200 Subject: [PATCH] Slight cleanup and added convenience function to retrieve Request::content string. --- http_examples.cpp | 10 ++++++---- https_examples.cpp | 10 ++++++---- server_http.hpp | 26 +++++++++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/http_examples.cpp b/http_examples.cpp index 3f1b274..55bb541 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -24,10 +24,12 @@ int main() { //Add resources using path-regex and method-string, and an anonymous function //POST-example for the path /string, responds the posted string server.resource["^/string$"]["POST"]=[](HttpServer::Response& response, shared_ptr request) { - //Retrieve string from istream (request->content) - stringstream ss; - ss << request->content.rdbuf(); - string content=ss.str(); + //Retrieve string: + auto content=request->content.string(); + //request->content.string() is a convenience function for: + //stringstream ss; + //ss << request->content.rdbuf(); + //string content=ss.str(); response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; }; diff --git a/https_examples.cpp b/https_examples.cpp index 69dd580..4164659 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -24,10 +24,12 @@ int main() { //Add resources using path-regex and method-string, and an anonymous function //POST-example for the path /string, responds the posted string server.resource["^/string$"]["POST"]=[](HttpsServer::Response& response, shared_ptr request) { - //Retrieve string from istream (request->content) - stringstream ss; - ss << request->content.rdbuf(); - string content=ss.str(); + //Retrieve string: + auto content=request->content.string(); + //request->content.string() is a convenience function for: + //stringstream ss; + //ss << request->content.rdbuf(); + //string content=ss.str(); response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content; }; diff --git a/server_http.hpp b/server_http.hpp index ac5f0a9..75e8c21 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace SimpleWeb { template @@ -38,18 +39,29 @@ namespace SimpleWeb { throw std::runtime_error(ec.message()); } }; - - static Response& flush(Response& r) { - r.flush(); - return r; - } + + class Content : public std::istream { + friend class ServerBase; + public: + size_t size() { + return streambuf.size(); + } + std::string string() { + std::stringstream ss; + ss << rdbuf(); + return ss.str(); + } + private: + boost::asio::streambuf &streambuf; + Content(boost::asio::streambuf &streambuf): std::istream(&streambuf), streambuf(streambuf) {} + }; class Request { friend class ServerBase; public: std::string method, path, http_version; - std::istream content; + Content content; std::unordered_multimap header; @@ -59,7 +71,7 @@ namespace SimpleWeb { unsigned short remote_endpoint_port; private: - Request(boost::asio::io_service &io_service): content(&streambuf), strand(io_service) {} + Request(boost::asio::io_service &io_service): content(streambuf), strand(io_service) {} boost::asio::streambuf streambuf;