Skip to content

Commit

Permalink
refactor: ignore trailing separator for permalink router (#2632)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.0
#### What this PR does / why we need it:
permalink 路由匹配时自动忽略末尾分隔符
例如
/archives  匹配到 /archives
/archives/ 匹配到 /archives

#### Special notes for your reviewer:
/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?

```release-note
None
```
  • Loading branch information
guqing authored Oct 26, 2022
1 parent fa3e0c4 commit 84617c3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/java/run/halo/app/theme/router/RadixRouterTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public HandlerFunction<ServerResponse> match(ServerRequest request) {
* TODO Optimize parameter route matching query.
* Router 仅匹配请求方法和请求的 URL 路径, 形如 /?p=post-name 是 URL query,而不是 URL 路径的一部分。
*/
private String pathToFind(ServerRequest request) {
static String pathToFind(ServerRequest request) {
String requestPath = processRequestPath(request.path());
MultiValueMap<String, String> queryParams = request.queryParams();
// 文章的 permalink 规则需要对 p 参数规则特殊处理
Expand All @@ -141,6 +141,7 @@ private String pathToFind(ServerRequest request) {
requestPath = requestPath.substring(0, i);
}
}
requestPath = StringUtils.removeEnd(requestPath, "/");
return StringUtils.prependIfMissing(requestPath, "/");
}

Expand Down Expand Up @@ -177,7 +178,7 @@ private static Map<String, String> mergePathVariables(Map<String, String> oldVar
}
}

private String processRequestPath(String requestPath) {
private static String processRequestPath(String requestPath) {
String path = StringUtils.prependIfMissing(requestPath, "/");
return UriUtils.decode(path, StandardCharsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
Expand Down Expand Up @@ -64,7 +65,7 @@ public HandlerFunction<ServerResponse> getHandler() {
@Override
public List<String> getRouterPaths(String prefix) {
return List.of(
prefix,
StringUtils.prependIfMissing(prefix, "/"),
PathUtils.combinePath(prefix, "/page/{page:\\d+}"),
PathUtils.combinePath(prefix, "/{year:\\d{4}}"),
PathUtils.combinePath(prefix, "/{year:\\d{4}}/page/{page:\\d+}"),
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/run/halo/app/theme/router/RadixRouterTreeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package run.halo.app.theme.router;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.reactive.function.server.MockServerRequest;

/**
* Tests for {@link RadixRouterTree}.
*
* @author guqing
* @since 2.0.0
*/
class RadixRouterTreeTest {

@Test
void pathToFind() throws URISyntaxException {
MockServerRequest request =
MockServerRequest.builder().uri(new URI("/archives"))
.method(HttpMethod.GET).build();
String path = RadixRouterTree.pathToFind(request);
assertThat(path).isEqualTo("/archives");

request = MockServerRequest.builder().uri(new URI("/archives/"))
.method(HttpMethod.GET).build();
assertThat(RadixRouterTree.pathToFind(request)).isEqualTo("/archives");

request = MockServerRequest.builder().uri(new URI("/archives/page/1"))
.method(HttpMethod.GET).build();
assertThat(RadixRouterTree.pathToFind(request)).isEqualTo("/archives");

request = MockServerRequest.builder().uri(new URI("/"))
.method(HttpMethod.GET).build();
assertThat(RadixRouterTree.pathToFind(request)).isEqualTo("/");

request = MockServerRequest.builder().uri(new URI("/"))
.queryParam("p", "fake-post")
.method(HttpMethod.GET).build();
assertThat(RadixRouterTree.pathToFind(request)).isEqualTo("/?p=fake-post");
}
}

0 comments on commit 84617c3

Please sign in to comment.