root/src/server.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "server.h"
#include <iostream>
#include <QFile>
#include <string>
#include <qhttprequest.h>
#include "systemutils.h"
#include "nwutils.h"

Server::Server(QString publicDirectoryPath, MainWindow &window, Library &library, VideoPlayer* player) :
    QObject(NULL), publicDirectory(publicDirectoryPath),
    window(window),
    library(library),
    player(player),
    pushEvents(library, *player)
{

}

std::pair<bool, int> Server::tryToBindPort(QHttpServer* server, int originalPort) {
    const int maxTries = 20; // try the 20 following ports
    bool success = false;
    int tryCount = 0;
    for (; (!success  && tryCount < maxTries); ++tryCount) {
        success = server->listen(originalPort + tryCount);
    }
    return std::make_pair(success, (originalPort + tryCount-1));
}

int Server::start(int serverPort) {
    this->server = new QHttpServer();
    std::pair<bool, int> boundPort = tryToBindPort(server, serverPort);

    if (!boundPort.first) {
        //delete this->server;
        return -1;
    }

    connect(this->server, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)),
            this, SLOT(handleRequest(QHttpRequest*, QHttpResponse*)));

    return boundPort.second;
}

void Server::handleRequest(QHttpRequest* req, QHttpResponse* resp) {
    if (req->path().contains(QRegExp("^\\/api\\/"))) {
        if (!handleApiRequest(req, resp)) {
            simpleWrite(resp, 405, "Api request not supported. Maybe a typo");
        }
    } else if (req->path().startsWith("/video/")) {
        streamVideo(req, resp);
    } else {
        sendFile(req, resp);
    }
}

bool Server::handleApiRequest(QHttpRequest* req, QHttpResponse* resp) {
    QString path = req->path();
    if (path.startsWith(QString("/api/setPage/"))) {
        QString pageName = QDir(req->path()).dirName();
        window.setPage(pageName, req->url().query(QUrl::FullyDecoded));
        simpleWrite(resp, 200, QString("{\"status\":\"ok\",\"page\":\"%1\"}").arg(pageName), mime::json);
        return true;
    } else if (path.startsWith("/api/assurePage")) {
        QString pageName = QDir(req->path()).dirName();
        const QString query = req->url().query(QUrl::FullyDecoded);
        if (!window.getPage() || !window.getPage()->conformsTo(query)) {
            window.setPage(pageName, query);
            simpleWrite(resp, 200, QString("{\"status\":\"set\",\"page\":\"%1\"}").arg(pageName), mime::json);
        } else {
            simpleWrite(resp, 200, QString("{\"status\":\"ok\",\"page\":\"%1\"}").arg(pageName), mime::json);
        }
        return true;
    } else if (path.startsWith("/api/activePage")) {
        simpleWrite(resp, 200, QString("{\"page\":\"%1\"}").arg(window.activePageId()), mime::json);
        return true;
    } else if (path.startsWith("/api/player/")) {
        return player->handleApiRequest(req, resp);
    } else if (path.startsWith("/api/library/")) {
        return library.handleApiRequest(req, resp);
    } else if (path.startsWith("/api/events/")) {
        return pushEvents.handleApiRequest(req, resp);
    } else if (path.startsWith("/api/page/") && window.getPage()) {
        return window.getPage()->handleApiRequest(req, resp);
    } else if (path.startsWith("/api/online/")) {
        return library.onlineSync.handleApiRequest(req, resp);
    }
    return false;
}

void Server::streamVideo(QHttpRequest* req, QHttpResponse* resp) {
    QString filePath = QString(req->url().path(QUrl::FullyDecoded)).remove(0, sizeof("/video") -1);
    QFile file(filePath);
    if (file.exists() && file.open(QFile::ReadOnly)) {
       QByteArray data = file.readAll();
       simpleWrite(resp, 200, QString(data), SystemUtils::fileMime(filePath));
       file.close();
    } else {
       simpleWrite(resp, 404, "File not found (Error 404)");
    }
}

void Server::sendFile(QHttpRequest* req, QHttpResponse* resp) {
    QDir localDir = QDir(this->publicDirectory.path().append(req->path()));

    QString filePath;
    if (localDir.exists()) {
        filePath = localDir.absoluteFilePath("index.html");
    } else {
        QString path = req->path();
        if (path.at(0) == '/') {
            path = path.right(path.length()-1);
        }
        filePath = this->publicDirectory.absoluteFilePath(path);
    }


    if (!filePath.startsWith(this->publicDirectory.absolutePath())) {
        simpleWrite(resp, 403, QString("Access denied. Only files in the public directory will be sent."));
        return;
    }

    QFile file(filePath);
    if (file.exists() && file.open(QFile::ReadOnly)) {
        QByteArray data = file.readAll();
        simpleWrite(resp, 200, QString(data), SystemUtils::fileMime(filePath));
        file.close();
    } else {
        simpleWrite(resp, 404, "File not found (Error 404)");
    }
}

void Server::sendRedirect(QHttpResponse* resp, const QString& location) {
    resp->setHeader("Location", location);
    resp->writeHead(301);
    resp->end();
}

void Server::simpleWrite(QHttpResponse* resp, int statusCode, const QString& data, QString mime) {
    simpleWriteBytes(resp, statusCode, data.toUtf8(), mime);
}

void Server::simpleWriteBytes(QHttpResponse* resp, int statusCode, const QByteArray& data, QString mime) {
    resp->setHeader("Content-Length", QString::number(data.size()));
    resp->setHeader("Content-Type", mime);
    resp->writeHead(statusCode);
    resp->write(data);
    resp->end();
}


RequestBodyListener::RequestBodyListener(QHttpResponse *resp, QObject* parent) :
    QObject(parent),
    resp(resp)
{
}

void RequestBodyListener::onDataReceived(QByteArray bytes) {
    QHttpRequest* req = dynamic_cast<QHttpRequest*>(sender());
    if (req) {
        emit bodyReceived(resp, bytes);
    } else {
        Server::simpleWrite(resp, 500, "Internal-Server-Error: wrong callback connected to bodyEnd()");
    }
    deleteLater();
}

ServerDataReady::ServerDataReady(QHttpResponse *resp, QObject* parent) :
    QObject(parent),
    resp(resp)
{
}