Skip to content

Commit

Permalink
Merge pull request envoyproxy#486 from kyessenov/fix_life_span
Browse files Browse the repository at this point in the history
fixes to filter state getters/setters
  • Loading branch information
kyessenov authored Apr 20, 2020
2 parents 53d86e6 + 2fa666d commit f80bba6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 13 additions & 7 deletions source/extensions/common/wasm/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ static absl::flat_hash_map<std::string, PropertyToken> property_tokens = {PROPER
#undef _PARI

absl::optional<google::api::expr::runtime::CelValue>
Context::FindValue(absl::string_view name, Protobuf::Arena* arena) const {
Context::findValue(absl::string_view name, Protobuf::Arena* arena, bool last) const {
using google::api::expr::runtime::CelValue;

const StreamInfo::StreamInfo* info = getConstRequestStreamInfo();
Expand All @@ -442,11 +442,15 @@ Context::FindValue(absl::string_view name, Protobuf::Arena* arena) const {
const WasmState* state;
if (info->filterState().hasData<WasmState>(key)) {
state = &info->filterState().getDataReadOnly<WasmState>(key);
} else if (info->upstreamFilterState()->hasData<WasmState>(key)) {
} else if (info->upstreamFilterState() &&
info->upstreamFilterState()->hasData<WasmState>(key)) {
state = &info->upstreamFilterState()->getDataReadOnly<WasmState>(key);
} else {
return {};
}
if (last) {
return CelValue::CreateBytes(&state->value());
}
return state->exprValue(arena);
}
return {};
Expand Down Expand Up @@ -573,7 +577,7 @@ WasmResult Context::getProperty(absl::string_view path, std::string* result) {
if (first) {
// top-level ident
first = false;
auto top_value = FindValue(part, &arena);
auto top_value = findValue(part, &arena, start >= path.size());
if (!top_value.has_value()) {
return WasmResult::NotFound;
}
Expand Down Expand Up @@ -1115,12 +1119,14 @@ WasmResult Context::setProperty(absl::string_view path, absl::string_view value)
state = &stream_info->filterState()->getDataMutable<WasmState>(key);
} else {
const auto& it = rootContext()->state_prototypes_.find(path);
auto state_ptr = std::make_unique<WasmState>(it == rootContext()->state_prototypes_.end()
? DefaultWasmStatePrototype::get()
: *it->second.get());
const WasmStatePrototype& prototype = it == rootContext()->state_prototypes_.end()
? DefaultWasmStatePrototype::get()
: *it->second.get();
auto state_ptr = std::make_unique<WasmState>(prototype);
state = state_ptr.get();
stream_info->filterState()->setData(key, std::move(state_ptr),
StreamInfo::FilterState::StateType::ReadOnly);
StreamInfo::FilterState::StateType::Mutable,
prototype.life_span_);
}
if (!state->setValue(value)) {
return WasmResult::BadArgument;
Expand Down
6 changes: 5 additions & 1 deletion source/extensions/common/wasm/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ class Context : public proxy_wasm::ContextBase,
return {};
}
absl::optional<google::api::expr::runtime::CelValue>
FindValue(absl::string_view name, Protobuf::Arena* arena) const override;
findValue(absl::string_view name, Protobuf::Arena* arena, bool last) const;
absl::optional<google::api::expr::runtime::CelValue>
FindValue(absl::string_view name, Protobuf::Arena* arena) const override {
return findValue(name, arena, false);
}
bool IsPathUnknown(absl::string_view) const override { return false; }
const std::vector<google::api::expr::runtime::CelAttributePattern>&
unknown_attribute_patterns() const override {
Expand Down

0 comments on commit f80bba6

Please sign in to comment.