Class: WrapItRuby::Middleware::ProxyMiddleware

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

Overview

Rack middleware that proxies /_proxy/host/path to the upstream host.

Strips frame-blocking headers so responses render inside an iframe. Rewrites Location headers so redirects stay within the proxy. Detects WebSocket upgrades and pipes frames bidirectionally.

Constant Summary collapse

PATTERN =
%r{\A/_proxy/(?<host>[^/]+)(?<path>/.*)?\z}
HOP_HEADERS =
%w[
  connection
  keep-alive
  proxy-authenticate
  proxy-authorization
  te
  trailers
  transfer-encoding
  upgrade
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ProxyMiddleware

Returns a new instance of ProxyMiddleware.



29
30
31
32
# File 'lib/wrap_it_ruby/middleware/proxy_middleware.rb', line 29

def initialize(app)
  @app     = app
  @clients = {}
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wrap_it_ruby/middleware/proxy_middleware.rb', line 34

def call(env)
  PATTERN.match(env["PATH_INFO"].to_s).then do |match|
    if match
      host = match[:host]
      path = match[:path] || "/"

      if websocket?(env)
        proxy_websocket(env, host, path)
      else
        proxy_http(env, host, path)
      end
    else
      @app.call(env)
    end
  end
end