Skip to content

Commit

Permalink
Add bool support to get_query_value() (#6658)
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou authored Nov 19, 2024
1 parent 5258312 commit ba6d143
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
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'");
}
}
}

0 comments on commit ba6d143

Please sign in to comment.