Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 Status code is not being set in a middleware function #391

Closed
securisec opened this issue May 17, 2020 · 2 comments
Closed

🐞 Status code is not being set in a middleware function #391

securisec opened this issue May 17, 2020 · 2 comments

Comments

@securisec
Copy link

securisec commented May 17, 2020

Fiber version/commit
1.9.6

Issue description
When setting Status in conjunction with SendFile in a middleware function, in my case, the last handler for a catchall for nonexistent routes, the status is not being set, but instead defaulting to 200 which is default for SendFile. This is slightly different behavior from what is expected based on a different example from the docs. The html is SendFile is correct, but the status code is not.

Expected behavior
The response should contain the set status code. On the attached code snippet, the response code should be 404.

Steps to reproduce
Output using httpie

>> http -p h :3001/nonexistant
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Encoding: gzip
Content-Length: 1025
Content-Type: text/html; charset=utf-8
Date: Sun, 17 May 2020 21:42:30 GMT
Last-Modified: Tue, 12 May 2020 20:40:50 GMT

Code snippet

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  // .. all other middlewares

  // .. all routes

  // last handler before port
  // catchall 404
  app.Use(func(c *fiber.Ctx) {
	c.Status(404).SendFile("./pages/404.html")
  })
  
  app.Listen(port)
}
@welcome
Copy link

welcome bot commented May 17, 2020

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@Fenny
Copy link
Member

Fenny commented May 18, 2020

Welcome @securisec, thank you for your detailed bug report!

I reproduced the same result using SendFile(), this is because Fasthttp overwrites the status code. I applied a fix and this will ship with the next release in v1.10.

For the time being, you could use the following snippet:

app.Use(func(c *fiber.Ctx) {
	c.SendFile("./pages/404.html")
	c.Status(404)
})

I will leave this issue open until we tag v1.10 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants