MCPcopy Create free account
hub / github.com/azadkuh/qhttp / ClientHandler

Class ClientHandler

example/basic-server/main.cpp:20–74  ·  view source on GitHub ↗

connection class for gathering incoming chunks of data from HTTP client. * @warning please note that the incoming request instance is the parent of * this QObject instance. Thus this class will be deleted automatically. * */

Source from the content-addressed store, hash-verified

18 * this QObject instance. Thus this class will be deleted automatically.
19 * */
20class ClientHandler : public QObject
21{
22public:
23 explicit ClientHandler(quint64 id, QHttpRequest* req, QHttpResponse* res)
24 : QObject(req /* as parent*/), iconnectionId(id) {
25
26 qDebug("connection #%llu ..." , id);
27
28 // automatically collect http body(data) upto 1KB
29 req->collectData(1024);
30
31 // when all the incoming data are gathered, send some response to client.
32 req->onEnd([this, req, res]() {
33
34 qDebug(" connection (#%llu): request from %s:%d\n method: %s url: %s",
35 iconnectionId,
36 req->remoteAddress().toUtf8().constData(),
37 req->remotePort(),
38 qhttp::Stringify::toString(req->method()),
39 qPrintable(req->url().toString())
40 );
41
42 if ( req->collectedData().size() > 0 )
43 qDebug(" body (#%llu): %s",
44 iconnectionId,
45 req->collectedData().constData()
46 );
47
48 QString message =
49 QString("Hello World\n packet count = %1\n time = %2\n")
50 .arg(iconnectionId)
51 .arg(QLocale::c().toString(QDateTime::currentDateTime(),
52 "yyyy-MM-dd hh:mm:ss")
53 );
54
55 res->setStatusCode(qhttp::ESTATUS_OK);
56 res->addHeaderValue("content-length", message.size());
57 res->end(message.toUtf8());
58
59 if ( req->headers().keyHasValue("command", "quit") ) {
60 qDebug("\n\na quit has been requested!\n");
61 QCoreApplication::instance()->quit();
62 }
63 });
64 }
65
66 virtual ~ClientHandler() {
67 qDebug(" ~ClientHandler(#%llu): I've being called automatically!",
68 iconnectionId
69 );
70 }
71
72protected:
73 quint64 iconnectionId;
74};
75
76///////////////////////////////////////////////////////////////////////////////
77} // namespace anon

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected