123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::PermissionsPolicy::Middleware

Relationships & Source Files
Inherits: Object
Defined in: actionpack/lib/action_dispatch/http/permissions_policy.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(app) ⇒ Middleware

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/permissions_policy.rb', line 31

def initialize(app)
  @app = app
end

Instance Method Details

#call(env)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/permissions_policy.rb', line 35

def call(env)
  _, headers, _ = response = @app.call(env)

  return response unless html_response?(headers)
  return response if policy_present?(headers)

  request = ActionDispatch::Request.new(env)

  if policy = request.permissions_policy
    headers[ActionDispatch::Constants::FEATURE_POLICY] = policy.build(request.controller_instance)
  end

  if policy_empty?(policy)
    headers.delete(ActionDispatch::Constants::FEATURE_POLICY)
  end

  response
end

#html_response?(headers) ⇒ Boolean (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/permissions_policy.rb', line 55

def html_response?(headers)
  if content_type = headers[Rack::CONTENT_TYPE]
    content_type.include?("html")
  end
end

#policy_empty?(policy) ⇒ Boolean (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/permissions_policy.rb', line 65

def policy_empty?(policy)
  policy&.directives&.empty?
end

#policy_present?(headers) ⇒ Boolean (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/http/permissions_policy.rb', line 61

def policy_present?(headers)
  headers[ActionDispatch::Constants::FEATURE_POLICY]
end