Module: MiddleSquid::Actions

Included in:
Runner
Defined in:
lib/middle_squid/actions.rb

Predefined Actions collapse

Instance Method Details

#acceptObject

Allow the request to pass through. This is the default action.

Examples:

Whitelist a domain

run lambda {|uri, extras|
  accept if uri.host == 'github.com'
}


12
13
14
# File 'lib/middle_squid/actions.rb', line 12

def accept
  action :accept
end

#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

#redirect_to(url, status: 301) ⇒ Object

Redirect the browser to another URL.

Examples:

Redirect google.com to duckduckgo.com

run lambda {|uri, extras|
  redirect_to "http://duckduckgo.com/#{uri.request_uri}" if uri.host == 'google.com'
}

Parameters:



24
25
26
# File 'lib/middle_squid/actions.rb', line 24

def redirect_to(url, status: 301)
  action :redirect, status: status, url: url
end

#replace_by(url) ⇒ Object

Serve another page in place of the requested one. Avoid in favor of #redirect_to when possible.

Examples:

Block Google advertisements

run lambda {|uri, extras|
  redirect_to 'http://webserver.lan/blocked.html' if uri.host == 'ads.google.com'
}

Parameters:

  • url (String)

    the substitute url



36
37
38
# File 'lib/middle_squid/actions.rb', line 36

def replace_by(url)
  action :replace, url: url
end