Skip to content

Commit 5e163c9

Browse files
committed
feature: ngx.encode_base64() now supports the (optional) no_padding argument. thanks Shuxin Yang for the patch in #4.
1 parent 5496e3b commit 5e163c9

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

lib/resty/core/base.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ local FREE_LIST_REF = 0
1717

1818
if not ngx.config
1919
or not ngx.config.ngx_lua_version
20-
or ngx.config.ngx_lua_version < 9011
20+
or ngx.config.ngx_lua_version < 9016
2121
then
22-
error("ngx_lua 0.9.11+ required")
22+
error("ngx_lua 0.9.16+ required")
2323
end
2424

2525

lib/resty/core/base64.lua

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,49 @@ local tonumber = tonumber
2121

2222
ffi.cdef[[
2323
size_t ngx_http_lua_ffi_encode_base64(const unsigned char *src,
24-
size_t len, unsigned char *dst);
24+
size_t len, unsigned char *dst,
25+
int no_padding);
2526

2627
int ngx_http_lua_ffi_decode_base64(const unsigned char *src,
2728
size_t len, unsigned char *dst,
2829
size_t *dlen);
2930
]]
3031

3132

32-
local function base64_encoded_length(len)
33-
return floor((len + 2) / 3) * 4
33+
local function base64_encoded_length(len, no_padding)
34+
return no_padding and floor((len * 8 + 5) / 6) or
35+
floor((len + 2) / 3) * 4
3436
end
3537

3638

37-
ngx.encode_base64 = function (s)
39+
ngx.encode_base64 = function (s, no_padding)
3840
if type(s) ~= 'string' then
3941
if not s then
4042
s = ''
4143
else
4244
s = tostring(s)
4345
end
4446
end
47+
4548
local slen = #s
46-
local dlen = base64_encoded_length(slen)
47-
-- print("dlen: ", tonumber(dlen))
49+
local no_padding_bool = false;
50+
local no_padding_int = 0;
51+
52+
if no_padding then
53+
if no_padding ~= true then
54+
return error("boolean argument only")
55+
end
56+
57+
no_padding_bool = true
58+
no_padding_int = 1;
59+
end
60+
61+
local dlen = base64_encoded_length(slen, no_padding_bool)
4862
local dst = get_string_buf(dlen)
49-
dlen = C.ngx_http_lua_ffi_encode_base64(s, slen, dst)
50-
return ffi_string(dst, dlen)
63+
local r_dlen = C.ngx_http_lua_ffi_encode_base64(s, slen, dst,
64+
no_padding_int)
65+
-- if dlen ~= r_dlen then error("discrepancy in len") end
66+
return ffi_string(dst, r_dlen)
5167
end
5268

5369

t/encode-base64.t

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,51 @@ qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
178178
[error]
179179
-- NYI:
180180
181+
182+
183+
=== TEST 7: set base64 (number) without padding (explicitly specified)
184+
--- http_config eval: $::HttpConfig
185+
--- config
186+
location = /base64 {
187+
content_by_lua '
188+
local s
189+
for i = 1, 200 do
190+
s = ngx.encode_base64(3.14, true)
191+
end
192+
ngx.say(s)
193+
';
194+
}
195+
--- request
196+
GET /base64
197+
--- response_body
198+
My4xNA
199+
--- error_log eval
200+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
201+
--- no_error_log
202+
[error]
203+
-- NYI:
204+
205+
206+
207+
=== TEST 8: set base64 (number) with padding (explictly specified)
208+
--- http_config eval: $::HttpConfig
209+
--- config
210+
location = /base64 {
211+
content_by_lua '
212+
local s
213+
for i = 1, 200 do
214+
s = ngx.encode_base64(3.14, false)
215+
end
216+
ngx.say(s)
217+
';
218+
}
219+
--- request
220+
GET /base64
221+
--- response_body
222+
My4xNA==
223+
--- error_log eval
224+
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
225+
--- no_error_log
226+
[error]
227+
-- NYI:
228+

0 commit comments

Comments
 (0)