This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new c31edf3  bug: uri safe encode. (#1461)
c31edf3 is described below

commit c31edf3899eeeba97bc2ac26fc7b9c49c9833a97
Author: YuanSheng Wang <[email protected]>
AuthorDate: Thu Apr 16 08:00:45 2020 +0800

    bug: uri safe encode. (#1461)
---
 apisix/core/utils.lua            | 21 +++++++++++++++++++++
 apisix/plugins/proxy-rewrite.lua |  2 ++
 2 files changed, 23 insertions(+)

diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua
index 98e678e..d85f7bb 100644
--- a/apisix/core/utils.lua
+++ b/apisix/core/utils.lua
@@ -18,11 +18,22 @@ local table    = require("apisix.core.table")
 local ngx_re   = require("ngx.re")
 local resolver = require("resty.dns.resolver")
 local ipmatcher= require("resty.ipmatcher")
+local ffi      = require("ffi")
+local base     = require("resty.core.base")
 local open     = io.open
 local math     = math
 local sub_str  = string.sub
 local str_byte = string.byte
 local tonumber = tonumber
+local C        = ffi.C
+local ffi_string = ffi.string
+local get_string_buf = base.get_string_buf
+
+
+ffi.cdef[[
+    int ngx_escape_uri(char *dst, const char *src,
+        size_t size, int type);
+]]
 
 
 local _M = {
@@ -144,4 +155,14 @@ function _M.parse_addr(addr)
 end
 
 
+function _M.uri_safe_encode(uri)
+    local count_escaped = C.ngx_escape_uri(nil, uri, #uri, 0)
+    local len = #uri + 2 * count_escaped
+    local buf = get_string_buf(len)
+    C.ngx_escape_uri(buf, uri, #uri, 0)
+
+    return ffi_string(buf, len)
+end
+
+
 return _M
diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua
index 70d4cf3..b65384d 100644
--- a/apisix/plugins/proxy-rewrite.lua
+++ b/apisix/plugins/proxy-rewrite.lua
@@ -144,6 +144,8 @@ function _M.rewrite(conf, ctx)
         end
     end
 
+    upstream_uri = core.utils.uri_safe_encode(upstream_uri)
+
     if ctx.var.is_args == "?" then
         ctx.var.upstream_uri = upstream_uri .. "?" .. (ctx.var.args or "")
     else

Reply via email to