예제 #1
0
 /// <summary>
 /// Create the URI from the adapter.
 /// </summary>
 /// <param name="adapter">The current adapter.</param>
 /// <param name="resource">The trailing resource path.</param>
 /// <returns>The URI to connect to.</returns>
 private Uri CreateUri(WebdavConnectionAdapter adapter, string resource)
 {
     // If the standard ports are not used.
     if (adapter.Port != 80 && adapter.Port != 443)
     {
         // Append the port number.
         return(new Uri(adapter.WebdavServer.TrimEnd('/') + ":" + adapter.Port.ToString() + "/" + resource.TrimStart('/')));
     }
     else
     {
         // Create the standard URI.
         return(new Uri(adapter.WebdavServer.TrimEnd('/') + "/" + resource.TrimStart('/')));
     }
 }
예제 #2
0
        /// <summary>
        /// Get the directory list.
        /// </summary>
        /// <param name="adapter">The webdav connection adapter.</param>
        /// <param name="directoryPath">The directory path hierachy to search in.</param>
        /// <param name="directoryDepth">Recursion depth (directory depth). Set default depth to 1. This would prevent recursion.</param>
        /// <returns>The list of directories and files is any; else empty.</returns>
        public List <string> DirectoryList(WebdavConnectionAdapter adapter, string directoryPath = "/", int?directoryDepth = 1)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException("adapter");
            }
            if (String.IsNullOrEmpty(directoryPath))
            {
                throw new ArgumentNullException("directoryPath");
            }

            HttpWebRequest request  = null;
            WebResponse    response = null;

            // The read and write stream objects
            // for the transfer of data.
            Stream        requestStream  = null;
            Stream        responseStream = null;
            List <string> list           = new List <string>();

            try
            {
                // Create the request text.
                StringBuilder propfind = new StringBuilder();
                propfind.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                propfind.Append("<propfind xmlns=\"DAV:\">");
                propfind.Append("  <propname/>");
                propfind.Append("</propfind>");

                // Create the request bytes.
                byte[] bytesReq = Encoding.UTF8.GetBytes(propfind.ToString());
                Uri    uri      = CreateUri(adapter, Uri.EscapeDataString(directoryPath.TrimStart('/').TrimEnd('/')));

                // Create the request object.
                request = (HttpWebRequest)HttpWebRequest.Create(uri);

                // If a proxy server has been specified.
                if (adapter.Proxy != null)
                {
                    request.Proxy = adapter.Proxy;
                }

                // If a client certificate is to be used
                // add the client certificate.
                if (adapter.ClientCertificate != null)
                {
                    request.ClientCertificates.Add(adapter.ClientCertificate);
                }

                // If the web dav connection is not
                // anonymous then create a new
                // network credential.
                if (!adapter.IsAnonymousUser)
                {
                    request.Credentials = new NetworkCredential(
                        adapter.UserName, adapter.Password, adapter.Domain);
                }

                request.KeepAlive     = false;
                request.Timeout       = adapter.TimeOut;
                request.Method        = "PROPFIND";
                request.ContentLength = bytesReq.Length;
                request.ContentType   = "text/xml";

                // Create a list of headers to pass.
                IDictionary <string, string> headers = new Dictionary <string, string>();
                if (directoryDepth != null)
                {
                    headers.Add("Depth", directoryDepth.ToString());
                }

                // Need to send along headers?
                if (headers != null)
                {
                    foreach (string key in headers.Keys)
                    {
                        request.Headers.Set(key, headers[key]);
                    }
                }

                // Override the validation of
                // the server certificate.
                if (adapter.UseSSLConnection && adapter.SslCertificateOverride)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new
                                                                              RemoteCertificateValidationCallback(OnCertificateValidationOverride);
                }
                else if (adapter.UseSSLConnection)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new
                                                                              RemoteCertificateValidationCallback(OnCertificateValidation);
                }

                // Write the request data to the request stream.
                using (requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bytesReq, 0, bytesReq.Length);
                    requestStream.Flush();
                    requestStream.Close();
                }

                // Assign the response object from
                // the request server.
                response = (HttpWebResponse)request.GetResponse();

                // Create a new xml document.
                XmlDocument document = new XmlDocument();

                // Get all the data from the server.
                using (responseStream = response.GetResponseStream())
                {
                    // Load the xml response.
                    document.Load(responseStream);

                    // Close stream reader.
                    responseStream.Close();
                }

                // Get the DAV namesapce.
                XmlNamespaceManager xmlNsManager = new XmlNamespaceManager(document.NameTable);
                xmlNsManager.AddNamespace("d", "DAV:");

                // For each directory node.
                foreach (XmlNode node in document.DocumentElement.ChildNodes)
                {
                    // Get each directory and file.
                    XmlNode xmlNode = node.SelectSingleNode("d:href", xmlNsManager);
                    string  path    = Uri.UnescapeDataString(xmlNode.InnerXml).Trim('/');
                    string  dir     = path.Replace(Uri.UnescapeDataString(uri.AbsoluteUri), "").Trim('/');

                    // If a directory or file exits.
                    if (!String.IsNullOrEmpty(dir))
                    {
                        list.Add(dir);
                    }
                }

                // Close the response stream.
                response.Close();
                return(list);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // Close and clean up
                // all streams.
                if (requestStream != null)
                {
                    requestStream.Dispose();
                }

                if (responseStream != null)
                {
                    responseStream.Dispose();
                }

                if (response != null)
                {
                    _httpStatusCode        = ((HttpWebResponse)response).StatusCode;
                    _httpStatusDescription = ((HttpWebResponse)response).StatusDescription;
                    response.Dispose();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Execute a command on the web dav server.
        /// </summary>
        /// <param name="adapter">The webdav connection adapter.</param>
        /// <param name="method">The method to execute.</param>
        /// <param name="requestData">The data to send as a request to the server.</param>
        /// <param name="responseData">The data returned from the server.</param>
        /// <param name="contentType">The content type.</param>
        /// <param name="contentLengthRequest">The request data content length.</param>
        /// <param name="contentLengthResponse">The response data content length.</param>
        /// <param name="path">The path hierachy resources to execute.</param>
        /// <param name="headers">The list of headers to include in the request.</param>
        public void Execute(WebdavConnectionAdapter adapter, string method, System.IO.Stream requestData = null, System.IO.Stream responseData = null,
                            string contentType = "text/xml", long contentLengthRequest = 0, long contentLengthResponse = 0, string path = null, IDictionary <string, string> headers = null)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException("adapter");
            }
            if (String.IsNullOrEmpty(method))
            {
                throw new ArgumentNullException("method");
            }

            HttpWebRequest request  = null;
            WebResponse    response = null;

            // The read and write stream objects
            // for the transfer of data.
            Stream requestStream  = null;
            Stream responseStream = null;

            try
            {
                // Create the Uri
                Uri uri = CreateUri(adapter,
                                    (!String.IsNullOrEmpty(path) ?
                                     Uri.EscapeDataString(path.TrimStart('/').TrimEnd('/')) : ""));

                // Create the request object.
                request = (HttpWebRequest)HttpWebRequest.Create(uri);

                // If a proxy server has been specified.
                if (adapter.Proxy != null)
                {
                    request.Proxy = adapter.Proxy;
                }

                // If a client certificate is to be used
                // add the client certificate.
                if (adapter.ClientCertificate != null)
                {
                    request.ClientCertificates.Add(adapter.ClientCertificate);
                }

                // If the web dav connection is not
                // anonymous then create a new
                // network credential.
                if (!adapter.IsAnonymousUser)
                {
                    request.Credentials = new NetworkCredential(
                        adapter.UserName, adapter.Password, adapter.Domain);
                }

                request.KeepAlive     = false;
                request.Timeout       = adapter.TimeOut;
                request.Method        = method;
                request.ContentLength = contentLengthRequest;
                request.ContentType   = contentType;

                // Need to send along headers?
                if (headers != null)
                {
                    foreach (string key in headers.Keys)
                    {
                        request.Headers.Set(key, headers[key]);
                    }
                }

                // Override the validation of
                // the server certificate.
                if (adapter.UseSSLConnection && adapter.SslCertificateOverride)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new
                                                                              RemoteCertificateValidationCallback(OnCertificateValidationOverride);
                }
                else if (adapter.UseSSLConnection)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new
                                                                              RemoteCertificateValidationCallback(OnCertificateValidation);
                }

                // If request data stream exists.
                if (requestData != null)
                {
                    // Write the request data to the request stream.
                    using (requestStream = request.GetRequestStream())
                    {
                        // Read the data from the local stream.
                        Nequeo.IO.Stream.Operation.CopyStream(requestData, requestStream, contentLengthRequest, adapter.RequestTimeout);
                        requestStream.Close();
                    }
                }

                // Assign the response object from
                // the request server.
                response = (HttpWebResponse)request.GetResponse();

                // If response data stream exists.
                if (responseData != null)
                {
                    // Get all the data from the server.
                    using (responseStream = response.GetResponseStream())
                    {
                        // Write the data to the local stream.
                        Nequeo.IO.Stream.Operation.CopyStream(responseStream, responseData, contentLengthResponse, adapter.ResponseTimeout);

                        // Close stream reader.
                        responseStream.Close();
                    }
                }

                // Close the response stream.
                response.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // Close and clean up
                // all streams.
                if (requestStream != null)
                {
                    requestStream.Dispose();
                }

                if (responseStream != null)
                {
                    responseStream.Dispose();
                }

                if (response != null)
                {
                    _httpStatusCode        = ((HttpWebResponse)response).StatusCode;
                    _httpStatusDescription = ((HttpWebResponse)response).StatusDescription;
                    response.Dispose();
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Delete the resource.
        /// </summary>
        /// <param name="adapter">The webdav connection adapter.</param>
        /// <param name="path">The path hierachy resources to delete.</param>
        public void Delete(WebdavConnectionAdapter adapter, string path)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException("adapter");
            }
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            HttpWebRequest request  = null;
            WebResponse    response = null;

            try
            {
                // Create the Uri
                Uri uri = CreateUri(adapter, Uri.EscapeDataString(path.TrimStart('/').TrimEnd('/')));

                // Create the request object.
                request = (HttpWebRequest)HttpWebRequest.Create(uri);

                // If a proxy server has been specified.
                if (adapter.Proxy != null)
                {
                    request.Proxy = adapter.Proxy;
                }

                // If a client certificate is to be used
                // add the client certificate.
                if (adapter.ClientCertificate != null)
                {
                    request.ClientCertificates.Add(adapter.ClientCertificate);
                }

                // If the web dav connection is not
                // anonymous then create a new
                // network credential.
                if (!adapter.IsAnonymousUser)
                {
                    request.Credentials = new NetworkCredential(
                        adapter.UserName, adapter.Password, adapter.Domain);
                }

                request.KeepAlive = false;
                request.Timeout   = adapter.TimeOut;
                request.Method    = "DELETE";

                // Override the validation of
                // the server certificate.
                if (adapter.UseSSLConnection && adapter.SslCertificateOverride)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new
                                                                              RemoteCertificateValidationCallback(OnCertificateValidationOverride);
                }
                else if (adapter.UseSSLConnection)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new
                                                                              RemoteCertificateValidationCallback(OnCertificateValidation);
                }

                // Assign the response object from
                // the request server.
                response = (HttpWebResponse)request.GetResponse();

                // Close the response stream.
                response.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // Close and clean up
                // all streams.
                if (response != null)
                {
                    _httpStatusCode        = ((HttpWebResponse)response).StatusCode;
                    _httpStatusDescription = ((HttpWebResponse)response).StatusDescription;
                    response.Dispose();
                }
            }
        }