Skip to content

Commit

Permalink
Use powershell for download function for Windows (#25477)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jan 13, 2018
1 parent 9df04ac commit 4a50e64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 7 additions & 5 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,14 @@ end

downloadcmd = nothing
if Sys.iswindows()
downloadcmd = :powershell
function download(url::AbstractString, filename::AbstractString)
res = ccall((:URLDownloadToFileW,:urlmon),stdcall,Cuint,
(Ptr{Cvoid},Cwstring,Cwstring,Cuint,Ptr{Cvoid}),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
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 = read(metachar_file, String)
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

0 comments on commit 4a50e64

Please sign in to comment.