OSDN Git Service

na-get-lib,タスクセットの改修(NaGetTaskSet2)および、そのUnitTestを追加
[applistation/AppliStation.git] / na-get-lib / NaGet.Net / Downloader.cs
index f4c3670..5a0a721 100644 (file)
@@ -1,9 +1,9 @@
 using System;\r
 using System.Net;\r
+using System.Net.Mime;\r
 using System.IO;\r
-using System.Collections;\r
 using System.Threading;\r
-using NaGet.SubCommands;\r
+using NaGet.Tasks;\r
 \r
 namespace NaGet.Net\r
 {\r
@@ -11,12 +11,12 @@ namespace NaGet.Net
 /// <summary>\r
 /// ダウンロードイベントオブジェクト\r
 /// </summary>\r
-public class DownloadEventArgs : NaGetEventArgs\r
+public class DownloadEventArgs : TaskEventArgs\r
 {\r
        /// <summary>\r
        /// イベントの種類\r
        /// </summary>\r
-       public DownloadEventType Type;\r
+       public DownloadEventType DownloadTaskType;\r
 \r
        /// <summary>\r
        /// ダウンロード済みのバイト数\r
@@ -37,12 +37,12 @@ public class DownloadEventArgs : NaGetEventArgs
        /// <param name="max">ダウンロードする総バイト数</param>\r
        public DownloadEventArgs(DownloadEventType type, string msg, long pos, long max)\r
        {\r
-               Type = type;\r
+               DownloadTaskType = type;\r
                DownloadSize = pos;\r
                MaxSize = max;\r
                \r
                TaskMessage = msg;\r
-               TaskProgressPercent = (max > 0)? (100.0f * pos / max) : -1;\r
+               ProgressPercent = (max > 0)? (100.0f * pos / max) : -1;\r
        }\r
 }\r
 \r
@@ -61,7 +61,7 @@ public enum DownloadEventType {
 /// <summary>\r
 /// ダウンロード処理を行うクラス\r
 /// </summary>\r
-public class Downloader : NaGetTask\r
+public class Downloader : Task\r
 {\r
        public IWebProxy proxy;\r
        \r
@@ -73,7 +73,7 @@ public class Downloader : NaGetTask
        /// <summary>\r
        /// アクセスURL\r
        /// </summary>\r
-       protected string url;\r
+       protected Uri url;\r
        \r
        /// <summary>\r
        /// 保存先\r
@@ -129,7 +129,7 @@ public class Downloader : NaGetTask
        /// <param name="filepath">保存先ファイルパス</param>\r
        public void Download(string url, string filepath)\r
        {\r
-               this.url = url;\r
+               this.url = new Uri(url);\r
                this.filepath = filepath;\r
                \r
                try {\r
@@ -165,22 +165,28 @@ public class Downloader : NaGetTask
                        request.Proxy = this.Proxy;\r
                        request.CachePolicy = new System.Net.Cache.RequestCachePolicy(CacheLevel);\r
                        \r
+                       HttpWebRequest httpRequest = request as HttpWebRequest;\r
+                       if (httpRequest != null) {\r
+                               httpRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;  \r
+                               httpRequest.UserAgent = "AppliStation/1.3";\r
+                       }\r
+                       \r
                        if (cancelCalled) {\r
-                               throw new NaGetTaskCanceledException(string.Empty);\r
+                               throw new TaskCanceledException(string.Empty);\r
                        }\r
                        \r
                        try {\r
                                response = request.GetResponse();\r
                        } catch (WebException e) {\r
                                if (cancelCalled) { // キャンセル時\r
-                                       throw new NaGetTaskCanceledException(string.Empty, e);\r
+                                       throw new TaskCanceledException(string.Empty, e);\r
                                } else {\r
                                        throw new WebException(e.Message, e);\r
                                }\r
                        }\r
                        \r
                        if (cancelCalled) {\r
-                               throw new NaGetTaskCanceledException(string.Empty);\r
+                               throw new TaskCanceledException(string.Empty);\r
                        }\r
                        \r
                        try {\r
@@ -222,7 +228,7 @@ public class Downloader : NaGetTask
                                                        fs.Write(data, 0, size);\r
                                                        \r
                                                        if (cancelCalled) {\r
-                                                               throw new NaGetTaskCanceledException(string.Empty);\r
+                                                               throw new TaskCanceledException(string.Empty);\r
                                                        }\r
                                                }\r
                                        } finally {\r
@@ -234,7 +240,7 @@ public class Downloader : NaGetTask
                                        RaiseDownloadEvent(DownloadEventType.COMPLETED, fs.Position, contentLength);\r
                                } catch (IOException ex) {\r
                                        if (cancelCalled) {\r
-                                               throw new NaGetTaskCanceledException(string.Empty);\r
+                                               throw new TaskCanceledException(string.Empty);\r
                                        } else {\r
                                                RaiseDownloadEvent(DownloadEventType.ERROR, 0, 0);\r
                                                throw new IOException(ex.Message, ex);\r
@@ -249,10 +255,13 @@ public class Downloader : NaGetTask
                        \r
                        // 更新日を補完\r
                        if (File.Exists(filepath)) {\r
-                               if (response is HttpWebResponse) {\r
-                                       File.SetLastWriteTime(filepath, ((HttpWebResponse) response).LastModified);\r
-                               } else if (response is FtpWebResponse) {\r
-                                       File.SetLastWriteTime(filepath, ((FtpWebResponse) response).LastModified);\r
+                               HttpWebResponse httpResponse = response as HttpWebResponse;\r
+                               FtpWebResponse  ftpResponse      = response as FtpWebResponse;\r
+                               \r
+                               if (httpResponse != null) {\r
+                                       File.SetLastWriteTime(filepath, httpResponse.LastModified);\r
+                               } else if (ftpResponse != null) {\r
+                                       File.SetLastWriteTime(filepath, ftpResponse.LastModified);\r
                                }\r
                        }\r
                } finally {\r
@@ -270,15 +279,15 @@ public class Downloader : NaGetTask
                if (DownloadEventRaised != null) {\r
                        DownloadEventArgs e = new DownloadEventArgs(type, string.Empty, pos, max);\r
                        \r
-                       switch (e.Type) {\r
+                       switch (e.DownloadTaskType) {\r
                                case DownloadEventType.CONNECTED:\r
                                        e.TaskMessage = "接続しました";\r
                                        break;\r
                                case DownloadEventType.STARTED:\r
                                case DownloadEventType.DOWNLOADING:\r
                                case DownloadEventType.COMPLETED:\r
-                                       if (e.TaskProgressPercent >= 0) {\r
-                                               e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.TaskProgressPercent);\r
+                                       if (e.ProgressPercent >= 0) {\r
+                                               e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.ProgressPercent);\r
                                        } else {\r
                                                e.TaskMessage = string.Format("{0} bytes", e.DownloadSize);\r
                                        }\r
@@ -286,7 +295,7 @@ public class Downloader : NaGetTask
                                        \r
                                        if (stopwatch != null && stopwatch.IsRunning && stopwatch.ElapsedMilliseconds > 3000) {\r
                                                long bpers = e.DownloadSize * 1000 / stopwatch.ElapsedMilliseconds;\r
-                                               if ((e.TaskProgressPercent >= 0) && (bpers > 0)) {\r
+                                               if ((e.ProgressPercent >= 0) && (bpers > 0)) {\r
                                                        TimeSpan rest = TimeSpan.FromSeconds((max - e.DownloadSize) / bpers);\r
                                                        e.TaskMessage += string.Format(" 推定残り時間:{0} ({1}/s)", rest, NaGet.Utils.FormatSize(bpers));\r
                                                } else {\r
@@ -331,27 +340,27 @@ public class Downloader : NaGetTask
        /// <summary>\r
        /// Webレスポンスからダウンロードしたファイルの名前を取得\r
        /// </summary>\r
-       /// <param name="response"></param>\r
-       /// <returns></returns>\r
-       private string getFileNameFromWebResponse(WebResponse response)\r
+       /// <remarks>Content-Dispositionヘッダから取得あるいはURLの末尾から推定します</remarks>\r
+       /// <param name="response">レスポンスオブジェクト</param>\r
+       /// <returns>取得したファイル名</returns>\r
+       private static string getFileNameFromWebResponse(WebResponse response)\r
        {\r
-               if (response is HttpWebResponse) {\r
-                       string contentDisposition = ((HttpWebResponse) response).Headers["Content-Disposition"];\r
+               HttpWebResponse httpresp = response as HttpWebResponse;\r
+               if (httpresp != null) {\r
+                       string contentDisposition = httpresp.Headers["Content-Disposition"];\r
                        \r
-                       // TODO check license for http://www.atmarkit.co.jp/fdotnet/dotnettips/618downnoname/downnoname.html\r
                        if (! string.IsNullOrEmpty(contentDisposition)) {\r
-                               System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(\r
-                                       @"filename\s*=\s*(?:""(?<filename>[^""]*)""|(?<filename>[^;]*))",\r
-                                       System.Text.RegularExpressions.RegexOptions.IgnoreCase);\r
-                               \r
-                               System.Text.RegularExpressions.Match m = re.Match(contentDisposition);\r
-                               if (m.Success) {\r
-                                       return m.Groups["filename"].Value;\r
+                               try {\r
+                                       ContentDisposition parser = new ContentDisposition(contentDisposition);\r
+                                       if (! string.IsNullOrEmpty(parser.FileName)) {\r
+                                               return parser.FileName;\r
+                                       }\r
+                               } catch (FormatException) {\r
                                }\r
                        }\r
                }\r
                \r
-               return NaGet.Utils.Url2filename(response.ResponseUri.ToString());\r
+               return NaGet.Utils.Url2filename(response.ResponseUri);\r
        }\r
 }\r
 \r