]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.0+20231006c6a051a
authorqydysky <qydysky@foxmail.com>
Fri, 6 Oct 2023 19:07:46 +0000 (03:07 +0800)
committerqydysky <qydysky@foxmail.com>
Fri, 6 Oct 2023 19:07:46 +0000 (03:07 +0800)
web/Web.go

index 79aced2d6714359ae060dae67e0663e82c4b28c1..d7b6b2be19c5f4a94524bb84931fcdc490cf3364 100644 (file)
@@ -349,36 +349,35 @@ func (t *Cache) Cache(key string, aliveDur time.Duration, w http.ResponseWriter)
 }
 
 type withflush struct {
-       f  func()
-       h  func() http.Header
-       w  func([]byte) (int, error)
-       wh func(int)
+       raw http.ResponseWriter
 }
 
 func (t withflush) Header() http.Header {
-       return t.h()
+       if t.raw != nil {
+               return t.raw.Header()
+       }
+       return make(http.Header)
 }
 func (t withflush) Write(b []byte) (i int, e error) {
-       i, e = t.w(b)
-       if t.f != nil {
-               t.f()
+       if t.raw != nil {
+               i, e = t.Write(b)
+               if e != nil {
+                       return
+               }
+               if flusher, ok := t.raw.(http.Flusher); ok {
+                       flusher.Flush()
+               }
        }
        return
 }
 func (t withflush) WriteHeader(i int) {
-       t.wh(i)
+       if t.raw != nil {
+               t.raw.WriteHeader(i)
+       }
 }
 
 func WithFlush(w http.ResponseWriter) http.ResponseWriter {
-       if Flusher, ok := w.(http.Flusher); ok {
-               return withflush{
-                       f:  Flusher.Flush,
-                       h:  w.Header,
-                       w:  w.Write,
-                       wh: w.WriteHeader,
-               }
-       }
-       return w
+       return withflush{w}
 }
 
 func WithStatusCode(w http.ResponseWriter, code int) {