HttpListenerResponse.cs 9.2 KB

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