Fix: Plugin Downloads via HttpWebRequest

Enables 'KeepAlive=false' support.
This commit is contained in:
Gary Sharp
2013-05-06 19:08:09 +10:00
parent 1f561d6535
commit 9d39eff7ee
2 changed files with 46 additions and 20 deletions
+23 -9
View File
@@ -36,18 +36,32 @@ namespace Disco.Services.Plugins
Directory.CreateDirectory(Path.GetDirectoryName(packageFilePath)); Directory.CreateDirectory(Path.GetDirectoryName(packageFilePath));
// Need to Download the Package // Need to Download the Package
WebClient downloader = new WebClient(); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(packageUrlPath);
DateTime progressExpires = DateTime.Now; webRequest.KeepAlive = false;
downloader.DownloadProgressChanged += (sender, e) =>
webRequest.ContentType = "application/xml";
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.UserAgent = string.Format("Disco/{0} (PluginLibrary)", Disco.Services.Plugins.CommunityInterop.PluginLibraryUpdateTask.CurrentDiscoVersion());
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{ {
Console.WriteLine(e.ProgressPercentage); if (webResponse.StatusCode == HttpStatusCode.OK)
if (progressExpires <= DateTime.Now)
{ {
this.Status.UpdateStatus(e.ProgressPercentage, string.Format("{0} of {1} KB downloaded", e.BytesReceived / 1024, e.TotalBytesToReceive / 1024)); Status.UpdateStatus(0, "Downloading...");
progressExpires = DateTime.Now.AddMilliseconds(250); using (var wResStream = webResponse.GetResponseStream())
{
using (FileStream fsOut = new FileStream(packageFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
wResStream.CopyTo(fsOut);
}
}
} }
}; else
downloader.DownloadFileTaskAsync(new Uri(packageUrlPath), packageFilePath).Wait(); {
Status.SetTaskException(new WebException(string.Format("Server responded with: [{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription)));
return;
}
}
} }
this.Status.UpdateStatus(10, "Opening Plugin Package", Path.GetFileName(packageFilePath)); this.Status.UpdateStatus(10, "Opening Plugin Package", Path.GetFileName(packageFilePath));
+23 -11
View File
@@ -149,7 +149,6 @@ namespace Disco.Services.Plugins
if (string.IsNullOrEmpty(packageTempFilePath)) if (string.IsNullOrEmpty(packageTempFilePath))
{ {
// Download Update // Download Update
Status.UpdateStatus(0, string.Format("Downloading Plugin Package: {0}", pluginName), "Connecting..."); Status.UpdateStatus(0, string.Format("Downloading Plugin Package: {0}", pluginName), "Connecting...");
packageTempFilePath = Path.Combine(pluginPackagesLocation, string.Format("{0}.discoPlugin", pluginId)); packageTempFilePath = Path.Combine(pluginPackagesLocation, string.Format("{0}.discoPlugin", pluginId));
@@ -160,19 +159,32 @@ namespace Disco.Services.Plugins
Directory.CreateDirectory(Path.GetDirectoryName(packageTempFilePath)); Directory.CreateDirectory(Path.GetDirectoryName(packageTempFilePath));
// Need to Download the Package // Need to Download the Package
WebClient downloader = new WebClient(); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(catalogueItem.LatestDownloadUrl);
DateTime progressExpires = DateTime.Now; webRequest.KeepAlive = false;
downloader.DownloadProgressChanged += (sender, e) =>
webRequest.ContentType = "application/xml";
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.UserAgent = string.Format("Disco/{0} (PluginLibrary)", Disco.Services.Plugins.CommunityInterop.PluginLibraryUpdateTask.CurrentDiscoVersion());
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{ {
Console.WriteLine(e.ProgressPercentage); if (webResponse.StatusCode == HttpStatusCode.OK)
if (progressExpires <= DateTime.Now)
{ {
Status.UpdateStatus(e.ProgressPercentage, string.Format("{0} of {1} KB downloaded", e.BytesReceived / 1024, e.TotalBytesToReceive / 1024)); Status.UpdateStatus(0, "Downloading...");
// Throttle Updates for SignalR using (var wResStream = webResponse.GetResponseStream())
progressExpires = DateTime.Now.AddMilliseconds(250); {
using (FileStream fsOut = new FileStream(packageTempFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
wResStream.CopyTo(fsOut);
}
}
} }
}; else
downloader.DownloadFileTaskAsync(new Uri(catalogueItem.LatestDownloadUrl), packageTempFilePath).Wait(); {
Status.SetTaskException(new WebException(string.Format("Server responded with: [{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription)));
return;
}
}
} }
Status.UpdateStatus(10, "Opening Plugin Package", Path.GetFileName(packageTempFilePath)); Status.UpdateStatus(10, "Opening Plugin Package", Path.GetFileName(packageTempFilePath));