public static WebDavRequestResult CreateObjectsFromNetworkStream(Stream data, string targetUrl, IStorageProviderService service, IStorageProviderSession session, NameBaseFilterCallback callback) { var config = session.ServiceConfiguration as WebDavConfiguration; var results = new WebDavRequestResult(); var queryLessUri = HttpUtilityEx.GetPathAndQueryLessUri(config.ServiceLocator).ToString().TrimEnd('/'); var decodedTargetUrl = HttpUtility.UrlDecode(targetUrl); string s; using (var streamReader = new StreamReader(data)) { s = streamReader.ReadToEnd(); } //todo: var xDoc = XDocument.Load(new StringReader(s.Replace("d:d:", "d:"))); var responses = xDoc.Descendants(XName.Get("response", DavNamespace)); foreach (var response in responses) { var isHidden = false; var isDirectory = false; var lastModified = DateTime.Now; long contentLength = 0; var href = response.Element(XName.Get("href", DavNamespace)).ValueOrEmpty(); var propstats = response.Descendants(XName.Get("propstat", DavNamespace)); foreach (var propstat in propstats) { var prop = propstat.Element(XName.Get("prop", DavNamespace)); var status = propstat.Element(XName.Get("status", DavNamespace)).ValueOrEmpty(); if (!status.Equals(HttpOk) || prop == null) { continue; } var strLastModified = prop.Element(XName.Get("getlastmodified", DavNamespace)).ValueOrEmpty(); var strContentLength = prop.Element(XName.Get("getcontentlength", DavNamespace)).ValueOrEmpty(); var resourceType = prop.Element(XName.Get("resourcetype", DavNamespace)); var strIsHidden = prop.Element(XName.Get("ishidden", DavNamespace)).ValueOrEmpty(); if (!string.IsNullOrEmpty(strIsHidden)) { int code; if (!int.TryParse(strIsHidden, out code)) { code = 0; } isHidden = Convert.ToBoolean(code); } if (resourceType != null && resourceType.Element(XName.Get("collection", DavNamespace)) != null) { isDirectory = true; } if (!string.IsNullOrEmpty(strContentLength)) { contentLength = Convert.ToInt64(strContentLength); } if (!string.IsNullOrEmpty(strLastModified) && DateTime.TryParse(strLastModified, out lastModified)) { lastModified = lastModified.ToUniversalTime(); } } //entry not to be encluded if (isHidden) { continue; } var nameBase = href; if (callback != null) { nameBase = callback(targetUrl, service, session, nameBase); } string nameBaseForSelfCheck; if (nameBase.StartsWith(config.ServiceLocator.ToString())) { nameBaseForSelfCheck = HttpUtility.UrlDecode(nameBase); nameBase = nameBase.Remove(0, config.ServiceLocator.ToString().Length); } else { nameBaseForSelfCheck = queryLessUri + HttpUtilityEx.PathDecodeUTF8(nameBase); } nameBase = nameBase.TrimEnd('/'); nameBaseForSelfCheck = nameBaseForSelfCheck.TrimEnd('/'); if (targetUrl.EndsWith("/")) { nameBaseForSelfCheck += "/"; } var isSelf = nameBaseForSelfCheck.Equals(decodedTargetUrl); var ph = new PathHelper(nameBase); var resourceName = HttpUtility.UrlDecode(ph.GetFileName()); var entry = !isDirectory ? new BaseFileEntry(resourceName, contentLength, lastModified, service, session) : new BaseDirectoryEntry(resourceName, contentLength, lastModified, service, session); if (isSelf) { results.Self = entry; } else { results.Childs.Add(entry); } } return(results); }
public static WebDavRequestResult CreateObjectsFromNetworkStream(Stream data, String targetUrl, IStorageProviderService service, IStorageProviderSession session, NameBaseFilterCallback callback) { return(null); }
public static WebDavRequestResult CreateObjectsFromNetworkStream(Stream data, String targetUrl, IStorageProviderService service, IStorageProviderSession session, NameBaseFilterCallback callback) { var config = session.ServiceConfiguration as WebDavConfiguration; var results = new WebDavRequestResult(); var queryLessUri = HttpUtilityEx.GetPathAndQueryLessUri(config.ServiceLocator).ToString().TrimEnd('/'); var decodedTargetUrl = HttpUtility.UrlDecode(targetUrl); var s = new StreamReader(data).ReadToEnd(); //todo: var xDoc = XDocument.Load(new StringReader(s.Replace("d:d:", "d:"))); var responses = xDoc.Descendants(XName.Get("response", DavNamespace)); foreach (var response in responses) { bool isHidden = false; bool isDirectory = false; DateTime lastModified = DateTime.Now; long contentLength = 0; var href = response.Element(XName.Get("href", DavNamespace)).ValueOrEmpty(); var propstats = response.Descendants(XName.Get("propstat", DavNamespace)); foreach (var propstat in propstats) { var prop = propstat.Element(XName.Get("prop", DavNamespace)); var status = propstat.Element(XName.Get("status", DavNamespace)).ValueOrEmpty(); if (!status.Equals(HttpOk) || prop == null) continue; var strLastModified = prop.Element(XName.Get("getlastmodified", DavNamespace)).ValueOrEmpty(); var strContentLength = prop.Element(XName.Get("getcontentlength", DavNamespace)).ValueOrEmpty(); var resourceType = prop.Element(XName.Get("resourcetype", DavNamespace)); var strIsHidden = prop.Element(XName.Get("ishidden", DavNamespace)).ValueOrEmpty(); if (!String.IsNullOrEmpty(strIsHidden)) { int code; if (!int.TryParse(strIsHidden, out code)) code = 0; isHidden = Convert.ToBoolean(code); } if (resourceType.Element(XName.Get("collection", DavNamespace)) != null) isDirectory = true; if (!String.IsNullOrEmpty(strContentLength)) contentLength = Convert.ToInt64(strContentLength); if (!String.IsNullOrEmpty(strLastModified)) DateTime.TryParse(strLastModified, out lastModified); } //entry not to be encluded if (isHidden) continue; var nameBase = href; if (callback != null) nameBase = callback(targetUrl, service, session, nameBase); String nameBaseForSelfCheck; if (nameBase.StartsWith(config.ServiceLocator.ToString())) { nameBaseForSelfCheck = HttpUtility.UrlDecode(nameBase); nameBase = nameBase.Remove(0, config.ServiceLocator.ToString().Length); } else { nameBaseForSelfCheck = queryLessUri + HttpUtilityEx.PathDecodeUTF8(nameBase); } nameBase = nameBase.TrimEnd('/'); nameBaseForSelfCheck = nameBaseForSelfCheck.TrimEnd('/'); if (targetUrl.EndsWith("/")) nameBaseForSelfCheck += "/"; bool isSelf = nameBaseForSelfCheck.Equals(decodedTargetUrl); var ph = new PathHelper(nameBase); var resourceName = HttpUtility.UrlDecode(ph.GetFileName()); BaseFileEntry entry = !isDirectory ? new BaseFileEntry(resourceName, contentLength, lastModified, service, session) : new BaseDirectoryEntry(resourceName, contentLength, lastModified, service, session); if (isSelf) { results.Self = entry; } else { results.Childs.Add(entry); } } return results; }
public static WebDavRequestResult CreateObjectsFromNetworkStream(Stream data, String targetUrl, IStorageProviderService service, IStorageProviderSession session, NameBaseFilterCallback callback) { WebDavConfiguration config = session.ServiceConfiguration as WebDavConfiguration; WebDavRequestResult results = new WebDavRequestResult(); try { String resourceName = String.Empty; long resourceLength = 0; DateTime resourceModificationDate = DateTime.Now; DateTime resourceCreationDate = DateTime.Now; String resourceContentType = String.Empty; Boolean bIsHidden = false; Boolean bIsDirectory = false; Boolean bIsSelf = false; // build queryless uri String queryLessUri = HttpUtilityEx.GetPathAndQueryLessUri(config.ServiceLocator).ToString().TrimEnd('/'); // work with decoded target url String decodedTargetUrl = HttpUtility.UrlDecode(targetUrl); XmlTextReader reader = new XmlTextReader(data); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: { // we are on an element and we have to handle this elements String currentElement = reader.Name; // we found a resource name if (currentElement.Contains(":href")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // go one more step reader.Read(); // get the name String nameBase = reader.Value; // call the filter if needed if (callback != null) { nameBase = callback(targetUrl, service, session, nameBase); } /* * Just for bug fixing * * Console.WriteLine(" SVC Locator: " + config.ServiceLocator.ToString()); * Console.WriteLine("SVC Locator QL: " + HttpUtilityEx.GetPathAndQueryLessUri(config.ServiceLocator)); * Console.WriteLine(" Target Url: " + targetUrl); * Console.WriteLine(" NameBase: " + nameBase); */ // generate namebase for self check String nameBaseForSelfCheck; // remove the base url and build for selfcheck if (nameBase.StartsWith(config.ServiceLocator.ToString())) { nameBaseForSelfCheck = HttpUtility.UrlDecode(nameBase); nameBase = nameBase.Remove(0, config.ServiceLocator.ToString().Length); } else { nameBaseForSelfCheck = queryLessUri + HttpUtilityEx.PathDecodeUTF8(nameBase); } // trim all trailing slashes nameBase = nameBase.TrimEnd('/'); // work with the trailing lsahed nameBaseForSelfCheck = nameBaseForSelfCheck.TrimEnd('/'); if (targetUrl.EndsWith("/")) { nameBaseForSelfCheck += "/"; } // check if we are self if (nameBaseForSelfCheck.Equals(decodedTargetUrl)) { bIsSelf = true; } else { bIsSelf = false; } // get the last file or directory name PathHelper ph = new PathHelper(nameBase); resourceName = ph.GetFileName(); // unquote name resourceName = HttpUtility.UrlDecode(resourceName); } else if (currentElement.Contains(":ishidden")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // go one more step reader.Read(); // try to parse int iIsHidden = 0; if (!int.TryParse(reader.Value, out iIsHidden)) { iIsHidden = 0; } // convert bIsHidden = Convert.ToBoolean(iIsHidden); } else if (currentElement.Contains(":getcontentlength")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // go one more step reader.Read(); // read value if (!long.TryParse(reader.Value, out resourceLength)) { resourceLength = -1; } } else if (currentElement.Contains(":creationdate")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // go one more step reader.Read(); // parse if (!DateTime.TryParse(reader.Value, out resourceCreationDate)) { resourceCreationDate = DateTime.Now; } } else if (currentElement.Contains(":getlastmodified")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // go one more step reader.Read(); // parse if (!DateTime.TryParse(reader.Value, out resourceModificationDate)) { resourceModificationDate = DateTime.Now; } } else if (currentElement.Contains(":getcontenttype")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // go one more step reader.Read(); // parse resourceContentType = reader.Value; } else if (currentElement.Contains(":collection")) { // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(currentElement, reader)) { continue; } // set as directory bIsDirectory = true; } // go ahead break; } case XmlNodeType.EndElement: { // handle the end of an response if (!reader.Name.ToLower().Contains(":response")) { break; } // check if it is an element with valud namespace prefix if (!CheckIfNameSpaceDAVSpace(reader.Name, reader)) { continue; } // handle the end of an response, this means // create entry BaseFileEntry entry = null; if (bIsDirectory) { entry = new BaseDirectoryEntry(resourceName, resourceLength, resourceModificationDate, service, session); } else { entry = new BaseFileEntry(resourceName, resourceLength, resourceModificationDate, service, session); } entry.SetPropertyValue("CreationDate", resourceCreationDate); entry.SetPropertyValue("ContentType", resourceContentType); if (!bIsHidden) { if (bIsSelf) { results.Self = entry; } else { results.Childs.Add(entry); } } // reset all state properties resourceName = String.Empty; resourceLength = 0; resourceModificationDate = DateTime.Now; resourceCreationDate = DateTime.Now; resourceContentType = String.Empty; bIsHidden = false; bIsDirectory = false; // go ahead break; } default: { break; } } } ; if (results.Self == null) { throw new Exception("Unknown error in webrequest parser"); } } catch (Exception) { } // go ahead return(results); }
public static WebDavRequestResult CreateObjectsFromNetworkStream(Stream data, String targetUrl, IStorageProviderService service, IStorageProviderSession session, NameBaseFilterCallback callback) { return null; }