Class: FaradayLocalhostHeader
- Inherits:
-
Object
- Object
- FaradayLocalhostHeader
- Defined in:
- lib/faraday/localhost_header.rb
Overview
Faraday request middleware that processes requests made to domains such as *.lvh.me or *.xip.io. When encountering one, it swaps out the hostname with the raw IP, but populates the ‘Host’ request HTTP header with the original hostname.
This enables usage of lvh.me and xip.io domains even when their DNS service is down, because it completely circumvents any DNS lookup.
Constant Summary collapse
- HOST =
'Host'.freeze
- HOME =
'127.0.0.1'
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #detect_domain_hack(url) ⇒ Object
-
#initialize(app, patterns) ⇒ FaradayLocalhostHeader
constructor
A new instance of FaradayLocalhostHeader.
Constructor Details
#initialize(app, patterns) ⇒ FaradayLocalhostHeader
Returns a new instance of FaradayLocalhostHeader.
14 15 16 17 |
# File 'lib/faraday/localhost_header.rb', line 14 def initialize app, patterns @app = app @patterns = Array(patterns) end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/faraday/localhost_header.rb', line 9 def app @app end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
9 10 11 |
# File 'lib/faraday/localhost_header.rb', line 9 def patterns @patterns end |
Instance Method Details
#call(env) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/faraday/localhost_header.rb', line 19 def call env detect_domain_hack env[:url] do |hostname, ip| env[:url].host = ip env[:request_headers][HOST] = hostname end app.call env end |
#detect_domain_hack(url) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/faraday/localhost_header.rb', line 27 def detect_domain_hack url patterns.each do |re| if url.host =~ re ip = $1 ? $1.chomp('.') : HOME yield url.host, ip return end end end |