HttpResponse.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Text;
  5. using MediaBrowser.Model.Services;
  6. using SocketHttpListener.Net;
  7. using HttpStatusCode = SocketHttpListener.Net.HttpStatusCode;
  8. using HttpVersion = SocketHttpListener.Net.HttpVersion;
  9. namespace SocketHttpListener
  10. {
  11. internal class HttpResponse : HttpBase
  12. {
  13. #region Private Fields
  14. private string _code;
  15. private string _reason;
  16. #endregion
  17. #region Private Constructors
  18. private HttpResponse(string code, string reason, Version version, QueryParamCollection headers)
  19. : base(version, headers)
  20. {
  21. _code = code;
  22. _reason = reason;
  23. }
  24. #endregion
  25. #region Internal Constructors
  26. internal HttpResponse(HttpStatusCode code)
  27. : this(code, code.GetDescription())
  28. {
  29. }
  30. internal HttpResponse(HttpStatusCode code, string reason)
  31. : this(((int)code).ToString(), reason, HttpVersion.Version11, new QueryParamCollection())
  32. {
  33. Headers["Server"] = "websocket-sharp/1.0";
  34. }
  35. #endregion
  36. #region Public Properties
  37. public CookieCollection Cookies
  38. {
  39. get
  40. {
  41. return GetCookies(Headers, true);
  42. }
  43. }
  44. private static CookieCollection GetCookies(QueryParamCollection headers, bool response)
  45. {
  46. var name = response ? "Set-Cookie" : "Cookie";
  47. return headers == null || !headers.Contains(name)
  48. ? new CookieCollection()
  49. : CookieHelper.Parse(headers[name], response);
  50. }
  51. public bool IsProxyAuthenticationRequired
  52. {
  53. get
  54. {
  55. return _code == "407";
  56. }
  57. }
  58. public bool IsUnauthorized
  59. {
  60. get
  61. {
  62. return _code == "401";
  63. }
  64. }
  65. public bool IsWebSocketResponse
  66. {
  67. get
  68. {
  69. var headers = Headers;
  70. return ProtocolVersion > HttpVersion.Version10 &&
  71. _code == "101" &&
  72. headers.Contains("Upgrade", "websocket") &&
  73. headers.Contains("Connection", "Upgrade");
  74. }
  75. }
  76. public string Reason
  77. {
  78. get
  79. {
  80. return _reason;
  81. }
  82. }
  83. public string StatusCode
  84. {
  85. get
  86. {
  87. return _code;
  88. }
  89. }
  90. #endregion
  91. #region Internal Methods
  92. internal static HttpResponse CreateCloseResponse(HttpStatusCode code)
  93. {
  94. var res = new HttpResponse(code);
  95. res.Headers["Connection"] = "close";
  96. return res;
  97. }
  98. #endregion
  99. #region Public Methods
  100. public void SetCookies(CookieCollection cookies)
  101. {
  102. if (cookies == null || cookies.Count == 0)
  103. return;
  104. var headers = Headers;
  105. var sorted = cookies.OfType<Cookie>().OrderBy(i => i.Name).ToList();
  106. foreach (var cookie in sorted)
  107. headers.Add("Set-Cookie", cookie.ToString());
  108. }
  109. public override string ToString()
  110. {
  111. var output = new StringBuilder(64);
  112. output.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf);
  113. var headers = Headers;
  114. foreach (var key in headers.Keys)
  115. output.AppendFormat("{0}: {1}{2}", key, headers[key], CrLf);
  116. output.Append(CrLf);
  117. var entity = EntityBody;
  118. if (entity.Length > 0)
  119. output.Append(entity);
  120. return output.ToString();
  121. }
  122. #endregion
  123. }
  124. }