AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
AsyncFw::HttpServer Class Reference

The HttpServer class. More...

#include <AsyncFw/HttpServer>

Collaboration diagram for AsyncFw::HttpServer:
[legend]

Public Types

using RulesMap = std::multimap<std::string, std::unique_ptr<HttpRule>>
 

Public Member Functions

 HttpServer (const std::string &={})
 
template<typename A>
void addRoute (const std::string &url, Request::Method method, A action, const std::any &data={})
 
template<typename O, typename A>
void addRoute (const O object, const std::string &url, Request::Method method, A action, const std::any &data={})
 
template<typename T>
void clearConnections (const T &_data)
 
void clearConnections ()
 
template<typename T>
int sendToWebSockets (const T &_data, const AsyncFw::DataArray &_da)
 
void sendToWebSockets (const std::string &)
 
void disconnectFromHost (TcpSocket *socket)
 
bool listen (uint16_t port)
 
void close ()
 
uint16_t port ()
 
bool hasTls ()
 
void addRule (const std::string &, const Request::Method, std::function< void(const Request &)>)
 
bool execRule (const Request &)
 
void setTlsContext (const AsyncFw::TlsContext &)
 
void setEnableCorsRequests (bool)
 
void setPeek (std::function< bool(const Request &, const std::any &)> f)
 

Static Public Member Functions

static HttpServerinstance ()
 

Public Attributes

AsyncFw::FunctionConnectorProtected< HttpServer >::Connector< int, const std::string &, bool * > incoming {AsyncFw::AbstractFunctionConnector::SyncOnly}
 

Protected Member Functions

virtual void fileUploadProgress (TcpSocket *, int)
 

Protected Attributes

RulesMap rules
 

Friends

LogStreamoperator<< (LogStream &log, const HttpServer &s)
 

Detailed Description

The HttpServer class.

Example:

#include <AsyncFw/HttpServer>
#include <AsyncFw/MainThread>
#include <AsyncFw/LogStream>
using namespace AsyncFw;
int main(int argc, char *argv[]) {
HttpServer _http {HTTP_SERVER_HOME};
_http.addRoute("/quit", HttpServer::Request::Method::Get, [](const HttpServer::Request &request) {
HttpServer::Response *resp = request.response();
resp->setMimeType("octet-stream");
resp->setStatusCode(HttpServer::Response::StatusCode::Ok);
resp->addHeader("ContentLenght:0");
resp->send();
MainThread::exit();
});
_http.listen(18080);
if (argc == 2 && std::string(argv[1]) == "--tst") {
HttpSocket *_socket = HttpSocket::create();
_socket->stateChanged([_socket](const AsyncFw::AbstractSocket::State state) {
if (state == AsyncFw::AbstractSocket::State::Active) {
logDebug() << "Send request";
_socket->write("GET / HTTP/1.1\r\n\r\n");
}
});
_socket->received([_socket](const AsyncFw::DataArray &_da) {
lsDebug() << _da.view(0, _da.size() > 1024 ? 1024 : _da.size());
_socket->write("GET /quit HTTP/1.1\r\n\r\n");
});
_socket->connect("127.0.0.1", 18080);
}
lsNotice() << "Start Applicaiton";
int ret = MainThread::exec();
lsNotice() << "End Applicaiton" << ret;
return ret;
}

The documentation for this class was generated from the following files: