reformat Server source and migrate it to C++11
This commit is contained in:
parent
bd316e7e98
commit
3d68769503
@ -28,17 +28,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include "Http/Request.h"
|
||||
#include "RequestParser.h"
|
||||
|
||||
namespace http {
|
||||
namespace http
|
||||
{
|
||||
|
||||
RequestParser::RequestParser() : state_(method_start) { }
|
||||
RequestParser::RequestParser() : state_(method_start) {}
|
||||
|
||||
void RequestParser::Reset() { state_ = method_start; }
|
||||
|
||||
boost::tuple<boost::tribool, char*> RequestParser::Parse(
|
||||
Request& req,
|
||||
char* begin,
|
||||
char* end,
|
||||
http::CompressionType * compressionType)
|
||||
boost::tuple<boost::tribool, char *>
|
||||
RequestParser::Parse(Request &req, char *begin, char *end, http::CompressionType *compressionType)
|
||||
{
|
||||
while (begin != end)
|
||||
{
|
||||
@ -52,7 +50,8 @@ boost::tuple<boost::tribool, char*> RequestParser::Parse(
|
||||
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_)
|
||||
{
|
||||
@ -173,7 +172,7 @@ boost::tribool RequestParser::consume(Request& req, char input, http::Compressio
|
||||
}
|
||||
return false;
|
||||
case header_line_start:
|
||||
if(header.name == "Accept-Encoding")
|
||||
if (header.name == "Accept-Encoding")
|
||||
{
|
||||
/* giving gzip precedence over deflate */
|
||||
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;
|
||||
}
|
||||
if (isCTL(input)) {
|
||||
if (isCTL(input))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
state_ = header_value;
|
||||
@ -269,33 +269,38 @@ boost::tribool RequestParser::consume(Request& req, char input, http::Compressio
|
||||
}
|
||||
}
|
||||
|
||||
inline bool RequestParser::isChar(int c)
|
||||
{
|
||||
return c >= 0 && c <= 127;
|
||||
}
|
||||
inline bool RequestParser::isChar(int c) { return c >= 0 && c <= 127; }
|
||||
|
||||
inline bool RequestParser::isCTL(int c)
|
||||
{
|
||||
return (c >= 0 && c <= 31) || (c == 127);
|
||||
}
|
||||
inline bool RequestParser::isCTL(int c) { return (c >= 0 && c <= 31) || (c == 127); }
|
||||
|
||||
inline bool RequestParser::isTSpecial(int c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '(': case ')': case '<': case '>': case '@':
|
||||
case ',': case ';': case ':': case '\\': case '"':
|
||||
case '/': case '[': case ']': case '?': case '=':
|
||||
case '{': case '}': case ' ': case '\t':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
case '(':
|
||||
case ')':
|
||||
case '<':
|
||||
case '>':
|
||||
case '@':
|
||||
case ',':
|
||||
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)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
inline bool RequestParser::isDigit(int c) { return c >= '0' && c <= '9'; }
|
||||
}
|
||||
|
@ -34,28 +34,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <boost/logic/tribool.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
|
||||
namespace http {
|
||||
namespace http
|
||||
{
|
||||
|
||||
struct Request;
|
||||
|
||||
class RequestParser {
|
||||
public:
|
||||
class RequestParser
|
||||
{
|
||||
public:
|
||||
RequestParser();
|
||||
void Reset();
|
||||
|
||||
boost::tuple<boost::tribool, char*> Parse(
|
||||
Request& req,
|
||||
char* begin,
|
||||
char* end,
|
||||
CompressionType * compressionType
|
||||
);
|
||||
boost::tuple<boost::tribool, char *>
|
||||
Parse(Request &req, char *begin, char *end, CompressionType *compressionType);
|
||||
|
||||
private:
|
||||
boost::tribool consume(
|
||||
Request& req,
|
||||
char input,
|
||||
CompressionType * compressionType
|
||||
);
|
||||
private:
|
||||
boost::tribool consume(Request &req, char input, CompressionType *compressionType);
|
||||
|
||||
inline bool isChar(int c);
|
||||
|
||||
@ -65,29 +59,28 @@ private:
|
||||
|
||||
inline bool isDigit(int c);
|
||||
|
||||
enum state {
|
||||
method_start,
|
||||
method,
|
||||
uri_start,
|
||||
uri,
|
||||
http_version_h,
|
||||
http_version_t_1,
|
||||
http_version_t_2,
|
||||
http_version_p,
|
||||
http_version_slash,
|
||||
http_version_major_start,
|
||||
http_version_major,
|
||||
http_version_minor_start,
|
||||
http_version_minor,
|
||||
expecting_newline_1,
|
||||
header_line_start,
|
||||
header_lws,
|
||||
header_name,
|
||||
space_before_header_value,
|
||||
header_value,
|
||||
expecting_newline_2,
|
||||
expecting_newline_3
|
||||
} state_;
|
||||
enum state
|
||||
{ method_start,
|
||||
method,
|
||||
uri_start,
|
||||
uri,
|
||||
http_version_h,
|
||||
http_version_t_1,
|
||||
http_version_t_2,
|
||||
http_version_p,
|
||||
http_version_slash,
|
||||
http_version_major_start,
|
||||
http_version_major,
|
||||
http_version_minor_start,
|
||||
http_version_minor,
|
||||
expecting_newline_1,
|
||||
header_line_start,
|
||||
header_lws,
|
||||
header_name,
|
||||
space_before_header_value,
|
||||
header_value,
|
||||
expecting_newline_2,
|
||||
expecting_newline_3 } state_;
|
||||
|
||||
Header header;
|
||||
};
|
||||
|
117
Server/Server.h
117
Server/Server.h
@ -34,83 +34,68 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/make_shared.hpp>
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Server: private boost::noncopyable {
|
||||
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);
|
||||
boost::asio::ip::tcp::resolver::query query(address, port);
|
||||
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
|
||||
class Server
|
||||
{
|
||||
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::endpoint endpoint = *resolver.resolve(query);
|
||||
|
||||
acceptor.open(endpoint.protocol());
|
||||
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
||||
acceptor.bind(endpoint);
|
||||
acceptor.listen();
|
||||
acceptor.async_accept(
|
||||
newConnection->socket(),
|
||||
boost::bind(
|
||||
&Server::handleAccept,
|
||||
this,
|
||||
boost::asio::placeholders::error
|
||||
)
|
||||
);
|
||||
}
|
||||
acceptor.open(endpoint.protocol());
|
||||
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
|
||||
acceptor.bind(endpoint);
|
||||
acceptor.listen();
|
||||
acceptor.async_accept(
|
||||
new_connection->socket(),
|
||||
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
|
||||
}
|
||||
|
||||
void Run() {
|
||||
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();
|
||||
}
|
||||
Server(const Server &) = delete;
|
||||
|
||||
void Stop() {
|
||||
ioService.stop();
|
||||
}
|
||||
void Run()
|
||||
{
|
||||
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() {
|
||||
return requestHandler;
|
||||
}
|
||||
void Stop() { io_service.stop(); }
|
||||
|
||||
private:
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
RequestHandler &GetRequestHandlerPtr() { return request_handler; }
|
||||
|
||||
unsigned threadPoolSize;
|
||||
boost::asio::io_service ioService;
|
||||
boost::asio::ip::tcp::acceptor acceptor;
|
||||
boost::shared_ptr<http::Connection> newConnection;
|
||||
RequestHandler requestHandler;
|
||||
private:
|
||||
void HandleAccept(const boost::system::error_code &e)
|
||||
{
|
||||
if (!e)
|
||||
{
|
||||
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
|
||||
|
@ -25,8 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef SERVERFACTORY_H_
|
||||
#define SERVERFACTORY_H_
|
||||
#ifndef SERVER_FACTORY_H
|
||||
#define SERVER_FACTORY_H
|
||||
|
||||
#include "Server.h"
|
||||
#include "../Util/OpenMPWrapper.h"
|
||||
@ -35,27 +35,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#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 {
|
||||
static Server * CreateServer(
|
||||
std::string& ip_address,
|
||||
int ip_port,
|
||||
int threads
|
||||
) {
|
||||
|
||||
SimpleLogger().Write() <<
|
||||
"http 1.1 compression handled by zlib version " << zlibVersion();
|
||||
SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion();
|
||||
|
||||
std::string port_stream;
|
||||
intToString(ip_port, port_stream);
|
||||
|
||||
return new Server(
|
||||
ip_address,
|
||||
port_stream,
|
||||
std::min( omp_get_num_procs(), threads )
|
||||
);
|
||||
}
|
||||
return new Server(ip_address, port_stream, std::min(omp_get_num_procs(), threads));
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SERVERFACTORY_H_ */
|
||||
#endif // SERVER_FACTORY_H
|
||||
|
Loading…
Reference in New Issue
Block a user