OverHTTP1.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_WEBSOCKET
  2. using System;
  3. using BestHTTP.Connections;
  4. using BestHTTP.Extensions;
  5. using BestHTTP.WebSocket.Frames;
  6. namespace BestHTTP.WebSocket
  7. {
  8. internal sealed class OverHTTP1 : WebSocketBaseImplementation
  9. {
  10. public override bool IsOpen => webSocket != null && !webSocket.IsClosed;
  11. public override int BufferedAmount => webSocket.BufferedAmount;
  12. public override int Latency => this.webSocket.Latency;
  13. public override DateTime LastMessageReceived => this.webSocket.lastMessage;
  14. /// <summary>
  15. /// Indicates whether we sent out the connection request to the server.
  16. /// </summary>
  17. private bool requestSent;
  18. /// <summary>
  19. /// The internal WebSocketResponse object
  20. /// </summary>
  21. private WebSocketResponse webSocket;
  22. public OverHTTP1(WebSocket parent, Uri uri, string origin, string protocol) : base(parent, uri, origin, protocol)
  23. {
  24. string scheme = HTTPProtocolFactory.IsSecureProtocol(uri) ? "wss" : "ws";
  25. int port = uri.Port != -1 ? uri.Port : (scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) ? 443 : 80);
  26. // Somehow if i use the UriBuilder it's not the same as if the uri is constructed from a string...
  27. //uri = new UriBuilder(uri.Scheme, uri.Host, uri.Scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) ? 443 : 80, uri.PathAndQuery).Uri;
  28. base.Uri = new Uri(scheme + "://" + uri.Host + ":" + port + uri.GetRequestPathAndQueryURL());
  29. }
  30. protected override void CreateInternalRequest()
  31. {
  32. if (this._internalRequest != null)
  33. return;
  34. this._internalRequest = new HTTPRequest(base.Uri, OnInternalRequestCallback);
  35. this._internalRequest.Context.Add("WebSocket", this.Parent.Context);
  36. // Called when the regular GET request is successfully upgraded to WebSocket
  37. this._internalRequest.OnUpgraded = OnInternalRequestUpgraded;
  38. //http://tools.ietf.org/html/rfc6455#section-4
  39. // The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword.
  40. this._internalRequest.SetHeader("Upgrade", "websocket");
  41. // The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token.
  42. this._internalRequest.SetHeader("Connection", "Upgrade");
  43. // The request MUST include a header field with the name |Sec-WebSocket-Key|. The value of this header field MUST be a nonce consisting of a
  44. // randomly selected 16-byte value that has been base64-encoded (see Section 4 of [RFC4648]). The nonce MUST be selected randomly for each connection.
  45. this._internalRequest.SetHeader("Sec-WebSocket-Key", WebSocket.GetSecKey(new object[] { this, InternalRequest, base.Uri, new object() }));
  46. // The request MUST include a header field with the name |Origin| [RFC6454] if the request is coming from a browser client.
  47. // If the connection is from a non-browser client, the request MAY include this header field if the semantics of that client match the use-case described here for browser clients.
  48. // More on Origin Considerations: http://tools.ietf.org/html/rfc6455#section-10.2
  49. if (!string.IsNullOrEmpty(Origin))
  50. this._internalRequest.SetHeader("Origin", Origin);
  51. // The request MUST include a header field with the name |Sec-WebSocket-Version|. The value of this header field MUST be 13.
  52. this._internalRequest.SetHeader("Sec-WebSocket-Version", "13");
  53. if (!string.IsNullOrEmpty(Protocol))
  54. this._internalRequest.SetHeader("Sec-WebSocket-Protocol", Protocol);
  55. // Disable caching
  56. this._internalRequest.SetHeader("Cache-Control", "no-cache");
  57. this._internalRequest.SetHeader("Pragma", "no-cache");
  58. #if !BESTHTTP_DISABLE_CACHING
  59. this._internalRequest.DisableCache = true;
  60. #endif
  61. #if !BESTHTTP_DISABLE_PROXY
  62. this._internalRequest.Proxy = this.Parent.GetProxy(this.Uri);
  63. #endif
  64. if (this.Parent.OnInternalRequestCreated != null)
  65. {
  66. try
  67. {
  68. this.Parent.OnInternalRequestCreated(this.Parent, this._internalRequest);
  69. }
  70. catch (Exception ex)
  71. {
  72. HTTPManager.Logger.Exception("OverHTTP1", "CreateInternalRequest", ex, this.Parent.Context);
  73. }
  74. }
  75. }
  76. public override void StartClose(UInt16 code, string message)
  77. {
  78. if (this.State == WebSocketStates.Connecting)
  79. {
  80. if (this.InternalRequest != null)
  81. this.InternalRequest.Abort();
  82. this.State = WebSocketStates.Closed;
  83. if (this.Parent.OnClosed != null)
  84. this.Parent.OnClosed(this.Parent, (ushort)WebSocketStausCodes.NoStatusCode, string.Empty);
  85. }
  86. else
  87. {
  88. this.State = WebSocketStates.Closing;
  89. webSocket.Close(code, message);
  90. }
  91. }
  92. public override void StartOpen()
  93. {
  94. if (requestSent)
  95. throw new InvalidOperationException("Open already called! You can't reuse this WebSocket instance!");
  96. if (this.Parent.Extensions != null)
  97. {
  98. try
  99. {
  100. for (int i = 0; i < this.Parent.Extensions.Length; ++i)
  101. {
  102. var ext = this.Parent.Extensions[i];
  103. if (ext != null)
  104. ext.AddNegotiation(InternalRequest);
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. HTTPManager.Logger.Exception("OverHTTP1", "Open", ex, this.Parent.Context);
  110. }
  111. }
  112. InternalRequest.Send();
  113. requestSent = true;
  114. this.State = WebSocketStates.Connecting;
  115. }
  116. private void OnInternalRequestCallback(HTTPRequest req, HTTPResponse resp)
  117. {
  118. string reason = string.Empty;
  119. switch (req.State)
  120. {
  121. case HTTPRequestStates.Finished:
  122. HTTPManager.Logger.Information("OverHTTP1", string.Format("Request finished. Status Code: {0} Message: {1}", resp.StatusCode.ToString(), resp.Message), this.Parent.Context);
  123. if (resp.StatusCode == 101)
  124. {
  125. // The request upgraded successfully.
  126. return;
  127. }
  128. else
  129. reason = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
  130. resp.StatusCode,
  131. resp.Message,
  132. resp.DataAsText);
  133. break;
  134. // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
  135. case HTTPRequestStates.Error:
  136. reason = "Request Finished with Error! " + (req.Exception != null ? ("Exception: " + req.Exception.Message + req.Exception.StackTrace) : string.Empty);
  137. break;
  138. // The request aborted, initiated by the user.
  139. case HTTPRequestStates.Aborted:
  140. reason = "Request Aborted!";
  141. break;
  142. // Connecting to the server is timed out.
  143. case HTTPRequestStates.ConnectionTimedOut:
  144. reason = "Connection Timed Out!";
  145. break;
  146. // The request didn't finished in the given time.
  147. case HTTPRequestStates.TimedOut:
  148. reason = "Processing the request Timed Out!";
  149. break;
  150. default:
  151. return;
  152. }
  153. if (this.State != WebSocketStates.Connecting || !string.IsNullOrEmpty(reason))
  154. {
  155. if (this.Parent.OnError != null)
  156. this.Parent.OnError(this.Parent, reason);
  157. else if (!HTTPManager.IsQuitting)
  158. HTTPManager.Logger.Error("OverHTTP1", reason, this.Parent.Context);
  159. }
  160. else if (this.Parent.OnClosed != null)
  161. this.Parent.OnClosed(this.Parent, (ushort)WebSocketStausCodes.NormalClosure, "Closed while opening");
  162. this.State = WebSocketStates.Closed;
  163. if (!req.IsKeepAlive && resp != null && resp is WebSocketResponse)
  164. (resp as WebSocketResponse).CloseStream();
  165. }
  166. private void OnInternalRequestUpgraded(HTTPRequest req, HTTPResponse resp)
  167. {
  168. HTTPManager.Logger.Information("OverHTTP1", "Internal request upgraded!", this.Parent.Context);
  169. webSocket = resp as WebSocketResponse;
  170. if (webSocket == null)
  171. {
  172. if (this.Parent.OnError != null)
  173. {
  174. string reason = string.Empty;
  175. if (req.Exception != null)
  176. reason = req.Exception.Message + " " + req.Exception.StackTrace;
  177. this.Parent.OnError(this.Parent, reason);
  178. }
  179. this.State = WebSocketStates.Closed;
  180. return;
  181. }
  182. // If Close called while we connected
  183. if (this.State == WebSocketStates.Closed)
  184. {
  185. webSocket.CloseStream();
  186. return;
  187. }
  188. if (!resp.HasHeader("sec-websocket-accept"))
  189. {
  190. this.State = WebSocketStates.Closed;
  191. webSocket.CloseStream();
  192. if (this.Parent.OnError != null)
  193. this.Parent.OnError(this.Parent, "No Sec-Websocket-Accept header is sent by the server!");
  194. return;
  195. }
  196. webSocket.WebSocket = this.Parent;
  197. if (this.Parent.Extensions != null)
  198. {
  199. for (int i = 0; i < this.Parent.Extensions.Length; ++i)
  200. {
  201. var ext = this.Parent.Extensions[i];
  202. try
  203. {
  204. if (ext != null && !ext.ParseNegotiation(webSocket))
  205. this.Parent.Extensions[i] = null; // Keep extensions only that successfully negotiated
  206. }
  207. catch (Exception ex)
  208. {
  209. HTTPManager.Logger.Exception("OverHTTP1", "ParseNegotiation", ex, this.Parent.Context);
  210. // Do not try to use a defective extension in the future
  211. this.Parent.Extensions[i] = null;
  212. }
  213. }
  214. }
  215. this.State = WebSocketStates.Open;
  216. if (this.Parent.OnOpen != null)
  217. {
  218. try
  219. {
  220. this.Parent.OnOpen(this.Parent);
  221. }
  222. catch (Exception ex)
  223. {
  224. HTTPManager.Logger.Exception("OverHTTP1", "OnOpen", ex, this.Parent.Context);
  225. }
  226. }
  227. webSocket.OnText = (ws, msg) =>
  228. {
  229. if (this.Parent.OnMessage != null)
  230. this.Parent.OnMessage(this.Parent, msg);
  231. };
  232. webSocket.OnBinary = (ws, bin) =>
  233. {
  234. if (this.Parent.OnBinary != null)
  235. this.Parent.OnBinary(this.Parent, bin);
  236. };
  237. webSocket.OnClosed = (ws, code, msg) =>
  238. {
  239. this.State = WebSocketStates.Closed;
  240. if (this.Parent.OnClosed != null)
  241. this.Parent.OnClosed(this.Parent, code, msg);
  242. };
  243. if (this.Parent.OnIncompleteFrame != null)
  244. webSocket.OnIncompleteFrame = (ws, frame) =>
  245. {
  246. if (this.Parent.OnIncompleteFrame != null)
  247. this.Parent.OnIncompleteFrame(this.Parent, frame);
  248. };
  249. if (this.Parent.StartPingThread)
  250. webSocket.StartPinging(Math.Max(this.Parent.PingFrequency, 100));
  251. webSocket.StartReceive();
  252. }
  253. public override void Send(string message)
  254. {
  255. webSocket.Send(message);
  256. }
  257. public override void Send(byte[] buffer)
  258. {
  259. webSocket.Send(buffer);
  260. }
  261. public override void Send(byte[] buffer, ulong offset, ulong count)
  262. {
  263. webSocket.Send(buffer, offset, count);
  264. }
  265. public override void Send(WebSocketFrame frame)
  266. {
  267. webSocket.Send(frame);
  268. }
  269. }
  270. }
  271. #endif