From 5852c67b738fcb04bf3a1f5f6ff6959d4f8df18c Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Mon, 16 Dec 2024 17:39:13 +0800 Subject: [PATCH] fix: missing atom:link with rel=self --- .../java/run/halo/feed/FeedPluginEndpoint.java | 8 ++++++-- .../java/run/halo/feed/RssCacheManager.java | 14 ++++++++------ .../main/java/run/halo/feed/RssXmlBuilder.java | 17 +++++++++++++++++ app/src/test/java/run/halo/feed/RSS2Test.java | 3 +++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/run/halo/feed/FeedPluginEndpoint.java b/app/src/main/java/run/halo/feed/FeedPluginEndpoint.java index 799829d..3504555 100644 --- a/app/src/main/java/run/halo/feed/FeedPluginEndpoint.java +++ b/app/src/main/java/run/halo/feed/FeedPluginEndpoint.java @@ -42,7 +42,9 @@ public class FeedPluginEndpoint { RouterFunction rssRouterFunction() { return RouterFunctions.route() .GET(path("/feed.xml").or(path("/rss.xml")).and(ACCEPT_PREDICATE), - request -> rssCacheManager.get("/rss.xml", postRssProvider.handler(request)) + request -> rssCacheManager.get(request.requestPath().toString(), + postRssProvider.handler(request) + ) .flatMap(this::buildResponse) ) .build(); @@ -116,7 +118,9 @@ record RouteItem(RequestPredicate requestPredicate, } private HandlerFunction buildHandleFunction(RssRouteItem routeItem) { - return request -> rssCacheManager.get(request.path(), routeItem.handler(request)) + return request -> rssCacheManager.get(request.requestPath().toString(), + routeItem.handler(request) + ) .flatMap(item -> buildResponse(item)); } diff --git a/app/src/main/java/run/halo/feed/RssCacheManager.java b/app/src/main/java/run/halo/feed/RssCacheManager.java index 02d9a7b..bd02623 100644 --- a/app/src/main/java/run/halo/feed/RssCacheManager.java +++ b/app/src/main/java/run/halo/feed/RssCacheManager.java @@ -25,18 +25,20 @@ public class RssCacheManager { private final SystemInfoGetter systemInfoGetter; private final ReactiveSettingFetcher settingFetcher; - public Mono get(String key, Mono loader) { - return Mono.fromCallable(() -> cache.get(key, () -> generateRssXml(loader) - .doOnNext(xml -> cache.put(key, xml)) - .block() + public Mono get(String requestPath, Mono loader) { + return Mono.fromCallable(() -> cache.get(requestPath, + () -> generateRssXml(requestPath, loader) + .doOnNext(xml -> cache.put(requestPath, xml)) + .block() )) .cache() .subscribeOn(Schedulers.boundedElastic()); } - private Mono generateRssXml(Mono loader) { + private Mono generateRssXml(String requestPath, Mono loader) { var builder = new RssXmlBuilder() - .withGenerator("Halo v2.0"); + .withGenerator("Halo v2.0") + .withRequestPath(requestPath); var rssMono = loader.doOnNext(builder::withRss2); diff --git a/app/src/main/java/run/halo/feed/RssXmlBuilder.java b/app/src/main/java/run/halo/feed/RssXmlBuilder.java index 0d9b732..0580448 100644 --- a/app/src/main/java/run/halo/feed/RssXmlBuilder.java +++ b/app/src/main/java/run/halo/feed/RssXmlBuilder.java @@ -41,6 +41,7 @@ public class RssXmlBuilder { private String generator = "Halo v2.0"; private String extractRssTags; private Instant lastBuildDate = Instant.now(); + private String requestPath; private String externalUrl; public RssXmlBuilder withRss2(RSS2 rss2) { @@ -48,6 +49,11 @@ public RssXmlBuilder withRss2(RSS2 rss2) { return this; } + public RssXmlBuilder withRequestPath(String requestPath) { + this.requestPath = requestPath; + return this; + } + /** * For test. */ @@ -80,12 +86,21 @@ public String toXmlString() { Element root = DocumentHelper.createElement("rss"); root.addAttribute("version", "2.0"); root.addNamespace("dc", "http://purl.org/dc/elements/1.1/"); + root.addNamespace("atom", "http://www.w3.org/2005/Atom"); root.addNamespace("media", "http://search.yahoo.com/mrss/"); document.setRootElement(root); Element channel = root.addElement("channel"); channel.addElement("title").addText(rss2.getTitle()); channel.addElement("link").addText(rss2.getLink()); + if (StringUtils.isNotBlank(requestPath)) { + channel.addElement("atom:link") + .addAttribute("href", UriComponentsBuilder.fromUriString(rss2.getLink()) + .path(requestPath).toUriString() + ) + .addAttribute("rel", "self") + .addAttribute("type", "application/rss+xml"); + } var description = StringUtils.defaultIfBlank(rss2.getDescription(), rss2.getTitle()); var secureDescription = XmlCharUtils.removeInvalidXmlChar(description); @@ -286,6 +301,8 @@ private Long getFileSizeBytes(String url) { return webClient.get() .uri(url) .header(HttpHeaders.USER_AGENT, UA) + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range + .header(HttpHeaders.RANGE, "bytes=0-0") .retrieve() .toBodilessEntity() .map(HttpEntity::getHeaders) diff --git a/app/src/test/java/run/halo/feed/RSS2Test.java b/app/src/test/java/run/halo/feed/RSS2Test.java index 80cb37d..da2330c 100644 --- a/app/src/test/java/run/halo/feed/RSS2Test.java +++ b/app/src/test/java/run/halo/feed/RSS2Test.java @@ -46,6 +46,7 @@ void toXmlString() { var expected = """ title @@ -111,6 +112,7 @@ void extractRssTagsTest() { var expected = """ title @@ -163,6 +165,7 @@ void invalidCharTest() { var expected = """ title