Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
HttpAsyncServer.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "HttpConnection.h"
5#include "HttpExport.h"
6
7namespace SC
8{
9namespace TypeTraits
10{
12template <typename Base, typename Derived>
14{
15 static constexpr bool value = __is_base_of(Base, Derived);
16};
17
18} // namespace TypeTraits
19
21template <int ReadQueue, int WriteQueue, int HeaderBytes, int StreamBytes>
22struct SC_HTTP_EXPORT HttpAsyncConnection
23 : public HttpStaticConnection<ReadQueue, WriteQueue, HeaderBytes, StreamBytes, 8, HttpConnection>
24{
25};
26
30struct SC_HTTP_EXPORT HttpAsyncServerTlsOptions
31{
32 bool enabled = false;
33
34 Span<const char> pemCertificateChain;
35 Span<const char> pemPrivateKey;
36 Span<StringSpan> alpnProtocols;
37};
38
51struct SC_HTTP_EXPORT HttpAsyncServer
52{
54 template <typename T,
55 typename = typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<HttpConnection, T>::value>::type>
56 Result init(Span<T> clients)
57 {
58 return initInternal({clients.data(), clients.sizeInElements(), sizeof(T)});
59 }
60
61 template <typename T,
62 typename = typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<HttpConnection, T>::value>::type>
63 Result resize(Span<T> clients)
64 {
65 return resizeInternal({clients.data(), clients.sizeInElements(), sizeof(T)});
66 }
67
70 Result close();
71
77 Result start(AsyncEventLoop& loop, StringSpan address, uint16_t port);
78
81 Result stop();
82
85 void setMaxHeaderSize(uint32_t bytes) { maxHeaderSize = bytes; }
86
88 [[nodiscard]] uint32_t getMaxHeaderSize() const { return maxHeaderSize; }
89
91 void setTlsOptions(const HttpAsyncServerTlsOptions& options) { tlsOptions = options; }
92
94 void clearTlsOptions() { tlsOptions = {}; }
95
96 [[nodiscard]] const HttpAsyncServerTlsOptions& getTlsOptions() const { return tlsOptions; }
97
99 [[nodiscard]] bool isStarted() const { return state == State::Started; }
100
101 [[nodiscard]] const HttpConnectionsPool& getConnections() const { return connections; }
102
104 Function<void(HttpConnection&)> onRequest;
105
107 Function<void(Result)> onError;
108
112 void setDefaultKeepAlive(bool enabled) { defaultKeepAlive = enabled; }
113
115 [[nodiscard]] bool getDefaultKeepAlive() const { return defaultKeepAlive; }
116
119 void setMaxRequestsPerConnection(uint32_t maxRequests) { maxRequestsPerConnection = maxRequests; }
120
122 [[nodiscard]] uint32_t getMaxRequestsPerConnection() const { return maxRequestsPerConnection; }
123
124 private:
125 HttpConnectionsPool connections;
126
127 uint32_t maxHeaderSize = 8 * 1024;
128
129 HttpAsyncServerTlsOptions tlsOptions;
130
131 enum class State
132 {
133 Stopped, // Server was not started at all, or it was stopped and wait(ed)ForStopToFinish
134 Started, // Server has been started (successfully)
135 Stopping, // Server has stop() called and needs waitForStopToFinish() call
136 };
137
138 State state = State::Stopped;
139
140 bool defaultKeepAlive = true;
141 uint32_t maxRequestsPerConnection = 0;
142
143 void onNewClient(AsyncSocketAccept::Result& result);
144 void closeAsync(HttpConnection& requestClient);
145 void deactivateConnection(HttpConnection& requestClient);
146 void onStreamReceive(HttpConnection& client, AsyncBufferView::ID bufferID);
147 void onRequestBodyData(HttpConnection& client, AsyncBufferView::ID bufferID);
148
149 Result waitForStopToFinish();
150 Result initInternal(SpanWithStride<HttpConnection> connections);
151 Result resizeInternal(SpanWithStride<HttpConnection> connections);
152
153 AsyncEventLoop* eventLoop = nullptr;
154 SocketDescriptor serverSocket;
155 AsyncSocketAccept asyncServerAccept;
156
157 struct EventDataListener;
158 struct EventBodyDataListener;
159 struct EventEndListener;
160 struct EventCloseListener;
161};
162} // namespace SC
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1447
Adds compile-time configurable read and write queues to HttpConnection.
Definition HttpAsyncServer.h:24
TLS policy for future HttpAsyncServer HTTPS listeners.
Definition HttpAsyncServer.h:31
Async Http Server.
Definition HttpAsyncServer.h:52
Result start(AsyncEventLoop &loop, StringSpan address, uint16_t port)
Starts the http server on the given AsyncEventLoop, address and port.
void setTlsOptions(const HttpAsyncServerTlsOptions &options)
Sets TLS options used by future HTTPS listener integration.
Definition HttpAsyncServer.h:91
uint32_t getMaxRequestsPerConnection() const
Get the maximum requests per connection.
Definition HttpAsyncServer.h:122
Result stop()
Stops http server asynchronously pushing cancel and close requests to the event loop.
Result init(Span< T > clients)
Initializes the async server with all needed memory buffers.
Definition HttpAsyncServer.h:56
void clearTlsOptions()
Restores default plain HTTP listener options.
Definition HttpAsyncServer.h:94
bool getDefaultKeepAlive() const
Get the default keep-alive setting.
Definition HttpAsyncServer.h:115
void setDefaultKeepAlive(bool enabled)
Set default keep-alive behavior for all connections.
Definition HttpAsyncServer.h:112
Function< void(HttpConnection &)> onRequest
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.
Definition HttpAsyncServer.h:104
Function< void(Result)> onError
Called on accept, parse, protocol, or streaming errors before the affected connection is closed.
Definition HttpAsyncServer.h:107
void setMaxRequestsPerConnection(uint32_t maxRequests)
Set maximum requests per keep-alive connection.
Definition HttpAsyncServer.h:119
bool isStarted() const
Returns true if the server has been started.
Definition HttpAsyncServer.h:99
void setMaxHeaderSize(uint32_t bytes)
Sets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:85
uint32_t getMaxHeaderSize() const
Gets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:88
Result close()
Closes the server, removing references to the memory buffers passed during init.
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:490
A pool of HttpConnection that can be active or inactive.
Definition HttpConnection.h:589
Adds compile-time configurable read and write queues to any class subclassing HttpConnectionBase.
Definition HttpConnection.h:654
IsBaseOf evaluates to true if the type Base is a base class of Derived, false otherwise.
Definition HttpAsyncServer.h:14