Skip to content

Commit

Permalink
Merge pull request #12 from hsanjuan/feat/custom-tag
Browse files Browse the repository at this point in the history
Allow customizing the protocol tag for each RoundTripper
  • Loading branch information
hsanjuan committed Oct 3, 2018
2 parents 6d651c6 + c374b84 commit 0fd1652
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1: Qmc6tqtdKn1fVCGmU2StfULdXb8xPxmGh19NsYsgVkqjbw
1.1.2: QmXkLVayepHcdV46UUHEhVF4CapBWRRWVWvfjGJPrixrFC
59 changes: 41 additions & 18 deletions p2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,51 @@ import (
protocol "github.com/libp2p/go-libp2p-protocol"
)

// P2PProtocol is used to tag and identify streams
// DefaultP2PProtocol is used to tag and identify streams
// handled by go-libp2p-http
var P2PProtocol protocol.ID = "/libp2p-http"
var DefaultP2PProtocol protocol.ID = "/libp2p-http"

// options holds configuration options for the transport.
type options struct {
Protocol protocol.ID
}

// Option allows to set the libp2p transport options.
type Option func(o *options)

// ProtocolOption sets the Protocol Tag associated to the libp2p roundtripper.
func ProtocolOption(p protocol.ID) Option {
return func(o *options) {
o.Protocol = p
}
}

// RoundTripper implemenets http.RoundTrip and can be used as
// custom transport with Go http.Client.
type RoundTripper struct {
h host.Host
h host.Host
opts options
}

// NewTransport returns a new RoundTripper which uses the provided
// libP2P host to perform an http request and obtain the response.
//
// The typical use case for NewTransport is to register the "libp2p"
// protocol with a Transport, as in:
// t := &http.Transport{}
// t.RegisterProtocol("libp2p", p2phttp.NewTransport(host, ProtocolOption(DefaultP2PProtocol)))
// c := &http.Client{Transport: t}
// res, err := c.Get("libp2p://Qmaoi4isbcTbFfohQyn28EiYM5CDWQx9QRCjDh3CTeiY7P/index.html")
// ...
func NewTransport(h host.Host, opts ...Option) *RoundTripper {
defOpts := options{
Protocol: DefaultP2PProtocol,
}
for _, o := range opts {
o(&defOpts)
}

return &RoundTripper{h, defOpts}
}

// we wrap the response body and close the stream
Expand Down Expand Up @@ -93,7 +130,7 @@ func (rt *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, err
}

conn, err := gostream.Dial(rt.h, peer.ID(pid), P2PProtocol)
conn, err := gostream.Dial(rt.h, peer.ID(pid), rt.opts.Protocol)
if err != nil {
if r.Body != nil {
r.Body.Close()
Expand Down Expand Up @@ -124,17 +161,3 @@ func (rt *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {

return resp, nil
}

// NewTransport returns a new RoundTripper which uses the provided
// libP2P host to perform an http request and obtain the response.
//
// The typical use case for NewTransport is to register the "libp2p"
// protocol with a Transport, as in:
// t := &http.Transport{}
// t.RegisterProtocol("libp2p", p2phttp.NewTransport(host))
// c := &http.Client{Transport: t}
// res, err := c.Get("libp2p://Qmaoi4isbcTbFfohQyn28EiYM5CDWQx9QRCjDh3CTeiY7P/index.html")
// ...
func NewTransport(h host.Host) *RoundTripper {
return &RoundTripper{h}
}
4 changes: 2 additions & 2 deletions p2phttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestServerClient(t *testing.T) {
srvHost.Peerstore().AddAddrs(clientHost.ID(), clientHost.Addrs(), peerstore.PermanentAddrTTL)
clientHost.Peerstore().AddAddrs(srvHost.ID(), srvHost.Addrs(), peerstore.PermanentAddrTTL)

listener, err := gostream.Listen(srvHost, P2PProtocol)
listener, err := gostream.Listen(srvHost, "/testiti-test")
if err != nil {
t.Fatal(err)
}
Expand All @@ -61,7 +61,7 @@ func TestServerClient(t *testing.T) {
}()

tr := &http.Transport{}
tr.RegisterProtocol("libp2p", NewTransport(clientHost))
tr.RegisterProtocol("libp2p", NewTransport(clientHost, ProtocolOption("/testiti-test")))
client := &http.Client{Transport: tr}

buf := bytes.NewBufferString("Hector")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"license": "MIT",
"name": "go-libp2p-http",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "1.1.1"
"version": "1.1.2"
}

0 comments on commit 0fd1652

Please sign in to comment.