Skip to content

Commit

Permalink
Don't use a CORS proxy when not going cross origin
Browse files Browse the repository at this point in the history
  • Loading branch information
timangus committed Jul 18, 2024
1 parent c6dd9ee commit 2fb574f
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions source/shared/utils/downloadqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
#include <iostream>
#include <cstdio>

#ifdef Q_OS_WASM
#include <emscripten.h>
#include <emscripten/val.h>
#endif

using namespace Qt::Literals::StringLiterals;

DownloadQueue::DownloadQueue()
Expand All @@ -52,13 +57,22 @@ DownloadQueue::~DownloadQueue()

bool DownloadQueue::add(QUrl url)
{
// If the environment variable CORS_PROXY is present then prepend
// it to the resource being downloaded, so that the WebAssembly
// build doesn't run into CORS related denials when accessing
// resources outside of its own domain
const QUrl corsUrl(qEnvironmentVariable("CORS_PROXY"));
if(corsUrl.isValid())
url = QUrl(u"%1/%2"_s.arg(corsUrl.toString(), url.toString()));
#ifdef Q_OS_WASM
emscripten::val location = emscripten::val::global("location");
QUrl hostUrl(QString::fromStdString(location["href"].as<std::string>()));

// Cross origin measures are only necessary when actually going cross origin
if(hostUrl.host() != url.host())
{
// If the environment variable CORS_PROXY is present then prepend
// it to the resource being downloaded, so that the WebAssembly
// build doesn't run into CORS related denials when accessing
// resources outside of its own domain
const QUrl corsUrl(qEnvironmentVariable("CORS_PROXY"));
if(corsUrl.isValid())
url = QUrl(u"%1/%2"_s.arg(corsUrl.toString(), url.toString()));
}
#endif

// If idle, start downloading
if(idle())
Expand Down

0 comments on commit 2fb574f

Please sign in to comment.