Skip to content

Commit 9844284

Browse files
committed
perf(http_proxy) small optimization to avoid unessary allocation
1 parent 107d58d commit 9844284

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

gateway/src/apicast/http_proxy.lua

Lines changed: 12 additions & 9 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')
@@ -12,7 +14,6 @@ local file_size = require("resty.file").file_size
1214
local client_body_reader = require("resty.http.request_reader").get_client_body_reader
1315
local send_response = require("resty.http.response_writer").send_response
1416
local proxy_response = require("resty.http.response_writer").proxy_response
15-
local concat = table.concat
1617

1718
local _M = { }
1819

@@ -50,9 +51,11 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
5051
local sock
5152
local opts = proxy_opts or {}
5253
local req_method = ngx_get_method()
53-
local encoding = ngx.req.get_headers()["Transfer-Encoding"]
54+
local headers = ngx_req_get_headers(0, true)
55+
56+
local encoding = headers["Transfer-Encoding"]
5457
local is_chunked = encoding and encoding:lower() == "chunked"
55-
local content_type = ngx.req.get_headers()["Content-Type"]
58+
local content_type = headers["Content-Type"]
5659
local content_type_is_urlencoded = content_type and content_type:lower() == "application/x-www-form-urlencoded"
5760
local raw = false
5861

@@ -123,7 +126,7 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
123126
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
124127
end
125128

126-
ngx.req.set_header("Content-Length", tostring(contentLength))
129+
headers["Content-Length"] = tostring(contentLength)
127130
end
128131
end
129132
end
@@ -132,16 +135,16 @@ local function forward_https_request(proxy_uri, uri, proxy_opts)
132135
-- header, otherwise the upstream won't be able to read the body as it expected chunk encoded
133136
-- body
134137
if is_chunked then
135-
ngx.req.set_header("Transfer-Encoding", nil)
138+
headers["Content-Length"] = nil
136139
end
137140
end
138141
end
139142

140143
local request = {
141144
uri = uri,
142-
method = ngx.req.get_method(),
143-
headers = ngx.req.get_headers(0, true),
144-
path = format('%s%s%s', ngx.var.uri, ngx.var.is_args, ngx.var.query_string or ''),
145+
method = req_method,
146+
headers = headers,
147+
path = ngx.var.uri .. ngx.var.is_args .. (ngx.var.query_string or ''),
145148
body = body,
146149
proxy_uri = proxy_uri,
147150
proxy_options = opts
@@ -199,7 +202,7 @@ function _M.request(upstream, proxy_uri)
199202
local proxy_auth
200203

201204
if proxy_uri.user or proxy_uri.password then
202-
proxy_auth = "Basic " .. ngx.encode_base64(concat({ proxy_uri.user or '', proxy_uri.password or '' }, ':'))
205+
proxy_auth = "Basic " .. ngx.encode_base64((proxy_uri.user or '') .. ":" .. (proxy_uri.password or ''))
203206
end
204207

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

0 commit comments

Comments
 (0)