win_get_url: Fixed a few issues with using FTP and added tests (#39646)

* win_get_url: Fixed a few issues with using FTP and added tests

* Fixed typo in docs
This commit is contained in:
Jordan Borean 2018-05-04 08:39:37 +10:00 committed by GitHub
commit f75b7a9437
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 393 additions and 151 deletions

View file

@ -5,8 +5,7 @@
# Copyright: (c) 2017, Dag Wieers <dag@wieers.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# WANT_JSON
# POWERSHELL_COMMON
#Requires -Module Ansible.ModuleUtils.Legacy
$ErrorActionPreference = 'Stop'
@ -35,8 +34,8 @@ Function CheckModified-File($url, $dest, $headers, $credentials, $timeout, $use_
$fileLastMod = ([System.IO.FileInfo]$dest).LastWriteTimeUtc
$webLastMod = $null
$webRequest = [System.Net.HttpWebRequest]::Create($url)
$webRequest = [System.Net.WebRequest]::Create($url)
foreach ($header in $headers.GetEnumerator()) {
$webRequest.Headers.Add($header.Name, $header.Value)
}
@ -53,14 +52,23 @@ Function CheckModified-File($url, $dest, $headers, $credentials, $timeout, $use_
}
if ($credentials) {
$webRequest.Credentials = $credentials
if ($force_basic_auth) {
$extWebClient.Headers.Add("Authorization","Basic $credentials")
} else {
$extWebClient.Credentials = $credentials
}
}
$webRequest.Method = "HEAD"
if ($webRequest -is [System.Net.FtpWebRequest]) {
$webRequest.Method = [System.Net.WebRequestMethods+Ftp]::GetDateTimestamp
} else {
$webRequest.Method = [System.Net.WebRequestMethods+Http]::Head
}
Try {
[System.Net.HttpWebResponse]$webResponse = $webRequest.GetResponse()
$webResponse = $webRequest.GetResponse()
$webLastMod = $webResponse.GetResponseHeader("Last-Modified")
$webLastMod = $webResponse.LastModified
} Catch [System.Net.WebException] {
$result.status_code = $_.Exception.Response.StatusCode
Fail-Json -obj $result -message "Error requesting '$url'. $($_.Exception.Message)"
@ -174,7 +182,7 @@ if ($proxy_url) {
}
$credentials = $null
if ($url_username -and $url_password) {
if ($url_username) {
if ($force_basic_auth) {
$credentials = [convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($url_username+":"+$url_password))
} else {
@ -241,3 +249,4 @@ if ($force -or -not (Test-Path -LiteralPath $dest)) {
}
Exit-Json -obj $result

View file

@ -15,9 +15,10 @@ DOCUMENTATION = r'''
---
module: win_get_url
version_added: "1.7"
short_description: Fetches a file from a given URL
short_description: Downloads file from HTTP, HTTPS, or FTP to node
description:
- Fetches a file from a URL and saves it locally.
- Downloads files from HTTP, HTTPS, or FTP to the remote server. The remote
server I(must) have direct access to the remote resource.
- For non-Windows targets, use the M(get_url) module instead.
author:
- Paul Durivage (@angstwad)
@ -101,8 +102,6 @@ options:
- Timeout in seconds for URL request.
default: 10
version_added : '2.4'
notes:
- For non-Windows targets, use the M(get_url) module instead.
'''
EXAMPLES = r'''
@ -124,6 +123,13 @@ EXAMPLES = r'''
proxy_url: http://10.0.0.1:8080
proxy_username: username
proxy_password: password
- name: Download file from FTP with authentication
win_get_url:
url: ftp://server/file.txt
dest: '%TEMP%\ftp-file.txt'
url_username: ftp-user
url_password: ftp-password
'''
RETURN = r'''