38 #include <QtConcurrent>
39 #include <QNetworkAccessManager>
40 #include <QHostAddress>
41 #include <qhttpserver.h>
42 #include <qhttprequest.h>
43 #include <qhttpresponse.h>
51 #include <QJsonObject>
52 #include <QJsonDocument>
55 #include <QApplication>
66 mRequests.push_front(RequestType(req,resp));
68 connect(req, SIGNAL(
end()),
this, SLOT(onRequestSuccessful()));
71 void HttpRequestHandler::onRequestSuccessful()
73 for (QList<RequestType>::iterator iter = mRequests.begin(); iter!=mRequests.end(); )
75 if (iter->req->successful())
78 iter = mRequests.erase(iter);
102 CX_LOG_DEBUG() << QString(
"Web server request [%1] from [%2]")
104 .arg(req->remoteAddress());
106 if (req->path() ==
"/")
110 else if (req->path().startsWith(
"/screen"))
114 else if (req->path().startsWith(
"/layout"))
126 std::cout <<
"handle_layout" << std::endl;
127 CX_ASSERT(req->path().startsWith(
"/layout"));
129 if (req->path() ==
"/layout")
133 else if (req->path()==
"/layout/display/stream")
137 else if (req->path() ==
"/layout/display")
150 std::cout <<
"process_layout" << std::endl;
153 if (req->method()==QHttpRequest::HTTP_GET)
165 CX_ASSERT(req->path()==
"/layout/display/stream");
166 std::cout <<
"process_stream" << std::endl;
170 if (req->method()==QHttpRequest::HTTP_PUT)
174 else if (req->method()==QHttpRequest::HTTP_DELETE)
186 CX_ASSERT(req->path()==
"/layout/display");
187 std::cout <<
"process_display, success=" << req->successful() << std::endl;
193 if (req->method()==QHttpRequest::HTTP_GET)
197 else if (req->method()==QHttpRequest::HTTP_PUT)
201 else if (req->method()==QHttpRequest::HTTP_DELETE)
214 QStringList layouts =
mApi->getAvailableLayouts();
224 root.insert(
"Layouts", QJsonArray::fromStringList(layouts));
225 QByteArray ba = QJsonDocument(root).toJson();
227 resp->setHeader(
"Content-Type",
"application/json");
228 resp->setHeader(
"Content-Length", QString::number(ba.size()));
229 resp->writeHead(200);
236 QImage image =
mApi->grabLayout();
237 QByteArray ba = this->generatePNGEncoding(image);
239 resp->setHeader(
"Content-Type",
"image/png");
240 resp->setHeader(
"Content-Length", QString::number(ba.size()));
241 resp->writeHead(200);
251 QJsonDocument doc = QJsonDocument::fromJson(req->body());
254 size.setWidth(doc.object()[
"width"].toInt());
255 size.setHeight(doc.object()[
"height"].toInt());
256 QString layout = doc.object()[
"layout"].toString();
258 CX_LOG_CHANNEL_DEBUG(
"CA") <<
"size " << size.width() <<
"," << size.height() <<
", layout " << layout;
260 mApi->createLayoutWidget(size, layout);
262 resp->writeHead(200);
268 mApi->closeLayoutWidget();
271 QByteArray HttpRequestHandler::generatePNGEncoding(QImage image)
275 buffer.open(QIODevice::WriteOnly);
276 image.save(&buffer,
"PNG");
294 if (req->method()==QHttpRequest::HTTP_GET)
306 QString body(
"</body>"
309 "Arguments are passed using JSON format."
312 "<table border=\"1\">"
313 "<tr><th>method</th><th>resource</th><th>description</th><th>arguments</th></tr>"
315 "<tr><td>GET</td><td>/</td><td>main page: short server info</td><td>html page</td></tr>"
316 "<tr><td>GET</td><td>/layout</td><td>return list of all layouts</td><td>layouts</td></tr>"
319 "<td>PUT</td><td>/layout/display</td>"
320 "<td>create layout display of given size and layout.</td>"
321 "<td>width=(int),height=(int),layout=(uid)</td>"
323 "<tr><td>GET</td><td>/layout/display</td><td>get image of layout</td><td>png image</td></tr>"
324 "<tr><td>DELETE</td><td>/layout/display</td><td>delete display</td></tr>"
332 .arg(qApp->applicationDisplayName())
335 QByteArray ba = body.toUtf8();
337 resp->setHeader(
"Content-Type",
"text/html");
338 resp->setHeader(
"Content-Length", QString::number(ba.size()));
339 resp->writeHead(200);
346 std::cout <<
"handle_screen" << std::endl;
347 CX_ASSERT(req->path().startsWith(
"/screen"));
349 if (req->path() ==
"/screen")
361 resp->writeHead(404);
362 resp->end(QByteArray(
"Not found"));
367 resp->writeHead(405);
368 resp->end(QByteArray(
"Method Not Allowed"));
389 QImage image =
mApi->grabScreen();
390 QByteArray ba = this->generatePNGEncoding(image);
392 resp->setHeader(
"Content-Type",
"image/png");
393 resp->setHeader(
"Content-Length", QString::number(ba.size()));
394 resp->writeHead(200);
void process_mainpage(QHttpRequest *req, QHttpResponse *resp)
void handle_request(QHttpRequest *req, QHttpResponse *resp)
virtual QString getAdditionalMainPageDescription() const
void handle_layout(QHttpRequest *req, QHttpResponse *resp)
virtual void create_stream(QHttpRequest *req, QHttpResponse *resp)
#define CX_ASSERT(statement)
void handle_complete_request(QHttpRequest *req, QHttpResponse *resp)
void reply_method_not_allowed(QHttpResponse *resp)
void process_stream(QHttpRequest *req, QHttpResponse *resp)
void handle_screen(QHttpRequest *req, QHttpResponse *resp)
virtual void delete_stream(QHttpResponse *resp)
void reply_screenshot(QHttpResponse *resp)
boost::shared_ptr< class RemoteAPI > RemoteAPIPtr
#define CX_LOG_CHANNEL_DEBUG(channel)
void get_display_image(QHttpResponse *resp)
void reply_notfound(QHttpResponse *resp)
void process_display(QHttpRequest *req, QHttpResponse *resp)
void process_screen(QHttpRequest *req, QHttpResponse *resp)
void process_layout(QHttpRequest *req, QHttpResponse *resp)
void create_display(QHttpRequest *req, QHttpResponse *resp)
HttpRequestHandler(RemoteAPIPtr api)
void reply_layout_list(QHttpResponse *resp)
void reply_mainpage(QHttpResponse *resp)
void delete_display(QHttpResponse *resp)