Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing atom:link with rel=self #51

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions app/src/main/java/run/halo/feed/FeedPluginEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public class FeedPluginEndpoint {
RouterFunction<ServerResponse> 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();
Expand Down Expand Up @@ -116,7 +118,9 @@ record RouteItem(RequestPredicate requestPredicate,
}

private HandlerFunction<ServerResponse> 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));
}

Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/run/halo/feed/RssCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ public class RssCacheManager {
private final SystemInfoGetter systemInfoGetter;
private final ReactiveSettingFetcher settingFetcher;

public Mono<String> get(String key, Mono<RSS2> loader) {
return Mono.fromCallable(() -> cache.get(key, () -> generateRssXml(loader)
.doOnNext(xml -> cache.put(key, xml))
.block()
public Mono<String> get(String requestPath, Mono<RSS2> loader) {
return Mono.fromCallable(() -> cache.get(requestPath,
() -> generateRssXml(requestPath, loader)
.doOnNext(xml -> cache.put(requestPath, xml))
.block()
))
.cache()
.subscribeOn(Schedulers.boundedElastic());
}

private Mono<String> generateRssXml(Mono<RSS2> loader) {
private Mono<String> generateRssXml(String requestPath, Mono<RSS2> loader) {
var builder = new RssXmlBuilder()
.withGenerator("Halo v2.0");
.withGenerator("Halo v2.0")
.withRequestPath(requestPath);

var rssMono = loader.doOnNext(builder::withRss2);

Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/run/halo/feed/RssXmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ 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) {
this.rss2 = rss2;
return this;
}

public RssXmlBuilder withRequestPath(String requestPath) {
this.requestPath = requestPath;
return this;
}

/**
* For test.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions app/src/test/java/run/halo/feed/RSS2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void toXmlString() {
var expected = """
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>title</title>
Expand Down Expand Up @@ -111,6 +112,7 @@ void extractRssTagsTest() {
var expected = """
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>title</title>
Expand Down Expand Up @@ -163,6 +165,7 @@ void invalidCharTest() {
var expected = """
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>title</title>
Expand Down
Loading