WebDAVPutMethodHandler.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using WebDAVSharp.Server.Adapters;
  7. using WebDAVSharp.Server.Exceptions;
  8. using WebDAVSharp.Server.Stores;
  9. namespace WebDAVSharp.Server.MethodHandlers
  10. {
  11. /// <summary>
  12. /// This class implements the <c>PUT</c> HTTP method for WebDAV#.
  13. /// </summary>
  14. internal class WebDavPutMethodHandler : WebDavMethodHandlerBase
  15. {
  16. #region Properties
  17. /// <summary>
  18. /// Gets the collection of the names of the HTTP methods handled by this instance.
  19. /// </summary>
  20. /// <value>
  21. /// The names.
  22. /// </value>
  23. public override IEnumerable<string> Names => new[]
  24. {
  25. "PUT"
  26. };
  27. #endregion
  28. #region Functions
  29. /// <summary>
  30. /// Processes the request.
  31. /// </summary>
  32. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  33. /// <param name="context">
  34. /// The
  35. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  36. /// objects to use.
  37. /// </param>
  38. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  39. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
  40. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavLengthRequiredException">If the ContentLength header was not found</exception>
  41. public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  42. {
  43. // Get the parent collection
  44. IWebDavStoreCollection parentCollection = GetParentCollection(server, store, context.Request.Url);
  45. // Gets the item name from the url
  46. string itemName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));
  47. IWebDavStoreItem item = parentCollection.GetItemByName(itemName);
  48. IWebDavStoreDocument doc;
  49. if (item != null)
  50. {
  51. doc = item as IWebDavStoreDocument;
  52. if (doc == null)
  53. throw new WebDavMethodNotAllowedException();
  54. }
  55. else
  56. {
  57. doc = parentCollection.CreateDocument(itemName);
  58. }
  59. if (context.Request.ContentLength64 < 0)
  60. throw new WebDavLengthRequiredException();
  61. using (Stream stream = doc.OpenWriteStream(false))
  62. {
  63. long left = context.Request.ContentLength64;
  64. stream.SetLength(left);
  65. byte[] buffer = new byte[65536];
  66. while (left > 0)
  67. {
  68. int toRead = Convert.ToInt32(Math.Min(left, buffer.Length));
  69. int inBuffer = context.Request.InputStream.Read(buffer, 0, toRead);
  70. stream.Write(buffer, 0, inBuffer);
  71. left -= inBuffer;
  72. }
  73. }
  74. context.SendSimpleResponse((int) HttpStatusCode.Created);
  75. }
  76. #endregion
  77. }
  78. }