Skip to content
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
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ RUN apk add --no-cache \
--no-create-home \
--uid "$UID" "$USER" \
&& mkdir -p /app \
&& mkdir -p /app/tmp/rack-cache-body \
&& mkdir -p /app/tmp/rack-cache-meta \
&& chown "$USER":"$USER" -R /app

WORKDIR /app
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'html2rss', '~> 0.14'
# gem 'html2rss', '~> 0.14'
gem 'html2rss', github: 'html2rss/html2rss'

gem 'html2rss-configs', github: 'html2rss/html2rss-configs'

# Use these instead of the two above (uncomment them) when developing locally:
Expand Down
229 changes: 143 additions & 86 deletions Gemfile.lock

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,38 @@ def self.development? = ENV['RACK_ENV'] == 'development'
end

r.on String, String do |folder_name, config_name_with_ext|
handle_html2rss_configs(request, folder_name, config_name_with_ext)
response['Content-Type'] = CONTENT_TYPE_RSS

name = "#{folder_name}/#{File.basename(config_name_with_ext, '.*')}"
config = Html2rss::Configs.find_by_name(name)

if (params = request.params).any?
config[:params] ||= {}
config[:params].merge!(params)
end

feed = Html2rss.feed(config)

HttpCache.expires(response, feed.channel.ttl.to_i * 60, cache_control: 'public')

feed.to_s
end

r.on String do |config_name_with_ext|
handle_local_config_feeds(request, config_name_with_ext)
response['Content-Type'] = CONTENT_TYPE_RSS

config = LocalConfig.find(File.basename(config_name_with_ext, '.*'))

if (params = request.params).any?
config[:params] ||= {}
config[:params].merge!(params)
end

feed = Html2rss.feed(config)

HttpCache.expires(response, feed.channel.ttl.to_i * 60, cache_control: 'public')

feed.to_s
end
end

Expand Down
64 changes: 0 additions & 64 deletions app/html2rss_facade.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/http_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module HttpCache
# @param seconds [Integer]
# @param cache_control [String, nil]
def expires(response, seconds, cache_control: nil)
expires_now(response) and return if seconds <= 0

response['Expires'] = (Time.now + seconds).httpdate

cache_value = "max-age=#{seconds}"
Expand Down
49 changes: 0 additions & 49 deletions app/request_path.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/ssrf_filter_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def execute
response = SsrfFilter.get(ctx.url, headers:)

Html2rss::RequestService::Response.new(body: response.body,
url: ctx.url,
headers: response.to_hash.transform_values(&:first))
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/feeds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ feeds:
selector: "li > div"
title:
selector: "h4"
link:
url:
selector: "a"
extractor: "href"
6 changes: 2 additions & 4 deletions helpers/handle_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ module Web
class App
def handle_error(error) # rubocop:disable Metrics/MethodLength
case error
when Html2rss::Config::ParamsMissing,
when Html2rss::Config::DynamicParams::ParamsMissing,
Roda::RodaPlugins::TypecastParams::Error
set_error_response('Parameters missing or invalid', 422)
when Html2rss::AttributePostProcessors::UnknownPostProcessorName,
Html2rss::ItemExtractors::UnknownExtractorName,
Html2rss::Config::ChannelMissing
when Html2rss::Selectors::PostProcessors::UnknownPostProcessorName
set_error_response('Invalid feed config', 422)
when LocalConfig::NotFound,
Html2rss::Configs::ConfigNotFound
Expand Down
16 changes: 0 additions & 16 deletions helpers/handle_html2rss_configs.rb

This file was deleted.

16 changes: 0 additions & 16 deletions helpers/handle_local_config_feeds.rb

This file was deleted.

27 changes: 9 additions & 18 deletions routes/auto_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,19 @@ class App

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

response['Content-Type'] = CONTENT_TYPE_RSS

url = Addressable::URI.parse Base64.urlsafe_decode64(encoded_url)
rss = Html2rss.auto_source(url, strategy:)
url = Addressable::URI.parse(Base64.urlsafe_decode64(encoded_url))

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

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

HttpCache.expires response,
AutoSource.ttl_in_seconds(rss),
cache_control: 'private, must-revalidate'

xml_content
response['Content-Type'] = CONTENT_TYPE_RSS
response.status = 200
feed.to_xml
end
else
# auto_source feature is disabled
Expand Down
30 changes: 0 additions & 30 deletions spec/html2rss/web/app/html2rss_facade_spec.rb

This file was deleted.

41 changes: 0 additions & 41 deletions spec/html2rss/web/app/request_path_spec.rb

This file was deleted.

5 changes: 3 additions & 2 deletions spec/routes/auto_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def app = described_class

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

it 'responds with Error', :aggregate_failures do
expect(response.status).to eq 422
expect(response.body).to match(/UnknownStrategy/)
expect(response.body).to match(/Html2rss::Config::InvalidConfig/)
end
end
end
Expand Down
Loading