removed 'using namespace' from .hpp-files

This commit is contained in:
eidheim 2014-07-14 13:27:06 +02:00
commit 7a7956b700
2 changed files with 44 additions and 44 deletions

View file

@ -7,19 +7,17 @@
#include <unordered_map> #include <unordered_map>
#include <thread> #include <thread>
using namespace std; namespace SimpleWeb {
using namespace boost::asio;
namespace SimpleWeb {
struct Request { struct Request {
string method, path, http_version; std::string method, path, http_version;
shared_ptr<istream> content; std::shared_ptr<std::istream> content;
unordered_map<string, string> header; std::unordered_map<std::string, std::string> header;
}; };
typedef map<string, unordered_map<string, function<void(ostream&, const Request&, const smatch&)> > > resource_type; typedef std::map<std::string, std::unordered_map<std::string,
std::function<void(std::ostream&, const Request&, const std::smatch&)> > > resource_type;
template <class socket_type> template <class socket_type>
class ServerBase { class ServerBase {
@ -28,7 +26,7 @@ namespace SimpleWeb {
resource_type default_resource; resource_type default_resource;
ServerBase(unsigned short port, size_t num_threads=1) : endpoint(ip::tcp::v4(), port), ServerBase(unsigned short port, size_t num_threads=1) : endpoint(boost::asio::ip::tcp::v4(), port),
acceptor(m_io_service, endpoint), num_threads(num_threads) {} acceptor(m_io_service, endpoint), num_threads(num_threads) {}
void start() { void start() {
@ -54,31 +52,31 @@ namespace SimpleWeb {
m_io_service.run(); m_io_service.run();
//Wait for the rest of the threads, if any, to finish as well //Wait for the rest of the threads, if any, to finish as well
for(thread& t: threads) { for(auto& t: threads) {
t.join(); t.join();
} }
} }
protected: protected:
io_service m_io_service; boost::asio::io_service m_io_service;
ip::tcp::endpoint endpoint; boost::asio::ip::tcp::endpoint endpoint;
ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
//shared_ptr<ssl::context> context; //shared_ptr<ssl::context> context;
size_t num_threads; size_t num_threads;
vector<thread> threads; std::vector<std::thread> threads;
//All resources with default_resource at the end of vector //All resources with default_resource at the end of vector
//Created in start() //Created in start()
vector<resource_type::iterator> all_resources; std::vector<resource_type::iterator> all_resources;
virtual void accept() {} virtual void accept() {}
void process_request_and_respond(shared_ptr<socket_type> socket) { void process_request_and_respond(std::shared_ptr<socket_type> socket) {
//Create new read_buffer for async_read_until() //Create new read_buffer for async_read_until()
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf); std::shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf);
async_read_until(*socket, *read_buffer, "\r\n\r\n", boost::asio::async_read_until(*socket, *read_buffer, "\r\n\r\n",
[this, socket, read_buffer](const boost::system::error_code& ec, size_t bytes_transferred) { [this, socket, read_buffer](const boost::system::error_code& ec, size_t bytes_transferred) {
if(!ec) { if(!ec) {
//read_buffer->size() is not necessarily the same as bytes_transferred, from Boost-docs: //read_buffer->size() is not necessarily the same as bytes_transferred, from Boost-docs:
@ -88,20 +86,21 @@ namespace SimpleWeb {
size_t total=read_buffer->size(); size_t total=read_buffer->size();
//Convert to istream to extract string-lines //Convert to istream to extract string-lines
istream stream(read_buffer.get()); std::istream stream(read_buffer.get());
shared_ptr<Request> request(new Request()); std::shared_ptr<Request> request(new Request());
*request=parse_request(stream); *request=parse_request(stream);
size_t num_additional_bytes=total-bytes_transferred; size_t num_additional_bytes=total-bytes_transferred;
//If content, read that as well //If content, read that as well
if(request->header.count("Content-Length")>0) { if(request->header.count("Content-Length")>0) {
async_read(*socket, *read_buffer, transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes), boost::asio::async_read(*socket, *read_buffer,
boost::asio::transfer_exactly(stoull(request->header["Content-Length"])-num_additional_bytes),
[this, socket, read_buffer, request](const boost::system::error_code& ec, size_t bytes_transferred) { [this, socket, read_buffer, request](const boost::system::error_code& ec, size_t bytes_transferred) {
if(!ec) { if(!ec) {
//Store pointer to read_buffer as istream object //Store pointer to read_buffer as istream object
request->content=shared_ptr<istream>(new istream(read_buffer.get())); request->content=std::shared_ptr<std::istream>(new std::istream(read_buffer.get()));
respond(socket, request); respond(socket, request);
} }
@ -114,18 +113,18 @@ namespace SimpleWeb {
}); });
} }
Request parse_request(istream& stream) { Request parse_request(std::istream& stream) {
Request request; Request request;
regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$"); std::regex e("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$");
smatch sm; std::smatch sm;
//First parse request method, path, and HTTP-version from the first line //First parse request method, path, and HTTP-version from the first line
string line; std::string line;
getline(stream, line); getline(stream, line);
line.pop_back(); line.pop_back();
if(regex_match(line, sm, e)) { if(std::regex_match(line, sm, e)) {
request.method=sm[1]; request.method=sm[1];
request.path=sm[2]; request.path=sm[2];
request.http_version=sm[3]; request.http_version=sm[3];
@ -136,7 +135,7 @@ namespace SimpleWeb {
do { do {
getline(stream, line); getline(stream, line);
line.pop_back(); line.pop_back();
matched=regex_match(line, sm, e); matched=std::regex_match(line, sm, e);
if(matched) { if(matched) {
request.header[sm[1]]=sm[2]; request.header[sm[1]]=sm[2];
} }
@ -147,19 +146,20 @@ namespace SimpleWeb {
return request; return request;
} }
void respond(shared_ptr<socket_type> socket, shared_ptr<Request> request) { void respond(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
//Find path- and method-match, and generate response //Find path- and method-match, and generate response
for(auto res_it: all_resources) { for(auto res_it: all_resources) {
regex e(res_it->first); std::regex e(res_it->first);
smatch sm_res; std::smatch sm_res;
if(regex_match(request->path, sm_res, e)) { if(std::regex_match(request->path, sm_res, e)) {
if(res_it->second.count(request->method)>0) { if(res_it->second.count(request->method)>0) {
shared_ptr<boost::asio::streambuf> write_buffer(new boost::asio::streambuf); std::shared_ptr<boost::asio::streambuf> write_buffer(new boost::asio::streambuf);
ostream response(write_buffer.get()); std::ostream response(write_buffer.get());
res_it->second[request->method](response, *request, sm_res); res_it->second[request->method](response, *request, sm_res);
//Capture write_buffer in lambda so it is not destroyed before async_write is finished //Capture write_buffer in lambda so it is not destroyed before async_write is finished
async_write(*socket, *write_buffer, [this, socket, request, write_buffer](const boost::system::error_code& ec, size_t bytes_transferred) { boost::asio::async_write(*socket, *write_buffer,
[this, socket, request, write_buffer](const boost::system::error_code& ec, size_t bytes_transferred) {
//HTTP persistent connection (HTTP 1.1): //HTTP persistent connection (HTTP 1.1):
if(!ec && stof(request->http_version)>1.05) if(!ec && stof(request->http_version)>1.05)
process_request_and_respond(socket); process_request_and_respond(socket);
@ -174,7 +174,7 @@ namespace SimpleWeb {
template<class socket_type> template<class socket_type>
class Server : public ServerBase<socket_type> {}; class Server : public ServerBase<socket_type> {};
typedef ip::tcp::socket HTTP; typedef boost::asio::ip::tcp::socket HTTP;
template<> template<>
class Server<HTTP> : public ServerBase<HTTP> { class Server<HTTP> : public ServerBase<HTTP> {
@ -185,7 +185,7 @@ namespace SimpleWeb {
void accept() { void accept() {
//Create new socket for this connection //Create new socket for this connection
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
shared_ptr<HTTP> socket(new HTTP(m_io_service)); std::shared_ptr<HTTP> socket(new HTTP(m_io_service));
acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec) { acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec) {
//Immediately start accepting a new connection //Immediately start accepting a new connection

View file

@ -5,31 +5,31 @@
#include <boost/asio/ssl.hpp> #include <boost/asio/ssl.hpp>
namespace SimpleWeb { namespace SimpleWeb {
typedef ssl::stream<ip::tcp::socket> HTTPS; typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;
template<> template<>
class Server<HTTPS> : public ServerBase<HTTPS> { class Server<HTTPS> : public ServerBase<HTTPS> {
public: public:
Server(unsigned short port, size_t num_threads, const string& cert_file, const string& private_key_file) : Server(unsigned short port, size_t num_threads, const std::string& cert_file, const std::string& private_key_file) :
ServerBase<HTTPS>::ServerBase(port, num_threads), context(ssl::context::sslv23) { ServerBase<HTTPS>::ServerBase(port, num_threads), context(boost::asio::ssl::context::sslv23) {
context.use_certificate_chain_file(cert_file); context.use_certificate_chain_file(cert_file);
context.use_private_key_file(private_key_file, ssl::context::pem); context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
} }
private: private:
ssl::context context; boost::asio::ssl::context context;
void accept() { void accept() {
//Create new socket for this connection //Create new socket for this connection
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
shared_ptr<HTTPS> socket(new HTTPS(m_io_service, context)); std::shared_ptr<HTTPS> socket(new HTTPS(m_io_service, context));
acceptor.async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code& ec) { acceptor.async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code& ec) {
//Immediately start accepting a new connection //Immediately start accepting a new connection
accept(); accept();
if(!ec) { if(!ec) {
(*socket).async_handshake(ssl::stream_base::server, [this, socket](const boost::system::error_code& ec) { (*socket).async_handshake(boost::asio::ssl::stream_base::server, [this, socket](const boost::system::error_code& ec) {
if(!ec) { if(!ec) {
process_request_and_respond(socket); process_request_and_respond(socket);
} }