[http_proxy_over_p2p] proxy async.
Simultaneously send request-body while reading response-body when proxying requests. License: MIT Signed-off-by: Chris Boddy <chris@boddy.im>
Chris Boddy committed
Oct 2, 2018 at 17:40 UTC
90f5bad718a544bc1cf9ba67695fafc458581171
1 file changed
+18
-7
core/corehttp/proxy.go
+18
-7
@@ -15,7 +15,7 @@ import (
15
inet "gx/ipfs/QmfDPh144WGBqRxZb1TGDHerbMnZATrHZggAPw7putNnBq/go-libp2p-net"
16
)
17
18
-// This adds an endpoint for proxying a HTTP request to another ipfs peer
18
+// ProxyOption is an endpoint for proxying a HTTP request to another ipfs peer
19
func ProxyOption() ServeOption {
20
return func(ipfsNode *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
21
mux.HandleFunc("/proxy/http/", func(w http.ResponseWriter, request *http.Request) {
@@ -33,8 +33,8 @@ func ProxyOption() ServeOption {
33
handleError(w, msg, err, 500)
34
return
35
}
36
-
37
- newReverseHttpProxy(parsedRequest, &stream).ServeHTTP(w, request)
36
+ //send proxy request and response to client
37
+ newReverseHTTPProxy(parsedRequest, &stream).ServeHTTP(w, request)
38
})
39
return mux, nil
40
}
@@ -71,7 +71,7 @@ func handleError(w http.ResponseWriter, msg string, err error, code int) {
71
log.Warningf("server error: %s: %s", err)
72
}
73
74
-func newReverseHttpProxy(req *proxyRequest, streamToPeer *inet.Stream) *httputil.ReverseProxy {
74
+func newReverseHTTPProxy(req *proxyRequest, streamToPeer *inet.Stream) *httputil.ReverseProxy {
75
director := func(r *http.Request) {
76
r.URL.Path = req.httpPath //the scheme etc. doesn't matter
77
}
@@ -85,8 +85,19 @@ type roundTripper struct {
85
stream *inet.Stream
86
}
87
88
-func (self *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
89
- req.Write(*self.stream)
90
- s := bufio.NewReader(*self.stream)
88
+func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
89
+
90
+ sendRequest := func() {
91
+ err := req.Write(*rt.stream)
92
+ if err != nil {
93
+ (*(rt.stream)).Close()
94
+ }
95
+ if req.Body != nil {
96
+ req.Body.Close()
97
+ }
98
+ }
99
+ //send request while reading response
100
+ go sendRequest()
101
+ s := bufio.NewReader(*rt.stream)
102
return http.ReadResponse(s, req)
103
}