Пример #1
0
        private WebDAVResource GetResource(string[] pathcomps)
        {
            WebDAVResource res = m_RootFolder;

            foreach (string pathcomp in pathcomps)
            {
                res = res.GetResource(Uri.UnescapeDataString(pathcomp));
            }

            return(res);
        }
Пример #2
0
        private WebDAVResource GetResourceTo(string[] pathcomps, out string last)
        {
            WebDAVResource res = m_RootFolder;

            last = Uri.UnescapeDataString(pathcomps[pathcomps.Length - 1]);
            for (int i = 0; i < pathcomps.Length - 1; ++i)
            {
                res = res.GetResource(Uri.UnescapeDataString(pathcomps[i]));
            }

            return(res);
        }
Пример #3
0
        private XmlElement PropFindProcess(string rawurl, WebDAVResource resource, XmlDocument resDoc, XmlElement reqprop)
        {
            XmlElement responseElem = resDoc.CreateElement("DAV", "response");
            XmlElement hrefElem     = resDoc.CreateElement("DAV", "href");

            hrefElem.InnerText = rawurl;
            responseElem.AppendChild(hrefElem);
            XmlElement propStatElem = resDoc.CreateElement("DAV", "propstat");

            responseElem.AppendChild(propStatElem);

            foreach (XmlElement srcelem in reqprop.ChildNodes.OfType <XmlElement>())
            {
                XmlElement propElem = resDoc.CreateElement("DAV", "prop");
                propStatElem.AppendChild(propElem);
                try
                {
                    XmlElement propData = resource.GetProperty(srcelem.Prefix, srcelem.LocalName, resDoc);
                    propElem.AppendChild(propData);
                    XmlElement status = resDoc.CreateElement("DAV", "status");
                    status.InnerText = "HTTP/1.1 200 OK";
                    propStatElem.AppendChild(status);
                }
                catch (HttpException e)
                {
                    XmlElement propData = resDoc.CreateElement(srcelem.Prefix, srcelem.LocalName);
                    propElem.AppendChild(propData);
                    XmlElement status = resDoc.CreateElement("DAV", "status");
                    status.InnerText = $"HTTP/1.1 {e.GetHttpCode()} {e.Message}";
                    propStatElem.AppendChild(status);
                }
                catch (Exception)
                {
                    XmlElement propData = resDoc.CreateElement(srcelem.Prefix, srcelem.LocalName);
                    propElem.AppendChild(propData);
                    XmlElement status = resDoc.CreateElement("DAV", "status");
                    status.InnerText = "HTTP/1.1 500 Internal Server Error";
                    propStatElem.AppendChild(status);
                }
            }
            return(responseElem);
        }
Пример #4
0
        private void HandlePropPatch(WebDAVResource resource, HttpRequest req)
        {
            var doc = new XmlDocument
            {
                XmlResolver = null
            };

            using (Stream s = req.Body)
            {
                doc.Load(s);
            }

            if (doc.DocumentElement.LocalName != "propertyupdate")
            {
                req.ErrorResponse(HttpStatusCode.BadRequest);
                return;
            }

            var prop = (XmlElement)doc.DocumentElement.GetElementsByTagName("set")[0];

            prop = (XmlElement)prop.GetElementsByTagName("prop")[0];
            var resDoc = new XmlDocument
            {
                XmlResolver = null
            };
            XmlElement elem = resDoc.CreateElement("DAV", "multistatus");

            resDoc.AppendChild(elem);
            XmlElement responseElem = resDoc.CreateElement("DAV", "response");

            elem.AppendChild(responseElem);
            XmlElement hrefElem = resDoc.CreateElement("DAV", "href");

            hrefElem.InnerText = "";
            responseElem.AppendChild(hrefElem);
            XmlElement propStatElem = resDoc.CreateElement("DAV", "propstat");

            responseElem.AppendChild(propStatElem);

            foreach (XmlElement srcelem in prop.ChildNodes.OfType <XmlElement>())
            {
                XmlElement propElem = resDoc.CreateElement("DAV", "prop");
                propStatElem.AppendChild(propElem);
                try
                {
                    resource.SetProperty(srcelem.Prefix, srcelem.LocalName, srcelem);
                    XmlElement status = resDoc.CreateElement("DAV", "status");
                    status.InnerText = "HTTP/1.1 200 OK";
                    propStatElem.AppendChild(status);
                }
                catch (HttpException e)
                {
                    XmlElement propData = resDoc.CreateElement(srcelem.Prefix, srcelem.LocalName);
                    propElem.AppendChild(propData);
                    XmlElement status = resDoc.CreateElement("DAV", "status");
                    status.InnerText = $"HTTP/1.1 {e.GetHttpCode()} {e.Message}";
                    propStatElem.AppendChild(status);
                }
                catch (Exception)
                {
                    XmlElement propData = resDoc.CreateElement(srcelem.Prefix, srcelem.LocalName);
                    propElem.AppendChild(propData);
                    XmlElement status = resDoc.CreateElement("DAV", "status");
                    status.InnerText = "HTTP/1.1 500 Internal Server Error";
                    propStatElem.AppendChild(status);
                }
            }

            using (HttpResponse res = req.BeginResponse("application/xml"))
                using (Stream s = res.GetOutputStream())
                    using (XmlTextWriter writer = s.UTF8XmlTextWriter())
                    {
                        resDoc.WriteTo(writer);
                    }
        }
Пример #5
0
        private void HandlePropFind(WebDAVResource resource, HttpRequest req)
        {
            var doc = new XmlDocument
            {
                XmlResolver = null
            };

            using (Stream s = req.Body)
            {
                doc.Load(s);
            }

            if (doc.DocumentElement.Name != "propfind")
            {
                req.ErrorResponse(HttpStatusCode.BadRequest);
                return;
            }

            int    depth;
            string depthstr;

            if (req.TryGetHeader("depth", out depthstr))
            {
                depth = depthstr == "infinity" ? int.MaxValue : int.Parse(depthstr);
            }
            else
            {
                depth = 1;
            }

            var prop   = (XmlElement)doc.DocumentElement.GetElementsByTagName("prop")[0];
            var resDoc = new XmlDocument
            {
                XmlResolver = null
            };
            XmlElement elem = resDoc.CreateElement("DAV", "multistatus");

            resDoc.AppendChild(elem);
            elem.AppendChild(PropFindProcess(req.RawUrl, resource, resDoc, prop));

            if (depth > 0 && resource is WebDAVCollection)
            {
                string childUrl = req.RawUrl;
                if (!childUrl.EndsWith("/"))
                {
                    childUrl += "/";
                }

                foreach (WebDAVResource child in ((WebDAVCollection)resource).Children)
                {
                    elem.AppendChild(PropFindProcess(childUrl + Uri.EscapeDataString(child.ResourceName), resource, resDoc, prop));
                }
            }

            using (HttpResponse res = req.BeginResponse("application/xml"))
                using (Stream s = res.GetOutputStream())
                    using (XmlTextWriter writer = s.UTF8XmlTextWriter())
                    {
                        resDoc.WriteTo(writer);
                    }
        }