Some bugfixes and cleanup. Added client

This commit is contained in:
eidheim 2014-08-11 10:03:27 +02:00
commit 8bc7d3e623
5 changed files with 56 additions and 309 deletions

View file

@ -8,21 +8,29 @@
#include <thread>
namespace SimpleWeb {
struct Request {
std::string method, path, http_version;
std::shared_ptr<std::istream> content;
std::unordered_map<std::string, std::string> header;
std::smatch path_match;
};
typedef std::map<std::string, std::unordered_map<std::string, std::function<void(std::ostream&, Request&)> > > resource_type;
template <class socket_type>
class ServerBase {
public:
class Request {
friend class ServerBase<socket_type>;
public:
std::string method, path, http_version;
std::istream content;
std::unordered_map<std::string, std::string> header;
std::smatch path_match;
private:
Request(): content(&streambuf) {}
boost::asio::streambuf streambuf;
};
typedef std::map<std::string, std::unordered_map<std::string,
std::function<void(std::ostream&, std::shared_ptr<ServerBase<socket_type>::Request>)> > > resource_type;
resource_type resource;
resource_type default_resource;
@ -67,7 +75,7 @@ namespace SimpleWeb {
//All resources with default_resource at the end of vector
//Created in start()
std::vector<resource_type::iterator> all_resources;
std::vector<typename resource_type::iterator> all_resources;
ServerBase(unsigned short port, size_t num_threads, size_t timeout_request, size_t timeout_send_or_receive) :
endpoint(boost::asio::ip::tcp::v4(), port), acceptor(m_io_service, endpoint), num_threads(num_threads),
@ -77,34 +85,28 @@ namespace SimpleWeb {
virtual std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, size_t seconds)=0;
void process_request_and_respond(std::shared_ptr<socket_type> socket) {
//Create new read_buffer for async_read_until()
//Shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf);
void read_request_and_content(std::shared_ptr<socket_type> socket) {
//Create new streambuf (Request::streambuf) for async_read_until()
//shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<Request> request(new Request());
//Set timeout on the following boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer;
if(timeout_request>0)
timer=set_timeout_on_socket(socket, timeout_request);
boost::asio::async_read_until(*socket, *read_buffer, "\r\n\r\n",
[this, socket, read_buffer, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n",
[this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
if(timeout_request>0)
timer->cancel();
if(!ec) {
//read_buffer->size() is not necessarily the same as bytes_transferred, from Boost-docs:
//request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs:
//"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter"
//The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the
//read_buffer (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content).
size_t total=read_buffer->size();
//Convert to istream to extract string-lines
std::istream stream(read_buffer.get());
//streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content).
size_t num_additional_bytes=request->streambuf.size()-bytes_transferred;
std::shared_ptr<Request> request(new Request());
*request=parse_request(stream);
size_t num_additional_bytes=total-bytes_transferred;
parse_request(request, request->content);
//If content, read that as well
if(request->header.count("Content-Length")>0) {
@ -113,30 +115,24 @@ namespace SimpleWeb {
if(timeout_content>0)
timer=set_timeout_on_socket(socket, timeout_content);
boost::asio::async_read(*socket, *read_buffer,
boost::asio::async_read(*socket, request->streambuf,
boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes),
[this, socket, read_buffer, request, timer]
[this, socket, request, timer]
(const boost::system::error_code& ec, size_t bytes_transferred) {
if(timeout_content>0)
timer->cancel();
if(!ec) {
//Store pointer to read_buffer as istream object
request->content=std::shared_ptr<std::istream>(new std::istream(read_buffer.get()));
respond(socket, request);
}
if(!ec)
write_response(socket, request);
});
}
else {
respond(socket, request);
write_response(socket, request);
}
}
});
}
Request parse_request(std::istream& stream) const {
Request request;
void parse_request(std::shared_ptr<Request> request, std::istream& stream) const {
std::regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$");
std::smatch sm;
@ -146,9 +142,9 @@ namespace SimpleWeb {
getline(stream, line);
line.pop_back();
if(std::regex_match(line, sm, e)) {
request.method=sm[1];
request.path=sm[2];
request.http_version=sm[3];
request->method=sm[1];
request->path=sm[2];
request->http_version=sm[3];
bool matched;
e="^([^:]*): ?(.*)$";
@ -158,16 +154,14 @@ namespace SimpleWeb {
line.pop_back();
matched=std::regex_match(line, sm, e);
if(matched) {
request.header[sm[1]]=sm[2];
request->header[sm[1]]=sm[2];
}
} while(matched==true);
}
return request;
}
void respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
void write_response(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
//Find path- and method-match, and generate response
for(auto res_it: all_resources) {
std::regex e(res_it->first);
@ -178,7 +172,7 @@ namespace SimpleWeb {
std::shared_ptr<boost::asio::streambuf> write_buffer(new boost::asio::streambuf);
std::ostream response(write_buffer.get());
res_it->second[request->method](response, *request);
res_it->second[request->method](response, request);
//Set timeout on the following boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer;
@ -193,7 +187,7 @@ namespace SimpleWeb {
timer->cancel();
//HTTP persistent connection (HTTP 1.1):
if(!ec && stof(request->http_version)>1.05)
process_request_and_respond(socket);
read_request_and_content(socket);
});
return;
}
@ -218,13 +212,13 @@ namespace SimpleWeb {
//Create new socket for this connection
//Shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<HTTP> socket(new HTTP(m_io_service));
acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec) {
//Immediately start accepting a new connection
accept();
if(!ec) {
process_request_and_respond(socket);
read_request_and_content(socket);
}
});
}