WIP
This commit is contained in:
parent
aeb0d3c445
commit
06cbd98b77
|
|
@ -132,13 +132,12 @@ void fractal(
|
||||||
{
|
{
|
||||||
// Other methods are not supported for directory acces
|
// Other methods are not supported for directory acces
|
||||||
//
|
//
|
||||||
boost::beast::http::response<boost::beast::http::string_body> bad_request{boost::beast::http::status::bad_request, req.version()};
|
auto& bad_request = promise.CreateResponse<boost::beast::http::response<boost::beast::http::string_body>>(boost::beast::http::status::bad_request, req.version());
|
||||||
bad_request.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
bad_request.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
bad_request.set(boost::beast::http::field::content_type, "text/plain");
|
bad_request.set(boost::beast::http::field::content_type, "text/plain");
|
||||||
bad_request.prepare_payload();
|
bad_request.prepare_payload();
|
||||||
bad_request.body() = "Bad request type.\nOnly GET and HEAD are expected for this URL.";
|
bad_request.body() = "Bad request type.\nOnly GET and HEAD are expected for this URL.";
|
||||||
bad_request.keep_alive(req.keep_alive());
|
bad_request.keep_alive(req.keep_alive());
|
||||||
promise.SendResponse(std::move(bad_request));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,7 +154,7 @@ void fractal(
|
||||||
std::cout << "HTTP inside render function " << target << std::endl;
|
std::cout << "HTTP inside render function " << target << std::endl;
|
||||||
constexpr int pixel_width = 256;
|
constexpr int pixel_width = 256;
|
||||||
constexpr int pixel_height = 256;
|
constexpr int pixel_height = 256;
|
||||||
constexpr int max_iterations = 100;
|
constexpr int max_iterations = 255;
|
||||||
|
|
||||||
|
|
||||||
boost::gil::gray8_image_t image(pixel_width,pixel_height);
|
boost::gil::gray8_image_t image(pixel_width,pixel_height);
|
||||||
|
|
@ -189,13 +188,12 @@ void fractal(
|
||||||
boost::gil::view(image),
|
boost::gil::view(image),
|
||||||
boost::gil::image_write_info<boost::gil::jpeg_tag>(95));
|
boost::gil::image_write_info<boost::gil::jpeg_tag>(95));
|
||||||
|
|
||||||
boost::beast::http::response<boost::beast::http::string_body> ok{boost::beast::http::status::ok, req.version()};
|
auto& ok = promise.CreateResponse<boost::beast::http::response<boost::beast::http::string_body>>(boost::beast::http::status::ok, req.version());
|
||||||
ok.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
ok.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
ok.set(boost::beast::http::field::content_type, "image/jpeg");
|
ok.set(boost::beast::http::field::content_type, "image/jpeg");
|
||||||
ok.body() = out_buffer.str();
|
ok.body() = out_buffer.str();
|
||||||
ok.keep_alive(req.keep_alive());
|
ok.keep_alive(req.keep_alive());
|
||||||
ok.prepare_payload();
|
ok.prepare_payload();
|
||||||
promise.SendResponse(std::move(ok));
|
|
||||||
|
|
||||||
std::cout << " DONE HTTP inside render function " << target << std::endl;
|
std::cout << " DONE HTTP inside render function " << target << std::endl;
|
||||||
};
|
};
|
||||||
|
|
@ -250,7 +248,7 @@ int main(int argc, char **argv)
|
||||||
});
|
});
|
||||||
|
|
||||||
std::vector<std::jthread> threads;
|
std::vector<std::jthread> threads;
|
||||||
while(threads.size() < 20)
|
while(threads.size() < 4)
|
||||||
{
|
{
|
||||||
threads.emplace_back([&ioc]{ioc.run();});
|
threads.emplace_back([&ioc]{ioc.run();});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,32 +10,66 @@
|
||||||
#include <boost/asio/ip/tcp.hpp>
|
#include <boost/asio/ip/tcp.hpp>
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace bmrshared::web
|
namespace bmrshared::web
|
||||||
{
|
{
|
||||||
|
|
||||||
class response_promise final
|
class response_promise final
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
class response_writer_interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~response_writer_interface() = default;
|
||||||
|
virtual void write_response(boost::beast::tcp_stream&) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TResponse>
|
||||||
|
class response_writer : public response_writer_interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
template<typename... TArgs>
|
||||||
|
response_writer(TArgs... args)
|
||||||
|
: m_response(args...)
|
||||||
|
{}
|
||||||
|
|
||||||
|
~response_writer() override = default;
|
||||||
|
|
||||||
|
void write_response(boost::beast::tcp_stream& stream) override
|
||||||
|
{
|
||||||
|
boost::beast::http::write(stream, m_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
TResponse& response()
|
||||||
|
{
|
||||||
|
return m_response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
TResponse m_response;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using response_sender = std::function<void(boost::beast::tcp_stream& stream)>;
|
using response_sender = std::function<void(boost::beast::tcp_stream& stream)>;
|
||||||
using callback_on_response = std::function<void(const response_sender& fnSendResponse)>;
|
using callback_on_response = std::function<void(response_sender fnSendResponse)>;
|
||||||
|
|
||||||
response_promise() = delete;
|
response_promise() = delete;
|
||||||
explicit response_promise(callback_on_response cbOnResponse);
|
explicit response_promise(callback_on_response cbOnResponse);
|
||||||
~response_promise();
|
~response_promise();
|
||||||
|
|
||||||
template<typename Body, typename Fields>
|
template<typename TResponse, typename... TArgs>
|
||||||
void SendResponse(boost::beast::http::response<Body, Fields> response)
|
TResponse& CreateResponse(TArgs&&... args)
|
||||||
{
|
{
|
||||||
response_sender responder = [r = std::move(response)](boost::beast::tcp_stream& stream)
|
auto created = std::make_unique<response_writer<TResponse>>(std::forward<TArgs>(args)...);
|
||||||
{
|
auto& resp = created->response();
|
||||||
boost::beast::http::write(stream, r);
|
m_response_writer = std::move(created);
|
||||||
};
|
return resp;
|
||||||
|
|
||||||
m_call_on_response(std::move(responder));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
callback_on_response m_call_on_response;
|
callback_on_response m_call_on_response;
|
||||||
|
std::shared_ptr<response_writer_interface> m_response_writer;
|
||||||
};
|
};
|
||||||
} // namespace bmrshared::web
|
} // namespace bmrshared::web
|
||||||
|
|
|
||||||
|
|
@ -50,13 +50,12 @@ void directory_request_handler::handle_request_http(const address_type& address,
|
||||||
{
|
{
|
||||||
// Other methods are not supported for directory acces
|
// Other methods are not supported for directory acces
|
||||||
//
|
//
|
||||||
http::response<http::string_body> bad_request{http::status::bad_request, req.version()};
|
auto& bad_request = promise.CreateResponse<http::response<http::string_body>>(http::status::bad_request, req.version());
|
||||||
bad_request.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
bad_request.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
bad_request.set(http::field::content_type, "text/plain");
|
bad_request.set(http::field::content_type, "text/plain");
|
||||||
bad_request.prepare_payload();
|
bad_request.prepare_payload();
|
||||||
bad_request.body() = "Bad request type.\nOnly GET and HEAD are expected for this URL.";
|
bad_request.body() = "Bad request type.\nOnly GET and HEAD are expected for this URL.";
|
||||||
bad_request.keep_alive(req.keep_alive());
|
bad_request.keep_alive(req.keep_alive());
|
||||||
promise.SendResponse(std::move(bad_request));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,13 +85,12 @@ void directory_request_handler::handle_request_http(const address_type& address,
|
||||||
|
|
||||||
if (!found_file)
|
if (!found_file)
|
||||||
{
|
{
|
||||||
http::response<http::string_body> not_found{http::status::not_found, req.version()};
|
auto& not_found = promise.CreateResponse<http::response<http::string_body>>(http::status::not_found, req.version());
|
||||||
not_found.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
not_found.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
not_found.set(http::field::content_type, "text/plain");
|
not_found.set(http::field::content_type, "text/plain");
|
||||||
not_found.prepare_payload();
|
not_found.prepare_payload();
|
||||||
not_found.body() = "File not found.";
|
not_found.body() = "File not found.";
|
||||||
not_found.keep_alive(req.keep_alive());
|
not_found.keep_alive(req.keep_alive());
|
||||||
promise.SendResponse(std::move(not_found));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,13 +100,12 @@ void directory_request_handler::handle_request_http(const address_type& address,
|
||||||
body.open(found_file->native().c_str(), beast::file_mode::scan, ec);
|
body.open(found_file->native().c_str(), beast::file_mode::scan, ec);
|
||||||
if (ec)
|
if (ec)
|
||||||
{
|
{
|
||||||
http::response<http::string_body> internal{http::status::internal_server_error, req.version()};
|
auto& internal = promise.CreateResponse<http::response<http::string_body>>(http::status::internal_server_error, req.version());
|
||||||
internal.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
internal.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
internal.set(http::field::content_type, "text/plain");
|
internal.set(http::field::content_type, "text/plain");
|
||||||
internal.prepare_payload();
|
internal.prepare_payload();
|
||||||
internal.body() = "Internal server error.";
|
internal.body() = "Internal server error.";
|
||||||
internal.keep_alive(req.keep_alive());
|
internal.keep_alive(req.keep_alive());
|
||||||
promise.SendResponse(std::move(internal));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,10 +116,9 @@ void directory_request_handler::handle_request_http(const address_type& address,
|
||||||
|
|
||||||
if (req.count(http::field::if_modified_since) != 0 && (req[http::field::if_modified_since] == last_modified))
|
if (req.count(http::field::if_modified_since) != 0 && (req[http::field::if_modified_since] == last_modified))
|
||||||
{
|
{
|
||||||
http::response<http::string_body> head{http::status::not_modified, req.version()};
|
auto& head = promise.CreateResponse<http::response<http::string_body>>(http::status::not_modified, req.version());
|
||||||
head.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
head.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
head.keep_alive(req.keep_alive());
|
head.keep_alive(req.keep_alive());
|
||||||
promise.SendResponse(std::move(head));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,29 +126,27 @@ void directory_request_handler::handle_request_http(const address_type& address,
|
||||||
// Okay, we have a file so let us send the correct response.
|
// Okay, we have a file so let us send the correct response.
|
||||||
if (req.method() == http::verb::head)
|
if (req.method() == http::verb::head)
|
||||||
{
|
{
|
||||||
http::response<http::string_body> head{http::status::ok, req.version()};
|
auto& head = promise.CreateResponse<http::response<http::string_body>>(http::status::ok, req.version());
|
||||||
head.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
head.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
head.set(http::field::content_type, mime_type);
|
head.set(http::field::content_type, mime_type);
|
||||||
head.set(http::field::last_modified, last_modified);
|
head.set(http::field::last_modified, last_modified);
|
||||||
head.set(http::field::cache_control, "public, max-age=360");
|
head.set(http::field::cache_control, "public, max-age=360");
|
||||||
head.set(http::field::content_length, std::to_string(file_size));
|
head.set(http::field::content_length, std::to_string(file_size));
|
||||||
head.keep_alive(req.keep_alive());
|
head.keep_alive(req.keep_alive());
|
||||||
promise.SendResponse(std::move(head));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (req.method() == http::verb::get)
|
else if (req.method() == http::verb::get)
|
||||||
{
|
{/*
|
||||||
http::response<http::file_body> head{std::piecewise_construct,
|
auto& head = promise.CreateResponse<http::response<http::file_body>>(std::piecewise_construct,
|
||||||
std::make_tuple(std::move(body)),
|
std::make_tuple(std::move(body)),
|
||||||
std::make_tuple(http::status::ok, req.version())};
|
std::make_tuple(http::status::ok, req.version()));
|
||||||
|
|
||||||
head.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
head.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||||
head.set(http::field::content_type, mime_type);
|
head.set(http::field::content_type, mime_type);
|
||||||
head.set(http::field::last_modified, last_modified);
|
head.set(http::field::last_modified, last_modified);
|
||||||
head.set(http::field::cache_control, "public, max-age=360");
|
head.set(http::field::cache_control, "public, max-age=360");
|
||||||
head.set(http::field::content_length, std::to_string(file_size));
|
head.set(http::field::content_length, std::to_string(file_size));
|
||||||
head.keep_alive(req.keep_alive());
|
head.keep_alive(req.keep_alive());*/
|
||||||
//promise.SendResponse(std::move(head));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,4 +14,14 @@ response_promise::response_promise(callback_on_response cbOnResponse)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
response_promise::~response_promise() = default;
|
response_promise::~response_promise()
|
||||||
|
{
|
||||||
|
if (m_response_writer)
|
||||||
|
{
|
||||||
|
m_call_on_response(
|
||||||
|
[r = m_response_writer](boost::beast::tcp_stream& stream)
|
||||||
|
{
|
||||||
|
r->write_response(stream);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue