Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libp2phttp.Host implements RoundTripper #2840

Merged
merged 5 commits into from
Jun 25, 2024

Conversation

MarcoPolo
Copy link
Contributor

After working on the js-libp2p API for libp2p+HTTP, I realized it's quite natural to make an HTTP request given a multiaddr URI (who would've guessed?).

This change makes using the HTTP client with libp2p easier and more natural. Instead of having to create a specialized roundtripper for each server, you can now have a single http.Client that can make requests to multiple servers. The usage feels very familiar to the classic HTTP api. You can now do this:

client := http.Client{Transport: &libp2phttp.Host{ options... }}
client.Get("https://example.com") // Standard way of making an HTTP request
client.Get("multiaddr:/dns/example.com/tls/http") // Same as above, but with a multiaddr scheme
client.Get("multiaddr:/dnsaddr/example.com/p2p/QmFoo") // galaxy brain; make an HTTP request over a libp2p transport

@MarcoPolo MarcoPolo requested a review from sukunrt June 13, 2024 17:44

testCases := []string{
// Version that has an http-path. Will uncomment when we get the http-path changes in go-multiaddr
// "multiaddr:" + serverHost.Addrs()[0].String() + "/http-path/hello",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! ❤️

Comment on lines +410 to +412
// if true, we won't add the server's addresses to the peerstore. This
// should only be set when creating the struct.
skipAddAddrs bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on why we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really doesn't matter, but it saves us the synchronization overhead of the addrsAdded sync.Once call.

Comment on lines +663 to +664
if parsed.sni != parsed.host {
// We have a different host and SNI (e.g. using an IP address but specifying a SNI)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with /dns addrs?

Do we need something like: https://github.com/libp2p/go-libp2p/blob/master/p2p/transport/websocket/websocket.go#L123 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes /dns works. Roundtripper handles this implicitly. Fails if the server doesn't support the SNI. Example: /dns/example.com/tls/sni/example.net/httpworks, but /dns/example.net/tls/sni/notexample.com/http fails.

@MarcoPolo MarcoPolo merged commit effa3fc into master Jun 25, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants