Slight cleanup and added convenience function to retrieve Request::content string.

This commit is contained in:
eidheim 2015-09-19 13:01:40 +02:00
commit 7951ded3ec
3 changed files with 31 additions and 15 deletions

View file

@ -24,10 +24,12 @@ int main() {
//Add resources using path-regex and method-string, and an anonymous function //Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string //POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) { server.resource["^/string$"]["POST"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
//Retrieve string from istream (request->content) //Retrieve string:
stringstream ss; auto content=request->content.string();
ss << request->content.rdbuf(); //request->content.string() is a convenience function for:
string content=ss.str(); //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; response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}; };

View file

@ -24,10 +24,12 @@ int main() {
//Add resources using path-regex and method-string, and an anonymous function //Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string //POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) { server.resource["^/string$"]["POST"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
//Retrieve string from istream (request->content) //Retrieve string:
stringstream ss; auto content=request->content.string();
ss << request->content.rdbuf(); //request->content.string() is a convenience function for:
string content=ss.str(); //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; response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
}; };

View file

@ -9,6 +9,7 @@
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <sstream>
namespace SimpleWeb { namespace SimpleWeb {
template <class socket_type> template <class socket_type>
@ -39,17 +40,28 @@ namespace SimpleWeb {
} }
}; };
static Response& flush(Response& r) { class Content : public std::istream {
r.flush(); friend class ServerBase<socket_type>;
return r; 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 { class Request {
friend class ServerBase<socket_type>; friend class ServerBase<socket_type>;
public: public:
std::string method, path, http_version; std::string method, path, http_version;
std::istream content; Content content;
std::unordered_multimap<std::string, std::string> header; std::unordered_multimap<std::string, std::string> header;
@ -59,7 +71,7 @@ namespace SimpleWeb {
unsigned short remote_endpoint_port; unsigned short remote_endpoint_port;
private: 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; boost::asio::streambuf streambuf;