17 #include <QtConcurrent>
18 #include <QNetworkAccessManager>
19 #include <QHostAddress>
20 #include <qhttpserver.h>
21 #include <qhttprequest.h>
22 #include <qhttpresponse.h>
30 #include <QJsonObject>
31 #include <QJsonDocument>
34 #include <QApplication>
45 mRequests.push_front(RequestType(req,resp));
47 connect(req, SIGNAL(
end()),
this, SLOT(onRequestSuccessful()));
50 void HttpRequestHandler::onRequestSuccessful()
52 for (QList<RequestType>::iterator iter = mRequests.begin(); iter!=mRequests.end(); )
54 if (iter->req->successful())
57 iter = mRequests.erase(iter);
81 CX_LOG_DEBUG() << QString(
"Web server request [%1] from [%2]")
83 .arg(req->remoteAddress());
85 if (req->path() ==
"/")
89 else if (req->path().startsWith(
"/screen"))
93 else if (req->path().startsWith(
"/layout"))
105 CX_ASSERT(req->path().startsWith(
"/layout"));
107 if (req->path() ==
"/layout")
111 else if (req->path()==
"/layout/display/stream")
115 else if (req->path() ==
"/layout/display")
129 if (req->method()==QHttpRequest::HTTP_GET)
141 CX_ASSERT(req->path()==
"/layout/display/stream");
143 if (req->method()==QHttpRequest::HTTP_PUT)
147 else if (req->method()==QHttpRequest::HTTP_DELETE)
159 CX_ASSERT(req->path()==
"/layout/display");
161 if (req->method()==QHttpRequest::HTTP_GET)
165 else if (req->method()==QHttpRequest::HTTP_PUT)
169 else if (req->method()==QHttpRequest::HTTP_DELETE)
182 QStringList layouts =
mApi->getAvailableLayouts();
192 root.insert(
"Layouts", QJsonArray::fromStringList(layouts));
193 QByteArray ba = QJsonDocument(root).toJson();
195 resp->setHeader(
"Content-Type",
"application/json");
196 resp->setHeader(
"Content-Length", QString::number(ba.size()));
197 resp->writeHead(200);
204 QImage image =
mApi->grabLayout();
205 QByteArray ba = this->generatePNGEncoding(image);
207 resp->setHeader(
"Content-Type",
"image/png");
208 resp->setHeader(
"Content-Length", QString::number(ba.size()));
209 resp->writeHead(200);
219 QJsonDocument doc = QJsonDocument::fromJson(req->body());
222 size.setWidth(doc.object()[
"width"].toInt());
223 size.setHeight(doc.object()[
"height"].toInt());
224 QString layout = doc.object()[
"layout"].toString();
226 CX_LOG_CHANNEL_DEBUG(
"CA") <<
"size " << size.width() <<
"," << size.height() <<
", layout " << layout;
228 mApi->createLayoutWidget(size, layout);
230 resp->writeHead(200);
236 mApi->closeLayoutWidget();
239 QByteArray HttpRequestHandler::generatePNGEncoding(QImage image)
243 buffer.open(QIODevice::WriteOnly);
244 image.save(&buffer,
"PNG");
262 if (req->method()==QHttpRequest::HTTP_GET)
274 QString body(
"</body>"
277 "Arguments are passed using JSON format."
280 "<table border=\"1\">"
281 "<tr><th>method</th><th>resource</th><th>description</th><th>arguments</th></tr>"
283 "<tr><td>GET</td><td>/</td><td>main page: short server info</td><td>html page</td></tr>"
284 "<tr><td>GET</td><td>/layout</td><td>return list of all layouts</td><td>layouts</td></tr>"
287 "<td>PUT</td><td>/layout/display</td>"
288 "<td>create layout display of given size and layout.</td>"
289 "<td>width=(int),height=(int),layout=(uid)</td>"
291 "<tr><td>GET</td><td>/layout/display</td><td>get image of layout</td><td>png image</td></tr>"
292 "<tr><td>DELETE</td><td>/layout/display</td><td>delete display</td></tr>"
300 .arg(qApp->applicationDisplayName())
303 QByteArray ba = body.toUtf8();
305 resp->setHeader(
"Content-Type",
"text/html");
306 resp->setHeader(
"Content-Length", QString::number(ba.size()));
307 resp->writeHead(200);
314 CX_ASSERT(req->path().startsWith(
"/screen"));
316 if (req->path() ==
"/screen")
328 resp->writeHead(404);
329 resp->end(QByteArray(
"Not found"));
334 resp->writeHead(405);
335 resp->end(QByteArray(
"Method Not Allowed"));
356 QImage image =
mApi->grabScreen();
357 QByteArray ba = this->generatePNGEncoding(image);
359 resp->setHeader(
"Content-Type",
"image/png");
360 resp->setHeader(
"Content-Length", QString::number(ba.size()));
361 resp->writeHead(200);