Skip to content

Commit 19dfa3f

Browse files
committed
Add ability to set cookie without escaping
TODO: broken test Method resp:setcookie() implicitly escapes cookie values. Commit adds ability to set cookie without any escaping with option 'raw': resp:setcookie('name', 'value', { 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 broken
1 parent c4337ff commit 19dfa3f

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@ end
240240
* `resp.status` - HTTP response code.
241241
* `resp.headers` - a Lua table with normalized headers.
242242
* `resp.body` - response body (string|table|wrapped\_iterator).
243-
* `resp:setcookie({ name = 'name', value = 'value', path = '/', expires = '+1y', domain = 'example.com'))` -
244-
adds `Set-Cookie` headers to `resp.headers`.
243+
* `resp:setcookie({ name = 'name', value = 'value', path = '/', expires = '+1y', domain = 'example.com'}, {raw = true})` -
244+
adds `Set-Cookie` headers to `resp.headers`, if `raw` option was set then cookie will not be escaped,
245+
otherwise cookie's value and path will be escaped
245246

246247
### Examples
247248

http/server.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ local function expires_str(str)
265265
return os.date(fmt, gmtnow + diff)
266266
end
267267

268-
local function setcookie(resp, cookie)
268+
local function setcookie(resp, cookie, options)
269+
options = options or {}
269270
local name = cookie.name
270271
local value = cookie.value
271272

test/integration/http_server_requests_test.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,55 @@ pgroup.test_get_escaped_cookie = function(g)
261261
t.assert_equals(r.body, 'name=' .. str_non_escaped, 'body with escaped cookie')
262262
end
263263

264+
-- Set escaped cookie (Günter -> G%C3%BCnter).
265+
pgroup.test_set_escaped_cookie = function(g)
266+
local str_escaped = 'G%C3%BCnter'
267+
local str_non_escaped = 'Günter'
268+
269+
local httpd = g.httpd
270+
httpd:route({
271+
path = '/cookie'
272+
}, function(req)
273+
local resp = req:render({
274+
text = ''
275+
})
276+
resp:setcookie({
277+
name = 'name',
278+
value = str_non_escaped
279+
})
280+
return resp
281+
end)
282+
283+
local r = http_client.get(helpers.base_uri .. '/cookie')
284+
t.assert_equals(r.status, 200, 'response status')
285+
t.assert_equals(r.headers['set-cookie'], 'name=' .. str_escaped, 'header with escaped cookie')
286+
end
287+
288+
-- Set raw cookie (Günter -> Günter).
289+
pgroup.test_set_raw_cookie = function(g)
290+
t.skip('Broken')
291+
local cookie = 'Günter'
292+
local httpd = g.httpd
293+
httpd:route({
294+
path = '/cookie'
295+
}, function(req)
296+
local resp = req:render({
297+
text = ''
298+
})
299+
resp:setcookie({
300+
name = 'name',
301+
value = cookie
302+
}, {
303+
raw = true
304+
})
305+
return resp
306+
end)
307+
308+
local r = http_client.get(helpers.base_uri .. '/cookie')
309+
t.assert_equals(r.status, 200, 'response status')
310+
t.assert_equals(r.headers['set-cookie'], 'name=' .. cookie, 'header with raw cookie')
311+
end
312+
264313
-- Request object methods.
265314
pgroup.test_request_object_methods = function(g)
266315
local httpd = g.httpd

0 commit comments

Comments
 (0)