Skip to content

Rewrite middleware in v4.1.16 no longer supports matching beginning of path #1573

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

Closed
3 tasks done
chlindell opened this issue May 18, 2020 · 1 comment · Fixed by #1588
Closed
3 tasks done

Rewrite middleware in v4.1.16 no longer supports matching beginning of path #1573

chlindell opened this issue May 18, 2020 · 1 comment · Fixed by #1588
Labels

Comments

@chlindell
Copy link

Issue Description

Rewrite middleware was changed in v4.1.16 here:
5428358

After this change it seems it's not possible to explicitly match beginning of path anymore.

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

Before we were using this:

	// Rewrite requests without version to use v1
	e.Pre(echomiddleware.Rewrite(map[string]string{
		"^/abc/*": "/v1/abc/$1",
	}))

where ^ is regexp for beginning of string.

Some examples before and what they got mapped to:

/abc/test -> /v1/abc/test
/v1/abc/test  (same, no rewrite)
/v2/abc/test (same, no rewrite)

Actual behaviour

The new versions re-formats the beginning of line character using regexp.QuoteMeta so it can't be used anymore. Rewrite will change ANY "version prefix" to v1 if removing "^", not just ones without it, e.g.:

/abc/test -> /v1/abc/test
/v2/abc/test -> /v1/abc/test
/not/like/this/abc/test -> /v1/abc/test

which is not what we intended and breaks backwards compatibility.

Steps to reproduce

Working code to debug

package main

import(
	"github.com/labstack/echo/v4"
	echomiddleware "github.com/labstack/echo/v4/middleware"
)

func main() {
	e := echo.New()
	// Rewrite requests without version to use v1.
	// This worked on v4.1.13 but now nothing matches until removing the ^ here.
	// However, that makes it match in the middle of strings.
	e.Pre(echomiddleware.Rewrite(map[string]string{
		"^/abc/*": "/v1/abc/$1",
	}))

	group := e.Group("")
	group.Use(echomiddleware.Logger())
	group.Any("/v1/abc/test", func(c echo.Context) error {
		return c.String(http.StatusOK, "Route: /v1/abc/test")
	})

	e.Logger.Fatal(e.StartServer(e.Server))
}

Version/commit

@stale
Copy link

stale bot commented Jul 18, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 18, 2020
@stale stale bot closed this as completed Jul 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant