HttpResponse.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #region License
  2. /*
  3. * HttpResponse.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012-2022 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Collections.Specialized;
  30. using System.IO;
  31. using System.Text;
  32. using WebSocketSharp.Net;
  33. namespace WebSocketSharp
  34. {
  35. internal class HttpResponse : HttpBase
  36. {
  37. #region Private Fields
  38. private int _code;
  39. private string _reason;
  40. #endregion
  41. #region Private Constructors
  42. private HttpResponse (
  43. int code, string reason, Version version, NameValueCollection headers
  44. )
  45. : base (version, headers)
  46. {
  47. _code = code;
  48. _reason = reason;
  49. }
  50. #endregion
  51. #region Internal Constructors
  52. internal HttpResponse (int code)
  53. : this (code, code.GetStatusDescription ())
  54. {
  55. }
  56. internal HttpResponse (HttpStatusCode code)
  57. : this ((int) code)
  58. {
  59. }
  60. internal HttpResponse (int code, string reason)
  61. : this (
  62. code,
  63. reason,
  64. HttpVersion.Version11,
  65. new NameValueCollection ()
  66. )
  67. {
  68. Headers["Server"] = "websocket-sharp/1.0";
  69. }
  70. internal HttpResponse (HttpStatusCode code, string reason)
  71. : this ((int) code, reason)
  72. {
  73. }
  74. #endregion
  75. #region Internal Properties
  76. internal string StatusLine {
  77. get {
  78. return _reason != null
  79. ? String.Format (
  80. "HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf
  81. )
  82. : String.Format (
  83. "HTTP/{0} {1}{2}", ProtocolVersion, _code, CrLf
  84. );
  85. }
  86. }
  87. #endregion
  88. #region Public Properties
  89. public bool CloseConnection {
  90. get {
  91. var compType = StringComparison.OrdinalIgnoreCase;
  92. return Headers.Contains ("Connection", "close", compType);
  93. }
  94. }
  95. public CookieCollection Cookies {
  96. get {
  97. return Headers.GetCookies (true);
  98. }
  99. }
  100. public bool IsProxyAuthenticationRequired {
  101. get {
  102. return _code == 407;
  103. }
  104. }
  105. public bool IsRedirect {
  106. get {
  107. return _code == 301 || _code == 302;
  108. }
  109. }
  110. public bool IsSuccess {
  111. get {
  112. return _code >= 200 && _code <= 299;
  113. }
  114. }
  115. public bool IsUnauthorized {
  116. get {
  117. return _code == 401;
  118. }
  119. }
  120. public bool IsWebSocketResponse {
  121. get {
  122. return ProtocolVersion > HttpVersion.Version10
  123. && _code == 101
  124. && Headers.Upgrades ("websocket");
  125. }
  126. }
  127. public override string MessageHeader {
  128. get {
  129. return StatusLine + HeaderSection;
  130. }
  131. }
  132. public string Reason {
  133. get {
  134. return _reason;
  135. }
  136. }
  137. public int StatusCode {
  138. get {
  139. return _code;
  140. }
  141. }
  142. #endregion
  143. #region Internal Methods
  144. internal static HttpResponse CreateCloseResponse (HttpStatusCode code)
  145. {
  146. var ret = new HttpResponse (code);
  147. ret.Headers["Connection"] = "close";
  148. return ret;
  149. }
  150. internal static HttpResponse CreateUnauthorizedResponse (string challenge)
  151. {
  152. var ret = new HttpResponse (HttpStatusCode.Unauthorized);
  153. ret.Headers["WWW-Authenticate"] = challenge;
  154. return ret;
  155. }
  156. internal static HttpResponse CreateWebSocketHandshakeResponse ()
  157. {
  158. var ret = new HttpResponse (HttpStatusCode.SwitchingProtocols);
  159. var headers = ret.Headers;
  160. headers["Upgrade"] = "websocket";
  161. headers["Connection"] = "Upgrade";
  162. return ret;
  163. }
  164. internal static HttpResponse Parse (string[] messageHeader)
  165. {
  166. var len = messageHeader.Length;
  167. if (len == 0) {
  168. var msg = "An empty response header.";
  169. throw new ArgumentException (msg);
  170. }
  171. var slParts = messageHeader[0].Split (new[] { ' ' }, 3);
  172. var plen = slParts.Length;
  173. if (plen < 2) {
  174. var msg = "It includes an invalid status line.";
  175. throw new ArgumentException (msg);
  176. }
  177. var code = slParts[1].ToInt32 ();
  178. var reason = plen == 3 ? slParts[2] : null;
  179. var ver = slParts[0].Substring (5).ToVersion ();
  180. var headers = new WebHeaderCollection ();
  181. for (var i = 1; i < len; i++)
  182. headers.InternalSet (messageHeader[i], true);
  183. return new HttpResponse (code, reason, ver, headers);
  184. }
  185. internal static HttpResponse ReadResponse (
  186. Stream stream, int millisecondsTimeout
  187. )
  188. {
  189. return Read<HttpResponse> (stream, Parse, millisecondsTimeout);
  190. }
  191. #endregion
  192. #region Public Methods
  193. public void SetCookies (CookieCollection cookies)
  194. {
  195. if (cookies == null || cookies.Count == 0)
  196. return;
  197. var headers = Headers;
  198. foreach (var cookie in cookies.Sorted) {
  199. var val = cookie.ToResponseString ();
  200. headers.Add ("Set-Cookie", val);
  201. }
  202. }
  203. #endregion
  204. }
  205. }