Exemplo n.º 1
0
 public static string Html(string path, Action<AdvancedWebClient> setup = null)
 {
     if (HttpContext.Current == null || !Paths.IsLocal(path)) {
         if (!path.Contains(':')) path = Paths.Url(path);
         var web = new AdvancedWebClient(false);
         if (setup != null) setup(web);
         return web.DownloadString(Paths.Url(path));
     } else {
         return Execute(path);
     }
 }
Exemplo n.º 2
0
        public static Stream Download(Uri url, Action<AdvancedWebClient> setup = null, string toFile = null)
        {
            if (toFile != null) Save(Download(url, setup, null), toFile);
            switch (url.Scheme) {
                case "http":
                case "https":
                    if (Paths.IsLocal(url)) {
                        var path = Uri.UnescapeDataString(url.AbsoluteUri.Replace(Paths.Home, "~"));
                        return OpenVirtual(path);
                    } else {
                        var web = new AdvancedWebClient(true);
                        if (setup != null) setup(web);
                        return web.OpenRead(url.ToString());
                    }
                case "ftp":
                case "ftps":
                    if (Providers.HasProvider(typeof(Ftp.FtpClient))) {

                        Ftp.FtpSecurityProtocol protocol;
                        if (url.Scheme == "ftp") protocol = Ftp.FtpSecurityProtocol.None;
                        else protocol = Ftp.FtpSecurityProtocol.Tls1OrSsl3Explicit;

                        var path = url.PathAndQuery;
                        if (path.Contains('?')) path = path.Substring(0, path.IndexOf('?'));

                        var user = url.UserInfo;
                        var password = "******";
                        if (user.IsNullOrEmpty()) {
                            user = "******";
                        } else if (user.Contains(":")) {
                            int p = user.IndexOf(':');
                            password = user.Substring(p+1);
                            user = user.Substring(0, p);
                        }

                        var stream = new PipeStream();
                        Tasks.Do(() => {
                            using (var ftp = new Ftp.FtpClient(url.Host, url.Port, protocol)) {
                                ftp.Open(user, password);
                                ftp.GetFile(path, stream, false);
                            }
                        });
                        return stream;
                    } else throw new NotSupportedException("There is no FTP Provider available.");
                default:
                    if (url.IsFile) return Read(url.LocalPath);
                    throw new NotSupportedException("Only http, https, ftp & ftps url's are supported.");
            }
        }