Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use .netrc if available and enable session cookies by default #98

Merged
merged 7 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Curl/Easy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function set_defaults(easy::Easy)
setopt(easy, CURLOPT_MAXREDIRS, 50)
setopt(easy, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL)
setopt(easy, CURLOPT_USERAGENT, USER_AGENT)
setopt(easy, CURLOPT_NETRC, CURL_NETRC_OPTIONAL)
setopt(easy, CURLOPT_COOKIEFILE, "")

# ssh-related options
setopt(easy, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path())
setopt(easy, CURLOPT_SSH_PUBLIC_KEYFILE, ssh_pub_key_path())
Expand Down
46 changes: 46 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,52 @@ include("setup.jl")
end
end

@testset "session support" begin
downloader = Downloader()

# This url will redirect to /cookies, which echoes the set cookies as json
set_cookie_url = "$server/cookies/set?k1=v1&k2=v2"
cookies = download_json(set_cookie_url, downloader=downloader)
Copy link
Sponsor Member Author

@evetion evetion Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have a control test for this, but it seems there's no way to actually disable the cookie engine once set.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setopt(easy, CURLOPT_COOKIEFILE, C_NULL)?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to work, this fails:

# Setup control (disable cookie engine)
downloader = Downloader()
easy_hook = (easy, info) -> begin
    Downloads.Curl.setopt(
        easy,
        Downloads.Curl.CURLOPT_COOKIEFILE, C_NULL)
end
downloader.easy_hook = easy_hook
cookies = download_json(set_cookie_url, downloader=downloader)
@test isempty(cookies)

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. That's a shame. I guess we don't strictly need the control. I may file an issue with libcurl.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, that's quite productive. Do you want to wait for a fix (and subsequent release?).

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we should proceed without it since it's a bunch of effort to update the library version (new BinaryBuilder release, bumping version in Julia's makefiles, checksum updates — it's a whole process).

@test get(cookies, "k1", "") == "v1"
@test get(cookies, "k2", "") == "v2"

# As the handle is destroyed, subsequent requests have no cookies
cookie_url = "$server/cookies"
cookies = download_json(cookie_url, downloader=downloader)
@test isempty(cookies)
end

@testset "netrc support" begin

auth_url = "$server/basic-auth/user/passwd"
resp = request(auth_url)
@test resp isa Response
@test resp.status == 401 # no succesful authentication

# Setup .netrc
servername = split(server, "/")[end] # strip https://
netrc = tempname()
open(netrc, "w") do io
write(io, "machine $servername login user password passwd\n")
end

# Setup config to point to custom .netrc (normally in ~/.netrc)
downloader = Downloads.Downloader()
easy_hook = (easy, info) -> begin
Downloads.Curl.setopt(
easy,
Downloads.Curl.CURLOPT_NETRC_FILE, joinpath(@__DIR__, netrc))
evetion marked this conversation as resolved.
Show resolved Hide resolved
end
downloader.easy_hook = easy_hook

resp = request(auth_url, throw=false, downloader=downloader)
@test resp isa Response
@test resp.status == 200 # succesful authentication

# Cleanup
rm(netrc) # isn't cleaned automatically on Julia 1.3
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best practice to explicitly delete it anyway.

end

@testset "file protocol" begin
@testset "success" begin
path = tempname()
Expand Down