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

Use powershell command for download #144

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.6
Compat 0.42.0
Compat 0.56.0
URIParser 0.0.3
@unix HTTPClient 0.0.0
LibExpat 0.2.8
Expand Down
22 changes: 11 additions & 11 deletions src/WinRPM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ if isunix()
end
elseif iswindows()
function download(source::AbstractString; retry=5)
dest = Vector{UInt16}(261)
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)
filename = joinpath(tempdir(), split(source, "/")[end])
downloadfile = "($client).DownloadFile('$(@compat replace(source, "'" => "''"))', '$(@compat replace(filename, "'" => "''"))')"
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't need the at-compat macro for these, believe they're method extensions in compat rather than syntax rewrites

Copy link
Author

Choose a reason for hiding this comment

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

Actually, the old method replace(str, pat, r) has been deprecated in 0.7 with replace(s::AbstractString, pat=>r; [count::Integer]). Therefore the @compat for use on both 0.6 and 1.0.

Copy link
Member

Choose a reason for hiding this comment

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

@compat is only needed for syntactic changes. Else you just need using Compat somewhere (either in the file or once for the whole package).

for i in 1:retry
res = ccall((:URLDownloadToCacheFileW, :urlmon), stdcall, Cuint,
(Ptr{Void}, Ptr{UInt16}, Ptr{UInt16}, Clong, Cint, Ptr{Void}),
C_NULL, transcode(UInt16, source), dest, sizeof(dest) >> 1, 0, C_NULL)
if res == 0
resize!(dest, findfirst(iszero, dest) - 1)
filename = transcode(String, dest)
try
run(`$ps -NoProfile -Command "$tls12; $downloadfile"`)
if isfile(filename)
return read(filename, String), 200
return readstring(filename), 200
end
else
warn("Unknown download failure, error code: $res")
catch ex
warn("Unknown download failure. Retry $i/$retry downloading: $source")
end
warn("Retry $i/$retry downloading: $source")
end
return "", 0
end
Expand Down