Class: HTMLPipeline::NodeFilter::AssetProxyFilter
- Inherits:
-
HTMLPipeline::NodeFilter
- Object
- Filter
- HTMLPipeline::NodeFilter
- HTMLPipeline::NodeFilter::AssetProxyFilter
- Defined in:
- lib/html_pipeline/node_filter/asset_proxy_filter.rb
Overview
Proxy images/assets to another server, such as [cactus/go-camo](github.com/cactus/go-camo#). Reduces mixed content warnings as well as hiding the customer’s IP address when requesting images. Copies the original img ‘src` to `data-canonical-src` then replaces the `src` with a new url to the proxy server.
Based on github.com/gjtorikian/html-pipeline/blob/v2.14.3/lib/html/pipeline/camo_filter.rb
Constant Summary collapse
- SELECTOR =
Selma::Selector.new(match_element: "img")
Instance Attribute Summary
Attributes inherited from HTMLPipeline::NodeFilter
Attributes inherited from Filter
Class Method Summary collapse
- .compile_allowlist(domain_list) ⇒ Object
- .determine_allowlist(proxy_settings) ⇒ Object
-
.transform_context(context, proxy_settings = {}) ⇒ Object
This helps setup the context.
Instance Method Summary collapse
- #asset_host_allowed?(host) ⇒ Boolean
- #handle_element(element) ⇒ Object
- #selector ⇒ Object
- #validate ⇒ Object
Methods inherited from HTMLPipeline::NodeFilter
call, #html, #initialize, #reset!
Methods inherited from Filter
#base_url, #call, call, #has_ancestor?, #initialize, #needs
Constructor Details
This class inherits a constructor from HTMLPipeline::NodeFilter
Class Method Details
.compile_allowlist(domain_list) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 61 def compile_allowlist(domain_list) return if domain_list.empty? escaped = domain_list.map { |domain| Regexp.escape(domain).gsub("\\*", ".*?") } Regexp.new("^(#{escaped.join("|")})$", Regexp::IGNORECASE) end |
.determine_allowlist(proxy_settings) ⇒ Object
68 69 70 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 68 def determine_allowlist(proxy_settings) proxy_settings[:allowlist] || [] end |
.transform_context(context, proxy_settings = {}) ⇒ Object
This helps setup the context. It’s not needed if you’re always providing all the necessary keys in the context. One example would be to override this and pull the settings from a set of global application settings.
51 52 53 54 55 56 57 58 59 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 51 def transform_context(context, proxy_settings = {}) context[:asset_proxy] = proxy_settings[:url] if proxy_settings[:url] context[:asset_proxy_secret_key] = proxy_settings[:secret_key] if proxy_settings[:secret_key] allowlist = determine_allowlist(proxy_settings) context[:asset_proxy_domain_regexp] ||= compile_allowlist(allowlist) context end |
Instance Method Details
#asset_host_allowed?(host) ⇒ Boolean
43 44 45 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 43 def asset_host_allowed?(host) context[:asset_proxy_domain_regexp] ? context[:asset_proxy_domain_regexp].match?(host) : false end |
#handle_element(element) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 22 def handle_element(element) original_src = element["src"] return unless original_src begin uri = URI.parse(original_src) rescue StandardError return end return if uri.host.nil? && !original_src.start_with?("///") return if asset_host_allowed?(uri.host) element["src"] = asset_proxy_url(original_src) element["data-canonical-src"] = original_src end |
#selector ⇒ Object
18 19 20 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 18 def selector SELECTOR end |
#validate ⇒ Object
39 40 41 |
# File 'lib/html_pipeline/node_filter/asset_proxy_filter.rb', line 39 def validate needs(:asset_proxy, :asset_proxy_secret_key) end |