Skip to content

Commit 309064c

Browse files
authored
bugfix: request headers contains "_" should be case-insensitive matched.
Signed-off-by: lijunlong <lijunlong@openresty.com>
1 parent cf64da2 commit 309064c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/resty/core/request.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local get_string_buf = base.get_string_buf
2020
local get_size_ptr = base.get_size_ptr
2121
local setmetatable = setmetatable
2222
local lower = string.lower
23+
local find = string.find
2324
local rawget = rawget
2425
local ngx = ngx
2526
local get_request = base.get_request
@@ -114,7 +115,12 @@ local truncated = ffi.new("int[1]")
114115

115116
local req_headers_mt = {
116117
__index = function (tb, key)
117-
return rawget(tb, (str_replace_char(lower(key), '_', '-')))
118+
key = lower(key)
119+
local value = rawget(tb, key)
120+
if value == nil and find(key, '_', 1, true) then
121+
value = rawget(tb, (str_replace_char(key, '_', '-')))
122+
end
123+
return value
118124
end
119125
}
120126

t/request.t

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):4 loop\]/
140140

141141

142142
=== TEST 4: ngx.req.get_headers (metatable)
143+
--- http_config
144+
underscores_in_headers on;
143145
--- config
144146
location = /t {
145147
set $foo hello;
@@ -159,6 +161,10 @@ qr/\[TRACE\s+\d+ content_by_lua\(nginx\.conf:\d+\):4 loop\]/
159161
for _, k in ipairs(keys) do
160162
ngx.say(k, ": ", headers[k])
161163
end
164+
165+
ngx.say("X_Bar_Header: ", headers["X_Bar_Header"])
166+
ngx.say("x_Bar_Header: ", headers["x_Bar_Header"])
167+
ngx.say("x_bar_header: ", headers["x_bar_header"])
162168
}
163169
}
164170
--- request
@@ -169,9 +175,14 @@ baz: baz
169175
connection: close
170176
foo-bar: foo
171177
host: localhost
178+
x_bar_header: bar
179+
X_Bar_Header: bar
180+
x_Bar_Header: bar
181+
x_bar_header: bar
172182
--- more_headers
173183
Foo-Bar: foo
174184
Baz: baz
185+
X_Bar_Header: bar
175186
--- wait: 0.2
176187
--- error_log eval
177188
qr/\[TRACE\s+\d+ .*? -> \d+\]/

0 commit comments

Comments
 (0)