WebDAVPutMethodHandler.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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>PUT</c> HTTP method for WebDAV#.
  12. /// </summary>
  13. internal class WebDavPutMethodHandler : 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. "PUT"
  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="WebDavMethodNotAllowedException"></exception>
  36. /// <exception cref="WebDavLengthRequiredException">If the ContentLength header was not found</exception>
  37. public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  38. {
  39. // Get the parent collection
  40. var parentCollection = GetParentCollection(server, store, context.Request.Url);
  41. // Gets the item name from the url
  42. var itemName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));
  43. var item = parentCollection.GetItemByName(itemName);
  44. IWebDavStoreDocument doc;
  45. if (item != null)
  46. {
  47. doc = item as IWebDavStoreDocument;
  48. if (doc == null)
  49. throw new WebDavMethodNotAllowedException();
  50. }
  51. else
  52. {
  53. doc = parentCollection.CreateDocument(itemName);
  54. }
  55. if (context.Request.ContentLength64 < 0)
  56. throw new WebDavLengthRequiredException();
  57. using (var stream = doc.OpenWriteStream(false))
  58. {
  59. var left = context.Request.ContentLength64;
  60. var buffer = new byte[4096];
  61. while (left > 0)
  62. {
  63. var toRead = Convert.ToInt32(Math.Min(left, buffer.Length));
  64. var inBuffer = context.Request.InputStream.Read(buffer, 0, toRead);
  65. stream.Write(buffer, 0, inBuffer);
  66. left -= inBuffer;
  67. }
  68. }
  69. context.SendSimpleResponse(HttpStatusCode.Created);
  70. }
  71. }
  72. }