fix future issues like #1607

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-27 07:23:56 +03:00
parent 9c56ed6261
commit 39e3911d41
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
4 changed files with 44 additions and 13 deletions

View File

@ -184,6 +184,8 @@ type CompressResponseWriter struct {
CompressWriter
ResponseWriter
http.Hijacker
Disabled bool
Encoding string
Level int
@ -204,6 +206,21 @@ func AcquireCompressResponseWriter(w ResponseWriter, r *http.Request, level int)
}
v := compressWritersPool.Get().(*CompressResponseWriter)
if h, ok := w.(http.Hijacker); ok {
v.Hijacker = h
} else {
v.Hijacker = nil
}
// The Naive() should be used to check for Pusher,
// as examples explicitly says, so don't do it:
// if p, ok := w.Naive().(http.Pusher); ok {
// v.Pusher = p
// } else {
// v.Pusher = nil
// }
v.ResponseWriter = w
v.Disabled = false
if level == -1 && encoding == BROTLI {
@ -255,6 +272,13 @@ func (w *CompressResponseWriter) FlushResponse() {
// write the status, after header set and before any flushed content sent.
w.ResponseWriter.FlushResponse()
if w.IsHijacked() {
// net/http docs:
// It becomes the caller's responsibility to manage
// and close the connection.
return
}
w.CompressWriter.Close() // flushes and closes.
}

View File

@ -259,7 +259,7 @@ func ingoreMainHandlerName(name string) bool {
return false
}
// Filter is just a type of func(Handler) bool which reports whether an action must be performed
// Filter is just a type of func(Context) bool which reports whether an action must be performed
// based on the incoming request.
//
// See `NewConditionalHandler` for more.

View File

@ -1354,7 +1354,7 @@ func getCaller() (string, int) {
if relFile, err := filepath.Rel(wd, file); err == nil {
if !strings.HasPrefix(relFile, "..") {
// Only if it's relative to this path, not parent.
file = "./" + relFile
file = "./" + filepath.ToSlash(relFile)
}
}

View File

@ -324,19 +324,26 @@ func TestUseWrapOrder(t *testing.T) {
wrapRouter = func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
if r.URL.Path == "/" {
w.Write([]byte("#1 .WrapRouter\n"))
// Note for beginners, reading this test:
// if we write something here on a not found page,
// in the raw net/http wrapper like this one,
// then the response writer sends 200 status OK
// (on first write) and so any error handler will not be fired as expected,
// these are basic things. If you w.WriteHeader you cannot change the status code later on too.
// In Iris handlers, if you write before status code set, then it sends 200
// and it cannot change too (if you want to change that behavior you use ctx.Record()).
// However if you
// just call ctx.StatusCode without content written then you are able to change the status code
// later on.
/* Note for new Gophers:
If we write something here, on a not found resource,
in the raw `net/http` wrapper like this one, then the
response writer will send `200` status OK (on first write).
Any error handler will not be fired as expected.
Also, when `w.WriteHeader` is called you can NOT
change the status code later on.
In Iris Handlers, if you write before status code set,
then it sends 200 status OK and it cannot change as well.
However if we just called `ctx.StatusCode` inside an
Iris Handler without any content written then we
would able to change the status code later on.
When you need to change that behavior you should
start the handler with a [ctx.Record()](responses/recorder.md) call.
*/
}
// Continue by executing the Iris Router and leave it do its job.
router(w, r)
}
)