HttpResponse.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #region License
  2. /*
  3. * HttpResponse.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012-2014 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 string _code;
  39. private string _reason;
  40. #endregion
  41. #region Private Constructors
  42. private HttpResponse (string code, string reason, Version version, NameValueCollection headers)
  43. : base (version, headers)
  44. {
  45. _code = code;
  46. _reason = reason;
  47. }
  48. #endregion
  49. #region Internal Constructors
  50. internal HttpResponse (HttpStatusCode code)
  51. : this (code, code.GetDescription ())
  52. {
  53. }
  54. internal HttpResponse (HttpStatusCode code, string reason)
  55. : this (((int) code).ToString (), reason, HttpVersion.Version11, new NameValueCollection ())
  56. {
  57. Headers["Server"] = "websocket-sharp/1.0";
  58. }
  59. #endregion
  60. #region Public Properties
  61. public CookieCollection Cookies {
  62. get {
  63. return Headers.GetCookies (true);
  64. }
  65. }
  66. public bool HasConnectionClose {
  67. get {
  68. var comparison = StringComparison.OrdinalIgnoreCase;
  69. return Headers.Contains ("Connection", "close", comparison);
  70. }
  71. }
  72. public bool IsProxyAuthenticationRequired {
  73. get {
  74. return _code == "407";
  75. }
  76. }
  77. public bool IsRedirect {
  78. get {
  79. return _code == "301" || _code == "302";
  80. }
  81. }
  82. public bool IsUnauthorized {
  83. get {
  84. return _code == "401";
  85. }
  86. }
  87. public bool IsWebSocketResponse {
  88. get {
  89. return ProtocolVersion > HttpVersion.Version10
  90. && _code == "101"
  91. && Headers.Upgrades ("websocket");
  92. }
  93. }
  94. public string Reason {
  95. get {
  96. return _reason;
  97. }
  98. }
  99. public string StatusCode {
  100. get {
  101. return _code;
  102. }
  103. }
  104. #endregion
  105. #region Internal Methods
  106. internal static HttpResponse CreateCloseResponse (HttpStatusCode code)
  107. {
  108. var res = new HttpResponse (code);
  109. res.Headers["Connection"] = "close";
  110. return res;
  111. }
  112. internal static HttpResponse CreateUnauthorizedResponse (string challenge)
  113. {
  114. var res = new HttpResponse (HttpStatusCode.Unauthorized);
  115. res.Headers["WWW-Authenticate"] = challenge;
  116. return res;
  117. }
  118. internal static HttpResponse CreateWebSocketResponse ()
  119. {
  120. var res = new HttpResponse (HttpStatusCode.SwitchingProtocols);
  121. var headers = res.Headers;
  122. headers["Upgrade"] = "websocket";
  123. headers["Connection"] = "Upgrade";
  124. return res;
  125. }
  126. internal static HttpResponse Parse (string[] headerParts)
  127. {
  128. var statusLine = headerParts[0].Split (new[] { ' ' }, 3);
  129. if (statusLine.Length != 3)
  130. throw new ArgumentException ("Invalid status line: " + headerParts[0]);
  131. var headers = new WebHeaderCollection ();
  132. for (int i = 1; i < headerParts.Length; i++)
  133. headers.InternalSet (headerParts[i], true);
  134. return new HttpResponse (
  135. statusLine[1], statusLine[2], new Version (statusLine[0].Substring (5)), headers);
  136. }
  137. internal static HttpResponse Read (Stream stream, int millisecondsTimeout)
  138. {
  139. return Read<HttpResponse> (stream, Parse, millisecondsTimeout);
  140. }
  141. #endregion
  142. #region Public Methods
  143. public void SetCookies (CookieCollection cookies)
  144. {
  145. if (cookies == null || cookies.Count == 0)
  146. return;
  147. var headers = Headers;
  148. foreach (var cookie in cookies.Sorted)
  149. headers.Add ("Set-Cookie", cookie.ToResponseString ());
  150. }
  151. public override string ToString ()
  152. {
  153. var output = new StringBuilder (64);
  154. output.AppendFormat ("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf);
  155. var headers = Headers;
  156. foreach (var key in headers.AllKeys)
  157. output.AppendFormat ("{0}: {1}{2}", key, headers[key], CrLf);
  158. output.Append (CrLf);
  159. var entity = EntityBody;
  160. if (entity.Length > 0)
  161. output.Append (entity);
  162. return output.ToString ();
  163. }
  164. #endregion
  165. }
  166. }