diff --git a/docs/root/configuration/http/http_filters/lua_filter.rst b/docs/root/configuration/http/http_filters/lua_filter.rst index 8e6f8eeffef8..5d335e69591f 100644 --- a/docs/root/configuration/http/http_filters/lua_filter.rst +++ b/docs/root/configuration/http/http_filters/lua_filter.rst @@ -424,6 +424,16 @@ the length of the signature. *data* is the content which will be hashed. *dataLe The function returns a pair. If the first element is *true*, the second element will be empty which means signature is verified; otherwise, the second element will store the error message. +.. _config_http_filters_lua_stream_handle_api_base64_escape: + +base64Escape() +^^^^^^^^^^^^^^ +.. code-block:: lua + + local base64_encoded = handle:base64Escape("input string") + +Encodes the input string as base64. This can be useful for escaping binary data. + .. _config_http_filters_lua_header_wrapper: Header object API diff --git a/docs/root/version_history/current.rst b/docs/root/version_history/current.rst index f1d42573eabf..e6668a1b26c7 100644 --- a/docs/root/version_history/current.rst +++ b/docs/root/version_history/current.rst @@ -59,6 +59,7 @@ New Features * http: introduced new HTTP/1 and HTTP/2 codec implementations that will remove the use of exceptions for control flow due to high risk factors and instead use error statuses. The old behavior is used by default, but the new codecs can be enabled for testing by setting the runtime feature `envoy.reloadable_features.new_codec_behavior` to true. The new codecs will be in development for one month, and then enabled by default while the old codecs are deprecated. * load balancer: added a :ref:`configuration` option to specify the active request bias used by the least request load balancer. * lua: added Lua APIs to access :ref:`SSL connection info ` object. +* lua: added Lua API for :ref:`base64 escaping a string `. * overload management: add :ref:`scaling ` trigger for OverloadManager actions. * postgres network filter: :ref:`metadata ` is produced based on SQL query. * ratelimit: added :ref:`enable_x_ratelimit_headers ` option to enable `X-RateLimit-*` headers as defined in `draft RFC `_. diff --git a/source/extensions/filters/http/lua/lua_filter.cc b/source/extensions/filters/http/lua/lua_filter.cc index 0053443cd549..4a3431df1d7e 100644 --- a/source/extensions/filters/http/lua/lua_filter.cc +++ b/source/extensions/filters/http/lua/lua_filter.cc @@ -12,6 +12,8 @@ #include "common/crypto/utility.h" #include "common/http/message_impl.h" +#include "absl/strings/escaping.h" + namespace Envoy { namespace Extensions { namespace HttpFilters { @@ -599,6 +601,15 @@ int StreamHandleWrapper::luaImportPublicKey(lua_State* state) { return 1; } +int StreamHandleWrapper::luaBase64Escape(lua_State* state) { + // Get input string. + absl::string_view input = luaL_checkstring(state, 2); + auto output = absl::Base64Escape(input); + lua_pushlstring(state, output.data(), output.length()); + + return 1; +} + FilterConfig::FilterConfig(const envoy::extensions::filters::http::lua::v3::Lua& proto_config, ThreadLocal::SlotAllocator& tls, Upstream::ClusterManager& cluster_manager, Api::Api& api) diff --git a/source/extensions/filters/http/lua/lua_filter.h b/source/extensions/filters/http/lua/lua_filter.h index 24909a95d649..fe4afc50257a 100644 --- a/source/extensions/filters/http/lua/lua_filter.h +++ b/source/extensions/filters/http/lua/lua_filter.h @@ -165,7 +165,8 @@ class StreamHandleWrapper : public Filters::Common::Lua::BaseLuaObjectdecodeHeaders(request_headers, true)); + + Http::TestResponseHeaderMapImpl response_headers{{":status", "200"}}; + + EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("YmFyZm9v"))); + EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->encodeHeaders(response_headers, true)); +} + } // namespace } // namespace Lua } // namespace HttpFilters