CppServer  1.0.4.0
C++ Server Library
http_session.cpp
Go to the documentation of this file.
1 
11 
12 namespace CppServer {
13 namespace HTTP {
14 
15 HTTPSession::HTTPSession(const std::shared_ptr<HTTPServer>& server)
16  : Asio::TCPSession(server),
17  _cache(server->cache())
18 {
19 }
20 
21 void 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!");
36  _request.Clear();
37  Disconnect();
38  return;
39  }
40 
41  // Receive HTTP request body
42  if (_request.ReceiveBody(buffer, size))
43  {
44  onReceivedRequestInternal(_request);
45  _request.Clear();
46  return;
47  }
48 
49  // Check for HTTP request error
50  if (_request.error())
51  {
52  onReceivedRequestError(_request, "Invalid HTTP request!");
53  _request.Clear();
54  Disconnect();
55  return;
56  }
57 }
58 
60 {
61  // Receive HTTP request body
62  if (_request.IsPendingBody())
63  {
64  onReceivedRequestInternal(_request);
65  _request.Clear();
66  return;
67  }
68 }
69 
70 void 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.
Definition: http_request.h:58
bool error() const noexcept
Is the HTTP request error flag set?
Definition: http_request.h:55
std::string_view url() const noexcept
Get the HTTP request URL.
Definition: http_request.h:60
HTTPRequest _request
HTTP request.
Definition: http_session.h:177
virtual void onReceivedCachedRequest(const HTTPRequest &request, std::string_view content)
Handle HTTP cached request received notification.
Definition: http_session.h:163
virtual void onReceivedRequestHeader(const HTTPRequest &request)
Handle HTTP request header received notification.
Definition: http_session.h:141
HTTPSession(const std::shared_ptr< HTTPServer > &server)
HTTPResponse & response() noexcept
Get the HTTP response.
Definition: http_session.h:45
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.
Definition: http_session.h:173
CppCommon::FileCache & cache() noexcept
Get the static content cache.
Definition: http_session.h:41
virtual void onReceivedRequest(const HTTPRequest &request)
Handle HTTP request received notification.
Definition: http_session.h:150
HTTP server definition.
HTTP session definition.
C++ Server project definitions.
Definition: asio.h:56