12 #include "../mbedTLS++/SslConfig.h"
21 #pragma warning(disable:4355)
56 LOGWARNING(
"HTTP server error on port %d: %d (%s)",
m_Port, a_ErrorCode, a_ErrorMsg.c_str());
90 if (!CertFile.empty() && !KeyFile.empty())
92 auto Cert = std::make_shared<cX509Cert>();
93 int res = Cert->Parse(CertFile.data(), CertFile.size());
96 auto CertPrivKey = std::make_shared<cCryptoKey>();
97 res = CertPrivKey->ParsePrivate(KeyFile.data(), KeyFile.size(),
"");
102 Config->SetOwnCert(Cert, CertPrivKey);
108 LOGWARNING(
"WebServer: Cannot read HTTPS certificate private key: -0x%x", -res);
113 LOGWARNING(
"WebServer: Cannot read HTTPS certificate: -0x%x", -res);
120 LOGWARNING(
"WebServer: The server will run in unsecured HTTP mode.");
121 LOGINFO(
"Put a valid HTTPS certificate in file 'webadmin/httpscert.crt' and its corresponding private key to 'webadmin/httpskey.pem' (without any password) to enable HTTPS support");
125 LOGINFO(
"WebServer: The server will run in secure HTTPS mode.");
140 for (
const auto & port : a_Ports)
145 LOGWARNING(
"WebServer: Invalid port value: \"%s\". Ignoring.", port.c_str());
148 auto Handle =
cNetwork::Listen(PortNum, std::make_shared<cHTTPServerListenCallbacks>(*
this, PortNum));
149 if (Handle->IsListening())
152 ports.push_back(port);
158 for (
const auto & port: ports)
160 if (!reportPorts.empty())
162 reportPorts.append(
", ");
164 reportPorts.append(port);
166 LOGINFO(
"WebAdmin is running on port(s) %s", reportPorts.c_str());
191 UNUSED(a_RemoteIPAddress);
196 return std::make_shared<cSslHTTPServerConnection>(*
this,
m_SslConfig);
200 return std::make_shared<cHTTPServerConnection>(*
this);
void LOGWARNING(std::string_view a_Format, const Args &... args)
void LOGINFO(std::string_view a_Format, const Args &... args)
std::vector< AString > AStringVector
bool StringToInteger(const AString &a_str, T &a_Num)
Parses any integer type.
Provides storage for an incoming HTTP request.
cHTTPServerListenCallbacks(cHTTPServer &a_HTTPServer, UInt16 a_Port)
virtual void OnAccepted(cTCPLink &a_Link) override
Called when the TCP server created with Listen() creates a new link for an incoming connection.
UInt16 m_Port
The port for which this instance is responsible.
virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString &a_RemoteIPAddress, UInt16 a_RemotePort) override
Called when the TCP server created with Listen() receives a new incoming connection.
cHTTPServer & m_HTTPServer
The HTTP server instance that we're attached to.
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
Called when the socket fails to listen on the specified port.
cCallbacks * m_Callbacks
The callbacks to call for various events.
bool Initialize(void)
Initializes the server - reads the cert files etc.
void NewRequest(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)
Called by cHTTPServerConnection when it finishes parsing the request header.
void RequestBody(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request, const void *a_Data, size_t a_Size)
Called by cHTTPConenction when it receives more data for the request body.
cServerHandlePtrs m_ServerHandles
The cNetwork API handle for the listening socket.
cTCPLink::cCallbacksPtr OnIncomingConnection(const AString &a_RemoteIPAddress, UInt16 a_RemotePort)
Called by cHTTPServerListenCallbacks when there's a new incoming connection.
void Stop(void)
Stops the server, drops all current connections.
bool Start(cCallbacks &a_Callbacks, const AStringVector &a_Ports)
Starts the server and assigns the callbacks to use for incoming requests.
std::shared_ptr< const cSslConfig > m_SslConfig
Configuration for server ssl connections.
void RequestFinished(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)
Called by cHTTPServerConnection when it detects that the request has finished (all of its body has be...
virtual void OnRequestBody(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request, const char *a_Data, size_t a_Size)=0
Called when another part of request body has arrived.
virtual void OnRequestBegun(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)=0
Called when a new request arrives over a connection and all its headers have been parsed.
virtual void OnRequestFinished(cHTTPServerConnection &a_Connection, cHTTPIncomingRequest &a_Request)=0
Called when the request body has been fully received in previous calls to OnRequestBody()
static std::shared_ptr< cSslConfig > MakeDefaultConfig(bool a_IsClient)
Creates a new config with some sensible defaults on top of mbedTLS basic settings.
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
Interface that provides the methods available on a single TCP connection.
std::shared_ptr< cCallbacks > cCallbacksPtr
static cServerHandlePtr Listen(UInt16 a_Port, cListenCallbacksPtr a_ListenCallbacks)
Opens up the specified port for incoming connections.
Callbacks used when listening for incoming connections as a server.