Skip to content

Commit

Permalink
Merge pull request #10 from hsanjuan/feat/streaming-http
Browse files Browse the repository at this point in the history
Feat/streaming http
  • Loading branch information
hsanjuan committed Aug 20, 2018
2 parents 31e5f1d + cc104ab commit 403189f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.8: QmSMWoH8wKuViGUe2ZDr3kDijbzkk81nY71zV1ApibFLxF
1.1.0: QmeyKL7WDSPhnhozCB3oC51j5pDs7DnCGWPyVaxgwpncA6
46 changes: 37 additions & 9 deletions p2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ package p2phttp

import (
"bufio"
"io"
"net"
"net/http"

gostream "github.com/hsanjuan/go-libp2p-gostream"
Expand All @@ -65,13 +67,22 @@ type RoundTripper struct {
h host.Host
}

// we wrap the response body and close the stream
// only when it's closed.
type respBody struct {
io.ReadCloser
conn net.Conn
}

// Closes the response's body and the connection.
func (rb *respBody) Close() error {
rb.conn.Close()
return rb.ReadCloser.Close()
}

// RoundTrip executes a single HTTP transaction, returning
// a Response for the provided Request.
func (rt *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
if r.Body != nil {
defer r.Body.Close()
}

addr := r.Host
if addr == "" {
addr = r.URL.Host
Expand All @@ -84,17 +95,34 @@ func (rt *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {

conn, err := gostream.Dial(rt.h, peer.ID(pid), P2PProtocol)
if err != nil {
if r.Body != nil {
r.Body.Close()
}
return nil, err
}
defer conn.Close()

err = r.Write(conn)
// Write the request while reading the response
go func() {
err := r.Write(conn)
if err != nil {
conn.Close()
}
if r.Body != nil {
r.Body.Close()
}
}()

resp, err := http.ReadResponse(bufio.NewReader(conn), r)
if err != nil {
return nil, err
return resp, err
}

resp.Body = &respBody{
ReadCloser: resp.Body,
conn: conn,
}

reader := bufio.NewReader(conn)
return http.ReadResponse(reader, r)
return resp, nil
}

// NewTransport returns a new RoundTripper which uses the provided
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.0.8"
"version": "1.1.0"
}

0 comments on commit 403189f

Please sign in to comment.