HTTPConnection.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. if (this.connector.Client.Available > 0)
  57. {
  58. TlsStream stream = (this.connector.Stream as TlsStream);
  59. if (stream != null)
  60. {
  61. try
  62. {
  63. var available = stream.Protocol.TestApplicationData();
  64. return !stream.Protocol.IsClosed;
  65. }
  66. catch {
  67. return false;
  68. }
  69. }
  70. }
  71. #endif
  72. bool connected = this.connector.Client.Connected;
  73. return connected;
  74. }
  75. catch
  76. {
  77. return false;
  78. }
  79. #else
  80. return base.TestConnection();
  81. #endif
  82. }
  83. internal override void Process(HTTPRequest request)
  84. {
  85. this.LastProcessedUri = request.CurrentUri;
  86. if (this.requestHandler == null || !this.requestHandler.HasCustomRequestProcessor)
  87. base.Process(request);
  88. else
  89. {
  90. this.requestHandler.Process(request);
  91. LastProcessTime = DateTime.Now;
  92. }
  93. }
  94. protected override void ThreadFunc()
  95. {
  96. if (this.CurrentRequest.IsRedirected)
  97. this.CurrentRequest.Timing.Add(TimingEventNames.Queued_For_Redirection);
  98. else
  99. this.CurrentRequest.Timing.Add(TimingEventNames.Queued);
  100. if (this.connector != null && !this.connector.IsConnected)
  101. {
  102. // this will send the request back to the queue
  103. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(CurrentRequest, RequestEvents.Resend));
  104. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HTTPConnectionStates.Closed));
  105. return;
  106. }
  107. if (this.connector == null)
  108. {
  109. this.connector = new Connections.TCPConnector();
  110. try
  111. {
  112. this.connector.Connect(this.CurrentRequest);
  113. }
  114. catch(Exception ex)
  115. {
  116. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  117. HTTPManager.Logger.Exception("HTTPConnection", "Connector.Connect", ex, this.Context, this.CurrentRequest.Context);
  118. if (ex is TimeoutException)
  119. this.CurrentRequest.State = HTTPRequestStates.ConnectionTimedOut;
  120. else if (!this.CurrentRequest.IsTimedOut) // Do nothing here if Abort() got called on the request, its State is already set.
  121. {
  122. this.CurrentRequest.Exception = ex;
  123. this.CurrentRequest.State = HTTPRequestStates.Error;
  124. }
  125. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HTTPConnectionStates.Closed));
  126. return;
  127. }
  128. #if !NETFX_CORE
  129. // data sending is buffered for all protocols, so when we put data into the socket we want to send them asap
  130. this.connector.Client.NoDelay = true;
  131. #endif
  132. StartTime = DateTime.UtcNow;
  133. HTTPManager.Logger.Information("HTTPConnection", "Negotiated protocol through ALPN: '" + this.connector.NegotiatedProtocol + "'", this.Context, this.CurrentRequest.Context);
  134. switch (this.connector.NegotiatedProtocol)
  135. {
  136. case HTTPProtocolFactory.W3C_HTTP1:
  137. this.requestHandler = new Connections.HTTP1Handler(this);
  138. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HostProtocolSupport.HTTP1));
  139. break;
  140. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
  141. case HTTPProtocolFactory.W3C_HTTP2:
  142. this.requestHandler = new Connections.HTTP2.HTTP2Handler(this);
  143. this.CurrentRequest = null;
  144. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HostProtocolSupport.HTTP2));
  145. break;
  146. #endif
  147. default:
  148. HTTPManager.Logger.Error("HTTPConnection", "Unknown negotiated protocol: " + this.connector.NegotiatedProtocol, this.Context, this.CurrentRequest.Context);
  149. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(CurrentRequest, RequestEvents.Resend));
  150. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this, HTTPConnectionStates.Closed));
  151. return;
  152. }
  153. }
  154. this.requestHandler.Context.Add("Connection", this.GetHashCode());
  155. this.Context.Add("RequestHandler", this.requestHandler.GetHashCode());
  156. this.requestHandler.RunHandler();
  157. LastProcessTime = DateTime.Now;
  158. }
  159. public override void Shutdown(ShutdownTypes type)
  160. {
  161. base.Shutdown(type);
  162. if (this.requestHandler != null)
  163. this.requestHandler.Shutdown(type);
  164. switch(this.ShutdownType)
  165. {
  166. case ShutdownTypes.Immediate:
  167. this.connector.Dispose();
  168. break;
  169. }
  170. }
  171. protected override void Dispose(bool disposing)
  172. {
  173. if (disposing)
  174. {
  175. LastProcessedUri = null;
  176. if (this.State != HTTPConnectionStates.WaitForProtocolShutdown)
  177. {
  178. if (this.connector != null)
  179. {
  180. try
  181. {
  182. this.connector.Close();
  183. }
  184. catch
  185. { }
  186. this.connector = null;
  187. }
  188. if (this.requestHandler != null)
  189. {
  190. try
  191. {
  192. this.requestHandler.Dispose();
  193. }
  194. catch
  195. { }
  196. this.requestHandler = null;
  197. }
  198. }
  199. else
  200. {
  201. // We have to connector to do not close its stream at any cost while disposing.
  202. // All references to this connection will be removed, so this and the connector may be finalized after some time.
  203. // But, finalizing (and disposing) the connector while the protocol is still active would be fatal,
  204. // so we have to make sure that it will not happen. This also means that the protocol has the responsibility (as always had)
  205. // to close the stream and TCP connection properly.
  206. if (this.connector != null)
  207. this.connector.LeaveOpen = true;
  208. }
  209. }
  210. base.Dispose(disposing);
  211. }
  212. }
  213. }
  214. #endif