ConnectionHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using System;
  2. using System.Collections.Generic;
  3. using BestHTTP.Authentication;
  4. using BestHTTP.Core;
  5. using BestHTTP.Extensions;
  6. #if !BESTHTTP_DISABLE_CACHING
  7. using BestHTTP.Caching;
  8. #endif
  9. #if !BESTHTTP_DISABLE_COOKIES
  10. using BestHTTP.Cookies;
  11. #endif
  12. using BestHTTP.Logger;
  13. using BestHTTP.Timings;
  14. namespace BestHTTP.Connections
  15. {
  16. /// <summary>
  17. /// https://tools.ietf.org/html/draft-thomson-hybi-http-timeout-03
  18. /// Test servers: http://tools.ietf.org/ http://nginx.org/
  19. /// </summary>
  20. public sealed class KeepAliveHeader
  21. {
  22. /// <summary>
  23. /// A host sets the value of the "timeout" parameter to the time that the host will allow an idle connection to remain open before it is closed. A connection is idle if no data is sent or received by a host.
  24. /// </summary>
  25. public TimeSpan TimeOut { get; private set; }
  26. /// <summary>
  27. /// The "max" parameter has been used to indicate the maximum number of requests that would be made on the connection.This parameter is deprecated.Any limit on requests can be enforced by sending "Connection: close" and closing the connection.
  28. /// </summary>
  29. public int MaxRequests { get; private set; }
  30. public void Parse(List<string> headerValues)
  31. {
  32. HeaderParser parser = new HeaderParser(headerValues[0]);
  33. HeaderValue value;
  34. if (parser.TryGet("timeout", out value) && value.HasValue)
  35. {
  36. int intValue = 0;
  37. if (int.TryParse(value.Value, out intValue) && intValue > 1)
  38. this.TimeOut = TimeSpan.FromSeconds(intValue - 1);
  39. else
  40. this.TimeOut = TimeSpan.MaxValue;
  41. }
  42. if (parser.TryGet("max", out value) && value.HasValue)
  43. {
  44. int intValue = 0;
  45. if (int.TryParse("max", out intValue))
  46. this.MaxRequests = intValue;
  47. else
  48. this.MaxRequests = int.MaxValue;
  49. }
  50. }
  51. }
  52. public static class ConnectionHelper
  53. {
  54. public static void HandleResponse(string context, HTTPRequest request, out bool resendRequest, out HTTPConnectionStates proposedConnectionState, ref KeepAliveHeader keepAlive, LoggingContext loggingContext1 = null, LoggingContext loggingContext2 = null, LoggingContext loggingContext3 = null)
  55. {
  56. resendRequest = false;
  57. proposedConnectionState = HTTPConnectionStates.Processing;
  58. if (request.Response != null)
  59. {
  60. #if !BESTHTTP_DISABLE_COOKIES
  61. // Try to store cookies before we do anything else, as we may remove the response deleting the cookies as well.
  62. if (request.IsCookiesEnabled)
  63. CookieJar.Set(request.Response);
  64. #endif
  65. switch (request.Response.StatusCode)
  66. {
  67. // Not authorized
  68. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2
  69. case 401:
  70. {
  71. string authHeader = DigestStore.FindBest(request.Response.GetHeaderValues("www-authenticate"));
  72. if (!string.IsNullOrEmpty(authHeader))
  73. {
  74. var digest = DigestStore.GetOrCreate(request.CurrentUri);
  75. digest.ParseChallange(authHeader);
  76. if (request.Credentials != null && digest.IsUriProtected(request.CurrentUri) && (!request.HasHeader("Authorization") || digest.Stale))
  77. resendRequest = true;
  78. }
  79. goto default;
  80. }
  81. case 407:
  82. {
  83. if (request.Proxy == null)
  84. goto default;
  85. resendRequest = request.Proxy.SetupRequest(request);
  86. goto default;
  87. }
  88. // Redirected
  89. case 301: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
  90. case 302: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3
  91. case 307: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8
  92. case 308: // http://tools.ietf.org/html/rfc7238
  93. {
  94. if (request.RedirectCount >= request.MaxRedirects)
  95. goto default;
  96. request.RedirectCount++;
  97. string location = request.Response.GetFirstHeaderValue("location");
  98. if (!string.IsNullOrEmpty(location))
  99. {
  100. Uri redirectUri = ConnectionHelper.GetRedirectUri(request, location);
  101. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  102. HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - Redirected to Location: '{1}' redirectUri: '{1}'", context, location, redirectUri), loggingContext1, loggingContext2, loggingContext3);
  103. if (redirectUri == request.CurrentUri)
  104. {
  105. HTTPManager.Logger.Information("HTTPConnection", string.Format("[{0}] - Redirected to the same location!", context), loggingContext1, loggingContext2, loggingContext3);
  106. goto default;
  107. }
  108. // Let the user to take some control over the redirection
  109. if (!request.CallOnBeforeRedirection(redirectUri))
  110. {
  111. HTTPManager.Logger.Information("HTTPConnection", string.Format("[{0}] OnBeforeRedirection returned False", context), loggingContext1, loggingContext2, loggingContext3);
  112. goto default;
  113. }
  114. // Remove the previously set Host header.
  115. request.RemoveHeader("Host");
  116. // Set the Referer header to the last Uri.
  117. request.SetHeader("Referer", request.CurrentUri.ToString());
  118. // Set the new Uri, the CurrentUri will return this while the IsRedirected property is true
  119. request.RedirectUri = redirectUri;
  120. request.IsRedirected = true;
  121. resendRequest = true;
  122. }
  123. else
  124. throw new Exception(string.Format("[{0}] Got redirect status({1}) without 'location' header!", context, request.Response.StatusCode.ToString()));
  125. goto default;
  126. }
  127. #if !BESTHTTP_DISABLE_CACHING
  128. case 304:
  129. if (request.DisableCache)
  130. break;
  131. if (ConnectionHelper.LoadFromCache(context, request, loggingContext1, loggingContext2, loggingContext3))
  132. {
  133. request.Timing.Add(TimingEventNames.Loading_From_Cache);
  134. HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - HandleResponse - Loaded from cache successfully!", context), loggingContext1, loggingContext2, loggingContext3);
  135. // Update any caching value
  136. HTTPCacheService.SetUpCachingValues(request.CurrentUri, request.Response);
  137. }
  138. else
  139. {
  140. HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - HandleResponse - Loaded from cache failed!", context), loggingContext1, loggingContext2, loggingContext3);
  141. resendRequest = true;
  142. }
  143. break;
  144. #endif
  145. default:
  146. #if !BESTHTTP_DISABLE_CACHING
  147. ConnectionHelper.TryStoreInCache(request);
  148. #endif
  149. break;
  150. }
  151. // Closing the stream is done manually?
  152. if (request.Response != null && !request.Response.IsClosedManually)
  153. {
  154. // If we have a response and the server telling us that it closed the connection after the message sent to us, then
  155. // we will close the connection too.
  156. bool closeByServer = request.Response.HasHeaderWithValue("connection", "close");
  157. bool closeByClient = !request.IsKeepAlive;
  158. if (closeByServer || closeByClient)
  159. {
  160. proposedConnectionState = HTTPConnectionStates.Closed;
  161. }
  162. else if (request.Response != null)
  163. {
  164. var keepAliveheaderValues = request.Response.GetHeaderValues("keep-alive");
  165. if (keepAliveheaderValues != null && keepAliveheaderValues.Count > 0)
  166. {
  167. if (keepAlive == null)
  168. keepAlive = new KeepAliveHeader();
  169. keepAlive.Parse(keepAliveheaderValues);
  170. }
  171. }
  172. }
  173. // Null out the response here instead of the redirected cases (301, 302, 307, 308)
  174. // because response might have a Connection: Close header that we would miss to process.
  175. // If Connection: Close is present, the server is closing the connection and we would
  176. // reuse that closed connection.
  177. if (resendRequest)
  178. {
  179. // Discard the redirect response, we don't need it any more
  180. request.Response = null;
  181. if (proposedConnectionState == HTTPConnectionStates.Closed)
  182. proposedConnectionState = HTTPConnectionStates.ClosedResendRequest;
  183. }
  184. }
  185. }
  186. #if !BESTHTTP_DISABLE_CACHING
  187. public static bool LoadFromCache(string context, HTTPRequest request, LoggingContext loggingContext1 = null, LoggingContext loggingContext2 = null, LoggingContext loggingContext3 = null)
  188. {
  189. if (request.IsRedirected)
  190. {
  191. if (LoadFromCache(context, request, request.RedirectUri, loggingContext1, loggingContext2, loggingContext3))
  192. return true;
  193. else
  194. {
  195. Caching.HTTPCacheService.DeleteEntity(request.RedirectUri);
  196. }
  197. }
  198. bool loaded = LoadFromCache(context, request, request.Uri, loggingContext1, loggingContext2, loggingContext3);
  199. if (!loaded)
  200. Caching.HTTPCacheService.DeleteEntity(request.Uri);
  201. return loaded;
  202. }
  203. private static bool LoadFromCache(string context, HTTPRequest request, Uri uri, LoggingContext loggingContext1 = null, LoggingContext loggingContext2 = null, LoggingContext loggingContext3 = null)
  204. {
  205. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  206. HTTPManager.Logger.Verbose("HTTPConnection", string.Format("[{0}] - LoadFromCache for Uri: {1}", context, uri.ToString()), loggingContext1, loggingContext2, loggingContext3);
  207. var cacheEntity = HTTPCacheService.GetEntity(uri);
  208. if (cacheEntity == null)
  209. {
  210. HTTPManager.Logger.Warning("HTTPConnection", string.Format("[{0}] - LoadFromCache for Uri: {1} - Cached entity not found!", context, uri.ToString()), loggingContext1, loggingContext2, loggingContext3);
  211. return false;
  212. }
  213. request.Response.CacheFileInfo = cacheEntity;
  214. try
  215. {
  216. int bodyLength;
  217. using (var cacheStream = cacheEntity.GetBodyStream(out bodyLength))
  218. {
  219. if (cacheStream == null)
  220. return false;
  221. if (!request.Response.HasHeader("content-length"))
  222. request.Response.AddHeader("content-length", bodyLength.ToString());
  223. request.Response.IsFromCache = true;
  224. if (!request.CacheOnly)
  225. request.Response.ReadRaw(cacheStream, bodyLength);
  226. }
  227. }
  228. catch
  229. {
  230. return false;
  231. }
  232. return true;
  233. }
  234. public static bool TryLoadAllFromCache(string context, HTTPRequest request, LoggingContext loggingContext1 = null, LoggingContext loggingContext2 = null, LoggingContext loggingContext3 = null)
  235. {
  236. // We will try to read the response from the cache, but if something happens we will fallback to the normal way.
  237. try
  238. {
  239. //Unless specifically constrained by a cache-control (section 14.9) directive, a caching system MAY always store a successful response (see section 13.8) as a cache entity,
  240. // MAY return it without validation if it is fresh, and MAY return it after successful validation.
  241. // MAY return it without validation if it is fresh!
  242. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  243. HTTPManager.Logger.Verbose("ConnectionHelper", string.Format("[{0}] - TryLoadAllFromCache - whole response loading from cache", context), loggingContext1, loggingContext2, loggingContext3);
  244. request.Response = HTTPCacheService.GetFullResponse(request);
  245. if (request.Response != null)
  246. return true;
  247. }
  248. catch
  249. {
  250. HTTPManager.Logger.Verbose("ConnectionHelper", string.Format("[{0}] - TryLoadAllFromCache - failed to load content!", context), loggingContext1, loggingContext2, loggingContext3);
  251. HTTPCacheService.DeleteEntity(request.CurrentUri);
  252. }
  253. return false;
  254. }
  255. public static void TryStoreInCache(HTTPRequest request)
  256. {
  257. // if UseStreaming && !DisableCache then we already wrote the response to the cache
  258. if (!request.UseStreaming &&
  259. !request.DisableCache &&
  260. request.Response != null &&
  261. HTTPCacheService.IsSupported &&
  262. HTTPCacheService.IsCacheble(request.CurrentUri, request.MethodType, request.Response))
  263. {
  264. if (request.IsRedirected)
  265. HTTPCacheService.Store(request.Uri, request.MethodType, request.Response);
  266. else
  267. HTTPCacheService.Store(request.CurrentUri, request.MethodType, request.Response);
  268. request.Timing.Add(TimingEventNames.Writing_To_Cache);
  269. PluginEventHelper.EnqueuePluginEvent(new PluginEventInfo(PluginEvents.SaveCacheLibrary));
  270. }
  271. }
  272. #endif
  273. public static Uri GetRedirectUri(HTTPRequest request, string location)
  274. {
  275. Uri result = null;
  276. try
  277. {
  278. result = new Uri(location);
  279. if (result.IsFile || result.AbsolutePath == location)
  280. result = null;
  281. }
  282. catch
  283. {
  284. // Sometimes the server sends back only the path and query component of the new uri
  285. result = null;
  286. }
  287. if (result == null)
  288. {
  289. var baseURL = request.CurrentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
  290. if (!location.StartsWith("/"))
  291. {
  292. var segments = request.CurrentUri.Segments;
  293. segments[segments.Length - 1] = location;
  294. location = String.Join(string.Empty, segments);
  295. if (location.StartsWith("//"))
  296. location = location.Substring(1);
  297. }
  298. bool endsWithSlash = baseURL[baseURL.Length - 1] == '/';
  299. bool startsWithSlash = location[0] == '/';
  300. if (endsWithSlash && startsWithSlash)
  301. result = new Uri(baseURL + location.Substring(1));
  302. else if (!endsWithSlash && !startsWithSlash)
  303. result = new Uri(baseURL + '/' + location);
  304. else
  305. result = new Uri(baseURL + location);
  306. }
  307. return result;
  308. }
  309. }
  310. }