using System; using System.Collections.Generic; using System.Linq; using WebDAVSharp.Server.Adapters; using WebDAVSharp.Server.Exceptions; using WebDAVSharp.Server.Stores; using static System.String; namespace WebDAVSharp.Server.MethodHandlers { /// /// This is the base class for implementations. /// public abstract class WebDavMethodHandlerBase : IWebDavMethodHandler { #region Variables private const int DepthInfinity = -1; //public WindowsIdentity UserIdentity; #endregion /// /// public abstract IEnumerable Names { get; } /// /// /// /// /// public abstract void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store); #region Static Functions /// /// 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) { Uri parentCollectionUri = childUri.GetParentUri(); return parentCollectionUri.GetItem(server, store) as IWebDavStoreCollection; } /// /// 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 = collection.GetItemByName(Uri.UnescapeDataString(childUri.Segments.Last().TrimEnd('/', '\\'))); if (item == null) throw new WebDavNotFoundException(); item.Href = childUri; 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 string depth = request.Headers["Depth"]; // check if the string is valid or not infinity // if so, try to parse it to an int if (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 string 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 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 string destinationUri = request.Headers["Destination"]; // check if the string is valid if (!IsNullOrEmpty(destinationUri)) return new Uri(destinationUri); // else, throw exception throw new WebDavConflictException(); } #endregion } }