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 bool support to get_query_value() #6658

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- `ccf::http::get_query_value()` now supports bool types with `"true"` and `"false"` as values.
- Service certificates and endorsements used for historical receipts now have a pathlen constraint of 1 instead of 0, reflecting the fact that there can be a single intermediate in endorsement chains. Historically the value had been 0, which happened to work because of a quirk in OpenSSL when Issuer and Subject match on an element in the chain.

### Fixed
Expand Down
21 changes: 21 additions & 0 deletions include/ccf/http_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ namespace ccf::http
val = T(param_val);
return true;
}
else if constexpr (std::is_same_v<T, bool>)
{
if (param_val == "true")
{
val = true;
return true;
}
else if (param_val == "false")
{
val = false;
return true;
}
else
{
error_reason = fmt::format(
"Unable to parse value '{}' as bool in parameter '{}'",
param_val,
param_key);
return false;
}
}
else if constexpr (std::is_integral_v<T>)
{
const auto [p, ec] =
Expand Down
65 changes: 65 additions & 0 deletions src/http/test/http_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,69 @@ DOCTEST_TEST_CASE("Accept header MIME matching")
DOCTEST_REQUIRE(c.matches("foo/baz"));
DOCTEST_REQUIRE(c.matches("fob/bar"));
DOCTEST_REQUIRE(c.matches("fob/baz"));
}

DOCTEST_TEST_CASE("Query parser getters")
{
{
constexpr auto query = "foo=bar&baz=123";
const auto parsed = ccf::http::parse_query(query);

std::string err = "";

{
std::string val;
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "foo", val, err));
DOCTEST_REQUIRE(val == "bar");
DOCTEST_REQUIRE(err.empty());
}

{
size_t val;
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "baz", val, err));
DOCTEST_REQUIRE(val == 123);
DOCTEST_REQUIRE(err.empty());
}

{
std::string val;
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "baz", val, err));
DOCTEST_REQUIRE(val == "123");
DOCTEST_REQUIRE(err.empty());
}

{
size_t val;
DOCTEST_REQUIRE(!ccf::http::get_query_value(parsed, "foo", val, err));
DOCTEST_REQUIRE(err == "Unable to parse value 'bar' in parameter 'foo'");
}
}

{
constexpr auto query = "t=true&f=false&fnf=filenotfound";
const auto parsed = ccf::http::parse_query(query);
std::string err = "";

{
bool val = false;
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "t", val, err));
DOCTEST_REQUIRE(val == true);
DOCTEST_REQUIRE(err.empty());
}

{
bool val = true;
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "f", val, err));
DOCTEST_REQUIRE(val == false);
DOCTEST_REQUIRE(err.empty());
}

{
bool val;
DOCTEST_REQUIRE(!ccf::http::get_query_value(parsed, "fnf", val, err));
DOCTEST_REQUIRE(
err ==
"Unable to parse value 'filenotfound' as bool in parameter 'fnf'");
}
}
}