X-Git-Url: http://git.sourceforge.jp/view?p=applistation%2FAppliStation.git;a=blobdiff_plain;f=na-get-lib%2FNaGet.Net%2FDownloader.cs;h=5a0a7216afd6abcf985bba068453353afbfd053d;hp=f4c3670874c385c3b939126101a766560ad0c3b8;hb=9e8830beb9bdcf182ca7e84c92a67533bbb1a76c;hpb=30c6c06dcc96976e0a7a8c7add4c6db5c9e5ce3f
diff --git a/na-get-lib/NaGet.Net/Downloader.cs b/na-get-lib/NaGet.Net/Downloader.cs
index f4c3670..5a0a721 100644
--- a/na-get-lib/NaGet.Net/Downloader.cs
+++ b/na-get-lib/NaGet.Net/Downloader.cs
@@ -1,9 +1,9 @@
using System;
using System.Net;
+using System.Net.Mime;
using System.IO;
-using System.Collections;
using System.Threading;
-using NaGet.SubCommands;
+using NaGet.Tasks;
namespace NaGet.Net
{
@@ -11,12 +11,12 @@ namespace NaGet.Net
///
/// ãã¦ã³ãã¼ãã¤ãã³ããªãã¸ã§ã¯ã
///
-public class DownloadEventArgs : NaGetEventArgs
+public class DownloadEventArgs : TaskEventArgs
{
///
/// ã¤ãã³ãã®ç¨®é¡
///
- public DownloadEventType Type;
+ public DownloadEventType DownloadTaskType;
///
/// ãã¦ã³ãã¼ãæ¸ã¿ã®ãã¤ãæ°
@@ -37,12 +37,12 @@ public class DownloadEventArgs : NaGetEventArgs
/// ãã¦ã³ãã¼ãããç·ãã¤ãæ°
public DownloadEventArgs(DownloadEventType type, string msg, long pos, long max)
{
- Type = type;
+ DownloadTaskType = type;
DownloadSize = pos;
MaxSize = max;
TaskMessage = msg;
- TaskProgressPercent = (max > 0)? (100.0f * pos / max) : -1;
+ ProgressPercent = (max > 0)? (100.0f * pos / max) : -1;
}
}
@@ -61,7 +61,7 @@ public enum DownloadEventType {
///
/// ãã¦ã³ãã¼ãå¦çãè¡ãã¯ã©ã¹
///
-public class Downloader : NaGetTask
+public class Downloader : Task
{
public IWebProxy proxy;
@@ -73,7 +73,7 @@ public class Downloader : NaGetTask
///
/// ã¢ã¯ã»ã¹URL
///
- protected string url;
+ protected Uri url;
///
/// ä¿åå
@@ -129,7 +129,7 @@ public class Downloader : NaGetTask
/// ä¿åå
ãã¡ã¤ã«ãã¹
public void Download(string url, string filepath)
{
- this.url = url;
+ this.url = new Uri(url);
this.filepath = filepath;
try {
@@ -165,22 +165,28 @@ public class Downloader : NaGetTask
request.Proxy = this.Proxy;
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(CacheLevel);
+ HttpWebRequest httpRequest = request as HttpWebRequest;
+ if (httpRequest != null) {
+ httpRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
+ httpRequest.UserAgent = "AppliStation/1.3";
+ }
+
if (cancelCalled) {
- throw new NaGetTaskCanceledException(string.Empty);
+ throw new TaskCanceledException(string.Empty);
}
try {
response = request.GetResponse();
} catch (WebException e) {
if (cancelCalled) { // ãã£ã³ã»ã«æ
- throw new NaGetTaskCanceledException(string.Empty, e);
+ throw new TaskCanceledException(string.Empty, e);
} else {
throw new WebException(e.Message, e);
}
}
if (cancelCalled) {
- throw new NaGetTaskCanceledException(string.Empty);
+ throw new TaskCanceledException(string.Empty);
}
try {
@@ -222,7 +228,7 @@ public class Downloader : NaGetTask
fs.Write(data, 0, size);
if (cancelCalled) {
- throw new NaGetTaskCanceledException(string.Empty);
+ throw new TaskCanceledException(string.Empty);
}
}
} finally {
@@ -234,7 +240,7 @@ public class Downloader : NaGetTask
RaiseDownloadEvent(DownloadEventType.COMPLETED, fs.Position, contentLength);
} catch (IOException ex) {
if (cancelCalled) {
- throw new NaGetTaskCanceledException(string.Empty);
+ throw new TaskCanceledException(string.Empty);
} else {
RaiseDownloadEvent(DownloadEventType.ERROR, 0, 0);
throw new IOException(ex.Message, ex);
@@ -249,10 +255,13 @@ public class Downloader : NaGetTask
// æ´æ°æ¥ãè£å®
if (File.Exists(filepath)) {
- if (response is HttpWebResponse) {
- File.SetLastWriteTime(filepath, ((HttpWebResponse) response).LastModified);
- } else if (response is FtpWebResponse) {
- File.SetLastWriteTime(filepath, ((FtpWebResponse) response).LastModified);
+ HttpWebResponse httpResponse = response as HttpWebResponse;
+ FtpWebResponse ftpResponse = response as FtpWebResponse;
+
+ if (httpResponse != null) {
+ File.SetLastWriteTime(filepath, httpResponse.LastModified);
+ } else if (ftpResponse != null) {
+ File.SetLastWriteTime(filepath, ftpResponse.LastModified);
}
}
} finally {
@@ -270,15 +279,15 @@ public class Downloader : NaGetTask
if (DownloadEventRaised != null) {
DownloadEventArgs e = new DownloadEventArgs(type, string.Empty, pos, max);
- switch (e.Type) {
+ switch (e.DownloadTaskType) {
case DownloadEventType.CONNECTED:
e.TaskMessage = "æ¥ç¶ãã¾ãã";
break;
case DownloadEventType.STARTED:
case DownloadEventType.DOWNLOADING:
case DownloadEventType.COMPLETED:
- if (e.TaskProgressPercent >= 0) {
- e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.TaskProgressPercent);
+ if (e.ProgressPercent >= 0) {
+ e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.ProgressPercent);
} else {
e.TaskMessage = string.Format("{0} bytes", e.DownloadSize);
}
@@ -286,7 +295,7 @@ public class Downloader : NaGetTask
if (stopwatch != null && stopwatch.IsRunning && stopwatch.ElapsedMilliseconds > 3000) {
long bpers = e.DownloadSize * 1000 / stopwatch.ElapsedMilliseconds;
- if ((e.TaskProgressPercent >= 0) && (bpers > 0)) {
+ if ((e.ProgressPercent >= 0) && (bpers > 0)) {
TimeSpan rest = TimeSpan.FromSeconds((max - e.DownloadSize) / bpers);
e.TaskMessage += string.Format(" æ¨å®æ®ãæé:{0} ({1}/s)", rest, NaGet.Utils.FormatSize(bpers));
} else {
@@ -331,27 +340,27 @@ public class Downloader : NaGetTask
///
/// Webã¬ã¹ãã³ã¹ãããã¦ã³ãã¼ããããã¡ã¤ã«ã®ååãåå¾
///
- ///
- ///
- private string getFileNameFromWebResponse(WebResponse response)
+ /// Content-Dispositionãããããåå¾ãããã¯URLã®æ«å°¾ããæ¨å®ãã¾ã
+ /// ã¬ã¹ãã³ã¹ãªãã¸ã§ã¯ã
+ /// åå¾ãããã¡ã¤ã«å
+ private static string getFileNameFromWebResponse(WebResponse response)
{
- if (response is HttpWebResponse) {
- string contentDisposition = ((HttpWebResponse) response).Headers["Content-Disposition"];
+ HttpWebResponse httpresp = response as HttpWebResponse;
+ if (httpresp != null) {
+ string contentDisposition = httpresp.Headers["Content-Disposition"];
- // TODO check license for http://www.atmarkit.co.jp/fdotnet/dotnettips/618downnoname/downnoname.html
if (! string.IsNullOrEmpty(contentDisposition)) {
- System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(
- @"filename\s*=\s*(?:""(?[^""]*)""|(?[^;]*))",
- System.Text.RegularExpressions.RegexOptions.IgnoreCase);
-
- System.Text.RegularExpressions.Match m = re.Match(contentDisposition);
- if (m.Success) {
- return m.Groups["filename"].Value;
+ try {
+ ContentDisposition parser = new ContentDisposition(contentDisposition);
+ if (! string.IsNullOrEmpty(parser.FileName)) {
+ return parser.FileName;
+ }
+ } catch (FormatException) {
}
}
}
- return NaGet.Utils.Url2filename(response.ResponseUri.ToString());
+ return NaGet.Utils.Url2filename(response.ResponseUri);
}
}