Method: MiddleSquid::Actions#intercept

Defined in:
lib/middle_squid/actions.rb

#intercept {|req, res| ... } ⇒ Object

Note:

With great power comes great responsibility. Please respect the privacy of your users.

Hijack the request and generate a dynamic reply. This can be used to skip landing pages, change the behaviour of a website depending on the browser’s headers or to generate an entire virtual website using your favorite Rack framework.

The block is called inside a fiber. If the return value is a Rack triplet, it will be sent to the browser.

Examples:

Hello World

run lambda {|uri, extras|
  intercept {|req, res|
    [200, {}, 'Hello World']
  }
}

Yield Parameters:

  • req (Rack::Request)

    the browser request

  • res (Thin::AsyncResponse)

    the response to send back

Yield Returns:

  • Rack triplet or anything else

Raises:

  • (ArgumentError)

See Also:



61
62
63
64
65
66
67
# File 'lib/middle_squid/actions.rb', line 61

def intercept(&block)
  raise ArgumentError, 'no block given' unless block_given?

  token = server.token_for block

  replace_by "http://#{server.host}:#{server.port}/#{token}"
end