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); }
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; } }
private BaseFileEntry RequestResourceFromWebDavShare(IStorageProviderSession session, ICredentials creds, String resourceUrl, out List <BaseFileEntry> childs) { // build the dav service var svc = new DavService(); try { // create the web request var request = svc.CreateWebRequestPROPFIND(resourceUrl, creds); // the result WebDavRequestResult requestResult; // get the response using (var response = svc.GetWebResponse(request) as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.Moved) { // get the new uri var newUri = response.Headers["Location"]; (session.ServiceConfiguration as WebDavConfiguration).ServiceLocator = new Uri(newUri); // close the response response.Close(); // redo it return(RequestResourceFromWebDavShare(session, creds, newUri, out childs)); } // get the response stream using (var data = svc.GetResponseStream(response)) { if (data == null) { childs = null; return(null); } // build the file entries requestResult = WebDavRequestParser.CreateObjectsFromNetworkStream(data, resourceUrl, this, session, WebDavNameBaseFilterCallback); // close the stream data.Close(); } // close the response response.Close(); } // get the request fileentry and fill the childs if (requestResult.Self == null) { childs = null; return(null); } // set the childs childs = requestResult.Childs; // go ahead return(requestResult.Self); } 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"]; (session.ServiceConfiguration as WebDavConfiguration).ServiceLocator = new Uri(newUri); // redo it return(RequestResourceFromWebDavShare(session, creds, newUri, out childs)); } NetworkCredential networkCredential; if (code == HttpStatusCode.Unauthorized && (networkCredential = creds as NetworkCredential) != null) { // get authentication method var headers = e.Response.Headers["WWW-Authenticate"]; if (!string.IsNullOrEmpty(headers)) { var search = new Regex(@"^\w+", RegexOptions.Singleline | RegexOptions.IgnoreCase); var authMethod = search.Match(headers).Value; var newCredentials = new CredentialCache { { new Uri((new Uri(resourceUrl)).GetLeftPart(UriPartial.Authority)), authMethod, networkCredential } }; // redo it return(RequestResourceFromWebDavShare(session, newCredentials, resourceUrl, out childs)); } } } childs = null; return(null); } }
private BaseFileEntry RequestResourceFromWebDavShare(IStorageProviderSession session, String resourceUrl, out List <BaseFileEntry> childs) { // get the credebtials ICredentials creds = session.SessionToken as ICredentials; // build the dav service DavService svc = new DavService(); // the result WebDavRequestResult RequestResult; try { // create the web request WebRequest request = svc.CreateWebRequestPROPFIND(resourceUrl, creds.GetCredential(null, null)); // get the response using (HttpWebResponse response = svc.GetWebResponse(request) as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.Moved) { // get the new uri String newUri = response.Headers["Location"]; // close the response response.Close(); // redo it return(RequestResourceFromWebDavShare(session, newUri, out childs)); } else { // get the response stream using (Stream data = svc.GetResponseStream(response)) { if (data == null) { childs = null; return(null); } // build the file entries RequestResult = WebDavRequestParser.CreateObjectsFromNetworkStream(data, resourceUrl, this, session, WebDavNameBaseFilterCallback); // close the stream data.Close(); } // close the response response.Close(); } } // get the request fileentry and fill the childs if (RequestResult.Self == null) { childs = null; return(null); } // set the childs childs = RequestResult.Childs; // go ahead return(RequestResult.Self); } catch (WebException) { childs = null; return(null); } }