Slight cleanup and added convenience function to retrieve Request::content string.
This commit is contained in:
parent
c58b7a788e
commit
7951ded3ec
3 changed files with 31 additions and 15 deletions
|
|
@ -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<HttpServer::Request> 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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<HttpsServer::Request> 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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include <thread>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace SimpleWeb {
|
||||
template <class socket_type>
|
||||
|
|
@ -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<socket_type>;
|
||||
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<socket_type>;
|
||||
public:
|
||||
std::string method, path, http_version;
|
||||
|
||||
std::istream content;
|
||||
Content content;
|
||||
|
||||
std::unordered_multimap<std::string, std::string> 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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue