5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 01:00:21 +08:00

adding http.CloseNotifier and http.Flusher interface to assetserver.contentTypeSniffer, for Gin (and other framework) compatibility.

This commit is contained in:
Jason Kulatunga 2024-06-15 07:56:55 +10:00 committed by Lea Anthony
parent bd59b8c724
commit 742b802cf7
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
3 changed files with 26 additions and 2 deletions

View File

@ -59,7 +59,7 @@ func NewAssetServer(options *Options) (*AssetServer, error) {
func (a *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (a *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
start := time.Now() start := time.Now()
wrapped := &contentTypeSniffer{rw: rw} wrapped := newContentTypeSniffer(rw)
defer func() { defer func() {
if _, err := wrapped.complete(); err != nil { if _, err := wrapped.complete(); err != nil {
a.options.Logger.Error("Error writing response data.", "uri", req.RequestURI, "error", err) a.options.Logger.Error("Error writing response data.", "uri", req.RequestURI, "error", err)

View File

@ -74,7 +74,7 @@ func (a *AssetServer) processWebViewRequestInternal(r webview.Request) {
} }
}() }()
rw := &contentTypeSniffer{rw: wrw} // Make sure we have a Content-Type sniffer var rw http.ResponseWriter = newContentTypeSniffer(wrw) // Make sure we have a Content-Type sniffer
defer func() { defer func() {
if _, err := rw.complete(); err != nil { if _, err := rw.complete(); err != nil {
a.options.Logger.Error("Error writing response data.", "uri", uri, "error", err) a.options.Logger.Error("Error writing response data.", "uri", uri, "error", err)

View File

@ -4,12 +4,20 @@ import (
"net/http" "net/http"
) )
func newContentTypeSniffer(rw http.ResponseWriter) *contentTypeSniffer {
return &contentTypeSniffer{
rw: rw,
closeChannel: make(chan bool, 1),
}
}
type contentTypeSniffer struct { type contentTypeSniffer struct {
rw http.ResponseWriter rw http.ResponseWriter
prefix []byte prefix []byte
status int status int
headerCommitted bool headerCommitted bool
headerWritten bool headerWritten bool
closeChannel chan bool
} }
// Unwrap returns the wrapped [http.ResponseWriter] for use with [http.ResponseController]. // Unwrap returns the wrapped [http.ResponseWriter] for use with [http.ResponseController].
@ -108,3 +116,19 @@ func (rw *contentTypeSniffer) complete() (n int, err error) {
return return
} }
// CloseNotify implements the http.CloseNotifier interface.
func (rw *contentTypeSniffer) CloseNotify() <-chan bool {
return rw.closeChannel
}
func (rw *contentTypeSniffer) closeClient() {
rw.closeChannel <- true
}
// Flush implements the http.Flusher interface.
func (rw *contentTypeSniffer) Flush() {
if f, ok := rw.rw.(http.Flusher); ok {
f.Flush()
}
}