コード例 #1
0
        /// <summary>
        /// Parses the DAV result.
        /// Note that it will only parse if the server status was 207.
        /// There maybe error outputs on 4xx and 5xx but this will not parsed
        /// by this class.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="uri"></param>
        public DAVRequestResult(WebDAV request, HttpWebResponse response, Uri uri)
        {
            Request = request;
            Items = new List<Item>();
            Status = (ServerStatus)Enum.Parse(typeof(ServerStatus), response.StatusCode.ToString(), false);
            StatusText = response.StatusDescription;
            IsMultiState = Status == ServerStatus.MultiStatus;
            _stream = new MemoryStream();
            response.GetResponseStream().CopyTo(_stream);

            // dispose
            response.Dispose();

            _stream.Seek(0, SeekOrigin.Begin);
            if (_stream.Length == 0) return;

            // A kingdom for normal DOMDocument support.
            // Why not XDocument? XmlReader is faster and less resource hungry.
            // A huge multitstatus would be first loaded to memory completely.
            //
            // This reader can only go forward. Read-* methods will cause
            // to jump over elements. Hence we stop at the element and
            // store the element name. Then wait for Text-Elements value
            // to capture.
            using(XmlReader reader = XmlReader.Create(_stream, null)) {

                Item item = new Item();
                var waitForResourceType = false;
                var lastElementName = "";
                var waitForLockScope = false;
                var waitForLockType = false;
                List<DAVLocking> lockingList = null;
                List<PropertyState> propertyStateList = null;
                PropertyState pItem = null;
                DAVLocking litem = null;

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        // look for special elements
                        case XmlNodeType.Element:
                            if (reader.NamespaceURI == XmlNamespaces.NsDav)
                            {
                                switch (reader.LocalName)
                                {
                                    // DAV Elements

                                    // Response
                                    case Elements.Response:
                                        // start a new item
                                        // pItem must be set before d:prop in order to
                                        // catch non-real properties such "href"
                                        item = new Item();
                                        propertyStateList = new List<PropertyState>();
                                        pItem = new PropertyState();
                                        break;

                                    // Resource type
                                    case Elements.Collection:
                                        if (waitForResourceType)
                                        {
                                            item.ResourceType = ResourceType.Collection;
                                        }
                                        break;

                                    // Lock
                                    case Elements.LockEntry:
                                        litem = new DAVLocking();
                                        lockingList.Add(litem);
                                        break;
                                    case Elements.LockScope:
                                        waitForLockScope = true;
                                        break;
                                    case Elements.LockType:
                                        waitForLockType = true;
                                        break;
                                    case Elements.ExclusiveLocking:
                                        if (waitForLockScope)
                                        {
                                            litem.Scope = DAVLocking.LockScope.Exclusive;
                                        }
                                        break;
                                    case Elements.SharedLocking:
                                        if (waitForLockScope)
                                        {
                                            litem.Scope = DAVLocking.LockScope.Shared;
                                        }
                                        break;
                                    case Elements.WriteLocking:
                                        if (waitForLockType)
                                        {
                                            litem.Type = DAVLocking.LockType.Write;
                                        }
                                        break;
                                    case Elements.LockDiscovery:
                                        ///TODO 
                                        break;

                                    // DAV Properties
                                    case Elements.Properties:
                                        // a pItem was already created before
                                        break;

                                    case Properties.ResourceType:
                                        waitForResourceType = true;
                                        break;

                                    case Properties.SupportedLock:
                                        lockingList = new List<DAVLocking>();
                                        break;
                                    
                                    default:
                                        lastElementName = reader.LocalName;
                                        break;
                                }
                            }
                            break;
                        
                        // clean up
                        case XmlNodeType.EndElement:
                            if (reader.NamespaceURI == XmlNamespaces.NsDav)
                            {
                                switch (reader.LocalName)
                                {
                                    // DAV Elements
                                    case Elements.PropertyState:
                                        // save to list and create a new one (which stays maybe temporary)
                                        propertyStateList.Add(pItem);
                                        pItem = new PropertyState();
                                        break;

                                    case Elements.Response:
                                        // clean the list
                                        // the HTTP Status is important
                                        foreach (PropertyState state in propertyStateList)
                                        {
                                            if (state.Status == ServerStatus.OK)
                                            {
                                                item.Properties = state.Properties;
                                            }
                                            else
                                            {
                                                item.FailedProperties.Add(state);
                                            }
                                        }

                                        // Close the item
                                        Items.Add(item);
                                        item = null;

                                        // Reset the property state list
                                        propertyStateList = null;
                                        pItem = null;
                                        break;

                                    // Locking
                                    case Elements.LockType:
                                        waitForLockType = false;
                                        break;
                                    case Elements.LockScope:
                                        waitForLockScope = false;
                                        break;

                                    // DAV Properties
                                    case Properties.ResourceType:
                                        waitForResourceType = false;
                                        break;
                                    case Properties.SupportedLock:
                                        item.Locking = lockingList;
                                        break;

                                }
                            }
                            break;

                        // Grap the text values
                        case XmlNodeType.Text:

                            // no whitespace please
                            if (reader.Value == null) continue;

                            // can't set in empty element
                            if (item == null) continue;

                            switch (lastElementName)
                            {
                                // DAV Elements
                                case Elements.Reference:
                                    string _ref = Uri.UnescapeDataString(reader.Value);
                                    pItem.Properties.Add(lastElementName, _ref);
                                    pItem.Properties.Add(lastElementName + ".local", _ref.Trim('/').Split('/').Last());
                                    break;

                                // Status element
                                case Elements.Status:
                                    List<string> s = new List<string>(reader.Value.Split(' '));
                                    s.RemoveAt(0);
                                    pItem.Status = (ServerStatus)Enum.Parse(typeof(ServerStatus), s[0], false);
                                    s.RemoveAt(0);
                                    pItem.ServerStatusText = String.Join(" ", s.ToArray());
                                    break;

                                // DAV Properties
                                case Properties.QuotaUsedBytes:
                                case Properties.QuotaAvailableBytes:
                                case Properties.GetContentLength:
                                    pItem.Properties.Add(lastElementName, long.Parse(reader.Value));
                                    break;
                                case Properties.DisplayName:
                                case Properties.GetContentLanguage:
                                case Properties.GetContentType:
                                case Properties.GetETag:
                                    pItem.Properties.Add(lastElementName, reader.Value);
                                    break;
                                case Properties.GetLastModified:
                                case Properties.CreationDate:
                                    pItem.Properties.Add(lastElementName, DateTime.Parse(reader.Value));
                                    break;
                            }
                            lastElementName = "";
                            break;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Parses the DAV result.
        /// Note that it will only parse if the server status was 207.
        /// There maybe error outputs on 4xx and 5xx but this will not parsed
        /// by this class.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="uri"></param>
        public DAVRequestResult(WebDAV request, HttpWebResponse response, Uri uri)
        {
            Request      = request;
            Items        = new List <Item>();
            Status       = (ServerStatus)Enum.Parse(typeof(ServerStatus), response.StatusCode.ToString(), false);
            IsMultiState = Status == ServerStatus.MultiStatus;
            _stream      = new MemoryStream();
            response.GetResponseStream().CopyTo(_stream);

            // dispose
            response.Dispose();

            _stream.Seek(0, SeekOrigin.Begin);
            if (_stream.Length == 0)
            {
                return;
            }

            // A kingdom for normal DOMDocument support.
            // Why not XDocument? XmlReader is faster and less resource hungry.
            // A huge multitstatus would be first loaded to memory completely.
            //
            // This reader can only go forward. Read-* methods will cause
            // to jump over elements. Hence we stop at the element and
            // store the element name. Then wait for Text-Elements value
            // to capture.
            using (XmlReader reader = XmlReader.Create(_stream, null)) {
                Item item = new Item();
                var  waitForResourceType               = false;
                var  lastElementName                   = "";
                var  waitForLockScope                  = false;
                var  waitForLockType                   = false;
                List <DAVLocking>    lockingList       = null;
                List <PropertyState> propertyStateList = null;
                PropertyState        pItem             = null;
                DAVLocking           litem             = null;

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    // look for special elements
                    case XmlNodeType.Element:
                        if (reader.NamespaceURI == XmlNamespaces.NsDav)
                        {
                            switch (reader.LocalName)
                            {
                            // DAV Elements

                            // Response
                            case Elements.Response:
                                // start a new item
                                // pItem must be set before d:prop in order to
                                // catch non-real properties such "href"
                                item = new Item();
                                propertyStateList = new List <PropertyState>();
                                pItem             = new PropertyState();
                                break;

                            // Resource type
                            case Elements.Collection:
                                if (waitForResourceType)
                                {
                                    item.ResourceType = ResourceType.Collection;
                                }
                                break;

                            // Lock
                            case Elements.LockEntry:
                                litem = new DAVLocking();
                                lockingList.Add(litem);
                                break;

                            case Elements.LockScope:
                                waitForLockScope = true;
                                break;

                            case Elements.LockType:
                                waitForLockType = true;
                                break;

                            case Elements.ExclusiveLocking:
                                if (waitForLockScope)
                                {
                                    litem.Scope = DAVLocking.LockScope.Exclusive;
                                }
                                break;

                            case Elements.SharedLocking:
                                if (waitForLockScope)
                                {
                                    litem.Scope = DAVLocking.LockScope.Shared;
                                }
                                break;

                            case Elements.WriteLocking:
                                if (waitForLockType)
                                {
                                    litem.Type = DAVLocking.LockType.Write;
                                }
                                break;

                            case Elements.LockDiscovery:
                                ///TODO
                                break;

                            // DAV Properties
                            case Elements.Properties:
                                // a pItem was already created before
                                break;

                            case Properties.ResourceType:
                                waitForResourceType = true;
                                break;

                            case Properties.SupportedLock:
                                lockingList = new List <DAVLocking>();
                                break;

                            default:
                                lastElementName = reader.LocalName;
                                break;
                            }
                        }
                        break;

                    // clean up
                    case XmlNodeType.EndElement:
                        if (reader.NamespaceURI == XmlNamespaces.NsDav)
                        {
                            switch (reader.LocalName)
                            {
                            // DAV Elements
                            case Elements.PropertyState:
                                // save to list and create a new one (which stays maybe temporary)
                                propertyStateList.Add(pItem);
                                pItem = new PropertyState();
                                break;

                            case Elements.Response:
                                // clean the list
                                // the HTTP Status is important
                                foreach (PropertyState state in propertyStateList)
                                {
                                    if (state.Status == ServerStatus.OK)
                                    {
                                        item.Properties = state.Properties;
                                    }
                                    else
                                    {
                                        item.FailedProperties.Add(state);
                                    }
                                }

                                // Close the item
                                Items.Add(item);
                                item = null;

                                // Reset the property state list
                                propertyStateList = null;
                                pItem             = null;
                                break;

                            // Locking
                            case Elements.LockType:
                                waitForLockType = false;
                                break;

                            case Elements.LockScope:
                                waitForLockScope = false;
                                break;

                            // DAV Properties
                            case Properties.ResourceType:
                                waitForResourceType = false;
                                break;

                            case Properties.SupportedLock:
                                item.Locking = lockingList;
                                break;
                            }
                        }
                        break;

                    // Grap the text values
                    case XmlNodeType.Text:

                        // no whitespace please
                        if (reader.Value == null)
                        {
                            continue;
                        }

                        // can't set in empty element
                        if (item == null)
                        {
                            continue;
                        }

                        switch (lastElementName)
                        {
                        // DAV Elements
                        case Elements.Reference:
                            string _ref      = Uri.UnescapeDataString(reader.Value);
                            string _localRef = _ref.Substring(uri.LocalPath.Length, _ref.Length - uri.LocalPath.Length);
                            pItem.Properties.Add(lastElementName, _ref);
                            pItem.Properties.Add(lastElementName + ".local", _localRef.Trim('/'));
                            break;

                        // Status element
                        case Elements.Status:
                            List <string> s = new List <string>(reader.Value.Split(' '));
                            s.RemoveAt(0);
                            pItem.Status = (ServerStatus)Enum.Parse(typeof(ServerStatus), s[0], false);
                            s.RemoveAt(0);
                            pItem.ServerStatusText = String.Join(" ", s.ToArray());
                            break;

                        // DAV Properties
                        case Properties.QuotaUsedBytes:
                        case Properties.QuotaAvailableBytes:
                        case Properties.GetContentLength:
                            pItem.Properties.Add(lastElementName, long.Parse(reader.Value));
                            break;

                        case Properties.DisplayName:
                        case Properties.GetContentLanguage:
                        case Properties.GetContentType:
                        case Properties.GetETag:
                            pItem.Properties.Add(lastElementName, reader.Value);
                            break;

                        case Properties.GetLastModified:
                        case Properties.CreationDate:
                            pItem.Properties.Add(lastElementName, DateTime.Parse(reader.Value));
                            break;
                        }
                        lastElementName = "";
                        break;
                    }
                }
            }
        }