Module: Ronin::Web::Server::Routing::ClassMethods

Defined in:
lib/ronin/web/server/routing.rb

Overview

Class methods to be added to the application base class.

Instance Method Summary collapse

Instance Method Details

#any(path, conditions = {}) { ... } ⇒ Object

Route any type of request for a given URL pattern.

Examples:

any '/submit' do
  puts request.inspect
end

Parameters:

  • path (String)

    The URL pattern to handle requests for.

  • conditions (Hash{Symbol => Object}) (defaults to: {})

    Additional routing conditions.

Yields:

  • [] The block that will handle the request.



68
69
70
71
72
73
74
75
# File 'lib/ronin/web/server/routing.rb', line 68

def any(path,conditions={},&block)
  get(path,conditions,&block)
  post(path,conditions,&block)
  put(path,conditions,&block)
  patch(path,conditions,&block)
  delete(path,conditions,&block)
  options(path,conditions,&block)
end

#basic_auth(auth_user, auth_password, realm: 'Restricted') ⇒ Object

Enables Basic-Auth authentication for the entire app.

Examples:

basic_auth 'admin', 's3cr3t'

Parameters:

  • auth_user (String)

    The desired username.

  • auth_password (String)

    The desired password

  • realm (String) (defaults to: 'Restricted')

    The "realm" message to display in the Basic-Auth dialog.



121
122
123
124
125
# File 'lib/ronin/web/server/routing.rb', line 121

def basic_auth(auth_user,auth_password, realm: 'Restricted')
  use Rack::Auth::Basic, realm do |user,password|
    user == auth_user && password == auth_password
  end
end

#default { ... } ⇒ Object

Sets the default route.

Examples:

default do
  status 200
  content_type :html

  %{
  <html>
    <body>
      <center><h1>YOU LOSE THE GAME</h1></center>
    </body>
  </html>
  }
end

Yields:

  • [] The block that will handle all other requests.



99
100
101
102
# File 'lib/ronin/web/server/routing.rb', line 99

def default(&block)
  not_found(&block)
  return self
end

#directory(path, local_dir, conditions = {}) ⇒ Object

Hosts the contents of the directory.

Examples:

directory '/download/', '/tmp/files/'

Parameters:

  • path (String)

    The path the web server will host the directory at.

  • local_dir (String)

    The path to the local directory.

  • conditions (Hash{Symbol => Object}) (defaults to: {})

    Additional routing conditions.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ronin/web/server/routing.rb', line 183

def directory(path,local_dir,conditions={})
  path = path.chomp('/')
  dir  = Rack::File.new(local_dir)

  get("#{path}/*",conditions) do |sub_path|
    response = dir.call(env.merge('PATH_INFO' => "/#{sub_path}"))

    if response[0] == 200 then response
    else                       pass
    end
  end
end

#file(path, local_file, conditions = {}) ⇒ Object

Hosts the contents of a file.

Examples:

file '/robots.txt', '/path/to/my_robots.txt'

Parameters:

  • path (String, Regexp)

    The path the web server will host the file at.

  • local_file (String)

    The path to the local file.

  • conditions (Hash{Symbol => Object}) (defaults to: {})

    Additional routing conditions.



162
163
164
# File 'lib/ronin/web/server/routing.rb', line 162

def file(path,local_file,conditions={})
  get(path,conditions) { send_file(local_file) }
end

#mount(dir, app, conditions = {}) ⇒ Object

Routes all requests within a given directory into another Rack application.

Examples:

mount '/subapp/', SubApp

Parameters:

  • dir (String)

    The directory that requests for will be routed from.

  • app (#call)

    The Rack application to route requests to.

  • conditions (Hash{Symbol => Object}) (defaults to: {})

    Additional routing conditions.



253
254
255
256
257
258
259
# File 'lib/ronin/web/server/routing.rb', line 253

def mount(dir,app,conditions={})
  dir = dir.chomp('/')

  any("#{dir}/?*",conditions) do |sub_path|
    app.call(env.merge('PATH_INFO' => "/#{sub_path}"))
  end
end

#public_dir(path, conditions = {}) ⇒ Object

Hosts the static contents within a given directory.

Examples:

public_dir 'path/to/another/public'

Parameters:

  • path (String)

    The path to a directory to serve static content from.

  • conditions (Hash{Symbol => Object}) (defaults to: {})

    Additional routing conditions.



210
211
212
# File 'lib/ronin/web/server/routing.rb', line 210

def public_dir(path,conditions={})
  directory('/',path,conditions)
end

#redirect(path, url) ⇒ Object

Sets up a 302 Redirect at the given path.

Examples:

redirect '/path', 'https://example.com/'

Parameters:

  • path (String)

    The path the web server will respond to.

  • url (String)

    The URL to redirect to.



141
142
143
# File 'lib/ronin/web/server/routing.rb', line 141

def redirect(path,url)
  get(path) { redirect(url) }
end

#vhost(host, app, conditions = {}) ⇒ Object

Routes all requests for a given virtual host to another Rack application.

Parameters:

  • host (Regexp, String)

    The host name to match against.

  • app (#call)

    The Rack application to route the requests to.

  • conditions (Hash) (defaults to: {})

    Additional routing conditions.



229
230
231
232
233
# File 'lib/ronin/web/server/routing.rb', line 229

def vhost(host,app,conditions={})
  any('*',conditions.merge(host: host)) do
    app.call(env)
  end
end