Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Webclient doesn't support downloading URLs with query-strings #29

Open
dandunckelman opened this issue Feb 22, 2016 · 2 comments
Open

Comments

@dandunckelman
Copy link

I came across this today when trying to use this module to download a git repo's zip file.

I think to support this, you could use a different web client or take in a new param to hold the query-string (and add that object to the webclient request).

@manheraz
Copy link

I had issues with this too.

Tried downloading Windows ADK and it fails, but I don't think that the problem is the webclient (System.Net.WebClient), as it works when launched from PowerShell:

$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile('https://go.microsoft.com/fwlink/p/?linkid=859206', '.\test-windows-adk.exe')

That worked as expected.

@manheraz
Copy link

manheraz commented Apr 14, 2018

Got it.

If you don't define the parameter destination_file, the module tries to obtain it from the URL:

if $destination_file {
    $filename = $destination_file
  } else {
    $filename = regsubst($url, '^http.*\/([^\/]+)$', '\1')
}

But if the URL doesn't end with a filename, the code pasted above could led to unexpected results. Let's use the Windows ADK download URL as an example:

$url = 'https://go.microsoft.com/fwlink/p/?linkid=859206'
$filename = regsubst($url, '^http.*\/([^\/]+)$', '\1')

# Now $filename has the value '?linkid=859206'

Windows can't create a file that has the ? character in the name, and that's why the module fails with those URLs.

The same URL ending with a filename should work:

$url = 'https://go.microsoft.com/fwlink/p/?linkid=859206/test.exe'
$filename = regsubst($url, '^http.*\/([^\/]+)$', '\1')

# Now $filename has the value 'test.exe'

So, if the URL doesn't end with a filename, the parameter destination_file should be mandatory to avoid strange filenames or errors. The documentation should be updated to explain this.

Also, the module could check if the $filename variable contains reserved characters: MSDN - Naming Files, Paths, and Namespaces

@liamjbennett and @bastelfreak: what do you think? Is this correct?

Edit: typos.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants