ResponseHandshake.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #region MIT License
  2. /**
  3. * ResponseHandshake.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2012 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.Text;
  31. using WebSocketSharp.Net;
  32. namespace WebSocketSharp
  33. {
  34. public class ResponseHandshake : Handshake
  35. {
  36. #region Constructor
  37. public ResponseHandshake()
  38. : this(HttpStatusCode.SwitchingProtocols)
  39. {
  40. AddHeader("Upgrade", "websocket");
  41. AddHeader("Connection", "Upgrade");
  42. }
  43. public ResponseHandshake(HttpStatusCode code)
  44. {
  45. StatusCode = ((int)code).ToString();
  46. Reason = Ext.GetDescription(code);
  47. }
  48. #endregion
  49. #region Properties
  50. public bool IsWebSocketResponse
  51. {
  52. get
  53. {
  54. if (ProtocolVersion != HttpVersion.Version11)
  55. return false;
  56. if (StatusCode != "101")
  57. return false;
  58. if (!HeaderExists("Upgrade", "websocket"))
  59. return false;
  60. if (!HeaderExists("Connection", "Upgrade"))
  61. return false;
  62. if (!HeaderExists("Sec-WebSocket-Accept"))
  63. return false;
  64. return true;
  65. }
  66. }
  67. public string Reason { get; internal set; }
  68. public string StatusCode { get; internal set; }
  69. #endregion
  70. #region Methods
  71. public static ResponseHandshake CreateCloseResponse(HttpStatusCode code)
  72. {
  73. var res = new ResponseHandshake(code);
  74. res.AddHeader("Connection", "close");
  75. return res;
  76. }
  77. public static ResponseHandshake Parse(string[] response)
  78. {
  79. var statusLine = response[0].Split(' ');
  80. if (statusLine.Length < 3)
  81. throw new ArgumentException("Invalid status line.");
  82. var reason = new StringBuilder(statusLine[2]);
  83. for (int i = 3; i < statusLine.Length; i++)
  84. reason.AppendFormat(" {0}", statusLine[i]);
  85. var headers = new WebHeaderCollection();
  86. for (int i = 1; i < response.Length; i++)
  87. headers.Add(response[i]);
  88. return new ResponseHandshake
  89. {
  90. Headers = headers,
  91. Reason = reason.ToString(),
  92. StatusCode = statusLine[1],
  93. ProtocolVersion = new Version(statusLine[0].Substring(5))
  94. };
  95. }
  96. public override string ToString()
  97. {
  98. var buffer = new StringBuilder();
  99. buffer.AppendFormat("HTTP/{0} {1} {2}{3}", ProtocolVersion, StatusCode, Reason, _crlf);
  100. foreach (string key in Headers.AllKeys)
  101. buffer.AppendFormat("{0}: {1}{2}", key, Headers[key], _crlf);
  102. buffer.Append(_crlf);
  103. return buffer.ToString();
  104. }
  105. #endregion
  106. }
  107. }