Skip to content

Commit

Permalink
add new function to copy http response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Mar 4, 2024
1 parent a51cd00 commit b0c1a43
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions http/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ func (f *Forwarder) Forward(w http.ResponseWriter, r *http.Request, host string)
return
}

// CopyResponse copyies the response to the request client.
func CopyResponse(w http.ResponseWriter, resp *http.Response) (err error) {
// Copy response header
// CopyResponseHeader copies the response header to the client.
//
// If filter is set and returns true for a certain key, which filters the key.
func CopyResponseHeader(w http.ResponseWriter, resp *http.Response, filter func(string) bool) {
header := w.Header()
for k, vs := range resp.Header {
switch {
Expand All @@ -103,12 +104,17 @@ func CopyResponse(w http.ResponseWriter, resp *http.Response) (err error) {
case k == "Host":
case k == "Connection":

case filter != nil && filter(k):

default:
header[k] = vs
}
}
}

// Copy response body
// CopyResponse copyies the response to the request client.
func CopyResponse(w http.ResponseWriter, resp *http.Response) (err error) {
CopyResponseHeader(w, resp, nil)
w.WriteHeader(resp.StatusCode)
_, err = io.CopyBuffer(w, resp.Body, make([]byte, 1024))
return
Expand Down

0 comments on commit b0c1a43

Please sign in to comment.