Skip to content

Commit cac28f5

Browse files
Pass down a client usign transport options
1 parent 479c9ab commit cac28f5

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

fclient/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ retryResolution:
346346
// If the cache returned nothing then we'll have no results here,
347347
// so go and hit the network.
348348
if len(resolutionResults) == 0 {
349-
resolutionResults, err = ResolveServer(r.Context(), serverName)
349+
resolutionResults, err = ResolveServer(r.Context(), &NewClient(WithTransport(f.getTransport(string(serverName), f.dialer))).client, serverName)
350350
if err != nil {
351351
return nil, err
352352
}

fclient/resolve.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"net"
22+
"net/http"
2223
"strconv"
2324

2425
"github.com/matrix-org/gomatrixserverlib/spec"
@@ -37,14 +38,14 @@ type ResolutionResult struct {
3738
// Returns a slice of ResolutionResult that can be used to send a federation
3839
// request to the server using a given server name.
3940
// Returns an error if the server name isn't valid.
40-
func ResolveServer(ctx context.Context, serverName spec.ServerName) (results []ResolutionResult, err error) {
41-
return resolveServer(ctx, serverName, true)
41+
func ResolveServer(ctx context.Context, client *http.Client, serverName spec.ServerName) (results []ResolutionResult, err error) {
42+
return resolveServer(ctx, client, serverName, true)
4243
}
4344

4445
// resolveServer does the same thing as ResolveServer, except it also requires
4546
// the checkWellKnown parameter, which indicates whether a .well-known file
4647
// should be looked up.
47-
func resolveServer(ctx context.Context, serverName spec.ServerName, checkWellKnown bool) (results []ResolutionResult, err error) {
48+
func resolveServer(ctx context.Context, client *http.Client, serverName spec.ServerName, checkWellKnown bool) (results []ResolutionResult, err error) {
4849
host, port, valid := spec.ParseAndValidateServerName(serverName)
4950
if !valid {
5051
err = fmt.Errorf("Invalid server name")
@@ -94,10 +95,10 @@ func resolveServer(ctx context.Context, serverName spec.ServerName, checkWellKno
9495
if checkWellKnown {
9596
// 3. If the hostname is not an IP literal
9697
var result *WellKnownResult
97-
result, err = LookupWellKnown(ctx, serverName)
98+
result, err = LookupWellKnown(ctx, client, serverName)
9899
if err == nil {
99100
// We don't want to check .well-known on the result
100-
return resolveServer(ctx, result.NewAddress, false)
101+
return resolveServer(ctx, client, result.NewAddress, false)
101102
}
102103
}
103104

fclient/resolve_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"net/http"
78
"reflect"
89
"testing"
910

@@ -27,8 +28,8 @@ func assertCritical(t *testing.T, val, expected interface{}) {
2728
// and expected certificate name.
2829
// If one of them doesn't match, or the resolution function returned with an
2930
// error, it aborts the current test.
30-
func testResolve(t *testing.T, serverName spec.ServerName, destination, host, certName string) {
31-
res, err := ResolveServer(context.Background(), serverName)
31+
func testResolve(t *testing.T, client *http.Client, serverName spec.ServerName, destination, host, certName string) {
32+
res, err := ResolveServer(context.Background(), client, serverName)
3233
assertCritical(t, err, nil)
3334
assertCritical(t, len(res), 1)
3435
assertCritical(t, res[0].Destination, destination)
@@ -40,6 +41,7 @@ func testResolve(t *testing.T, serverName spec.ServerName, destination, host, ce
4041
func TestResolutionIPLiteral(t *testing.T) {
4142
testResolve(
4243
t,
44+
http.DefaultClient,
4345
spec.ServerName("42.42.42.42"), // The server name is an IP literal without a port
4446
"42.42.42.42:8448", // Destination must be the IP address + port 8448
4547
"42.42.42.42", // Host must be the IP address
@@ -51,6 +53,7 @@ func TestResolutionIPLiteral(t *testing.T) {
5153
func TestResolutionIPv6Literal(t *testing.T) {
5254
testResolve(
5355
t,
56+
http.DefaultClient,
5457
spec.ServerName("[42:42::42]"), // The server name is an IP literal without a port
5558
"[42:42::42]:8448", // Destination must be the IP address + port 8448
5659
"[42:42::42]", // Host must be the IP address
@@ -62,6 +65,7 @@ func TestResolutionIPv6Literal(t *testing.T) {
6265
func TestResolutionIPLiteralWithPort(t *testing.T) {
6366
testResolve(
6467
t,
68+
http.DefaultClient,
6569
spec.ServerName("42.42.42.42:443"), // The server name is an IP literal with a port
6670
"42.42.42.42:443", // Destination must be the IP address + port
6771
"42.42.42.42:443", // Host must be the IP address + port
@@ -73,6 +77,7 @@ func TestResolutionIPLiteralWithPort(t *testing.T) {
7377
func TestResolutionIPv6LiteralWithPort(t *testing.T) {
7478
testResolve(
7579
t,
80+
http.DefaultClient,
7681
spec.ServerName("[42:42::42]:443"), // The server name is an IP literal with a port
7782
"[42:42::42]:443", // Destination must be the IP address + port
7883
"[42:42::42]:443", // Host must be the IP address + port
@@ -84,6 +89,7 @@ func TestResolutionIPv6LiteralWithPort(t *testing.T) {
8489
func TestResolutionHostnameAndPort(t *testing.T) {
8590
testResolve(
8691
t,
92+
http.DefaultClient,
8793
spec.ServerName("example.com:4242"), // The server name is not an IP literal and includes an explicit port
8894
"example.com:4242", // Destination must be the hostname + port
8995
"example.com:4242", // Host must be the hostname + port
@@ -102,6 +108,7 @@ func TestResolutionHostnameWellKnownWithIPLiteral(t *testing.T) {
102108

103109
testResolve(
104110
t,
111+
http.DefaultClient,
105112
spec.ServerName("example.com"), // The server name is a domain hosting a .well-known file which specifies an IP literal without a port
106113
"42.42.42.42:8448", // Destination must be the IP literal + port 8448
107114
"42.42.42.42", // Host must be the IP literal
@@ -120,6 +127,7 @@ func TestResolutionHostnameWellKnownWithIPLiteralAndPort(t *testing.T) {
120127

121128
testResolve(
122129
t,
130+
http.DefaultClient,
123131
spec.ServerName("example.com"), // The server name is a domain hosting a .well-known file which specifies an IP literal with a port
124132
"42.42.42.42:443", // Destination must be the IP literal + port
125133
"42.42.42.42:443", // Host must be the IP literal + port
@@ -138,6 +146,7 @@ func TestResolutionHostnameWellKnownWithHostnameAndPort(t *testing.T) {
138146

139147
testResolve(
140148
t,
149+
http.DefaultClient,
141150
spec.ServerName("example.com"), // The server name is a domain hosting a .well-known file which specifies a hostname that's not an IP literal and has a port
142151
"matrix.example.com:4242", // Destination must be the hostname + port
143152
"matrix.example.com:4242", // Host must be the hostname + port
@@ -159,6 +168,7 @@ func TestResolutionHostnameWellKnownWithHostnameSRV(t *testing.T) {
159168

160169
testResolve(
161170
t,
171+
http.DefaultClient,
162172
spec.ServerName("example.com"), // The server name is a domain hosting a .well-known file which specifies a hostname that's not an IP literal, has no port and for which a SRV record with a non-0 exists
163173
"matrix.otherexample.com:4242", // Destination must be the hostname + port from the SRV record
164174
"matrix.example.com", // Host must be the delegated hostname
@@ -180,6 +190,7 @@ func TestResolutionHostnameWellKnownWithHostnameNoSRV(t *testing.T) {
180190

181191
testResolve(
182192
t,
193+
http.DefaultClient,
183194
spec.ServerName("example.com"), // The server name is a domain hosting a .well-known file which specifies a hostname that's not an IP literal, has no port and for which no SRV record exists
184195
"matrix.example.com:8448", // Destination must be the delegated hostname + port 8448
185196
"matrix.example.com", // Host must be the delegated hostname
@@ -194,6 +205,7 @@ func TestResolutionHostnameWithSRV(t *testing.T) {
194205

195206
testResolve(
196207
t,
208+
http.DefaultClient,
197209
spec.ServerName("example.com"), // The server name is a domain for which a SRV record exists with a non-0 port
198210
"matrix.otherexample.com:4242", // Destination must be the hostname + port
199211
"example.com", // Host must be the server name
@@ -214,6 +226,7 @@ func TestResolutionHostnameWithNoWellKnownNorSRV(t *testing.T) {
214226

215227
testResolve(
216228
t,
229+
http.DefaultClient,
217230
spec.ServerName("example.com"), // The server name is a domain for no .well-known file nor SRV record exist
218231
"example.com:8448", // Destination must be the hostname + 8448
219232
"example.com", // Host must be the server name

fclient/well_known.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type WellKnownResult struct {
2929

3030
// LookupWellKnown looks up a well-known record for a matrix server. If one if
3131
// found, it returns the server to redirect to.
32-
func LookupWellKnown(ctx context.Context, serverNameType spec.ServerName) (*WellKnownResult, error) {
32+
func LookupWellKnown(ctx context.Context, client *http.Client, serverNameType spec.ServerName) (*WellKnownResult, error) {
3333
serverName := string(serverNameType)
3434

3535
// Handle ending "/"
@@ -43,7 +43,6 @@ func LookupWellKnown(ctx context.Context, serverNameType spec.ServerName) (*Well
4343
return nil, err
4444
}
4545
// Given well-known should be quite small and fast to fetch, timeout the request after 30s.
46-
client := http.Client{Timeout: time.Second * 30}
4746
resp, err := client.Do(req)
4847
if err != nil {
4948
return nil, err

0 commit comments

Comments
 (0)