11 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__HTTP_SESSION 12 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__HTTP_SESSION 23 #include <Balau/Network/Http/Server/ClientSession.hpp> 27 #pragma clang diagnostic push 28 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" 46 class HttpSession final :
public std::enable_shared_from_this<HttpSession> {
55 public:
HttpSession(Impl::HttpSessions & httpSessions_,
56 Impl::ClientSessions & clientSessions_,
57 std::shared_ptr<HttpServerConfiguration> serverConfiguration_,
58 TCP::socket && socket_);
64 return *serverConfiguration;
71 return socket.remote_endpoint().address();
80 const std::string & extraLogging =
"") {
81 sendResponse(std::move(response), configuration().logger, extraLogging);
90 const BalauLogger & log,
91 const std::string & extraLogging =
"") {
94 ,
"{} - {} {} {} - {} {} - \"{}\"{} - [{}]" 95 , remoteIpAddress().to_string()
97 , response.version() == 11 ?
"HTTP/1.1" :
"HTTP/1.0" 98 , response.result_int()
99 , response[Field::content_type]
100 , response[Field::content_length]
102 , extraLogging.empty() ?
"" :
" - " + extraLogging
103 , request[Field::user_agent]
108 Field::set_cookie, serverConfiguration->sessionCookieName +
"=" + clientSession->sessionId +
"; HttpOnly" 112 auto sharedResponse = std::make_shared<Response<BodyT>>(std::move(response));
113 auto sharedVoidResponse = std::shared_ptr<void>(sharedResponse);
114 cachedResponse = sharedVoidResponse;
119 , boost::asio::bind_executor(
122 &HttpSession::onWrite
124 , std::placeholders::_1
125 , std::placeholders::_2
126 , sharedResponse->need_eof()
134 friend class Listener;
135 friend class ::Balau::Network::Http::Impl::HttpSessions;
140 public:
void doRead();
145 public:
void close();
148 private:
void onRead(boost::system::error_code errorCode, std::size_t bytesTransferred);
152 private:
bool validateRequest(boost::system::error_code errorCode,
const StringRequest & request);
158 std::map<std::string, std::string> variables;
160 switch (request.method()) {
162 serverConfiguration->httpHandler->handleGetRequest(*
this, request, variables);
167 serverConfiguration->httpHandler->handleHeadRequest(*
this, request, variables);
172 serverConfiguration->httpHandler->handlePostRequest(*
this, request, variables);
181 }
catch (
const std::exception & e) {
182 BalauBalauLogError(serverConfiguration->logger,
"Exception thrown during request: {}", e);
185 *
this, request,
"The server experienced an error during the request. A report has been logged." 189 BalauBalauLogError(serverConfiguration->logger,
"Unknown exception thrown during request: {}");
192 *
this, request,
"The server experienced an error during the request. A report has been logged." 198 private:
void onWrite(boost::system::error_code errorCode, std::size_t bytesTransferred,
bool close);
199 private:
void doClose();
200 private:
void parseCookies();
201 private:
void setClientSession();
203 private: Impl::HttpSessions & httpSessions;
204 private: Impl::ClientSessions & clientSessions;
205 private: std::shared_ptr<ClientSession> clientSession;
206 private: std::shared_ptr<HttpServerConfiguration> serverConfiguration;
207 private: boost::asio::strand<boost::asio::io_context::executor_type> strand;
208 private: TCP::socket socket;
211 private: std::string cookieString;
212 private: std::map<std::string_view, std::string_view> cookies;
213 private: std::shared_ptr<void> cachedResponse;
214 private: std::allocator<char> allocator;
219 #pragma clang diagnostic pop 221 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER__HTTP_SESSION Components and utilities working on HTTP data transmission.
Definition: HttpClient.hpp:26
static StringResponse createBadRequestResponse(HttpSession &session, const StringRequest &request, std::string_view errorMessage)
Create a bad request response.
Manages the handling of HTTP messages and WebSocket upgrade requests in an HTTP connection.
Definition: HttpSession.hpp:46
HttpServerConfiguration & configuration() const
Get the shared state of the http server.
Definition: HttpSession.hpp:63
boost::beast::flat_buffer Buffer
A data buffer used in HTTP code.
Definition: NetworkTypes.hpp:308
Shared state between HTTP sessions.
Definition: HttpServerConfiguration.hpp:44
void sendResponse(Response< BodyT > &&response, const std::string &extraLogging="")
Send the response back to the client.
Definition: HttpSession.hpp:79
Abstract base class of HTTP web application handlers.
boost::asio::ip::address Address
An IP4 or IP6 address.
Definition: NetworkTypes.hpp:225
Request< StringBody > StringRequest
A request with a string body.
Definition: NetworkTypes.hpp:267
void sendResponse(Response< BodyT > &&response, const BalauLogger &log, const std::string &extraLogging="")
Send the response back to the client.
Definition: HttpSession.hpp:89
Manages the handling of WebSocket messages in a WebSocket connection.
boost::beast::http::response< Body, Fields > Response
The response type.
Definition: NetworkTypes.hpp:278
Shared state between HTTP sessions.
static StringResponse createServerErrorResponse(HttpSession &session, const StringRequest &request, std::string_view errorMessage)
Create a server error response.
Address remoteIpAddress() const
Get the requester's IP address for logging.
Definition: HttpSession.hpp:70