-
Notifications
You must be signed in to change notification settings - Fork 5.5k
lua: Fix printing Lua string containing hex characters #17994
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
Changes from 10 commits
35c1bb7
ca56457
3eccad4
559ba90
468c0c5
50dcaca
141afac
7785a54
1362378
a492a31
eb34c1c
c6e70f6
657db60
90cdf26
badd509
571966f
924c805
aa6ff26
d33d446
3af7a01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,19 @@ namespace Lua { | |
| lua_pushnumber(state, val); \ | ||
| lua_settable(state, -3); | ||
|
|
||
| /** | ||
| * Get absl::string_view from Lua string. This checks if the argument at index is a string | ||
| * and build an absl::string_view from it. | ||
| * @param state the current Lua state. | ||
| * @param index the index of argument. | ||
| * @return absl::string_view of Lua string with proper string length. | ||
| **/ | ||
| inline absl::string_view getStringViewFromLuaString(lua_State* state, int arg) { | ||
| size_t input_size = 0; | ||
| const char* input = luaL_checklstring(state, arg, &input_size); | ||
|
Patrick0308 marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to @dio 's comments, we also need to add a comment here to explain the case of parameter errors (the case that |
||
| return absl::string_view(input, input_size); | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the maximum space needed to be aligned. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ class TestFilter : public Filter { | |
| public: | ||
| using Filter::Filter; | ||
|
|
||
| MOCK_METHOD(void, scriptLog, (spdlog::level::level_enum level, const char* message)); | ||
| MOCK_METHOD(void, scriptLog, (spdlog::level::level_enum level, absl::string_view message)); | ||
| }; | ||
|
|
||
| class LuaHttpFilterTest : public testing::Test { | ||
|
|
@@ -796,7 +796,7 @@ TEST_F(LuaHttpFilterTest, HttpCall) { | |
| request_handle:logTrace(key .. " " .. value) | ||
| end | ||
| request_handle:logTrace(string.len(body)) | ||
| request_handle:logTrace(body) | ||
| request_handle:logTrace(string.sub(body, 1, 4) .. string.byte(body, 5) .. string.sub(body, 6, 8)) | ||
| request_handle:logTrace(string.byte(body, 5)) | ||
| request_handle:logTrace(string.sub(body, 6, 8)) | ||
| end | ||
|
|
@@ -818,6 +818,7 @@ TEST_F(LuaHttpFilterTest, HttpCall) { | |
| {":method", "POST"}, | ||
| {":path", "/"}, | ||
| {":authority", "foo"}, | ||
|
|
||
| {"set-cookie", "flavor=chocolate; Path=/"}, | ||
| {"set-cookie", "variant=chewy; Path=/"}, | ||
| {"content-length", "11"}}; | ||
|
|
@@ -841,7 +842,7 @@ TEST_F(LuaHttpFilterTest, HttpCall) { | |
| response_message->body().add(response, 8); | ||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq(":status 200"))); | ||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("8"))); | ||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("resp"))); | ||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("resp0nse"))); | ||
|
Patrick0308 marked this conversation as resolved.
Outdated
|
||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("0"))); | ||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("nse"))); | ||
| EXPECT_CALL(decoder_callbacks_, continueDecoding()); | ||
|
|
@@ -2392,6 +2393,56 @@ TEST_F(LuaHttpFilterTest, LuaFilterSetResponseBufferChunked) { | |
| EXPECT_EQ(Http::FilterDataStatus::Continue, filter_->encodeData(response_body, true)); | ||
| } | ||
|
|
||
|
Patrick0308 marked this conversation as resolved.
|
||
| // bodyBuffer should not truncated when bodyBuffer set hex character | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be: |
||
| TEST_F(LuaHttpFilterTest, LuaBodyBufferSetBytesWithHex) { | ||
|
Patrick0308 marked this conversation as resolved.
|
||
| const std::string SCRIPT{R"EOF( | ||
| function envoy_on_response(response_handle) | ||
| local bodyBuffer = response_handle:body() | ||
| bodyBuffer:setBytes("\x471111") | ||
| local body_str = bodyBuffer:getBytes(0, bodyBuffer:length()) | ||
| response_handle:logTrace(body_str) | ||
| end | ||
| )EOF"}; | ||
|
|
||
| InSequence s; | ||
| setup(SCRIPT); | ||
|
|
||
| Http::TestRequestHeaderMapImpl request_headers{{":path", "/"}}; | ||
| EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true)); | ||
|
|
||
| Http::TestResponseHeaderMapImpl response_headers{{":status", "200"}}; | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->encodeHeaders(response_headers, false)); | ||
|
|
||
| Buffer::OwnedImpl response_body(""); | ||
| EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("G1111"))); | ||
| EXPECT_EQ(Http::FilterDataStatus::Continue, filter_->encodeData(response_body, true)); | ||
| EXPECT_EQ(5, encoder_callbacks_.buffer_->length()); | ||
| } | ||
|
|
||
|
Patrick0308 marked this conversation as resolved.
|
||
| // bodyBuffer should not truncated when bodyBuffer set zero | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be: |
||
| TEST_F(LuaHttpFilterTest, LuaBodyBufferSetBytesWithZero) { | ||
| const std::string SCRIPT{R"EOF( | ||
| function envoy_on_response(response_handle) | ||
| local bodyBuffer = response_handle:body() | ||
| bodyBuffer:setBytes("\0") | ||
| end | ||
| )EOF"}; | ||
|
|
||
| InSequence s; | ||
| setup(SCRIPT); | ||
|
|
||
| Http::TestRequestHeaderMapImpl request_headers{{":path", "/"}}; | ||
| EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true)); | ||
|
|
||
| Http::TestResponseHeaderMapImpl response_headers{{":status", "200"}}; | ||
| EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, | ||
| filter_->encodeHeaders(response_headers, false)); | ||
|
|
||
| Buffer::OwnedImpl response_body("1111"); | ||
| EXPECT_EQ(Http::FilterDataStatus::Continue, filter_->encodeData(response_body, true)); | ||
| EXPECT_EQ(1, encoder_callbacks_.buffer_->length()); | ||
| } | ||
| } // namespace | ||
| } // namespace Lua | ||
| } // namespace HttpFilters | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.