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
39struct SC_HTTP_EXPORT HttpAsyncServer
40{
42 template <typename T,
45 {
46 return initInternal({clients.data(), clients.sizeInElements(), sizeof(T)});
47 }
48
49 template <typename T,
51 Result resize(Span<T> clients)
52 {
53 return resizeInternal({clients.data(), clients.sizeInElements(), sizeof(T)});
54 }
55
59
66
70
73 void setMaxHeaderSize(uint32_t bytes) { maxHeaderSize = bytes; }
74
76 [[nodiscard]] uint32_t getMaxHeaderSize() const { return maxHeaderSize; }
77
79 [[nodiscard]] bool isStarted() const { return state == State::Started; }
80
81 [[nodiscard]] const HttpConnectionsPool& getConnections() const { return connections; }
82
85
88
92 void setDefaultKeepAlive(bool enabled) { defaultKeepAlive = enabled; }
93
95 [[nodiscard]] bool getDefaultKeepAlive() const { return defaultKeepAlive; }
96
99 void setMaxRequestsPerConnection(uint32_t maxRequests) { maxRequestsPerConnection = maxRequests; }
100
102 [[nodiscard]] uint32_t getMaxRequestsPerConnection() const { return maxRequestsPerConnection; }
103
104 private:
105 HttpConnectionsPool connections;
106
107 uint32_t maxHeaderSize = 8 * 1024;
108
109 enum class State
110 {
111 Stopped, // Server was not started at all, or it was stopped and wait(ed)ForStopToFinish
112 Started, // Server has been started (successfully)
113 Stopping, // Server has stop() called and needs waitForStopToFinish() call
114 };
115
116 State state = State::Stopped;
117
118 bool defaultKeepAlive = true;
119 uint32_t maxRequestsPerConnection = 0;
120
121 void onNewClient(AsyncSocketAccept::Result& result);
122 void closeAsync(HttpConnection& requestClient);
123 void deactivateConnection(HttpConnection& requestClient);
124 void onStreamReceive(HttpConnection& client, AsyncBufferView::ID bufferID);
125 void onRequestBodyData(HttpConnection& client, AsyncBufferView::ID bufferID);
126
127 Result waitForStopToFinish();
128 Result initInternal(SpanWithStride<HttpConnection> connections);
129 Result resizeInternal(SpanWithStride<HttpConnection> connections);
130
131 AsyncEventLoop* eventLoop = nullptr;
132 SocketDescriptor serverSocket;
133 AsyncSocketAccept asyncServerAccept;
134
135 struct EventDataListener;
136 struct EventBodyDataListener;
137 struct EventEndListener;
138 struct EventCloseListener;
139};
140} // namespace SC
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:28
struct SC_FOUNDATION_EXPORT Function
Wraps function pointers, member functions and lambdas without ever allocating.
Definition Function.h:19
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1397
Adds compile-time configurable read and write queues to HttpConnection.
Definition HttpAsyncServer.h:24
Async Http Server.
Definition HttpAsyncServer.h:40
Result start(AsyncEventLoop &loop, StringSpan address, uint16_t port)
Starts the http server on the given AsyncEventLoop, address and port.
uint32_t getMaxRequestsPerConnection() const
Get the maximum requests per connection.
Definition HttpAsyncServer.h:102
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:44
bool getDefaultKeepAlive() const
Get the default keep-alive setting.
Definition HttpAsyncServer.h:95
void setDefaultKeepAlive(bool enabled)
Set default keep-alive behavior for all connections.
Definition HttpAsyncServer.h:92
Function< void(HttpConnection &)> onRequest
Called after enough data from a newly connected client has arrived, causing all headers to be parsed.
Definition HttpAsyncServer.h:84
Function< void(Result)> onError
Called on accept, parse, protocol, or streaming errors before the affected connection is closed.
Definition HttpAsyncServer.h:87
void setMaxRequestsPerConnection(uint32_t maxRequests)
Set maximum requests per keep-alive connection.
Definition HttpAsyncServer.h:99
bool isStarted() const
Returns true if the server has been started.
Definition HttpAsyncServer.h:79
void setMaxHeaderSize(uint32_t bytes)
Sets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:73
uint32_t getMaxHeaderSize() const
Gets the maximum accepted request-header size in bytes.
Definition HttpAsyncServer.h:76
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:467
A pool of HttpConnection that can be active or inactive.
Definition HttpConnection.h:557
Adds compile-time configurable read and write queues to any class subclassing HttpConnectionBase.
Definition HttpConnection.h:622
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
EnableIf conditionally defines a type if a boolean template parameter is true.
Definition TypeTraits.h:25
IsBaseOf evaluates to true if the type Base is a base class of Derived, false otherwise.
Definition HttpAsyncServer.h:14