1
- using GenHTTP . Api . Content ;
1
+ using System . Net ;
2
+
3
+ using GenHTTP . Api . Content ;
2
4
using GenHTTP . Api . Infrastructure ;
3
5
4
6
namespace GenHTTP . Modules . ReverseProxy . Provider ;
@@ -12,8 +14,15 @@ public sealed class ReverseProxyBuilder : IHandlerBuilder<ReverseProxyBuilder>
12
14
13
15
private string ? _Upstream ;
14
16
17
+ private Action < SocketsHttpHandler > ? _HandlerAdjustments ;
18
+ private Action < HttpClient > ? _ClientAdjustments ;
19
+
15
20
#region Functionality
16
21
22
+ /// <summary>
23
+ /// Sets the server to pass the incoming requests to.
24
+ /// </summary>
25
+ /// <param name="upstream">The URL of the server to pass requests to</param>
17
26
public ReverseProxyBuilder Upstream ( string upstream )
18
27
{
19
28
_Upstream = upstream ;
@@ -26,18 +35,46 @@ public ReverseProxyBuilder Upstream(string upstream)
26
35
return this ;
27
36
}
28
37
38
+ /// <summary>
39
+ /// The time allowed to try connecting to the upstream server (defaults to 10 seconds).
40
+ /// </summary>
41
+ /// <param name="connectTimeout">The connection timeout to be set</param>
29
42
public ReverseProxyBuilder ConnectTimeout ( TimeSpan connectTimeout )
30
43
{
31
44
_ConnectTimeout = connectTimeout ;
32
45
return this ;
33
46
}
34
47
48
+ /// <summary>
49
+ /// The time allowed for the upstream server to generate a response (defaults to 60 seconds).
50
+ /// </summary>
51
+ /// <param name="readTimeout">The read timeout to be set</param>
35
52
public ReverseProxyBuilder ReadTimeout ( TimeSpan readTimeout )
36
53
{
37
54
_ReadTimeout = readTimeout ;
38
55
return this ;
39
56
}
40
57
58
+ /// <summary>
59
+ /// A callback to be invoked to configure the socket HTTP handler used by the handler.
60
+ /// </summary>
61
+ /// <param name="adjustment">The callback to be invoked</param>
62
+ public ReverseProxyBuilder AdjustHandler ( Action < SocketsHttpHandler > adjustment )
63
+ {
64
+ _HandlerAdjustments = adjustment ;
65
+ return this ;
66
+ }
67
+
68
+ /// <summary>
69
+ /// A callback to be invoked to configure the HTTP client used by the handler.
70
+ /// </summary>
71
+ /// <param name="adjustment">The callback to be invoked</param>
72
+ public ReverseProxyBuilder AdjustClient ( Action < HttpClient > adjustment )
73
+ {
74
+ _ClientAdjustments = adjustment ;
75
+ return this ;
76
+ }
77
+
41
78
public ReverseProxyBuilder Add ( IConcernBuilder concern )
42
79
{
43
80
_Concerns . Add ( concern ) ;
@@ -51,7 +88,23 @@ public IHandler Build()
51
88
throw new BuilderMissingPropertyException ( "Upstream" ) ;
52
89
}
53
90
54
- return Concerns . Chain ( _Concerns , new ReverseProxyProvider ( _Upstream , _ConnectTimeout , _ReadTimeout ) ) ;
91
+ var handler = new SocketsHttpHandler
92
+ {
93
+ AllowAutoRedirect = false ,
94
+ AutomaticDecompression = DecompressionMethods . None ,
95
+ ConnectTimeout = _ConnectTimeout
96
+ } ;
97
+
98
+ _HandlerAdjustments ? . Invoke ( handler ) ;
99
+
100
+ var client = new HttpClient ( handler )
101
+ {
102
+ Timeout = _ReadTimeout
103
+ } ;
104
+
105
+ _ClientAdjustments ? . Invoke ( client ) ;
106
+
107
+ return Concerns . Chain ( _Concerns , new ReverseProxyProvider ( _Upstream , client ) ) ;
55
108
}
56
109
57
110
#endregion
0 commit comments