CppServer 1.0.4.0
C++ Server Library
Loading...
Searching...
No Matches
http_session.cpp
Go to the documentation of this file.
1
11
12namespace CppServer {
13namespace HTTP {
14
15HTTPSession::HTTPSession(const std::shared_ptr<HTTPServer>& server)
16 : Asio::TCPSession(server),
17 _cache(server->cache())
18{
19}
20
21void HTTPSession::onReceived(const void* buffer, size_t size)
22{
23 // Receive HTTP request header
24 if (_request.IsPendingHeader())
25 {
26 if (_request.ReceiveHeader(buffer, size))
28
29 size = 0;
30 }
31
32 // Check for HTTP request error
33 if (_request.error())
34 {
35 onReceivedRequestError(_request, "Invalid HTTP request!");
37 Disconnect();
38 return;
39 }
40
41 // Receive HTTP request body
42 if (_request.ReceiveBody(buffer, size))
43 {
44 onReceivedRequestInternal(_request);
46 return;
47 }
48
49 // Check for HTTP request error
50 if (_request.error())
51 {
52 onReceivedRequestError(_request, "Invalid HTTP request!");
54 Disconnect();
55 return;
56 }
57}
58
60{
61 // Receive HTTP request body
62 if (_request.IsPendingBody())
63 {
64 onReceivedRequestInternal(_request);
66 return;
67 }
68}
69
70void HTTPSession::onReceivedRequestInternal(const HTTPRequest& request)
71{
72 // Try to get the cached response
73 if (request.method() == "GET")
74 {
75 std::string_view url = request.url();
76 size_t index = url.find('?');
77 auto response = cache().find(std::string((index == std::string_view::npos) ? url : url.substr(0, index)));
78 if (response.first)
79 {
80 // Process the request with the cached response
81 onReceivedCachedRequest(request, response.second);
82 return;
83 }
84 }
85
86 // Process the request
87 onReceivedRequest(request);
88}
89
90} // namespace HTTP
91} // namespace CppServer
virtual bool Disconnect()
Disconnect the session.
Definition tcp_session.h:79
HTTPRequest & Clear()
Clear the HTTP request cache.
std::string_view method() const noexcept
Get the HTTP request method.
bool error() const noexcept
Is the HTTP request error flag set?
std::string_view url() const noexcept
Get the HTTP request URL.
HTTPRequest _request
HTTP request.
virtual void onReceivedCachedRequest(const HTTPRequest &request, std::string_view content)
Handle HTTP cached request received notification.
virtual void onReceivedRequestHeader(const HTTPRequest &request)
Handle HTTP request header received notification.
HTTPSession(const std::shared_ptr< HTTPServer > &server)
void onDisconnected() override
Handle session disconnected notification.
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
virtual void onReceivedRequestError(const HTTPRequest &request, const std::string &error)
Handle HTTP request error notification.
CppCommon::FileCache & cache() noexcept
Get the static content cache.
virtual void onReceivedRequest(const HTTPRequest &request)
Handle HTTP request received notification.
HTTPResponse & response() noexcept
Get the HTTP response.
HTTP server definition.
HTTP session definition.
C++ Server project definitions.
Definition asio.h:56