#include "bodydata.h" #include #include #include #include #include #include #include /// BodyData BodyData::BodyData() { QHttpServer *server = new QHttpServer(this); connect(server, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)), this, SLOT(handleRequest(QHttpRequest*, QHttpResponse*))); server->listen(QHostAddress::Any, 8080); } void BodyData::handleRequest(QHttpRequest *req, QHttpResponse *resp) { new Responder(req, resp); } /// Responder Responder::Responder(QHttpRequest *req, QHttpResponse *resp) : m_req(req) , m_resp(resp) { QRegExp exp("^/user/([a-z]+$)"); if (exp.indexIn(req->path()) == -1) { resp->writeHead(403); resp->end(QByteArray("You aren't allowed here!")); /// @todo There should be a way to tell request to stop streaming data return; } resp->setHeader("Content-Type", "text/html"); resp->writeHead(200); QString name = exp.capturedTexts()[1]; QString bodyStart = tr("BodyData App

Hello %1!

").arg(name); resp->write(bodyStart.toUtf8()); connect(req, SIGNAL(data(const QByteArray&)), this, SLOT(accumulate(const QByteArray&))); connect(req, SIGNAL(end()), this, SLOT(reply())); connect(m_resp, SIGNAL(done()), this, SLOT(deleteLater())); } Responder::~Responder() { } void Responder::accumulate(const QByteArray &data) { m_resp->write(data); } void Responder::reply() { m_resp->end(QByteArray("

")); } /// main int main(int argc, char **argv) { QCoreApplication app(argc, argv); BodyData bodydata; app.exec(); }