Skip to content

context.Path() isn't properly set for routes that don't exist #794

Closed
@mathvav

Description

@mathvav

Description

context.Path() isn't accurate for 404 pages. From playing around, it seems to return whatever was in the context before it was pulled from the pool.

Checklist

  • Dependencies installed (fresh download of echo AND go1.7.4)
  • No typos
  • Searched existing issues and docs

Working code to debug

package main

import (
	"net/http"
	"github.com/labstack/echo"
)

func main() {
	e := echo.New()
	e.GET("/a", func(c echo.Context) error {
		return c.String(http.StatusOK, c.Path())
	})
	e.GET("/b", func(c echo.Context) error {
		return c.String(http.StatusOK, c.Path())
	})
	e.HTTPErrorHandler = func(r error, c echo.Context) {
		c.String(http.StatusOK, c.Path())
	}
	e.Logger.Fatal(e.Start(":1111"))
}

Steps to reproduce

Go to localhost:1111/404 (route does NOT exist): the string sent to the browser is always blank (should be /404).

After visiting other URLs that do exist (such as localhost:1111/a—which sends /a to the browser as it should) and going back to the localhost:1111/404 page, it randomly switches between the paths of the pages that do exist that you've visited and a blank page. I'm guessing echo is grabbing a fresh context from the pool and not properly setting the path, so it's likely whatever was in the object before being put back into the pool.

Why this is needed

I send a JSON error response to paths that start with /api/ but I send a HTML response for everywhere else. I found this bug when getting approx. 1/10 requests to my 404 page returning the error as JSON and I began digging to isolate the problem.

Version/commit

Go 1.7.4.

Just downloaded it again ten minutes ago, so eb7ebca.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions