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

Expose ccf::http::parse_accept_header() #6706

Merged
merged 2 commits into from
Dec 13, 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [6.0.0-dev10]

[6.0.0-dev10]: https://github.com/microsoft/CCF/releases/tag/6.0.0-dev10

### Added

- Expose `ccf:http::parse_accept_header()` and `ccf::http::AcceptHeaderField` (#6706).

## [6.0.0-dev9]

[6.0.0-dev9]: https://github.com/microsoft/CCF/releases/tag/6.0.0-dev9
Expand Down
10 changes: 10 additions & 0 deletions doc/build_apps/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ HTTP Entity Tags Matching
:project: CCF
:members:

HTTP Accept Header Matching
---------------------------

.. doxygenstruct:: ccf::http::AcceptHeaderField
:project: CCF
:members:

.. doxygenfunction:: ccf::http::parse_accept_header
:project: CCF

COSE
----

Expand Down
4 changes: 2 additions & 2 deletions src/http/http_accept.h → include/ccf/http_accept.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include "ccf/ds/nonstd.h"
#include "ccf/http_status.h"
#include "ccf/odata_error.h"
#include "node/rpc/rpc_exception.h"
#include "ccf/rpc_exception.h"

#include <string_view>

namespace http
namespace ccf::http
{
struct AcceptHeaderField
{
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "ccf"
version = "6.0.0-dev9"
version = "6.0.0-dev10"
authors = [
{ name="CCF Team", email="CCF-Sec@microsoft.com" },
]
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include "ccf/json_handler.h"

#include "ccf/ds/logger.h"
#include "ccf/http_accept.h"
#include "ccf/http_consts.h"
#include "ccf/odata_error.h"
#include "ccf/redirect.h"
#include "ccf/rpc_context.h"
#include "http/http_accept.h"
#include "node/rpc/rpc_exception.h"
#include "ccf/rpc_exception.h"

#include <llhttp/llhttp.h>

Expand Down Expand Up @@ -64,7 +64,7 @@ namespace ccf
if (accept_it.has_value())
{
const auto accept_options =
::http::parse_accept_header(accept_it.value());
ccf::http::parse_accept_header(accept_it.value());
bool matched = false;
for (const auto& option : accept_options)
{
Expand Down
38 changes: 21 additions & 17 deletions src/http/test/http_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the Apache 2.0 License.

#include "ccf/crypto/key_pair.h"
#include "ccf/http_accept.h"
#include "ccf/http_query.h"
#include "http/http_accept.h"
#include "http/http_builder.h"
#include "http/http_parser.h"

Expand Down Expand Up @@ -565,12 +565,12 @@ DOCTEST_TEST_CASE("Query parser")
DOCTEST_TEST_CASE("Parse Accept header")
{
{
const auto fields = http::parse_accept_header("");
const auto fields = ccf::http::parse_accept_header("");
DOCTEST_REQUIRE(fields.empty());
}

{
const auto fields = http::parse_accept_header("foo/bar;q=0.25");
const auto fields = ccf::http::parse_accept_header("foo/bar;q=0.25");
DOCTEST_REQUIRE(fields.size() == 1);
const auto& field = fields[0];
DOCTEST_REQUIRE(field.mime_type == "foo");
Expand All @@ -581,7 +581,7 @@ DOCTEST_TEST_CASE("Parse Accept header")
{
// Shuffled and modified version of Firefox 91 default value, to test
// sorting
const auto fields = http::parse_accept_header(
const auto fields = ccf::http::parse_accept_header(
"image/webp;q=0.8, "
"image/*;q=0.8, "
"text/html, "
Expand All @@ -591,31 +591,35 @@ DOCTEST_TEST_CASE("Parse Accept header")
"*/*;q=0.8");
DOCTEST_REQUIRE(fields.size() == 7);

DOCTEST_REQUIRE(fields[0] == http::AcceptHeaderField{"text", "html", 1.0f});
DOCTEST_REQUIRE(
fields[1] == http::AcceptHeaderField{"image", "avif", 1.0f});
fields[0] == ccf::http::AcceptHeaderField{"text", "html", 1.0f});
DOCTEST_REQUIRE(
fields[2] == http::AcceptHeaderField{"application", "xhtml+xml", 1.0f});
fields[1] == ccf::http::AcceptHeaderField{"image", "avif", 1.0f});
DOCTEST_REQUIRE(
fields[3] == http::AcceptHeaderField{"application", "xml", 0.9f});
fields[2] ==
ccf::http::AcceptHeaderField{"application", "xhtml+xml", 1.0f});
DOCTEST_REQUIRE(
fields[4] == http::AcceptHeaderField{"image", "webp", 0.8f});
DOCTEST_REQUIRE(fields[5] == http::AcceptHeaderField{"image", "*", 0.8f});
DOCTEST_REQUIRE(fields[6] == http::AcceptHeaderField{"*", "*", 0.8f});
fields[3] == ccf::http::AcceptHeaderField{"application", "xml", 0.9f});
DOCTEST_REQUIRE(
fields[4] == ccf::http::AcceptHeaderField{"image", "webp", 0.8f});
DOCTEST_REQUIRE(
fields[5] == ccf::http::AcceptHeaderField{"image", "*", 0.8f});
DOCTEST_REQUIRE(fields[6] == ccf::http::AcceptHeaderField{"*", "*", 0.8f});
}

{
DOCTEST_REQUIRE_THROWS(http::parse_accept_header("not_a_mime_type"));
DOCTEST_REQUIRE_THROWS(http::parse_accept_header("valid/mime;q=notnum"));
DOCTEST_REQUIRE_THROWS(http::parse_accept_header(","));
DOCTEST_REQUIRE_THROWS(ccf::http::parse_accept_header("not_a_mime_type"));
DOCTEST_REQUIRE_THROWS(
ccf::http::parse_accept_header("valid/mime;q=notnum"));
DOCTEST_REQUIRE_THROWS(ccf::http::parse_accept_header(","));
}
}

DOCTEST_TEST_CASE("Accept header MIME matching")
{
const auto a = http::AcceptHeaderField{"foo", "bar", 1.0f};
const auto b = http::AcceptHeaderField{"foo", "*", 1.0f};
const auto c = http::AcceptHeaderField{"*", "*", 1.0f};
const auto a = ccf::http::AcceptHeaderField{"foo", "bar", 1.0f};
const auto b = ccf::http::AcceptHeaderField{"foo", "*", 1.0f};
const auto c = ccf::http::AcceptHeaderField{"*", "*", 1.0f};

DOCTEST_REQUIRE(a.matches("foo/bar"));
DOCTEST_REQUIRE_FALSE(a.matches("foo/baz"));
Expand Down
2 changes: 1 addition & 1 deletion src/node/rpc/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ccf/http_status.h"
#include "ccf/node_context.h"
#include "ccf/pal/locking.h"
#include "ccf/rpc_exception.h"
#include "ccf/service/node_info_network.h"
#include "ccf/service/signed_req.h"
#include "ccf/service/tables/jwt.h"
Expand All @@ -20,7 +21,6 @@
#include "kv/store.h"
#include "node/endpoint_context_impl.h"
#include "node/node_configuration_subsystem.h"
#include "rpc_exception.h"
#include "service/internal_tables_access.h"

#define FMT_HEADER_ONLY
Expand Down
Loading