Skip to content

Commit 1502b1a

Browse files
committed
feat(auto_source): replace stylesheet workaround
Replaced the previous RSS construction with a new feed builder to simplify code and improve maintainability. Updated caching logic to align with the new structure, enhancing response handling for auto-source features. Signed-off-by: Gil Desmarais <[email protected]>
1 parent 6d21e2e commit 1502b1a

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

routes/auto_source.rb

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,19 @@ class App
2121

2222
r.on String, method: :get do |encoded_url|
2323
strategy = (request.params['strategy'] || :ssrf_filter).to_sym
24-
unless Html2rss::RequestService.strategy_registered?(strategy)
25-
raise Html2rss::RequestService::UnknownStrategy
26-
end
2724

28-
response['Content-Type'] = CONTENT_TYPE_RSS
29-
30-
url = Addressable::URI.parse(Base64.urlsafe_decode64(encoded_url)).to_s
31-
rss = Html2rss.auto_source(url, strategy:)
25+
url = Addressable::URI.parse(Base64.urlsafe_decode64(encoded_url))
3226

33-
# Unfortunately, Ruby's rss gem does not provide a direct method to
34-
# add an XML stylesheet to the RSS::RSS object itself.
35-
stylesheet = Html2rss::RssBuilder::Stylesheet.new(href: '/rss.xsl', type: 'text/xsl').to_xml
27+
feed = Html2rss.feed(stylesheets: [{ href: '/rss.xsl', type: 'text/xsl' }],
28+
strategy:,
29+
channel: { url: url.to_s },
30+
auto_source: {})
3631

37-
xml_content = rss.to_xml
38-
xml_content.sub!(/^<\?xml version="1.0" encoding="UTF-8"\?>/,
39-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n#{stylesheet}")
32+
HttpCache.expires(response, AutoSource.ttl_in_seconds(feed), cache_control: 'private, must-revalidate')
4033

41-
HttpCache.expires response,
42-
AutoSource.ttl_in_seconds(rss),
43-
cache_control: 'private, must-revalidate'
44-
45-
xml_content
34+
response['Content-Type'] = CONTENT_TYPE_RSS
35+
response.status = 200
36+
feed.to_xml
4637
end
4738
else
4839
# auto_source feature is disabled

spec/routes/auto_source_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def app = described_class
7676

7777
it 'responds successfully', :aggregate_failures do
7878
expect(response).to be_ok
79+
expect(response.status).to eq 200
7980
expect(response.body).to start_with '<?xml version="1.0" encoding="UTF-8"?>'
8081
expect(response.get_header('cache-control')).to eq 'must-revalidate, no-cache, no-store, private, max-age=0'
8182
expect(response.get_header('content-type')).to eq described_class::CONTENT_TYPE_RSS
@@ -93,7 +94,7 @@ def app = described_class
9394

9495
it 'responds with Error', :aggregate_failures do
9596
expect(response.status).to eq 422
96-
expect(response.body).to match(/UnknownStrategy/)
97+
expect(response.body).to match(/Invalid configuration: {:strategy/)
9798
end
9899
end
99100
end

0 commit comments

Comments
 (0)