HttpListenerResponse.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. namespace SocketHttpListener.Net
  6. {
  7. public sealed partial class HttpListenerResponse : IDisposable
  8. {
  9. private BoundaryType _boundaryType = BoundaryType.None;
  10. private CookieCollection _cookies;
  11. private HttpListenerContext _httpContext;
  12. private bool _keepAlive = true;
  13. private HttpResponseStream _responseStream;
  14. private string _statusDescription;
  15. private WebHeaderCollection _webHeaders = new WebHeaderCollection();
  16. public WebHeaderCollection Headers => _webHeaders;
  17. public Encoding ContentEncoding { get; set; }
  18. public string ContentType
  19. {
  20. get => Headers["Content-Type"];
  21. set
  22. {
  23. CheckDisposed();
  24. if (string.IsNullOrEmpty(value))
  25. {
  26. Headers.Remove("Content-Type");
  27. }
  28. else
  29. {
  30. Headers.Set("Content-Type", value);
  31. }
  32. }
  33. }
  34. private HttpListenerContext HttpListenerContext => _httpContext;
  35. private HttpListenerRequest HttpListenerRequest => HttpListenerContext.Request;
  36. internal EntitySendFormat EntitySendFormat
  37. {
  38. get => (EntitySendFormat)_boundaryType;
  39. set
  40. {
  41. CheckDisposed();
  42. CheckSentHeaders();
  43. if (value == EntitySendFormat.Chunked && HttpListenerRequest.ProtocolVersion.Minor == 0)
  44. {
  45. throw new ProtocolViolationException("net_nochunkuploadonhttp10");
  46. }
  47. _boundaryType = (BoundaryType)value;
  48. if (value != EntitySendFormat.ContentLength)
  49. {
  50. _contentLength = -1;
  51. }
  52. }
  53. }
  54. public bool SendChunked
  55. {
  56. get => EntitySendFormat == EntitySendFormat.Chunked;
  57. set => EntitySendFormat = value ? EntitySendFormat.Chunked : EntitySendFormat.ContentLength;
  58. }
  59. // We MUST NOT send message-body when we send responses with these Status codes
  60. private static readonly int[] s_noResponseBody = { 100, 101, 204, 205, 304 };
  61. private static bool CanSendResponseBody(int responseCode)
  62. {
  63. for (int i = 0; i < s_noResponseBody.Length; i++)
  64. {
  65. if (responseCode == s_noResponseBody[i])
  66. {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. public long ContentLength64
  73. {
  74. get => _contentLength;
  75. set
  76. {
  77. CheckDisposed();
  78. CheckSentHeaders();
  79. if (value >= 0)
  80. {
  81. _contentLength = value;
  82. _boundaryType = BoundaryType.ContentLength;
  83. }
  84. else
  85. {
  86. throw new ArgumentOutOfRangeException(nameof(value));
  87. }
  88. }
  89. }
  90. public CookieCollection Cookies
  91. {
  92. get => _cookies ?? (_cookies = new CookieCollection());
  93. set => _cookies = value;
  94. }
  95. public bool KeepAlive
  96. {
  97. get => _keepAlive;
  98. set
  99. {
  100. CheckDisposed();
  101. _keepAlive = value;
  102. }
  103. }
  104. public Stream OutputStream
  105. {
  106. get
  107. {
  108. CheckDisposed();
  109. EnsureResponseStream();
  110. return _responseStream;
  111. }
  112. }
  113. public string RedirectLocation
  114. {
  115. get => Headers["Location"];
  116. set
  117. {
  118. // note that this doesn't set the status code to a redirect one
  119. CheckDisposed();
  120. if (string.IsNullOrEmpty(value))
  121. {
  122. Headers.Remove("Location");
  123. }
  124. else
  125. {
  126. Headers.Set("Location", value);
  127. }
  128. }
  129. }
  130. public string StatusDescription
  131. {
  132. get
  133. {
  134. if (_statusDescription == null)
  135. {
  136. // if the user hasn't set this, generated on the fly, if possible.
  137. // We know this one is safe, no need to verify it as in the setter.
  138. _statusDescription = HttpStatusDescription.Get(StatusCode);
  139. }
  140. if (_statusDescription == null)
  141. {
  142. _statusDescription = string.Empty;
  143. }
  144. return _statusDescription;
  145. }
  146. set
  147. {
  148. CheckDisposed();
  149. if (value == null)
  150. {
  151. throw new ArgumentNullException(nameof(value));
  152. }
  153. // Need to verify the status description doesn't contain any control characters except HT. We mask off the high
  154. // byte since that's how it's encoded.
  155. for (int i = 0; i < value.Length; i++)
  156. {
  157. char c = (char)(0x000000ff & (uint)value[i]);
  158. if ((c <= 31 && c != (byte)'\t') || c == 127)
  159. {
  160. throw new ArgumentException("net_WebHeaderInvalidControlChars");
  161. }
  162. }
  163. _statusDescription = value;
  164. }
  165. }
  166. public void AddHeader(string name, string value)
  167. {
  168. Headers.Set(name, value);
  169. }
  170. public void AppendHeader(string name, string value)
  171. {
  172. Headers.Add(name, value);
  173. }
  174. public void AppendCookie(Cookie cookie)
  175. {
  176. if (cookie == null)
  177. {
  178. throw new ArgumentNullException(nameof(cookie));
  179. }
  180. Cookies.Add(cookie);
  181. }
  182. private void ComputeCookies()
  183. {
  184. if (_cookies != null)
  185. {
  186. // now go through the collection, and concatenate all the cookies in per-variant strings
  187. //string setCookie2 = null, setCookie = null;
  188. //for (int index = 0; index < _cookies.Count; index++)
  189. //{
  190. // Cookie cookie = _cookies[index];
  191. // string cookieString = cookie.ToServerString();
  192. // if (cookieString == null || cookieString.Length == 0)
  193. // {
  194. // continue;
  195. // }
  196. // if (cookie.IsRfc2965Variant())
  197. // {
  198. // setCookie2 = setCookie2 == null ? cookieString : setCookie2 + ", " + cookieString;
  199. // }
  200. // else
  201. // {
  202. // setCookie = setCookie == null ? cookieString : setCookie + ", " + cookieString;
  203. // }
  204. //}
  205. //if (!string.IsNullOrEmpty(setCookie))
  206. //{
  207. // Headers.Set(HttpKnownHeaderNames.SetCookie, setCookie);
  208. // if (string.IsNullOrEmpty(setCookie2))
  209. // {
  210. // Headers.Remove(HttpKnownHeaderNames.SetCookie2);
  211. // }
  212. //}
  213. //if (!string.IsNullOrEmpty(setCookie2))
  214. //{
  215. // Headers.Set(HttpKnownHeaderNames.SetCookie2, setCookie2);
  216. // if (string.IsNullOrEmpty(setCookie))
  217. // {
  218. // Headers.Remove(HttpKnownHeaderNames.SetCookie);
  219. // }
  220. //}
  221. }
  222. }
  223. public void Redirect(string url)
  224. {
  225. Headers["Location"] = url;
  226. StatusCode = (int)HttpStatusCode.Redirect;
  227. StatusDescription = "Found";
  228. }
  229. public void SetCookie(Cookie cookie)
  230. {
  231. if (cookie == null)
  232. {
  233. throw new ArgumentNullException(nameof(cookie));
  234. }
  235. //Cookie newCookie = cookie.Clone();
  236. //int added = Cookies.InternalAdd(newCookie, true);
  237. //if (added != 1)
  238. //{
  239. // // The Cookie already existed and couldn't be replaced.
  240. // throw new ArgumentException("Cookie exists");
  241. //}
  242. }
  243. void IDisposable.Dispose()
  244. {
  245. Dispose();
  246. }
  247. private void CheckDisposed()
  248. {
  249. if (Disposed)
  250. {
  251. throw new ObjectDisposedException(GetType().FullName);
  252. }
  253. }
  254. private void CheckSentHeaders()
  255. {
  256. if (SentHeaders)
  257. {
  258. throw new InvalidOperationException();
  259. }
  260. }
  261. }
  262. }