WebDAVGetMethodHandler.cs 3.3 KB

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