From 3bda801f9f3ab88fc58f8fe4ba43298544a596b0 Mon Sep 17 00:00:00 2001 From: phlavenk Date: Mon, 5 Mar 2018 15:00:48 +0100 Subject: [PATCH 01/10] Use powershell command for download Use powershell command as proposed in JuliaLang#25477. Later on, I'd prefer only one download function in Base that would work - or would be easy to overwrite in .juliarc for really specific configuration. --- src/WinRPM.jl | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index d1037a0..c88104c 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -52,24 +52,15 @@ if isunix() unsafe_string(x.body), x.http_code end elseif iswindows() - function download(source::AbstractString; retry=5) - dest = Vector{UInt16}(261) - 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) - if isfile(filename) - return read(filename, String), 200 - end - else - warn("Unknown download failure, error code: $res") - end - warn("Retry $i/$retry downloading: $source") - end - return "", 0 + function download(source::AbstractString) + 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('$(replace(source, "'" => "''"))', '$(replace(filename, "'" => "''"))')" + run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) + readstring(filename), 200 end else error("Platform not supported: $(Sys.KERNEL)") From b830d324a09c5bb10cbdb3e1502993364db868b5 Mon Sep 17 00:00:00 2001 From: "Hlavenka, Petr" Date: Tue, 6 Mar 2018 08:57:30 +0100 Subject: [PATCH 02/10] retrying preserved --- src/WinRPM.jl | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index c88104c..359fd07 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -52,15 +52,24 @@ if isunix() unsafe_string(x.body), x.http_code end elseif iswindows() - function download(source::AbstractString) + function download(source::AbstractString; retry=5) 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('$(replace(source, "'" => "''"))', '$(replace(filename, "'" => "''"))')" - run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) - readstring(filename), 200 + for i in 1:retry + try + run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) + if isfile(filename) + return readstring(filename), 200 + end + catch ex + warn("Unknown download failure. Retry $i/$retry downloading: $source") + end + end + return "", 0 end else error("Platform not supported: $(Sys.KERNEL)") From 4c5d0075e46b3660a1062eb85a3c06626878a35f Mon Sep 17 00:00:00 2001 From: phlavenk Date: Tue, 6 Mar 2018 09:38:58 +0100 Subject: [PATCH 03/10] add @compat to replace --- src/WinRPM.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index 359fd07..ea06aa0 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -58,7 +58,7 @@ elseif iswindows() 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('$(replace(source, "'" => "''"))', '$(replace(filename, "'" => "''"))')" + downloadfile = "($client).DownloadFile('$(@compat replace(source, "'" => "''"))', '$(@compat replace(filename, "'" => "''"))')" for i in 1:retry try run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) From 3860451b90c4af0bdc298ddf5f745d87c56e2cc8 Mon Sep 17 00:00:00 2001 From: phlavenk Date: Tue, 6 Mar 2018 09:40:08 +0100 Subject: [PATCH 04/10] Due @compat replace, update Compat version --- REQUIRE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REQUIRE b/REQUIRE index 89b198a..c5d5eea 100644 --- a/REQUIRE +++ b/REQUIRE @@ -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 From c2fe28c47c62a3d9f093f757651865acc6304c1c Mon Sep 17 00:00:00 2001 From: phlavenk Date: Wed, 28 Mar 2018 14:32:21 +0200 Subject: [PATCH 05/10] Add warning for .Net version --- src/WinRPM.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index ea06aa0..649d5b8 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -69,6 +69,8 @@ elseif iswindows() warn("Unknown download failure. Retry $i/$retry downloading: $source") end end + warn("""Unknown download failure. WinRPM download function relies on Windows PowerShell functionality. + Check that .NET framework 4.5 or higher is installed (TLS 1.2 protocol support)""") return "", 0 end else From fccd06133b63d0babc3aa7d40b7908ef018c1f9a Mon Sep 17 00:00:00 2001 From: phlavenk Date: Thu, 29 Mar 2018 09:30:09 +0200 Subject: [PATCH 06/10] removed @compat and mention PS>=3 --- src/WinRPM.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index 649d5b8..e7fe74f 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -57,11 +57,11 @@ elseif iswindows() 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, "'" => "''"))')" + filename = joinpath(tempdir(), split(source, "/")[end]) + downloadfile = "($client).DownloadFile('$(replace(source, "'" => "''"))', '$(replace(filename, "'" => "''"))')" for i in 1:retry try - run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) + run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) if isfile(filename) return readstring(filename), 200 end @@ -70,7 +70,7 @@ elseif iswindows() end end warn("""Unknown download failure. WinRPM download function relies on Windows PowerShell functionality. - Check that .NET framework 4.5 or higher is installed (TLS 1.2 protocol support)""") + Check that PowerShell 3 or higher is installed and TLS 1.2 protocol support enabled.)""") return "", 0 end else From f19c01925ad1deae3fb0c4d4a77c1a391a9234ff Mon Sep 17 00:00:00 2001 From: phlavenk Date: Tue, 3 Apr 2018 06:15:40 +0200 Subject: [PATCH 07/10] fix trailing `)` --- src/WinRPM.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index e7fe74f..57ced34 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -70,7 +70,7 @@ elseif iswindows() end end warn("""Unknown download failure. WinRPM download function relies on Windows PowerShell functionality. - Check that PowerShell 3 or higher is installed and TLS 1.2 protocol support enabled.)""") + Check that PowerShell 3 or higher is installed and TLS 1.2 protocol support enabled.""") return "", 0 end else From 9b5860ceb77f460d1e1062fc34edb2f4fe75e425 Mon Sep 17 00:00:00 2001 From: "Hlavenka, Petr" Date: Wed, 4 Apr 2018 10:54:33 +0200 Subject: [PATCH 08/10] Use Base.download on 0.7 --- src/WinRPM.jl | 52 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index 57ced34..93912b2 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -46,22 +46,41 @@ function __init__() update(false, false) end -if isunix() - function download(source::AbstractString) - x = HTTPC.get(source) - unsafe_string(x.body), x.http_code +if VERSION < v"0.7.0-DEV" + if isunix() + function download(source::AbstractString) + x = HTTPC.get(source) + unsafe_string(x.body), x.http_code + end + elseif iswindows() + function download(source::AbstractString; retry=5) + dest = Vector{UInt16}(261) + 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) + if isfile(filename) + return read(filename, String), 200 + end + else + warn("Unknown download failure, error code: $res") + end + warn("Retry $i/$retry downloading: $source") + return "", 0 + end + end + else + error("Platform not supported: $(Sys.KERNEL)") end -elseif iswindows() +else function download(source::AbstractString; retry=5) - 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('$(replace(source, "'" => "''"))', '$(replace(filename, "'" => "''"))')" for i in 1:retry try - run(`$ps -NoProfile -Command "$tls12; $downloadfile"`) + filename = joinpath(tempdir(), split(source, "/")[end]) + filename = Base.download(source, filename) if isfile(filename) return readstring(filename), 200 end @@ -69,12 +88,13 @@ elseif iswindows() warn("Unknown download failure. Retry $i/$retry downloading: $source") end end - warn("""Unknown download failure. WinRPM download function relies on Windows PowerShell functionality. - Check that PowerShell 3 or higher is installed and TLS 1.2 protocol support enabled.""") + warn("""Unknown download failure in `Base.download` function""") + if iswindows() + warn("Base.download function relies on Windows PowerShell functionality. "* + "Check that PowerShell 3 or higher is installed and TLS 1.2 protocol support enabled.") + end return "", 0 end -else - error("Platform not supported: $(Sys.KERNEL)") end getcachedir(source) = getcachedir(cachedir, source) From 6de4f2160f6b125b24f484d0e1c68b7d05ce847b Mon Sep 17 00:00:00 2001 From: "Hlavenka, Petr" Date: Wed, 4 Apr 2018 16:33:16 +0200 Subject: [PATCH 09/10] Just print download error exception --- src/WinRPM.jl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/WinRPM.jl b/src/WinRPM.jl index 93912b2..a4b2481 100644 --- a/src/WinRPM.jl +++ b/src/WinRPM.jl @@ -68,30 +68,29 @@ if VERSION < v"0.7.0-DEV" else warn("Unknown download failure, error code: $res") end - warn("Retry $i/$retry downloading: $source") - return "", 0 + warn("Retry $i/$retry downloading: $source") end + return "", 0 end else error("Platform not supported: $(Sys.KERNEL)") end else - function download(source::AbstractString; retry=5) + function download(source::AbstractString; retry=5) for i in 1:retry try filename = joinpath(tempdir(), split(source, "/")[end]) filename = Base.download(source, filename) if isfile(filename) return readstring(filename), 200 - end + end catch ex - warn("Unknown download failure. Retry $i/$retry downloading: $source") + if i == retry + warn("download from $source failed: $ex") + return "", 0 + end end - end - warn("""Unknown download failure in `Base.download` function""") - if iswindows() - warn("Base.download function relies on Windows PowerShell functionality. "* - "Check that PowerShell 3 or higher is installed and TLS 1.2 protocol support enabled.") + warn("Retry $i/$retry downloading: $source") end return "", 0 end From c5a726938743ce865338775eefda3735f38e66b3 Mon Sep 17 00:00:00 2001 From: phlavenk Date: Wed, 4 Apr 2018 16:37:52 +0200 Subject: [PATCH 10/10] revert require --- REQUIRE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REQUIRE b/REQUIRE index c5d5eea..89b198a 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,5 +1,5 @@ julia 0.6 -Compat 0.56.0 +Compat 0.42.0 URIParser 0.0.3 @unix HTTPClient 0.0.0 LibExpat 0.2.8