Skip to content

Commit

Permalink
replace missed concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
Qup42 committed Feb 18, 2025
1 parent 79c4e02 commit a940130
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
49 changes: 49 additions & 0 deletions src/engine/ParsedRequestBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
using namespace ad_utility::use_type_identity;
using namespace ad_utility::url_parser::sparqlOperation;

// ____________________________________________________________________________
ParsedRequestBuilder::ParsedRequestBuilder(const RequestType& request) {
using namespace ad_utility::url_parser::sparqlOperation;
// For an HTTP request, `request.target()` yields the HTTP Request-URI.
// This is a concatenation of the URL path and the query strings.
auto parsedUrl = ad_utility::url_parser::parseRequestTarget(request.target());
parsedRequest_ = {std::move(parsedUrl.path_), std::nullopt,
std::move(parsedUrl.parameters_), None{}};
}

// ____________________________________________________________________________
void ParsedRequestBuilder::extractAccessToken(const RequestType& request) {
parsedRequest_.accessToken_ =
determineAccessToken(request, parsedRequest_.parameters_);
}

// ____________________________________________________________________________
void ParsedRequestBuilder::extractDatasetClauses() {
extractDatasetClauseIfOperationIs(ti<Query>, "default-graph-uri", false);
Expand Down Expand Up @@ -114,3 +130,36 @@ GraphOrDefault ParsedRequestBuilder::extractTargetGraph(
return DEFAULT{};
}
}

// ____________________________________________________________________________
std::optional<std::string> ParsedRequestBuilder::determineAccessToken(
const RequestType& request,
const ad_utility::url_parser::ParamValueMap& params) {
namespace http = boost::beast::http;
std::optional<std::string> tokenFromAuthorizationHeader;
std::optional<std::string> tokenFromParameter;
if (request.find(http::field::authorization) != request.end()) {
string_view authorization = request[http::field::authorization];
const std::string prefix = "Bearer ";
if (!authorization.starts_with(prefix)) {
throw std::runtime_error(absl::StrCat(
"Authorization header doesn't start with \"", prefix, "\"."));
}
authorization.remove_prefix(prefix.length());
tokenFromAuthorizationHeader = std::string(authorization);
}
if (params.contains("access-token")) {
tokenFromParameter = ad_utility::url_parser::getParameterCheckAtMostOnce(
params, "access-token");
}
// If both are specified, they must be equal. This way there is no hidden
// precedence.
if (tokenFromAuthorizationHeader && tokenFromParameter &&
tokenFromAuthorizationHeader != tokenFromParameter) {
throw std::runtime_error(
"Access token is specified both in the `Authorization` header and by "
"the `access-token` parameter, but they are not the same");
}
return tokenFromAuthorizationHeader ? std::move(tokenFromAuthorizationHeader)
: std::move(tokenFromParameter);
}
53 changes: 7 additions & 46 deletions src/engine/ParsedRequestBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,19 @@ struct ParsedRequestBuilder {
FRIEND_TEST(ParsedRequestBuilderTest, determineAccessToken);
FRIEND_TEST(ParsedRequestBuilderTest, parameterIsContainedExactlyOnce);

using RequestType =
boost::beast::http::request<boost::beast::http::string_body>;

ad_utility::url_parser::ParsedRequest parsedRequest_;

// Initialize a `ParsedRequestBuilder`, parsing the request target into the
// `ParsedRequest`.
explicit ParsedRequestBuilder(
const ad_utility::httpUtils::HttpRequest auto& request) {
using namespace ad_utility::url_parser::sparqlOperation;
// For an HTTP request, `request.target()` yields the HTTP Request-URI.
// This is a concatenation of the URL path and the query strings.
auto parsedUrl =
ad_utility::url_parser::parseRequestTarget(request.target());
parsedRequest_ = {std::move(parsedUrl.path_), std::nullopt,
std::move(parsedUrl.parameters_), None{}};
}
explicit ParsedRequestBuilder(const RequestType& request);

// Extract the access token from the access-token parameter or the
// Authorization header and set it for `ParsedRequest`. If both are given,
// then they must be the same.
void extractAccessToken(
const ad_utility::httpUtils::HttpRequest auto& request) {
parsedRequest_.accessToken_ =
determineAccessToken(request, parsedRequest_.parameters_);
}
void extractAccessToken(const RequestType& request);

// If applicable extract the dataset clauses from the parameters and set them
// on the Query or Update.
Expand Down Expand Up @@ -92,35 +82,6 @@ struct ParsedRequestBuilder {
// Determine the access token from the parameters and the requests
// Authorization header.
static std::optional<std::string> determineAccessToken(
const ad_utility::httpUtils::HttpRequest auto& request,
const ad_utility::url_parser::ParamValueMap& params) {
namespace http = boost::beast::http;
std::optional<std::string> tokenFromAuthorizationHeader;
std::optional<std::string> tokenFromParameter;
if (request.find(http::field::authorization) != request.end()) {
string_view authorization = request[http::field::authorization];
const std::string prefix = "Bearer ";
if (!authorization.starts_with(prefix)) {
throw std::runtime_error(absl::StrCat(
"Authorization header doesn't start with \"", prefix, "\"."));
}
authorization.remove_prefix(prefix.length());
tokenFromAuthorizationHeader = std::string(authorization);
}
if (params.contains("access-token")) {
tokenFromParameter = ad_utility::url_parser::getParameterCheckAtMostOnce(
params, "access-token");
}
// If both are specified, they must be equal. This way there is no hidden
// precedence.
if (tokenFromAuthorizationHeader && tokenFromParameter &&
tokenFromAuthorizationHeader != tokenFromParameter) {
throw std::runtime_error(
"Access token is specified both in the `Authorization` header and by "
"the `access-token` parameter, but they are not the same");
}
return tokenFromAuthorizationHeader
? std::move(tokenFromAuthorizationHeader)
: std::move(tokenFromParameter);
}
const RequestType& request,
const ad_utility::url_parser::ParamValueMap& params);
};
3 changes: 1 addition & 2 deletions src/engine/SPARQLProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class SPARQLProtocol {
static constexpr std::string_view contentTypeSparqlUpdate =
"application/sparql-update";

using RequestType =
boost::beast::http::request<boost::beast::http::string_body>;
using RequestType = ParsedRequestBuilder::RequestType;

// Parse an HTTP GET request into a `ParsedRequest`. The
// `ParsedRequestBuilder` must have already extracted the access token.
Expand Down

0 comments on commit a940130

Please sign in to comment.