Skip to content

Commit

Permalink
[http_proxy_over_p2p] proxy async.
Browse files Browse the repository at this point in the history
Simultaneously send request-body while reading response-body when
proxying requests.

License: MIT
Signed-off-by: Chris Boddy <chris@boddy.im>
  • Loading branch information
cboddy committed Oct 2, 2018
1 parent 72105f3 commit 4400212
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions core/corehttp/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
inet "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
)

// This adds an endpoint for proxying a HTTP request to another ipfs peer
// ProxyOption is an endpoint for proxying a HTTP request to another ipfs peer
func ProxyOption() ServeOption {
return func(ipfsNode *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/proxy/http/", func(w http.ResponseWriter, request *http.Request) {
Expand All @@ -33,8 +33,8 @@ func ProxyOption() ServeOption {
handleError(w, msg, err, 500)
return
}

newReverseHttpProxy(parsedRequest, &stream).ServeHTTP(w, request)
//send proxy request and response to client
newReverseHTTPProxy(parsedRequest, &stream).ServeHTTP(w, request)
})
return mux, nil
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func handleError(w http.ResponseWriter, msg string, err error, code int) {
log.Warningf("server error: %s: %s", err)
}

func newReverseHttpProxy(req *proxyRequest, streamToPeer *inet.Stream) *httputil.ReverseProxy {
func newReverseHTTPProxy(req *proxyRequest, streamToPeer *inet.Stream) *httputil.ReverseProxy {
director := func(r *http.Request) {
r.URL.Path = req.httpPath //the scheme etc. doesn't matter
}
Expand All @@ -85,8 +85,19 @@ type roundTripper struct {
stream *inet.Stream
}

func (self *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Write(*self.stream)
s := bufio.NewReader(*self.stream)
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {

sendRequest := func() {
err := req.Write(*rt.stream)
if err != nil {
(*(rt.stream)).Close()
}
if req.Body != nil {
req.Body.Close()
}
}
//send request while reading response
go sendRequest()
s := bufio.NewReader(*rt.stream)
return http.ReadResponse(s, req)
}

0 comments on commit 4400212

Please sign in to comment.