public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            // build the url
            string url = GetResourceUrl(session, fileSystemEntry, null);

            // get the session creds
            ICredentials creds = session.SessionToken as ICredentials;

            // Build the service
            DavService svc = new DavService();

            // create the webrequest
            WebRequest request = svc.CreateWebRequest(url, WebRequestMethodsEx.Http.Get, creds.GetCredential(null, null), false, null);

            // create the response
            WebResponse response = svc.GetWebResponse(request);

            // get the data
            Stream orgStream = svc.GetResponseStream(response);

            BaseFileEntryDownloadStream dStream = new BaseFileEntryDownloadStream(orgStream, fileSystemEntry);

            // put the disposable on the stack
            dStream._DisposableObjects.Push(response);

            // go ahead
            return(dStream);
        }
Example #2
0
        private WebResponse CreateDownloadResponse(string url, ICredentials creds)
        {
            // Build the service
            var svc = new DavService();

            // create the webrequest
            var request = svc.CreateWebRequest(url, WebRequestMethodsEx.Http.Get, creds, false, null);

            try
            {
                // create the response
                var response = svc.GetWebResponse(request);
                return(response);
            }
            catch (WebException e)
            {
                if (e.Response is HttpWebResponse)
                {
                    var code = (e.Response as HttpWebResponse).StatusCode;
                    if (code == HttpStatusCode.Moved)
                    {
                        // get the new uri
                        var newUri = e.Response.Headers["Location"];

                        // redo it
                        return(CreateDownloadResponse(newUri, creds));
                    }

                    NetworkCredential networkCredential;
                    if (code == HttpStatusCode.Unauthorized && (networkCredential = creds as NetworkCredential) != null)
                    {
                        var search = new Regex(@"^\w+", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                        // get authentication method
                        var authMethod     = search.Match(e.Response.Headers["WWW-Authenticate"]).Value;
                        var newCredentials = new CredentialCache {
                            { new Uri((new Uri(url)).GetLeftPart(UriPartial.Authority)), authMethod, networkCredential }
                        };

                        // redo it
                        return(CreateDownloadResponse(url, newCredentials));
                    }
                }

                throw;
            }
        }