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

download with powershell 2.0 for windows #21968

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
14 changes: 8 additions & 6 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,19 @@ end

# file downloading

downloadcmd = nothing
Copy link
Contributor

Choose a reason for hiding this comment

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

don't think this should be moved

if Sys.iswindows()
downloadcmd = :powershell
function download(url::AbstractString, filename::AbstractString)
res = ccall((:URLDownloadToFileW,:urlmon),stdcall,Cuint,
(Ptr{Void},Cwstring,Cwstring,Cuint,Ptr{Void}),C_NULL,url,filename,0,C_NULL)
if res != 0
error("automatic download failed (error: $res): $url")
end
ps = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
tls12 = "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
client = "New-Object System.Net.Webclient"
# in the following we escape ' with '' (see https://ss64.com/ps/syntax-esc.html)
downloadfile = "($client).DownloadFile('$(replace(url, "'", "''"))', '$(replace(filename, "'", "''"))')"
run(`$ps -NoProfile -Command "$tls12; $downloadfile"`)
filename
end
else
downloadcmd = nothing
function download(url::AbstractString, filename::AbstractString)
global downloadcmd
if downloadcmd === nothing
Expand Down
7 changes: 7 additions & 0 deletions test/download.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ mktempdir() do temp_dir
@test_throws ErrorException download("http://httpbin.org/status/404", missing_file)
@test !isfile(missing_file)

# Make sure we properly handle metachar '
metachar_file = joinpath(temp_dir, "metachar")
download("https://httpbin.org/get?test='^'", metachar_file)
metachar_string = readstring(metachar_file)
m = match(r"\"url\"\s*:\s*\"(.*)\"", metachar_string)
@test m.captures[1] == "https://httpbin.org/get?test='^'"

# Use a TEST-NET (192.0.2.0/24) address which shouldn't be bound
invalid_host_file = joinpath(temp_dir, "invalid_host")
@test_throws ErrorException download("http://192.0.2.1", invalid_host_file)
Expand Down