HttpListenerResponseAdapter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. namespace WebDAVSharp.Server.Adapters
  6. {
  7. /// <summary>
  8. /// This
  9. /// <see cref="IHttpListenerResponse" /> implementation wraps around a
  10. /// <see cref="HttpListenerResponse" /> instance.
  11. /// </summary>
  12. internal sealed class HttpListenerResponseAdapter : IHttpListenerResponse
  13. {
  14. #region Private Variables
  15. private readonly HttpListenerResponse _response;
  16. #endregion
  17. #region Properties
  18. /// <summary>
  19. /// Gets the internal instance that was adapted for WebDAV#.
  20. /// </summary>
  21. /// <value>
  22. /// The adapted instance.
  23. /// </value>
  24. public HttpListenerResponse AdaptedInstance => _response;
  25. /// <summary>
  26. /// Gets or sets the HTTP status code to be returned to the client.
  27. /// </summary>
  28. public int StatusCode
  29. {
  30. get { return _response.StatusCode; }
  31. set { _response.StatusCode = value; }
  32. }
  33. /// <summary>
  34. /// Gets or sets a text description of the HTTP <see cref="StatusCode">status code</see> returned to the client.
  35. /// </summary>
  36. public string StatusDescription
  37. {
  38. get { return _response.StatusDescription; }
  39. set { _response.StatusDescription = value ?? string.Empty; }
  40. }
  41. /// <summary>
  42. /// Gets a <see cref="Stream" /> object to which a response can be written.
  43. /// </summary>
  44. public Stream OutputStream => _response.OutputStream;
  45. /// <summary>
  46. /// Gets or sets the <see cref="Encoding" /> for this response's <see cref="OutputStream" />.
  47. /// </summary>
  48. public Encoding ContentEncoding
  49. {
  50. get { return _response.ContentEncoding; }
  51. set { _response.ContentEncoding = value; }
  52. }
  53. /// <summary>
  54. /// Gets or sets the number of bytes in the body data included in the response.
  55. /// </summary>
  56. public long ContentLength64
  57. {
  58. get { return _response.ContentLength64; }
  59. set { _response.ContentLength64 = value; }
  60. }
  61. #endregion
  62. #region Public Functions
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="HttpListenerResponseAdapter" /> class.
  65. /// </summary>
  66. /// <param name="response">The <see cref="HttpListenerResponse" /> to adapt for WebDAV#.</param>
  67. /// <exception cref="System.ArgumentNullException">Response</exception>
  68. /// <exception cref="ArgumentNullException"><paramref name="response" /> is <c>null</c>.</exception>
  69. public HttpListenerResponseAdapter(HttpListenerResponse response)
  70. {
  71. if (response == null)
  72. throw new ArgumentNullException(nameof(response));
  73. _response = response;
  74. }
  75. /// <summary>
  76. /// Sends the response to the client and releases the resources held by the adapted
  77. /// <see cref="HttpListenerResponse" /> instance.
  78. /// </summary>
  79. public void Close()
  80. {
  81. _response.Close();
  82. }
  83. /// <summary>
  84. /// Appends a value to the specified HTTP header to be sent with the response.
  85. /// </summary>
  86. /// <param name="name">The name of the HTTP header to append the <paramref name="value" /> to.</param>
  87. /// <param name="value">The value to append to the <paramref name="name" /> header.</param>
  88. public void AppendHeader(string name, string value)
  89. {
  90. _response.AppendHeader(name, value);
  91. }
  92. #endregion
  93. }
  94. }