HttpResponse.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 => GetCookies(Headers, true);
  38. private static CookieCollection GetCookies(QueryParamCollection headers, bool response)
  39. {
  40. var name = response ? "Set-Cookie" : "Cookie";
  41. return headers == null || !headers.Contains(name)
  42. ? new CookieCollection()
  43. : CookieHelper.Parse(headers[name], response);
  44. }
  45. public bool IsProxyAuthenticationRequired => _code == "407";
  46. public bool IsUnauthorized => _code == "401";
  47. public bool IsWebSocketResponse
  48. {
  49. get
  50. {
  51. var headers = Headers;
  52. return ProtocolVersion > HttpVersion.Version10 &&
  53. _code == "101" &&
  54. headers.Contains("Upgrade", "websocket") &&
  55. headers.Contains("Connection", "Upgrade");
  56. }
  57. }
  58. public string Reason => _reason;
  59. public string StatusCode => _code;
  60. #endregion
  61. #region Internal Methods
  62. internal static HttpResponse CreateCloseResponse(HttpStatusCode code)
  63. {
  64. var res = new HttpResponse(code);
  65. res.Headers["Connection"] = "close";
  66. return res;
  67. }
  68. #endregion
  69. #region Public Methods
  70. public void SetCookies(CookieCollection cookies)
  71. {
  72. if (cookies == null || cookies.Count == 0)
  73. return;
  74. var headers = Headers;
  75. var sorted = cookies.OfType<Cookie>().OrderBy(i => i.Name).ToList();
  76. foreach (var cookie in sorted)
  77. headers.Add("Set-Cookie", cookie.ToString());
  78. }
  79. public override string ToString()
  80. {
  81. var output = new StringBuilder(64);
  82. output.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf);
  83. var headers = Headers;
  84. foreach (var key in headers.Keys)
  85. output.AppendFormat("{0}: {1}{2}", key, headers[key], CrLf);
  86. output.Append(CrLf);
  87. var entity = EntityBody;
  88. if (entity.Length > 0)
  89. output.Append(entity);
  90. return output.ToString();
  91. }
  92. #endregion
  93. }
  94. }