Created
November 28, 2017 18:36
-
-
Save drewxa/90885e4b403d82393fc08d8e3b99724b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctime> | |
#include <iostream> | |
#include <string> | |
#include <memory> | |
#include <boost/array.hpp> | |
#include <boost/asio.hpp> | |
using boost::asio::ip::tcp; | |
std::string makeMessage(const char* data, int len) | |
{ | |
std::string ua(data, len); | |
std::stringstream responseBody; | |
std::stringstream response; | |
responseBody << "<title>Test C++ HTTP Server</title>\n" | |
<< "<h1>Test page</h1>\n" | |
<< "<p>This is body of the test page...</p>\n" | |
<< "<h2>Request headers</h2>\n" | |
<< "<pre>" << ua << "</pre>\n" | |
<< "<em><small>Test C++ Http Server</small></em>\n"; | |
// Формируем весь ответ вместе с заголовками | |
response << "HTTP/1.1 200 OK\r\n" | |
<< "Version: HTTP/1.1\r\n" | |
<< "Content-Type: text/html; charset=utf-8\r\n" | |
<< "Content-Length: " << responseBody.str().length() | |
<< "\r\n\r\n" | |
<< responseBody.str(); | |
return response.str(); | |
} | |
struct Connection | |
: std::enable_shared_from_this<Connection> | |
{ | |
using Ptr = std::shared_ptr<Connection>; | |
tcp::socket Socket; | |
boost::array<char, 128> buf; | |
Connection(boost::asio::io_service& io) | |
: Socket(io) | |
{ } | |
~Connection() | |
{ | |
std::cout << "!!!!!!!"; | |
} | |
void ReadHandler(std::size_t bytesTransferred) | |
{ | |
std::string message = makeMessage(buf.data(), bytesTransferred); | |
boost::system::error_code error; | |
Socket.write_some(boost::asio::buffer(message), error); | |
} | |
void Work() | |
{ | |
boost::system::error_code error; | |
auto self = shared_from_this(); | |
Socket.async_read_some( | |
boost::asio::buffer(buf), | |
[self](const boost::system::error_code& error, std::size_t bytesTransferred) { | |
self->ReadHandler(bytesTransferred); | |
}); | |
} | |
}; | |
struct Server | |
{ | |
boost::asio::io_service& IO; | |
tcp::acceptor Acceptor; | |
Server(boost::asio::io_service& io, tcp::endpoint ep) | |
: IO(io) | |
, Acceptor(io, ep) | |
{ | |
Start(); | |
} | |
void Handler(Connection::Ptr socket, const boost::system::error_code& e) | |
{ | |
socket->Work(); | |
std::cout << "I'm here"; | |
Start(); | |
} | |
void Start() | |
{ | |
auto session = std::make_shared<Connection>(IO); | |
Acceptor.async_accept( | |
session->Socket, | |
[this, session](const boost::system::error_code& e) { | |
this->Handler(session, e); | |
}); | |
} | |
}; | |
int main() | |
{ | |
try | |
{ | |
boost::asio::io_service io_service; | |
Server server(io_service, tcp::endpoint(tcp::v4(), 8000)); | |
io_service.run(); | |
} | |
catch (std::exception& e) | |
{ | |
std::cerr << e.what() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment