2
0

HttpListenerResponse.cs 9.1 KB

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