arch-repo-manager/librepomgr/webapi/session.h

79 lines
2.0 KiB
C
Raw Normal View History

2021-01-25 00:24:31 +01:00
#ifndef LIBREPOMGR_SESSION_H
#define LIBREPOMGR_SESSION_H
#include "./typedefs.h"
2022-07-10 20:08:31 +02:00
#include "../global.h"
2021-01-25 00:24:31 +01:00
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp>
2022-07-10 20:08:31 +02:00
#include <memory>
namespace Io {
class PasswordFile;
}
2021-01-25 00:24:31 +01:00
namespace LibRepoMgr {
struct ServiceSetup;
namespace WebAPI {
2022-07-10 20:08:31 +02:00
class LIBREPOMGR_EXPORT Session : public std::enable_shared_from_this<Session> {
2021-01-25 00:24:31 +01:00
public:
Session(boost::asio::ip::tcp::socket &&socket, ServiceSetup &config);
2022-07-10 20:08:31 +02:00
~Session();
2021-01-25 00:24:31 +01:00
void receive();
void respond(std::shared_ptr<Response> &&response);
void respond(
const char *localFilePath, boost::beast::string_view mimeType, boost::beast::string_view contentDisposition, std::string_view urlPath);
2021-01-25 00:24:31 +01:00
void close();
const Request &request() const;
void assignEmptyRequest();
2021-01-25 00:24:31 +01:00
boost::asio::ip::tcp::socket &socket();
void received(boost::system::error_code ec, std::size_t bytesTransferred);
void responded(boost::system::error_code ec, std::size_t bytesTransferred, bool shouldClose);
static boost::beast::string_view determineMimeType(std::string_view path, boost::beast::string_view fallback = "text/plain");
2022-07-10 20:08:31 +02:00
std::unique_ptr<Io::PasswordFile> &&secrets();
2021-01-25 00:24:31 +01:00
private:
boost::asio::ip::tcp::socket m_socket;
boost::asio::strand<boost::asio::ip::tcp::socket::executor_type> m_strand;
boost::beast::flat_buffer m_buffer;
std::unique_ptr<RequestParser> m_parser;
ServiceSetup &m_setup;
std::shared_ptr<void> m_res;
2022-07-10 20:08:31 +02:00
std::unique_ptr<Io::PasswordFile> m_secrets;
2021-01-25 00:24:31 +01:00
};
inline const Request &Session::request() const
{
return m_parser->get();
}
inline void Session::assignEmptyRequest()
{
m_parser = std::make_unique<RequestParser>();
}
2021-01-25 00:24:31 +01:00
inline boost::asio::ip::tcp::socket &Session::socket()
{
return m_socket;
}
2022-07-10 20:08:31 +02:00
inline std::unique_ptr<Io::PasswordFile> &&Session::secrets()
{
2022-07-10 20:08:31 +02:00
return std::move(m_secrets);
}
2021-01-25 00:24:31 +01:00
} // namespace WebAPI
} // namespace LibRepoMgr
#endif // LIBREPOMGR_SESSION_H