Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CORS header #519

Merged
merged 3 commits into from
Dec 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/engine/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ boost::asio::awaitable<void> Server::process(
auto filenameAndParams = ad_utility::UrlParser::parseTarget(request.target());
const auto& params = filenameAndParams._parameters;

auto sendWithCors = [&send](auto response) -> boost::asio::awaitable<void> {
response.set(http::field::access_control_allow_origin, "*");
co_return co_await send(std::move(response));
};

// If there is a command like "clear_cache" which might be combined with a
// query, track if there is such a command but no query and still send
// a useful response.
Expand All @@ -84,12 +89,11 @@ boost::asio::awaitable<void> Server::process(
if (cmd == "stats") {
LOG(INFO) << "Supplying index stats..." << std::endl;
auto response = createJsonResponse(composeStatsJson(), request);
co_return co_await send(std::move(response));
co_return co_await sendWithCors(std::move(response));
} else if (cmd == "cachestats") {
LOG(INFO) << "Supplying cache stats..." << std::endl;
auto response =
createJsonResponse(composeCacheStatsJson().dump(), request);
co_return co_await send(std::move(response));
auto response = createJsonResponse(composeCacheStatsJson(), request);
co_return co_await sendWithCors(std::move(response));
} else if (cmd == "clearcache") {
LOG(INFO) << "Clearing the cache, unpinned elements only" << std::endl;
_cache.clearUnpinnedOnly();
Expand All @@ -104,7 +108,7 @@ boost::asio::awaitable<void> Server::process(
"Successfully cleared the cache (including the pinned elements)",
request, ad_utility::MediaType::textPlain);
} else {
co_await send(createBadRequestResponse(
co_await sendWithCors(createBadRequestResponse(
R"(unknown value for query paramter "cmd" : ")" + cmd + '\"',
request));
co_return;
Expand All @@ -113,14 +117,14 @@ boost::asio::awaitable<void> Server::process(

if (params.contains("query")) {
if (params.at("query").empty()) {
co_return co_await send(createBadRequestResponse(
co_return co_await sendWithCors(createBadRequestResponse(
"Parameter \"query\" must not have an empty value", request));
}

co_return co_await processQuery(params, requestTimer, std::move(request),
send);
sendWithCors);
} else if (responseFromCommand.has_value()) {
co_return co_await send(std::move(responseFromCommand.value()));
co_return co_await sendWithCors(std::move(responseFromCommand.value()));
}

// Neither a query nor a command were specified, simply serve a file.
Expand Down