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_)
{ {
@ -173,7 +172,7 @@ boost::tribool RequestParser::consume(Request& req, char input, http::Compressio
} }
return false; return false;
case header_line_start: case header_line_start:
if(header.name == "Accept-Encoding") if (header.name == "Accept-Encoding")
{ {
/* giving gzip precedence over deflate */ /* giving gzip precedence over deflate */
if (header.value.find("deflate") != std::string::npos) if (header.value.find("deflate") != std::string::npos)
@ -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 '>':
return true; case '@':
default: case ',':
return false; case ';':
case ':':
case '\\':
case '"':
case '/':
case '[':
case ']':
case '?':
case '=':
case '{':
case '}':
case ' ':
case '\t':
return true;
default:
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,29 +59,28 @@ 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,
http_version_h, http_version_h,
http_version_t_1, http_version_t_1,
http_version_t_2, http_version_t_2,
http_version_p, http_version_p,
http_version_slash, http_version_slash,
http_version_major_start, http_version_major_start,
http_version_major, http_version_major,
http_version_minor_start, http_version_minor_start,
http_version_minor, http_version_minor,
expecting_newline_1, expecting_newline_1,
header_line_start, header_line_start,
header_lws, header_lws,
header_name, header_name,
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,83 +34,68 @@ 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( public:
const std::string& address, explicit Server(const std::string &address, const std::string &port, unsigned thread_pool_size)
const std::string& port, : thread_pool_size(thread_pool_size), acceptor(io_service),
unsigned thread_pool_size new_connection(new http::Connection(io_service, request_handler)), request_handler()
) : {
threadPoolSize(thread_pool_size), boost::asio::ip::tcp::resolver resolver(io_service);
acceptor(ioService), boost::asio::ip::tcp::resolver::query query(address, port);
newConnection(new http::Connection(ioService, requestHandler)), boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
requestHandler()
{
boost::asio::ip::tcp::resolver resolver(ioService);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
acceptor.open(endpoint.protocol()); acceptor.open(endpoint.protocol());
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
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;
std::vector<boost::shared_ptr<boost::thread> > threads;
for (unsigned i = 0; i < threadPoolSize; ++i) {
boost::shared_ptr<boost::thread> thread = boost::make_shared<boost::thread>(boost::bind(&boost::asio::io_service::run, &ioService));
threads.push_back(thread);
}
for (unsigned i = 0; i < threads.size(); ++i)
threads[i]->join();
}
void Stop() { void Run()
ioService.stop(); {
} std::vector<boost::shared_ptr<boost::thread>> threads;
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, &io_service));
threads.push_back(thread);
}
for (unsigned i = 0; i < threads.size(); ++i)
threads[i]->join();
}
RequestHandler & GetRequestHandlerPtr() { void Stop() { io_service.stop(); }
return requestHandler;
}
private: RequestHandler &GetRequestHandlerPtr() { return request_handler; }
void handleAccept(const boost::system::error_code& e) {
if (!e) {
newConnection->start();
newConnection.reset(
new http::Connection(ioService, requestHandler)
);
acceptor.async_accept(
newConnection->socket(),
boost::bind(
&Server::handleAccept,
this,
boost::asio::placeholders::error
)
);
}
}
unsigned threadPoolSize; private:
boost::asio::io_service ioService; void HandleAccept(const boost::system::error_code &e)
boost::asio::ip::tcp::acceptor acceptor; {
boost::shared_ptr<http::Connection> newConnection; if (!e)
RequestHandler requestHandler; {
new_connection->start();
new_connection.reset(new http::Connection(io_service, request_handler));
acceptor.async_accept(
new_connection->socket(),
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
}
}
unsigned thread_pool_size;
boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor acceptor;
boost::shared_ptr<http::Connection> new_connection;
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