Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feedback
  • Loading branch information
Raul Zavaczki committed Sep 30, 2025
commit 2a6c3fc78429ba416f8fd5f9ee5655b5e0bb8ab1
17 changes: 17 additions & 0 deletions lib/plug/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ defmodule Plug.Router do
The above will match `/hello/foo.json` but not `/hello/foo`.
Other delimiters such as `-`, `@` may be used to denote suffixes.

Identifier matching can be escaped using the `\` character:

get "/hello/\\:greet" do
send_resp(conn, 200, "hello")
end

The above will only match `/hello/:greet`.

Routes allow for globbing which will match the remaining parts
of a route. A glob match is done with the `*` character followed
by the variable name. Typically you prefix the variable name with
Expand All @@ -90,6 +98,15 @@ defmodule Plug.Router do
send_resp(conn, 200, "route after /hello: #{inspect glob}")
end

Similarly to `:identifiers`, globs are also escaped through the
`\` character:

get "/hello/\\*glob" do
send_resp(conn, 200, "this is not a glob route")
end

The above will only match `/hello/*glob`.

Opposite to `:identifiers`, globs do not allow prefix nor suffix
matches.

Expand Down
9 changes: 6 additions & 3 deletions lib/plug/router/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule Plug.Router.Utils do
including the known parameters.
"""
def build_path_clause(path, guard, context \\ nil) when is_binary(path) do
compiled = :binary.compile_pattern(["\\:", ":", "*"])
compiled = :binary.compile_pattern(["\\:", "\\*", ":", "*"])

{params, match, guards, post_match} =
path
Expand All @@ -150,9 +150,12 @@ defmodule Plug.Router.Utils do

[{prefix_size, match_length}] when match_length == 2 ->
suffix_size = byte_size(segment) - prefix_size - 2
<<prefix::binary-size(prefix_size), ?\\, char, suffix::binary-size(suffix_size)>> = segment

<<prefix::binary-size(prefix_size), ?\\, char, suffix::binary-size(suffix_size)>> =
segment

escaped_segment = [prefix <> <<char>> <> suffix | match]
build_path_clause(rest, params, match, guards, post_match, context, compiled)
build_path_clause(rest, params, escaped_segment, guards, post_match, context, compiled)

[{prefix_size, _}] ->
suffix_size = byte_size(segment) - prefix_size - 1
Expand Down
9 changes: 9 additions & 0 deletions test/plug/router/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule Plug.Router.UtilsTest do
end

test "build match with escaped identifiers" do
assert quote(@opts, do: {[], ["foo", ":"]}) == build_path_match("foo/\\:")
assert quote(@opts, do: {[], ["foo", ":id"]}) == build_path_match("/foo/\\:id")
assert quote(@opts, do: {[], ["foo", ":username"]}) == build_path_match("foo/\\:username")

Expand All @@ -83,6 +84,14 @@ defmodule Plug.Router.UtilsTest do
assert quote(@opts, do: {[:glob], ["foo" | glob]}) == build_path_match("foo/*glob")
end

test "build match with escaped glob" do
assert quote(@opts, do: {[], ["*bar"]}) == build_path_match("\\*bar")
assert quote(@opts, do: {[], ["*glob"]}) == build_path_match("/\\*glob")

assert quote(@opts, do: {[], ["foo", "*bar"]}) == build_path_match("/foo/\\*bar")
assert quote(@opts, do: {[], ["foo", "*glob"]}) == build_path_match("foo/\\*glob")
end

test "build invalid match with empty matches" do
assert_raise Plug.Router.InvalidSpecError,
"invalid dynamic path. The characters : and * must be immediately followed by lowercase letters or underscore, got: :",
Expand Down