examples: use ctx.ResponseWriter().Naive().(http.Pusher) instead

Former-commit-id: 7802f4eadf2b1c0a0d2a42ed5ebac3e5c77f88cc
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-24 22:36:46 +03:00
parent a4efb222cc
commit 2f8b29cb3d
6 changed files with 18 additions and 82 deletions

View File

@ -27,7 +27,7 @@ func pushHandler(ctx iris.Context) {
// parent request. // parent request.
target := "/main.js" target := "/main.js"
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok { if pusher, ok := ctx.ResponseWriter().Naive().(http.Pusher); ok {
err := pusher.Push(target, nil) err := pusher.Push(target, nil)
if err != nil { if err != nil {
if err == iris.ErrPushNotSupported { if err == iris.ErrPushNotSupported {

View File

@ -97,8 +97,11 @@ func main() {
} }
app.Get("/", func(ctx iris.Context) { app.Get("/", func(ctx iris.Context) {
ctx.View("todos/index.jet", todos) // <-- err := ctx.View("todos/index.jet", todos) // <--
// Note that the `ctx.View` already logs the error if logger level is allowing it and returns the error. // Note that the `ctx.View` already logs the error if logger level is allowing it and returns the error.
if err != nil {
ctx.StopWithText(iris.StatusInternalServerError, "Templates not rendered!")
}
}) })
app.Get("/todo", func(ctx iris.Context) { app.Get("/todo", func(ctx iris.Context) {

View File

@ -184,10 +184,6 @@ type CompressResponseWriter struct {
CompressWriter CompressWriter
ResponseWriter ResponseWriter
http.Pusher
http.Hijacker
http.CloseNotifier
Disabled bool Disabled bool
Encoding string Encoding string
Level int Level int
@ -234,26 +230,6 @@ func AcquireCompressResponseWriter(w ResponseWriter, r *http.Request, level int)
v.CompressWriter = encWriter v.CompressWriter = encWriter
AddCompressHeaders(w.Header(), encoding) AddCompressHeaders(w.Header(), encoding)
pusher, ok := w.(http.Pusher)
if !ok {
pusher = nil // make sure interface value is nil.
}
hijacker, ok := w.(http.Hijacker)
if !ok {
hijacker = nil
}
closeNotifier, ok := w.(http.CloseNotifier)
if !ok {
closeNotifier = nil
}
v.Pusher = pusher
v.Hijacker = hijacker
v.CloseNotifier = closeNotifier
return v, nil return v, nil
} }

View File

@ -33,9 +33,6 @@ func releaseResponseRecorder(w *ResponseRecorder) {
type ResponseRecorder struct { type ResponseRecorder struct {
ResponseWriter ResponseWriter
http.Hijacker
http.CloseNotifier
// keep track of the body in order to be // keep track of the body in order to be
// resetable and useful inside custom transactions // resetable and useful inside custom transactions
chunks []byte chunks []byte
@ -55,20 +52,6 @@ func (w *ResponseRecorder) Naive() http.ResponseWriter {
// prepares itself, the response recorder, to record and send response to the client. // prepares itself, the response recorder, to record and send response to the client.
func (w *ResponseRecorder) BeginRecord(underline ResponseWriter) { func (w *ResponseRecorder) BeginRecord(underline ResponseWriter) {
w.ResponseWriter = underline w.ResponseWriter = underline
hijacker, ok := underline.(http.Hijacker)
if !ok {
hijacker = nil
}
closeNotifier, ok := underline.(http.CloseNotifier)
if !ok {
closeNotifier = nil
}
w.Hijacker = hijacker
w.CloseNotifier = closeNotifier
w.headers = underline.Header() w.headers = underline.Header()
w.ResetBody() w.ResetBody()
} }
@ -282,7 +265,7 @@ var ErrPushNotSupported = errors.New("push feature is not supported by this Resp
func (w *ResponseRecorder) Push(target string, opts *http.PushOptions) (err error) { func (w *ResponseRecorder) Push(target string, opts *http.PushOptions) (err error) {
w.FlushResponse() w.FlushResponse()
if pusher, ok := w.ResponseWriter.(http.Pusher); ok { if pusher, ok := w.ResponseWriter.Naive().(http.Pusher); ok {
err = pusher.Push(target, opts) err = pusher.Push(target, opts)
if err != nil && err.Error() == http.ErrNotSupported.ErrorString { if err != nil && err.Error() == http.ErrNotSupported.ErrorString {
return ErrPushNotSupported return ErrPushNotSupported

View File

@ -132,10 +132,6 @@ func releaseResponseWriter(w ResponseWriter) {
// it writes directly to the underline http.ResponseWriter // it writes directly to the underline http.ResponseWriter
type responseWriter struct { type responseWriter struct {
http.ResponseWriter http.ResponseWriter
http.Pusher
http.Hijacker // Note:
// The http.CloseNotifier interface is deprecated. New code should use Request.Context instead.
http.CloseNotifier
statusCode int // the saved status code which will be used from the cache service statusCode int // the saved status code which will be used from the cache service
// statusCodeSent bool // reply header has been (logically) written | no needed any more as we have a variable to catch total len of written bytes // statusCodeSent bool // reply header has been (logically) written | no needed any more as we have a variable to catch total len of written bytes
@ -176,29 +172,6 @@ func (w *responseWriter) BeginResponse(underline http.ResponseWriter) {
// that this responseWriter should write on. // that this responseWriter should write on.
func (w *responseWriter) SetWriter(underline http.ResponseWriter) { func (w *responseWriter) SetWriter(underline http.ResponseWriter) {
w.ResponseWriter = underline w.ResponseWriter = underline
pusher, ok := underline.(http.Pusher)
if !ok {
pusher = nil // make sure interface value is nil.
}
hijacker, ok := underline.(http.Hijacker)
if !ok {
hijacker = nil
}
// This interface is obselete by Go authors
// and we only capture it
// for compatible reasons. End-developers SHOULD replace
// the use of CloseNotifier with the: Request.Context().Done() channel.
closeNotifier, ok := underline.(http.CloseNotifier)
if !ok {
closeNotifier = nil
}
w.Pusher = pusher
w.Hijacker = hijacker
w.CloseNotifier = closeNotifier
} }
// EndResponse is the last function which is called right before the server sent the final response. // EndResponse is the last function which is called right before the server sent the final response.

View File

@ -302,18 +302,18 @@ func FileServer(fs http.FileSystem, options DirOptions) context.Handler {
if indexFound && !options.Attachments.Enable { if indexFound && !options.Attachments.Enable {
if indexAssets, ok := options.PushTargets[name]; ok { if indexAssets, ok := options.PushTargets[name]; ok {
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok { if pusher, ok := ctx.ResponseWriter().Naive().(http.Pusher); ok {
var pushOpts *http.PushOptions
if encoding != "" {
pushOpts = &http.PushOptions{Header: r.Header}
}
for _, indexAsset := range indexAssets { for _, indexAsset := range indexAssets {
if indexAsset[0] != '/' { if indexAsset[0] != '/' {
// it's relative path. // it's relative path.
indexAsset = path.Join(r.RequestURI, indexAsset) indexAsset = path.Join(r.RequestURI, indexAsset)
} }
var pushOpts *http.PushOptions
if encoding != "" {
pushOpts = &http.PushOptions{Header: r.Header}
}
if err = pusher.Push(indexAsset, pushOpts); err != nil { if err = pusher.Push(indexAsset, pushOpts); err != nil {
break break
} }
@ -322,7 +322,12 @@ func FileServer(fs http.FileSystem, options DirOptions) context.Handler {
} }
if regex, ok := options.PushTargetsRegexp[r.URL.Path]; ok { if regex, ok := options.PushTargetsRegexp[r.URL.Path]; ok {
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok { if pusher, ok := ctx.ResponseWriter().Naive().(http.Pusher); ok {
var pushOpts *http.PushOptions
if encoding != "" {
pushOpts = &http.PushOptions{Header: r.Header}
}
prefixURL := strings.TrimSuffix(r.RequestURI, name) prefixURL := strings.TrimSuffix(r.RequestURI, name)
names, err := findNames(fs, name) names, err := findNames(fs, name)
if err == nil { if err == nil {
@ -335,10 +340,6 @@ func FileServer(fs http.FileSystem, options DirOptions) context.Handler {
// match using relative path (without the first '/' slash) // match using relative path (without the first '/' slash)
// to keep consistency between the `PushTargets` behavior // to keep consistency between the `PushTargets` behavior
if regex.MatchString(indexAsset) { if regex.MatchString(indexAsset) {
var pushOpts *http.PushOptions
if encoding != "" {
pushOpts = &http.PushOptions{Header: r.Header}
}
// println("Regex Matched: " + indexAsset) // println("Regex Matched: " + indexAsset)
if err = pusher.Push(path.Join(prefixURL, indexAsset), pushOpts); err != nil { if err = pusher.Push(path.Join(prefixURL, indexAsset), pushOpts); err != nil {