Skip to content

Commit

Permalink
CR's fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 16, 2023
1 parent 484f69e commit ecbf066
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,18 @@ func createProxyRequestMiddleware(next http.Handler, config config.Config, servi
// extract the original hostname the request was sent to
requestHostnameContext := context.WithValue(originRoundtripLatencyContext, RequestHostnameContextKey, r.Host)

rawBody, err := io.ReadAll(lrw.body)
var bodyCopy *bytes.Buffer
tee := io.TeeReader(lrw.body, bodyCopy)
// read all body from reader into bodyBytes, and copy into bodyCopy
bodyBytes, err := io.ReadAll(tee)
if err != nil {
serviceLogger.Error().Err(err).Msg("can't read lrw.body")
}
responseContext := context.WithValue(requestHostnameContext, cachemdw.ResponseContextKey, rawBody)

// replace empty body reader with fresh copy
lrw.body = bodyCopy
// set body in context
responseContext := context.WithValue(requestHostnameContext, cachemdw.ResponseContextKey, bodyBytes)

enrichedContext := responseContext

Expand Down

0 comments on commit ecbf066

Please sign in to comment.