| 21 | { |
| 22 | public: |
| 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!", |
nothing calls this directly
no test coverage detected