From 69112d325a28c4fcb6572c0fbd22926224fcacd8 Mon Sep 17 00:00:00 2001 From: overwriter <9856mmm@gmail.com> Date: Thu, 14 Nov 2024 22:55:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DCI-test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/windows.yml | 5 +++++ ApiServer/server.cpp | 29 +++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 36afaf4..dea6809 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -34,6 +34,11 @@ jobs: - qt_version: 6.2.4 qt_arch: win64_msvc2019_64 arch: x64 + # 6.7.3 + - qt_version: 6.7.3 + qt_arch: win64_msvc2022_64 + arch: x64 + modules: 'qthttpserver qtwebsockets' # 6.8.0 - qt_version: 6.8.0 qt_arch: win64_msvc2022_64 diff --git a/ApiServer/server.cpp b/ApiServer/server.cpp index 9b46380..0e703ed 100644 --- a/ApiServer/server.cpp +++ b/ApiServer/server.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "../QCloudMusicApi/apihelper.h" #include "../QCloudMusicApi/util/index.h" @@ -40,11 +41,14 @@ void Server::serveNcmApi(QVariantMap options) consturctServer({}); - if (port == server.listen(host, port)) { + auto tcpserver = new QTcpServer(); + if (tcpserver->listen(host, port) && server.bind(tcpserver)) { DEBUG << "server running @ http://" + (host != QHostAddress::Any ? host.toString() : "localhost") + ":" + QString::number(port); } else { DEBUG << "address already in use :::" + QString::number(port); + delete tcpserver; + return; } } @@ -60,7 +64,7 @@ void Server::consturctServer(QVariantMap options) const QString CORS_ALLOW_ORIGIN = parser.value(option); // 设置请求的路径和方法未知时的错误提示 - server.setMissingHandler([](const QHttpServerRequest& request, QHttpServerResponder&& responder) { + server.setMissingHandler(this, [](const QHttpServerRequest& request, QHttpServerResponder& responder) { QHttpServerResponse response(("Cannot GET " + request.url().path()).toUtf8() , QHttpServerResponse::StatusCode::NotFound); @@ -112,7 +116,7 @@ void Server::consturctServer(QVariantMap options) } QVariantMap headers; - for (auto& i : request.headers()) { + for (auto& i : request.headers().toListOfPairs()) { headers[QUrl::fromPercentEncoding(i.first)] = QUrl::fromPercentEncoding(i.second); } auto cookie = Index::cookieToJson(headers["Cookie"].toString()); @@ -127,18 +131,19 @@ void Server::consturctServer(QVariantMap options) auto response = QHttpServerResponse(result, (QHttpServerResponse::StatusCode)ret["status"].toInt()); + const auto cookies = ret["cookie"].toString(); /** * CORS & Preflight request */ - response.addHeaders({ - { "Access-Control-Allow-Credentials", "true" }, - { "Access-Control-Allow-Origin", (!CORS_ALLOW_ORIGIN.isEmpty() ? "*" : CORS_ALLOW_ORIGIN).toUtf8() }, - { "Access-Control-Allow-Headers", "X-Requested-With,Content-Type" }, - { "Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS" }, - { "Content-Type", "application/json; charset=utf-8" }, - }); - const auto cookies = ret["cookie"].toString(); - response.setHeader("Set-Cookie", cookies.toUtf8()); + response.setHeaders(QHttpHeaders::fromListOfPairs( + { + { "Access-Control-Allow-Credentials", "true" }, + { "Access-Control-Allow-Origin", (!CORS_ALLOW_ORIGIN.isEmpty() ? "*" : CORS_ALLOW_ORIGIN).toUtf8() }, + { "Access-Control-Allow-Headers", "X-Requested-With,Content-Type" }, + { "Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS" }, + { "Content-Type", "application/json; charset=utf-8" }, + { "Set-Cookie", cookies.toUtf8() }, + })); return response; });