/Assets/SocketIO/WebsocketSharp/HandshakeRequest.cs

https://gitlab.com/kbc14a06/BenchmarkProject · C# · 179 lines · 118 code · 36 blank · 25 comment · 14 complexity · 94e5d0a4841ba803a7518130c458f05e MD5 · raw file

  1. #region License
  2. /*
  3. * HandshakeRequest.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.Text;
  31. using WebSocketSharp.Net;
  32. namespace WebSocketSharp
  33. {
  34. internal class HandshakeRequest : HandshakeBase
  35. {
  36. #region Private Fields
  37. private string _method;
  38. private string _uri;
  39. private bool _websocketRequest;
  40. private bool _websocketRequestWasSet;
  41. #endregion
  42. #region Private Constructors
  43. private HandshakeRequest (Version version, NameValueCollection headers)
  44. : base (version, headers)
  45. {
  46. }
  47. #endregion
  48. #region Internal Constructors
  49. internal HandshakeRequest (string pathAndQuery)
  50. : base (HttpVersion.Version11, new NameValueCollection ())
  51. {
  52. _uri = pathAndQuery;
  53. _method = "GET";
  54. var headers = Headers;
  55. headers["User-Agent"] = "websocket-sharp/1.0";
  56. headers["Upgrade"] = "websocket";
  57. headers["Connection"] = "Upgrade";
  58. }
  59. #endregion
  60. #region Public Properties
  61. public AuthenticationResponse AuthResponse {
  62. get {
  63. var auth = Headers["Authorization"];
  64. return auth != null && auth.Length > 0
  65. ? AuthenticationResponse.Parse (auth)
  66. : null;
  67. }
  68. }
  69. public CookieCollection Cookies {
  70. get {
  71. return Headers.GetCookies (false);
  72. }
  73. }
  74. public string HttpMethod {
  75. get {
  76. return _method;
  77. }
  78. }
  79. public bool IsWebSocketRequest {
  80. get {
  81. if (!_websocketRequestWasSet) {
  82. var headers = Headers;
  83. _websocketRequest = _method == "GET" &&
  84. ProtocolVersion > HttpVersion.Version10 &&
  85. headers.Contains ("Upgrade", "websocket") &&
  86. headers.Contains ("Connection", "Upgrade");
  87. _websocketRequestWasSet = true;
  88. }
  89. return _websocketRequest;
  90. }
  91. }
  92. public string RequestUri {
  93. get {
  94. return _uri;
  95. }
  96. }
  97. #endregion
  98. #region Internal Methods
  99. internal static HandshakeRequest Parse (string[] headerParts)
  100. {
  101. var requestLine = headerParts[0].Split (new[] { ' ' }, 3);
  102. if (requestLine.Length != 3)
  103. throw new ArgumentException ("Invalid request line: " + headerParts[0]);
  104. var headers = new WebHeaderCollection ();
  105. for (int i = 1; i < headerParts.Length; i++)
  106. headers.SetInternally (headerParts[i], false);
  107. var req = new HandshakeRequest (new Version (requestLine[2].Substring (5)), headers);
  108. req._method = requestLine[0];
  109. req._uri = requestLine[1];
  110. return req;
  111. }
  112. #endregion
  113. #region Public Methods
  114. public void SetCookies (CookieCollection cookies)
  115. {
  116. if (cookies == null || cookies.Count == 0)
  117. return;
  118. var buff = new StringBuilder (64);
  119. foreach (var cookie in cookies.Sorted)
  120. if (!cookie.Expired)
  121. buff.AppendFormat ("{0}; ", cookie.ToString ());
  122. var len = buff.Length;
  123. if (len > 2) {
  124. buff.Length = len - 2;
  125. Headers["Cookie"] = buff.ToString ();
  126. }
  127. }
  128. public override string ToString ()
  129. {
  130. var output = new StringBuilder (64);
  131. output.AppendFormat ("{0} {1} HTTP/{2}{3}", _method, _uri, ProtocolVersion, CrLf);
  132. var headers = Headers;
  133. foreach (var key in headers.AllKeys)
  134. output.AppendFormat ("{0}: {1}{2}", key, headers[key], CrLf);
  135. output.Append (CrLf);
  136. var entity = EntityBody;
  137. if (entity.Length > 0)
  138. output.Append (entity);
  139. return output.ToString ();
  140. }
  141. #endregion
  142. }
  143. }