Пример #1
0
void QHtmlService::sendStaticResponse(QTcpSocket *socket, int statusCode, QString mimeType, QByteArray data) const
{
    QString responseHeader = QString::fromLatin1("HTTP/1.1 %1 %2\r\n").arg(statusCode).arg(translateHttpStatusCode(statusCode));
    if (data.isEmpty()) {
        mimeType = guessMimeType(QStringLiteral("html"));
        data = QString::fromLatin1("<html><head><title>%1 %2</title></header><body>%2</body></html>")
                                   .arg(statusCode).arg(translateHttpStatusCode(statusCode)).toLatin1();
    }
    responseHeader += QString::fromLatin1("Content-Type: %1\r\nContent-Length: %2\r\n").arg(mimeType).arg(data.size());
    responseHeader += QStringLiteral("\r\n");
    socket->write(responseHeader.toLatin1() + data);
    socket->close();
}
void FileServer::serve(const QString &filename, QHttpResponse *response)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly)) {
        response->writeHead(404);
        response->end("File not found");
        return;
    }

    response->setHeader("Content-Type", guessMimeType(filename));
    response->writeHead(200);
    response->write(file.readAll());
    response->end();
}
Пример #3
0
void QHtmlService::onHttpSocketReadyRead()
{
    QTcpSocket * const socket = qobject_cast<QTcpSocket*>(sender());
    Q_ASSERT(socket);

    // If we are waiting for full websocket handshake header
    QHash<QTcpSocket *, QStringList>::iterator header = mWebSocketHandshakeHeaders.find(socket);
    if (header != mWebSocketHandshakeHeaders.end()) {
        while (socket->canReadLine()) {
            const QString line = QString::fromLatin1(socket->readLine().constData());
            if (line == QStringLiteral("\r\n")) {
                disconnect(socket, SIGNAL(readyRead()), this, SLOT(onHttpSocketReadyRead()));
                sendWebSocketHandshake(socket);
                return;
            } else {
                header->append(line);
            }
        }
    }

    // handle http request
    if (socket->canReadLine()) {
        static const QRegExp requestRegExp(QStringLiteral("GET (.*) HTTP/1.1\r\n"));
        const QString request = QString::fromLatin1(socket->readLine().constData());

        if (requestRegExp.exactMatch(request)) {
            QString location = requestRegExp.cap(1);

            if (location == QStringLiteral("/socket")) {
                mWebSocketHandshakeHeaders.insert(socket, QStringList());
                onHttpSocketReadyRead();
            } else {
                if (location == QStringLiteral("/"))
                    location = QStringLiteral("/client.html");

                QFile file(QStringLiteral(":") + location);
                if (file.exists()) {
                    file.open(QFile::ReadOnly);
                    sendStaticResponse(socket, 200, guessMimeType(QFileInfo(file).suffix()), file.readAll());
                } else {
                    sendStaticResponse(socket, 404);
                }
            }
        } else {
            sendStaticResponse(socket, 501);
        }
    }
}