Skip to content

Commit

Permalink
Make Set-Cookie response header case insensitive, fixes #765.
Browse files Browse the repository at this point in the history
Co-authored-by: Rui Rojo <rui.rojo@gmail.com>
Co-authored-by: Jacob Quinn <quinn.jacobd@gmail.com>
Co-authored-by: Fredrik Ekre <ekrefredrik@gmail.com>
  • Loading branch information
3 people committed Sep 26, 2021
1 parent f5957aa commit b701515
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/CookieRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ..Dates
import ..Layer, ..request
using URIs
using ..Cookies
using ..Messages: ascii_lc_isequal
using ..Pairs: getkv, setkv
import ..@debug, ..DEBUG_LEVEL, ..access_threaded

Expand Down Expand Up @@ -72,7 +73,8 @@ function getcookies(cookies, url)
end

function setcookies(cookies, host, headers)
for (k,v) in filter(x->x[1]=="Set-Cookie", headers)
for (k, v) in headers
ascii_lc_isequal(k, "set-cookie") || continue
@debug 1 "Set-Cookie: $v (from $host)"
push!(cookies, Cookies.readsetcookie(host, v))
end
Expand Down
41 changes: 40 additions & 1 deletion test/cookies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,43 @@
@test HTTP.Cookies.readcookies(h, filter) == cookies
end
end
end
@testset "Set-Cookie casing" begin
server = Sockets.listen(Sockets.localhost, 8080)
tsk = @async HTTP.listen(Sockets.localhost, 8080; server=server) do http
t = http.message.target
HTTP.setstatus(http, 200)
if t == "/set-cookie"
HTTP.setheader(http, "set-cookie" => "cookie=lc_cookie")
elseif t == "/Set-Cookie"
HTTP.setheader(http, "Set-Cookie" => "cookie=cc_cookie")
elseif t == "/SET-COOKIE"
HTTP.setheader(http, "SET-COOKIE" => "cookie=uc_cookie")
elseif t == "/SeT-CooKiE"
HTTP.setheader(http, "SeT-CooKiE" => "cookie=spongebob_cookie")
elseif t =="/cookie"
HTTP.setheader(http, "X-Cookie" => HTTP.header(http, "Cookie"))
end
HTTP.startwrite(http)
end

cookiejar = Dict{String,Set{HTTP.Cookies.Cookie}}()
HTTP.get("http://localhost:8080/set-cookie"; cookies=true, cookiejar=cookiejar)
r = HTTP.get("http://localhost:8080/cookie"; cookies=true, cookiejar=cookiejar)
@test HTTP.header(r, "X-Cookie") == "cookie=lc_cookie"
empty!(cookiejar)
HTTP.get("http://localhost:8080/Set-Cookie"; cookies=true, cookiejar=cookiejar)
r = HTTP.get("http://localhost:8080/cookie"; cookies=true, cookiejar=cookiejar)
@test HTTP.header(r, "X-Cookie") == "cookie=cc_cookie"
empty!(cookiejar)
HTTP.get("http://localhost:8080/SET-COOKIE"; cookies=true, cookiejar=cookiejar)
r = HTTP.get("http://localhost:8080/cookie"; cookies=true, cookiejar=cookiejar)
@test HTTP.header(r, "X-Cookie") == "cookie=uc_cookie"
empty!(cookiejar)
HTTP.get("http://localhost:8080/SeT-CooKiE"; cookies=true, cookiejar=cookiejar)
r = HTTP.get("http://localhost:8080/cookie"; cookies=true, cookiejar=cookiejar)
@test HTTP.header(r, "X-Cookie") == "cookie=spongebob_cookie"
close(server)
try; wait(tsk); catch e; end
@test istaskdone(tsk)
end
end

0 comments on commit b701515

Please sign in to comment.