forked from halo-sigs/plugin-injection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement path matching logic for HTML injection (halo-sigs#11)
#### What type of PR is this? /kind feature #### What this PR does / why we need it: 此 PR 增加路径匹配逻辑,用户访问特定的页面时,检测到匹配该页面路径的所有启用的代码片段,完成注入 #### Which issue(s) this PR fixes: Fixes halo-sigs#10 #### Does this PR introduce a user-facing change? ```release-note None ```
- Loading branch information
Showing
4 changed files
with
39 additions
and
11 deletions.
There are no files selected for viewing
42 changes: 35 additions & 7 deletions
42
src/main/java/run/halo/injection/AbstractHtmlProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,46 @@ | ||
package run.halo.injection; | ||
|
||
import java.util.Set; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.server.PathContainer; | ||
import org.springframework.util.RouteMatcher; | ||
import org.springframework.web.util.pattern.PathPatternParser; | ||
import org.springframework.web.util.pattern.PathPatternRouteMatcher; | ||
import org.springframework.web.util.pattern.PatternParseException; | ||
import org.thymeleaf.context.Contexts; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.web.IWebRequest; | ||
|
||
@Slf4j | ||
public abstract class AbstractHtmlProcessor { | ||
protected static final String TEMPLATE_ID_VARIABLE = "_templateId"; | ||
private final RouteMatcher routeMatcher = createRouteMatcher(); | ||
|
||
protected boolean isContentTemplate(ITemplateContext context) { | ||
return "post".equals(context.getVariable(TEMPLATE_ID_VARIABLE)) | ||
|| "page".equals(context.getVariable(TEMPLATE_ID_VARIABLE)); | ||
private RouteMatcher createRouteMatcher() { | ||
PathPatternParser parser = new PathPatternParser(); | ||
parser.setPathOptions(PathContainer.Options.HTTP_PATH); | ||
return new PathPatternRouteMatcher(parser); | ||
} | ||
|
||
// 匹配路径接口 | ||
protected boolean isRequestPathMatchingRoute(String requestRoute, Set<String> pageRules) { | ||
return true; | ||
// 匹配路径 | ||
protected boolean isRequestPathMatchingRoute(ITemplateContext context, Set<String> pageRules) { | ||
if (!Contexts.isWebContext(context)) { | ||
return false; | ||
} | ||
IWebRequest request = Contexts.asWebContext(context).getExchange().getRequest(); | ||
String requestPath = request.getRequestPath(); | ||
RouteMatcher.Route requestRoute = routeMatcher.parseRoute(requestPath); | ||
|
||
// 遍历 pageRules 中的路径模式,检查是否有匹配的 | ||
for (String pathPattern : pageRules) { | ||
try { | ||
if (routeMatcher.match(pathPattern, requestRoute)) { | ||
return true; | ||
} | ||
} catch (PatternParseException e) { | ||
// ignore | ||
log.warn("Parse route pattern [{}] failed", pathPattern, e); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters