HTTPConnection.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #if !UNITY_WEBGL || UNITY_EDITOR
  2. using System;
  3. #if !BESTHTTP_DISABLE_ALTERNATE_SSL
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Tls;
  5. #endif
  6. using BestHTTP.Core;
  7. using BestHTTP.Timings;
  8. namespace BestHTTP.Connections
  9. {
  10. /// <summary>
  11. /// Represents and manages a connection to a server.
  12. /// </summary>
  13. public sealed class HTTPConnection : ConnectionBase
  14. {
  15. public TCPConnector connector;
  16. public IHTTPRequestHandler requestHandler;
  17. public override TimeSpan KeepAliveTime {
  18. get {
  19. if (this.requestHandler != null && this.requestHandler.KeepAlive != null)
  20. {
  21. if (this.requestHandler.KeepAlive.MaxRequests > 0)
  22. {
  23. if (base.KeepAliveTime < this.requestHandler.KeepAlive.TimeOut)
  24. return base.KeepAliveTime;
  25. else
  26. return this.requestHandler.KeepAlive.TimeOut;
  27. }
  28. else
  29. return TimeSpan.Zero;
  30. }
  31. return base.KeepAliveTime;
  32. }
  33. protected set
  34. {
  35. base.KeepAliveTime = value;
  36. }
  37. }
  38. public override bool CanProcessMultiple
  39. {
  40. get
  41. {
  42. if (this.requestHandler != null)
  43. return this.requestHandler.CanProcessMultiple;
  44. return base.CanProcessMultiple;
  45. }
  46. }
  47. internal HTTPConnection(string serverAddress)
  48. :base(serverAddress)
  49. {}
  50. public override bool TestConnection()
  51. {
  52. #if !NETFX_CORE
  53. try
  54. {
  55. #if !BESTHTTP_DISABLE_ALTERNATE_SSL
  56. TlsStream stream = (this.connector?.Stream as TlsStream);
  57. if (stream != null && stream.Protocol != null)
  58. {
  59. bool locked = stream.Protocol.TryEnterApplicationDataLock(0);
  60. try
  61. {
  62. if (locked && this.connector.Client.Available > 0)
  63. {
  64. try
  65. {
  66. var available = stream.Protocol.TestApplicationData();
  67. return !stream.Protocol.IsClosed;
  68. }
  69. catch
  70. {
  71. return false;
  72. }
  73. }
  74. }
  75. finally
  76. {
  77. if (locked)
  78. stream.Protocol.ExitApplicationDataLock();
  79. }
  80. }
  81. #endif
  82. bool connected = this.connector.Client.Connected;
  83. return connected;
  84. }
  85. catch
  86. {
  87. return false;
  88. }
  89. #else
  90. return base.TestConnection();
  91. #endif
  92. }
  93. internal override void Process(HTTPRequest request)
  94. {
  95. this.LastProcessedUri = request.CurrentUri;
  96. if (this.requestHandler == null || !this.requestHandler.HasCustomRequestProcessor)
  97. base.Process(request);
  98. else
  99. {
  100. this.requestHandler.Process(request);
  101. LastProcessTime = DateTime.Now;
  102. }
  103. }
  104. protected override void ThreadFunc()
  105. {
  106. if (this.CurrentRequest.IsRedirected)
  107. this.CurrentRequest.Timing.Add(TimingEventNames.Queued_For_Redirection);
  108. else
  109. this.CurrentRequest.Timing.Add(TimingEventNames.Queued);
  110. if (this.connector != null && !this.connector.IsConnected)
  111. {
  112. // this will send the request back to the queue
  113. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(CurrentRequest, RequestEvents.Resend));
  114. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HTTPConnectionStates.Closed));
  115. return;
  116. }
  117. if (this.connector == null)
  118. {
  119. this.connector = new Connections.TCPConnector();
  120. try
  121. {
  122. this.connector.Connect(this.CurrentRequest);
  123. }
  124. catch(Exception ex)
  125. {
  126. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  127. HTTPManager.Logger.Exception("HTTPConnection", "Connector.Connect", ex, this.Context, this.CurrentRequest.Context);
  128. if (ex is TimeoutException)
  129. this.CurrentRequest.State = HTTPRequestStates.ConnectionTimedOut;
  130. else if (!this.CurrentRequest.IsTimedOut) // Do nothing here if Abort() got called on the request, its State is already set.
  131. {
  132. this.CurrentRequest.Exception = ex;
  133. this.CurrentRequest.State = HTTPRequestStates.Error;
  134. }
  135. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HTTPConnectionStates.Closed));
  136. return;
  137. }
  138. #if !NETFX_CORE
  139. // data sending is buffered for all protocols, so when we put data into the socket we want to send them asap
  140. this.connector.Client.NoDelay = true;
  141. #endif
  142. StartTime = DateTime.UtcNow;
  143. HTTPManager.Logger.Information("HTTPConnection", "Negotiated protocol through ALPN: '" + this.connector.NegotiatedProtocol + "'", this.Context, this.CurrentRequest.Context);
  144. switch (this.connector.NegotiatedProtocol)
  145. {
  146. case HTTPProtocolFactory.W3C_HTTP1:
  147. this.requestHandler = new Connections.HTTP1Handler(this);
  148. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HostProtocolSupport.HTTP1));
  149. break;
  150. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
  151. case HTTPProtocolFactory.W3C_HTTP2:
  152. this.requestHandler = new Connections.HTTP2.HTTP2Handler(this);
  153. this.CurrentRequest = null;
  154. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HostProtocolSupport.HTTP2));
  155. break;
  156. #endif
  157. default:
  158. HTTPManager.Logger.Error("HTTPConnection", "Unknown negotiated protocol: " + this.connector.NegotiatedProtocol, this.Context, this.CurrentRequest.Context);
  159. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(CurrentRequest, RequestEvents.Resend));
  160. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HTTPConnectionStates.Closed));
  161. return;
  162. }
  163. }
  164. this.requestHandler.Context.Add("Connection", this.GetHashCode());
  165. this.Context.Add("RequestHandler", this.requestHandler.GetHashCode());
  166. this.requestHandler.RunHandler();
  167. LastProcessTime = DateTime.Now;
  168. }
  169. public override void Shutdown(ShutdownTypes type)
  170. {
  171. base.Shutdown(type);
  172. if (this.requestHandler != null)
  173. this.requestHandler.Shutdown(type);
  174. switch(this.ShutdownType)
  175. {
  176. case ShutdownTypes.Immediate:
  177. this.connector.Dispose();
  178. break;
  179. }
  180. }
  181. protected override void Dispose(bool disposing)
  182. {
  183. if (disposing)
  184. {
  185. LastProcessedUri = null;
  186. if (this.State != HTTPConnectionStates.WaitForProtocolShutdown)
  187. {
  188. if (this.connector != null)
  189. {
  190. try
  191. {
  192. this.connector.Close();
  193. }
  194. catch
  195. { }
  196. this.connector = null;
  197. }
  198. if (this.requestHandler != null)
  199. {
  200. try
  201. {
  202. this.requestHandler.Dispose();
  203. }
  204. catch
  205. { }
  206. this.requestHandler = null;
  207. }
  208. }
  209. else
  210. {
  211. // We have to connector to do not close its stream at any cost while disposing.
  212. // All references to this connection will be removed, so this and the connector may be finalized after some time.
  213. // But, finalizing (and disposing) the connector while the protocol is still active would be fatal,
  214. // so we have to make sure that it will not happen. This also means that the protocol has the responsibility (as always had)
  215. // to close the stream and TCP connection properly.
  216. if (this.connector != null)
  217. this.connector.LeaveOpen = true;
  218. }
  219. }
  220. base.Dispose(disposing);
  221. }
  222. }
  223. }
  224. #endif