提交 58a3ea14 作者: Dr Ian Preston 提交者: Steven Allen

Remove unnecessary pointer usage.

Make sure the p2p stream is closed eventually

License: MIT
Signed-off-by: 's avatarIan Preston <ianopolous@protonmail.com>
上级 cba97300
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"net" "net"
"io"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"strings" "strings"
...@@ -34,7 +35,7 @@ func ProxyOption() ServeOption { ...@@ -34,7 +35,7 @@ func ProxyOption() ServeOption {
return return
} }
//send proxy request and response to client //send proxy request and response to client
newReverseHTTPProxy(parsedRequest, &stream).ServeHTTP(w, request) newReverseHTTPProxy(parsedRequest, stream).ServeHTTP(w, request)
}) })
return mux, nil return mux, nil
} }
...@@ -71,7 +72,7 @@ func handleError(w http.ResponseWriter, msg string, err error, code int) { ...@@ -71,7 +72,7 @@ func handleError(w http.ResponseWriter, msg string, err error, code int) {
log.Warningf("server error: %s: %s", err) 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) { director := func(r *http.Request) {
r.URL.Path = req.httpPath //the scheme etc. doesn't matter r.URL.Path = req.httpPath //the scheme etc. doesn't matter
} }
...@@ -82,15 +83,28 @@ func newReverseHTTPProxy(req *proxyRequest, streamToPeer *inet.Stream) *httputil ...@@ -82,15 +83,28 @@ func newReverseHTTPProxy(req *proxyRequest, streamToPeer *inet.Stream) *httputil
} }
type roundTripper struct { type roundTripper struct {
stream *inet.Stream stream inet.Stream
}
// we wrap the response body and close the stream
// only when it's closed.
type respBody struct {
io.ReadCloser
stream inet.Stream
}
// Closes the response's body and the connection.
func (rb *respBody) Close() error {
rb.stream.Close()
return rb.ReadCloser.Close()
} }
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
sendRequest := func() { sendRequest := func() {
err := req.Write(*rt.stream) err := req.Write(rt.stream)
if err != nil { if err != nil {
(*(rt.stream)).Close() rt.stream.Close()
} }
if req.Body != nil { if req.Body != nil {
req.Body.Close() req.Body.Close()
...@@ -98,6 +112,17 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { ...@@ -98,6 +112,17 @@ func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
} }
//send request while reading response //send request while reading response
go sendRequest() go sendRequest()
s := bufio.NewReader(*rt.stream) s := bufio.NewReader(rt.stream)
return http.ReadResponse(s, req)
resp, err := http.ReadResponse(s, req)
if err != nil {
return resp, err
}
resp.Body = &respBody{
ReadCloser: resp.Body,
stream: rt.stream,
}
return resp, nil
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论