Class: Html2rss::RequestService::PuppetCommander::NavigationGuards

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/request_service/puppet_commander/navigation_guards.rb

Overview

Owns Puppeteer request interception, deferred navigation errors, main-frame filtering, and redirect-chain validation via Html2rss::RequestService::Policy.

Does not re-own Policy rules — only calls validate_*! on ctx.policy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, skip_request_resources:) ⇒ NavigationGuards

Returns a new instance of NavigationGuards.

Parameters:

  • ctx (Context)

    request context providing policy and origin

  • skip_request_resources (Set<String>)

    resource types to abort



28
29
30
31
32
33
34
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 28

def initialize(ctx:, skip_request_resources:)
  @ctx = ctx
  @skip_request_resources = skip_request_resources
  @navigation_error = nil
  @latest_navigation_response = nil
  @main_frame = nil
end

Instance Attribute Details

#latest_navigation_responsePuppeteer::HTTPResponse? (readonly)

Returns latest main-frame navigation response.

Returns:

  • (Puppeteer::HTTPResponse, nil)

    latest main-frame navigation response



68
69
70
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 68

def latest_navigation_response
  @latest_navigation_response
end

Class Method Details

.response_url(navigation_response, fallback_url) ⇒ Html2rss::Url

Resolves the final response URL for Response building and IP validation.

Parameters:

  • navigation_response (Puppeteer::HTTPResponse, nil)
  • fallback_url (String, Html2rss::Url)

Returns:



19
20
21
22
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 19

def response_url(navigation_response, fallback_url)
  raw_url = navigation_response&.url || fallback_url.to_s
  Html2rss::Url.from_absolute(raw_url)
end

Instance Method Details

#begin_navigation!void

This method returns an undefined value.

Clears deferred error and latest navigation response before goto.



52
53
54
55
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 52

def begin_navigation!
  @navigation_error = nil
  @latest_navigation_response = nil
end

#install!(page) ⇒ void

This method returns an undefined value.

Captures the main frame and wires request/response interceptors.

Parameters:

  • page (Puppeteer::Page)

    browser page



41
42
43
44
45
46
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 41

def install!(page)
  @main_frame = page.main_frame if page.respond_to?(:main_frame)
  page.request_interception = true
  page.on('request') { |request| handle_request(request) }
  page.on('response') { |response| handle_response(response) }
end

#raise_deferred_error!void

This method returns an undefined value.

Re-raises a deferred navigation error when one was captured.

Raises:

  • (Html2rss::Error)

    when a navigation request or response validation failed



62
63
64
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 62

def raise_deferred_error!
  raise @navigation_error if @navigation_error
end

#validate_final!(navigation_response) ⇒ void

This method returns an undefined value.

Validates the remote IP of a navigation response via Policy.

Parameters:

  • navigation_response (Puppeteer::HTTPResponse, nil)


75
76
77
78
# File 'lib/html2rss/request_service/puppet_commander/navigation_guards.rb', line 75

def validate_final!(navigation_response)
  final_url = self.class.response_url(navigation_response, ctx.url)
  ctx.policy.validate_remote_ip!(ip: remote_ip(navigation_response), url: final_url)
end