http: Introduce HTTPServer class and implement binding to listening socket

Introduce a new low-level socket managing class `HTTPServer`.

BindAndStartListening() was copied from CConnMan's BindListenPort()
in net.cpp and modernized.

Unit-test it with a new class `SocketTestingSetup` which mocks
`CreateSock()` and will enable mock client I/O in future commits.

Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
This commit is contained in:
Matthew Zipkin
2025-06-04 13:52:56 -04:00
parent 9463e98781
commit f5bc018948
5 changed files with 178 additions and 1 deletions

View File

@@ -6,13 +6,18 @@
#define BITCOIN_HTTPSERVER_H
#include <functional>
#include <memory>
#include <optional>
#include <span>
#include <stdexcept>
#include <string>
#include <vector>
#include <netaddress.h>
#include <rpc/protocol.h>
#include <util/byte_units.h>
#include <util/expected.h>
#include <util/sock.h>
#include <util/strencodings.h>
#include <util/string.h>
@@ -297,6 +302,33 @@ public:
bool LoadBody(LineReader& reader);
/// @}
};
class HTTPServer
{
public:
/**
* Bind to a new address:port, start listening and add the listen socket to `m_listen`.
* @param[in] to Where to bind.
* @returns {} or the reason for failure.
*/
util::Expected<void, std::string> BindAndStartListening(const CService& to);
/**
* Stop listening by closing all listening sockets.
*/
void StopListening();
/**
* Get the number of sockets the server is bound to and listening on
*/
size_t GetListeningSocketCount() const { return m_listen.size(); }
private:
/**
* List of listening sockets.
*/
std::vector<std::shared_ptr<Sock>> m_listen;
};
} // namespace http_bitcoin
#endif // BITCOIN_HTTPSERVER_H