CppServer  1.0.4.0
C++ Server Library
http_session.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_HTTP_HTTP_SESSION_H
10 #define CPPSERVER_HTTP_HTTP_SESSION_H
11 
12 #include "http_request.h"
13 #include "http_response.h"
14 
15 #include "cache/filecache.h"
17 
18 namespace CppServer {
19 namespace HTTP {
20 
21 class HTTPServer;
22 
24 
30 {
31 public:
32  explicit HTTPSession(const std::shared_ptr<HTTPServer>& server);
33  HTTPSession(const HTTPSession&) = delete;
34  HTTPSession(HTTPSession&&) = delete;
35  virtual ~HTTPSession() = default;
36 
37  HTTPSession& operator=(const HTTPSession&) = delete;
39 
41  CppCommon::FileCache& cache() noexcept { return _cache; }
42  const CppCommon::FileCache& cache() const noexcept { return _cache; }
43 
45  HTTPResponse& response() noexcept { return _response; }
46  const HTTPResponse& response() const noexcept { return _response; }
47 
49 
52  size_t SendResponse() { return SendResponse(_response); }
54 
58  size_t SendResponse(const HTTPResponse& response) { return Send(response.cache()); }
59 
61 
65  size_t SendResponseBody(std::string_view body) { return Send(body); }
67 
72  size_t SendResponseBody(const void* buffer, size_t size) { return Send(buffer, size); }
73 
75 
79  size_t SendResponse(const CppCommon::Timespan& timeout) { return SendResponse(_response, timeout); }
81 
86  size_t SendResponse(const HTTPResponse& response, const CppCommon::Timespan& timeout) { return Send(response.cache(), timeout); }
87 
89 
94  size_t SendResponseBody(std::string_view body, const CppCommon::Timespan& timeout) { return Send(body, timeout); }
96 
102  size_t SendResponseBody(const void* buffer, size_t size, const CppCommon::Timespan& timeout) { return Send(buffer, size, timeout); }
103 
105 
110 
115 
117 
121  bool SendResponseBodyAsync(std::string_view body) { return SendAsync(body); }
123 
128  bool SendResponseBodyAsync(const void* buffer, size_t size) { return SendAsync(buffer, size); }
129 
130 protected:
131  void onReceived(const void* buffer, size_t size) override;
132  void onDisconnected() override;
133 
135 
141  virtual void onReceivedRequestHeader(const HTTPRequest& request) {}
142 
144 
150  virtual void onReceivedRequest(const HTTPRequest& request) {}
152 
163  virtual void onReceivedCachedRequest(const HTTPRequest& request, std::string_view content) { SendAsync(content); }
164 
166 
173  virtual void onReceivedRequestError(const HTTPRequest& request, const std::string& error) {}
174 
175 protected:
180 
181 private:
182  // Static content cache
183  CppCommon::FileCache& _cache;
184 
185  void onReceivedRequestInternal(const HTTPRequest& request);
186 };
187 
188 } // namespace HTTP
189 } // namespace CppServer
190 
191 #endif // CPPSERVER_HTTP_HTTP_SESSION_H
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous)
virtual size_t Send(const void *buffer, size_t size)
Send data to the client (synchronous)
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
Definition: tcp_session.h:48
const std::string & cache() const noexcept
Get the HTTP response cache content.
Definition: http_response.h:82
bool SendResponseBodyAsync(const void *buffer, size_t size)
Send the HTTP response body (asynchronous)
Definition: http_session.h:128
HTTPSession & operator=(const HTTPSession &)=delete
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
HTTPSession & operator=(HTTPSession &&)=delete
size_t SendResponseBody(std::string_view body)
Send the HTTP response body (synchronous)
Definition: http_session.h:65
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.
size_t SendResponse(const HTTPResponse &response, const CppCommon::Timespan &timeout)
Send the HTTP response with timeout (synchronous)
Definition: http_session.h:86
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
size_t SendResponseBody(std::string_view body, const CppCommon::Timespan &timeout)
Send the HTTP response body with timeout (synchronous)
Definition: http_session.h:94
bool SendResponseBodyAsync(std::string_view body)
Send the HTTP response body (asynchronous)
Definition: http_session.h:121
virtual void onReceivedRequestError(const HTTPRequest &request, const std::string &error)
Handle HTTP request error notification.
Definition: http_session.h:173
HTTPSession(const HTTPSession &)=delete
virtual ~HTTPSession()=default
size_t SendResponse(const HTTPResponse &response)
Send the HTTP response (synchronous)
Definition: http_session.h:58
HTTPResponse _response
HTTP response.
Definition: http_session.h:179
size_t SendResponse(const CppCommon::Timespan &timeout)
Send the current HTTP response with timeout (synchronous)
Definition: http_session.h:79
bool SendResponseAsync()
Send the current HTTP response (asynchronous)
Definition: http_session.h:108
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
size_t SendResponseBody(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Send the HTTP response body with timeout (synchronous)
Definition: http_session.h:102
size_t SendResponse()
Send the current HTTP response (synchronous)
Definition: http_session.h:52
HTTPSession(HTTPSession &&)=delete
bool SendResponseAsync(const HTTPResponse &response)
Send the HTTP response (asynchronous)
Definition: http_session.h:114
const CppCommon::FileCache & cache() const noexcept
Definition: http_session.h:42
const HTTPResponse & response() const noexcept
Definition: http_session.h:46
size_t SendResponseBody(const void *buffer, size_t size)
Send the HTTP response body (synchronous)
Definition: http_session.h:72
HTTP request definition.
HTTP response definition.
C++ Server project definitions.
Definition: asio.h:56
TCP session definition.