Skip to content

Commit a1b7405

Browse files
committed
Add option to get cookie without escaping
Method req:cookie() implicitly unescapes cookie values. Commit adds ability to get cookie without unescaping: req:cookie('name', { raw = true }) This change was added as a part of http v2 support in commit 'Added ability to set and get cookie without escaping' (42e3002) and later reverted in scope of ticket with discard v2. Follows up #126 Part of #134
1 parent 446620a commit a1b7405

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1919
- Add workflow that publish rockspec.
2020
- Add editorconfig to configure indentation.
2121
- Add luacheck integration.
22+
- Add option to get cookie without escaping.
2223

2324
### Fixed
2425

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ end
226226
* `req:query_param(name)` - returns a single GET request parameter value.
227227
If `name` is `nil`, returns a Lua table with all arguments.
228228
* `req:param(name)` - any request parameter, either GET or POST.
229-
* `req:cookie(name)` - to get a cookie in the request.
229+
* `req:cookie(name, {raw = true})` | to get a cookie in the request. if `raw`
230+
option was set then cookie will not be unescaped, otherwise cookie's value
231+
will be unescaped.
230232
* `req:stash(name[, value])` - get or set a variable "stashed"
231233
when dispatching a route.
232234
* `req:url_for(name, args, query)` - returns the route's exact URL.

http/server.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,18 @@ local function setcookie(resp, cookie)
304304
return resp
305305
end
306306

307-
local function cookie(tx, cookie)
307+
local function cookie(tx, cookie, options)
308+
options = options or {}
308309
if tx.headers.cookie == nil then
309310
return nil
310311
end
311312
for k, v in string.gmatch(
312313
tx.headers.cookie, "([^=,; \t]+)=([^,; \t]+)") do
313314
if k == cookie then
314-
return uri_unescape(v)
315+
if not options.raw then
316+
v = uri_unescape(v)
317+
end
318+
return v
315319
end
316320
end
317321
return nil

test/integration/http_server_requests_test.lua

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -203,49 +203,53 @@ g.test_chunked_encoding = function()
203203
t.assert_equals(r.body, 'chunkedencodingt\r\nest', 'chunked body')
204204
end
205205

206-
-- Get cookie.
206+
-- Get raw cookie value (Günter -> Günter).
207207
g.test_get_cookie = function()
208+
local cookie = 'Günter'
208209
local httpd = g.httpd
209210
httpd:route({
210211
path = '/receive_cookie'
211212
}, function(req)
212-
local foo = req:cookie('foo')
213-
local baz = req:cookie('baz')
213+
local name = req:cookie('name', {
214+
raw = true
215+
})
214216
return req:render({
215-
text = ('foo=%s; baz=%s'):format(foo, baz)
217+
text = ('name=%s'):format(name)
216218
})
217219
end)
220+
218221
local r = http_client.get(helpers.base_uri .. '/receive_cookie', {
219222
headers = {
220-
cookie = 'foo=bar; baz=feez',
223+
cookie = 'name=' .. cookie,
221224
}
222225
})
223-
t.assert_equals(r.status, 200, 'status')
224-
t.assert_equals(r.body, 'foo=bar; baz=feez', 'body')
226+
227+
t.assert_equals(r.status, 200, 'response status')
228+
t.assert_equals(r.body, 'name=' .. cookie, 'body with raw cookie')
225229
end
226230

227-
-- Cookie.
228-
g.test_set_cookie = function()
231+
-- Get escaped cookie (G%C3%BCnter -> Günter).
232+
g.test_get_escaped_cookie = function()
233+
local str_escaped = 'G%C3%BCnter'
234+
local str_non_escaped = 'Günter'
229235
local httpd = g.httpd
230236
httpd:route({
231-
path = '/cookie'
237+
path = '/receive_cookie'
232238
}, function(req)
233-
local resp = req:render({text = ''})
234-
resp:setcookie({
235-
name = 'test',
236-
value = 'tost',
237-
expires = '+1y',
238-
path = '/abc'
239-
})
240-
resp:setcookie({
241-
name = 'xxx',
242-
value = 'yyy'
239+
local name = req:cookie('name')
240+
return req:render({
241+
text = ('name=%s'):format(name)
243242
})
244-
return resp
245243
end)
246-
local r = http_client.get(helpers.base_uri .. '/cookie')
247-
t.assert_equals(r.status, 200, 'status')
248-
t.assert(r.headers['set-cookie'] ~= nil, 'header')
244+
245+
local r = http_client.get(helpers.base_uri .. '/receive_cookie', {
246+
headers = {
247+
cookie = 'name=' .. str_escaped,
248+
}
249+
})
250+
251+
t.assert_equals(r.status, 200, 'response status')
252+
t.assert_equals(r.body, 'name=' .. str_non_escaped, 'body with escaped cookie')
249253
end
250254

251255
-- Request object methods.

0 commit comments

Comments
 (0)