Skip to content

Commit

Permalink
Use powershell for download function for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed Jun 2, 2017
1 parent a692b2a commit a909b64
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,12 @@ end
downloadcmd = nothing
if is_windows()
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
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("https://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("https://192.0.2.1", invalid_host_file)
Expand Down

0 comments on commit a909b64

Please sign in to comment.