WebDAVMkColMethodHandler.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters;
  6. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions;
  7. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
  8. namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers
  9. {
  10. /// <summary>
  11. /// This class implements the <c>MKCOL</c> HTTP method for WebDAV#.
  12. /// </summary>
  13. internal class WebDavMkColMethodHandler : WebDavMethodHandlerBase, IWebDavMethodHandler
  14. {
  15. /// <summary>
  16. /// Gets the collection of the names of the HTTP methods handled by this instance.
  17. /// </summary>
  18. /// <value>
  19. /// The names.
  20. /// </value>
  21. public IEnumerable<string> Names => new[]
  22. {
  23. "MKCOL"
  24. };
  25. /// <summary>
  26. /// Processes the request.
  27. /// </summary>
  28. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  29. /// <param name="context">
  30. /// The
  31. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  32. /// objects to use.
  33. /// </param>
  34. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  35. /// <exception cref="WebDavUnsupportedMediaTypeException"></exception>
  36. /// <exception cref="WebDavMethodNotAllowedException"></exception>
  37. public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  38. {
  39. if (context.Request.ContentLength64 > 0)
  40. throw new WebDavUnsupportedMediaTypeException();
  41. var collection = GetParentCollection(server, store, context.Request.Url);
  42. var collectionName = Uri.UnescapeDataString(
  43. context.Request.Url.Segments.Last().TrimEnd('/', '\\')
  44. );
  45. if (collection.GetItemByName(collectionName) != null)
  46. throw new WebDavMethodNotAllowedException();
  47. collection.CreateCollection(collectionName);
  48. context.SendSimpleResponse(HttpStatusCode.Created);
  49. }
  50. }
  51. }