Skip to content

Commit

Permalink
Rename AddProtocolMapping to AddProtocolMeta. Expand comment
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Aug 23, 2023
1 parent 802c9a2 commit 7e37919
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func ExampleHost_NewRoundTripper() {

func ExampleWellKnownHandler() {
var h libp2phttp.WellKnownHandler
h.AddProtocolMapping("/hello/1", libp2phttp.ProtocolMeta{
h.AddProtocolMeta("/hello/1", libp2phttp.ProtocolMeta{
Path: "/hello-path/",
})

Expand Down
15 changes: 10 additions & 5 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (h *WellKnownHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(mapping)
}

func (h *WellKnownHandler) AddProtocolMapping(p protocol.ID, protocolMeta ProtocolMeta) {
func (h *WellKnownHandler) AddProtocolMeta(p protocol.ID, protocolMeta ProtocolMeta) {
h.wellknownMapMu.Lock()
if h.wellKnownMapping == nil {
h.wellKnownMapping = make(map[protocol.ID]ProtocolMeta)
Expand All @@ -90,7 +90,7 @@ func (h *WellKnownHandler) AddProtocolMapping(p protocol.ID, protocolMeta Protoc
h.wellknownMapMu.Unlock()
}

func (h *WellKnownHandler) RemoveProtocolMapping(p protocol.ID) {
func (h *WellKnownHandler) RemoveProtocolMeta(p protocol.ID) {
h.wellknownMapMu.Lock()
if h.wellKnownMapping != nil {
delete(h.wellKnownMapping, p)
Expand All @@ -115,13 +115,18 @@ type Host struct {
TLSConfig *tls.Config
// ServeInsecureHTTP indicates if the server should serve unencrypted HTTP requests over TCP.
ServeInsecureHTTP bool
// ServeMux is the http.ServeMux used by the server to serve requests
// ServeMux is the http.ServeMux used by the server to serve requests. The
// zero value is a new serve mux. Users may manually add handlers to this
// mux instead of using `SetHTTPHandler`, but if they do, they should also
// update the WellKnownHandler's protocol mapping.
ServeMux http.ServeMux

// DefaultClientRoundTripper is the default http.RoundTripper for clients
DefaultClientRoundTripper *http.Transport

// WellKnownHandler is the http handler for the `.well-known/libp2p` resource
// WellKnownHandler is the http handler for the `.well-known/libp2p`
// resource. It is responsible for sharing this node's protocol metadata
// with other nodes.
WellKnownHandler WellKnownHandler

// peerMetadata is an lru cache of a peer's well-known protocol map.
Expand Down Expand Up @@ -339,7 +344,7 @@ func (h *Host) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Han
// We are nesting this handler under this path, so it should end with a slash.
path += "/"
}
h.WellKnownHandler.AddProtocolMapping(p, ProtocolMeta{Path: path})
h.WellKnownHandler.AddProtocolMeta(p, ProtocolMeta{Path: path})
h.ServeMux.Handle(path, http.StripPrefix(path, handler))
}

Expand Down
2 changes: 1 addition & 1 deletion p2p/http/libp2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestPlainOldHTTPServer(t *testing.T) {
mux.Handle("/.well-known/libp2p", &wk)

mux.Handle("/ping/", httpping.Ping{})
wk.AddProtocolMapping(httpping.PingProtocolID, libp2phttp.ProtocolMeta{Path: "/ping/"})
wk.AddProtocolMeta(httpping.PingProtocolID, libp2phttp.ProtocolMeta{Path: "/ping/"})

server := &http.Server{Addr: "127.0.0.1:0", Handler: mux}

Expand Down

0 comments on commit 7e37919

Please sign in to comment.