Skip to content

Commit

Permalink
Use stream host from option instead of parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Aug 4, 2023
1 parent 61da6d1 commit 49d7db4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
20 changes: 12 additions & 8 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func (h *HTTPHost) Serve() error {

// get resolved port
_, port, err := net.SplitHostPort(l.Addr().String())
if err != nil {
closeAllListeners()
return err
}

var listenAddr ma.Multiaddr
if parsedAddr.useHTTPS && parsedAddr.sni != "" && parsedAddr.sni != host {
Expand Down Expand Up @@ -430,8 +434,8 @@ func (h *HTTPHost) NamespaceRoundTripper(roundtripper http.RoundTripper, p proto
}

// NamespacedClient returns an http.Client that is scoped to the given protocol on the given server.
func (h *HTTPHost) NamespacedClient(streamHost host.Host, p protocol.ID, server peer.AddrInfo, opts ...RoundTripperOptsFn) (http.Client, error) {
rt, err := h.NewRoundTripper(streamHost, server, opts...)
func (h *HTTPHost) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts ...RoundTripperOptsFn) (http.Client, error) {
rt, err := h.NewRoundTripper(server, opts...)
if err != nil {
return http.Client{}, err
}
Expand All @@ -454,7 +458,7 @@ type RoundTripperOptsFn func(o roundTripperOpts) roundTripperOpts
// NewRoundTripper returns an http.RoundTripper that can fulfill and HTTP
// request to the given server. It may use an HTTP transport or a stream based
// transport. It is valid to pass an empty server.ID and a nil streamHost.
func (h *HTTPHost) NewRoundTripper(streamHost host.Host, server peer.AddrInfo, opts ...RoundTripperOptsFn) (http.RoundTripper, error) {
func (h *HTTPHost) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOptsFn) (http.RoundTripper, error) {
options := roundTripperOpts{}
for _, o := range opts {
options = o(options)
Expand Down Expand Up @@ -498,8 +502,8 @@ func (h *HTTPHost) NewRoundTripper(streamHost host.Host, server peer.AddrInfo, o

// Do we have an existing connection to this peer?
existingStreamConn := false
if server.ID != "" && streamHost != nil {
existingStreamConn = len(streamHost.Network().ConnsToPeer(server.ID)) > 0
if server.ID != "" && h.streamHost != nil {
existingStreamConn = len(h.streamHost.Network().ConnsToPeer(server.ID)) > 0
}

if len(httpAddrs) > 0 && (options.preferHTTPTransport || (firstAddrIsHTTP && !existingStreamConn)) {
Expand Down Expand Up @@ -532,20 +536,20 @@ func (h *HTTPHost) NewRoundTripper(streamHost host.Host, server peer.AddrInfo, o
}

// Otherwise use a stream based transport
if streamHost == nil {
if h.streamHost == nil {
return nil, fmt.Errorf("no http addresses for peer, and no stream host provided")
}
if !existingStreamConn {
if server.ID == "" {
return nil, fmt.Errorf("no http addresses for peer, and no server peer ID provided")
}
err := streamHost.Connect(context.Background(), peer.AddrInfo{ID: server.ID, Addrs: nonHttpAddrs})
err := h.streamHost.Connect(context.Background(), peer.AddrInfo{ID: server.ID, Addrs: nonHttpAddrs})
if err != nil {
return nil, fmt.Errorf("failed to connect to peer: %w", err)
}
}

return NewStreamRoundTripper(streamHost, server.ID), nil
return NewStreamRoundTripper(h.streamHost, server.ID), nil
}

type httpMultiaddr struct {
Expand Down
22 changes: 11 additions & 11 deletions p2p/http/libp2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestRoundTrippers(t *testing.T) {
{
name: "HTTP preferred",
setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper {
rt, err := clientHTTPHost.NewRoundTripper(clientStreamHost, peer.AddrInfo{
rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{
ID: serverHost.ID(),
Addrs: serverMultiaddrs,
}, libp2phttp.RoundTripperPreferHTTPTransport)
Expand All @@ -104,7 +104,7 @@ func TestRoundTrippers(t *testing.T) {
{
name: "HTTP first",
setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper {
rt, err := clientHTTPHost.NewRoundTripper(clientStreamHost, peer.AddrInfo{
rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{
ID: serverHost.ID(),
Addrs: []ma.Multiaddr{serverHTTPAddr, serverHost.Addrs()[0]},
})
Expand All @@ -115,7 +115,7 @@ func TestRoundTrippers(t *testing.T) {
{
name: "No HTTP transport",
setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper {
rt, err := clientHTTPHost.NewRoundTripper(clientStreamHost, peer.AddrInfo{
rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{
ID: serverHost.ID(),
Addrs: []ma.Multiaddr{serverHost.Addrs()[0]},
})
Expand All @@ -127,7 +127,7 @@ func TestRoundTrippers(t *testing.T) {
{
name: "Stream transport first",
setupRoundTripper: func(t *testing.T, clientStreamHost host.Host, clientHTTPHost *libp2phttp.HTTPHost) http.RoundTripper {
rt, err := clientHTTPHost.NewRoundTripper(clientStreamHost, peer.AddrInfo{
rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{
ID: serverHost.ID(),
Addrs: []ma.Multiaddr{serverHost.Addrs()[0], serverHTTPAddr},
})
Expand All @@ -143,7 +143,7 @@ func TestRoundTrippers(t *testing.T) {
ID: serverHost.ID(),
Addrs: serverHost.Addrs(),
})
rt, err := clientHTTPHost.NewRoundTripper(clientStreamHost, peer.AddrInfo{
rt, err := clientHTTPHost.NewRoundTripper(peer.AddrInfo{
ID: serverHost.ID(),
Addrs: []ma.Multiaddr{serverHTTPAddr, serverHost.Addrs()[0]},
})
Expand All @@ -157,14 +157,14 @@ func TestRoundTrippers(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Start client
clientHost, err := libp2p.New(libp2p.NoListenAddrs)
clientStreamHost, err := libp2p.New(libp2p.NoListenAddrs)
require.NoError(t, err)
defer clientHost.Close()
defer clientStreamHost.Close()

clientHttpHost, err := libp2phttp.New()
clientHttpHost, err := libp2phttp.New(libp2phttp.StreamHost(clientStreamHost))
require.NoError(t, err)

rt := tc.setupRoundTripper(t, clientHost, clientHttpHost)
rt := tc.setupRoundTripper(t, clientStreamHost, clientHttpHost)
if tc.expectStreamRoundTripper {
// Hack to get the private type of this roundtripper
typ := reflect.TypeOf(rt).String()
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestPlainOldHTTPServer(t *testing.T) {
do: func(t *testing.T, request *http.Request) (*http.Response, error) {
clientHttpHost, err := libp2phttp.New()
require.NoError(t, err)
rt, err := clientHttpHost.NewRoundTripper(nil, peer.AddrInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/" + serverAddrParts[1] + "/http")}})
rt, err := clientHttpHost.NewRoundTripper(peer.AddrInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/" + serverAddrParts[1] + "/http")}})
require.NoError(t, err)

client := &http.Client{Transport: rt}
Expand All @@ -251,7 +251,7 @@ func TestPlainOldHTTPServer(t *testing.T) {
getWellKnown: func(t *testing.T) (libp2phttp.WellKnownProtoMap, error) {
clientHttpHost, err := libp2phttp.New()
require.NoError(t, err)
rt, err := clientHttpHost.NewRoundTripper(nil, peer.AddrInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/" + serverAddrParts[1] + "/http")}})
rt, err := clientHttpHost.NewRoundTripper(peer.AddrInfo{Addrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/" + serverAddrParts[1] + "/http")}})
require.NoError(t, err)
return clientHttpHost.GetAndStorePeerProtoMap(rt, "")
},
Expand Down

0 comments on commit 49d7db4

Please sign in to comment.