HttpListenerResponse.cs 9.0 KB

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