Class: WrapItRuby::Middleware::RootRelativeProxyMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/wrap_it_ruby/middleware/root_relative_proxy_middleware.rb

Overview

Handles root-relative requests (e.g. /logo.png, /api/data) that originate from within a proxied iframe page.

Detects the proxy host from two sources (in priority order):

1. X-Proxy-Host header -- set by the interception script on fetch/XHR.
   Signals the request came from inside the proxy. The actual upstream
   host is resolved from the Referer header.

2. Referer header -- contains /_proxy/{host}/... for requests where the
   browser sends the full path. Works for both scripted requests and
   asset loads (<img>, <link>, <script>).

Rewrites PATH_INFO to /_proxy/hostpath so ProxyMiddleware handles it. Must be inserted BEFORE ProxyMiddleware in the Rack stack.

Constant Summary collapse

PROXY_PREFIX =
"/_proxy"
PROXY_HOST_HEADER =
"HTTP_X_PROXY_HOST"
REFERER_PATTERN =
%r{/_proxy/(?<host>[^/]+)}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RootRelativeProxyMiddleware

Returns a new instance of RootRelativeProxyMiddleware.



26
27
28
# File 'lib/wrap_it_ruby/middleware/root_relative_proxy_middleware.rb', line 26

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wrap_it_ruby/middleware/root_relative_proxy_middleware.rb', line 30

def call(env)
  path = env["PATH_INFO"].to_s

  unless path.start_with?(PROXY_PREFIX)
    host = extract_proxy_host(env)
    if host
      env["PATH_INFO"] = "#{PROXY_PREFIX}/#{host}#{path}"
    end
  end

  @app.call(env)
end