diff --git a/CHANGELOG.md b/CHANGELOG.md index 3300696851b7..e1883c99b119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/include/ccf/http_query.h b/include/ccf/http_query.h index 42e731d9bdcb..88157af8d530 100644 --- a/include/ccf/http_query.h +++ b/include/ccf/http_query.h @@ -56,6 +56,27 @@ namespace ccf::http val = T(param_val); return true; } + else if constexpr (std::is_same_v) + { + 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) { const auto [p, ec] = diff --git a/src/http/test/http_test.cpp b/src/http/test/http_test.cpp index 202111459e38..1c573984cbe4 100644 --- a/src/http/test/http_test.cpp +++ b/src/http/test/http_test.cpp @@ -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'"); + } + } } \ No newline at end of file