-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eloy Coto <eloy.coto@gmail.com>
- Loading branch information
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
local ffi = require "ffi" | ||
|
||
local C = ffi.C | ||
local ffi_str = ffi.string | ||
|
||
ffi.cdef[[ | ||
uintptr_t ngx_escape_uri(unsigned char *dst, unsigned char *src, size_t size, unsigned int type); | ||
]] | ||
|
||
local ngx_escape_uri = C.ngx_escape_uri | ||
|
||
local _M = { } | ||
|
||
|
||
function _M.escape_uri(source_uri) | ||
if not source_uri then | ||
return "" | ||
end | ||
|
||
local source_uri_len = #source_uri | ||
|
||
local source_str = ffi.new("unsigned char[?]", source_uri_len) | ||
ffi.copy(source_str, source_uri) | ||
|
||
-- If destination is NUL ngx_escape_uri returns the number of characters that | ||
-- are going to be escaped, need to calculate first to make sure to | ||
-- allocate the right amount of memory. | ||
local escape_len = 2 * ngx_escape_uri(nil, source_str, source_uri_len, 0) | ||
|
||
local dst = ffi.new("unsigned char[?]", source_uri_len + 1 + tonumber(escape_len)) | ||
ngx_escape_uri(dst, source_str, source_uri_len, 0) | ||
return ffi_str(dst) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
local escape = require("resty.http.uri_escape") | ||
|
||
describe('resty.http.uri_escape', function() | ||
|
||
it("escapes uri correctly", function() | ||
local test_cases = { | ||
{"/foo /test", "/foo%20/test"}, | ||
{"/foo/test", "/foo/test"}, | ||
{"/foo/", "/foo/"}, | ||
{"/foo / test", "/foo%20/%20test"}, | ||
{"/foo / test", "/foo%20%20%20%20/%20%20test"}, | ||
{"/foo#/test", "/foo%23/test"}, | ||
{"/foo$/test", "/foo$/test"}, | ||
{"/foo=/test", "/foo=/test"}, | ||
{"/foo!/test", "/foo!/test"} , | ||
{"/foo,/test", "/foo,/test"}, | ||
} | ||
|
||
for _,val in ipairs(test_cases) do | ||
assert.are.same(escape.escape_uri(val[1]), val[2]) | ||
end | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters