Introduce the Download#FailOnError option. (#195)

* Introduce the Download#FailOnError option.

The change also adds logging of download operations to the wrapper log

* Add documentation for the failOnError flag
This commit is contained in:
Oleg Nenashev
2017-04-11 22:57:39 +02:00
committed by GitHub
parent 5803d3ce15
commit f0770a0e15
6 changed files with 111 additions and 6 deletions

26
src/Core/WinSWCore/Download.cs Normal file → Executable file
View File

@@ -20,11 +20,22 @@ namespace winsw
public readonly string Username;
public readonly string Password;
public readonly bool UnsecureAuth = false;
public readonly bool FailOnError;
public Download(string from, string to, bool failOnError = false)
{
From = from;
To = to;
FailOnError = failOnError;
}
internal Download(XmlNode n)
{
From = Environment.ExpandEnvironmentVariables(n.Attributes["from"].Value);
To = Environment.ExpandEnvironmentVariables(n.Attributes["to"].Value);
var failOnErrorNode = n.Attributes["failOnError"];
FailOnError = failOnErrorNode != null ? Boolean.Parse(failOnErrorNode.Value) : false;
string tmpStr = "";
try
@@ -80,6 +91,12 @@ namespace winsw
request.Headers["Authorization"] = "Basic " + authInfo;
}
/// <summary>
/// Downloads the requested file and puts it to the specified target.
/// </summary>
/// <exception cref="System.Net.WebException">
/// Download failure. FailOnError flag should be processed outside.
/// </exception>
public void Perform()
{
WebRequest req = WebRequest.Create(From);
@@ -106,6 +123,15 @@ namespace winsw
File.Move(To + ".tmp", To);
}
/// <summary>
/// Produces the XML configuuration entry.
/// </summary>
/// <returns>XML String for the configuration file</returns>
public String toXMLConfig()
{
return "<download from=\"" + From + "\" to=\"" + To + "\" failOnError=\"" + FailOnError + "\"/>";
}
private static void CopyStream(Stream i, Stream o)
{
byte[] buf = new byte[8192];