Exemple #1
0
        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);
            }
        }