The requested file was " + "not found.
"); +} + +void HttpServer::sendResponse(QTcpSocket *socket, int statusCode, + const QString &contentType, + const QByteArray &data) +{ + QString statusText; + switch (statusCode) { + case 200: + statusText = "OK"; + break; + case 404: + statusText = "Not Found"; + break; + case 405: + statusText = "Method Not Allowed"; + break; + case 500: + statusText = "Internal Server Error"; + break; + default: + statusText = "Unknown"; + break; + } + + QString response = + QString("HTTP/1.1 %1 %2\r\n").arg(statusCode).arg(statusText); + response += QString("Content-Type: %1\r\n").arg(contentType); + response += QString("Content-Length: %1\r\n").arg(data.size()); + response += "Access-Control-Allow-Origin: *\r\n"; + response += "Connection: close\r\n"; + response += "\r\n"; + + socket->write(response.toUtf8()); + socket->write(data); + socket->disconnectFromHost(); +} + +void HttpServer::sendFile(QTcpSocket *socket, const QString &filePath) +{ + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly)) { + sendResponse(socket, 404, "text/plain", "File not found"); + return; + } + + QByteArray data = file.readAll(); + QString mimeType = getMimeType(filePath); + + // Emit progress signal + QFileInfo info(filePath); + emit downloadProgress(info.fileName(), data.size(), data.size()); + + sendResponse(socket, 200, mimeType, data); +} + +void HttpServer::sendJsonManifest(QTcpSocket *socket) +{ + QString jsonContent = generateJsonManifest(); + sendResponse(socket, 200, "application/json", jsonContent.toUtf8()); +} + +QString HttpServer::generateJsonManifest() const +{ + QString serverIP = getLocalIP(); + + QJsonObject manifest; + QJsonArray items; + + for (const QString &file : fileList) { + QFileInfo info(file); + QJsonObject item; + item["path"] = QString("http://%1:%2/serve/%3") + .arg(serverIP) + .arg(port) + .arg(QString::fromUtf8( + QUrl::toPercentEncoding(info.fileName()))); + items.append(item); + } + + manifest["items"] = items; + + QJsonDocument doc(manifest); + return doc.toJson(); +} + +QString HttpServer::getLocalIP() const +{ + foreach (const QNetworkInterface &interface, + QNetworkInterface::allInterfaces()) { + if (interface.flags().testFlag(QNetworkInterface::IsUp) && + !interface.flags().testFlag(QNetworkInterface::IsLoopBack)) { + foreach (const QNetworkAddressEntry &entry, + interface.addressEntries()) { + if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { + return entry.ip().toString(); + } + } + } + } + return "127.0.0.1"; +} + +QString HttpServer::getMimeType(const QString &filePath) const +{ + QMimeDatabase db; + QMimeType type = db.mimeTypeForFile(filePath); + return type.name(); +} diff --git a/src/simplehttpserver.h b/src/httpserver.h similarity index 87% rename from src/simplehttpserver.h rename to src/httpserver.h index 96547d7..2a8780c 100644 --- a/src/simplehttpserver.h +++ b/src/httpserver.h @@ -17,8 +17,8 @@ * along with this program. If not, seeThe requested file was " - "not found.
"); -} - -void SimpleHttpServer::sendResponse(QTcpSocket *socket, int statusCode, - const QString &contentType, - const QByteArray &data) -{ - QString statusText; - switch (statusCode) { - case 200: - statusText = "OK"; - break; - case 404: - statusText = "Not Found"; - break; - case 405: - statusText = "Method Not Allowed"; - break; - case 500: - statusText = "Internal Server Error"; - break; - default: - statusText = "Unknown"; - break; - } - - QString response = - QString("HTTP/1.1 %1 %2\r\n").arg(statusCode).arg(statusText); - response += QString("Content-Type: %1\r\n").arg(contentType); - response += QString("Content-Length: %1\r\n").arg(data.size()); - response += "Access-Control-Allow-Origin: *\r\n"; - response += "Connection: close\r\n"; - response += "\r\n"; - - socket->write(response.toUtf8()); - socket->write(data); - socket->disconnectFromHost(); -} - -void SimpleHttpServer::sendFile(QTcpSocket *socket, const QString &filePath) -{ - QFile file(filePath); - if (!file.open(QIODevice::ReadOnly)) { - sendResponse(socket, 404, "text/plain", "File not found"); - return; - } - - QByteArray data = file.readAll(); - QString mimeType = getMimeType(filePath); - - // Emit progress signal - QFileInfo info(filePath); - emit downloadProgress(info.fileName(), data.size(), data.size()); - - sendResponse(socket, 200, mimeType, data); -} - -void SimpleHttpServer::sendJsonManifest(QTcpSocket *socket) -{ - QString jsonContent = generateJsonManifest(); - sendResponse(socket, 200, "application/json", jsonContent.toUtf8()); -} - -void SimpleHttpServer::sendHtmlPage(QTcpSocket *socket) -{ - QString htmlContent = generateHtmlPage(); - sendResponse(socket, 200, "text/html", htmlContent.toUtf8()); -} - -QString SimpleHttpServer::generateJsonManifest() const -{ - QString serverIP = getLocalIP(); - - QJsonObject manifest; - QJsonArray items; - - for (const QString &file : fileList) { - QFileInfo info(file); - QJsonObject item; - item["path"] = QString("http://%1:%2/serve/%3") - .arg(serverIP) - .arg(port) - .arg(info.fileName()); - items.append(item); - } - - manifest["items"] = items; - - QJsonDocument doc(manifest); - return doc.toJson(); -} - -QString SimpleHttpServer::generateHtmlPage() const -{ - QString serverIP = getLocalIP(); - QString shortcutPath = - QString("shortcuts://import-shortcut?url=http://%1:%2/import.shortcut") - .arg(serverIP) - .arg(port); - - QString html = QString(R"( - - - - - -Ready to serve %4 files
-