Simple Http Server Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Node.JS HTTP server displaying GooglePortable periodic/one-shot timer implementationConcise HTTP serverCalling other machines from the LinkedList if one is blocked or downReceiving a JSON string response from a URLSimple Router classPython HTTP ServerSimple Multi-Threaded Web ServerNodeJS static file HTTP serverBasic Python HTTP Server

Moving a wrapfig vertically to encroach partially on a subsection title

Simple Http Server

Why weren't discrete x86 CPUs ever used in game hardware?

Is there hard evidence that the grant peer review system performs significantly better than random?

Is openssl rand command cryptographically secure?

Did any compiler fully use 80-bit floating point?

Are the endpoints of the domain of a function counted as critical points?

Connecting Mac Book Pro 2017 to 2 Projectors via USB C

What is the difference between a "ranged attack" and a "ranged weapon attack"?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Relating to the President and obstruction, were Mueller's conclusions preordained?

Is there public access to the Meteor Crater in Arizona?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

Mounting TV on a weird wall that has some material between the drywall and stud

The test team as an enemy of development? And how can this be avoided?

Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?

Why are vacuum tubes still used in amateur radios?

Can two person see the same photon?

"klopfte jemand" or "jemand klopfte"?

Why does electrolysis of aqueous concentrated sodium bromide produce bromine at the anode?

Can an iPhone 7 be made to function as a NFC Tag?

GDP with Intermediate Production

One-one communication

Test print coming out spongy



Simple Http Server



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Node.JS HTTP server displaying GooglePortable periodic/one-shot timer implementationConcise HTTP serverCalling other machines from the LinkedList if one is blocked or downReceiving a JSON string response from a URLSimple Router classPython HTTP ServerSimple Multi-Threaded Web ServerNodeJS static file HTTP serverBasic Python HTTP Server



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5












$begingroup$


I'm working on a project (studying) and I have a lot of time for proof of concepts and write something from scratch.



Basically I'm creating a Http Server (simple, but not too simple) in C++ using sockets and multi-threading.



There are two topics that I'm concerned about: the design pattern of my code structure and the efficiency of my thread implementation. Obs: a little worried about C++ best practices, since I'm diving too fast into C++ (Am I abusing of std items?, since this require a low-level implementation).



Server is the main file, that calls Routes, Request and Response. Request and Response contains Struct (that contains Status Code). And Routes contains Request and Response.





IMPORTANT: I'm using nlohmann/json (https://github.com/nlohmann/json) and j-ulrich status-codes (https://github.com/j-ulrich/http-status-codes-cpp).



Server (Accepting Sockets and Multi-threading):




#pragma once

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <iostream>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <condition_variable>

#include "Server/Request.h"
#include "Server/Response.h"
#include "Server/Struct.h"
#include "Server/Routes.h"
#include "Tools/Logger.h"

class Server
public:
Server(unsigned int port, unsigned int max_connections = 64, unsigned int thread_count = 5);
~Server();

bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
bool doListen();
bool doStop();

private:
unsigned int _port;
unsigned int _max_connections;
unsigned int _thread_count;

std::mutex _mutex;
std::condition_variable _condition;

bool _signal;

std::vector<unsigned int> _queue;

std::thread* _thread_consume;
std::thread* _thread_process;

Routes* _routes;

int _socket;
struct sockaddr_in _address;
bool _listen;

bool _doStop();
bool _doCreateSocket(int& socket_in);
bool _doBindSocket(int file_descriptor);
void _doConsumeSocket();
void _doProcessSocket(int id);
bool _doProcessRequest(Request* request, Response* response);
;



#include "Server/Server.h"

Server::Server(unsigned int port, unsigned int max_connections, unsigned int thread_count)
if (port > 65535)
Logger::doSendMessage(Logger::TYPES::ERROR, "[Port must be something between 0 and 65535 on Server::Constructor.");


if (max_connections < 1)
Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


_port = port;
_max_connections = max_connections;
_thread_count = thread_count;

_routes = new Routes();

int status = _doCreateSocket(_socket);
if (!status)
Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


if (!_doBindSocket(_socket))
Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");
;

_signal = false;
_listen = false;


Server::~Server()
_doStop();

shutdown(_socket, SHUT_RD);
close(_socket);

try
_thread_consume->join();
for (size_t i = 0; i < _thread_count; i++)
_thread_process[i].join();

catch (...)

delete _thread_consume;
delete[] _thread_process;
delete _routes;


bool Server::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))
return _routes->setRoute(path, method, callback);


bool Server::doListen()
if (_listen) return false;

int status;

status = listen(_socket, _max_connections);
if (status < 0) return false;

Logger::doSendMessage(Logger::TYPES::INFO, "Server running with success at port " + std::to_string(_port) + ".");

_listen = true;

_thread_consume = new std::thread(&Server::_doConsumeSocket, this);
_thread_process = new std::thread[_thread_count];
for (size_t i = 0; i < _thread_count; i++)
_thread_process[i] = std::thread(&Server::_doProcessSocket, this, i);


return true;


bool Server::doStop()
return _doStop();


bool Server::_doStop()
if (!_listen) return false;

std::lock_guard<std::mutex> lock(_mutex);
_listen = false;

_condition.notify_one();
return true;


bool Server::_doCreateSocket(int& socket_in) SO_REUSEPORT, &opt, sizeof(opt));
if (error) return false;

socket_in = file_descriptor;
return true;


bool Server::_doBindSocket(int file_descriptor)
if (!file_descriptor) return false;

_address.sin_family = AF_INET;
_address.sin_addr.s_addr = INADDR_ANY;
_address.sin_port = htons(_port);

int status;

status = bind(file_descriptor, (struct sockaddr*) &_address, sizeof(_address));
if (status < 0) return false;

return true;


void Server::_doConsumeSocket()
int socket_in;
int address_size = sizeof(_address);

while (_listen)
socket_in = accept(_socket, (struct sockaddr*) &_address, (socklen_t*) &address_size);
if (socket_in < 0) continue;


std::lock_guard<std::mutex> lock(_mutex);
_queue.push_back(socket_in);
_signal = true;


_condition.notify_one();



void Server::_doProcessSocket(int id)
while (_listen)
int queue_size = 0;


std::unique_lock<std::mutex> lock(_mutex);
_condition.wait(lock,
[this]
if (this->_signal) return true;
if (!this->_listen && !this->_queue.size()) return true;
return false;

);
queue_size = _queue.size();


if (!queue_size)

std::lock_guard<std::mutex> lock(_mutex);
_signal = false;


_condition.notify_one();
continue;


int socket_in = 0;

std::lock_guard<std::mutex> lock(_mutex);
socket_in = _queue[0];
_queue.erase(_queue.begin());


Request* request = new Request(socket_in);
Response* response = new Response(socket_in);

int status = _doProcessRequest(request, response);

delete request;
delete response;
close(socket_in);



bool Server::_doProcessRequest(Request* request, Response* response)
if (!request->isValid())
response->doSendError(HttpStatus::Code::BadRequest, "Invalid request.");
return false;


std::string path = request->getPath();
Struct::Methods method = request->getMethod();

Routes::Route route;
if (!(_routes->getRoute(path, method, route) && route.isValid()))
response->doSendError(HttpStatus::Code::Forbidden, "Path invalid/not found.");
return false;


if (route.method != method)
response->doSendError(HttpStatus::Code::MethodNotAllowed, "Method invalid/not found.");
return false;


void (*callback)(Request*, Response*) = route.callback;
callback(request, response);

if (!response->isSent())
response->doSendError(HttpStatus::Code::ServiceUnavailable, "Resource was not found or can't respond now.");


return true;



Request (parsing) and Response (sending)




Request



#pragma once

#include <unistd.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <regex>
#include <json.hpp>

#include "Server/Struct.h"

using json = nlohmann::json;

class Request
public:
Request(int socket, unsigned int buffer_size = 1024);
~Request();

bool isValid();
std::string getPath() return _attributes.path;
Struct::Methods getMethod() return _attributes.method;
std::unordered_map<std::string, std::string> getHeaders() return _attributes.headers;
std::string getHeader(std::string header) return _attributes.headers[header];
json getBody() return _attributes.body;

private:
int _socket;
unsigned int _buffer_size;
std::string _data;
Struct::Attributes _attributes;
bool _status;

std::string _doReceiveData(int sock_in);
bool _doParseData(std::string data, Struct::Attributes& attributes);
std::vector<std::string> _doSplitText(std::string text, std::string delimiter);
std::vector<std::string> _doSplitText(std::string text, std::string delimiter, int lock);
;



Request::Request(int socket, unsigned int buffer_size) 
_socket = socket;
_buffer_size = buffer_size;
_status = false;

_data = _doReceiveData(_socket);
if (!_data.length()) return;

bool result;
result = _doParseData(_data, _attributes);

if (!result) return;
if (!_attributes.isValidRequest()) return;

_status = true;


Request::~Request()



bool Request::isValid()
return _status;


std::string Request::_doReceiveData(int sock_in)
char* buffer = new char[_buffer_size];
memset(buffer, '', _buffer_size);
read(sock_in, buffer, _buffer_size);

std::string data;
data.assign(buffer);
delete[] buffer;
return data;


bool Request::_doParseData(std::string data, Struct::Attributes& attributes)
std::string delimiter = "rn";
std::vector<std::string> rows = _doSplitText(data, delimiter);
if (!rows.size()) return false;

std::string header = rows[0];
rows.erase(rows.begin());

if (!header.length()) return false;

std::vector<std::string> parsed_header = _doSplitText(header, std::string(" "));
if (parsed_header.size() < 2) return false;

Struct::Methods method = Struct::doParseHttpMethod(parsed_header[0]);
if (method == Struct::Methods::NONE) return false;

std::string path = parsed_header[1];

std::unordered_map<std::string, std::string> headers;
for (size_t i = 0; i < rows.size(); i++)
std::string row = rows[i];
delimiter = ":";

std::vector<std::string> splited = _doSplitText(row, delimiter, true);
if (splited.size() != 2) continue;

headers[splited[0]] = splited[1];


_attributes.method = method;
_attributes.path = path;
_attributes.headers = headers;

std::string content_length = headers["Content-Length"];
int content_size = 0;

if (content_size = atoi(content_length.c_str()))
std::string body = data.substr(data.length() - content_size, data.length());
json parsed_body = json::parse(body, nullptr, false);
if (parsed_body != NULL && !parsed_body.is_discarded()) _attributes.body = parsed_body;


return true;


std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter)
std::vector<std::string> result;
int delimiter_length = delimiter.length();

std::string block;
std::string region;
int index = 0;

for (size_t i = 0; i < text.length(); i++)
block = text.substr(i, delimiter_length);
if (block.length() != delimiter_length) continue;

if (block == delimiter)
region = text.substr(index, i - index);
result.push_back(region);
index = i + delimiter_length;



return result;


std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter, int lock)
...



Response



#pragma once

#include <unistd.h>
#include <iostream>
#include <string>
#include <unordered_map>

#include "Server/Struct.h"

class Response
public:
Response(int socket_in);
~Response();

bool isSent() return _sent;
void setCode(HttpStatus::Code code) _attributes.code = code;
void setHeader(std::string key, std::string value) _attributes.headers[key] = value;
void setBody(json body) _attributes.body = body;
void doClearHeaders() _attributes.headers.clear();
void doClearBody() _attributes.body = json::value_t::object;

bool doSendSuccess();
bool doSendError(HttpStatus::Code code, const std::string& message);

private:
int _socket;
bool _sent;

Struct::Attributes _attributes;

bool _doSendPayload();
bool _doCreatePayload(std::string& payload);
;



#include "Response.h"

Response::Response(int socket_in)
_socket = socket_in;
_sent = false;


Response::~Response()



bool Response::doSendSuccess()
setCode(HttpStatus::Code::OK);
setHeader("Connection", "Closed");
return _doSendPayload();


bool Response::doSendError(HttpStatus::Code code, const std::string& message)
setCode(code);
doClearHeaders();
doClearBody();

setHeader("Connection", "Closed");

json body;
body["error"] = ;
body["error"]["code"] = code;
body["error"]["message"] = message;

setBody(body);
return _doSendPayload();


bool Response::_doSendPayload()
if (_sent) return false;

int status;

setHeader("Server", "Dark");
setHeader("Content-Type", "application/json");

std::string payload;
status = _doCreatePayload(payload);
if (!status) return false;

status = write(_socket, payload.c_str(), payload.size());
if (status < 1) return false;

_sent = true;
return true;


bool Response::_doCreatePayload(std::string& payload)
std::string current_payload;
std::string data = _attributes.body.dump(4);

int data_length = data.size();

if (data_length)
_attributes.headers["Content-Length"] = std::to_string(data_length);


current_payload += _attributes.version + " " + std::to_string((int) _attributes.code) + " " + HttpStatus::getReasonPhrase(_attributes.code) + "rn";

std::unordered_map<std::string, std::string>::iterator iterator;
for (iterator = _attributes.headers.begin(); iterator != _attributes.headers.end(); iterator++)
std::string key = iterator->first;
std::string value = iterator->second;

current_payload += key + ": " + value + "rn";


if (data_length) current_payload += "rn" + data + "rnrn";

payload = current_payload;
return true;



Routes




#pragma once

#include <iostream>
#include <string>
#include <vector>

#include "Server/Request.h"
#include "Server/Response.h"

class Routes
public:
Routes();
~Routes();

struct Route
std::string path;
Struct::Methods method;
void (*callback)(Request*, Response*);

bool isValid() method > Struct::Methods::LAST) return false;
if (callback == nullptr) return false;
return true;

;

bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
bool getRoute(std::string path, Struct::Methods method, Route& route);

private:
std::vector<Route> _routes;

int _getRouteIndex(std::string path, Struct::Methods method);
;



#include "Routes.h"

Routes::Routes()



Routes::~Routes()



bool Routes::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))

bool Routes::getRoute(std::string path, Struct::Methods method, Routes::Route& route)
int index = _getRouteIndex(path, method);
if (index < 0) return false;
route = _routes[index];
return true;


int Routes::_getRouteIndex(std::string path, Struct::Methods method)
for (size_t i = 0; i < _routes.size(); i++)
Route* route = &_routes[i];
if (route->path == path && route->method == method)
return i;


return -1;



Struct and StatusCode




StatusCode




https://github.com/j-ulrich/http-status-codes-cpp




Struct



#pragma once

#include <json.hpp>

#include "Server/StatusCode.h"

using json = nlohmann::json;

class Struct
public:
enum class Methods
NONE = 0, GET = 1, POST = 2, FIRST = GET, LAST = POST
;

struct Attributes
const std::string version = "HTTP/1.1";

std::string path;
Methods method;
HttpStatus::Code code;
std::unordered_map<std::string, std::string> headers;
json body;

Attributes()
code = HttpStatus::Code::InternalServerError;
body = json::value_t::object;


bool isValidRequest()
if (!path.length()) return false;
if (method < Methods::FIRST

bool isValidResponse()
if (!headers.size()) return false;
return true;

;

static Methods doParseHttpMethod(std::string value)
Methods target = Methods::NONE;

if (value == "GET") target = Methods::GET;
if (value == "POST") target = Methods::POST;

return target;


private:
;


Main (usage):




#include "Server/Server.h"

void exec(Request* request, Response* response)
json body;

body["foo"] = 123;
body["bar"] = true;

response->setBody(body);
response->doSendSuccess();


int main(int argc, char* argv[])
Server* server = new Server(5000);
server->setRoute("/getStatus", Struct::Methods::GET, exec);
server->setRoute("/postStatus", Struct::Methods::POST, exec);
server->doListen();

// let threads live for some time before they eternaly be gone
/*
actually I'm stuck with this sleep, I don't know how to hold
this until caller call doStop() without using while and
consuming process power
*/
sleep(30);

delete server;
return 1;




Basically I'm mirroring NodeJS express API Usage:
server.setRoute(path, route, callback);




So, what can be done to improve my code in terms of optimization and efficiency?



Thanks in advance.










share|improve this question









New contributor




Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$


















    5












    $begingroup$


    I'm working on a project (studying) and I have a lot of time for proof of concepts and write something from scratch.



    Basically I'm creating a Http Server (simple, but not too simple) in C++ using sockets and multi-threading.



    There are two topics that I'm concerned about: the design pattern of my code structure and the efficiency of my thread implementation. Obs: a little worried about C++ best practices, since I'm diving too fast into C++ (Am I abusing of std items?, since this require a low-level implementation).



    Server is the main file, that calls Routes, Request and Response. Request and Response contains Struct (that contains Status Code). And Routes contains Request and Response.





    IMPORTANT: I'm using nlohmann/json (https://github.com/nlohmann/json) and j-ulrich status-codes (https://github.com/j-ulrich/http-status-codes-cpp).



    Server (Accepting Sockets and Multi-threading):




    #pragma once

    #include <unistd.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <iostream>
    #include <unordered_map>
    #include <thread>
    #include <mutex>
    #include <condition_variable>

    #include "Server/Request.h"
    #include "Server/Response.h"
    #include "Server/Struct.h"
    #include "Server/Routes.h"
    #include "Tools/Logger.h"

    class Server
    public:
    Server(unsigned int port, unsigned int max_connections = 64, unsigned int thread_count = 5);
    ~Server();

    bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
    bool doListen();
    bool doStop();

    private:
    unsigned int _port;
    unsigned int _max_connections;
    unsigned int _thread_count;

    std::mutex _mutex;
    std::condition_variable _condition;

    bool _signal;

    std::vector<unsigned int> _queue;

    std::thread* _thread_consume;
    std::thread* _thread_process;

    Routes* _routes;

    int _socket;
    struct sockaddr_in _address;
    bool _listen;

    bool _doStop();
    bool _doCreateSocket(int& socket_in);
    bool _doBindSocket(int file_descriptor);
    void _doConsumeSocket();
    void _doProcessSocket(int id);
    bool _doProcessRequest(Request* request, Response* response);
    ;



    #include "Server/Server.h"

    Server::Server(unsigned int port, unsigned int max_connections, unsigned int thread_count)
    if (port > 65535)
    Logger::doSendMessage(Logger::TYPES::ERROR, "[Port must be something between 0 and 65535 on Server::Constructor.");


    if (max_connections < 1)
    Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


    _port = port;
    _max_connections = max_connections;
    _thread_count = thread_count;

    _routes = new Routes();

    int status = _doCreateSocket(_socket);
    if (!status)
    Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


    if (!_doBindSocket(_socket))
    Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");
    ;

    _signal = false;
    _listen = false;


    Server::~Server()
    _doStop();

    shutdown(_socket, SHUT_RD);
    close(_socket);

    try
    _thread_consume->join();
    for (size_t i = 0; i < _thread_count; i++)
    _thread_process[i].join();

    catch (...)

    delete _thread_consume;
    delete[] _thread_process;
    delete _routes;


    bool Server::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))
    return _routes->setRoute(path, method, callback);


    bool Server::doListen()
    if (_listen) return false;

    int status;

    status = listen(_socket, _max_connections);
    if (status < 0) return false;

    Logger::doSendMessage(Logger::TYPES::INFO, "Server running with success at port " + std::to_string(_port) + ".");

    _listen = true;

    _thread_consume = new std::thread(&Server::_doConsumeSocket, this);
    _thread_process = new std::thread[_thread_count];
    for (size_t i = 0; i < _thread_count; i++)
    _thread_process[i] = std::thread(&Server::_doProcessSocket, this, i);


    return true;


    bool Server::doStop()
    return _doStop();


    bool Server::_doStop()
    if (!_listen) return false;

    std::lock_guard<std::mutex> lock(_mutex);
    _listen = false;

    _condition.notify_one();
    return true;


    bool Server::_doCreateSocket(int& socket_in) SO_REUSEPORT, &opt, sizeof(opt));
    if (error) return false;

    socket_in = file_descriptor;
    return true;


    bool Server::_doBindSocket(int file_descriptor)
    if (!file_descriptor) return false;

    _address.sin_family = AF_INET;
    _address.sin_addr.s_addr = INADDR_ANY;
    _address.sin_port = htons(_port);

    int status;

    status = bind(file_descriptor, (struct sockaddr*) &_address, sizeof(_address));
    if (status < 0) return false;

    return true;


    void Server::_doConsumeSocket()
    int socket_in;
    int address_size = sizeof(_address);

    while (_listen)
    socket_in = accept(_socket, (struct sockaddr*) &_address, (socklen_t*) &address_size);
    if (socket_in < 0) continue;


    std::lock_guard<std::mutex> lock(_mutex);
    _queue.push_back(socket_in);
    _signal = true;


    _condition.notify_one();



    void Server::_doProcessSocket(int id)
    while (_listen)
    int queue_size = 0;


    std::unique_lock<std::mutex> lock(_mutex);
    _condition.wait(lock,
    [this]
    if (this->_signal) return true;
    if (!this->_listen && !this->_queue.size()) return true;
    return false;

    );
    queue_size = _queue.size();


    if (!queue_size)

    std::lock_guard<std::mutex> lock(_mutex);
    _signal = false;


    _condition.notify_one();
    continue;


    int socket_in = 0;

    std::lock_guard<std::mutex> lock(_mutex);
    socket_in = _queue[0];
    _queue.erase(_queue.begin());


    Request* request = new Request(socket_in);
    Response* response = new Response(socket_in);

    int status = _doProcessRequest(request, response);

    delete request;
    delete response;
    close(socket_in);



    bool Server::_doProcessRequest(Request* request, Response* response)
    if (!request->isValid())
    response->doSendError(HttpStatus::Code::BadRequest, "Invalid request.");
    return false;


    std::string path = request->getPath();
    Struct::Methods method = request->getMethod();

    Routes::Route route;
    if (!(_routes->getRoute(path, method, route) && route.isValid()))
    response->doSendError(HttpStatus::Code::Forbidden, "Path invalid/not found.");
    return false;


    if (route.method != method)
    response->doSendError(HttpStatus::Code::MethodNotAllowed, "Method invalid/not found.");
    return false;


    void (*callback)(Request*, Response*) = route.callback;
    callback(request, response);

    if (!response->isSent())
    response->doSendError(HttpStatus::Code::ServiceUnavailable, "Resource was not found or can't respond now.");


    return true;



    Request (parsing) and Response (sending)




    Request



    #pragma once

    #include <unistd.h>
    #include <string.h>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <unordered_map>
    #include <regex>
    #include <json.hpp>

    #include "Server/Struct.h"

    using json = nlohmann::json;

    class Request
    public:
    Request(int socket, unsigned int buffer_size = 1024);
    ~Request();

    bool isValid();
    std::string getPath() return _attributes.path;
    Struct::Methods getMethod() return _attributes.method;
    std::unordered_map<std::string, std::string> getHeaders() return _attributes.headers;
    std::string getHeader(std::string header) return _attributes.headers[header];
    json getBody() return _attributes.body;

    private:
    int _socket;
    unsigned int _buffer_size;
    std::string _data;
    Struct::Attributes _attributes;
    bool _status;

    std::string _doReceiveData(int sock_in);
    bool _doParseData(std::string data, Struct::Attributes& attributes);
    std::vector<std::string> _doSplitText(std::string text, std::string delimiter);
    std::vector<std::string> _doSplitText(std::string text, std::string delimiter, int lock);
    ;



    Request::Request(int socket, unsigned int buffer_size) 
    _socket = socket;
    _buffer_size = buffer_size;
    _status = false;

    _data = _doReceiveData(_socket);
    if (!_data.length()) return;

    bool result;
    result = _doParseData(_data, _attributes);

    if (!result) return;
    if (!_attributes.isValidRequest()) return;

    _status = true;


    Request::~Request()



    bool Request::isValid()
    return _status;


    std::string Request::_doReceiveData(int sock_in)
    char* buffer = new char[_buffer_size];
    memset(buffer, '', _buffer_size);
    read(sock_in, buffer, _buffer_size);

    std::string data;
    data.assign(buffer);
    delete[] buffer;
    return data;


    bool Request::_doParseData(std::string data, Struct::Attributes& attributes)
    std::string delimiter = "rn";
    std::vector<std::string> rows = _doSplitText(data, delimiter);
    if (!rows.size()) return false;

    std::string header = rows[0];
    rows.erase(rows.begin());

    if (!header.length()) return false;

    std::vector<std::string> parsed_header = _doSplitText(header, std::string(" "));
    if (parsed_header.size() < 2) return false;

    Struct::Methods method = Struct::doParseHttpMethod(parsed_header[0]);
    if (method == Struct::Methods::NONE) return false;

    std::string path = parsed_header[1];

    std::unordered_map<std::string, std::string> headers;
    for (size_t i = 0; i < rows.size(); i++)
    std::string row = rows[i];
    delimiter = ":";

    std::vector<std::string> splited = _doSplitText(row, delimiter, true);
    if (splited.size() != 2) continue;

    headers[splited[0]] = splited[1];


    _attributes.method = method;
    _attributes.path = path;
    _attributes.headers = headers;

    std::string content_length = headers["Content-Length"];
    int content_size = 0;

    if (content_size = atoi(content_length.c_str()))
    std::string body = data.substr(data.length() - content_size, data.length());
    json parsed_body = json::parse(body, nullptr, false);
    if (parsed_body != NULL && !parsed_body.is_discarded()) _attributes.body = parsed_body;


    return true;


    std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter)
    std::vector<std::string> result;
    int delimiter_length = delimiter.length();

    std::string block;
    std::string region;
    int index = 0;

    for (size_t i = 0; i < text.length(); i++)
    block = text.substr(i, delimiter_length);
    if (block.length() != delimiter_length) continue;

    if (block == delimiter)
    region = text.substr(index, i - index);
    result.push_back(region);
    index = i + delimiter_length;



    return result;


    std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter, int lock)
    ...



    Response



    #pragma once

    #include <unistd.h>
    #include <iostream>
    #include <string>
    #include <unordered_map>

    #include "Server/Struct.h"

    class Response
    public:
    Response(int socket_in);
    ~Response();

    bool isSent() return _sent;
    void setCode(HttpStatus::Code code) _attributes.code = code;
    void setHeader(std::string key, std::string value) _attributes.headers[key] = value;
    void setBody(json body) _attributes.body = body;
    void doClearHeaders() _attributes.headers.clear();
    void doClearBody() _attributes.body = json::value_t::object;

    bool doSendSuccess();
    bool doSendError(HttpStatus::Code code, const std::string& message);

    private:
    int _socket;
    bool _sent;

    Struct::Attributes _attributes;

    bool _doSendPayload();
    bool _doCreatePayload(std::string& payload);
    ;



    #include "Response.h"

    Response::Response(int socket_in)
    _socket = socket_in;
    _sent = false;


    Response::~Response()



    bool Response::doSendSuccess()
    setCode(HttpStatus::Code::OK);
    setHeader("Connection", "Closed");
    return _doSendPayload();


    bool Response::doSendError(HttpStatus::Code code, const std::string& message)
    setCode(code);
    doClearHeaders();
    doClearBody();

    setHeader("Connection", "Closed");

    json body;
    body["error"] = ;
    body["error"]["code"] = code;
    body["error"]["message"] = message;

    setBody(body);
    return _doSendPayload();


    bool Response::_doSendPayload()
    if (_sent) return false;

    int status;

    setHeader("Server", "Dark");
    setHeader("Content-Type", "application/json");

    std::string payload;
    status = _doCreatePayload(payload);
    if (!status) return false;

    status = write(_socket, payload.c_str(), payload.size());
    if (status < 1) return false;

    _sent = true;
    return true;


    bool Response::_doCreatePayload(std::string& payload)
    std::string current_payload;
    std::string data = _attributes.body.dump(4);

    int data_length = data.size();

    if (data_length)
    _attributes.headers["Content-Length"] = std::to_string(data_length);


    current_payload += _attributes.version + " " + std::to_string((int) _attributes.code) + " " + HttpStatus::getReasonPhrase(_attributes.code) + "rn";

    std::unordered_map<std::string, std::string>::iterator iterator;
    for (iterator = _attributes.headers.begin(); iterator != _attributes.headers.end(); iterator++)
    std::string key = iterator->first;
    std::string value = iterator->second;

    current_payload += key + ": " + value + "rn";


    if (data_length) current_payload += "rn" + data + "rnrn";

    payload = current_payload;
    return true;



    Routes




    #pragma once

    #include <iostream>
    #include <string>
    #include <vector>

    #include "Server/Request.h"
    #include "Server/Response.h"

    class Routes
    public:
    Routes();
    ~Routes();

    struct Route
    std::string path;
    Struct::Methods method;
    void (*callback)(Request*, Response*);

    bool isValid() method > Struct::Methods::LAST) return false;
    if (callback == nullptr) return false;
    return true;

    ;

    bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
    bool getRoute(std::string path, Struct::Methods method, Route& route);

    private:
    std::vector<Route> _routes;

    int _getRouteIndex(std::string path, Struct::Methods method);
    ;



    #include "Routes.h"

    Routes::Routes()



    Routes::~Routes()



    bool Routes::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))

    bool Routes::getRoute(std::string path, Struct::Methods method, Routes::Route& route)
    int index = _getRouteIndex(path, method);
    if (index < 0) return false;
    route = _routes[index];
    return true;


    int Routes::_getRouteIndex(std::string path, Struct::Methods method)
    for (size_t i = 0; i < _routes.size(); i++)
    Route* route = &_routes[i];
    if (route->path == path && route->method == method)
    return i;


    return -1;



    Struct and StatusCode




    StatusCode




    https://github.com/j-ulrich/http-status-codes-cpp




    Struct



    #pragma once

    #include <json.hpp>

    #include "Server/StatusCode.h"

    using json = nlohmann::json;

    class Struct
    public:
    enum class Methods
    NONE = 0, GET = 1, POST = 2, FIRST = GET, LAST = POST
    ;

    struct Attributes
    const std::string version = "HTTP/1.1";

    std::string path;
    Methods method;
    HttpStatus::Code code;
    std::unordered_map<std::string, std::string> headers;
    json body;

    Attributes()
    code = HttpStatus::Code::InternalServerError;
    body = json::value_t::object;


    bool isValidRequest()
    if (!path.length()) return false;
    if (method < Methods::FIRST

    bool isValidResponse()
    if (!headers.size()) return false;
    return true;

    ;

    static Methods doParseHttpMethod(std::string value)
    Methods target = Methods::NONE;

    if (value == "GET") target = Methods::GET;
    if (value == "POST") target = Methods::POST;

    return target;


    private:
    ;


    Main (usage):




    #include "Server/Server.h"

    void exec(Request* request, Response* response)
    json body;

    body["foo"] = 123;
    body["bar"] = true;

    response->setBody(body);
    response->doSendSuccess();


    int main(int argc, char* argv[])
    Server* server = new Server(5000);
    server->setRoute("/getStatus", Struct::Methods::GET, exec);
    server->setRoute("/postStatus", Struct::Methods::POST, exec);
    server->doListen();

    // let threads live for some time before they eternaly be gone
    /*
    actually I'm stuck with this sleep, I don't know how to hold
    this until caller call doStop() without using while and
    consuming process power
    */
    sleep(30);

    delete server;
    return 1;




    Basically I'm mirroring NodeJS express API Usage:
    server.setRoute(path, route, callback);




    So, what can be done to improve my code in terms of optimization and efficiency?



    Thanks in advance.










    share|improve this question









    New contributor




    Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      5












      5








      5





      $begingroup$


      I'm working on a project (studying) and I have a lot of time for proof of concepts and write something from scratch.



      Basically I'm creating a Http Server (simple, but not too simple) in C++ using sockets and multi-threading.



      There are two topics that I'm concerned about: the design pattern of my code structure and the efficiency of my thread implementation. Obs: a little worried about C++ best practices, since I'm diving too fast into C++ (Am I abusing of std items?, since this require a low-level implementation).



      Server is the main file, that calls Routes, Request and Response. Request and Response contains Struct (that contains Status Code). And Routes contains Request and Response.





      IMPORTANT: I'm using nlohmann/json (https://github.com/nlohmann/json) and j-ulrich status-codes (https://github.com/j-ulrich/http-status-codes-cpp).



      Server (Accepting Sockets and Multi-threading):




      #pragma once

      #include <unistd.h>
      #include <stdio.h>
      #include <sys/socket.h>
      #include <stdlib.h>
      #include <netinet/in.h>
      #include <string.h>
      #include <iostream>
      #include <unordered_map>
      #include <thread>
      #include <mutex>
      #include <condition_variable>

      #include "Server/Request.h"
      #include "Server/Response.h"
      #include "Server/Struct.h"
      #include "Server/Routes.h"
      #include "Tools/Logger.h"

      class Server
      public:
      Server(unsigned int port, unsigned int max_connections = 64, unsigned int thread_count = 5);
      ~Server();

      bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
      bool doListen();
      bool doStop();

      private:
      unsigned int _port;
      unsigned int _max_connections;
      unsigned int _thread_count;

      std::mutex _mutex;
      std::condition_variable _condition;

      bool _signal;

      std::vector<unsigned int> _queue;

      std::thread* _thread_consume;
      std::thread* _thread_process;

      Routes* _routes;

      int _socket;
      struct sockaddr_in _address;
      bool _listen;

      bool _doStop();
      bool _doCreateSocket(int& socket_in);
      bool _doBindSocket(int file_descriptor);
      void _doConsumeSocket();
      void _doProcessSocket(int id);
      bool _doProcessRequest(Request* request, Response* response);
      ;



      #include "Server/Server.h"

      Server::Server(unsigned int port, unsigned int max_connections, unsigned int thread_count)
      if (port > 65535)
      Logger::doSendMessage(Logger::TYPES::ERROR, "[Port must be something between 0 and 65535 on Server::Constructor.");


      if (max_connections < 1)
      Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


      _port = port;
      _max_connections = max_connections;
      _thread_count = thread_count;

      _routes = new Routes();

      int status = _doCreateSocket(_socket);
      if (!status)
      Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


      if (!_doBindSocket(_socket))
      Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");
      ;

      _signal = false;
      _listen = false;


      Server::~Server()
      _doStop();

      shutdown(_socket, SHUT_RD);
      close(_socket);

      try
      _thread_consume->join();
      for (size_t i = 0; i < _thread_count; i++)
      _thread_process[i].join();

      catch (...)

      delete _thread_consume;
      delete[] _thread_process;
      delete _routes;


      bool Server::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))
      return _routes->setRoute(path, method, callback);


      bool Server::doListen()
      if (_listen) return false;

      int status;

      status = listen(_socket, _max_connections);
      if (status < 0) return false;

      Logger::doSendMessage(Logger::TYPES::INFO, "Server running with success at port " + std::to_string(_port) + ".");

      _listen = true;

      _thread_consume = new std::thread(&Server::_doConsumeSocket, this);
      _thread_process = new std::thread[_thread_count];
      for (size_t i = 0; i < _thread_count; i++)
      _thread_process[i] = std::thread(&Server::_doProcessSocket, this, i);


      return true;


      bool Server::doStop()
      return _doStop();


      bool Server::_doStop()
      if (!_listen) return false;

      std::lock_guard<std::mutex> lock(_mutex);
      _listen = false;

      _condition.notify_one();
      return true;


      bool Server::_doCreateSocket(int& socket_in) SO_REUSEPORT, &opt, sizeof(opt));
      if (error) return false;

      socket_in = file_descriptor;
      return true;


      bool Server::_doBindSocket(int file_descriptor)
      if (!file_descriptor) return false;

      _address.sin_family = AF_INET;
      _address.sin_addr.s_addr = INADDR_ANY;
      _address.sin_port = htons(_port);

      int status;

      status = bind(file_descriptor, (struct sockaddr*) &_address, sizeof(_address));
      if (status < 0) return false;

      return true;


      void Server::_doConsumeSocket()
      int socket_in;
      int address_size = sizeof(_address);

      while (_listen)
      socket_in = accept(_socket, (struct sockaddr*) &_address, (socklen_t*) &address_size);
      if (socket_in < 0) continue;


      std::lock_guard<std::mutex> lock(_mutex);
      _queue.push_back(socket_in);
      _signal = true;


      _condition.notify_one();



      void Server::_doProcessSocket(int id)
      while (_listen)
      int queue_size = 0;


      std::unique_lock<std::mutex> lock(_mutex);
      _condition.wait(lock,
      [this]
      if (this->_signal) return true;
      if (!this->_listen && !this->_queue.size()) return true;
      return false;

      );
      queue_size = _queue.size();


      if (!queue_size)

      std::lock_guard<std::mutex> lock(_mutex);
      _signal = false;


      _condition.notify_one();
      continue;


      int socket_in = 0;

      std::lock_guard<std::mutex> lock(_mutex);
      socket_in = _queue[0];
      _queue.erase(_queue.begin());


      Request* request = new Request(socket_in);
      Response* response = new Response(socket_in);

      int status = _doProcessRequest(request, response);

      delete request;
      delete response;
      close(socket_in);



      bool Server::_doProcessRequest(Request* request, Response* response)
      if (!request->isValid())
      response->doSendError(HttpStatus::Code::BadRequest, "Invalid request.");
      return false;


      std::string path = request->getPath();
      Struct::Methods method = request->getMethod();

      Routes::Route route;
      if (!(_routes->getRoute(path, method, route) && route.isValid()))
      response->doSendError(HttpStatus::Code::Forbidden, "Path invalid/not found.");
      return false;


      if (route.method != method)
      response->doSendError(HttpStatus::Code::MethodNotAllowed, "Method invalid/not found.");
      return false;


      void (*callback)(Request*, Response*) = route.callback;
      callback(request, response);

      if (!response->isSent())
      response->doSendError(HttpStatus::Code::ServiceUnavailable, "Resource was not found or can't respond now.");


      return true;



      Request (parsing) and Response (sending)




      Request



      #pragma once

      #include <unistd.h>
      #include <string.h>
      #include <iostream>
      #include <string>
      #include <vector>
      #include <unordered_map>
      #include <regex>
      #include <json.hpp>

      #include "Server/Struct.h"

      using json = nlohmann::json;

      class Request
      public:
      Request(int socket, unsigned int buffer_size = 1024);
      ~Request();

      bool isValid();
      std::string getPath() return _attributes.path;
      Struct::Methods getMethod() return _attributes.method;
      std::unordered_map<std::string, std::string> getHeaders() return _attributes.headers;
      std::string getHeader(std::string header) return _attributes.headers[header];
      json getBody() return _attributes.body;

      private:
      int _socket;
      unsigned int _buffer_size;
      std::string _data;
      Struct::Attributes _attributes;
      bool _status;

      std::string _doReceiveData(int sock_in);
      bool _doParseData(std::string data, Struct::Attributes& attributes);
      std::vector<std::string> _doSplitText(std::string text, std::string delimiter);
      std::vector<std::string> _doSplitText(std::string text, std::string delimiter, int lock);
      ;



      Request::Request(int socket, unsigned int buffer_size) 
      _socket = socket;
      _buffer_size = buffer_size;
      _status = false;

      _data = _doReceiveData(_socket);
      if (!_data.length()) return;

      bool result;
      result = _doParseData(_data, _attributes);

      if (!result) return;
      if (!_attributes.isValidRequest()) return;

      _status = true;


      Request::~Request()



      bool Request::isValid()
      return _status;


      std::string Request::_doReceiveData(int sock_in)
      char* buffer = new char[_buffer_size];
      memset(buffer, '', _buffer_size);
      read(sock_in, buffer, _buffer_size);

      std::string data;
      data.assign(buffer);
      delete[] buffer;
      return data;


      bool Request::_doParseData(std::string data, Struct::Attributes& attributes)
      std::string delimiter = "rn";
      std::vector<std::string> rows = _doSplitText(data, delimiter);
      if (!rows.size()) return false;

      std::string header = rows[0];
      rows.erase(rows.begin());

      if (!header.length()) return false;

      std::vector<std::string> parsed_header = _doSplitText(header, std::string(" "));
      if (parsed_header.size() < 2) return false;

      Struct::Methods method = Struct::doParseHttpMethod(parsed_header[0]);
      if (method == Struct::Methods::NONE) return false;

      std::string path = parsed_header[1];

      std::unordered_map<std::string, std::string> headers;
      for (size_t i = 0; i < rows.size(); i++)
      std::string row = rows[i];
      delimiter = ":";

      std::vector<std::string> splited = _doSplitText(row, delimiter, true);
      if (splited.size() != 2) continue;

      headers[splited[0]] = splited[1];


      _attributes.method = method;
      _attributes.path = path;
      _attributes.headers = headers;

      std::string content_length = headers["Content-Length"];
      int content_size = 0;

      if (content_size = atoi(content_length.c_str()))
      std::string body = data.substr(data.length() - content_size, data.length());
      json parsed_body = json::parse(body, nullptr, false);
      if (parsed_body != NULL && !parsed_body.is_discarded()) _attributes.body = parsed_body;


      return true;


      std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter)
      std::vector<std::string> result;
      int delimiter_length = delimiter.length();

      std::string block;
      std::string region;
      int index = 0;

      for (size_t i = 0; i < text.length(); i++)
      block = text.substr(i, delimiter_length);
      if (block.length() != delimiter_length) continue;

      if (block == delimiter)
      region = text.substr(index, i - index);
      result.push_back(region);
      index = i + delimiter_length;



      return result;


      std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter, int lock)
      ...



      Response



      #pragma once

      #include <unistd.h>
      #include <iostream>
      #include <string>
      #include <unordered_map>

      #include "Server/Struct.h"

      class Response
      public:
      Response(int socket_in);
      ~Response();

      bool isSent() return _sent;
      void setCode(HttpStatus::Code code) _attributes.code = code;
      void setHeader(std::string key, std::string value) _attributes.headers[key] = value;
      void setBody(json body) _attributes.body = body;
      void doClearHeaders() _attributes.headers.clear();
      void doClearBody() _attributes.body = json::value_t::object;

      bool doSendSuccess();
      bool doSendError(HttpStatus::Code code, const std::string& message);

      private:
      int _socket;
      bool _sent;

      Struct::Attributes _attributes;

      bool _doSendPayload();
      bool _doCreatePayload(std::string& payload);
      ;



      #include "Response.h"

      Response::Response(int socket_in)
      _socket = socket_in;
      _sent = false;


      Response::~Response()



      bool Response::doSendSuccess()
      setCode(HttpStatus::Code::OK);
      setHeader("Connection", "Closed");
      return _doSendPayload();


      bool Response::doSendError(HttpStatus::Code code, const std::string& message)
      setCode(code);
      doClearHeaders();
      doClearBody();

      setHeader("Connection", "Closed");

      json body;
      body["error"] = ;
      body["error"]["code"] = code;
      body["error"]["message"] = message;

      setBody(body);
      return _doSendPayload();


      bool Response::_doSendPayload()
      if (_sent) return false;

      int status;

      setHeader("Server", "Dark");
      setHeader("Content-Type", "application/json");

      std::string payload;
      status = _doCreatePayload(payload);
      if (!status) return false;

      status = write(_socket, payload.c_str(), payload.size());
      if (status < 1) return false;

      _sent = true;
      return true;


      bool Response::_doCreatePayload(std::string& payload)
      std::string current_payload;
      std::string data = _attributes.body.dump(4);

      int data_length = data.size();

      if (data_length)
      _attributes.headers["Content-Length"] = std::to_string(data_length);


      current_payload += _attributes.version + " " + std::to_string((int) _attributes.code) + " " + HttpStatus::getReasonPhrase(_attributes.code) + "rn";

      std::unordered_map<std::string, std::string>::iterator iterator;
      for (iterator = _attributes.headers.begin(); iterator != _attributes.headers.end(); iterator++)
      std::string key = iterator->first;
      std::string value = iterator->second;

      current_payload += key + ": " + value + "rn";


      if (data_length) current_payload += "rn" + data + "rnrn";

      payload = current_payload;
      return true;



      Routes




      #pragma once

      #include <iostream>
      #include <string>
      #include <vector>

      #include "Server/Request.h"
      #include "Server/Response.h"

      class Routes
      public:
      Routes();
      ~Routes();

      struct Route
      std::string path;
      Struct::Methods method;
      void (*callback)(Request*, Response*);

      bool isValid() method > Struct::Methods::LAST) return false;
      if (callback == nullptr) return false;
      return true;

      ;

      bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
      bool getRoute(std::string path, Struct::Methods method, Route& route);

      private:
      std::vector<Route> _routes;

      int _getRouteIndex(std::string path, Struct::Methods method);
      ;



      #include "Routes.h"

      Routes::Routes()



      Routes::~Routes()



      bool Routes::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))

      bool Routes::getRoute(std::string path, Struct::Methods method, Routes::Route& route)
      int index = _getRouteIndex(path, method);
      if (index < 0) return false;
      route = _routes[index];
      return true;


      int Routes::_getRouteIndex(std::string path, Struct::Methods method)
      for (size_t i = 0; i < _routes.size(); i++)
      Route* route = &_routes[i];
      if (route->path == path && route->method == method)
      return i;


      return -1;



      Struct and StatusCode




      StatusCode




      https://github.com/j-ulrich/http-status-codes-cpp




      Struct



      #pragma once

      #include <json.hpp>

      #include "Server/StatusCode.h"

      using json = nlohmann::json;

      class Struct
      public:
      enum class Methods
      NONE = 0, GET = 1, POST = 2, FIRST = GET, LAST = POST
      ;

      struct Attributes
      const std::string version = "HTTP/1.1";

      std::string path;
      Methods method;
      HttpStatus::Code code;
      std::unordered_map<std::string, std::string> headers;
      json body;

      Attributes()
      code = HttpStatus::Code::InternalServerError;
      body = json::value_t::object;


      bool isValidRequest()
      if (!path.length()) return false;
      if (method < Methods::FIRST

      bool isValidResponse()
      if (!headers.size()) return false;
      return true;

      ;

      static Methods doParseHttpMethod(std::string value)
      Methods target = Methods::NONE;

      if (value == "GET") target = Methods::GET;
      if (value == "POST") target = Methods::POST;

      return target;


      private:
      ;


      Main (usage):




      #include "Server/Server.h"

      void exec(Request* request, Response* response)
      json body;

      body["foo"] = 123;
      body["bar"] = true;

      response->setBody(body);
      response->doSendSuccess();


      int main(int argc, char* argv[])
      Server* server = new Server(5000);
      server->setRoute("/getStatus", Struct::Methods::GET, exec);
      server->setRoute("/postStatus", Struct::Methods::POST, exec);
      server->doListen();

      // let threads live for some time before they eternaly be gone
      /*
      actually I'm stuck with this sleep, I don't know how to hold
      this until caller call doStop() without using while and
      consuming process power
      */
      sleep(30);

      delete server;
      return 1;




      Basically I'm mirroring NodeJS express API Usage:
      server.setRoute(path, route, callback);




      So, what can be done to improve my code in terms of optimization and efficiency?



      Thanks in advance.










      share|improve this question









      New contributor




      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm working on a project (studying) and I have a lot of time for proof of concepts and write something from scratch.



      Basically I'm creating a Http Server (simple, but not too simple) in C++ using sockets and multi-threading.



      There are two topics that I'm concerned about: the design pattern of my code structure and the efficiency of my thread implementation. Obs: a little worried about C++ best practices, since I'm diving too fast into C++ (Am I abusing of std items?, since this require a low-level implementation).



      Server is the main file, that calls Routes, Request and Response. Request and Response contains Struct (that contains Status Code). And Routes contains Request and Response.





      IMPORTANT: I'm using nlohmann/json (https://github.com/nlohmann/json) and j-ulrich status-codes (https://github.com/j-ulrich/http-status-codes-cpp).



      Server (Accepting Sockets and Multi-threading):




      #pragma once

      #include <unistd.h>
      #include <stdio.h>
      #include <sys/socket.h>
      #include <stdlib.h>
      #include <netinet/in.h>
      #include <string.h>
      #include <iostream>
      #include <unordered_map>
      #include <thread>
      #include <mutex>
      #include <condition_variable>

      #include "Server/Request.h"
      #include "Server/Response.h"
      #include "Server/Struct.h"
      #include "Server/Routes.h"
      #include "Tools/Logger.h"

      class Server
      public:
      Server(unsigned int port, unsigned int max_connections = 64, unsigned int thread_count = 5);
      ~Server();

      bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
      bool doListen();
      bool doStop();

      private:
      unsigned int _port;
      unsigned int _max_connections;
      unsigned int _thread_count;

      std::mutex _mutex;
      std::condition_variable _condition;

      bool _signal;

      std::vector<unsigned int> _queue;

      std::thread* _thread_consume;
      std::thread* _thread_process;

      Routes* _routes;

      int _socket;
      struct sockaddr_in _address;
      bool _listen;

      bool _doStop();
      bool _doCreateSocket(int& socket_in);
      bool _doBindSocket(int file_descriptor);
      void _doConsumeSocket();
      void _doProcessSocket(int id);
      bool _doProcessRequest(Request* request, Response* response);
      ;



      #include "Server/Server.h"

      Server::Server(unsigned int port, unsigned int max_connections, unsigned int thread_count)
      if (port > 65535)
      Logger::doSendMessage(Logger::TYPES::ERROR, "[Port must be something between 0 and 65535 on Server::Constructor.");


      if (max_connections < 1)
      Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


      _port = port;
      _max_connections = max_connections;
      _thread_count = thread_count;

      _routes = new Routes();

      int status = _doCreateSocket(_socket);
      if (!status)
      Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


      if (!_doBindSocket(_socket))
      Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");
      ;

      _signal = false;
      _listen = false;


      Server::~Server()
      _doStop();

      shutdown(_socket, SHUT_RD);
      close(_socket);

      try
      _thread_consume->join();
      for (size_t i = 0; i < _thread_count; i++)
      _thread_process[i].join();

      catch (...)

      delete _thread_consume;
      delete[] _thread_process;
      delete _routes;


      bool Server::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))
      return _routes->setRoute(path, method, callback);


      bool Server::doListen()
      if (_listen) return false;

      int status;

      status = listen(_socket, _max_connections);
      if (status < 0) return false;

      Logger::doSendMessage(Logger::TYPES::INFO, "Server running with success at port " + std::to_string(_port) + ".");

      _listen = true;

      _thread_consume = new std::thread(&Server::_doConsumeSocket, this);
      _thread_process = new std::thread[_thread_count];
      for (size_t i = 0; i < _thread_count; i++)
      _thread_process[i] = std::thread(&Server::_doProcessSocket, this, i);


      return true;


      bool Server::doStop()
      return _doStop();


      bool Server::_doStop()
      if (!_listen) return false;

      std::lock_guard<std::mutex> lock(_mutex);
      _listen = false;

      _condition.notify_one();
      return true;


      bool Server::_doCreateSocket(int& socket_in) SO_REUSEPORT, &opt, sizeof(opt));
      if (error) return false;

      socket_in = file_descriptor;
      return true;


      bool Server::_doBindSocket(int file_descriptor)
      if (!file_descriptor) return false;

      _address.sin_family = AF_INET;
      _address.sin_addr.s_addr = INADDR_ANY;
      _address.sin_port = htons(_port);

      int status;

      status = bind(file_descriptor, (struct sockaddr*) &_address, sizeof(_address));
      if (status < 0) return false;

      return true;


      void Server::_doConsumeSocket()
      int socket_in;
      int address_size = sizeof(_address);

      while (_listen)
      socket_in = accept(_socket, (struct sockaddr*) &_address, (socklen_t*) &address_size);
      if (socket_in < 0) continue;


      std::lock_guard<std::mutex> lock(_mutex);
      _queue.push_back(socket_in);
      _signal = true;


      _condition.notify_one();



      void Server::_doProcessSocket(int id)
      while (_listen)
      int queue_size = 0;


      std::unique_lock<std::mutex> lock(_mutex);
      _condition.wait(lock,
      [this]
      if (this->_signal) return true;
      if (!this->_listen && !this->_queue.size()) return true;
      return false;

      );
      queue_size = _queue.size();


      if (!queue_size)

      std::lock_guard<std::mutex> lock(_mutex);
      _signal = false;


      _condition.notify_one();
      continue;


      int socket_in = 0;

      std::lock_guard<std::mutex> lock(_mutex);
      socket_in = _queue[0];
      _queue.erase(_queue.begin());


      Request* request = new Request(socket_in);
      Response* response = new Response(socket_in);

      int status = _doProcessRequest(request, response);

      delete request;
      delete response;
      close(socket_in);



      bool Server::_doProcessRequest(Request* request, Response* response)
      if (!request->isValid())
      response->doSendError(HttpStatus::Code::BadRequest, "Invalid request.");
      return false;


      std::string path = request->getPath();
      Struct::Methods method = request->getMethod();

      Routes::Route route;
      if (!(_routes->getRoute(path, method, route) && route.isValid()))
      response->doSendError(HttpStatus::Code::Forbidden, "Path invalid/not found.");
      return false;


      if (route.method != method)
      response->doSendError(HttpStatus::Code::MethodNotAllowed, "Method invalid/not found.");
      return false;


      void (*callback)(Request*, Response*) = route.callback;
      callback(request, response);

      if (!response->isSent())
      response->doSendError(HttpStatus::Code::ServiceUnavailable, "Resource was not found or can't respond now.");


      return true;



      Request (parsing) and Response (sending)




      Request



      #pragma once

      #include <unistd.h>
      #include <string.h>
      #include <iostream>
      #include <string>
      #include <vector>
      #include <unordered_map>
      #include <regex>
      #include <json.hpp>

      #include "Server/Struct.h"

      using json = nlohmann::json;

      class Request
      public:
      Request(int socket, unsigned int buffer_size = 1024);
      ~Request();

      bool isValid();
      std::string getPath() return _attributes.path;
      Struct::Methods getMethod() return _attributes.method;
      std::unordered_map<std::string, std::string> getHeaders() return _attributes.headers;
      std::string getHeader(std::string header) return _attributes.headers[header];
      json getBody() return _attributes.body;

      private:
      int _socket;
      unsigned int _buffer_size;
      std::string _data;
      Struct::Attributes _attributes;
      bool _status;

      std::string _doReceiveData(int sock_in);
      bool _doParseData(std::string data, Struct::Attributes& attributes);
      std::vector<std::string> _doSplitText(std::string text, std::string delimiter);
      std::vector<std::string> _doSplitText(std::string text, std::string delimiter, int lock);
      ;



      Request::Request(int socket, unsigned int buffer_size) 
      _socket = socket;
      _buffer_size = buffer_size;
      _status = false;

      _data = _doReceiveData(_socket);
      if (!_data.length()) return;

      bool result;
      result = _doParseData(_data, _attributes);

      if (!result) return;
      if (!_attributes.isValidRequest()) return;

      _status = true;


      Request::~Request()



      bool Request::isValid()
      return _status;


      std::string Request::_doReceiveData(int sock_in)
      char* buffer = new char[_buffer_size];
      memset(buffer, '', _buffer_size);
      read(sock_in, buffer, _buffer_size);

      std::string data;
      data.assign(buffer);
      delete[] buffer;
      return data;


      bool Request::_doParseData(std::string data, Struct::Attributes& attributes)
      std::string delimiter = "rn";
      std::vector<std::string> rows = _doSplitText(data, delimiter);
      if (!rows.size()) return false;

      std::string header = rows[0];
      rows.erase(rows.begin());

      if (!header.length()) return false;

      std::vector<std::string> parsed_header = _doSplitText(header, std::string(" "));
      if (parsed_header.size() < 2) return false;

      Struct::Methods method = Struct::doParseHttpMethod(parsed_header[0]);
      if (method == Struct::Methods::NONE) return false;

      std::string path = parsed_header[1];

      std::unordered_map<std::string, std::string> headers;
      for (size_t i = 0; i < rows.size(); i++)
      std::string row = rows[i];
      delimiter = ":";

      std::vector<std::string> splited = _doSplitText(row, delimiter, true);
      if (splited.size() != 2) continue;

      headers[splited[0]] = splited[1];


      _attributes.method = method;
      _attributes.path = path;
      _attributes.headers = headers;

      std::string content_length = headers["Content-Length"];
      int content_size = 0;

      if (content_size = atoi(content_length.c_str()))
      std::string body = data.substr(data.length() - content_size, data.length());
      json parsed_body = json::parse(body, nullptr, false);
      if (parsed_body != NULL && !parsed_body.is_discarded()) _attributes.body = parsed_body;


      return true;


      std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter)
      std::vector<std::string> result;
      int delimiter_length = delimiter.length();

      std::string block;
      std::string region;
      int index = 0;

      for (size_t i = 0; i < text.length(); i++)
      block = text.substr(i, delimiter_length);
      if (block.length() != delimiter_length) continue;

      if (block == delimiter)
      region = text.substr(index, i - index);
      result.push_back(region);
      index = i + delimiter_length;



      return result;


      std::vector<std::string> Request::_doSplitText(std::string text, std::string delimiter, int lock)
      ...



      Response



      #pragma once

      #include <unistd.h>
      #include <iostream>
      #include <string>
      #include <unordered_map>

      #include "Server/Struct.h"

      class Response
      public:
      Response(int socket_in);
      ~Response();

      bool isSent() return _sent;
      void setCode(HttpStatus::Code code) _attributes.code = code;
      void setHeader(std::string key, std::string value) _attributes.headers[key] = value;
      void setBody(json body) _attributes.body = body;
      void doClearHeaders() _attributes.headers.clear();
      void doClearBody() _attributes.body = json::value_t::object;

      bool doSendSuccess();
      bool doSendError(HttpStatus::Code code, const std::string& message);

      private:
      int _socket;
      bool _sent;

      Struct::Attributes _attributes;

      bool _doSendPayload();
      bool _doCreatePayload(std::string& payload);
      ;



      #include "Response.h"

      Response::Response(int socket_in)
      _socket = socket_in;
      _sent = false;


      Response::~Response()



      bool Response::doSendSuccess()
      setCode(HttpStatus::Code::OK);
      setHeader("Connection", "Closed");
      return _doSendPayload();


      bool Response::doSendError(HttpStatus::Code code, const std::string& message)
      setCode(code);
      doClearHeaders();
      doClearBody();

      setHeader("Connection", "Closed");

      json body;
      body["error"] = ;
      body["error"]["code"] = code;
      body["error"]["message"] = message;

      setBody(body);
      return _doSendPayload();


      bool Response::_doSendPayload()
      if (_sent) return false;

      int status;

      setHeader("Server", "Dark");
      setHeader("Content-Type", "application/json");

      std::string payload;
      status = _doCreatePayload(payload);
      if (!status) return false;

      status = write(_socket, payload.c_str(), payload.size());
      if (status < 1) return false;

      _sent = true;
      return true;


      bool Response::_doCreatePayload(std::string& payload)
      std::string current_payload;
      std::string data = _attributes.body.dump(4);

      int data_length = data.size();

      if (data_length)
      _attributes.headers["Content-Length"] = std::to_string(data_length);


      current_payload += _attributes.version + " " + std::to_string((int) _attributes.code) + " " + HttpStatus::getReasonPhrase(_attributes.code) + "rn";

      std::unordered_map<std::string, std::string>::iterator iterator;
      for (iterator = _attributes.headers.begin(); iterator != _attributes.headers.end(); iterator++)
      std::string key = iterator->first;
      std::string value = iterator->second;

      current_payload += key + ": " + value + "rn";


      if (data_length) current_payload += "rn" + data + "rnrn";

      payload = current_payload;
      return true;



      Routes




      #pragma once

      #include <iostream>
      #include <string>
      #include <vector>

      #include "Server/Request.h"
      #include "Server/Response.h"

      class Routes
      public:
      Routes();
      ~Routes();

      struct Route
      std::string path;
      Struct::Methods method;
      void (*callback)(Request*, Response*);

      bool isValid() method > Struct::Methods::LAST) return false;
      if (callback == nullptr) return false;
      return true;

      ;

      bool setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*));
      bool getRoute(std::string path, Struct::Methods method, Route& route);

      private:
      std::vector<Route> _routes;

      int _getRouteIndex(std::string path, Struct::Methods method);
      ;



      #include "Routes.h"

      Routes::Routes()



      Routes::~Routes()



      bool Routes::setRoute(std::string path, Struct::Methods method, void (*callback)(Request*, Response*))

      bool Routes::getRoute(std::string path, Struct::Methods method, Routes::Route& route)
      int index = _getRouteIndex(path, method);
      if (index < 0) return false;
      route = _routes[index];
      return true;


      int Routes::_getRouteIndex(std::string path, Struct::Methods method)
      for (size_t i = 0; i < _routes.size(); i++)
      Route* route = &_routes[i];
      if (route->path == path && route->method == method)
      return i;


      return -1;



      Struct and StatusCode




      StatusCode




      https://github.com/j-ulrich/http-status-codes-cpp




      Struct



      #pragma once

      #include <json.hpp>

      #include "Server/StatusCode.h"

      using json = nlohmann::json;

      class Struct
      public:
      enum class Methods
      NONE = 0, GET = 1, POST = 2, FIRST = GET, LAST = POST
      ;

      struct Attributes
      const std::string version = "HTTP/1.1";

      std::string path;
      Methods method;
      HttpStatus::Code code;
      std::unordered_map<std::string, std::string> headers;
      json body;

      Attributes()
      code = HttpStatus::Code::InternalServerError;
      body = json::value_t::object;


      bool isValidRequest()
      if (!path.length()) return false;
      if (method < Methods::FIRST

      bool isValidResponse()
      if (!headers.size()) return false;
      return true;

      ;

      static Methods doParseHttpMethod(std::string value)
      Methods target = Methods::NONE;

      if (value == "GET") target = Methods::GET;
      if (value == "POST") target = Methods::POST;

      return target;


      private:
      ;


      Main (usage):




      #include "Server/Server.h"

      void exec(Request* request, Response* response)
      json body;

      body["foo"] = 123;
      body["bar"] = true;

      response->setBody(body);
      response->doSendSuccess();


      int main(int argc, char* argv[])
      Server* server = new Server(5000);
      server->setRoute("/getStatus", Struct::Methods::GET, exec);
      server->setRoute("/postStatus", Struct::Methods::POST, exec);
      server->doListen();

      // let threads live for some time before they eternaly be gone
      /*
      actually I'm stuck with this sleep, I don't know how to hold
      this until caller call doStop() without using while and
      consuming process power
      */
      sleep(30);

      delete server;
      return 1;




      Basically I'm mirroring NodeJS express API Usage:
      server.setRoute(path, route, callback);




      So, what can be done to improve my code in terms of optimization and efficiency?



      Thanks in advance.







      c++ multithreading http socket server






      share|improve this question









      New contributor




      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 4 hours ago







      Radagast













      New contributor




      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      RadagastRadagast

      1263




      1263




      New contributor




      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Radagast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes


















          3












          $begingroup$

          Note: This review focuses on the use of C++, rather than the functionality.




          Naming:



          • IMHO, the use of "do" at the start of function names is unnecessary and makes the code harder to read. The names would be fine without it (e.g. sendSuccess, sendError, createSocket all make perfect sense).


          Server:



          • If the port must always fit in a 16 bit unsigned int, we can use std::uint16_t (from the <cstdint> header) instead of an unsigned int.


          • The new keyword should almost never be used in modern C++. If we need to create it on the heap, _routes should be a std::unique_ptr (from <memory>), which will be cleaned up automatically for us. In this case it looks like the variable could just be created on the stack (i.e. declared as Routes _routes;).


          • _doCreateSocket() returns a bool, but the Server constructor uses an int to hold the return type.



          • It's better to use the constructor's member initializer list to initialize variables where possible (it's neater, and we don't have to worry about initializing objects twice), e.g.:



            Server::Server(std::uint16_t port, unsigned int max_connections, unsigned int thread_count):
            _port(port),
            _max_connections(max_connections),
            _thread_count(thread_count),
            _signal(false),
            _thread_consume(nullptr),
            _thread_process(nullptr),
            _routes(nullptr),
            _socket(-1), // or something
            _listen(false)

            if (max_connections < 1)
            Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


            if (!_doCreateSocket(_socket))
            Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


            if (!_doBindSocket(_socket))
            Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");




          • Note that plain data variables (e.g. pointers, ints) are left uninitialized (and may contain any random value) unless we explicitly initialize them. It's safest to always set them to a known value in the constructor.


          • _thread_consume can also be created on the stack (the std::thread default constructor doesn't launch a new thread), and _thread_process can be a std::vector<std::thread>. This saves us from having to do any manual memory management.


          • Prefer to use std::function from the <functional> header, instead of raw function pointers. (e.g. std::function<void(Request*, Response*)>).


          • The request and response variables in Server::_doProcessSocket should be created on the stack. We can still pass them by pointers if necessary by taking their addresses (_doProcessRequest(&request, &response)), or (better) we could pass them by reference.


          • The status code returned by Server::_doProcessRequest() isn't used.


          • In Server::_doProcessRequest(), the if (route.method != method) check is unnecessary, since we used the method while finding the route.


          • The forwarding from doStop to _doStop is unnecessary.


          • This class is doing several things. It manages a thread pool, as well as doing raw socket stuff. We could definitely split the socket functionality into a separate class.



          Request:



          • Member functions that don't alter the member variables of a class should be declared const, e.g.: bool isValid() const;. This means we can make proper use of const and const& variables, allowing the compiler to perform better optimisations, and preventing programmer error.


          • The getter functions in this class all return by value. This probably results in some unnecessary copies being made, which may be expensive where the objects require allocation of memory (e.g. copying the unordered_map / strings). It might be better to return by const& instead, e.g.: std::unordered_map<std::string, std::string> const& getHeaders();. This still prevents the caller from altering referenced variable, but allows them to decide whether to copy it, copy part of it, or not copy it at all.


          • In Request::_doReceiveData, we can use a std::vector<char> for the buffer rather than doing manual memory management. (It's guaranteed to provide contiguous memory, which we can access using the .data() member function).



          Routes:



          • Routes::setRoute should probably use the Route::isValid method, rather than duplicating the checks.


          • There's some unnecessary string copies in getRoute. We should pass the variable as a reference: const std::string& path instead.



          • Using iterators and the standard library search algorithms is more idiomatic C++ than indices. e.g.:



            auto route = std::find_if(_routes.begin(), routes.end(), 
            [] (Route const& route) return (route.path == path) && (route->method == method); );

            if (route == routes.end()) // route not found!


          • (Unless it's reused elsewhere, I'd be inclined to remove the Routes class in favor of a std::vector<Route> _routes; in the Server class.)



          Main:



          • (Use std::this_thread::sleep_for for a portable sleep function.)


          • I think std::condition_variable::wait() may be what you're looking for.






          share|improve this answer









          $endgroup$













            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Radagast is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217783%2fsimple-http-server%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3












            $begingroup$

            Note: This review focuses on the use of C++, rather than the functionality.




            Naming:



            • IMHO, the use of "do" at the start of function names is unnecessary and makes the code harder to read. The names would be fine without it (e.g. sendSuccess, sendError, createSocket all make perfect sense).


            Server:



            • If the port must always fit in a 16 bit unsigned int, we can use std::uint16_t (from the <cstdint> header) instead of an unsigned int.


            • The new keyword should almost never be used in modern C++. If we need to create it on the heap, _routes should be a std::unique_ptr (from <memory>), which will be cleaned up automatically for us. In this case it looks like the variable could just be created on the stack (i.e. declared as Routes _routes;).


            • _doCreateSocket() returns a bool, but the Server constructor uses an int to hold the return type.



            • It's better to use the constructor's member initializer list to initialize variables where possible (it's neater, and we don't have to worry about initializing objects twice), e.g.:



              Server::Server(std::uint16_t port, unsigned int max_connections, unsigned int thread_count):
              _port(port),
              _max_connections(max_connections),
              _thread_count(thread_count),
              _signal(false),
              _thread_consume(nullptr),
              _thread_process(nullptr),
              _routes(nullptr),
              _socket(-1), // or something
              _listen(false)

              if (max_connections < 1)
              Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


              if (!_doCreateSocket(_socket))
              Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


              if (!_doBindSocket(_socket))
              Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");




            • Note that plain data variables (e.g. pointers, ints) are left uninitialized (and may contain any random value) unless we explicitly initialize them. It's safest to always set them to a known value in the constructor.


            • _thread_consume can also be created on the stack (the std::thread default constructor doesn't launch a new thread), and _thread_process can be a std::vector<std::thread>. This saves us from having to do any manual memory management.


            • Prefer to use std::function from the <functional> header, instead of raw function pointers. (e.g. std::function<void(Request*, Response*)>).


            • The request and response variables in Server::_doProcessSocket should be created on the stack. We can still pass them by pointers if necessary by taking their addresses (_doProcessRequest(&request, &response)), or (better) we could pass them by reference.


            • The status code returned by Server::_doProcessRequest() isn't used.


            • In Server::_doProcessRequest(), the if (route.method != method) check is unnecessary, since we used the method while finding the route.


            • The forwarding from doStop to _doStop is unnecessary.


            • This class is doing several things. It manages a thread pool, as well as doing raw socket stuff. We could definitely split the socket functionality into a separate class.



            Request:



            • Member functions that don't alter the member variables of a class should be declared const, e.g.: bool isValid() const;. This means we can make proper use of const and const& variables, allowing the compiler to perform better optimisations, and preventing programmer error.


            • The getter functions in this class all return by value. This probably results in some unnecessary copies being made, which may be expensive where the objects require allocation of memory (e.g. copying the unordered_map / strings). It might be better to return by const& instead, e.g.: std::unordered_map<std::string, std::string> const& getHeaders();. This still prevents the caller from altering referenced variable, but allows them to decide whether to copy it, copy part of it, or not copy it at all.


            • In Request::_doReceiveData, we can use a std::vector<char> for the buffer rather than doing manual memory management. (It's guaranteed to provide contiguous memory, which we can access using the .data() member function).



            Routes:



            • Routes::setRoute should probably use the Route::isValid method, rather than duplicating the checks.


            • There's some unnecessary string copies in getRoute. We should pass the variable as a reference: const std::string& path instead.



            • Using iterators and the standard library search algorithms is more idiomatic C++ than indices. e.g.:



              auto route = std::find_if(_routes.begin(), routes.end(), 
              [] (Route const& route) return (route.path == path) && (route->method == method); );

              if (route == routes.end()) // route not found!


            • (Unless it's reused elsewhere, I'd be inclined to remove the Routes class in favor of a std::vector<Route> _routes; in the Server class.)



            Main:



            • (Use std::this_thread::sleep_for for a portable sleep function.)


            • I think std::condition_variable::wait() may be what you're looking for.






            share|improve this answer









            $endgroup$

















              3












              $begingroup$

              Note: This review focuses on the use of C++, rather than the functionality.




              Naming:



              • IMHO, the use of "do" at the start of function names is unnecessary and makes the code harder to read. The names would be fine without it (e.g. sendSuccess, sendError, createSocket all make perfect sense).


              Server:



              • If the port must always fit in a 16 bit unsigned int, we can use std::uint16_t (from the <cstdint> header) instead of an unsigned int.


              • The new keyword should almost never be used in modern C++. If we need to create it on the heap, _routes should be a std::unique_ptr (from <memory>), which will be cleaned up automatically for us. In this case it looks like the variable could just be created on the stack (i.e. declared as Routes _routes;).


              • _doCreateSocket() returns a bool, but the Server constructor uses an int to hold the return type.



              • It's better to use the constructor's member initializer list to initialize variables where possible (it's neater, and we don't have to worry about initializing objects twice), e.g.:



                Server::Server(std::uint16_t port, unsigned int max_connections, unsigned int thread_count):
                _port(port),
                _max_connections(max_connections),
                _thread_count(thread_count),
                _signal(false),
                _thread_consume(nullptr),
                _thread_process(nullptr),
                _routes(nullptr),
                _socket(-1), // or something
                _listen(false)

                if (max_connections < 1)
                Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


                if (!_doCreateSocket(_socket))
                Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


                if (!_doBindSocket(_socket))
                Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");




              • Note that plain data variables (e.g. pointers, ints) are left uninitialized (and may contain any random value) unless we explicitly initialize them. It's safest to always set them to a known value in the constructor.


              • _thread_consume can also be created on the stack (the std::thread default constructor doesn't launch a new thread), and _thread_process can be a std::vector<std::thread>. This saves us from having to do any manual memory management.


              • Prefer to use std::function from the <functional> header, instead of raw function pointers. (e.g. std::function<void(Request*, Response*)>).


              • The request and response variables in Server::_doProcessSocket should be created on the stack. We can still pass them by pointers if necessary by taking their addresses (_doProcessRequest(&request, &response)), or (better) we could pass them by reference.


              • The status code returned by Server::_doProcessRequest() isn't used.


              • In Server::_doProcessRequest(), the if (route.method != method) check is unnecessary, since we used the method while finding the route.


              • The forwarding from doStop to _doStop is unnecessary.


              • This class is doing several things. It manages a thread pool, as well as doing raw socket stuff. We could definitely split the socket functionality into a separate class.



              Request:



              • Member functions that don't alter the member variables of a class should be declared const, e.g.: bool isValid() const;. This means we can make proper use of const and const& variables, allowing the compiler to perform better optimisations, and preventing programmer error.


              • The getter functions in this class all return by value. This probably results in some unnecessary copies being made, which may be expensive where the objects require allocation of memory (e.g. copying the unordered_map / strings). It might be better to return by const& instead, e.g.: std::unordered_map<std::string, std::string> const& getHeaders();. This still prevents the caller from altering referenced variable, but allows them to decide whether to copy it, copy part of it, or not copy it at all.


              • In Request::_doReceiveData, we can use a std::vector<char> for the buffer rather than doing manual memory management. (It's guaranteed to provide contiguous memory, which we can access using the .data() member function).



              Routes:



              • Routes::setRoute should probably use the Route::isValid method, rather than duplicating the checks.


              • There's some unnecessary string copies in getRoute. We should pass the variable as a reference: const std::string& path instead.



              • Using iterators and the standard library search algorithms is more idiomatic C++ than indices. e.g.:



                auto route = std::find_if(_routes.begin(), routes.end(), 
                [] (Route const& route) return (route.path == path) && (route->method == method); );

                if (route == routes.end()) // route not found!


              • (Unless it's reused elsewhere, I'd be inclined to remove the Routes class in favor of a std::vector<Route> _routes; in the Server class.)



              Main:



              • (Use std::this_thread::sleep_for for a portable sleep function.)


              • I think std::condition_variable::wait() may be what you're looking for.






              share|improve this answer









              $endgroup$















                3












                3








                3





                $begingroup$

                Note: This review focuses on the use of C++, rather than the functionality.




                Naming:



                • IMHO, the use of "do" at the start of function names is unnecessary and makes the code harder to read. The names would be fine without it (e.g. sendSuccess, sendError, createSocket all make perfect sense).


                Server:



                • If the port must always fit in a 16 bit unsigned int, we can use std::uint16_t (from the <cstdint> header) instead of an unsigned int.


                • The new keyword should almost never be used in modern C++. If we need to create it on the heap, _routes should be a std::unique_ptr (from <memory>), which will be cleaned up automatically for us. In this case it looks like the variable could just be created on the stack (i.e. declared as Routes _routes;).


                • _doCreateSocket() returns a bool, but the Server constructor uses an int to hold the return type.



                • It's better to use the constructor's member initializer list to initialize variables where possible (it's neater, and we don't have to worry about initializing objects twice), e.g.:



                  Server::Server(std::uint16_t port, unsigned int max_connections, unsigned int thread_count):
                  _port(port),
                  _max_connections(max_connections),
                  _thread_count(thread_count),
                  _signal(false),
                  _thread_consume(nullptr),
                  _thread_process(nullptr),
                  _routes(nullptr),
                  _socket(-1), // or something
                  _listen(false)

                  if (max_connections < 1)
                  Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


                  if (!_doCreateSocket(_socket))
                  Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


                  if (!_doBindSocket(_socket))
                  Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");




                • Note that plain data variables (e.g. pointers, ints) are left uninitialized (and may contain any random value) unless we explicitly initialize them. It's safest to always set them to a known value in the constructor.


                • _thread_consume can also be created on the stack (the std::thread default constructor doesn't launch a new thread), and _thread_process can be a std::vector<std::thread>. This saves us from having to do any manual memory management.


                • Prefer to use std::function from the <functional> header, instead of raw function pointers. (e.g. std::function<void(Request*, Response*)>).


                • The request and response variables in Server::_doProcessSocket should be created on the stack. We can still pass them by pointers if necessary by taking their addresses (_doProcessRequest(&request, &response)), or (better) we could pass them by reference.


                • The status code returned by Server::_doProcessRequest() isn't used.


                • In Server::_doProcessRequest(), the if (route.method != method) check is unnecessary, since we used the method while finding the route.


                • The forwarding from doStop to _doStop is unnecessary.


                • This class is doing several things. It manages a thread pool, as well as doing raw socket stuff. We could definitely split the socket functionality into a separate class.



                Request:



                • Member functions that don't alter the member variables of a class should be declared const, e.g.: bool isValid() const;. This means we can make proper use of const and const& variables, allowing the compiler to perform better optimisations, and preventing programmer error.


                • The getter functions in this class all return by value. This probably results in some unnecessary copies being made, which may be expensive where the objects require allocation of memory (e.g. copying the unordered_map / strings). It might be better to return by const& instead, e.g.: std::unordered_map<std::string, std::string> const& getHeaders();. This still prevents the caller from altering referenced variable, but allows them to decide whether to copy it, copy part of it, or not copy it at all.


                • In Request::_doReceiveData, we can use a std::vector<char> for the buffer rather than doing manual memory management. (It's guaranteed to provide contiguous memory, which we can access using the .data() member function).



                Routes:



                • Routes::setRoute should probably use the Route::isValid method, rather than duplicating the checks.


                • There's some unnecessary string copies in getRoute. We should pass the variable as a reference: const std::string& path instead.



                • Using iterators and the standard library search algorithms is more idiomatic C++ than indices. e.g.:



                  auto route = std::find_if(_routes.begin(), routes.end(), 
                  [] (Route const& route) return (route.path == path) && (route->method == method); );

                  if (route == routes.end()) // route not found!


                • (Unless it's reused elsewhere, I'd be inclined to remove the Routes class in favor of a std::vector<Route> _routes; in the Server class.)



                Main:



                • (Use std::this_thread::sleep_for for a portable sleep function.)


                • I think std::condition_variable::wait() may be what you're looking for.






                share|improve this answer









                $endgroup$



                Note: This review focuses on the use of C++, rather than the functionality.




                Naming:



                • IMHO, the use of "do" at the start of function names is unnecessary and makes the code harder to read. The names would be fine without it (e.g. sendSuccess, sendError, createSocket all make perfect sense).


                Server:



                • If the port must always fit in a 16 bit unsigned int, we can use std::uint16_t (from the <cstdint> header) instead of an unsigned int.


                • The new keyword should almost never be used in modern C++. If we need to create it on the heap, _routes should be a std::unique_ptr (from <memory>), which will be cleaned up automatically for us. In this case it looks like the variable could just be created on the stack (i.e. declared as Routes _routes;).


                • _doCreateSocket() returns a bool, but the Server constructor uses an int to hold the return type.



                • It's better to use the constructor's member initializer list to initialize variables where possible (it's neater, and we don't have to worry about initializing objects twice), e.g.:



                  Server::Server(std::uint16_t port, unsigned int max_connections, unsigned int thread_count):
                  _port(port),
                  _max_connections(max_connections),
                  _thread_count(thread_count),
                  _signal(false),
                  _thread_consume(nullptr),
                  _thread_process(nullptr),
                  _routes(nullptr),
                  _socket(-1), // or something
                  _listen(false)

                  if (max_connections < 1)
                  Logger::doSendMessage(Logger::TYPES::ERROR, "Max connections can't be lower than 1 on Server::Constructor.");


                  if (!_doCreateSocket(_socket))
                  Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to create socket on Server::Constructor.");


                  if (!_doBindSocket(_socket))
                  Logger::doSendMessage(Logger::TYPES::ERROR, "Failed to bind socket on Server::Constructor.");




                • Note that plain data variables (e.g. pointers, ints) are left uninitialized (and may contain any random value) unless we explicitly initialize them. It's safest to always set them to a known value in the constructor.


                • _thread_consume can also be created on the stack (the std::thread default constructor doesn't launch a new thread), and _thread_process can be a std::vector<std::thread>. This saves us from having to do any manual memory management.


                • Prefer to use std::function from the <functional> header, instead of raw function pointers. (e.g. std::function<void(Request*, Response*)>).


                • The request and response variables in Server::_doProcessSocket should be created on the stack. We can still pass them by pointers if necessary by taking their addresses (_doProcessRequest(&request, &response)), or (better) we could pass them by reference.


                • The status code returned by Server::_doProcessRequest() isn't used.


                • In Server::_doProcessRequest(), the if (route.method != method) check is unnecessary, since we used the method while finding the route.


                • The forwarding from doStop to _doStop is unnecessary.


                • This class is doing several things. It manages a thread pool, as well as doing raw socket stuff. We could definitely split the socket functionality into a separate class.



                Request:



                • Member functions that don't alter the member variables of a class should be declared const, e.g.: bool isValid() const;. This means we can make proper use of const and const& variables, allowing the compiler to perform better optimisations, and preventing programmer error.


                • The getter functions in this class all return by value. This probably results in some unnecessary copies being made, which may be expensive where the objects require allocation of memory (e.g. copying the unordered_map / strings). It might be better to return by const& instead, e.g.: std::unordered_map<std::string, std::string> const& getHeaders();. This still prevents the caller from altering referenced variable, but allows them to decide whether to copy it, copy part of it, or not copy it at all.


                • In Request::_doReceiveData, we can use a std::vector<char> for the buffer rather than doing manual memory management. (It's guaranteed to provide contiguous memory, which we can access using the .data() member function).



                Routes:



                • Routes::setRoute should probably use the Route::isValid method, rather than duplicating the checks.


                • There's some unnecessary string copies in getRoute. We should pass the variable as a reference: const std::string& path instead.



                • Using iterators and the standard library search algorithms is more idiomatic C++ than indices. e.g.:



                  auto route = std::find_if(_routes.begin(), routes.end(), 
                  [] (Route const& route) return (route.path == path) && (route->method == method); );

                  if (route == routes.end()) // route not found!


                • (Unless it's reused elsewhere, I'd be inclined to remove the Routes class in favor of a std::vector<Route> _routes; in the Server class.)



                Main:



                • (Use std::this_thread::sleep_for for a portable sleep function.)


                • I think std::condition_variable::wait() may be what you're looking for.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                user673679user673679

                3,75311231




                3,75311231




















                    Radagast is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    Radagast is a new contributor. Be nice, and check out our Code of Conduct.












                    Radagast is a new contributor. Be nice, and check out our Code of Conduct.











                    Radagast is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217783%2fsimple-http-server%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    На ростанях Змест Гісторыя напісання | Месца дзеяння | Час дзеяння | Назва | Праблематыка трылогіі | Аўтабіяграфічнасць | Трылогія ў тэатры і кіно | Пераклады | У культуры | Зноскі Літаратура | Спасылкі | НавігацыяДагледжаная версіяправерана1 зменаДагледжаная версіяправерана1 зменаАкадэмік МІЦКЕВІЧ Канстанцін Міхайлавіч (Якуб Колас) Прадмова М. І. Мушынскага, доктара філалагічных навук, члена-карэспандэнта Нацыянальнай акадэміі навук Рэспублікі Беларусь, прафесараНашаніўцы ў трылогіі Якуба Коласа «На ростанях»: вобразы і прататыпы125 лет Янке МавруКнижно-документальная выставка к 125-летию со дня рождения Якуба Коласа (1882—1956)Колас Якуб. Новая зямля (паэма), На ростанях (трылогія). Сулкоўскі Уладзімір. Радзіма Якуба Коласа (серыял жывапісных палотнаў)Вокладка кнігіІлюстрацыя М. С. БасалыгіНа ростаняхАўдыёверсія трылогііВ. Жолтак У Люсiнскай школе 1959

                    Францішак Багушэвіч Змест Сям'я | Біяграфія | Творчасць | Мова Багушэвіча | Ацэнкі дзейнасці | Цікавыя факты | Спадчына | Выбраная бібліяграфія | Ушанаванне памяці | У філатэліі | Зноскі | Літаратура | Спасылкі | НавігацыяЛяхоўскі У. Рупіўся дзеля Бога і людзей: Жыццёвы шлях Лявона Вітан-Дубейкаўскага // Вольскі і Памідораў з песняй пра немца Адвакат, паэт, народны заступнік Ашмянскі веснікВ Минске появится площадь Богушевича и улица Сырокомли, Белорусская деловая газета, 19 июля 2001 г.Айцец беларускай нацыянальнай ідэі паўстаў у бронзе Сяргей Аляксандравіч Адашкевіч (1918, Мінск). 80-я гады. Бюст «Францішак Багушэвіч».Яўген Мікалаевіч Ціхановіч. «Партрэт Францішка Багушэвіча»Мікола Мікалаевіч Купава. «Партрэт зачынальніка новай беларускай літаратуры Францішка Багушэвіча»Уладзімір Іванавіч Мелехаў. На помніку «Змагарам за родную мову» Барэльеф «Францішак Багушэвіч»Памяць пра Багушэвіча на Віленшчыне Страчаная сталіца. Беларускія шыльды на вуліцах Вільні«Krynica». Ideologia i przywódcy białoruskiego katolicyzmuФранцішак БагушэвічТворы на knihi.comТворы Францішка Багушэвіча на bellib.byСодаль Уладзімір. Францішак Багушэвіч на Лідчыне;Луцкевіч Антон. Жыцьцё і творчасьць Фр. Багушэвіча ў успамінах ягоных сучасьнікаў // Запісы Беларускага Навуковага таварыства. Вільня, 1938. Сшытак 1. С. 16-34.Большая российская1188761710000 0000 5537 633Xn9209310021619551927869394п

                    Беларусь Змест Назва Гісторыя Геаграфія Сімволіка Дзяржаўны лад Палітычныя партыі Міжнароднае становішча і знешняя палітыка Адміністрацыйны падзел Насельніцтва Эканоміка Культура і грамадства Сацыяльная сфера Узброеныя сілы Заўвагі Літаратура Спасылкі НавігацыяHGЯOiТоп-2011 г. (па версіі ej.by)Топ-2013 г. (па версіі ej.by)Топ-2016 г. (па версіі ej.by)Топ-2017 г. (па версіі ej.by)Нацыянальны статыстычны камітэт Рэспублікі БеларусьШчыльнасць насельніцтва па краінахhttp://naviny.by/rubrics/society/2011/09/16/ic_articles_116_175144/А. Калечыц, У. Ксяндзоў. Спробы засялення краю неандэртальскім чалавекам.І ў Менску былі мамантыА. Калечыц, У. Ксяндзоў. Старажытны каменны век (палеаліт). Першапачатковае засяленне тэрыторыіГ. Штыхаў. Балты і славяне ў VI—VIII стст.М. Клімаў. Полацкае княства ў IX—XI стст.Г. Штыхаў, В. Ляўко. Палітычная гісторыя Полацкай зямліГ. Штыхаў. Дзяржаўны лад у землях-княствахГ. Штыхаў. Дзяржаўны лад у землях-княствахБеларускія землі ў складзе Вялікага Княства ЛітоўскагаЛюблінская унія 1569 г."The Early Stages of Independence"Zapomniane prawdy25 гадоў таму было аб'яўлена, што Язэп Пілсудскі — беларус (фота)Наша вадаДакументы ЧАЭС: Забруджванне тэрыторыі Беларусі « ЧАЭС Зона адчужэнняСведения о политических партиях, зарегистрированных в Республике Беларусь // Министерство юстиции Республики БеларусьСтатыстычны бюлетэнь „Полаўзроставая структура насельніцтва Рэспублікі Беларусь на 1 студзеня 2012 года і сярэднегадовая колькасць насельніцтва за 2011 год“Индекс человеческого развития Беларуси — не было бы нижеБеларусь занимает первое место в СНГ по индексу развития с учетом гендерного факцёраНацыянальны статыстычны камітэт Рэспублікі БеларусьКанстытуцыя РБ. Артыкул 17Трансфармацыйныя задачы БеларусіВыйсце з крызісу — далейшае рэфармаванне Беларускі рубель — сусветны лідар па дэвальвацыяхПра змену коштаў у кастрычніку 2011 г.Бядней за беларусаў у СНД толькі таджыкіСярэдні заробак у верасні дасягнуў 2,26 мільёна рублёўЭканомікаГаласуем за ТОП-100 беларускай прозыСучасныя беларускія мастакіАрхитектура Беларуси BELARUS.BYА. Каханоўскі. Культура Беларусі ўсярэдзіне XVII—XVIII ст.Анталогія беларускай народнай песні, гуказапісы спеваўБеларускія Музычныя IнструментыБеларускі рок, які мы страцілі. Топ-10 гуртоў«Мясцовы час» — нязгаслая легенда беларускай рок-музыкіСЯРГЕЙ БУДКІН. МЫ НЯ ЗНАЕМ СВАЁЙ МУЗЫКІМ. А. Каладзінскі. НАРОДНЫ ТЭАТРМагнацкія культурныя цэнтрыПублічная дыскусія «Беларуская новая пьеса: без беларускай мовы ці беларуская?»Беларускія драматургі па-ранейшаму лепш ставяцца за мяжой, чым на радзіме«Працэс незалежнага кіно пайшоў, і дзяржаву турбуе яго непадкантрольнасць»Беларускія філосафы ў пошуках прасторыВсе идём в библиотекуАрхіваванаАб Нацыянальнай праграме даследавання і выкарыстання касмічнай прасторы ў мірных мэтах на 2008—2012 гадыУ космас — разам.У суседнім з Барысаўскім раёне пабудуюць Камандна-вымяральны пунктСвяты і абрады беларусаў«Мірныя бульбашы з малой краіны» — 5 непраўдзівых стэрэатыпаў пра БеларусьМ. Раманюк. Беларускае народнае адзеннеУ Беларусі скарачаецца колькасць злачынстваўЛукашэнка незадаволены мінскімі ўладамі Крадзяжы складаюць у Мінску каля 70% злачынстваў Узровень злачыннасці ў Мінскай вобласці — адзін з самых высокіх у краіне Генпракуратура аналізуе стан са злачыннасцю ў Беларусі па каэфіцыенце злачыннасці У Беларусі стабілізавалася крымінагеннае становішча, лічыць генпракурорЗамежнікі сталі здзяйсняць у Беларусі больш злачынстваўМУС Беларусі турбуе рост рэцыдыўнай злачыннасціЯ з ЖЭСа. Дазволіце вас абкрасці! Рэйтынг усіх службаў і падраздзяленняў ГУУС Мінгарвыканкама вырасАб КДБ РБГісторыя Аператыўна-аналітычнага цэнтра РБГісторыя ДКФРТаможняagentura.ruБеларусьBelarus.by — Афіцыйны сайт Рэспублікі БеларусьСайт урада БеларусіRadzima.org — Збор архітэктурных помнікаў, гісторыя Беларусі«Глобус Беларуси»Гербы и флаги БеларусиАсаблівасці каменнага веку на БеларусіА. Калечыц, У. Ксяндзоў. Старажытны каменны век (палеаліт). Першапачатковае засяленне тэрыторыіУ. Ксяндзоў. Сярэдні каменны век (мезаліт). Засяленне краю плямёнамі паляўнічых, рыбакоў і збіральнікаўА. Калечыц, М. Чарняўскі. Плямёны на тэрыторыі Беларусі ў новым каменным веку (неаліце)А. Калечыц, У. Ксяндзоў, М. Чарняўскі. Гаспадарчыя заняткі ў каменным векуЭ. Зайкоўскі. Духоўная культура ў каменным векуАсаблівасці бронзавага веку на БеларусіФарміраванне супольнасцей ранняга перыяду бронзавага векуФотографии БеларусиРоля беларускіх зямель ва ўтварэнні і ўмацаванні ВКЛВ. Фадзеева. З гісторыі развіцця беларускай народнай вышыўкіDMOZGran catalanaБольшая российскаяBritannica (анлайн)Швейцарскі гістарычны15325917611952699xDA123282154079143-90000 0001 2171 2080n9112870100577502ge128882171858027501086026362074122714179пппппп