Skip to content

Commit

Permalink
Rename HTTPHost to Host
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Aug 21, 2023
1 parent 10f83fd commit 23bc261
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 60 deletions.
46 changes: 23 additions & 23 deletions p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
ma "github.com/multiformats/go-multiaddr"
)

func ExampleHTTPHost_withAStockGoHTTPClient() {
server := libp2phttp.HTTPHost{
func ExampleHost_withAStockGoHTTPClient() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/0/http")},
}
Expand Down Expand Up @@ -56,12 +56,12 @@ func ExampleHTTPHost_withAStockGoHTTPClient() {
// Output: Hello HTTP
}

func ExampleHTTPHost_listenOnHTTPTransportAndStreams() {
func ExampleHost_listenOnHTTPTransportAndStreams() {
serverStreamHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/50124/quic-v1"))
if err != nil {
log.Fatal(err)
}
server := libp2phttp.HTTPHost{
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50124/http")},
StreamHost: serverStreamHost,
Expand All @@ -73,13 +73,13 @@ func ExampleHTTPHost_listenOnHTTPTransportAndStreams() {
// Output: Server listening on: [/ip4/127.0.0.1/udp/50124/quic-v1 /ip4/127.0.0.1/tcp/50124/http]
}

func ExampleHTTPHost_overLibp2pStreams() {
func ExampleHost_overLibp2pStreams() {
serverStreamHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/udp/0/quic-v1"))
if err != nil {
log.Fatal(err)
}

server := libp2phttp.HTTPHost{
server := libp2phttp.Host{
StreamHost: serverStreamHost,
}

Expand All @@ -96,7 +96,7 @@ func ExampleHTTPHost_overLibp2pStreams() {
log.Fatal(err)
}

client := libp2phttp.HTTPHost{StreamHost: clientStreamHost}
client := libp2phttp.Host{StreamHost: clientStreamHost}

// Make an HTTP request using the Go standard library, but over libp2p
// streams. If the server were listening on an HTTP transport, this could
Expand All @@ -119,8 +119,8 @@ func ExampleHTTPHost_overLibp2pStreams() {
// Output: Hello HTTP
}

func ExampleHTTPHost_Serve() {
server := libp2phttp.HTTPHost{
func ExampleHost_Serve() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")},
}
Expand All @@ -133,8 +133,8 @@ func ExampleHTTPHost_Serve() {
// Output: [/ip4/127.0.0.1/tcp/50221/http]
}

func ExampleHTTPHost_SetHTTPHandler() {
server := libp2phttp.HTTPHost{
func ExampleHost_SetHTTPHandler() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50222/http")},
}
Expand Down Expand Up @@ -167,8 +167,8 @@ func ExampleHTTPHost_SetHTTPHandler() {
// Output: Hello World
}

func ExampleHTTPHost_SetHTTPHandlerAtPath() {
server := libp2phttp.HTTPHost{
func ExampleHost_SetHTTPHandlerAtPath() {
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50224/http")},
}
Expand Down Expand Up @@ -201,11 +201,11 @@ func ExampleHTTPHost_SetHTTPHandlerAtPath() {
// Output: Hello World
}

func ExampleHTTPHost_NamespacedClient() {
var client libp2phttp.HTTPHost
func ExampleHost_NamespacedClient() {
var client libp2phttp.Host

// Create the server
server := libp2phttp.HTTPHost{
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50221/http")},
}
Expand Down Expand Up @@ -239,11 +239,11 @@ func ExampleHTTPHost_NamespacedClient() {
// Output: Hello World
}

func ExampleHTTPHost_NamespaceRoundTripper() {
var client libp2phttp.HTTPHost
func ExampleHost_NamespaceRoundTripper() {
var client libp2phttp.Host

// Create the server
server := libp2phttp.HTTPHost{
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50223/http")},
}
Expand Down Expand Up @@ -283,11 +283,11 @@ func ExampleHTTPHost_NamespaceRoundTripper() {
// Output: Hello World
}

func ExampleHTTPHost_NewRoundTripper() {
var client libp2phttp.HTTPHost
func ExampleHost_NewRoundTripper() {
var client libp2phttp.Host

// Create the server
server := libp2phttp.HTTPHost{
server := libp2phttp.Host{
ServeInsecureHTTP: true, // For our example, we'll allow insecure HTTP
ListenAddrs: []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/tcp/50225/http")},
}
Expand Down Expand Up @@ -333,7 +333,7 @@ func ExampleWellKnownHandler() {
}

defer listener.Close()
// Serve `.well-known/libp2p`. Note, this is handled automatically if you use the HTTPHost.
// Serve `.well-known/libp2p`. Note, this is handled automatically if you use the libp2phttp.Host.
go http.Serve(listener, &h)

// Get the `.well-known/libp2p` resource
Expand Down
38 changes: 19 additions & 19 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ func (h *WellKnownHandler) RemoveProtocolMapping(p protocol.ID) {
h.wellknownMapMu.Unlock()
}

// HTTPHost is a libp2p host for request/responses with HTTP semantics. This is
// in contrast to a stream-oriented host like the host.Host interface. Its
// zero-value (&HTTPHost{}) is usable. Do not copy by value.
// Host is a libp2p host for request/responses with HTTP semantics. This is
// in contrast to a stream-oriented host like the core host.Host interface. Its
// zero-value (&Host{}) is usable. Do not copy by value.
// See examples for usage.
//
// Warning, this is experimental. The API will likely change.
type HTTPHost struct {
type Host struct {
// StreamHost is a stream based libp2p host used to do HTTP over libp2p streams. May be nil
StreamHost host.Host
// ListenAddrs are the requested addresses to listen on. Multiaddrs must be
Expand Down Expand Up @@ -152,7 +152,7 @@ func newPeerMetadataCache() *lru.Cache[peer.ID, PeerMeta] {
return peerMetadata
}

func (h *HTTPHost) Addrs() []ma.Multiaddr {
func (h *Host) Addrs() []ma.Multiaddr {
h.createHTTPTransport.Do(func() {
h.httpTransport = newHTTPTransport()
})
Expand All @@ -161,7 +161,7 @@ func (h *HTTPHost) Addrs() []ma.Multiaddr {
}

// ID returns the peer ID of the underlying stream host, or the zero value if there is no stream host.
func (h *HTTPHost) PeerID() peer.ID {
func (h *Host) PeerID() peer.ID {
if h.StreamHost != nil {
return h.StreamHost.ID()
}
Expand All @@ -172,7 +172,7 @@ var ErrNoListeners = errors.New("nothing to listen on")

// Serve starts the HTTP transport listeners. Always returns a non-nil error.
// If there are no listeners, returns ErrNoListeners.
func (h *HTTPHost) Serve() error {
func (h *Host) Serve() error {
// assert that each addr contains a /http component
for _, addr := range h.ListenAddrs {
_, isHTTP := normalizeHTTPMultiaddr(addr)
Expand Down Expand Up @@ -312,7 +312,7 @@ func (h *HTTPHost) Serve() error {
return err
}

func (h *HTTPHost) Close() error {
func (h *Host) Close() error {
h.createHTTPTransport.Do(func() {
h.httpTransport = newHTTPTransport()
})
Expand All @@ -324,15 +324,15 @@ func (h *HTTPHost) Close() error {
// manages the .well-known/libp2p mapping.
// http.StripPrefix is called on the handler, so the handler will be unaware of
// its prefix path.
func (h *HTTPHost) SetHTTPHandler(p protocol.ID, handler http.Handler) {
func (h *Host) SetHTTPHandler(p protocol.ID, handler http.Handler) {
h.SetHTTPHandlerAtPath(p, string(p), handler)
}

// SetHTTPHandlerAtPath sets the HTTP handler for a given protocol using the
// given path. Automatically manages the .well-known/libp2p mapping.
// http.StripPrefix is called on the handler, so the handler will be unaware of
// its prefix path.
func (h *HTTPHost) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Handler) {
func (h *Host) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Handler) {
if path == "" || path[len(path)-1] != '/' {
// We are nesting this handler under this path, so it should end with a slash.
path += "/"
Expand All @@ -349,7 +349,7 @@ type PeerMetadataGetter interface {
type streamRoundTripper struct {
server peer.ID
h host.Host
httpHost *HTTPHost
httpHost *Host
}

type streamReadCloser struct {
Expand Down Expand Up @@ -395,7 +395,7 @@ func (rt *streamRoundTripper) RoundTrip(r *http.Request) (*http.Response, error)
type roundTripperForSpecificServer struct {
http.RoundTripper
ownRoundtripper bool
httpHost *HTTPHost
httpHost *Host
server peer.ID
targetServerAddr string
sni string
Expand Down Expand Up @@ -480,7 +480,7 @@ func (rt *namespacedRoundTripper) RoundTrip(r *http.Request) (*http.Response, er
}

// NamespaceRoundTripper returns an http.RoundTripper that are scoped to the given protocol on the given server.
func (h *HTTPHost) NamespaceRoundTripper(roundtripper http.RoundTripper, p protocol.ID, server peer.ID) (*namespacedRoundTripper, error) {
func (h *Host) NamespaceRoundTripper(roundtripper http.RoundTripper, p protocol.ID, server peer.ID) (*namespacedRoundTripper, error) {
protos, err := h.getAndStorePeerMetadata(roundtripper, server)
if err != nil {
return &namespacedRoundTripper{}, err
Expand Down Expand Up @@ -514,7 +514,7 @@ func (h *HTTPHost) NamespaceRoundTripper(roundtripper http.RoundTripper, p proto
// creating many namespaced clients, consider creating a round tripper directly
// and namespacing the roundripper yourself, then creating clients from the
// namespace round tripper.
func (h *HTTPHost) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts ...RoundTripperOption) (http.Client, error) {
func (h *Host) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts ...RoundTripperOption) (http.Client, error) {
rt, err := h.NewRoundTripper(server, opts...)
if err != nil {
return http.Client{}, err
Expand All @@ -531,7 +531,7 @@ func (h *HTTPHost) NamespacedClient(p protocol.ID, server peer.AddrInfo, opts ..
// 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.
func (h *HTTPHost) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOption) (http.RoundTripper, error) {
func (h *Host) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOption) (http.RoundTripper, error) {
options := roundTripperOpts{}
for _, o := range opts {
options = o(options)
Expand Down Expand Up @@ -678,7 +678,7 @@ func normalizeHTTPMultiaddr(addr ma.Multiaddr) (ma.Multiaddr, bool) {
// ProtocolPathPrefix looks up the protocol path in the well-known mapping and
// returns it. Will only store the peer's protocol mapping if the server ID is
// provided.
func (h *HTTPHost) getAndStorePeerMetadata(roundtripper http.RoundTripper, server peer.ID) (PeerMeta, error) {
func (h *Host) getAndStorePeerMetadata(roundtripper http.RoundTripper, server peer.ID) (PeerMeta, error) {
if h.peerMetadata == nil {
h.peerMetadata = newPeerMetadataCache()
}
Expand Down Expand Up @@ -730,23 +730,23 @@ func (h *HTTPHost) getAndStorePeerMetadata(roundtripper http.RoundTripper, serve

// AddPeerMetadata adds a peer's protocol metadata to the http host. Useful if
// you have out-of-band knowledge of a peer's protocol mapping.
func (h *HTTPHost) AddPeerMetadata(server peer.ID, meta PeerMeta) {
func (h *Host) AddPeerMetadata(server peer.ID, meta PeerMeta) {
if h.peerMetadata == nil {
h.peerMetadata = newPeerMetadataCache()
}
h.peerMetadata.Add(server, meta)
}

// GetPeerMetadata gets a peer's cached protocol metadata from the http host.
func (h *HTTPHost) GetPeerMetadata(server peer.ID) (PeerMeta, bool) {
func (h *Host) GetPeerMetadata(server peer.ID) (PeerMeta, bool) {
if h.peerMetadata == nil {
return nil, false
}
return h.peerMetadata.Get(server)
}

// RemovePeerMetadata removes a peer's protocol metadata from the http host
func (h *HTTPHost) RemovePeerMetadata(server peer.ID, meta PeerMeta) {
func (h *Host) RemovePeerMetadata(server peer.ID, meta PeerMeta) {
if h.peerMetadata == nil {
return
}
Expand Down
Loading

0 comments on commit 23bc261

Please sign in to comment.