mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 17:28:31 +02:00
fix(middleware): enhance response modification handling in ServeHTTP
- Replaced ResponseModifier with new LazyResponseModifier. - Added logic to skip modification for non-HTML content.
This commit is contained in:
2
goutils
2
goutils
Submodule goutils updated: 22ec87826e...316d9a68f4
@@ -197,10 +197,16 @@ func (m *Middleware) ServeHTTP(next http.HandlerFunc, w http.ResponseWriter, r *
|
|||||||
}
|
}
|
||||||
|
|
||||||
if exec, ok := m.impl.(ResponseModifier); ok {
|
if exec, ok := m.impl.(ResponseModifier); ok {
|
||||||
rm := httputils.NewResponseModifier(w)
|
lrm := httputils.NewLazyResponseModifier(w, needsBuffering)
|
||||||
defer rm.FlushRelease()
|
defer lrm.FlushRelease()
|
||||||
next(rm, r)
|
next(lrm, r)
|
||||||
|
|
||||||
|
// Skip modification if response wasn't buffered (non-HTML content)
|
||||||
|
if !lrm.IsBuffered() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rm := lrm.ResponseModifier()
|
||||||
currentBody := rm.BodyReader()
|
currentBody := rm.BodyReader()
|
||||||
currentResp := &http.Response{
|
currentResp := &http.Response{
|
||||||
StatusCode: rm.StatusCode(),
|
StatusCode: rm.StatusCode(),
|
||||||
@@ -228,6 +234,12 @@ func (m *Middleware) ServeHTTP(next http.HandlerFunc, w http.ResponseWriter, r *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// needsBuffering determines if a response should be buffered for modification.
|
||||||
|
// Only HTML responses need buffering; streaming content (video, audio, etc.) should pass through.
|
||||||
|
func needsBuffering(header http.Header) bool {
|
||||||
|
return httputils.GetContentType(header).IsHTML()
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Middleware) LogWarn(req *http.Request) *zerolog.Event {
|
func (m *Middleware) LogWarn(req *http.Request) *zerolog.Event {
|
||||||
return log.Warn().Str("middleware", m.name).
|
return log.Warn().Str("middleware", m.name).
|
||||||
Str("host", req.Host).
|
Str("host", req.Host).
|
||||||
|
|||||||
Reference in New Issue
Block a user