Skip to content

Commit

Permalink
fix: 修复CI-test
Browse files Browse the repository at this point in the history
  • Loading branch information
s12mmm3 committed Nov 14, 2024
1 parent cc5f34f commit 69112d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 17 additions & 12 deletions ApiServer/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QMetaObject>
#include <QUrlQuery>
#include <QtConcurrent>
#include <QTcpServer>

#include "../QCloudMusicApi/apihelper.h"
#include "../QCloudMusicApi/util/index.h"
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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;
});

Expand Down

0 comments on commit 69112d3

Please sign in to comment.