Skip to content

Commit 8813f78

Browse files
authored
Merge pull request #551 from 3scale/http-ng-performance
[http_ng] improve performance
2 parents efb50a9 + ee000af commit 8813f78

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

gateway/src/resty/http_ng/headers.lua

+9-12
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ local getmetatable = getmetatable
99
local type = type
1010
local lower = string.lower
1111
local upper = string.upper
12-
local sub = string.sub
1312
local ngx_re = ngx.re
13+
local re_gsub = ngx_re.gsub
1414

1515
local normalize_exceptions = {
1616
etag = 'ETag'
@@ -27,20 +27,17 @@ local headers_mt = {
2727
end
2828
}
2929

30-
local capitalize = function(string)
31-
return upper(sub(string, 1, 1)) .. sub(string, 2)
30+
local capitalize = function(m)
31+
return upper(m[0])
3232
end
3333

34-
local regex_parts = [[[^_-]+]]
34+
local letter = [[\b([a-z])]]
3535

36-
local key_parts_capitalized = function(key)
37-
local parts = {}
36+
local capitalize_header = function(key)
37+
key = re_gsub(key, '_', '-', 'jo')
38+
key = re_gsub(key, letter, capitalize, 'jo')
3839

39-
for matches in ngx_re.gmatch(key, regex_parts, 'jo') do
40-
insert(parts, capitalize(matches[0]))
41-
end
42-
43-
return parts
40+
return key
4441
end
4542

4643
headers.normalize_key = function(key)
@@ -50,7 +47,7 @@ headers.normalize_key = function(key)
5047
return exception
5148
end
5249

53-
return concat(key_parts_capitalized(key), '-')
50+
return capitalize_header(key)
5451
end
5552

5653
local header_mt = {

gateway/src/resty/http_ng/request.lua

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
local match = string.match
1+
local find = string.find
2+
local sub = string.sub
23
local assert = assert
34
local setmetatable = setmetatable
45

@@ -19,12 +20,21 @@ local request = { }
1920

2021
request.headers = require 'resty.http_ng.headers'
2122

23+
local function extract_host(url)
24+
local _, last = find(url, '://', 0, true)
25+
local len = find(url, '/', last + 1, true)
26+
27+
if len then len = len - 1 end
28+
29+
return sub(url, last + 1, len)
30+
end
31+
2232
function request.extract_headers(req)
2333
local options = req.options or {}
2434
local headers = request.headers.new(options.headers)
2535

2636
headers.user_agent = headers.user_agent or 'APIcast (+https://www.apicast.io)'
27-
headers.host = headers.host or match(req.url, "^.+://([^/]+)")
37+
headers.host = headers.host or extract_host(req.url)
2838
headers.connection = headers.connection or 'Keep-Alive'
2939

3040
options.headers = nil

0 commit comments

Comments
 (0)