Async 1.8.0
AsyncHttpServer_demo.cpp

An example of how to use the HttpServerConnection class in a client

// A demo http/https server.
// Run AsyncSslX509_demo first to generate CA, key and certificate
// Use a web browser or curl to access:
//
// curl http://localhost:8080
// curl --cacert demo_ca.crt https://localhost:8443
//
#include <sstream>
#include <AsyncTcpServer.h>
void requestReceived(Async::HttpServerConnection *con,
{
std::cout << "--- " << req.method << " " << req.target << std::endl;
if ((req.method != "GET") && (req.method != "HEAD"))
{
res.setCode(501);
res.setContent("text/plain", req.method + ": Method not implemented");
con->write(res);
return;
}
std::ostringstream os;
os << "{"
<< "\"method\":\"" << req.method << "\","
<< "\"target\":\"" << req.target << "\","
<< "\"client-proto-major\":" << req.ver_major << ","
<< "\"client-proto-minor\":" << req.ver_minor << ","
<< "\"headers\":{";
Async::HttpServerConnection::Headers::const_iterator it;
for (it=req.headers.begin(); it!=req.headers.end(); ++it)
{
std::cout << (*it).first << ": " << (*it).second << std::endl;
if (it != req.headers.begin())
{
os << ",";
}
os << "\"" << (*it).first << "\":\"" << (*it).second << "\"";
}
os << "}}";
res.setContent("application/json", os.str());
if (req.method == "HEAD")
{
res.setSendContent(false);
}
res.setCode(200);
con->write(res);
} /* requestReceived */
void clientConnected(Async::HttpServerConnection *con)
{
std::cout << "/// Client connected: "
<< con->remoteHost() << ":" << con->remotePort() << std::endl;
con->requestReceived.connect(sigc::ptr_fun(&requestReceived));
} /* clientConnected */
void clientDisconnected(Async::HttpServerConnection *con,
{
std::cout << "\\\\\\ Client disconnected: "
<< con->remoteHost() << ":" << con->remotePort()
<< std::endl;
} /* clientConnected */
void sslClientConnected(Async::HttpServerConnection *con)
{
std::cout << "/// SSL Client connected: "
<< con->remoteHost() << ":" << con->remotePort() << std::endl;
con->requestReceived.connect(sigc::ptr_fun(&requestReceived));
con->enableSsl(true);
} /* sslClientConnected */
void sslClientDisconnected(Async::HttpServerConnection *con,
{
std::cout << "\\\\\\ SSL Client disconnected: "
<< con->remoteHost() << ":" << con->remotePort()
<< std::endl;
} /* sslClientConnected */
int main(void)
{
app.catchUnixSignal(SIGINT);
app.catchUnixSignal(SIGTERM);
app.unixSignalCaught.connect(
sigc::hide(sigc::mem_fun(app, &Async::CppApplication::quit)));
// Listen for http connections on TCP port 8080
server.clientConnected.connect(sigc::ptr_fun(&clientConnected));
server.clientDisconnected.connect(sigc::ptr_fun(&clientDisconnected));
// Listen for https connections on TCP port 8443
ctx.setCertificateFiles("demo.key", "demo.crt");
ssl_server.setSslContext(ctx);
ssl_server.clientConnected.connect(sigc::ptr_fun(&sslClientConnected));
ssl_server.clientDisconnected.connect(sigc::ptr_fun(&sslClientDisconnected));
app.exec();
}
The core class for writing asyncronous cpp applications.
A simple HTTP Server connection class.
A class for creating a TCP server.
An application class for writing non GUI applications.
void quit(void)
Exit the application main loop.
void catchUnixSignal(int signum)
Catch the specified UNIX signal.
void exec(void)
Execute the application main loop.
sigc::signal< void, int > unixSignalCaught
A signal that is emitted when a monitored UNIX signal is caught.
void setContent(const std::string &content_type, const std::string &content)
A class representing a HTTP server side connection.
sigc::signal< void, HttpServerConnection *, Request & > requestReceived
A signal that is emitted when a HTTP request has been received on the connection.
virtual bool write(const Response &res)
Send a HTTP response.
SSL context meant to be used with TcpConnection and friends.
bool setCertificateFiles(const std::string &keyfile, const std::string &crtfile)
Set which key and certificate file to use for connections.
static const char * disconnectReasonStr(DisconnectReason reason)
Translate disconnect reason to a string.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
uint16_t remotePort(void) const
Return the remote port used.
DisconnectReason
Reason code for disconnects.
void enableSsl(bool enable)
Enable or disable TLS for this connection.
A class for creating a TCP server.