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

There are repeated slashes when jumping to index #116

Open
hungtcs opened this issue Dec 10, 2023 · 2 comments
Open

There are repeated slashes when jumping to index #116

hungtcs opened this issue Dec 10, 2023 · 2 comments

Comments

@hungtcs
Copy link

hungtcs commented Dec 10, 2023

The regular match contains a slash, but an extra slash is spliced below.

echo-swagger/swagger.go

Lines 131 to 164 in 5602047

var re = regexp.MustCompile(`^(.*/)([^?].*)?[?|.]*$`)
return func(c echo.Context) error {
if c.Request().Method != http.MethodGet {
return echo.NewHTTPError(http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
}
matches := re.FindStringSubmatch(c.Request().RequestURI)
path := matches[2]
switch filepath.Ext(path) {
case ".html":
c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
case ".css":
c.Response().Header().Set("Content-Type", "text/css; charset=utf-8")
case ".js":
c.Response().Header().Set("Content-Type", "application/javascript")
case ".json":
c.Response().Header().Set("Content-Type", "application/json; charset=utf-8")
case ".yaml":
c.Response().Header().Set("Content-Type", "text/plain; charset=utf-8")
case ".png":
c.Response().Header().Set("Content-Type", "image/png")
}
response := c.Response()
// This check fixes an error introduced here: https://github.com/labstack/echo/blob/8da8e161380fd926d4341721f0328f1e94d6d0a2/response.go#L86-L88
if _, ok := response.Writer.(http.Flusher); ok {
defer response.Flush()
}
switch path {
case "":
_ = c.Redirect(http.StatusMovedPermanently, matches[1]+"/"+"index.html")

So if you access the path /swagger/, you will get /swagger//index.html

@evan-goode
Copy link

This is my workaround FWIW:

swaggerRedirect := func(c echo.Context) error {
    return c.Redirect(http.StatusMovedPermanently, "/api/v1/doc/index.html")
}
e.GET("/api/v1/doc", swaggerRedirect)
e.GET("/api/v1/doc/", swaggerRedirect)
e.GET("/api/v1/doc/*", echoSwagger.WrapHandler)

@kripsy
Copy link

kripsy commented Apr 27, 2024

I have same problem.
I'm going to look to the gin side...

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

No branches or pull requests

3 participants