Skip to content

Commit f75a8a3

Browse files
committed
perf(http_proxy): small optimization to avoid unnecessary allocation
1 parent c6ea9e1 commit f75a8a3

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

gateway/src/apicast/http_proxy.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local format = string.format
22
local tostring = tostring
3+
local ngx = ngx
34
local ngx_get_method = ngx.req.get_method
45
local ngx_http_version = ngx.req.http_version
6+
local ngx_req_get_headers = ngx.req.get_headers
57

68
local resty_url = require "resty.url"
79
local url_helper = require('resty.url_helper')
@@ -49,9 +51,9 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
4951
local sock
5052
local opts = proxy_opts or {}
5153
local req_method = ngx_get_method()
52-
local encoding = ngx.req.get_headers()["Transfer-Encoding"]
54+
local encoding = ngx_req_get_headers()["Transfer-Encoding"]
5355
local is_chunked = encoding and encoding:lower() == "chunked"
54-
local content_type = ngx.req.get_headers()["Content-Type"]
56+
local content_type = ngx_req_get_headers()["Content-Type"]
5557
local content_type_is_urlencoded = content_type and content_type:lower() == "application/x-www-form-urlencoded"
5658
local raw = false
5759

@@ -138,9 +140,9 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
138140

139141
local request = {
140142
uri = uri,
141-
method = ngx.req.get_method(),
142-
headers = ngx.req.get_headers(0, true),
143-
path = format('%s%s%s', ngx.var.uri, ngx.var.is_args, ngx.var.query_string or ''),
143+
method = req_method,
144+
headers = ngx_req_get_headers(0, true),
145+
path = (ngx.var.uri or '') .. (ngx.var.is_args or '') .. (ngx.var.query_string or ''),
144146
body = body,
145147
proxy_uri = proxy_uri,
146148
proxy_options = opts
@@ -194,7 +196,7 @@ function _M.request(upstream, proxy_uri)
194196
local proxy_auth
195197

196198
if proxy_uri.user or proxy_uri.password then
197-
proxy_auth = "Basic " .. ngx.encode_base64(concat({ proxy_uri.user or '', proxy_uri.password or '' }, ':'))
199+
proxy_auth = "Basic " .. ngx.encode_base64((proxy_uri.user or '') .. ":" .. (proxy_uri.password or ''))
198200
end
199201

200202
if uri.scheme == 'http' then -- rewrite the request to use http_proxy

0 commit comments

Comments
 (0)