12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using WebDAVSharp.Server.Adapters;
- using WebDAVSharp.Server.Exceptions;
- using WebDAVSharp.Server.Stores;
- namespace WebDAVSharp.Server.MethodHandlers
- {
- /// <summary>
- /// This class implements the <c>MOVE</c> HTTP method for WebDAV#.
- /// </summary>
- internal class WebDavMoveMethodHandler : WebDavMethodHandlerBase
- {
- #region Properties
- /// <summary>
- /// Gets the collection of the names of the HTTP methods handled by this instance.
- /// </summary>
- /// <value>
- /// The names.
- /// </value>
- public override IEnumerable<string> Names => new[]
- {
- "MOVE"
- };
- #endregion
- #region Functions
- /// <summary>
- /// Processes the request.
- /// </summary>
- /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
- /// <param name="context">
- /// The
- /// <see cref="IHttpListenerContext" /> object containing both the request and response
- /// objects to use.
- /// </param>
- /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
- public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
- {
- MoveItem(server, context, store, context.Request.Url.GetItem(server, store));
- }
- /// <summary>
- /// Moves the
- /// </summary>
- /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
- /// <param name="context">
- /// The
- /// <see cref="IHttpListenerContext" /> object containing both the request and response
- /// objects to use.
- /// </param>
- /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
- /// <param name="sourceWebDavStoreItem">The <see cref="IWebDavStoreItem" /> that will be moved</param>
- /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavForbiddenException">
- /// If the source path is the same as the
- /// destination path
- /// </exception>
- /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavPreconditionFailedException">If one of the preconditions failed</exception>
- private static void MoveItem(WebDavServer server, IHttpListenerContext context, IWebDavStore store,
- IWebDavStoreItem sourceWebDavStoreItem)
- {
- Uri destinationUri = GetDestinationHeader(context.Request);
- IWebDavStoreCollection destinationParentCollection = GetParentCollection(server, store, destinationUri);
- bool isNew = true;
- string destinationName = Uri.UnescapeDataString(destinationUri.Segments.Last().TrimEnd('/', '\\'));
- IWebDavStoreItem destination = destinationParentCollection.GetItemByName(destinationName);
- if (destination != null)
- {
- if (sourceWebDavStoreItem.ItemPath == destination.ItemPath)
- throw new WebDavForbiddenException();
- // if the overwrite header is F, statuscode = precondition failed
- if (!GetOverwriteHeader(context.Request))
- throw new WebDavPreconditionFailedException();
- // else delete destination and set isNew to false
- destinationParentCollection.Delete(destination);
- isNew = false;
- }
- destinationParentCollection.MoveItemHere(sourceWebDavStoreItem, destinationName);
- // send correct response
- context.SendSimpleResponse(isNew ? (int) HttpStatusCode.Created : (int) HttpStatusCode.NoContent);
- }
- #endregion
- }
- }
|