From 0ffcb47f0ece2bf5355dd76c0362447eb528d32f Mon Sep 17 00:00:00 2001 From: Rohit Agrawal Date: Sun, 5 Jan 2025 19:42:51 +0100 Subject: [PATCH] lua: add new function to get the name of the matched route Signed-off-by: Rohit Agrawal --- changelogs/current.yaml | 4 ++ .../http/http_filters/lua_filter.rst | 20 +++++++++ .../extensions/filters/http/lua/wrappers.cc | 6 +++ source/extensions/filters/http/lua/wrappers.h | 9 +++- .../filters/http/lua/wrappers_test.cc | 44 +++++++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) diff --git a/changelogs/current.yaml b/changelogs/current.yaml index 909c0053f23d..cd14d76862cd 100644 --- a/changelogs/current.yaml +++ b/changelogs/current.yaml @@ -407,6 +407,10 @@ new_features: change: | Add the option to reduce the rate limit budget based on request/response contexts on stream done. See :ref:`apply_on_stream_done ` for more details. +- area: lua + change: | + Added :ref:`routeName() ` API to the Stream Info Object to get the + name of the route matched by the filter chain. deprecated: - area: rbac diff --git a/docs/root/configuration/http/http_filters/lua_filter.rst b/docs/root/configuration/http/http_filters/lua_filter.rst index 02483d8dae9f..5eee43fb9d22 100644 --- a/docs/root/configuration/http/http_filters/lua_filter.rst +++ b/docs/root/configuration/http/http_filters/lua_filter.rst @@ -823,6 +823,26 @@ Stream info object API Returns the string representation of :repo:`HTTP protocol ` used by the current request. The possible values are: ``HTTP/1.0``, ``HTTP/1.1``, ``HTTP/2`` and ``HTTP/3*``. +.. _config_http_filters_lua_stream_info_route_name: + +``routeName()`` +^^^^^^^^^^^^^^^ + +.. code-block:: lua + + streamInfo:routeName() + +Returns the name of the route matched by the filter chain. Returns an empty string if no route was matched. + +Example usage: + +.. code-block:: lua + + function envoy_on_request(request_handle) + local route_name = request_handle:streamInfo():routeName() + request_handle:logInfo("Matched route: " .. route_name) + end + .. _config_http_filters_lua_stream_info_downstream_direct_local_address: ``downstreamDirectLocalAddress()`` diff --git a/source/extensions/filters/http/lua/wrappers.cc b/source/extensions/filters/http/lua/wrappers.cc index dc3cd331ec1d..1463636ac609 100644 --- a/source/extensions/filters/http/lua/wrappers.cc +++ b/source/extensions/filters/http/lua/wrappers.cc @@ -230,6 +230,12 @@ int StreamInfoWrapper::luaRequestedServerName(lua_State* state) { return 1; } +int StreamInfoWrapper::luaRouteName(lua_State* state) { + const std::string& route_name = stream_info_.getRouteName(); + lua_pushlstring(state, route_name.data(), route_name.length()); + return 1; +} + DynamicMetadataMapIterator::DynamicMetadataMapIterator(DynamicMetadataMapWrapper& parent) : parent_{parent}, current_{parent_.streamInfo().dynamicMetadata().filter_metadata().begin()} {} diff --git a/source/extensions/filters/http/lua/wrappers.h b/source/extensions/filters/http/lua/wrappers.h index 0e2ca9abbadb..868230c9eeab 100644 --- a/source/extensions/filters/http/lua/wrappers.h +++ b/source/extensions/filters/http/lua/wrappers.h @@ -274,7 +274,8 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject stream_info; + std::string route_name = "test_route"; + ON_CALL(stream_info, getRouteName()).WillByDefault(testing::ReturnRef(route_name)); + + Filters::Common::Lua::LuaDeathRef wrapper( + StreamInfoWrapper::create(coroutine_->luaState(), stream_info), true); + EXPECT_CALL(printer_, testPrint("test_route")); + start("callMe"); + wrapper.reset(); +} + +// Test for empty route name +TEST_F(LuaStreamInfoWrapperTest, GetEmptyRouteName) { + const std::string SCRIPT{R"EOF( + function callMe(object) + testPrint(object:routeName()) + end + )EOF"}; + + InSequence s; + setup(SCRIPT); + + NiceMock stream_info; + std::string empty_route; + ON_CALL(stream_info, getRouteName()).WillByDefault(testing::ReturnRef(empty_route)); + + Filters::Common::Lua::LuaDeathRef wrapper( + StreamInfoWrapper::create(coroutine_->luaState(), stream_info), true); + EXPECT_CALL(printer_, testPrint("")); + start("callMe"); + wrapper.reset(); +} + } // namespace } // namespace Lua } // namespace HttpFilters