Skip to content

Commit

Permalink
header: HeaderListView implementation (envoyproxy#10733)
Browse files Browse the repository at this point in the history
This is an abstraction of HeaderList. This is required for us to return all of headers from HeaderMap to implement List/Map expression processor for google/cel which is not implemented right now.

Signed-off-by: shikugawa <rei@tetrate.io>
  • Loading branch information
Shikugawa authored May 14, 2020
1 parent 510c10c commit e71d162
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "header_list_view_lib",
srcs = ["header_list_view.cc"],
hdrs = ["header_list_view.h"],
deps = [
"//include/envoy/http:header_map_interface",
],
)

envoy_cc_library(
name = "header_map_lib",
srcs = ["header_map_impl.cc"],
Expand Down
19 changes: 19 additions & 0 deletions source/common/http/header_list_view.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "common/http/header_list_view.h"

namespace Envoy {
namespace Http {

HeaderListView::HeaderListView(const HeaderMap& header_map) {
header_map.iterate(
[](const Http::HeaderEntry& header, void* context) -> HeaderMap::Iterate {
auto* context_ptr = static_cast<HeaderListView*>(context);
context_ptr->keys_.emplace_back(std::reference_wrapper<const HeaderString>(header.key()));
context_ptr->values_.emplace_back(
std::reference_wrapper<const HeaderString>(header.value()));
return HeaderMap::Iterate::Continue;
},
this);
}

} // namespace Http
} // namespace Envoy
24 changes: 24 additions & 0 deletions source/common/http/header_list_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <vector>

#include "envoy/http/header_map.h"

namespace Envoy {
namespace Http {

class HeaderListView {
public:
using HeaderStringRefs = std::vector<std::reference_wrapper<const HeaderString>>;

HeaderListView(const HeaderMap& header_map);
const HeaderStringRefs& keys() const { return keys_; }
const HeaderStringRefs& values() const { return values_; }

private:
HeaderStringRefs keys_;
HeaderStringRefs values_;
};

} // namespace Http
} // namespace Envoy
1 change: 1 addition & 0 deletions test/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ envoy_cc_test(
name = "header_map_impl_test",
srcs = ["header_map_impl_test.cc"],
deps = [
"//source/common/http:header_list_view_lib",
"//source/common/http:header_map_lib",
"//source/common/http:header_utility_lib",
"//test/test_common:utility_lib",
Expand Down
21 changes: 21 additions & 0 deletions test/common/http/header_map_impl_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <algorithm>
#include <memory>
#include <string>

#include "common/http/header_list_view.h"
#include "common/http/header_map_impl.h"
#include "common/http/header_utility.h"

Expand All @@ -9,6 +11,7 @@

#include "gtest/gtest.h"

using ::testing::ElementsAre;
using ::testing::InSequence;

namespace Envoy {
Expand Down Expand Up @@ -920,6 +923,24 @@ TEST(HeaderMapImplTest, Get) {
}
}

TEST(HeaderMapImplTest, TestHeaderList) {
std::array<Http::LowerCaseString, 2> keys{Headers::get().Path, LowerCaseString("hello")};
std::array<std::string, 2> values{"/", "world"};

auto headers = createHeaderMap<TestHeaderMapImpl>({{keys[0], values[0]}, {keys[1], values[1]}});
HeaderListView header_list(headers->header_map_);
auto to_string_views =
[](const HeaderListView::HeaderStringRefs& strs) -> std::vector<absl::string_view> {
std::vector<absl::string_view> str_views(strs.size());
std::transform(strs.begin(), strs.end(), str_views.begin(),
[](auto value) -> absl::string_view { return value.get().getStringView(); });
return str_views;
};

EXPECT_THAT(to_string_views(header_list.keys()), ElementsAre(":path", "hello"));
EXPECT_THAT(to_string_views(header_list.values()), ElementsAre("/", "world"));
}

TEST(HeaderMapImplTest, TestAppendHeader) {
// Test appending to a string with a value.
{
Expand Down

0 comments on commit e71d162

Please sign in to comment.