-
-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathFlurlClientFactory.cs
99 lines (88 loc) · 3.49 KB
/
FlurlClientFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
namespace Flurl.Http.Configuration
{
/// <summary>
/// Interface for helper methods used to construct IFlurlClient instances.
/// </summary>
public interface IFlurlClientFactory
{
/// <summary>
/// Creates and configures a new HttpClient as needed when a new IFlurlClient instance is created.
/// Implementors should NOT attempt to cache or reuse HttpClient instances here - their lifetime is
/// bound one-to-one with an IFlurlClient, whose caching and reuse is managed by IFlurlClientCache.
/// </summary>
/// <param name="handler">The HttpMessageHandler passed to the constructor of the HttpClient.</param>
HttpClient CreateHttpClient(HttpMessageHandler handler);
/// <summary>
/// Creates and configures a new HttpMessageHandler as needed when a new IFlurlClient instance is created.
/// The default implementation creates an instance of SocketsHttpHandler for platforms that support it,
/// otherwise HttpClientHandler.
/// </summary>
HttpMessageHandler CreateInnerHandler();
}
/// <summary>
/// Extension methods on IFlurlClientFactory
/// </summary>
public static class FlurlClientFactoryExtensions
{
/// <summary>
/// Creates an HttpClient with the HttpMessageHandler returned from this factory's CreateInnerHandler method.
/// </summary>
public static HttpClient CreateHttpClient(this IFlurlClientFactory fac) => fac.CreateHttpClient(fac.CreateInnerHandler());
}
/// <summary>
/// Default implementation of IFlurlClientFactory, used to build and cache IFlurlClient instances.
/// </summary>
public class DefaultFlurlClientFactory : IFlurlClientFactory
{
// cached Blazor/WASM check (#543, #823)
private readonly bool _isBrowser =
#if NET
OperatingSystem.IsBrowser();
#else
false;
#endif
/// <inheritdoc />
public virtual HttpClient CreateHttpClient(HttpMessageHandler handler) {
return new HttpClient(handler);
}
/// <summary>
/// Creates and configures a new HttpMessageHandler as needed when a new IFlurlClient instance is created.
/// </summary>
public virtual HttpMessageHandler CreateInnerHandler() {
// Flurl has its own mechanisms for managing cookies and redirects, so we need to disable them in the inner handler.
var handler = new HttpClientHandler();
if (handler.SupportsRedirectConfiguration)
handler.AllowAutoRedirect = false;
// #266
// deflate not working? see #474
if (handler.SupportsAutomaticDecompression)
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
if (!_isBrowser) {
try { handler.UseCookies = false; }
catch (PlatformNotSupportedException) { } // already checked for Blazor, but just in case any other surprises pop up
}
return handler;
}
}
#if NET
/// <summary>
/// An implementation of IFlurlClientFactory that uses SocketsHttpHandler on supported platforms.
/// </summary>
public class SocketsHandlerFlurlClientFactory : DefaultFlurlClientFactory
{
/// <summary>
/// Creates and configures a new SocketsHttpHandler as needed when a new IFlurlClient instance is created.
/// </summary>
public override HttpMessageHandler CreateInnerHandler() => new SocketsHttpHandler {
// Flurl has its own mechanisms for managing cookies and redirects, so we need to disable them in the inner handler.
UseCookies = false,
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
}
#endif
}