reformat Server source and migrate it to C++11

This commit is contained in:
Dennis Luxen 2014-05-07 17:14:57 +02:00
parent bd316e7e98
commit 3d68769503
4 changed files with 129 additions and 153 deletions

View File

@ -28,17 +28,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Http/Request.h" #include "Http/Request.h"
#include "RequestParser.h" #include "RequestParser.h"
namespace http { namespace http
{
RequestParser::RequestParser() : state_(method_start) {} RequestParser::RequestParser() : state_(method_start) {}
void RequestParser::Reset() { state_ = method_start; } void RequestParser::Reset() { state_ = method_start; }
boost::tuple<boost::tribool, char*> RequestParser::Parse( boost::tuple<boost::tribool, char *>
Request& req, RequestParser::Parse(Request &req, char *begin, char *end, http::CompressionType *compressionType)
char* begin,
char* end,
http::CompressionType * compressionType)
{ {
while (begin != end) while (begin != end)
{ {
@ -52,7 +50,8 @@ boost::tuple<boost::tribool, char*> RequestParser::Parse(
return boost::make_tuple(result, begin); return boost::make_tuple(result, begin);
} }
boost::tribool RequestParser::consume(Request& req, char input, http::CompressionType * compressionType) boost::tribool
RequestParser::consume(Request &req, char input, http::CompressionType *compressionType)
{ {
switch (state_) switch (state_)
{ {
@ -219,7 +218,8 @@ boost::tribool RequestParser::consume(Request& req, char input, http::Compressio
{ {
return boost::indeterminate; return boost::indeterminate;
} }
if (isCTL(input)) { if (isCTL(input))
{
return false; return false;
} }
state_ = header_value; state_ = header_value;
@ -269,33 +269,38 @@ boost::tribool RequestParser::consume(Request& req, char input, http::Compressio
} }
} }
inline bool RequestParser::isChar(int c) inline bool RequestParser::isChar(int c) { return c >= 0 && c <= 127; }
{
return c >= 0 && c <= 127;
}
inline bool RequestParser::isCTL(int c) inline bool RequestParser::isCTL(int c) { return (c >= 0 && c <= 31) || (c == 127); }
{
return (c >= 0 && c <= 31) || (c == 127);
}
inline bool RequestParser::isTSpecial(int c) inline bool RequestParser::isTSpecial(int c)
{ {
switch (c) switch (c)
{ {
case '(': case ')': case '<': case '>': case '@': case '(':
case ',': case ';': case ':': case '\\': case '"': case ')':
case '/': case '[': case ']': case '?': case '=': case '<':
case '{': case '}': case ' ': case '\t': case '>':
case '@':
case ',':
case ';':
case ':':
case '\\':
case '"':
case '/':
case '[':
case ']':
case '?':
case '=':
case '{':
case '}':
case ' ':
case '\t':
return true; return true;
default: default:
return false; return false;
} }
} }
inline bool RequestParser::isDigit(int c) inline bool RequestParser::isDigit(int c) { return c >= '0' && c <= '9'; }
{
return c >= '0' && c <= '9';
}
} }

View File

@ -34,28 +34,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/logic/tribool.hpp> #include <boost/logic/tribool.hpp>
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
namespace http { namespace http
{
struct Request; struct Request;
class RequestParser { class RequestParser
{
public: public:
RequestParser(); RequestParser();
void Reset(); void Reset();
boost::tuple<boost::tribool, char*> Parse( boost::tuple<boost::tribool, char *>
Request& req, Parse(Request &req, char *begin, char *end, CompressionType *compressionType);
char* begin,
char* end,
CompressionType * compressionType
);
private: private:
boost::tribool consume( boost::tribool consume(Request &req, char input, CompressionType *compressionType);
Request& req,
char input,
CompressionType * compressionType
);
inline bool isChar(int c); inline bool isChar(int c);
@ -65,8 +59,8 @@ private:
inline bool isDigit(int c); inline bool isDigit(int c);
enum state { enum state
method_start, { method_start,
method, method,
uri_start, uri_start,
uri, uri,
@ -86,8 +80,7 @@ private:
space_before_header_value, space_before_header_value,
header_value, header_value,
expecting_newline_2, expecting_newline_2,
expecting_newline_3 expecting_newline_3 } state_;
} state_;
Header header; Header header;
}; };

View File

@ -34,25 +34,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/make_shared.hpp> #include <boost/make_shared.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <vector> #include <vector>
class Server: private boost::noncopyable { class Server
public:
explicit Server(
const std::string& address,
const std::string& port,
unsigned thread_pool_size
) :
threadPoolSize(thread_pool_size),
acceptor(ioService),
newConnection(new http::Connection(ioService, requestHandler)),
requestHandler()
{ {
boost::asio::ip::tcp::resolver resolver(ioService); public:
explicit Server(const std::string &address, const std::string &port, unsigned thread_pool_size)
: thread_pool_size(thread_pool_size), acceptor(io_service),
new_connection(new http::Connection(io_service, request_handler)), request_handler()
{
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(address, port); boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
@ -61,56 +55,47 @@ public:
acceptor.bind(endpoint); acceptor.bind(endpoint);
acceptor.listen(); acceptor.listen();
acceptor.async_accept( acceptor.async_accept(
newConnection->socket(), new_connection->socket(),
boost::bind( boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
&Server::handleAccept,
this,
boost::asio::placeholders::error
)
);
} }
void Run() { Server(const Server &) = delete;
void Run()
{
std::vector<boost::shared_ptr<boost::thread>> threads; std::vector<boost::shared_ptr<boost::thread>> threads;
for (unsigned i = 0; i < threadPoolSize; ++i) { for (unsigned i = 0; i < thread_pool_size; ++i)
boost::shared_ptr<boost::thread> thread = boost::make_shared<boost::thread>(boost::bind(&boost::asio::io_service::run, &ioService)); {
boost::shared_ptr<boost::thread> thread = boost::make_shared<boost::thread>(
boost::bind(&boost::asio::io_service::run, &io_service));
threads.push_back(thread); threads.push_back(thread);
} }
for (unsigned i = 0; i < threads.size(); ++i) for (unsigned i = 0; i < threads.size(); ++i)
threads[i]->join(); threads[i]->join();
} }
void Stop() { void Stop() { io_service.stop(); }
ioService.stop();
}
RequestHandler & GetRequestHandlerPtr() { RequestHandler &GetRequestHandlerPtr() { return request_handler; }
return requestHandler;
}
private: private:
void handleAccept(const boost::system::error_code& e) { void HandleAccept(const boost::system::error_code &e)
if (!e) { {
newConnection->start(); if (!e)
newConnection.reset( {
new http::Connection(ioService, requestHandler) new_connection->start();
); new_connection.reset(new http::Connection(io_service, request_handler));
acceptor.async_accept( acceptor.async_accept(
newConnection->socket(), new_connection->socket(),
boost::bind( boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
&Server::handleAccept,
this,
boost::asio::placeholders::error
)
);
} }
} }
unsigned threadPoolSize; unsigned thread_pool_size;
boost::asio::io_service ioService; boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::acceptor acceptor;
boost::shared_ptr<http::Connection> newConnection; boost::shared_ptr<http::Connection> new_connection;
RequestHandler requestHandler; RequestHandler request_handler;
}; };
#endif // SERVER_H #endif // SERVER_H

View File

@ -25,8 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef SERVERFACTORY_H_ #ifndef SERVER_FACTORY_H
#define SERVERFACTORY_H_ #define SERVER_FACTORY_H
#include "Server.h" #include "Server.h"
#include "../Util/OpenMPWrapper.h" #include "../Util/OpenMPWrapper.h"
@ -35,27 +35,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <zlib.h> #include <zlib.h>
#include <boost/noncopyable.hpp> struct ServerFactory
{
ServerFactory() = delete;
ServerFactory(const ServerFactory &) = delete;
static Server *CreateServer(std::string &ip_address, int ip_port, int threads)
{
struct ServerFactory : boost::noncopyable { SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion();
static Server * CreateServer(
std::string& ip_address,
int ip_port,
int threads
) {
SimpleLogger().Write() <<
"http 1.1 compression handled by zlib version " << zlibVersion();
std::string port_stream; std::string port_stream;
intToString(ip_port, port_stream); intToString(ip_port, port_stream);
return new Server( return new Server(ip_address, port_stream, std::min(omp_get_num_procs(), threads));
ip_address,
port_stream,
std::min( omp_get_num_procs(), threads )
);
} }
}; };
#endif /* SERVERFACTORY_H_ */ #endif // SERVER_FACTORY_H