Class: WrapItRuby::Middleware::ScriptInjectionMiddleware
- Inherits:
-
Object
- Object
- WrapItRuby::Middleware::ScriptInjectionMiddleware
- Defined in:
- lib/wrap_it_ruby/middleware/script_injection_middleware.rb
Overview
Injects interception.js inline into HTML responses from the proxy.
-
Strips Accept-Encoding from proxy requests so upstream sends uncompressed HTML (avoids decompress/recompress just to inject).
-
Buffers HTML responses and injects the script as the first child of <head>.
-
Extracts the proxy host from PATH_INFO and injects it as window.__proxyHost so the script works even when the browser URL doesn’t contain /_proxy/ (e.g. after root-relative navigation).
-
Injects window.__hostingSite so the interception script knows which host is the proxy server itself.
-
Reads the script file once and caches it.
Constant Summary collapse
- PROXY_PREFIX =
"/_proxy/"- SCRIPT_FILE =
File.("../../../app/assets/javascripts/wrap_it_ruby/interception.js", __dir__).freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ ScriptInjectionMiddleware
constructor
A new instance of ScriptInjectionMiddleware.
Constructor Details
#initialize(app) ⇒ ScriptInjectionMiddleware
Returns a new instance of ScriptInjectionMiddleware.
22 23 24 |
# File 'lib/wrap_it_ruby/middleware/script_injection_middleware.rb', line 22 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/wrap_it_ruby/middleware/script_injection_middleware.rb', line 26 def call(env) path = env["PATH_INFO"].to_s if path.start_with?(PROXY_PREFIX) host = path.delete_prefix(PROXY_PREFIX).split("/", 2).first hosting_site = env["HTTP_HOST"] env.delete("HTTP_ACCEPT_ENCODING") status, headers, body = @app.call(env) if html_response?(headers) body = inject_script(body, headers, host, hosting_site) end [status, headers, body] else @app.call(env) end end |