Exemplo n.º 1
0
		private int DavLockBase_InternalProcessDavRequest(object sender, EventArgs e)
		{
			int _responseCode = (int)DavLockResponseCode.Ok;

			string[] _lockTokens = this.RequestLock.GetLockTokens();

			//Check to see if a lock refresh was requested
			if (base.HttpApplication.Request.Headers["If"] != null)
			{
				if (_lockTokens.Length == 1)
				{
					DavRefreshEventArgs _refreshEventArgs = new DavRefreshEventArgs(_lockTokens[0], this.RequestLock.LockTimeout);
					OnRefreshLockDavRequest(_refreshEventArgs);
				}

				base.HttpApplication.Response.AppendHeader("Timeout", "Second-" + this.ResponseLock.LockTimeout);
			}
			else
			{
				//New lock request
				StringBuilder _opaquelockTokens = new StringBuilder();
				foreach (string _lockToken in _lockTokens)
					_opaquelockTokens.Append("<opaquelocktoken:" + _lockToken + ">");

				base.HttpApplication.Response.AppendHeader("Lock-Token", _opaquelockTokens.ToString());
			}

			//Check to see if there were any process errors...
			Enum[] _errorResources = this.ProcessErrorResources;
			if (_errorResources.Length > 0)
			{
				//Append a response node
				XmlDocument _xmlDocument = new XmlDocument();
				XmlNode _responseNode = _xmlDocument.CreateNode(XmlNodeType.Element, _xmlDocument.GetPrefixOfNamespace("DAV:"), "response", "DAV:");

				//Add the HREF
				XmlElement _requestLockHrefElement = _xmlDocument.CreateElement("href", "DAV:");
				_requestLockHrefElement.InnerText = base.RelativeRequestPath;
				_responseNode.AppendChild(_requestLockHrefElement);


				//Add the propstat
				XmlElement _propstatElement = _xmlDocument.CreateElement("propstat", "DAV:");
				XmlElement _propElement = _xmlDocument.CreateElement("prop", "DAV:");
				XmlElement _lockDiscoveryElement = _xmlDocument.CreateElement("lockdiscovery", "DAV:");
				_propElement.AppendChild(_lockDiscoveryElement);
				_propstatElement.AppendChild(_propElement);

				XmlElement _statusElement = _xmlDocument.CreateElement("status", "DAV:");
				_statusElement.InnerText = InternalFunctions.GetEnumHttpResponse(DavLockResponseCode.FailedDependency);
				_propstatElement.AppendChild(_statusElement);

				_responseNode.AppendChild(_propstatElement);

				base.SetResponseXml(InternalFunctions.ProcessErrorRequest(this.ProcessErrors, _responseNode));
				_responseCode = (int)ServerResponseCode.MultiStatus;
			}
			else
			{
				//No issues
				using (Stream _responseStream = new MemoryStream())
				{
					XmlTextWriter _xmlWriter = new XmlTextWriter(_responseStream, new UTF8Encoding(false));

					_xmlWriter.Formatting = Formatting.Indented;
					_xmlWriter.IndentChar = '\t';
					_xmlWriter.Indentation = 1;
					_xmlWriter.WriteStartDocument();

					//Open the prop element section
					_xmlWriter.WriteStartElement("D", "prop", "DAV:");
					_xmlWriter.WriteStartElement("lockdiscovery", "DAV:");
					this.ResponseLock.ActiveLock.WriteTo(_xmlWriter);
					_xmlWriter.WriteEndElement();
					_xmlWriter.WriteEndElement();

					_xmlWriter.WriteEndDocument();
					_xmlWriter.Flush();

					base.SetResponseXml(_responseStream);
					_xmlWriter.Close();
				}
			}

			return _responseCode;
		}
Exemplo n.º 2
0
        /// <summary>
        /// Create a repsonse for a PUT request - uploading a file
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        private Byte[] CreatePutResponse(HTTPHeader header, Byte[] body, params string[] properties)
        {
            #region Get Owner

            XmlDocument OwnerXmlDocument = new XmlDocument();
            OwnerXmlDocument.LoadXml(Encoding.UTF8.GetString(body));

            String NamespacePrefix = OwnerXmlDocument.GetPrefixOfNamespace(S_DAV_NAMESPACE_URI);
            if (NamespacePrefix == "") NamespacePrefix = S_DAV_PREFIX;

            XmlNamespaceManager XmlNamespaceManager = new XmlNamespaceManager(OwnerXmlDocument.NameTable);
            XmlNamespaceManager.AddNamespace(NamespacePrefix, S_DAV_NAMESPACE_URI);

            XmlElement OwnerRoot = OwnerXmlDocument.DocumentElement;
            XmlNode OwnerNode = OwnerRoot.SelectSingleNode(String.Concat("/", S_DAV_PREFIX, ":lockinfo/", S_DAV_PREFIX, ":owner/", S_DAV_PREFIX, ":href"), XmlNamespaceManager);

            String Owner = OwnerNode.InnerText;

            #endregion

            #region Create XmlDocument

            XmlDocument XmlDocument = new XmlDocument();

            XmlElement Root = XmlDocument.CreateElement(S_DAV_PREFIX, "prop", S_DAV_NAMESPACE_URI);
            XmlElement LockDiscovery = XmlDocument.CreateElement(S_DAV_PREFIX, "lockdiscovery", S_DAV_NAMESPACE_URI);
            XmlElement ActiveLock = XmlDocument.CreateElement(S_DAV_PREFIX, "activelock", S_DAV_NAMESPACE_URI);
            XmlElement LockType = XmlDocument.CreateElement(S_DAV_PREFIX, "locktype", S_DAV_NAMESPACE_URI);
            LockType.AppendChild(XmlDocument.CreateElement(S_DAV_PREFIX, "write", S_DAV_NAMESPACE_URI));
            XmlElement LockScope = XmlDocument.CreateElement(S_DAV_PREFIX, "lockscope", S_DAV_NAMESPACE_URI);
            LockType.AppendChild(XmlDocument.CreateElement(S_DAV_PREFIX, "exclusive", S_DAV_NAMESPACE_URI));

            Dictionary<String, String> MoreProps = new Dictionary<String, String>();
            MoreProps.Add("depth", "0");
            MoreProps.Add("owner", Owner);
            MoreProps.Add("timeout", "Second-3600");
            XmlElement ElemMoreActivelockProps = CreatePropstatElement(header, XmlDocument, MoreProps, header.Destination);

            #region LockToken

            XmlElement LockToken = XmlDocument.CreateElement(S_DAV_PREFIX, "locktoken", S_DAV_NAMESPACE_URI);
            XmlElement LockTokenHRef = XmlDocument.CreateElement(S_DAV_PREFIX, "href", S_DAV_NAMESPACE_URI);
            //"opaquelocktoken:{A2E9F1BD-47DB-487E-AA85-8A10ACFA391D}20090323T091747Z";
            LockTokenHRef.InnerText = "opaquelocktoken:{" + Guid.NewGuid().ToString() + "}" + TimestampNonce.AsString(S_DATETIME_FORMAT); //20090323T091747Z";
            LockToken.AppendChild(LockTokenHRef);

            #endregion

            ActiveLock.AppendChild(LockType);
            ActiveLock.AppendChild(LockScope);
            ActiveLock.AppendChild(ElemMoreActivelockProps);
            ActiveLock.AppendChild(LockToken);

            LockDiscovery.AppendChild(ActiveLock);
            Root.AppendChild(LockDiscovery);

            XmlDocument.AppendChild(Root);

            #endregion

            #region Stream XmlDocument to ByteArray

            XmlWriterSettings settings;
            settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;

            using (MemoryStream stream = new MemoryStream())
            {

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {

                    XmlDocument.WriteContentTo(writer);

                    writer.Flush();

                    return stream.ToArray();

                }

            }

            #endregion
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses a webDAV request body for some properties and return them
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        private PropfindProperties ParsePropfindBody(Byte[] body)
        {
            PropfindProperties FoundPropfindProperties = PropfindProperties.NONE;
            String BodyString = Encoding.UTF8.GetString(body);

            try
            {
                XmlDocument XmlDocument = new XmlDocument();
                XmlDocument.LoadXml(BodyString);

                String NamespacePrefix = XmlDocument.GetPrefixOfNamespace(S_DAV_NAMESPACE_URI);
                if (NamespacePrefix == "") NamespacePrefix = S_DAV_PREFIX;

                XmlNamespaceManager XmlNamespaceManager = new XmlNamespaceManager(XmlDocument.NameTable);
                XmlNamespaceManager.AddNamespace(NamespacePrefix, S_DAV_NAMESPACE_URI);

                if (XmlDocument.SelectSingleNode(String.Concat("/", NamespacePrefix, ":propfind/", NamespacePrefix, ":allprop"), XmlNamespaceManager) != null)
                    return PropfindProperties.AllProp;

                XmlNode Node = XmlDocument.SelectSingleNode(String.Concat("/",NamespacePrefix,":propfind/",NamespacePrefix,":prop"), XmlNamespaceManager);

                if (Node != null)
                {
                    foreach (XmlNode ChildNodes in Node.ChildNodes)
                    {
                        String Prop = ChildNodes.LocalName;
                        switch (Prop.ToLower())
                        {
                            case "creationdate":
                                FoundPropfindProperties |= PropfindProperties.Creationdate;
                                break;
                            case "displayname":
                                FoundPropfindProperties |= PropfindProperties.Displayname;
                                break;
                            case "getcontentlanguage":
                                FoundPropfindProperties |= PropfindProperties.Getcontentlanguage;
                                break;
                            case "getcontentlength":
                                FoundPropfindProperties |= PropfindProperties.Getcontentlength;
                                break;
                            case "getcontenttype":
                                FoundPropfindProperties |= PropfindProperties.Getcontenttype;
                                break;
                            case "getetag":
                                FoundPropfindProperties |= PropfindProperties.Getetag;
                                break;
                            case "getlastmodified":
                                FoundPropfindProperties |= PropfindProperties.Getlastmodified;
                                break;
                            case "lockdiscovery":
                                FoundPropfindProperties |= PropfindProperties.Lockdiscovery;
                                break;
                            case "resourcetype":
                                FoundPropfindProperties |= PropfindProperties.Resourcetype;
                                break;
                            case "supportedlock":
                                FoundPropfindProperties |= PropfindProperties.Supportedlock;
                                break;

                            #region SVN

                            case "version-controlled-configuration":
                                FoundPropfindProperties |= PropfindProperties.VersionControlledConfiguration;
                                break;
                            case "baseline-relative-path":
                                FoundPropfindProperties |= PropfindProperties.BaselineRelativePath;
                                break;
                            case "repository-uuid":
                                FoundPropfindProperties |= PropfindProperties.RepositoryUuid;
                                break;
                            case "checked-in":
                                FoundPropfindProperties |= PropfindProperties.CheckedIn;
                                break;
                            case "baseline-collection":
                                FoundPropfindProperties |= PropfindProperties.BaselineCollection;
                                break;
                            case "version-Name":
                                FoundPropfindProperties |= PropfindProperties.VersionName;
                                break;
                            case "creator-displayname":
                                FoundPropfindProperties |= PropfindProperties.CreatorDisplayname;
                                break;
                            case "deadprop-count":
                                FoundPropfindProperties |= PropfindProperties.DeadpropCount;
                                break;

                            #endregion

                            default:
                                //FoundPropfindProperties |= PropfindProperties.NONE;
                                break;
                        }

                    }
                }
            }
            catch
            {
                return PropfindProperties.NONE;
            }

            return FoundPropfindProperties;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a repsonse for a LOCK request - introducing a file copy (get)
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        private Byte[] CreateLockResponse(HTTPHeader header, Byte[] body, WebDAVDepth depth, params string[] properties)
        {
            #region Get Owner

            XmlDocument OwnerXmlDocument = new XmlDocument();
            OwnerXmlDocument.LoadXml(Encoding.UTF8.GetString(body));

            String NamespacePrefix = OwnerXmlDocument.GetPrefixOfNamespace(S_DAV_NAMESPACE_URI);
            if (NamespacePrefix == "") NamespacePrefix = S_DAV_PREFIX;

            XmlNamespaceManager XmlNamespaceManager = new XmlNamespaceManager(OwnerXmlDocument.NameTable);
            XmlNamespaceManager.AddNamespace(NamespacePrefix, S_DAV_NAMESPACE_URI);

            XmlElement OwnerRoot = OwnerXmlDocument.DocumentElement;
            XmlNode OwnerNode = OwnerRoot.SelectSingleNode(String.Concat("/", S_DAV_PREFIX, ":lockinfo/", S_DAV_PREFIX, ":owner/", S_DAV_PREFIX, ":href"), XmlNamespaceManager);

            String Owner = OwnerNode.InnerText;

            #endregion

            String LockTokenString = String.Concat("{" + Guid.NewGuid().ToString() + "}");
            TimeSpan LockLifetime;
            try
            {
                LockLifetime = TimeSpan.FromSeconds(Double.Parse(header.Headers["Timeout"].Substring(header.Headers["Timeout"].IndexOf('-') + 1)));
            }
            catch
            {
                LockLifetime = TimeSpan.FromSeconds(60 * 60);
            }
            RessourceLock.LockRessource(LockTokenString, header.Destination, LockLifetime);

            #region Create XmlDocument

            XmlDocument XmlDocument = new XmlDocument();

            XmlElement Root = XmlDocument.CreateElement(S_DAV_PREFIX, "prop", S_DAV_NAMESPACE_URI);
            AddPropfindLockdiscoveryElements(Root, header.Destination, header.FullHTTPDestinationPath());

               XmlDocument.AppendChild(Root);

            #endregion

            #region Stream XmlDocument to ByteArray

            XmlWriterSettings settings;
            settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;

            using (MemoryStream stream = new MemoryStream())
            {

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {

                    XmlDocument.WriteContentTo(writer);

                    writer.Flush();

                    return CleanContent(stream.ToArray());

                }

            }

            #endregion
        }