using System; using System.Linq; using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters; using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions; using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores; namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers { /// /// This is the base class for implementations. /// internal abstract class WebDavMethodHandlerBase { private const int DepthInfinity = -1; /// /// Get the parent collection from the requested /// . /// 409 Conflict possible. /// /// The through which the request came in from the client. /// The that the is hosting. /// The object containing the specific location of the child /// /// The parrent collection as an /// /// /// /// /// When the user is unauthorized and doesn't have access /// When the parent collection doesn't exist public static IWebDavStoreCollection GetParentCollection(WebDavServer server, IWebDavStore store, Uri childUri) { var parentCollectionUri = childUri.GetParentUri(); IWebDavStoreCollection collection; try { collection = parentCollectionUri.GetItem(server, store) as IWebDavStoreCollection; } catch (UnauthorizedAccessException) { throw new WebDavUnauthorizedException(); } catch (WebDavNotFoundException) { throw new WebDavConflictException(); } if (collection == null) throw new WebDavConflictException(); return collection; } /// /// Get the item in the collection from the requested /// . /// 409 Conflict possible. /// /// The parent collection as a /// The object containing the specific location of the child /// /// The from the /// /// /// If user is not authorized to get access to /// the item /// /// If item not found. public static IWebDavStoreItem GetItemFromCollection(IWebDavStoreCollection collection, Uri childUri) { IWebDavStoreItem item; try { item = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\'))); } catch (UnauthorizedAccessException) { throw new WebDavUnauthorizedException(); } catch (WebDavNotFoundException) { throw new WebDavNotFoundException(); } if (item == null) throw new WebDavNotFoundException(); return item; } /// /// Gets the Depth header : 0, 1 or infinity /// /// The with the response included /// /// The values 0, 1 or -1 (for infinity) /// public static int GetDepthHeader(IHttpListenerRequest request) { // get the value of the depth header as a string var depth = request.Headers["Depth"]; // check if the string is valid or not infinity // if so, try to parse it to an int if (string.IsNullOrEmpty(depth) || depth.Equals("infinity")) return DepthInfinity; int value; if (!int.TryParse(depth, out value)) return DepthInfinity; if (value == 0 || value == 1) return value; // else, return the infinity value return DepthInfinity; } /// /// Gets the Overwrite header : T or F /// /// The has the header included /// The true if overwrite, false if no overwrite public static bool GetOverwriteHeader(IHttpListenerRequest request) { // get the value of the Overwrite header as a string var overwrite = request.Headers["Overwrite"]; // check if the string is valid and if it equals T return overwrite != null && overwrite.Equals("T"); // else, return false } /// /// Gets the Timeout header : Second-number /// /// The request with the request included /// The value of the Timeout header as a string public static string GetTimeoutHeader(IHttpListenerRequest request) { // get the value of the timeout header as a string var timeout = request.Headers["Timeout"]; // check if the string is valid or not infinity // if so, try to parse it to an int if (!string.IsNullOrEmpty(timeout) && !timeout.Equals("infinity") && !timeout.Equals("Infinite, Second-4100000000")) return timeout; // else, return the timeout value as if it was requested to be 4 days return "Second-345600"; } /// /// Gets the Destination header as an URI /// /// The has the header included /// The containing the destination public static Uri GetDestinationHeader(IHttpListenerRequest request) { // get the value of the Destination header as a string var destinationUri = request.Headers["Destination"]; // check if the string is valid if (!string.IsNullOrEmpty(destinationUri)) return new Uri(destinationUri); // else, throw exception throw new WebDavConflictException(); } } }