From 4031d38f347b3f11e9aa4ddcdda3317a013b1a76 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sat, 19 Jan 2019 08:48:35 -0500 Subject: [PATCH] more --- main.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/main.go b/main.go index 443e448..b603c5a 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,14 @@ func transfer(dst io.WriteCloser, src io.ReadCloser) { io.Copy(dst, src) } +func copyHeader(dst, src http.Header) { + for k, vv := range src { + for _, v := range vv { + dst.Add(k, v) + } + } +} + func (h *httpProxyHandler) dialOut(addr string) (net.Conn, error) { host, _, err := net.SplitHostPort(addr) if err != nil { @@ -56,6 +64,15 @@ func (h *httpProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { go transfer(conn, outConn) go transfer(outConn, conn) } else { + resp, err := http.DefaultTransport.RoundTrip(r) + if err != nil { + http.Error(w, err.Error(), http.StatusServiceUnavailable) + return + } + defer resp.Body.Close() + copyHeader(w.Header(), resp.Header) + w.WriteHeader(resp.StatusCode) + io.Copy(w, resp.Body) w.WriteHeader(http.StatusMethodNotAllowed) } }