Skip to content

Commit

Permalink
代码优化 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Sep 29, 2024
1 parent 170800f commit 2996f4d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scx-core/src/main/java/cool/scx/core/ScxHttpRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ScxHttpRouter(Scx scx) {
//设置基本的 handler
this.corsHandler = initCorsHandler(scx.scxOptions().allowedOrigin());
//注册路由
this.corsHandlerRoute = this.route().handler(corsHandler);
this.corsHandlerRoute = this.route(-10000).handler(corsHandler);
}

private static CorsHandler initCorsHandler(String allowedOriginPattern) {
Expand Down
2 changes: 1 addition & 1 deletion scx-ext/src/test/resources/c1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<body>
<div>
<h1>当指向单文件时 所有请求都会被转发给单文件</h1>
<a href="/c2">目录形式</a>
<a href="/c2/">目录形式</a>
<h1 id="h11"></h1>

</div>
Expand Down
6 changes: 6 additions & 0 deletions scx-http/src/main/java/cool/scx/http/routing/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ default RouteWritable route() {
return route;
}

default RouteWritable route(int order) {
var route = Route.of().order(order);
addRoute(route);
return route;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cool.scx.http.routing.handler;

import cool.scx.http.exception.NotFoundException;
import cool.scx.http.routing.RoutingContext;

import java.nio.file.Files;
Expand Down Expand Up @@ -46,7 +47,11 @@ public Path getFilePath(String p) {
if (regularFile) {
return root;
}
var path = root.resolve(p);
var path = root.resolve(p).normalize();
// 1, 重要!!! , 防止访问到上级文件的情况
if (!path.startsWith(root)) {
throw new NotFoundException();
}
if (Files.isDirectory(path)) {
return path.resolve("index.html");
} else {
Expand Down

0 comments on commit 2996f4d

Please sign in to comment.