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

Support HTTP resolution #16

Merged
merged 1 commit into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/utils.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <sourcemeta/hydra/httpclient.h>
#include <sourcemeta/jsontoolkit/uri.h>

#include "utils.h"

#include <cassert> // assert
Expand Down Expand Up @@ -130,8 +133,31 @@ static auto fallback_resolver(std::string_view identifier)
return promise.get_future();
}

// If the URI is not an HTTP URL, then abort
const sourcemeta::jsontoolkit::URI uri{std::string{identifier}};
const auto maybe_scheme{uri.scheme()};
if (uri.is_urn() || !maybe_scheme.has_value() ||
(maybe_scheme.value() != "https" && maybe_scheme.value() != "http")) {
std::promise<std::optional<sourcemeta::jsontoolkit::JSON>> promise;
promise.set_value(std::nullopt);
return promise.get_future();
}

// TODO: Only print in verbose mode
std::cerr << "-- Attempting to fetch over HTTP: " << identifier << "\n";

sourcemeta::hydra::http::ClientRequest request{std::string{identifier}};
request.method(sourcemeta::hydra::http::Method::GET);
sourcemeta::hydra::http::ClientResponse response{request.send().get()};
if (response.status() != sourcemeta::hydra::http::Status::OK) {
std::ostringstream error;
error << "Failed to fetch " << identifier
<< " over HTTP. Got status code: " << response.status();
throw std::runtime_error(error.str());
}

std::promise<std::optional<sourcemeta::jsontoolkit::JSON>> promise;
promise.set_value(std::nullopt);
promise.set_value(sourcemeta::jsontoolkit::parse(response.body()));
return promise.get_future();
}

Expand Down
Loading