CppServer  1.0.4.0
C++ Server Library
https_session.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_HTTP_HTTPS_SESSION_H
10 #define CPPSERVER_HTTP_HTTPS_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 HTTPSServer;
22 
24 
30 {
31 public:
32  explicit HTTPSSession(const std::shared_ptr<HTTPSServer>& server);
33  HTTPSSession(const HTTPSSession&) = delete;
35  virtual ~HTTPSSession() = default;
36 
37  HTTPSSession& operator=(const HTTPSSession&) = 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_HTTPS_SESSION_H
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous)
std::shared_ptr< SSLServer > & server() noexcept
Get the server.
Definition: ssl_session.h:48
virtual size_t Send(const void *buffer, size_t size)
Send data to the client (synchronous)
const std::string & cache() const noexcept
Get the HTTP response cache content.
Definition: http_response.h:82
virtual ~HTTPSSession()=default
bool SendResponseAsync(const HTTPResponse &response)
Send the HTTP response (asynchronous)
void onDisconnected() override
Handle session disconnected notification.
HTTPSSession & operator=(HTTPSSession &&)=delete
size_t SendResponse()
Send the current HTTP response (synchronous)
Definition: https_session.h:52
HTTPSSession(HTTPSSession &&)=delete
virtual void onReceivedRequestHeader(const HTTPRequest &request)
Handle HTTP request header received notification.
size_t SendResponseBody(std::string_view body)
Send the HTTP response body (synchronous)
Definition: https_session.h:65
HTTPSSession(const HTTPSSession &)=delete
bool SendResponseBodyAsync(std::string_view body)
Send the HTTP response body (asynchronous)
size_t SendResponse(const CppCommon::Timespan &timeout)
Send the current HTTP response with timeout (synchronous)
Definition: https_session.h:79
HTTPRequest _request
HTTP request.
size_t SendResponse(const HTTPResponse &response)
Send the HTTP response (synchronous)
Definition: https_session.h:58
HTTPSSession(const std::shared_ptr< HTTPSServer > &server)
size_t SendResponseBody(const void *buffer, size_t size)
Send the HTTP response body (synchronous)
Definition: https_session.h:72
size_t SendResponseBody(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Send the HTTP response body with timeout (synchronous)
virtual void onReceivedRequestError(const HTTPRequest &request, const std::string &error)
Handle HTTP request error notification.
HTTPResponse & response() noexcept
Get the HTTP response.
Definition: https_session.h:45
virtual void onReceivedRequest(const HTTPRequest &request)
Handle HTTP request received notification.
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
HTTPResponse _response
HTTP response.
bool SendResponseAsync()
Send the current HTTP response (asynchronous)
bool SendResponseBodyAsync(const void *buffer, size_t size)
Send the HTTP response body (asynchronous)
CppCommon::FileCache & cache() noexcept
Get the static content cache.
Definition: https_session.h:41
HTTPSSession & operator=(const HTTPSSession &)=delete
const CppCommon::FileCache & cache() const noexcept
Definition: https_session.h:42
size_t SendResponseBody(std::string_view body, const CppCommon::Timespan &timeout)
Send the HTTP response body with timeout (synchronous)
Definition: https_session.h:94
const HTTPResponse & response() const noexcept
Definition: https_session.h:46
size_t SendResponse(const HTTPResponse &response, const CppCommon::Timespan &timeout)
Send the HTTP response with timeout (synchronous)
Definition: https_session.h:86
virtual void onReceivedCachedRequest(const HTTPRequest &request, std::string_view content)
Handle HTTP cached request received notification.
HTTP request definition.
HTTP response definition.
C++ Server project definitions.
Definition: asio.h:56
SSL session definition.