WebDAVGetMethodHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Net;
  4. using WebDAVSharp.Server.Adapters;
  5. using WebDAVSharp.Server.Exceptions;
  6. using WebDAVSharp.Server.Stores;
  7. namespace WebDAVSharp.Server.MethodHandlers
  8. {
  9. /// <summary>
  10. /// This class implements the <c>GET</c> HTTP method for WebDAV#.
  11. /// </summary>
  12. internal sealed class WebDavGetMethodHandler : WebDavMethodHandlerBase
  13. {
  14. #region Properties
  15. /// <summary>
  16. /// Gets the collection of the names of the verbs handled by this instance.
  17. /// </summary>
  18. /// <value>
  19. /// The names.
  20. /// </value>
  21. public override IEnumerable<string> Names => new[]
  22. {
  23. "GET"
  24. };
  25. #endregion
  26. #region Functions
  27. /// <summary>
  28. /// Processes the request.
  29. /// </summary>
  30. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  31. /// <param name="context">
  32. /// The
  33. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  34. /// objects to use.
  35. /// </param>
  36. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  37. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException"></exception>
  38. /// <exception cref="WebDavNotFoundException">
  39. /// <para>
  40. /// <paramref name="context" /> specifies a request for a store item that does not exist.
  41. /// </para>
  42. /// <para>- or -</para>
  43. /// <para>
  44. /// <paramref name="context" /> specifies a request for a store item that is not a document.
  45. /// </para>
  46. /// </exception>
  47. /// <exception cref="WebDavConflictException">
  48. /// <paramref name="context" /> specifies a request for a store item using a
  49. /// collection path that does not exist.
  50. /// </exception>
  51. public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  52. {
  53. IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);
  54. IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);
  55. IWebDavStoreDocument doc = item as IWebDavStoreDocument;
  56. if (doc == null)
  57. throw new WebDavNotFoundException();
  58. long docSize = doc.Size;
  59. if (docSize == 0)
  60. {
  61. context.Response.StatusCode = (int) HttpStatusCode.OK;
  62. context.Response.ContentLength64 = 0;
  63. }
  64. using (Stream stream = doc.OpenReadStream())
  65. {
  66. if (stream == null)
  67. {
  68. context.Response.StatusCode = (int) HttpStatusCode.OK;
  69. context.Response.ContentLength64 = 0;
  70. }
  71. else
  72. {
  73. context.Response.StatusCode = (int) HttpStatusCode.OK;
  74. if (docSize > 0)
  75. context.Response.ContentLength64 = docSize;
  76. byte[] buffer = new byte[65536];
  77. int inBuffer;
  78. while ((inBuffer = stream.Read(buffer, 0, buffer.Length)) > 0)
  79. context.Response.OutputStream.Write(buffer, 0, inBuffer);
  80. context.Response.OutputStream.Flush();
  81. }
  82. }
  83. context.Response.Close();
  84. }
  85. #endregion
  86. }
  87. }