Skip to content

Commit b641baf

Browse files
authored
Parse set-cookie expires with trailing GMT (JuliaWeb#1035)
* Parse set-cookie expires with trailing GMT * Test if addcookie! matches readsetcookies
1 parent b2c3d8f commit b641baf

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/cookies.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ end
215215

216216
iscookienamevalid(raw) = raw == "" ? false : any(isurlchar, raw)
217217

218-
const AlternateRFC1123Format = Dates.DateFormat("e, dd-uuu-yyyy HH:MM:SS G\\MT")
218+
gmtformat(::DateFormat{S,T}) where {S,T} = Dates.DateFormat(string(S, " G\\MT"))
219+
const AlternateRFC1123GMTFormat = gmtformat(dateformat"e, dd-uuu-yyyy HH:MM:SS")
220+
const RFC1123GMTFormat = gmtformat(Dates.RFC1123Format)
219221

220222
# readSetCookies parses all "Set-Cookie" values from
221223
# the header h and returns the successfully parsed Cookies.
@@ -287,10 +289,10 @@ function readsetcookies(h::Headers)
287289
elseif lowerattr == "expires"
288290
c.rawexpires = val
289291
try
290-
c.expires = Dates.DateTime(val, Dates.RFC1123Format)
292+
c.expires = Dates.DateTime(val, RFC1123GMTFormat)
291293
catch
292294
try
293-
c.expires = Dates.DateTime(val, AlternateRFC1123Format)
295+
c.expires = Dates.DateTime(val, AlternateRFC1123GMTFormat)
294296
catch
295297
continue
296298
end

test/client.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ end
8888
url = "https://$httpbin/cookies/delete?hey"
8989
r = HTTP.get(url, socket_type_tls=tls)
9090
cookies = HTTP.Cookies.getcookies!(HTTP.COOKIEJAR, URI(url))
91-
@test length(cookies) == 2
92-
@test cookies[2].value == ""
91+
@test length(cookies) == 1
9392
end
9493

9594
@testset "Client Streaming Test" begin

test/cookies.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ using Sockets, Test
7171
(["Set-Cookie" => "Cookie-1=v\$1"], [HTTP.Cookie("Cookie-1", "v\$1")]),
7272
(["Set-Cookie" => "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"],
7373
[HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain=".google.ch", httponly=true, expires=HTTP.Dates.DateTime(2011, 11, 23, 1, 5, 3, 0))]),
74+
(["Set-Cookie" => "NID=99=YsDT5i3E-CXax-; expires=Wed, 23 Nov 2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"],
75+
[HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain=".google.ch", httponly=true, expires=HTTP.Dates.DateTime(2011, 11, 23, 1, 5, 3, 0))]),
7476
(["Set-Cookie" => ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"],
7577
[HTTP.Cookie(".ASPXAUTH", "7E3AA"; path="/", expires=HTTP.Dates.DateTime(2012, 3, 7, 14, 25, 6, 0), httponly=true)]),
78+
(["Set-Cookie" => ".ASPXAUTH=7E3AA; expires=Wed, 07 Mar 2012 14:25:06 GMT; path=/; HttpOnly"],
79+
[HTTP.Cookie(".ASPXAUTH", "7E3AA"; path="/", expires=HTTP.Dates.DateTime(2012, 3, 7, 14, 25, 6, 0), httponly=true)]),
7680
(["Set-Cookie" => "ASP.NET_SessionId=foo; path=/; HttpOnly"],
7781
[HTTP.Cookie("ASP.NET_SessionId", "foo"; path="/", httponly=true)]),
7882
(["Set-Cookie" => "samesitedefault=foo; SameSite"], [HTTP.Cookie("samesitedefault", "foo"; samesite=HTTP.Cookies.SameSiteDefaultMode)]),
@@ -264,16 +268,19 @@ using Sockets, Test
264268

265269
@testset "addcookie!" begin
266270
r = HTTP.Request("GET", "/")
267-
c = HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain=".google.ch", httponly=true, expires=HTTP.Dates.DateTime(2011, 11, 23, 1, 5, 3, 0))
271+
c = HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain=".google.ch", httponly=true, expires=HTTP.Dates.DateTime(2011, 11, 23, 1, 5, 3, 0))
272+
c_parsed = HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain="google.ch", httponly=true, expires=HTTP.Dates.DateTime(2011, 11, 23, 1, 5, 3, 0))
268273
HTTP.addcookie!(r, c)
269274
@test HTTP.header(r, "Cookie") == "NID=99=YsDT5i3E-CXax-"
270275
HTTP.addcookie!(r, c)
271276
@test HTTP.header(r, "Cookie") == "NID=99=YsDT5i3E-CXax-; NID=99=YsDT5i3E-CXax-"
272277
r = HTTP.Response(200)
273278
HTTP.addcookie!(r, c)
274279
@test HTTP.header(r, "Set-Cookie") == "NID=99=YsDT5i3E-CXax-; Path=/; Domain=google.ch; Expires=Wed, 23 Nov 2011 01:05:03 GMT; HttpOnly"
280+
@test [c_parsed] == HTTP.Cookies.readsetcookies(["Set-Cookie" => HTTP.header(r, "Set-Cookie")])
275281
HTTP.addcookie!(r, c)
276282
@test HTTP.headers(r, "Set-Cookie") == ["NID=99=YsDT5i3E-CXax-; Path=/; Domain=google.ch; Expires=Wed, 23 Nov 2011 01:05:03 GMT; HttpOnly", "NID=99=YsDT5i3E-CXax-; Path=/; Domain=google.ch; Expires=Wed, 23 Nov 2011 01:05:03 GMT; HttpOnly"]
283+
@test [c_parsed, c_parsed] == HTTP.Cookies.readsetcookies(["Set-Cookie"] .=> HTTP.headers(r, "Set-Cookie"))
277284
end
278285
end
279286

0 commit comments

Comments
 (0)