Async 1.7.0
AsyncHttpServer_demo.cpp

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

#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 */
int main(void)
{
app.catchUnixSignal(SIGINT);
app.catchUnixSignal(SIGTERM);
app.unixSignalCaught.connect(
sigc::hide(sigc::mem_fun(app, &Async::CppApplication::quit)));
server.clientConnected.connect(sigc::ptr_fun(&clientConnected));
server.clientDisconnected.connect(sigc::ptr_fun(&clientDisconnected));
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.
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.
A class for creating a TCP server.