gin-contrib / timeout

Timeout middleware for Gin
MIT License
183 stars 37 forks source link

panic occured while processing redirect request #33

Closed WisperDin closed 11 months ago

WisperDin commented 2 years ago

redirect handler like that:

    r.GET("/redirect", func(c *gin.Context) {
        c.Redirect(302, "OK")
    })

panic when calling c.Render, because the call carries the parameter code=-1

// Redirect returns an HTTP redirect to the specific location.
func (c *Context) Redirect(code int, location string) {
    c.Render(-1, render.Redirect{
        Code:     code,
        Location: location,
        Request:  c.Request,
    })
}

Panic when calling WriteHeader because timeout middleware rewrites the writer

https://github.com/gin-contrib/timeout/blob/7e229ecbcfb14762a93e6d7b4938ba31b78a85d8/writer.go#L41

https://github.com/gin-contrib/timeout/blob/7e229ecbcfb14762a93e6d7b4938ba31b78a85d8/writer.go#L75

func checkWriteHeaderCode(code int) {
    if code < 100 || code > 999 {
        panic(fmt.Sprintf("invalid http status code: %d", code))
    }
}
larsgroeber commented 1 year ago

We ran into the same issue when implementing the timeout middleware on one of our services.

While -1 is obviously not a valid status code and you could see this an issue with gin itself, I think this library should handle that case, too.

So maybe we can just allow a code == -1 in writer.go?

v0vanec commented 1 year ago

Have exactly the same issue.

wsgomes commented 1 year ago

I fell into the same issue when returning SSEvent. https://github.com/gin-gonic/gin/blob/v1.9.1/context.go#L1073-L1078