Skip to content

Commit ba6d143

Browse files
authored
Add bool support to get_query_value() (#6658)
1 parent 5258312 commit ba6d143

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111

1212
### Changed
1313

14+
- `ccf::http::get_query_value()` now supports bool types with `"true"` and `"false"` as values.
1415
- 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.
1516

1617
### Fixed

include/ccf/http_query.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ namespace ccf::http
5656
val = T(param_val);
5757
return true;
5858
}
59+
else if constexpr (std::is_same_v<T, bool>)
60+
{
61+
if (param_val == "true")
62+
{
63+
val = true;
64+
return true;
65+
}
66+
else if (param_val == "false")
67+
{
68+
val = false;
69+
return true;
70+
}
71+
else
72+
{
73+
error_reason = fmt::format(
74+
"Unable to parse value '{}' as bool in parameter '{}'",
75+
param_val,
76+
param_key);
77+
return false;
78+
}
79+
}
5980
else if constexpr (std::is_integral_v<T>)
6081
{
6182
const auto [p, ec] =

src/http/test/http_test.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,69 @@ DOCTEST_TEST_CASE("Accept header MIME matching")
631631
DOCTEST_REQUIRE(c.matches("foo/baz"));
632632
DOCTEST_REQUIRE(c.matches("fob/bar"));
633633
DOCTEST_REQUIRE(c.matches("fob/baz"));
634+
}
635+
636+
DOCTEST_TEST_CASE("Query parser getters")
637+
{
638+
{
639+
constexpr auto query = "foo=bar&baz=123";
640+
const auto parsed = ccf::http::parse_query(query);
641+
642+
std::string err = "";
643+
644+
{
645+
std::string val;
646+
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "foo", val, err));
647+
DOCTEST_REQUIRE(val == "bar");
648+
DOCTEST_REQUIRE(err.empty());
649+
}
650+
651+
{
652+
size_t val;
653+
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "baz", val, err));
654+
DOCTEST_REQUIRE(val == 123);
655+
DOCTEST_REQUIRE(err.empty());
656+
}
657+
658+
{
659+
std::string val;
660+
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "baz", val, err));
661+
DOCTEST_REQUIRE(val == "123");
662+
DOCTEST_REQUIRE(err.empty());
663+
}
664+
665+
{
666+
size_t val;
667+
DOCTEST_REQUIRE(!ccf::http::get_query_value(parsed, "foo", val, err));
668+
DOCTEST_REQUIRE(err == "Unable to parse value 'bar' in parameter 'foo'");
669+
}
670+
}
671+
672+
{
673+
constexpr auto query = "t=true&f=false&fnf=filenotfound";
674+
const auto parsed = ccf::http::parse_query(query);
675+
std::string err = "";
676+
677+
{
678+
bool val = false;
679+
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "t", val, err));
680+
DOCTEST_REQUIRE(val == true);
681+
DOCTEST_REQUIRE(err.empty());
682+
}
683+
684+
{
685+
bool val = true;
686+
DOCTEST_REQUIRE(ccf::http::get_query_value(parsed, "f", val, err));
687+
DOCTEST_REQUIRE(val == false);
688+
DOCTEST_REQUIRE(err.empty());
689+
}
690+
691+
{
692+
bool val;
693+
DOCTEST_REQUIRE(!ccf::http::get_query_value(parsed, "fnf", val, err));
694+
DOCTEST_REQUIRE(
695+
err ==
696+
"Unable to parse value 'filenotfound' as bool in parameter 'fnf'");
697+
}
698+
}
634699
}

0 commit comments

Comments
 (0)