HttpResponse.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. namespace SocketHttpListener
  8. {
  9. // TODO what is the point of this class?
  10. internal class HttpResponse : HttpBase
  11. {
  12. #region Private Fields
  13. private string _code;
  14. private string _reason;
  15. #endregion
  16. #region Private Constructors
  17. private HttpResponse(string code, string reason, Version version, QueryParamCollection headers)
  18. : base(version, headers)
  19. {
  20. _code = code;
  21. _reason = reason;
  22. }
  23. #endregion
  24. #region Internal Constructors
  25. internal HttpResponse(HttpStatusCode code)
  26. : this(code, code.GetDescription())
  27. {
  28. }
  29. internal HttpResponse(HttpStatusCode code, string reason)
  30. : this(((int)code).ToString(), reason, HttpVersion.Version11, new QueryParamCollection())
  31. {
  32. Headers["Server"] = "websocket-sharp/1.0";
  33. }
  34. #endregion
  35. #region Public Properties
  36. public CookieCollection Cookies => GetCookies(Headers, true);
  37. private static CookieCollection GetCookies(QueryParamCollection headers, bool response)
  38. {
  39. var name = response ? "Set-Cookie" : "Cookie";
  40. return headers == null || !headers.Contains(name)
  41. ? new CookieCollection()
  42. : CookieHelper.Parse(headers[name], response);
  43. }
  44. public bool IsProxyAuthenticationRequired => _code == "407";
  45. public bool IsUnauthorized => _code == "401";
  46. public bool IsWebSocketResponse
  47. {
  48. get
  49. {
  50. var headers = Headers;
  51. return ProtocolVersion > HttpVersion.Version10 &&
  52. _code == "101" &&
  53. headers.Contains("Upgrade", "websocket") &&
  54. headers.Contains("Connection", "Upgrade");
  55. }
  56. }
  57. public string Reason => _reason;
  58. public string StatusCode => _code;
  59. #endregion
  60. #region Internal Methods
  61. internal static HttpResponse CreateCloseResponse(HttpStatusCode code)
  62. {
  63. var res = new HttpResponse(code);
  64. res.Headers["Connection"] = "close";
  65. return res;
  66. }
  67. #endregion
  68. #region Public Methods
  69. public void SetCookies(CookieCollection cookies)
  70. {
  71. if (cookies == null || cookies.Count == 0)
  72. return;
  73. var headers = Headers;
  74. var sorted = cookies.OfType<Cookie>().OrderBy(i => i.Name).ToList();
  75. foreach (var cookie in sorted)
  76. headers.Add("Set-Cookie", cookie.ToString());
  77. }
  78. public override string ToString()
  79. {
  80. var output = new StringBuilder(64);
  81. output.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, _code, _reason, CrLf);
  82. var headers = Headers;
  83. foreach (var key in headers.Keys)
  84. output.AppendFormat("{0}: {1}{2}", key, headers[key], CrLf);
  85. output.Append(CrLf);
  86. return output.ToString();
  87. }
  88. #endregion
  89. }
  90. }